Debian Codegraph @

gm/fontmgr.cpp

raw
1 /*
2 * Copyright 2013 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/SkFont.h"
12 #include "include/core/SkFontMetrics.h"
13 #include "include/core/SkFontMgr.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkFontTypes.h"
16 #include "include/core/SkGraphics.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPath.h"
19 #include "include/core/SkPathEffect.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkScalar.h"
24 #include "include/core/SkSize.h"
25 #include "include/core/SkString.h"
26 #include "include/core/SkTypeface.h"
27 #include "include/core/SkTypes.h"
28 #include "include/effects/SkDashPathEffect.h"
29 #include "src/core/SkFontPriv.h"
30 #include "tools/SkMetaData.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33
34 #include <utility>
35
36 // limit this just so we don't take too long to draw
37 #define MAX_FAMILIES 30
38
39 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
40 SkScalar y, const SkFont& font) {
41 canvas->drawString(text, x, y, font, SkPaint());
42 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
43 }
44
45 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
46 SkScalar y, const SkFont& origFont, SkFontMgr* fm,
47 const char* fontName, const char* bcp47[], int bcp47Count,
48 const SkFontStyle& fontStyle) {
49 SkFont font = origFont;
50 // find typeface containing the requested character and draw it
51 SkString ch;
52 ch.appendUnichar(character);
53 sk_sp<SkTypeface> typeface(fm->matchFamilyStyleCharacter(fontName, fontStyle,
54 bcp47, bcp47Count, character));
55 font.setTypeface(typeface);
56 x = drawString(canvas, ch, x, y, font) + 20;
57
58 if (nullptr == typeface) {
59 return x;
60 }
61
62 // repeat the process, but this time use the family name of the typeface
63 // from the first pass. This emulates the behavior in Blink where it
64 // it expects to get the same glyph when following this pattern.
65 SkString familyName;
66 typeface->getFamilyName(&familyName);
67 font.setTypeface(fm->legacyMakeTypeface(familyName.c_str(), typeface->fontStyle()));
68 return drawString(canvas, ch, x, y, font) + 20;
69 }
70
71 static const char* zh = "zh";
72 static const char* ja = "ja";
73
74 class FontMgrGM : public skiagm::GM {
75 sk_sp<SkFontMgr> fFM;
76
77 void onOnceBeforeDraw() override {
78 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
79 fFM = ToolUtils::TestFontMgr();
80 }
81
82 SkString getName() const override { return SkString("fontmgr_iter"); }
83
84 SkISize getISize() override { return {1536, 768}; }
85
86 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
87 SkScalar y = 20;
88 SkFont font = ToolUtils::DefaultFont();
89 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
90 font.setSubpixel(true);
91 font.setSize(17);
92
93 SkFontMgr* fm = fFM.get();
94 int count = std::min(fm->countFamilies(), MAX_FAMILIES);
95 if (count == 0) {
96 *errorMsg = "No families in SkFontMgr";
97 return DrawResult::kSkip;
98 }
99
100 for (int i = 0; i < count; ++i) {
101 SkString familyName;
102 fm->getFamilyName(i, &familyName);
103 font.setTypeface(ToolUtils::DefaultTypeface());
104 (void)drawString(canvas, familyName, 20, y, font);
105
106 SkScalar x = 220;
107
108 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
109 for (int j = 0; j < set->count(); ++j) {
110 SkString sname;
111 SkFontStyle fs;
112 set->getStyle(j, &fs, &sname);
113 sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.slant());
114
115 font.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
116 x = drawString(canvas, sname, x, y, font) + 20;
117
118 // check to see that we get different glyphs in japanese and chinese
119 x = drawCharacter(canvas, 0x5203, x, y, font, fm, familyName.c_str(), &zh, 1, fs);
120 x = drawCharacter(canvas, 0x5203, x, y, font, fm, familyName.c_str(), &ja, 1, fs);
121 // check that emoji characters are found
122 x = drawCharacter(canvas, 0x1f601, x, y, font, fm, familyName.c_str(), nullptr,0, fs);
123 }
124 y += 24;
125 }
126 return DrawResult::kOk;
127 }
128 };
129
130 class FontMgrMatchGM : public skiagm::GM {
131 sk_sp<SkFontMgr> fFM;
132
133 void onOnceBeforeDraw() override {
134 fFM = ToolUtils::TestFontMgr();
135 SkGraphics::SetFontCacheLimit(16 * 1024 * 1024);
136 }
137
138 SkString getName() const override { return SkString("fontmgr_match"); }
139
140 SkISize getISize() override { return {640, 1024}; }
141
142 void iterateFamily(SkCanvas* canvas, const SkFont& font, SkFontStyleSet* fset) {
143 SkFont f(font);
144 SkScalar y = 0;
145
146 for (int j = 0; j < fset->count(); ++j) {
147 SkString sname;
148 SkFontStyle fs;
149 fset->getStyle(j, &fs, &sname);
150
151 sname.appendf(" [%d %d]", fs.weight(), fs.width());
152
153 f.setTypeface(sk_sp<SkTypeface>(fset->createTypeface(j)));
154 SkScalar x = 0;
155 x = drawString(canvas, sname, x, y, f) + 20;
156 // check to see that we get different glyphs in japanese and chinese
157 // and the style matches with no name
158 x = drawCharacter(canvas, 0x5203, x, y, font, fFM.get(), nullptr, &zh, 1, fs);
159 x = drawCharacter(canvas, 0x5203, x, y, font, fFM.get(), nullptr, &ja, 1, fs);
160 y += 24;
161 }
162 }
163
164 void exploreFamily(SkCanvas* canvas, const SkFont& font, SkFontStyleSet* fset) {
165 SkFont f(font);
166 SkScalar y = 0;
167
168 for (int weight = 100; weight <= 900; weight += 200) {
169 for (int width = 1; width <= 9; width += 2) {
170 SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant);
171 sk_sp<SkTypeface> face(fset->matchStyle(fs));
172 if (face) {
173 SkString str;
174 str.printf("request [%d %d]", fs.weight(), fs.width());
175 f.setTypeface(std::move(face));
176 SkScalar x = 0;
177 x = drawString(canvas, str, x, y, f) + 20;
178 // check to see that we get different glyphs in japanese and chinese
179 // and the style matches with no name
180 x = drawCharacter(canvas, 0x5203, x, y, font, fFM.get(), nullptr, &zh, 1, fs);
181 x = drawCharacter(canvas, 0x5203, x, y, font, fFM.get(), nullptr, &ja, 1, fs);
182 y += 24;
183 }
184 }
185 }
186 }
187
188 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
189 SkFont font = ToolUtils::DefaultFont();
190 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
191 font.setSubpixel(true);
192 font.setSize(17);
193
194 const char* gNames[] = {
195 "Helvetica Neue", "Arial", "sans", "Roboto"
196 };
197
198 sk_sp<SkFontStyleSet> fset;
199 for (size_t i = 0; i < std::size(gNames); ++i) {
200 fset = fFM->matchFamily(gNames[i]);
201 if (fset->count() > 0) {
202 break;
203 }
204 }
205 if (!fset || fset->count() == 0) {
206 *errorMsg = "No SkFontStyleSet";
207 return DrawResult::kSkip;
208 }
209
210 canvas->translate(20, 40);
211 this->exploreFamily(canvas, font, fset.get());
212 canvas->translate(350, 0);
213 this->iterateFamily(canvas, font, fset.get());
214 return DrawResult::kOk;
215 }
216 };
217
218 class FontMgrBoundsGM : public skiagm::GM {
219 public:
220 FontMgrBoundsGM(float scale, float skew) : fScaleX(scale) , fSkewX(skew) {}
221
222 private:
223 SkString getName() const override {
224 if (fScaleX != 1 || fSkewX != 0) {
225 return SkStringPrintf("fontmgr_bounds_%g_%g", fScaleX, fSkewX);
226 }
227 return SkString("fontmgr_bounds");
228 }
229
230 void onOnceBeforeDraw() override { fFM = ToolUtils::TestFontMgr(); }
231
232 bool onGetControls(SkMetaData* controls) override {
233 controls->setBool("Label Bounds", fLabelBounds);
234 return true;
235 }
236
237 void onSetControls(const SkMetaData& controls) override {
238 controls.findBool("Label Bounds", &fLabelBounds);
239 }
240
241 static SkRect show_bounds(SkCanvas* canvas, const SkFont& font, SkScalar x, SkScalar y,
242 SkColor boundsColor, bool labelBounds)
243 {
244 SkGlyphID left = 0, right = 0, top = 0, bottom = 0;
245 SkRect min = SkRect::MakeLTRB(SK_ScalarInfinity, SK_ScalarInfinity,
246 SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity);
247 {
248 int numGlyphs = font.getTypeface()->countGlyphs();
249 for (int i = 0; i < numGlyphs; ++i) {
250 SkGlyphID glyphId = i;
251 SkRect cur = font.getBounds(glyphId, nullptr);
252 if (cur.fLeft < min.fLeft ) { min.fLeft = cur.fLeft; left = i; }
253 if (cur.fTop < min.fTop ) { min.fTop = cur.fTop ; top = i; }
254 if (min.fRight < cur.fRight ) { min.fRight = cur.fRight; right = i; }
255 if (min.fBottom < cur.fBottom) { min.fBottom = cur.fBottom; bottom = i; }
256 }
257 }
258
259 SkRect fontBounds = SkFontPriv::GetFontBounds(font);
260
261 SkRect drawBounds = min;
262 drawBounds.join(fontBounds);
263
264 SkAutoCanvasRestore acr(canvas, true);
265 canvas->translate(x - drawBounds.left(), y);
266
267 SkPaint boundsPaint;
268 boundsPaint.setAntiAlias(true);
269 boundsPaint.setColor(boundsColor);
270 boundsPaint.setStyle(SkPaint::kStroke_Style);
271 canvas->drawRect(fontBounds, boundsPaint);
272
273 const SkScalar intervals[] = { 10.f, 10.f };
274 boundsPaint.setPathEffect(SkDashPathEffect::Make(intervals, 0.f));
275 canvas->drawRect(min, boundsPaint);
276
277 SkFontMetrics fm;
278 font.getMetrics(&fm);
279 SkPaint metricsPaint(boundsPaint);
280 metricsPaint.setStyle(SkPaint::kFill_Style);
281 metricsPaint.setAlphaf(0.25f);
282 if ((fm.fFlags & SkFontMetrics::kUnderlinePositionIsValid_Flag) &&
283 (fm.fFlags & SkFontMetrics::kUnderlineThicknessIsValid_Flag))
284 {
285 SkRect underline{ min.fLeft, fm.fUnderlinePosition,
286 min.fRight, fm.fUnderlinePosition + fm.fUnderlineThickness };
287 canvas->drawRect(underline, metricsPaint);
288 }
289
290 if ((fm.fFlags & SkFontMetrics::kStrikeoutPositionIsValid_Flag) &&
291 (fm.fFlags & SkFontMetrics::kStrikeoutThicknessIsValid_Flag))
292 {
293 SkRect strikeout{ min.fLeft, fm.fStrikeoutPosition - fm.fStrikeoutThickness,
294 min.fRight, fm.fStrikeoutPosition };
295 canvas->drawRect(strikeout, metricsPaint);
296 }
297
298 struct GlyphToDraw {
299 SkGlyphID id;
300 SkPoint location;
301 SkScalar rotation;
302 } glyphsToDraw [] = {
303 {left, {min.left(), min.centerY()}, 270},
304 {right, {min.right(), min.centerY()}, 90},
305 {top, {min.centerX(), min.top() }, 0},
306 {bottom, {min.centerX(), min.bottom() }, 180},
307 };
308
309 SkFont labelFont;
310 labelFont.setEdging(SkFont::Edging::kAntiAlias);
311 labelFont.setTypeface(ToolUtils::DefaultPortableTypeface());
312
313 if (labelBounds) {
314 SkString name;
315 font.getTypeface()->getFamilyName(&name);
316 canvas->drawString(name, min.fLeft, min.fBottom, labelFont, SkPaint());
317 }
318 for (const GlyphToDraw& glyphToDraw : glyphsToDraw) {
319 SkPath path = font.getPath(glyphToDraw.id).value_or(SkPath());
320 SkPaint::Style style = path.isEmpty() ? SkPaint::kFill_Style : SkPaint::kStroke_Style;
321 SkPaint glyphPaint;
322 glyphPaint.setStyle(style);
323 canvas->drawSimpleText(&glyphToDraw.id, sizeof(glyphToDraw.id),
324 SkTextEncoding::kGlyphID, 0, 0, font, glyphPaint);
325
326 if (labelBounds) {
327 SkAutoCanvasRestore acr2(canvas, true);
328 canvas->translate(glyphToDraw.location.fX, glyphToDraw.location.fY);
329 canvas->rotate(glyphToDraw.rotation);
330 SkString glyphStr;
331 glyphStr.appendS32(glyphToDraw.id);
332 canvas->drawString(glyphStr, 0, 0, labelFont, SkPaint());
333 }
334 }
335
336 return drawBounds;
337 }
338
339 SkISize getISize() override { return {1024, 850}; }
340
341 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
342 SkFont font = ToolUtils::DefaultFont();
343 font.setEdging(SkFont::Edging::kAntiAlias);
344 font.setSubpixel(true);
345 font.setSize(100);
346 font.setScaleX(fScaleX);
347 font.setSkewX(fSkewX);
348
349 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
350
351 SkFontMgr* fm = fFM.get();
352 int count = fm->countFamilies();
353 if (count == 0) {
354 *errorMsg = "No families in SkFontMgr under test.";
355 return DrawResult::kSkip;
356 }
357
358 int index = 0;
359 SkScalar x = 0, y = 0;
360
361 canvas->translate(10, 120);
362
363 int typefacesVisited = 0;
364 for (int i = 0; i < count && typefacesVisited < 32; ++i) {
365 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
366 int stylesVisited = 0;
367 for (int j = 0; j < set->count() && typefacesVisited < 32 && stylesVisited < 3; ++j) {
368 font.setTypeface(sk_sp<SkTypeface>(set->createTypeface(j)));
369 // Fonts with lots of glyphs are interesting, but can take a long time to find
370 // the glyphs which make up the maximum extent.
371 SkTypeface* typeface = font.getTypeface();
372 if (typeface && 0 < typeface->countGlyphs() && typeface->countGlyphs() < 1000) {
373 ++typefacesVisited;
374 ++stylesVisited;
375
376 SkColor color = boundsColors[index & 1];
377 SkRect drawBounds = show_bounds(canvas, font, x, y, color, fLabelBounds);
378 x += drawBounds.width() + 20;
379 index += 1;
380 if (x > 900) {
381 x = 0;
382 y += 160;
383 }
384 if (y >= 700) {
385 return DrawResult::kOk;
386 }
387 }
388 }
389 }
390 return DrawResult::kOk;
391 }
392
393 sk_sp<SkFontMgr> fFM;
394 const SkScalar fScaleX;
395 const SkScalar fSkewX;
396 bool fLabelBounds = false;
397 };
398
399 //////////////////////////////////////////////////////////////////////////////
400
401 DEF_GM(return new FontMgrGM;)
402 DEF_GM(return new FontMgrMatchGM;)
403 DEF_GM(return new FontMgrBoundsGM(1, 0);)
404 DEF_GM(return new FontMgrBoundsGM(0.75f, 0);)
405 DEF_GM(return new FontMgrBoundsGM(1, -0.25f);)