blob: 54c6b4417cacdca3b0c85be99c9aa95ca9b0a512 [file] [log] [blame]
joshualitt523ed6c2015-04-17 09:01:05 -07001/*
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
17namespace skiagm {
18class TextBlobTransforms : public GM {
19public:
20 // This gm tests that textblobs can be translated, rotated, and scaled
21 TextBlobTransforms() {}
22
23protected:
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";
caryclark1818acb2015-07-24 12:09:25 -070031 sk_tool_utils::set_portable_typeface(&paint);
joshualitt523ed6c2015-04-17 09:01:05 -070032
33 SkRect bounds;
34 paint.measureText(text, strlen(text), &bounds);
tfarina567ff2f2015-04-27 07:01:44 -070035 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070036
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
fmalita37283c22016-09-13 10:00:23 -070052 fBlob = builder.make();
joshualitt523ed6c2015-04-17 09:01:05 -070053 }
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
caryclark85693c12015-07-20 10:48:01 -070065 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
joshualitt523ed6c2015-04-17 09:01:05 -070066
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.
caryclark85693c12015-07-20 10:48:01 -070075 //SkColor colors[] = {SK_ColorCYAN, sk_tool_utils::color_to_565(SK_ColorLTGRAY), SK_ColorYELLOW, SK_ColorWHITE};
joshualitt523ed6c2015-04-17 09:01:05 -070076
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
160private:
fmalita37283c22016-09-13 10:00:23 -0700161 sk_sp<SkTextBlob> fBlob;
joshualitt523ed6c2015-04-17 09:01:05 -0700162
mtkleindbfd7ab2016-09-01 11:24:54 -0700163 static constexpr int kWidth = 1000;
164 static constexpr int kHeight = 1200;
joshualitt523ed6c2015-04-17 09:01:05 -0700165
166 typedef GM INHERITED;
167};
168
169//////////////////////////////////////////////////////////////////////////////
170
halcanary385fe4d2015-08-26 13:07:48 -0700171DEF_GM(return new TextBlobTransforms;)
joshualitt523ed6c2015-04-17 09:01:05 -0700172}