1 /*
2 * Copyright 2012 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/SkBlurTypes.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkMaskFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTextBlob.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/core/SkTypes.h"
24 #include "include/private/SkTemplates.h"
25 #include "tools/Resources.h"
26
27 #include <string.h>
28 #include <utility>
29
getGlyphPositions(const SkFont & font,const uint16_t glyphs[],int count,SkScalar x,SkScalar y,SkPoint pos[])30 static void getGlyphPositions(const SkFont& font, const uint16_t glyphs[],
31 int count, SkScalar x, SkScalar y, SkPoint pos[]) {
32 SkAutoSTMalloc<128, SkScalar> widthStorage(count);
33 SkScalar* widths = widthStorage.get();
34 font.getWidths(glyphs, count, widths);
35
36 for (int i = 0; i < count; ++i) {
37 pos[i].set(x, y);
38 x += widths[i];
39 }
40 }
41
applyKerning(SkPoint pos[],const int32_t adjustments[],int count,const SkFont & font)42 static void applyKerning(SkPoint pos[], const int32_t adjustments[], int count,
43 const SkFont& font) {
44 SkScalar scale = font.getSize() / font.getTypefaceOrDefault()->getUnitsPerEm();
45
46 SkScalar globalAdj = 0;
47 for (int i = 0; i < count - 1; ++i) {
48 globalAdj += adjustments[i] * scale;
49 pos[i + 1].fX += globalAdj;
50 }
51 }
52
drawKernText(SkCanvas * canvas,const void * text,size_t len,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)53 static void drawKernText(SkCanvas* canvas, const void* text, size_t len,
54 SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) {
55 SkTypeface* face = font.getTypefaceOrDefault();
56 if (!face) {
57 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
58 return;
59 }
60
61 SkAutoSTMalloc<128, uint16_t> glyphStorage(len);
62 uint16_t* glyphs = glyphStorage.get();
63 int glyphCount = font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
64 if (glyphCount < 1) {
65 return;
66 }
67
68 SkAutoSTMalloc<128, int32_t> adjustmentStorage(glyphCount - 1);
69 int32_t* adjustments = adjustmentStorage.get();
70 if (!face->getKerningPairAdjustments(glyphs, glyphCount, adjustments)) {
71 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
72 return;
73 }
74
75
76 SkTextBlobBuilder builder;
77 auto rec = builder.allocRunPos(font, glyphCount);
78 memcpy(rec.glyphs, glyphs, glyphCount * sizeof(SkGlyphID));
79 getGlyphPositions(font, glyphs, glyphCount, x, y, rec.points());
80 applyKerning(rec.points(), adjustments, glyphCount, font);
81
82 canvas->drawTextBlob(builder.make(), 0, 0, paint);
83 }
84
85 static constexpr SkFontStyle gStyles[] = {
86 SkFontStyle::Normal(),
87 SkFontStyle::Bold(),
88 SkFontStyle::Italic(),
89 SkFontStyle::BoldItalic(),
90 };
91
92 constexpr int gStylesCount = SK_ARRAY_COUNT(gStyles);
93
94 class TypefaceStylesGM : public skiagm::GM {
95 sk_sp<SkTypeface> fFaces[gStylesCount];
96 bool fApplyKerning;
97
98 public:
TypefaceStylesGM(bool applyKerning)99 TypefaceStylesGM(bool applyKerning) : fApplyKerning(applyKerning) {}
100
101 protected:
onOnceBeforeDraw()102 void onOnceBeforeDraw() override {
103 for (int i = 0; i < gStylesCount; i++) {
104 fFaces[i] = SkTypeface::MakeFromName(nullptr, gStyles[i]);
105 }
106 }
107
onShortName()108 SkString onShortName() override {
109 SkString name("typefacestyles");
110 if (fApplyKerning) {
111 name.append("_kerning");
112 }
113 return name;
114 }
115
onISize()116 SkISize onISize() override {
117 return SkISize::Make(640, 480);
118 }
119
onDraw(SkCanvas * canvas)120 void onDraw(SkCanvas* canvas) override {
121 SkFont font;
122 font.setSize(30);
123
124 const char* text = fApplyKerning ? "Type AWAY" : "Hamburgefons";
125 const size_t textLen = strlen(text);
126
127 SkScalar x = SkIntToScalar(10);
128 SkScalar dy = font.getMetrics(nullptr);
129 SkScalar y = dy;
130
131 if (fApplyKerning) {
132 font.setSubpixel(true);
133 } else {
134 font.setLinearMetrics(true);
135 }
136
137 SkPaint paint;
138 for (int i = 0; i < gStylesCount; i++) {
139 font.setTypeface(fFaces[i]);
140 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
141 if (fApplyKerning) {
142 drawKernText(canvas, text, textLen, x + 240, y, font, paint);
143 }
144 y += dy;
145 }
146 }
147
148 private:
149 typedef skiagm::GM INHERITED;
150 };
151
152 DEF_GM( return new TypefaceStylesGM(false); )
DEF_GM(return new TypefaceStylesGM (true);)153 DEF_GM( return new TypefaceStylesGM(true); )
154
155 ////////////////////////////////////////////////////////////////////////////////
156
157 static void draw_typeface_rendering_gm(SkCanvas* canvas, sk_sp<SkTypeface> face,
158 char character = 'A') {
159 struct AliasType {
160 SkFont::Edging edging;
161 bool inLayer;
162 } constexpr aliasTypes[] {
163 #ifndef SK_BUILD_FOR_IOS
164 // This gm crashes on iOS when drawing an embedded bitmap when requesting aliased rendering.
165 // The crash looks like
166 // libTrueTypeScaler.dylib`<redacted> + 80
167 // stop reason = EXC_BAD_ACCESS (code=EXC_ARM_DA_ALIGN, address=...)
168 // -> 0x330b19d0 <+80>: strd r2, r3, [r5, #36]
169 // 0x330b19d4 <+84>: movs r3, #0x0
170 // 0x330b19d6 <+86>: add r2, sp, #0x28
171 // 0x330b19d8 <+88>: ldr r0, [r4, #0x4]
172 // Disable testing embedded bitmaps on iOS for now.
173 // See https://bug.skia.org/5530 .
174 { SkFont::Edging::kAlias , false },
175 #endif
176 { SkFont::Edging::kAntiAlias , false },
177 { SkFont::Edging::kSubpixelAntiAlias, false },
178 { SkFont::Edging::kAntiAlias , true },
179 { SkFont::Edging::kSubpixelAntiAlias, true },
180 };
181
182 // The hintgasp.ttf is designed for the following sizes to be different.
183 // GASP_DOGRAY 0x0002 0<=ppem<=10
184 // GASP_SYMMETRIC_SMOOTHING 0x0008 0<=ppem<=10
185 // GASP_GRIDFIT 0x0001 11<=ppem<=12
186 // GASP_SYMMETRIC_GRIDFIT 0x0004 11<=ppem<=12
187 // GASP_DOGRAY|GASP_GRIDFIT 0x0003 13<=ppem<=14
188 // GASP_SYMMETRIC_SMOOTHING|GASP_SYMMETRIC_GRIDFIT 0x000C 13<=ppem<=14
189 // (neither) 0x0000 15<=ppem
190 // Odd sizes have embedded bitmaps.
191 constexpr SkScalar textSizes[] = { 9, 10, 11, 12, 13, 14, 15, 16 };
192
193 constexpr SkFontHinting hintingTypes[] = {
194 SkFontHinting::kNone,
195 SkFontHinting::kSlight,
196 SkFontHinting::kNormal,
197 SkFontHinting::kFull
198 };
199
200 struct SubpixelType {
201 bool requested;
202 SkVector offset;
203 } constexpr subpixelTypes[] = {
204 { false, { 0.00, 0.00 } },
205 { true , { 0.00, 0.00 } },
206 { true , { 0.25, 0.00 } },
207 { true , { 0.25, 0.25 } },
208 };
209
210 constexpr bool rotateABitTypes[] = { false, true };
211
212 SkScalar y = 0; // The baseline of the previous output
213 {
214 SkPaint paint;
215
216 SkFont font(face);
217 font.setEmbeddedBitmaps(true);
218
219 SkScalar x = 0;
220 SkScalar xMax = x;
221 SkScalar xBase = 0;
222 for (const SubpixelType subpixel : subpixelTypes) {
223 y = 0;
224 font.setSubpixel(subpixel.requested);
225
226 for (const AliasType& alias : aliasTypes) {
227 font.setEdging(alias.edging);
228 SkAutoCanvasRestore acr(canvas, false);
229 if (alias.inLayer) {
230 canvas->saveLayer(nullptr, &paint);
231 }
232
233 for (const SkScalar& textSize : textSizes) {
234 x = xBase + 5;
235 font.setSize(textSize);
236
237 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
238 y += dy;
239 for (const SkFontHinting& hinting : hintingTypes) {
240 font.setHinting(hinting);
241
242 for (const bool& rotateABit : rotateABitTypes) {
243 SkAutoCanvasRestore acr(canvas, true);
244 if (rotateABit) {
245 canvas->rotate(2, x + subpixel.offset.x(),
246 y + subpixel.offset.y());
247 }
248 canvas->drawSimpleText(&character, 1, SkTextEncoding::kUTF8,
249 x + subpixel.offset.x(),
250 y + subpixel.offset.y(), font, paint);
251
252 SkScalar dx = SkScalarCeilToScalar(
253 font.measureText(&character, 1, SkTextEncoding::kUTF8)) + 5;
254 x += dx;
255 xMax = SkTMax(x, xMax);
256 }
257 }
258 }
259 y += 10;
260 }
261 xBase = xMax;
262 }
263 }
264
265 constexpr struct StyleTests {
266 SkPaint::Style style;
267 SkScalar strokeWidth;
268 } styleTypes[] = {
269 { SkPaint::kFill_Style, 0.0f},
270 { SkPaint::kStroke_Style, 0.0f},
271 { SkPaint::kStroke_Style, 0.5f},
272 { SkPaint::kStrokeAndFill_Style, 1.0f},
273 };
274
275 constexpr bool fakeBoldTypes[] = { false, true };
276
277 {
278 SkPaint paint;
279
280 SkFont font(face, 16);
281
282 SkScalar x = 0;
283 for (const bool& fakeBold : fakeBoldTypes) {
284 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
285 y += dy;
286 x = 5;
287
288 font.setEmbolden(fakeBold);
289 for (const AliasType& alias : aliasTypes) {
290 font.setEdging(alias.edging);
291 SkAutoCanvasRestore acr(canvas, false);
292 if (alias.inLayer) {
293 canvas->saveLayer(nullptr, &paint);
294 }
295 for (const StyleTests& style : styleTypes) {
296 paint.setStyle(style.style);
297 paint.setStrokeWidth(style.strokeWidth);
298 canvas->drawSimpleText(&character, 1, SkTextEncoding::kUTF8, x, y, font, paint);
299
300 SkScalar dx = SkScalarCeilToScalar(font.measureText(&character, 1,
301 SkTextEncoding::kUTF8)) + 5;
302 x += dx;
303 }
304 }
305 y += 10;
306 }
307 }
308
309 constexpr struct MaskTests {
310 SkBlurStyle style;
311 SkScalar sigma;
312 } maskTypes[] = {
313 { SkBlurStyle::kNormal_SkBlurStyle, 0.0f},
314 { SkBlurStyle::kSolid_SkBlurStyle, 0.0f},
315 { SkBlurStyle::kOuter_SkBlurStyle, 0.0f},
316 { SkBlurStyle::kInner_SkBlurStyle, 0.0f},
317
318 { SkBlurStyle::kNormal_SkBlurStyle, 0.5f},
319 { SkBlurStyle::kSolid_SkBlurStyle, 0.5f},
320 { SkBlurStyle::kOuter_SkBlurStyle, 0.5f},
321 { SkBlurStyle::kInner_SkBlurStyle, 0.5f},
322
323 { SkBlurStyle::kNormal_SkBlurStyle, 2.0f},
324 { SkBlurStyle::kSolid_SkBlurStyle, 2.0f},
325 { SkBlurStyle::kOuter_SkBlurStyle, 2.0f},
326 { SkBlurStyle::kInner_SkBlurStyle, 2.0f},
327 };
328
329 {
330 SkPaint paint;
331
332 SkFont font(face, 16);
333
334 SkScalar x = 0;
335 {
336 for (const AliasType& alias : aliasTypes) {
337 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
338 y += dy;
339 x = 5;
340
341 font.setEdging(alias.edging);
342 SkAutoCanvasRestore acr(canvas, false);
343 if (alias.inLayer) {
344 canvas->saveLayer(nullptr, &paint);
345 }
346 for (const MaskTests& mask : maskTypes) {
347 paint.setMaskFilter(SkMaskFilter::MakeBlur(mask.style, mask.sigma));
348 canvas->drawSimpleText(&character, 1, SkTextEncoding::kUTF8, x, y, font, paint);
349
350 SkScalar dx = SkScalarCeilToScalar(font.measureText(&character, 1,
351 SkTextEncoding::kUTF8)) + 5;
352 x += dx;
353 }
354 paint.setMaskFilter(nullptr);
355 }
356 y += 10;
357 }
358 }
359 }
360
361 DEF_SIMPLE_GM(typefacerendering, canvas, 640, 840) {
362 if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("fonts/hintgasp.ttf")) {
363 draw_typeface_rendering_gm(canvas, std::move(face));
364 }
365 }
366
367 // Type1 fonts don't currently work in Skia on Windows.
368 #ifndef SK_BUILD_FOR_WIN
369
370 DEF_SIMPLE_GM(typefacerendering_pfa, canvas, 640, 840) {
371 if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("fonts/Roboto2-Regular.pfa")) {
372 // This subsetted typeface doesn't have the character 'A'.
373 draw_typeface_rendering_gm(canvas, std::move(face), 'O');
374 }
375 }
376
377 DEF_SIMPLE_GM(typefacerendering_pfb, canvas, 640, 840) {
378 if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("fonts/Roboto2-Regular.pfb")) {
379 draw_typeface_rendering_gm(canvas, std::move(face), 'O');
380 }
381 }
382
383 #endif
384