1 /*
2 * Copyright 2014 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 "tools/ToolUtils.h"
9
10 #include "include/core/SkFontMgr.h"
11 #include "include/core/SkFontStyle.h"
12 #include "include/core/SkTypeface.h"
13 #include "include/private/base/SkMutex.h"
14 #include "include/utils/SkCustomTypeface.h"
15 #include "src/base/SkUTF.h"
16 #include "src/core/SkOSFile.h"
17 #include "tools/Resources.h"
18 #include "tools/fonts/TestFontMgr.h"
19
20 namespace ToolUtils {
21
planet_typeface()22 sk_sp<SkTypeface> planet_typeface() {
23 static const sk_sp<SkTypeface> planetTypeface = []() {
24 const char* filename;
25 #if defined(SK_BUILD_FOR_WIN)
26 filename = "fonts/planetcolr.ttf";
27 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
28 filename = "fonts/planetsbix.ttf";
29 #else
30 filename = "fonts/planetcbdt.ttf";
31 #endif
32 sk_sp<SkTypeface> typeface = MakeResourceAsTypeface(filename);
33 if (typeface) {
34 return typeface;
35 }
36 return SkTypeface::MakeFromName("Planet", SkFontStyle());
37 }();
38 return planetTypeface;
39 }
40
emoji_typeface()41 sk_sp<SkTypeface> emoji_typeface() {
42 static const sk_sp<SkTypeface> emojiTypeface = []() {
43 const char* filename;
44 #if defined(SK_BUILD_FOR_WIN)
45 filename = "fonts/colr.ttf";
46 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
47 filename = "fonts/sbix.ttf";
48 #else
49 filename = "fonts/cbdt.ttf";
50 #endif
51 sk_sp<SkTypeface> typeface = MakeResourceAsTypeface(filename);
52 if (typeface) {
53 return typeface;
54 }
55 return SkTypeface::MakeFromName("Emoji", SkFontStyle());
56 }();
57 return emojiTypeface;
58 }
59
sample_user_typeface()60 sk_sp<SkTypeface> sample_user_typeface() {
61 SkCustomTypefaceBuilder builder;
62 SkFont font;
63 const float upem = 200;
64
65 {
66 SkFontMetrics metrics;
67 metrics.fFlags = 0;
68 metrics.fTop = -200;
69 metrics.fAscent = -150;
70 metrics.fDescent = 50;
71 metrics.fBottom = -75;
72 metrics.fLeading = 10;
73 metrics.fAvgCharWidth = 150;
74 metrics.fMaxCharWidth = 300;
75 metrics.fXMin = -20;
76 metrics.fXMax = 290;
77 metrics.fXHeight = -100;
78 metrics.fCapHeight = 0;
79 metrics.fUnderlineThickness = 5;
80 metrics.fUnderlinePosition = 2;
81 metrics.fStrikeoutThickness = 5;
82 metrics.fStrikeoutPosition = -50;
83 builder.setMetrics(metrics, 1.0f/upem);
84 }
85 builder.setFontStyle(SkFontStyle(367, 3, SkFontStyle::kOblique_Slant));
86
87 const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
88 for (SkGlyphID index = 0; index <= 67; ++index) {
89 SkScalar width;
90 width = 100;
91
92 builder.setGlyph(index, width/upem, SkPath::Circle(50, -50, 75).makeTransform(scale));
93 }
94
95 return builder.detach();
96 }
97
create_font(const char * name,SkFontStyle style)98 static sk_sp<SkTypeface> create_font(const char* name, SkFontStyle style) {
99 static sk_sp<SkFontMgr> portableFontMgr = MakePortableFontMgr();
100 return portableFontMgr->legacyMakeTypeface(name, style);
101 }
102
create_portable_typeface(const char * name,SkFontStyle style)103 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
104 return create_font(name, style);
105 }
106 } // namespace ToolUtils
107