blob: 20b2eacea27b0cf31fa6bbb700aed2d2cdc6155c [file] [log] [blame]
Florin Malitaf9c50632018-08-17 12:29:45 -04001/*
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 "SkottiePriv.h"
9
Florin Malita40543512018-09-19 21:43:29 -040010#include "SkData.h"
Florin Malitaf9c50632018-08-17 12:29:45 -040011#include "SkFontMgr.h"
12#include "SkMakeUnique.h"
Florin Malita9402c7d2018-08-26 22:06:55 -040013#include "SkottieAdapter.h"
Florin Malitaf9c50632018-08-17 12:29:45 -040014#include "SkottieJson.h"
15#include "SkottieValue.h"
16#include "SkSGColor.h"
17#include "SkSGDraw.h"
18#include "SkSGGroup.h"
19#include "SkSGText.h"
20#include "SkTypes.h"
21
22#include <string.h>
23
24namespace skottie {
25namespace internal {
26
27namespace {
28
Florin Malitaf9c50632018-08-17 12:29:45 -040029SkFontStyle FontStyle(const char* style) {
30 static constexpr struct {
31 const char* fName;
32 const SkFontStyle::Weight fWeight;
33 } gWeightMap[] = {
34 { "ExtraLight", SkFontStyle::kExtraLight_Weight },
35 { "Light" , SkFontStyle::kLight_Weight },
36 { "Regular" , SkFontStyle::kNormal_Weight },
37 { "Medium" , SkFontStyle::kMedium_Weight },
38 { "SemiBold" , SkFontStyle::kSemiBold_Weight },
39 { "Bold" , SkFontStyle::kBold_Weight },
40 { "ExtraBold" , SkFontStyle::kExtraBold_Weight },
41 };
42
43 SkFontStyle::Weight weight = SkFontStyle::kNormal_Weight;
44 for (const auto& w : gWeightMap) {
45 const auto name_len = strlen(w.fName);
46 if (!strncmp(style, w.fName, name_len)) {
47 weight = w.fWeight;
48 style += name_len;
49 break;
50 }
51 }
52
53 static constexpr struct {
54 const char* fName;
55 const SkFontStyle::Slant fSlant;
56 } gSlantMap[] = {
57 { "Italic" , SkFontStyle::kItalic_Slant },
58 { "Oblique", SkFontStyle::kOblique_Slant },
59 };
60
61 SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
62 if (*style != '\0') {
63 for (const auto& s : gSlantMap) {
64 if (!strcmp(style, s.fName)) {
65 slant = s.fSlant;
66 style += strlen(s.fName);
67 break;
68 }
69 }
70 }
71
72 if (*style != '\0') {
73 LOG("?? Unknown font style: %s\n", style);
74 }
75
76 return SkFontStyle(weight, SkFontStyle::kNormal_Width, slant);
77}
78
79} // namespace
80
Florin Malitafa0441b2018-08-21 13:17:27 -040081bool AnimationBuilder::FontInfo::matches(const char family[], const char style[]) const {
Florin Malitaf9c50632018-08-17 12:29:45 -040082 return 0 == strcmp(fFamily.c_str(), family)
83 && 0 == strcmp(fStyle.c_str(), style);
84}
85
Florin Malitafa0441b2018-08-21 13:17:27 -040086void AnimationBuilder::parseFonts(const skjson::ObjectValue* jfonts,
87 const skjson::ArrayValue* jchars) {
Florin Malitaf9c50632018-08-17 12:29:45 -040088 // Optional array of font entries, referenced (by name) from text layer document nodes. E.g.
89 // "fonts": {
90 // "list": [
91 // {
92 // "ascent": 75,
93 // "fClass": "",
94 // "fFamily": "Roboto",
95 // "fName": "Roboto-Regular",
Florin Malita40543512018-09-19 21:43:29 -040096 // "fPath": "https://fonts.googleapis.com/css?family=Roboto",
Florin Malitaf9c50632018-08-17 12:29:45 -040097 // "fPath": "",
98 // "fStyle": "Regular",
99 // "fWeight": "",
100 // "origin": 1
101 // }
102 // ]
103 // },
104 if (jfonts) {
105 if (const skjson::ArrayValue* jlist = (*jfonts)["list"]) {
106 for (const skjson::ObjectValue* jfont : *jlist) {
107 if (!jfont) {
108 continue;
109 }
110
111 const skjson::StringValue* jname = (*jfont)["fName"];
112 const skjson::StringValue* jfamily = (*jfont)["fFamily"];
113 const skjson::StringValue* jstyle = (*jfont)["fStyle"];
Florin Malita40543512018-09-19 21:43:29 -0400114 const skjson::StringValue* jpath = (*jfont)["fPath"];
Florin Malitaf9c50632018-08-17 12:29:45 -0400115
116 if (!jname || !jname->size() ||
117 !jfamily || !jfamily->size() ||
118 !jstyle || !jstyle->size()) {
119 LogJSON(*jfont, "!! Ignoring invalid font");
120 continue;
121 }
122
Florin Malitabe5e8652018-08-31 12:54:18 -0400123 const auto& fmgr = fLazyFontMgr.get();
Florin Malita40543512018-09-19 21:43:29 -0400124
125 // Typeface fallback order:
126 // 1) external web font, if a path/url is provided
127 // 2) system font (family/style)
128 // 3) system default
129
130 sk_sp<SkTypeface> tf;
131
132 if (jpath) {
133 tf = fmgr->makeFromData(fResourceProvider->loadWebFont(jpath->begin()));
134 }
135
136 if (!tf) {
137 tf.reset(fmgr->matchFamilyStyle(jfamily->begin(), FontStyle(jstyle->begin())));
138 }
139
Florin Malitaf9c50632018-08-17 12:29:45 -0400140 if (!tf) {
141 LOG("!! Could not create typeface for %s|%s\n",
142 jfamily->begin(), jstyle->begin());
143 // Last resort.
Florin Malitabe5e8652018-08-31 12:54:18 -0400144 tf = fmgr->legacyMakeTypeface(nullptr, FontStyle(jstyle->begin()));
Florin Malitaf9c50632018-08-17 12:29:45 -0400145 if (!tf) {
146 continue;
147 }
148 }
149
Florin Malitafa0441b2018-08-21 13:17:27 -0400150 fFonts.set(SkString(jname->begin(), jname->size()),
Florin Malitaf9c50632018-08-17 12:29:45 -0400151 {
152 SkString(jfamily->begin(), jfamily->size()),
153 SkString(jstyle->begin(), jstyle->size()),
154 ParseDefault((*jfont)["ascent"] , 0.0f),
155 std::move(tf)
156 });
157 }
158 }
159 }
160
161 // Optional array of glyphs, to be associated with one of the declared fonts. E.g.
162 // "chars": [
163 // {
164 // "ch": "t",
165 // "data": {
166 // "shapes": [...]
167 // },
168 // "fFamily": "Roboto",
169 // "size": 50,
170 // "style": "Regular",
171 // "w": 32.67
172 // }
173 // ]
174 if (jchars) {
175 FontInfo* current_font = nullptr;
176
177 for (const skjson::ObjectValue* jchar : *jchars) {
178 if (!jchar) {
179 continue;
180 }
181
182 const skjson::StringValue* jch = (*jchar)["ch"];
183 if (!jch) {
184 continue;
185 }
186
187 const skjson::StringValue* jfamily = (*jchar)["fFamily"];
188 const skjson::StringValue* jstyle = (*jchar)["style"]; // "style", not "fStyle"...
189
190 const auto* ch_ptr = jch->begin();
191 const auto ch_len = jch->size();
192
193 if (!jfamily || !jstyle || (SkUTF::CountUTF8(ch_ptr, ch_len) != 1)) {
194 LogJSON(*jchar, "!! Invalid glyph");
195 continue;
196 }
197
198 const auto uni = SkUTF::NextUTF8(&ch_ptr, ch_ptr + ch_len);
199 SkASSERT(uni != -1);
200
201 const auto* family = jfamily->begin();
202 const auto* style = jstyle->begin();
203
204 // Locate (and cache) the font info. Unlike text nodes, glyphs reference the font by
205 // (family, style) -- not by name :( For now this performs a linear search over *all*
206 // fonts: generally there are few of them, and glyph definitions are font-clustered.
207 // If problematic, we can refactor as a two-level hashmap.
208 if (!current_font || !current_font->matches(family, style)) {
209 current_font = nullptr;
Florin Malitafa0441b2018-08-21 13:17:27 -0400210 fFonts.foreach([&](const SkString& name, FontInfo* finfo) {
Florin Malitaf9c50632018-08-17 12:29:45 -0400211 if (finfo->matches(family, style)) {
212 current_font = finfo;
213 // TODO: would be nice to break early here...
214 }
215 });
216 if (!current_font) {
217 LOG("!! Font not found for codepoint (%d, %s, %s)\n", uni, family, style);
218 continue;
219 }
220 }
221
Florin Malitafa0441b2018-08-21 13:17:27 -0400222 // TODO: parse glyphs
Florin Malitaf9c50632018-08-17 12:29:45 -0400223 }
224 }
Florin Malitaf9c50632018-08-17 12:29:45 -0400225}
226
Florin Malita9402c7d2018-08-26 22:06:55 -0400227sk_sp<SkTypeface> AnimationBuilder::findFont(const SkString& font_name) const {
228 if (const auto* font = fFonts.find(font_name)) {
229 return font->fTypeface;
230 }
231
232 LOG("!! Unknown font: \"%s\"\n", font_name.c_str());
233 return nullptr;
234}
235
Florin Malitafa0441b2018-08-21 13:17:27 -0400236sk_sp<sksg::RenderNode> AnimationBuilder::attachTextLayer(const skjson::ObjectValue& layer,
Florin Malita471a9462018-08-25 20:22:34 -0400237 AnimatorScope* ascope) const {
Florin Malitaf9c50632018-08-17 12:29:45 -0400238 // General text node format:
239 // "t": {
240 // "a": [], // animators (TODO)
241 // "d": {
242 // "k": [
243 // {
244 // "s": {
245 // "f": "Roboto-Regular",
246 // "fc": [
247 // 0.42,
248 // 0.15,
249 // 0.15
250 // ],
251 // "j": 1,
252 // "lh": 60,
253 // "ls": 0,
254 // "s": 50,
255 // "t": "text align right",
256 // "tr": 0
257 // },
258 // "t": 0
259 // }
260 // ]
261 // },
262 // "m": {}, // "more options" (TODO)
263 // "p": {} // "path options" (TODO)
264 // },
265 const skjson::ObjectValue* jt = layer["t"];
266 if (!jt) {
267 LogJSON(layer, "!! Missing text layer \"t\" property");
268 return nullptr;
269 }
270
271 const skjson::ArrayValue* animated_props = (*jt)["a"];
272 if (animated_props && animated_props->size() > 0) {
273 LOG("?? Unsupported animated text properties.\n");
274 }
275
Florin Malitaf9c50632018-08-17 12:29:45 -0400276 const skjson::ObjectValue* jd = (*jt)["d"];
Florin Malita9402c7d2018-08-26 22:06:55 -0400277 if (!jd) {
Florin Malitaf9c50632018-08-17 12:29:45 -0400278 return nullptr;
279 }
280
Florin Malita9402c7d2018-08-26 22:06:55 -0400281 auto text_root = sksg::Group::Make();
282 auto adapter = sk_make_sp<TextAdapter>(text_root);
Florin Malitaf9c50632018-08-17 12:29:45 -0400283
Florin Malita9402c7d2018-08-26 22:06:55 -0400284 this->bindProperty<TextValue>(*jd, ascope, [adapter] (const TextValue& txt) {
285 adapter->setText(txt);
286 });
Florin Malitaf9c50632018-08-17 12:29:45 -0400287
Florin Malita9402c7d2018-08-26 22:06:55 -0400288 return std::move(text_root);
Florin Malitaf9c50632018-08-17 12:29:45 -0400289}
290
291} // namespace internal
292} // namespace skottie