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" 4REG_FIDDLE(SkFontMgr_example2, 1536, 512, false, 0) { 5const 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 14void draw(SkCanvas* canvas) { 15 float x = 10, y = 10; 16 float textScale = 24; 17 18 sk_sp<SkFontMgr> mgr(SkFontMgr::RefDefault()); 19 for (int i = 0; i < mgr->countFamilies(); ++i) { 20 SkString familyName; 21 mgr->getFamilyName(i, &familyName); 22 sk_sp<SkFontStyleSet> styleSet(mgr->createStyleSet(i)); 23 for (int j = 0; j < styleSet->count(); ++j) { 24 SkFontStyle fontStyle; 25 SkString style; 26 styleSet->getStyle(j, &fontStyle, &style); 27 auto s = SkStringPrintf( 28 "SkFont font(mgr->legacyMakeTypeface(\"%s\", SkFontStyle(%3d, %1d, %-27s), " 29 "%g);", 30 familyName.c_str(), fontStyle.weight(), fontStyle.width(), 31 tostr(fontStyle.slant()), textScale); 32 SkFont font(mgr->legacyMakeTypeface(familyName.c_str(), fontStyle), textScale); 33 y += font.getSpacing() * 1.5; 34 canvas->drawString(s, x, y, font, SkPaint()); 35 } 36 } 37} 38} // END FIDDLE 39