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