blob: d9ea8b46e9c83b7dce7f3e7e819297ec1461b7cb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkFontTypes.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "tools/ToolUtils.h"
22
23#include <string.h>
joshualitt523ed6c2015-04-17 09:01:05 -070024
25namespace skiagm {
26class TextBlobTransforms : public GM {
27public:
28 // This gm tests that textblobs can be translated, rotated, and scaled
29 TextBlobTransforms() {}
30
31protected:
32 void onOnceBeforeDraw() override {
33 SkTextBlobBuilder builder;
34
35 // make textblob. To stress distance fields, we choose sizes appropriately
Mike Kleinea3f0142019-03-20 11:12:10 -050036 SkFont font(ToolUtils::create_portable_typeface(), 162);
Mike Reed28bd8822018-12-22 22:29:45 -050037 font.setEdging(SkFont::Edging::kAlias);
joshualitt523ed6c2015-04-17 09:01:05 -070038 const char* text = "A";
joshualitt523ed6c2015-04-17 09:01:05 -070039
40 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040041 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050042 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070043
44 // Medium
45 SkScalar xOffset = bounds.width() + 5;
Mike Reed28bd8822018-12-22 22:29:45 -050046 font.setSize(72);
joshualitt523ed6c2015-04-17 09:01:05 -070047 text = "B";
Mike Kleinea3f0142019-03-20 11:12:10 -050048 ToolUtils::add_to_text_blob(&builder, text, font, xOffset, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070049
Ben Wagner51e15a62019-05-07 15:38:46 -040050 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitt523ed6c2015-04-17 09:01:05 -070051 SkScalar yOffset = bounds.height();
52
53 // Small
Mike Reed28bd8822018-12-22 22:29:45 -050054 font.setSize(32);
joshualitt523ed6c2015-04-17 09:01:05 -070055 text = "C";
Mike Kleinea3f0142019-03-20 11:12:10 -050056 ToolUtils::add_to_text_blob(&builder, text, font, xOffset, -yOffset - 10);
joshualitt523ed6c2015-04-17 09:01:05 -070057
58 // build
fmalita37283c22016-09-13 10:00:23 -070059 fBlob = builder.make();
joshualitt523ed6c2015-04-17 09:01:05 -070060 }
61
62 SkString onShortName() override {
63 return SkString("textblobtransforms");
64 }
65
66 SkISize onISize() override {
67 return SkISize::Make(kWidth, kHeight);
68 }
69
70 void onDraw(SkCanvas* canvas) override {
71
Mike Kleind46dce32018-08-16 10:17:03 -040072 canvas->drawColor(SK_ColorGRAY);
joshualitt523ed6c2015-04-17 09:01:05 -070073
74 SkPaint paint;
75
76 SkRect bounds = fBlob->bounds();
77 canvas->translate(20, 20);
78
79 // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8
80 // Texture Blobs based on the canonical color they map to. Canonical colors are used to
81 // create masks. For A8 there are 8 of them.
Mike Kleind46dce32018-08-16 10:17:03 -040082 //SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE};
joshualitt523ed6c2015-04-17 09:01:05 -070083
84 SkScalar xOffset = SkScalarCeilToScalar(bounds.width());
85 SkScalar yOffset = SkScalarCeilToScalar(bounds.height());
86 // first translate
87 canvas->translate(xOffset, 2 * yOffset);
88 canvas->drawTextBlob(fBlob, 0, 0, paint);
89 canvas->translate(-xOffset, 0);
90 canvas->drawTextBlob(fBlob, 0, 0, paint);
91 canvas->translate(2 * xOffset, 0);
92 canvas->drawTextBlob(fBlob, 0, 0, paint);
93 canvas->translate(-xOffset, -yOffset);
94 canvas->drawTextBlob(fBlob, 0, 0, paint);
95 canvas->translate(0, 2 * yOffset);
96 canvas->drawTextBlob(fBlob, 0, 0, paint);
97
98 // now rotate
99 canvas->translate(4 * xOffset, -yOffset);
100 canvas->rotate(180.f);
101 canvas->drawTextBlob(fBlob, 0, 0, paint);
102 canvas->rotate(-180.f);
103 canvas->translate(0, -yOffset);
104 canvas->rotate(-180.f);
105 canvas->drawTextBlob(fBlob, 0, 0, paint);
106 canvas->rotate(270.f);
107 canvas->drawTextBlob(fBlob, 0, 0, paint);
108 canvas->rotate(-90.f);
109 canvas->translate(-xOffset, yOffset);
110 canvas->rotate(-90.f);
111 canvas->drawTextBlob(fBlob, 0, 0, paint);
112 canvas->rotate(90.f);
113
114 // and scales
115 canvas->translate(- 3 * xOffset, 3 * yOffset);
116 canvas->scale(1.5f, 1.5f);
117 canvas->drawTextBlob(fBlob, 0, 0, paint);
118 canvas->translate(xOffset, 0);
119 canvas->scale(.25f, .25f);
120 canvas->drawTextBlob(fBlob, 0, 0, paint);
121 canvas->translate(xOffset, 0);
122 canvas->scale(3.f, 2.f);
123 canvas->drawTextBlob(fBlob, 0, 0, paint);
124
125 // finally rotates, scales, and translates together
126 canvas->translate(xOffset, 0);
127 canvas->rotate(23.f);
128 canvas->scale(.33f, .5f);
129 canvas->drawTextBlob(fBlob, 0, 0, paint);
130
131 canvas->rotate(-46.f);
132 canvas->translate(xOffset, 0);
133 canvas->scale(1.2f, 1.1f);
134 canvas->drawTextBlob(fBlob, 0, 0, paint);
135
136 canvas->rotate(46.f);
137 canvas->translate(xOffset, 0);
138 canvas->scale(1.1f, 1.2f);
139 canvas->drawTextBlob(fBlob, 0, 0, paint);
140
141 canvas->rotate(46.f);
142 canvas->translate(xOffset, 0);
143 canvas->scale(.95f, 1.1f);
144 canvas->drawTextBlob(fBlob, 0, 0, paint);
145
146 canvas->rotate(46.f);
147 canvas->translate(xOffset, 0);
148 canvas->scale(1.3f, .7f);
149 canvas->drawTextBlob(fBlob, 0, 0, paint);
150
151 canvas->rotate(46.f);
152 canvas->translate(xOffset, 0);
153 canvas->scale(.8f, 1.1f);
154 canvas->drawTextBlob(fBlob, 0, 0, paint);
155
156 canvas->rotate(10.f);
157 canvas->translate(xOffset, 0);
158 canvas->scale(1.f, 5.f);
159 canvas->drawTextBlob(fBlob, 0, 0, paint);
160
161 canvas->rotate(5.f);
162 canvas->translate(xOffset, 0);
163 canvas->scale(5.f, 1.f);
164 canvas->drawTextBlob(fBlob, 0, 0, paint);
165 }
166
167private:
fmalita37283c22016-09-13 10:00:23 -0700168 sk_sp<SkTextBlob> fBlob;
joshualitt523ed6c2015-04-17 09:01:05 -0700169
mtkleindbfd7ab2016-09-01 11:24:54 -0700170 static constexpr int kWidth = 1000;
171 static constexpr int kHeight = 1200;
joshualitt523ed6c2015-04-17 09:01:05 -0700172
173 typedef GM INHERITED;
174};
175
176//////////////////////////////////////////////////////////////////////////////
177
halcanary385fe4d2015-08-26 13:07:48 -0700178DEF_GM(return new TextBlobTransforms;)
joshualitt523ed6c2015-04-17 09:01:05 -0700179}