Debian Codegraph @

gm/textblobrandomfont.cpp

raw
1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontStyle.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkSurfaceProps.h"
24 #include "include/core/SkTextBlob.h"
25 #include "include/core/SkTypeface.h"
26 #include "tools/ToolUtils.h"
27 #include "tools/fonts/FontToolUtils.h"
28 #include "tools/fonts/RandomScalerContext.h"
29
30 #if defined(SK_GANESH)
31 #include "include/gpu/ganesh/GrDirectContext.h"
32 #include "include/gpu/ganesh/GrRecordingContext.h"
33 #endif
34
35 #include <string.h>
36 #include <utility>
37
38 namespace skiagm {
39 class TextBlobRandomFont : public GM {
40 public:
41 // This gm tests that textblobs can be translated and scaled with a font that returns random
42 // but deterministic masks
43 TextBlobRandomFont() { }
44
45 protected:
46 void onOnceBeforeDraw() override {
47 SkTextBlobBuilder builder;
48
49 const char* text = "The quick brown fox jumps over the lazy dog.";
50
51 SkPaint paint;
52 paint.setAntiAlias(true);
53 paint.setColor(SK_ColorMAGENTA);
54
55 // make textbloben
56 SkFont font;
57 font.setSize(32);
58 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
59
60 // Setup our random scaler context
61 auto typeface = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold());
62 SkASSERT(typeface);
63 font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false));
64
65 SkScalar y = 0;
66 SkRect bounds;
67 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
68 y -= bounds.fTop;
69 ToolUtils::add_to_text_blob(&builder, text, font, 0, y);
70 y += bounds.fBottom;
71
72 // A8
73 const char* bigtext1 = "The quick brown fox";
74 const char* bigtext2 = "jumps over the lazy dog.";
75 font.setSize(160);
76 font.setSubpixel(false);
77 font.setEdging(SkFont::Edging::kAntiAlias);
78 font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds);
79 y -= bounds.fTop;
80 ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y);
81 y += bounds.fBottom;
82
83 font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds);
84 y -= bounds.fTop;
85 ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y);
86 y += bounds.fBottom;
87
88 // color emoji
89 ToolUtils::EmojiTestSample sample = ToolUtils::EmojiSample();
90 if (sample.typeface) {
91 font.setTypeface(sk_make_sp<SkRandomTypeface>(sample.typeface, paint, false));
92 font.measureText(sample.sampleText, strlen(sample.sampleText), SkTextEncoding::kUTF8, &bounds);
93 y -= bounds.fTop;
94 ToolUtils::add_to_text_blob(&builder, sample.sampleText, font, 0, y);
95 y += bounds.fBottom;
96 }
97
98 // build
99 fBlob = builder.make();
100 }
101
102 SkString getName() const override { return SkString("textblobrandomfont"); }
103
104 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
105
106 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
107 bool isGPU = false;
108
109 #if defined(SK_GANESH)
110 isGPU = isGPU || SkToBool(GrAsDirectContext(canvas->recordingContext()));
111 #endif
112
113 #if defined(SK_GRAPHITE)
114 isGPU = isGPU || SkToBool(canvas->recorder());
115 #endif
116
117 if (!isGPU) {
118 *errorMsg = skiagm::GM::kErrorMsg_DrawSkippedGpuOnly;
119 return skiagm::DrawResult::kSkip;
120 }
121
122 // This GM uses ToolUtils::makeSurface which doesn't work well with vias.
123 // This GM uses SkRandomTypeface which doesn't work well with serialization.
124 canvas->drawColor(SK_ColorWHITE);
125
126 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
127 kPremul_SkAlphaType,
128 canvas->imageInfo().refColorSpace());
129 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
130 auto surface(ToolUtils::makeSurface(canvas, info, &props));
131 if (!surface) {
132 *errorMsg = "This test requires a surface";
133 return DrawResult::kFail;
134 }
135
136 SkPaint paint;
137 paint.setAntiAlias(true);
138
139 SkCanvas* surfaceCanvas = surface->getCanvas();
140
141 SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height());
142 SkScalar yOffset = 5;
143
144 canvas->save();
145 // Originally we would alternate between rotating and not to force blob regeneration,
146 // but that code seems to have rotted. Keeping the rotate to match the old GM as
147 // much as possible, and it seems like a reasonable stress test for transformed
148 // color emoji.
149 canvas->rotate(-0.05f);
150 canvas->drawTextBlob(fBlob, 10, yOffset, paint);
151 yOffset += stride;
152 canvas->restore();
153
154 // Rotate in the surface canvas, not the final canvas, to avoid aliasing
155 surfaceCanvas->rotate(-0.05f);
156 surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint);
157 surface->draw(canvas, 0, 0);
158 yOffset += stride;
159
160 #if defined(SK_GANESH)
161 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
162 // free gpu resources and verify
163 dContext->freeGpuResources();
164 }
165 #endif
166
167 canvas->rotate(-0.05f);
168 canvas->drawTextBlob(fBlob, 10, yOffset, paint);
169 yOffset += stride;
170 return DrawResult::kOk;
171 }
172
173 private:
174 sk_sp<SkTextBlob> fBlob;
175
176 inline static constexpr int kWidth = 2000;
177 inline static constexpr int kHeight = 1600;
178
179 using INHERITED = GM;
180 };
181
182 //////////////////////////////////////////////////////////////////////////////
183
184 DEF_GM(return new TextBlobRandomFont;)
185 } // namespace skiagm