blob: 597227b85302734584c38dd492c6bbe761cd9f85 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualitt523ed6c2015-04-17 09:01:05 -070010
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
18namespace skiagm {
19class TextBlobTransforms : public GM {
20public:
21 // This gm tests that textblobs can be translated, rotated, and scaled
22 TextBlobTransforms() {}
23
24protected:
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";
caryclark1818acb2015-07-24 12:09:25 -070032 sk_tool_utils::set_portable_typeface(&paint);
joshualitt523ed6c2015-04-17 09:01:05 -070033
34 SkRect bounds;
35 paint.measureText(text, strlen(text), &bounds);
tfarina567ff2f2015-04-27 07:01:44 -070036 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070037
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
fmalita37283c22016-09-13 10:00:23 -070053 fBlob = builder.make();
joshualitt523ed6c2015-04-17 09:01:05 -070054 }
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
caryclark85693c12015-07-20 10:48:01 -070066 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
joshualitt523ed6c2015-04-17 09:01:05 -070067
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.
caryclark85693c12015-07-20 10:48:01 -070076 //SkColor colors[] = {SK_ColorCYAN, sk_tool_utils::color_to_565(SK_ColorLTGRAY), SK_ColorYELLOW, SK_ColorWHITE};
joshualitt523ed6c2015-04-17 09:01:05 -070077
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
161private:
fmalita37283c22016-09-13 10:00:23 -0700162 sk_sp<SkTextBlob> fBlob;
joshualitt523ed6c2015-04-17 09:01:05 -0700163
mtkleindbfd7ab2016-09-01 11:24:54 -0700164 static constexpr int kWidth = 1000;
165 static constexpr int kHeight = 1200;
joshualitt523ed6c2015-04-17 09:01:05 -0700166
167 typedef GM INHERITED;
168};
169
170//////////////////////////////////////////////////////////////////////////////
171
halcanary385fe4d2015-08-26 13:07:48 -0700172DEF_GM(return new TextBlobTransforms;)
joshualitt523ed6c2015-04-17 09:01:05 -0700173}