joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontTypes.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkTextBlob.h" |
| 20 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "tools/ToolUtils.h" |
| 22 | |
| 23 | #include <string.h> |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 24 | |
| 25 | namespace skiagm { |
| 26 | class TextBlobTransforms : public GM { |
| 27 | public: |
| 28 | // This gm tests that textblobs can be translated, rotated, and scaled |
| 29 | TextBlobTransforms() {} |
| 30 | |
| 31 | protected: |
| 32 | void onOnceBeforeDraw() override { |
| 33 | SkTextBlobBuilder builder; |
| 34 | |
| 35 | // make textblob. To stress distance fields, we choose sizes appropriately |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 36 | SkFont font(ToolUtils::create_portable_typeface(), 162); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 37 | font.setEdging(SkFont::Edging::kAlias); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 38 | const char* text = "A"; |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 39 | |
| 40 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 41 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 42 | ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 43 | |
| 44 | // Medium |
| 45 | SkScalar xOffset = bounds.width() + 5; |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 46 | font.setSize(72); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 47 | text = "B"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 48 | ToolUtils::add_to_text_blob(&builder, text, font, xOffset, 0); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 49 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 50 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 51 | SkScalar yOffset = bounds.height(); |
| 52 | |
| 53 | // Small |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 54 | font.setSize(32); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 55 | text = "C"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 56 | ToolUtils::add_to_text_blob(&builder, text, font, xOffset, -yOffset - 10); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 57 | |
| 58 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 59 | fBlob = builder.make(); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SkString onShortName() override { |
| 63 | return SkString("textblobtransforms"); |
| 64 | } |
| 65 | |
| 66 | SkISize onISize() override { |
| 67 | return SkISize::Make(kWidth, kHeight); |
| 68 | } |
| 69 | |
| 70 | void onDraw(SkCanvas* canvas) override { |
| 71 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 72 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 73 | |
| 74 | SkPaint paint; |
| 75 | |
| 76 | SkRect bounds = fBlob->bounds(); |
| 77 | canvas->translate(20, 20); |
| 78 | |
| 79 | // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8 |
| 80 | // Texture Blobs based on the canonical color they map to. Canonical colors are used to |
| 81 | // create masks. For A8 there are 8 of them. |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 82 | //SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE}; |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 83 | |
| 84 | SkScalar xOffset = SkScalarCeilToScalar(bounds.width()); |
| 85 | SkScalar yOffset = SkScalarCeilToScalar(bounds.height()); |
| 86 | // first translate |
| 87 | canvas->translate(xOffset, 2 * yOffset); |
| 88 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 89 | canvas->translate(-xOffset, 0); |
| 90 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 91 | canvas->translate(2 * xOffset, 0); |
| 92 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 93 | canvas->translate(-xOffset, -yOffset); |
| 94 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 95 | canvas->translate(0, 2 * yOffset); |
| 96 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 97 | |
| 98 | // now rotate |
| 99 | canvas->translate(4 * xOffset, -yOffset); |
| 100 | canvas->rotate(180.f); |
| 101 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 102 | canvas->rotate(-180.f); |
| 103 | canvas->translate(0, -yOffset); |
| 104 | canvas->rotate(-180.f); |
| 105 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 106 | canvas->rotate(270.f); |
| 107 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 108 | canvas->rotate(-90.f); |
| 109 | canvas->translate(-xOffset, yOffset); |
| 110 | canvas->rotate(-90.f); |
| 111 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 112 | canvas->rotate(90.f); |
| 113 | |
| 114 | // and scales |
| 115 | canvas->translate(- 3 * xOffset, 3 * yOffset); |
| 116 | canvas->scale(1.5f, 1.5f); |
| 117 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 118 | canvas->translate(xOffset, 0); |
| 119 | canvas->scale(.25f, .25f); |
| 120 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 121 | canvas->translate(xOffset, 0); |
| 122 | canvas->scale(3.f, 2.f); |
| 123 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 124 | |
| 125 | // finally rotates, scales, and translates together |
| 126 | canvas->translate(xOffset, 0); |
| 127 | canvas->rotate(23.f); |
| 128 | canvas->scale(.33f, .5f); |
| 129 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 130 | |
| 131 | canvas->rotate(-46.f); |
| 132 | canvas->translate(xOffset, 0); |
| 133 | canvas->scale(1.2f, 1.1f); |
| 134 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 135 | |
| 136 | canvas->rotate(46.f); |
| 137 | canvas->translate(xOffset, 0); |
| 138 | canvas->scale(1.1f, 1.2f); |
| 139 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 140 | |
| 141 | canvas->rotate(46.f); |
| 142 | canvas->translate(xOffset, 0); |
| 143 | canvas->scale(.95f, 1.1f); |
| 144 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 145 | |
| 146 | canvas->rotate(46.f); |
| 147 | canvas->translate(xOffset, 0); |
| 148 | canvas->scale(1.3f, .7f); |
| 149 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 150 | |
| 151 | canvas->rotate(46.f); |
| 152 | canvas->translate(xOffset, 0); |
| 153 | canvas->scale(.8f, 1.1f); |
| 154 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 155 | |
| 156 | canvas->rotate(10.f); |
| 157 | canvas->translate(xOffset, 0); |
| 158 | canvas->scale(1.f, 5.f); |
| 159 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 160 | |
| 161 | canvas->rotate(5.f); |
| 162 | canvas->translate(xOffset, 0); |
| 163 | canvas->scale(5.f, 1.f); |
| 164 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 165 | } |
| 166 | |
| 167 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 168 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 169 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 170 | static constexpr int kWidth = 1000; |
| 171 | static constexpr int kHeight = 1200; |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 172 | |
| 173 | typedef GM INHERITED; |
| 174 | }; |
| 175 | |
| 176 | ////////////////////////////////////////////////////////////////////////////// |
| 177 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 178 | DEF_GM(return new TextBlobTransforms;) |
joshualitt | 523ed6c | 2015-04-17 09:01:05 -0700 | [diff] [blame] | 179 | } |