1/*
2 * Copyright 2018 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 "modules/skottie/src/SkottiePriv.h"
9
10#include "include/core/SkData.h"
11#include "include/core/SkFontMgr.h"
12#include "include/core/SkTypes.h"
13#include "modules/skottie/src/SkottieJson.h"
14#include "modules/skottie/src/text/TextAdapter.h"
15#include "modules/skottie/src/text/TextAnimator.h"
16#include "modules/skottie/src/text/TextValue.h"
17#include "modules/sksg/include/SkSGDraw.h"
18#include "modules/sksg/include/SkSGGroup.h"
19#include "modules/sksg/include/SkSGPaint.h"
20#include "modules/sksg/include/SkSGText.h"
21
22#include <string.h>
23
24namespace skottie {
25namespace internal {
26
27namespace {
28
29SkFontStyle FontStyle(const AnimationBuilder* abuilder, const char* style) {
30 static constexpr struct {
31 const char* fName;
32 const SkFontStyle::Weight fWeight;
33 } gWeightMap[] = {
34 { "Regular" , SkFontStyle::kNormal_Weight },
35 { "Medium" , SkFontStyle::kMedium_Weight },
36 { "Bold" , SkFontStyle::kBold_Weight },
37 { "Light" , SkFontStyle::kLight_Weight },
38 { "Black" , SkFontStyle::kBlack_Weight },
39 { "Thin" , SkFontStyle::kThin_Weight },
40 { "Extra" , SkFontStyle::kExtraBold_Weight },
41 { "ExtraBold" , SkFontStyle::kExtraBold_Weight },
42 { "ExtraLight", SkFontStyle::kExtraLight_Weight },
43 { "ExtraBlack", SkFontStyle::kExtraBlack_Weight },
44 { "SemiBold" , SkFontStyle::kSemiBold_Weight },
45 { "Hairline" , SkFontStyle::kThin_Weight },
46 { "Normal" , SkFontStyle::kNormal_Weight },
47 { "Plain" , SkFontStyle::kNormal_Weight },
48 { "Standard" , SkFontStyle::kNormal_Weight },
49 { "Roman" , SkFontStyle::kNormal_Weight },
50 { "Heavy" , SkFontStyle::kBlack_Weight },
51 { "Demi" , SkFontStyle::kSemiBold_Weight },
52 { "DemiBold" , SkFontStyle::kSemiBold_Weight },
53 { "Ultra" , SkFontStyle::kExtraBold_Weight },
54 { "UltraBold" , SkFontStyle::kExtraBold_Weight },
55 { "UltraBlack", SkFontStyle::kExtraBlack_Weight },
56 { "UltraHeavy", SkFontStyle::kExtraBlack_Weight },
57 { "UltraLight", SkFontStyle::kExtraLight_Weight },
58 };
59
60 SkFontStyle::Weight weight = SkFontStyle::kNormal_Weight;
61 for (const auto& w : gWeightMap) {
62 const auto name_len = strlen(w.fName);
63 if (!strncmp(style, w.fName, name_len)) {
64 weight = w.fWeight;
65 style += name_len;
66 break;
67 }
68 }
69
70 static constexpr struct {
71 const char* fName;
72 const SkFontStyle::Slant fSlant;
73 } gSlantMap[] = {
74 { "Italic" , SkFontStyle::kItalic_Slant },
75 { "Oblique", SkFontStyle::kOblique_Slant },
76 };
77
78 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
79 if (*style != '\0') {
80 for (const auto& s : gSlantMap) {
81 if (!strcmp(style, s.fName)) {
82 slant = s.fSlant;
83 style += strlen(s.fName);
84 break;
85 }
86 }
87 }
88
89 if (*style != '\0') {
90 abuilder->log(Logger::Level::kWarning, nullptr, "Unknown font style: %s.", style);
91 }
92
93 return SkFontStyle(weight, SkFontStyle::kNormal_Width, slant);
94}
95
96} // namespace
97
98bool AnimationBuilder::FontInfo::matches(const char family[], const char style[]) const {
99 return 0 == strcmp(fFamily.c_str(), family)
100 && 0 == strcmp(fStyle.c_str(), style);
101}
102
103#ifdef SK_NO_FONTS
104void AnimationBuilder::parseFonts(const skjson::ObjectValue* jfonts,
105 const skjson::ArrayValue* jchars) {}
106
107sk_sp<sksg::RenderNode> AnimationBuilder::attachTextLayer(const skjson::ObjectValue& jlayer,
108 LayerInfo*) const {
109 return nullptr;
110}
111#else
112void AnimationBuilder::parseFonts(const skjson::ObjectValue* jfonts,
113 const skjson::ArrayValue* jchars) {
114 // Optional array of font entries, referenced (by name) from text layer document nodes. E.g.
115 // "fonts": {
116 // "list": [
117 // {
118 // "ascent": 75,
119 // "fClass": "",
120 // "fFamily": "Roboto",
121 // "fName": "Roboto-Regular",
122 // "fPath": "https://fonts.googleapis.com/css?family=Roboto",
123 // "fPath": "",
124 // "fStyle": "Regular",
125 // "fWeight": "",
126 // "origin": 1
127 // }
128 // ]
129 // },
130 if (jfonts) {
131 if (const skjson::ArrayValue* jlist = (*jfonts)["list"]) {
132 for (const skjson::ObjectValue* jfont : *jlist) {
133 if (!jfont) {
134 continue;
135 }
136
137 const skjson::StringValue* jname = (*jfont)["fName"];
138 const skjson::StringValue* jfamily = (*jfont)["fFamily"];
139 const skjson::StringValue* jstyle = (*jfont)["fStyle"];
140 const skjson::StringValue* jpath = (*jfont)["fPath"];
141
142 if (!jname || !jname->size() ||
143 !jfamily || !jfamily->size() ||
144 !jstyle || !jstyle->size()) {
145 this->log(Logger::Level::kError, jfont, "Invalid font.");
146 continue;
147 }
148
149 const auto& fmgr = fLazyFontMgr.get();
150
151 // Typeface fallback order:
152 // 1) externally-loaded font (provided by the embedder)
153 // 2) system font (family/style)
154 // 3) system default
155
156 sk_sp<SkTypeface> tf =
157 fmgr->makeFromData(fResourceProvider->loadFont(jname->begin(),
158 jpath ? jpath->begin()
159 : nullptr));
160
161 if (!tf) {
162 tf.reset(fmgr->matchFamilyStyle(jfamily->begin(),
163 FontStyle(this, jstyle->begin())));
164 }
165
166 if (!tf) {
167 this->log(Logger::Level::kError, nullptr,
168 "Could not create typeface for %s|%s.",
169 jfamily->begin(), jstyle->begin());
170 // Last resort.
171 tf = fmgr->legacyMakeTypeface(nullptr, FontStyle(this, jstyle->begin()));
172 if (!tf) {
173 continue;
174 }
175 }
176
177 fFonts.set(SkString(jname->begin(), jname->size()),
178 {
179 SkString(jfamily->begin(), jfamily->size()),
180 SkString(jstyle->begin(), jstyle->size()),
181 ParseDefault((*jfont)["ascent"] , 0.0f),
182 std::move(tf)
183 });
184 }
185 }
186 }
187
188 // Optional array of glyphs, to be associated with one of the declared fonts. E.g.
189 // "chars": [
190 // {
191 // "ch": "t",
192 // "data": {
193 // "shapes": [...]
194 // },
195 // "fFamily": "Roboto",
196 // "size": 50,
197 // "style": "Regular",
198 // "w": 32.67
199 // }
200 // ]
201 if (jchars) {
202 FontInfo* current_font = nullptr;
203
204 for (const skjson::ObjectValue* jchar : *jchars) {
205 if (!jchar) {
206 continue;
207 }
208
209 const skjson::StringValue* jch = (*jchar)["ch"];
210 if (!jch) {
211 continue;
212 }
213
214 const skjson::StringValue* jfamily = (*jchar)["fFamily"];
215 const skjson::StringValue* jstyle = (*jchar)["style"]; // "style", not "fStyle"...
216
217 const auto* ch_ptr = jch->begin();
218 const auto ch_len = jch->size();
219
220 if (!jfamily || !jstyle || (SkUTF::CountUTF8(ch_ptr, ch_len) != 1)) {
221 this->log(Logger::Level::kError, jchar, "Invalid glyph.");
222 continue;
223 }
224
225 const auto uni = SkUTF::NextUTF8(&ch_ptr, ch_ptr + ch_len);
226 SkASSERT(uni != -1);
227
228 const auto* family = jfamily->begin();
229 const auto* style = jstyle->begin();
230
231 // Locate (and cache) the font info. Unlike text nodes, glyphs reference the font by
232 // (family, style) -- not by name :( For now this performs a linear search over *all*
233 // fonts: generally there are few of them, and glyph definitions are font-clustered.
234 // If problematic, we can refactor as a two-level hashmap.
235 if (!current_font || !current_font->matches(family, style)) {
236 current_font = nullptr;
237 fFonts.foreach([&](const SkString& name, FontInfo* finfo) {
238 if (finfo->matches(family, style)) {
239 current_font = finfo;
240 // TODO: would be nice to break early here...
241 }
242 });
243 if (!current_font) {
244 this->log(Logger::Level::kError, nullptr,
245 "Font not found for codepoint (%d, %s, %s).", uni, family, style);
246 continue;
247 }
248 }
249
250 // TODO: parse glyphs
251 }
252 }
253}
254
255sk_sp<sksg::RenderNode> AnimationBuilder::attachTextLayer(const skjson::ObjectValue& jlayer,
256 LayerInfo*) const {
257 return this->attachDiscardableAdapter<TextAdapter>(jlayer,
258 this,
259 fLazyFontMgr.getMaybeNull(),
260 fLogger);
261}
262#endif
263
264const AnimationBuilder::FontInfo* AnimationBuilder::findFont(const SkString& font_name) const {
265 return fFonts.find(font_name);
266}
267
268
269} // namespace internal
270} // namespace skottie
271