blob: 9538d5698c4987a7b7085861a6d56bd9e85bf18b [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:
Florin Malita97b150e2018-09-27 10:59:52 -0400126 // 1) externally-loaded font (provided by the embedder)
Florin Malita40543512018-09-19 21:43:29 -0400127 // 2) system font (family/style)
128 // 3) system default
129
Florin Malita97b150e2018-09-27 10:59:52 -0400130 sk_sp<SkTypeface> tf =
131 fmgr->makeFromData(fResourceProvider->loadFont(jname->begin(),
132 jpath ? jpath->begin()
133 : nullptr));
Florin Malita40543512018-09-19 21:43:29 -0400134
135 if (!tf) {
136 tf.reset(fmgr->matchFamilyStyle(jfamily->begin(), FontStyle(jstyle->begin())));
137 }
138
Florin Malitaf9c50632018-08-17 12:29:45 -0400139 if (!tf) {
140 LOG("!! Could not create typeface for %s|%s\n",
141 jfamily->begin(), jstyle->begin());
142 // Last resort.
Florin Malitabe5e8652018-08-31 12:54:18 -0400143 tf = fmgr->legacyMakeTypeface(nullptr, FontStyle(jstyle->begin()));
Florin Malitaf9c50632018-08-17 12:29:45 -0400144 if (!tf) {
145 continue;
146 }
147 }
148
Florin Malitafa0441b2018-08-21 13:17:27 -0400149 fFonts.set(SkString(jname->begin(), jname->size()),
Florin Malitaf9c50632018-08-17 12:29:45 -0400150 {
151 SkString(jfamily->begin(), jfamily->size()),
152 SkString(jstyle->begin(), jstyle->size()),
153 ParseDefault((*jfont)["ascent"] , 0.0f),
154 std::move(tf)
155 });
156 }
157 }
158 }
159
160 // Optional array of glyphs, to be associated with one of the declared fonts. E.g.
161 // "chars": [
162 // {
163 // "ch": "t",
164 // "data": {
165 // "shapes": [...]
166 // },
167 // "fFamily": "Roboto",
168 // "size": 50,
169 // "style": "Regular",
170 // "w": 32.67
171 // }
172 // ]
173 if (jchars) {
174 FontInfo* current_font = nullptr;
175
176 for (const skjson::ObjectValue* jchar : *jchars) {
177 if (!jchar) {
178 continue;
179 }
180
181 const skjson::StringValue* jch = (*jchar)["ch"];
182 if (!jch) {
183 continue;
184 }
185
186 const skjson::StringValue* jfamily = (*jchar)["fFamily"];
187 const skjson::StringValue* jstyle = (*jchar)["style"]; // "style", not "fStyle"...
188
189 const auto* ch_ptr = jch->begin();
190 const auto ch_len = jch->size();
191
192 if (!jfamily || !jstyle || (SkUTF::CountUTF8(ch_ptr, ch_len) != 1)) {
193 LogJSON(*jchar, "!! Invalid glyph");
194 continue;
195 }
196
197 const auto uni = SkUTF::NextUTF8(&ch_ptr, ch_ptr + ch_len);
198 SkASSERT(uni != -1);
199
200 const auto* family = jfamily->begin();
201 const auto* style = jstyle->begin();
202
203 // Locate (and cache) the font info. Unlike text nodes, glyphs reference the font by
204 // (family, style) -- not by name :( For now this performs a linear search over *all*
205 // fonts: generally there are few of them, and glyph definitions are font-clustered.
206 // If problematic, we can refactor as a two-level hashmap.
207 if (!current_font || !current_font->matches(family, style)) {
208 current_font = nullptr;
Florin Malitafa0441b2018-08-21 13:17:27 -0400209 fFonts.foreach([&](const SkString& name, FontInfo* finfo) {
Florin Malitaf9c50632018-08-17 12:29:45 -0400210 if (finfo->matches(family, style)) {
211 current_font = finfo;
212 // TODO: would be nice to break early here...
213 }
214 });
215 if (!current_font) {
216 LOG("!! Font not found for codepoint (%d, %s, %s)\n", uni, family, style);
217 continue;
218 }
219 }
220
Florin Malitafa0441b2018-08-21 13:17:27 -0400221 // TODO: parse glyphs
Florin Malitaf9c50632018-08-17 12:29:45 -0400222 }
223 }
Florin Malitaf9c50632018-08-17 12:29:45 -0400224}
225
Florin Malita9402c7d2018-08-26 22:06:55 -0400226sk_sp<SkTypeface> AnimationBuilder::findFont(const SkString& font_name) const {
227 if (const auto* font = fFonts.find(font_name)) {
228 return font->fTypeface;
229 }
230
231 LOG("!! Unknown font: \"%s\"\n", font_name.c_str());
232 return nullptr;
233}
234
Florin Malitafa0441b2018-08-21 13:17:27 -0400235sk_sp<sksg::RenderNode> AnimationBuilder::attachTextLayer(const skjson::ObjectValue& layer,
Florin Malita471a9462018-08-25 20:22:34 -0400236 AnimatorScope* ascope) const {
Florin Malitaf9c50632018-08-17 12:29:45 -0400237 // General text node format:
238 // "t": {
239 // "a": [], // animators (TODO)
240 // "d": {
241 // "k": [
242 // {
243 // "s": {
244 // "f": "Roboto-Regular",
245 // "fc": [
246 // 0.42,
247 // 0.15,
248 // 0.15
249 // ],
250 // "j": 1,
251 // "lh": 60,
252 // "ls": 0,
253 // "s": 50,
254 // "t": "text align right",
255 // "tr": 0
256 // },
257 // "t": 0
258 // }
259 // ]
260 // },
261 // "m": {}, // "more options" (TODO)
262 // "p": {} // "path options" (TODO)
263 // },
264 const skjson::ObjectValue* jt = layer["t"];
265 if (!jt) {
266 LogJSON(layer, "!! Missing text layer \"t\" property");
267 return nullptr;
268 }
269
270 const skjson::ArrayValue* animated_props = (*jt)["a"];
271 if (animated_props && animated_props->size() > 0) {
272 LOG("?? Unsupported animated text properties.\n");
273 }
274
Florin Malitaf9c50632018-08-17 12:29:45 -0400275 const skjson::ObjectValue* jd = (*jt)["d"];
Florin Malita9402c7d2018-08-26 22:06:55 -0400276 if (!jd) {
Florin Malitaf9c50632018-08-17 12:29:45 -0400277 return nullptr;
278 }
279
Florin Malita9402c7d2018-08-26 22:06:55 -0400280 auto text_root = sksg::Group::Make();
281 auto adapter = sk_make_sp<TextAdapter>(text_root);
Florin Malitaf9c50632018-08-17 12:29:45 -0400282
Florin Malita9402c7d2018-08-26 22:06:55 -0400283 this->bindProperty<TextValue>(*jd, ascope, [adapter] (const TextValue& txt) {
284 adapter->setText(txt);
285 });
Florin Malitaf9c50632018-08-17 12:29:45 -0400286
Florin Malita9402c7d2018-08-26 22:06:55 -0400287 return std::move(text_root);
Florin Malitaf9c50632018-08-17 12:29:45 -0400288}
289
290} // namespace internal
291} // namespace skottie