1 /*
2 * Copyright 2017 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 "include/core/SkTypes.h"
9
10 #if SK_SUPPORT_ATLAS_TEXT
11
12 #include "gm/gm.h"
13 #include "include/atlastext/SkAtlasTextContext.h"
14 #include "include/atlastext/SkAtlasTextFont.h"
15 #include "include/atlastext/SkAtlasTextRenderer.h"
16 #include "include/atlastext/SkAtlasTextTarget.h"
17 #include "include/core/SkBitmap.h"
18 #include "include/core/SkBlendMode.h"
19 #include "include/core/SkCanvas.h"
20 #include "include/core/SkFont.h"
21 #include "include/core/SkFontStyle.h"
22 #include "include/core/SkFontTypes.h"
23 #include "include/core/SkMatrix.h"
24 #include "include/core/SkPaint.h"
25 #include "include/core/SkPoint.h"
26 #include "include/core/SkRefCnt.h"
27 #include "include/core/SkScalar.h"
28 #include "include/core/SkSize.h"
29 #include "include/core/SkString.h"
30 #include "include/core/SkTypeface.h"
31 #include "include/utils/SkRandom.h"
32 #include "src/utils/SkUTF.h"
33 #include "tools/ToolUtils.h"
34 #include "tools/gpu/atlastext/GLTestAtlasTextRenderer.h"
35 #include "tools/gpu/atlastext/TestAtlasTextRenderer.h"
36
37 #include <memory>
38 #include <utility>
39
40 class GrContext;
41 class GrRenderTargetContext;
42
43 // GM that draws text using the Atlas Text interface offscreen and then blits that to the canvas.
44
draw_string(SkAtlasTextTarget * target,const SkString & text,SkScalar x,SkScalar y,uint32_t color,sk_sp<SkTypeface> typeface,float size)45 static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
46 uint32_t color, sk_sp<SkTypeface> typeface, float size) {
47 if (!text.size()) {
48 return x;
49 }
50 auto atlas_font = SkAtlasTextFont::Make(typeface, size);
51 int cnt = SkUTF::CountUTF8(text.c_str(), text.size());
52 std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]);
53
54 // Using a paint to get the positions for each glyph.
55 SkFont font;
56 font.setSize(size);
57 font.setTypeface(std::move(typeface));
58
59 font.textToGlyphs(text.c_str(), text.size(), SkTextEncoding::kUTF8, glyphs.get(), cnt);
60
61 std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]);
62 font.getWidths(glyphs.get(), cnt, widths.get());
63
64 std::unique_ptr<SkPoint[]> positions(new SkPoint[cnt]);
65 positions[0] = {x, y};
66 for (int i = 1; i < cnt; ++i) {
67 positions[i] = {positions[i - 1].fX + widths[i - 1], y};
68 }
69
70 target->drawText(glyphs.get(), positions.get(), cnt, color, *atlas_font);
71
72 // Return the width of the of draw.
73 return positions[cnt - 1].fX + widths[cnt - 1] - positions[0].fX;
74 }
75
76 class AtlasTextGM : public skiagm::GpuGM {
77 public:
78 AtlasTextGM() = default;
79
80 protected:
onShortName()81 SkString onShortName() override { return SkString("atlastext"); }
82
onISize()83 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
84
onOnceBeforeDraw()85 void onOnceBeforeDraw() override {
86 fRenderer = sk_gpu_test::MakeGLTestAtlasTextRenderer();
87 if (!fRenderer) {
88 return;
89 }
90 fContext = SkAtlasTextContext::Make(fRenderer);
91 auto targetHandle = fRenderer->makeTargetHandle(kSize, kSize);
92 if (!targetHandle) {
93 return;
94 }
95
96 fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle);
97
98 fTypefaces[0] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic());
99 fTypefaces[1] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic());
100 fTypefaces[2] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Normal());
101 fTypefaces[3] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
102 fTypefaces[4] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold());
103 fTypefaces[5] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
104 }
105
onDraw(GrContext *,GrRenderTargetContext *,SkCanvas * canvas,SkString * errorMsg)106 DrawResult onDraw(GrContext*,
107 GrRenderTargetContext*,
108 SkCanvas* canvas,
109 SkString* errorMsg) override {
110 if (!fRenderer) {
111 *errorMsg = "No renderer... probably not supported.";
112 return DrawResult::kSkip;
113 }
114 if (!fTarget || !fTarget->handle()) {
115 *errorMsg = "No target... we can't continue.";
116 return DrawResult::kFail;
117 }
118 fRenderer->clearTarget(fTarget->handle(), 0xFF808080);
119 auto bmp = this->drawText();
120 SkPaint paint;
121 paint.setBlendMode(SkBlendMode::kSrc);
122 canvas->drawBitmap(bmp, 0, 0);
123 return DrawResult::kOk;
124 }
125
126 private:
drawText()127 SkBitmap drawText() {
128 static const int kSizes[] = {8, 13, 18, 23, 30};
129
130 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
131 SkString("abcdefghijklmnopqrstuvwxyz"),
132 SkString("0123456789"),
133 SkString("!@#$%^&*()<>[]{}")};
134 SkScalar x = 0;
135 SkScalar y = 10;
136
137 SkRandom random;
138 do {
139 for (auto s : kSizes) {
140 auto size = 2 * s;
141 for (const auto& typeface : fTypefaces) {
142 for (const auto& text : kTexts) {
143 // Choose a random color but don't let alpha be too small to see.
144 uint32_t color = random.nextU() | 0x40000000;
145 fTarget->save();
146 // Randomly add a little bit of perspective
147 if (random.nextBool()) {
148 SkMatrix persp;
149 persp.reset();
150 persp.setPerspY(0.0005f);
151 persp.preTranslate(-x, -y + s);
152 persp.postTranslate(x, y - s);
153 fTarget->concat(persp);
154 }
155 // Randomly switch between positioning with a matrix vs x, y passed to draw.
156 SkScalar drawX = x, drawY = y;
157 if (random.nextBool()) {
158 fTarget->translate(x, y);
159 drawX = drawY = 0;
160 }
161 x += size +
162 draw_string(fTarget.get(), text, drawX, drawY, color, typeface, size);
163 x = SkScalarCeilToScalar(x);
164 fTarget->restore();
165 // Flush periodically to test continued drawing after a flush.
166 if ((random.nextU() % 8) == 0) {
167 fTarget->flush();
168 }
169 if (x + 100 > kSize) {
170 x = 0;
171 y += SkScalarCeilToScalar(size + 3);
172 if (y > kSize) {
173 fTarget->flush();
174 return fRenderer->readTargetHandle(fTarget->handle());
175 }
176 }
177 }
178 }
179 }
180 } while (true);
181 }
182
183 static constexpr int kSize = 1280;
184
185 sk_sp<SkTypeface> fTypefaces[6];
186 sk_sp<sk_gpu_test::TestAtlasTextRenderer> fRenderer;
187 std::unique_ptr<SkAtlasTextTarget> fTarget;
188 sk_sp<SkAtlasTextContext> fContext;
189
190 typedef GM INHERITED;
191 };
192
193 constexpr int AtlasTextGM::kSize;
194
195 //////////////////////////////////////////////////////////////////////////////
196
197 DEF_GM(return new AtlasTextGM;)
198
199 #endif
200