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/SkMutex.h"
14 #include "include/utils/SkCustomTypeface.h"
15 #include "src/core/SkOSFile.h"
16 #include "src/utils/SkUTF.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
emoji_sample_text()60 const char* emoji_sample_text() {
61 return "\xF0\x9F\x98\x80"
62 " "
63 "\xE2\x99\xA2"; // ♢
64 }
65
sample_user_typeface()66 sk_sp<SkTypeface> sample_user_typeface() {
67 SkCustomTypefaceBuilder builder;
68 SkFont font;
69 const float upem = 200;
70
71 {
72 SkFontMetrics metrics;
73 metrics.fFlags = 0;
74 metrics.fTop = -200;
75 metrics.fAscent = -150;
76 metrics.fDescent = 50;
77 metrics.fBottom = -75;
78 metrics.fLeading = 10;
79 metrics.fAvgCharWidth = 150;
80 metrics.fMaxCharWidth = 300;
81 metrics.fXMin = -20;
82 metrics.fXMax = 290;
83 metrics.fXHeight = -100;
84 metrics.fCapHeight = 0;
85 metrics.fUnderlineThickness = 5;
86 metrics.fUnderlinePosition = 2;
87 metrics.fStrikeoutThickness = 5;
88 metrics.fStrikeoutPosition = -50;
89 builder.setMetrics(metrics, 1.0f/upem);
90 }
91 builder.setFontStyle(SkFontStyle(367, 3, SkFontStyle::kOblique_Slant));
92
93 const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem);
94 for (SkGlyphID index = 0; index <= 67; ++index) {
95 SkScalar width;
96 width = 100;
97
98 builder.setGlyph(index, width/upem, SkPath::Circle(50, -50, 75).makeTransform(scale));
99 }
100
101 return builder.detach();
102 }
103
create_font(const char * name,SkFontStyle style)104 static sk_sp<SkTypeface> create_font(const char* name, SkFontStyle style) {
105 static sk_sp<SkFontMgr> portableFontMgr = MakePortableFontMgr();
106 return portableFontMgr->legacyMakeTypeface(name, style);
107 }
108
create_portable_typeface(const char * name,SkFontStyle style)109 sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) {
110 return create_font(name, style);
111 }
112 } // namespace ToolUtils
113