Debian Codegraph @

gm/typeface.cpp

raw
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/base/SkTemplates.h"
25 #include "src/core/SkFontPriv.h"
26 #include "tools/Resources.h"
27 #include "tools/fonts/FontToolUtils.h"
28
29 #include <string.h>
30 #include <utility>
31
32 using namespace skia_private;
33
34 static void getGlyphPositions(const SkFont& font, SkSpan<const SkGlyphID> glyphs,
35 SkScalar x, SkScalar y, SkPoint pos[]) {
36 const auto count = glyphs.size();
37 AutoSTMalloc<128, SkScalar> widthStorage(count);
38 SkScalar* widths = widthStorage.get();
39 font.getWidths(glyphs, {widths, count});
40
41 for (size_t i = 0; i < count; ++i) {
42 pos[i].set(x, y);
43 x += widths[i];
44 }
45 }
46
47 static void applyKerning(SkPoint pos[], const int32_t adjustments[], int count,
48 const SkFont& font) {
49 SkScalar scale = font.getSize() / font.getTypeface()->getUnitsPerEm();
50
51 SkScalar globalAdj = 0;
52 for (int i = 0; i < count - 1; ++i) {
53 globalAdj += adjustments[i] * scale;
54 pos[i + 1].fX += globalAdj;
55 }
56 }
57
58 static void drawKernText(SkCanvas* canvas, const void* text, size_t len,
59 SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) {
60 SkTypeface* face = font.getTypeface();
61 if (!face) {
62 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
63 return;
64 }
65
66 AutoSTMalloc<128, SkGlyphID> glyphStorage(len);
67 SkGlyphID* glyphs = glyphStorage.get();
68 size_t glyphCount = font.textToGlyphs(text, len, SkTextEncoding::kUTF8, {glyphs, len});
69 if (glyphCount < 1) {
70 return;
71 }
72
73 AutoSTArray<128, int32_t> adjustmentStorage(glyphCount - 1);
74 int32_t* adjustments = adjustmentStorage.get();
75 if (!face->getKerningPairAdjustments({glyphs, glyphCount}, adjustmentStorage)) {
76 canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
77 return;
78 }
79
80
81 SkTextBlobBuilder builder;
82 auto rec = builder.allocRunPos(font, glyphCount);
83 memcpy(rec.glyphs, glyphs, glyphCount * sizeof(SkGlyphID));
84 getGlyphPositions(font, {glyphs, glyphCount}, x, y, rec.points());
85 applyKerning(rec.points(), adjustments, glyphCount, font);
86
87 canvas->drawTextBlob(builder.make(), 0, 0, paint);
88 }
89
90 static constexpr SkFontStyle gStyles[] = {
91 SkFontStyle::Normal(),
92 SkFontStyle::Bold(),
93 SkFontStyle::Italic(),
94 SkFontStyle::BoldItalic(),
95 };
96
97 constexpr int gStylesCount = std::size(gStyles);
98
99 // TODO(bungeman) delete this GM, as it is no longer useful.
100 class TypefaceStylesGM : public skiagm::GM {
101 sk_sp<SkTypeface> fFaces[gStylesCount];
102 bool fApplyKerning;
103
104 public:
105 TypefaceStylesGM(bool applyKerning) : fApplyKerning(applyKerning) {}
106
107 protected:
108 void onOnceBeforeDraw() override {
109 for (int i = 0; i < gStylesCount; i++) {
110 fFaces[i] = ToolUtils::CreateTestTypeface(nullptr, gStyles[i]);
111 }
112 }
113
114 SkString getName() const override {
115 SkString name("typefacestyles");
116 if (fApplyKerning) {
117 name.append("_kerning");
118 }
119 return name;
120 }
121
122 SkISize getISize() override { return SkISize::Make(640, 480); }
123
124 void onDraw(SkCanvas* canvas) override {
125 // Need to use a font to get dy below.
126 SkFont font = ToolUtils::DefaultFont();
127 font.setSize(30);
128
129 const char* text = fApplyKerning ? "Type AWAY" : "Hamburgefons";
130 const size_t textLen = strlen(text);
131
132 SkScalar x = SkIntToScalar(10);
133 SkScalar dy = font.getMetrics(nullptr);
134 SkASSERT(dy > 0);
135 SkScalar y = dy;
136
137 if (fApplyKerning) {
138 font.setSubpixel(true);
139 } else {
140 font.setLinearMetrics(true);
141 }
142
143 SkPaint paint;
144 for (int i = 0; i < gStylesCount; i++) {
145 SkASSERT(fFaces[i]);
146 font.setTypeface(fFaces[i]);
147 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
148 if (fApplyKerning) {
149 drawKernText(canvas, text, textLen, x + 240, y, font, paint);
150 }
151 y += dy;
152 }
153 }
154
155 private:
156 using INHERITED = skiagm::GM;
157 };
158
159 DEF_GM( return new TypefaceStylesGM(false); )
160 DEF_GM( return new TypefaceStylesGM(true); )
161
162 ////////////////////////////////////////////////////////////////////////////////
163
164 static void draw_typeface_rendering_gm(SkCanvas* canvas, sk_sp<SkTypeface> face, SkGlyphID glyph) {
165 struct AliasType {
166 SkFont::Edging edging;
167 bool inLayer;
168 } constexpr aliasTypes[] {
169 #ifndef SK_BUILD_FOR_IOS
170 // This gm crashes on iOS when drawing an embedded bitmap when requesting aliased rendering.
171 // The crash looks like
172 // libTrueTypeScaler.dylib`<redacted> + 80
173 // stop reason = EXC_BAD_ACCESS (code=EXC_ARM_DA_ALIGN, address=...)
174 // -> 0x330b19d0 <+80>: strd r2, r3, [r5, #36]
175 // 0x330b19d4 <+84>: movs r3, #0x0
176 // 0x330b19d6 <+86>: add r2, sp, #0x28
177 // 0x330b19d8 <+88>: ldr r0, [r4, #0x4]
178 // Disable testing embedded bitmaps on iOS for now.
179 // See skbug.com/40036707 .
180 { SkFont::Edging::kAlias , false },
181 #endif
182 { SkFont::Edging::kAntiAlias , false },
183 { SkFont::Edging::kSubpixelAntiAlias, false },
184 { SkFont::Edging::kAntiAlias , true },
185 { SkFont::Edging::kSubpixelAntiAlias, true },
186 };
187
188 // The hintgasp.ttf is designed for the following sizes to be different.
189 // GASP_DOGRAY 0x0002 0<=ppem<=10
190 // GASP_SYMMETRIC_SMOOTHING 0x0008 0<=ppem<=10
191 // GASP_GRIDFIT 0x0001 11<=ppem<=12
192 // GASP_SYMMETRIC_GRIDFIT 0x0004 11<=ppem<=12
193 // GASP_DOGRAY|GASP_GRIDFIT 0x0003 13<=ppem<=14
194 // GASP_SYMMETRIC_SMOOTHING|GASP_SYMMETRIC_GRIDFIT 0x000C 13<=ppem<=14
195 // (neither) 0x0000 15<=ppem
196 // Odd sizes have embedded bitmaps.
197 constexpr SkScalar textSizes[] = { 9, 10, 11, 12, 13, 14, 15, 16 };
198
199 constexpr SkFontHinting hintingTypes[] = {
200 SkFontHinting::kNone,
201 SkFontHinting::kSlight,
202 SkFontHinting::kNormal,
203 SkFontHinting::kFull
204 };
205
206 struct SubpixelType {
207 bool requested;
208 SkVector offset;
209 } constexpr subpixelTypes[] = {
210 { false, { 0.00, 0.00 } },
211 { true , { 0.00, 0.00 } },
212 { true , { 0.25, 0.00 } },
213 { true , { 0.25, 0.25 } },
214 };
215
216 constexpr bool rotateABitTypes[] = { false, true };
217
218 SkScalar y = 0; // The baseline of the previous output
219 {
220 SkPaint paint;
221
222 SkFont font(face);
223 font.setEmbeddedBitmaps(true);
224
225 SkScalar x = 0;
226 SkScalar xMax = x;
227 SkScalar xBase = 0;
228 for (const SubpixelType subpixel : subpixelTypes) {
229 y = 0;
230 font.setSubpixel(subpixel.requested);
231
232 for (const AliasType& alias : aliasTypes) {
233 font.setEdging(alias.edging);
234 SkAutoCanvasRestore acr1(canvas, false);
235 if (alias.inLayer) {
236 canvas->saveLayer(nullptr, &paint);
237 }
238
239 for (const SkScalar& textSize : textSizes) {
240 x = xBase + 5;
241 font.setSize(textSize);
242
243 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
244 y += dy;
245 for (const SkFontHinting& hinting : hintingTypes) {
246 font.setHinting(hinting);
247
248 for (const bool& rotateABit : rotateABitTypes) {
249 SkAutoCanvasRestore acr2(canvas, true);
250 if (rotateABit) {
251 canvas->rotate(2, x + subpixel.offset.x(),
252 y + subpixel.offset.y());
253 }
254 canvas->drawSimpleText(&glyph, sizeof(glyph), SkTextEncoding::kGlyphID,
255 x + subpixel.offset.x(),
256 y + subpixel.offset.y(), font, paint);
257
258 SkScalar dx = SkScalarCeilToScalar(font.measureText(
259 &glyph, sizeof(glyph), SkTextEncoding::kGlyphID)) + 5;
260 x += dx;
261 xMax = std::max(x, xMax);
262 }
263 }
264 }
265 y += 10;
266 }
267 xBase = xMax;
268 }
269 }
270
271 constexpr struct StyleTests {
272 SkPaint::Style style;
273 SkScalar strokeWidth;
274 } styleTypes[] = {
275 { SkPaint::kFill_Style, 0.0f},
276 { SkPaint::kStroke_Style, 0.0f},
277 { SkPaint::kStroke_Style, 0.5f},
278 { SkPaint::kStrokeAndFill_Style, 1.0f},
279 };
280
281 constexpr bool fakeBoldTypes[] = { false, true };
282
283 {
284 SkPaint paint;
285
286 SkFont font(face, 16);
287
288 SkScalar x = 0;
289 for (const bool& fakeBold : fakeBoldTypes) {
290 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
291 y += dy;
292 x = 5;
293
294 font.setEmbolden(fakeBold);
295 for (const AliasType& alias : aliasTypes) {
296 font.setEdging(alias.edging);
297 SkAutoCanvasRestore acr(canvas, false);
298 if (alias.inLayer) {
299 canvas->saveLayer(nullptr, &paint);
300 }
301 for (const StyleTests& style : styleTypes) {
302 paint.setStyle(style.style);
303 paint.setStrokeWidth(style.strokeWidth);
304 canvas->drawSimpleText(&glyph, sizeof(glyph), SkTextEncoding::kGlyphID,
305 x, y, font, paint);
306
307 SkScalar dx = SkScalarCeilToScalar(font.measureText(
308 &glyph, sizeof(glyph), SkTextEncoding::kGlyphID)) + 5;
309 x += dx;
310 }
311 }
312 y += 10;
313 }
314 }
315
316 constexpr struct MaskTests {
317 SkBlurStyle style;
318 SkScalar sigma;
319 } maskTypes[] = {
320 { SkBlurStyle::kNormal_SkBlurStyle, 0.0f},
321 { SkBlurStyle::kSolid_SkBlurStyle, 0.0f},
322 { SkBlurStyle::kOuter_SkBlurStyle, 0.0f},
323 { SkBlurStyle::kInner_SkBlurStyle, 0.0f},
324
325 { SkBlurStyle::kNormal_SkBlurStyle, 0.5f},
326 { SkBlurStyle::kSolid_SkBlurStyle, 0.5f},
327 { SkBlurStyle::kOuter_SkBlurStyle, 0.5f},
328 { SkBlurStyle::kInner_SkBlurStyle, 0.5f},
329
330 { SkBlurStyle::kNormal_SkBlurStyle, 2.0f},
331 { SkBlurStyle::kSolid_SkBlurStyle, 2.0f},
332 { SkBlurStyle::kOuter_SkBlurStyle, 2.0f},
333 { SkBlurStyle::kInner_SkBlurStyle, 2.0f},
334 };
335
336 {
337 SkPaint paint;
338
339 SkFont font(face, 16);
340
341 SkScalar x = 0;
342 {
343 for (const AliasType& alias : aliasTypes) {
344 SkScalar dy = SkScalarCeilToScalar(font.getMetrics(nullptr));
345 y += dy;
346 x = 5;
347
348 font.setEdging(alias.edging);
349 SkAutoCanvasRestore acr(canvas, false);
350 if (alias.inLayer) {
351 canvas->saveLayer(nullptr, &paint);
352 }
353 for (const MaskTests& mask : maskTypes) {
354 paint.setMaskFilter(SkMaskFilter::MakeBlur(mask.style, mask.sigma));
355 canvas->drawSimpleText(&glyph, sizeof(glyph), SkTextEncoding::kGlyphID,
356 x, y, font, paint);
357
358 SkScalar dx = SkScalarCeilToScalar(font.measureText(
359 &glyph, sizeof(glyph), SkTextEncoding::kGlyphID)) + 5;
360 x += dx;
361 }
362 paint.setMaskFilter(nullptr);
363 }
364 y += 10;
365 }
366 }
367 }
368
369 DEF_SIMPLE_GM_CAN_FAIL(typefacerendering, canvas, errMsg, 640, 840) {
370 sk_sp<SkTypeface> face = ToolUtils::CreateTypefaceFromResource("fonts/hintgasp.ttf");
371 if (!face) {
372 return skiagm::DrawResult::kSkip;
373 }
374 draw_typeface_rendering_gm(canvas, face, face->unicharToGlyph('A'));
375
376 // Should draw nothing and not do anything undefined.
377 draw_typeface_rendering_gm(canvas, face, 0xFFFF);
378 return skiagm::DrawResult::kOk;
379 }
380
381 // Type1 fonts don't currently work in Skia on Windows.
382 #ifndef SK_BUILD_FOR_WIN
383
384 DEF_SIMPLE_GM_CAN_FAIL(typefacerendering_pfa, canvas, errMsg, 640, 840) {
385 sk_sp<SkTypeface> face = ToolUtils::CreateTypefaceFromResource("fonts/Roboto2-Regular.pfa");
386 if (!face) {
387 return skiagm::DrawResult::kSkip;
388 }
389 draw_typeface_rendering_gm(canvas, face, face->unicharToGlyph('O'));
390 return skiagm::DrawResult::kOk;
391 }
392
393 DEF_SIMPLE_GM_CAN_FAIL(typefacerendering_pfb, canvas, errMsg, 640, 840) {
394 sk_sp<SkTypeface> face = ToolUtils::CreateTypefaceFromResource("fonts/Roboto2-Regular.pfb");
395 if (!face) {
396 return skiagm::DrawResult::kSkip;
397 }
398 draw_typeface_rendering_gm(canvas, face, face->unicharToGlyph('O'));
399 return skiagm::DrawResult::kOk;
400 }
401
402 #endif
403
404 ////////////////////////////////////////////////////////////////////////////////
405
406 // Exercise different paint styles and embolden, and compare with strokeandfill patheffect
407 DEF_SIMPLE_GM(typeface_styling, canvas, 710, 360) {
408 sk_sp<SkTypeface> face = ToolUtils::CreateTypefaceFromResource("fonts/Roboto-Regular.ttf");
409 if (!face) {
410 face = ToolUtils::DefaultPortableTypeface();
411 }
412 SkFont font(face, 100);
413 font.setEdging(SkFont::Edging::kAntiAlias);
414
415 SkGlyphID glyphs[1] = { font.unicharToGlyph('A') };
416 SkPoint pos[1] = { {0, 0} };
417
418 auto draw = [&](SkPaint::Style style, float width) {
419 // Draws 3 rows:
420 // 1. normal
421 // 2. emboldened
422 // 3. normal(white) on top of emboldened (to show the delta)
423
424 SkPaint paint;
425 paint.setStyle(style);
426 paint.setStrokeWidth(width);
427
428 font.setEmbolden(true);
429 canvas->drawGlyphs(glyphs, pos, {20, 120*2}, font, paint);
430 canvas->drawGlyphs(glyphs, pos, {20, 120*3}, font, paint);
431
432 font.setEmbolden(false);
433 canvas->drawGlyphs(glyphs, pos, {20, 120*1}, font, paint);
434 paint.setColor(SK_ColorYELLOW);
435 canvas->drawGlyphs(glyphs, pos, {20, 120*3}, font, paint);
436 };
437
438 const struct {
439 SkPaint::Style style;
440 float width;
441 } recs[] = {
442 { SkPaint::kFill_Style, 0 },
443 { SkPaint::kStroke_Style, 0 },
444 { SkPaint::kStroke_Style, 3 },
445 { SkPaint::kStrokeAndFill_Style, 0 },
446 { SkPaint::kStrokeAndFill_Style, 3 },
447 };
448
449 canvas->translate(0, -20);
450 for (auto r : recs) {
451 draw(r.style, r.width);
452 canvas->translate(100, 0);
453 }
454 }