blob: e6602116210005e223435545065af1c039ba206c [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"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040022#include "tools/fonts/FontToolUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023
24#include <string.h>
joshualitt523ed6c2015-04-17 09:01:05 -070025
26namespace skiagm {
27class TextBlobTransforms : public GM {
28public:
29 // This gm tests that textblobs can be translated, rotated, and scaled
30 TextBlobTransforms() {}
31
32protected:
33 void onOnceBeforeDraw() override {
34 SkTextBlobBuilder builder;
35
36 // make textblob. To stress distance fields, we choose sizes appropriately
Kevin Lubicke836c3a2023-10-20 06:55:35 -040037 SkFont font(ToolUtils::DefaultPortableTypeface(), 162);
Mike Reed28bd8822018-12-22 22:29:45 -050038 font.setEdging(SkFont::Edging::kAlias);
joshualitt523ed6c2015-04-17 09:01:05 -070039 const char* text = "A";
joshualitt523ed6c2015-04-17 09:01:05 -070040
41 SkRect bounds;
Ben Wagner51e15a62019-05-07 15:38:46 -040042 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
Mike Kleinea3f0142019-03-20 11:12:10 -050043 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070044
45 // Medium
46 SkScalar xOffset = bounds.width() + 5;
Mike Reed28bd8822018-12-22 22:29:45 -050047 font.setSize(72);
joshualitt523ed6c2015-04-17 09:01:05 -070048 text = "B";
Mike Kleinea3f0142019-03-20 11:12:10 -050049 ToolUtils::add_to_text_blob(&builder, text, font, xOffset, 0);
joshualitt523ed6c2015-04-17 09:01:05 -070050
Ben Wagner51e15a62019-05-07 15:38:46 -040051 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
joshualitt523ed6c2015-04-17 09:01:05 -070052 SkScalar yOffset = bounds.height();
53
54 // Small
Mike Reed28bd8822018-12-22 22:29:45 -050055 font.setSize(32);
joshualitt523ed6c2015-04-17 09:01:05 -070056 text = "C";
Mike Kleinea3f0142019-03-20 11:12:10 -050057 ToolUtils::add_to_text_blob(&builder, text, font, xOffset, -yOffset - 10);
joshualitt523ed6c2015-04-17 09:01:05 -070058
59 // build
fmalita37283c22016-09-13 10:00:23 -070060 fBlob = builder.make();
joshualitt523ed6c2015-04-17 09:01:05 -070061 }
62
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000063 SkString getName() const override { return SkString("textblobtransforms"); }
joshualitt523ed6c2015-04-17 09:01:05 -070064
Leandro Lovisolo8f023882023-08-15 21:13:52 +000065 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
joshualitt523ed6c2015-04-17 09:01:05 -070066
67 void onDraw(SkCanvas* canvas) override {
68
Mike Kleind46dce32018-08-16 10:17:03 -040069 canvas->drawColor(SK_ColorGRAY);
joshualitt523ed6c2015-04-17 09:01:05 -070070
71 SkPaint paint;
72
73 SkRect bounds = fBlob->bounds();
74 canvas->translate(20, 20);
75
76 // Colors were chosen to map to pairs of canonical colors. The GPU Backend will cache A8
77 // Texture Blobs based on the canonical color they map to. Canonical colors are used to
78 // create masks. For A8 there are 8 of them.
Mike Kleind46dce32018-08-16 10:17:03 -040079 //SkColor colors[] = {SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorYELLOW, SK_ColorWHITE};
joshualitt523ed6c2015-04-17 09:01:05 -070080
81 SkScalar xOffset = SkScalarCeilToScalar(bounds.width());
82 SkScalar yOffset = SkScalarCeilToScalar(bounds.height());
83 // first translate
84 canvas->translate(xOffset, 2 * yOffset);
85 canvas->drawTextBlob(fBlob, 0, 0, paint);
86 canvas->translate(-xOffset, 0);
87 canvas->drawTextBlob(fBlob, 0, 0, paint);
88 canvas->translate(2 * xOffset, 0);
89 canvas->drawTextBlob(fBlob, 0, 0, paint);
90 canvas->translate(-xOffset, -yOffset);
91 canvas->drawTextBlob(fBlob, 0, 0, paint);
92 canvas->translate(0, 2 * yOffset);
93 canvas->drawTextBlob(fBlob, 0, 0, paint);
94
95 // now rotate
96 canvas->translate(4 * xOffset, -yOffset);
97 canvas->rotate(180.f);
98 canvas->drawTextBlob(fBlob, 0, 0, paint);
99 canvas->rotate(-180.f);
100 canvas->translate(0, -yOffset);
101 canvas->rotate(-180.f);
102 canvas->drawTextBlob(fBlob, 0, 0, paint);
103 canvas->rotate(270.f);
104 canvas->drawTextBlob(fBlob, 0, 0, paint);
105 canvas->rotate(-90.f);
106 canvas->translate(-xOffset, yOffset);
107 canvas->rotate(-90.f);
108 canvas->drawTextBlob(fBlob, 0, 0, paint);
109 canvas->rotate(90.f);
110
111 // and scales
112 canvas->translate(- 3 * xOffset, 3 * yOffset);
113 canvas->scale(1.5f, 1.5f);
114 canvas->drawTextBlob(fBlob, 0, 0, paint);
115 canvas->translate(xOffset, 0);
116 canvas->scale(.25f, .25f);
117 canvas->drawTextBlob(fBlob, 0, 0, paint);
118 canvas->translate(xOffset, 0);
119 canvas->scale(3.f, 2.f);
120 canvas->drawTextBlob(fBlob, 0, 0, paint);
121
122 // finally rotates, scales, and translates together
123 canvas->translate(xOffset, 0);
124 canvas->rotate(23.f);
125 canvas->scale(.33f, .5f);
126 canvas->drawTextBlob(fBlob, 0, 0, paint);
127
128 canvas->rotate(-46.f);
129 canvas->translate(xOffset, 0);
130 canvas->scale(1.2f, 1.1f);
131 canvas->drawTextBlob(fBlob, 0, 0, paint);
132
133 canvas->rotate(46.f);
134 canvas->translate(xOffset, 0);
135 canvas->scale(1.1f, 1.2f);
136 canvas->drawTextBlob(fBlob, 0, 0, paint);
137
138 canvas->rotate(46.f);
139 canvas->translate(xOffset, 0);
140 canvas->scale(.95f, 1.1f);
141 canvas->drawTextBlob(fBlob, 0, 0, paint);
142
143 canvas->rotate(46.f);
144 canvas->translate(xOffset, 0);
145 canvas->scale(1.3f, .7f);
146 canvas->drawTextBlob(fBlob, 0, 0, paint);
147
148 canvas->rotate(46.f);
149 canvas->translate(xOffset, 0);
150 canvas->scale(.8f, 1.1f);
151 canvas->drawTextBlob(fBlob, 0, 0, paint);
152
153 canvas->rotate(10.f);
154 canvas->translate(xOffset, 0);
155 canvas->scale(1.f, 5.f);
156 canvas->drawTextBlob(fBlob, 0, 0, paint);
157
158 canvas->rotate(5.f);
159 canvas->translate(xOffset, 0);
160 canvas->scale(5.f, 1.f);
161 canvas->drawTextBlob(fBlob, 0, 0, paint);
162 }
163
164private:
fmalita37283c22016-09-13 10:00:23 -0700165 sk_sp<SkTextBlob> fBlob;
joshualitt523ed6c2015-04-17 09:01:05 -0700166
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400167 inline static constexpr int kWidth = 1000;
168 inline static constexpr int kHeight = 1200;
joshualitt523ed6c2015-04-17 09:01:05 -0700169
John Stiles7571f9e2020-09-02 22:42:33 -0400170 using INHERITED = GM;
joshualitt523ed6c2015-04-17 09:01:05 -0700171};
172
173//////////////////////////////////////////////////////////////////////////////
174
halcanary385fe4d2015-08-26 13:07:48 -0700175DEF_GM(return new TextBlobTransforms;)
John Stilesa6841be2020-08-06 14:11:56 -0400176} // namespace skiagm