xref: /aosp_15_r20/external/skia/docs/examples/SkFontMgr_example2.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(SkFontMgr_example2, 1536, 512, false, 0) {
tostr(SkFontStyle::Slant s)5 const char* tostr(SkFontStyle::Slant s) {
6     switch (s) {
7         case SkFontStyle::kUpright_Slant: return "SkFontStyle::kUpright_Slant";
8         case SkFontStyle::kItalic_Slant:  return "SkFontStyle::kItalic_Slant";
9         case SkFontStyle::kOblique_Slant: return "SkFontStyle::kOblique_Slant";
10         default: return "";
11     }
12 }
13 
draw(SkCanvas * canvas)14 void draw(SkCanvas* canvas) {
15     float x = 10, y = 10;
16     float textScale = 24;
17 
18     for (int i = 0; i < fontMgr->countFamilies(); ++i) {
19         SkString familyName;
20         fontMgr->getFamilyName(i, &familyName);
21         sk_sp<SkFontStyleSet> styleSet(fontMgr->createStyleSet(i));
22         for (int j = 0; j < styleSet->count(); ++j) {
23             SkFontStyle fontStyle;
24             SkString style;
25             styleSet->getStyle(j, &fontStyle, &style);
26             auto s = SkStringPrintf(
27                     "SkFont font(fontMgr->legacyMakeTypeface(\"%s\", SkFontStyle(%3d, %1d, %-27s), "
28                     "%g);",
29                     familyName.c_str(),
30                     fontStyle.weight(),
31                     fontStyle.width(),
32                     tostr(fontStyle.slant()),
33                     textScale);
34             SkFont font(fontMgr->legacyMakeTypeface(familyName.c_str(), fontStyle), textScale);
35             y += font.getSpacing() * 1.5;
36             canvas->drawString(s, x, y, font, SkPaint());
37         }
38     }
39 }
40 }  // END FIDDLE
41