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