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 #include "sk_tool_utils.h" 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 SkFont font(sk_tool_utils::create_portable_typeface(), 162); 30 font.setEdging(SkFont::Edging::kAlias); 31 const char* text = "A"; 32 33 SkRect bounds; 34 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); 35 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, 0); 36 37 // Medium 38 SkScalar xOffset = bounds.width() + 5; 39 font.setSize(72); 40 text = "B"; 41 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset, 0); 42 43 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); 44 SkScalar yOffset = bounds.height(); 45 46 // Small 47 font.setSize(32); 48 text = "C"; 49 sk_tool_utils::add_to_text_blob(&builder, text, font, xOffset, -yOffset - 10); 50 51 // build 52 fBlob = builder.make(); 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 65 canvas->drawColor(SK_ColorGRAY); 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. 75 //SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE}; 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 sk_sp<SkTextBlob> fBlob; 162 163 static constexpr int kWidth = 1000; 164 static constexpr int kHeight = 1200; 165 166 typedef GM INHERITED; 167 }; 168 169 ////////////////////////////////////////////////////////////////////////////// 170 171 DEF_GM(return new TextBlobTransforms;) 172 } 173