Debian Codegraph @

modules/skottie/src/text/Font.cpp

raw
1 /*
2 * Copyright 2022 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/text/Font.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/private/base/SkTFitsIn.h"
18 #include "include/private/base/SkTo.h"
19 #include "modules/jsonreader/SkJSONReader.h"
20 #include "modules/skottie/src/SkottieJson.h"
21 #include "modules/skottie/src/SkottiePriv.h"
22 #include "modules/sksg/include/SkSGPath.h"
23 #include "modules/sksg/include/SkSGTransform.h"
24 #include "src/base/SkUTF.h"
25
26 namespace skottie::internal {
27
28 bool CustomFont::Builder::parseGlyph(const AnimationBuilder* abuilder,
29 const skjson::ObjectValue& jchar) {
30 // Glyph encoding:
31 // {
32 // "ch": "t",
33 // "data": <glyph data>, // Glyph path or composition data
34 // "size": 50, // apparently ignored
35 // "w": 32.67, // width/advance (1/100 units)
36 // "t": 1 // Marker for composition glyphs only.
37 // }
38 const skjson::StringValue* jch = jchar["ch"];
39 const skjson::ObjectValue* jdata = jchar["data"];
40 if (!jch || !jdata) {
41 return false;
42 }
43
44 const auto* ch_ptr = jch->begin();
45 const auto ch_len = jch->size();
46 if (SkUTF::CountUTF8(ch_ptr, ch_len) != 1) {
47 return false;
48 }
49
50 const auto uni = SkUTF::NextUTF8(&ch_ptr, ch_ptr + ch_len);
51 SkASSERT(uni != -1);
52 if (!SkTFitsIn<SkGlyphID>(uni)) {
53 // Custom font keys are SkGlyphIDs. We could implement a remapping scheme if needed,
54 // but for now direct mapping seems to work well enough.
55 return false;
56 }
57 const auto glyph_id = SkTo<SkGlyphID>(uni);
58
59 // Normalize the path and advance for 1pt.
60 static constexpr float kPtScale = 0.01f;
61 const auto advance = ParseDefault(jchar["w"], 0.0f) * kPtScale;
62
63 // Custom glyphs are either compositions...
64 SkSize glyph_size;
65 if (auto comp_node = ParseGlyphComp(abuilder, *jdata, &glyph_size)) {
66 // With glyph comps, we use the SkCustomTypeface only for shaping -- not for rendering.
67 // We still need accurate glyph bounds though, for visual alignment.
68
69 // TODO: This assumes the glyph origin is always in the lower-left corner.
70 // Lottie may need to add an origin property, to allow designers full control over
71 // glyph comp positioning.
72 const auto glyph_bounds = SkRect::MakeLTRB(0, -glyph_size.fHeight, glyph_size.fWidth, 0);
73 fCustomBuilder.setGlyph(glyph_id, advance, SkPath::Rect(glyph_bounds));
74
75 // Rendering is handled explicitly, post shaping,
76 // based on info tracked in this GlyphCompMap.
77 fGlyphComps.set(glyph_id, std::move(comp_node));
78
79 return true;
80 }
81
82 // ... or paths.
83 SkPath path;
84 if (!ParseGlyphPath(abuilder, *jdata, &path)) {
85 return false;
86 }
87
88 path = path.makeTransform(SkMatrix::Scale(kPtScale, kPtScale));
89
90 fCustomBuilder.setGlyph(glyph_id, advance, path);
91
92 return true;
93 }
94
95 bool CustomFont::Builder::ParseGlyphPath(const skottie::internal::AnimationBuilder* abuilder,
96 const skjson::ObjectValue& jdata,
97 SkPath* path) {
98 // Glyph path encoding:
99 //
100 // "data": {
101 // "shapes": [ // follows the shape layer format
102 // {
103 // "ty": "gr", // group shape type
104 // "it": [ // group items
105 // {
106 // "ty": "sh", // actual shape
107 // "ks": <path data> // animatable path format, but always static
108 // },
109 // ...
110 // ]
111 // },
112 // ...
113 // ]
114 // }
115
116 const skjson::ArrayValue* jshapes = jdata["shapes"];
117 if (!jshapes) {
118 // Space/empty glyph.
119 return true;
120 }
121
122 SkPathBuilder builder;
123 for (const skjson::ObjectValue* jgrp : *jshapes) {
124 if (!jgrp) {
125 return false;
126 }
127
128 const skjson::ArrayValue* jit = (*jgrp)["it"];
129 if (!jit) {
130 return false;
131 }
132
133 for (const skjson::ObjectValue* jshape : *jit) {
134 if (!jshape) {
135 return false;
136 }
137
138 // Glyph paths should never be animated. But they are encoded as
139 // animatable properties, so we use the appropriate helpers.
140 skottie::internal::AnimationBuilder::AutoScope ascope(abuilder);
141 auto path_node = abuilder->attachPath((*jshape)["ks"]);
142 auto animators = ascope.release();
143
144 if (!path_node || !animators.empty()) {
145 return false;
146 }
147
148 builder.addPath(path_node->getPath());
149 }
150 }
151 *path = builder.detach();
152
153 return true;
154 }
155
156 sk_sp<sksg::RenderNode>
157 CustomFont::Builder::ParseGlyphComp(const AnimationBuilder* abuilder,
158 const skjson::ObjectValue& jdata,
159 SkSize* glyph_size) {
160 // Glyph comp encoding:
161 //
162 // "data": { // Follows the precomp layer format.
163 // "ip": <in point>,
164 // "op": <out point>,
165 // "refId": <comp ID>,
166 // "sr": <time remap info>,
167 // "st": <time remap info>,
168 // "ks": <transform info>
169 // }
170
171 LayerInfo linfo{
172 ParseDefault<SkString>(jdata["nm"], SkString()),
173 {0,0},
174 ParseDefault<float>(jdata["ip"], 0.0f),
175 ParseDefault<float>(jdata["op"], 0.0f)
176 };
177
178 if (!linfo.fInPoint && !linfo.fOutPoint) {
179 // Not a comp glyph.
180 return nullptr;
181 }
182
183 // Since the glyph composition encoding matches the precomp layer encoding, we can pretend
184 // we're attaching a precomp here.
185 auto comp_node = abuilder->attachPrecompLayer(jdata, &linfo);
186
187 // Normalize for 1pt.
188 static constexpr float kPtScale = 0.01f;
189
190 // For bounds/alignment purposes, we use a glyph size matching the normalized glyph comp size.
191 *glyph_size = {linfo.fSize.fWidth * kPtScale, linfo.fSize.fHeight * kPtScale};
192
193 sk_sp<sksg::Transform> glyph_transform =
194 sksg::Matrix<SkMatrix>::Make(SkMatrix::Scale(kPtScale, kPtScale));
195
196 // Additional/explicit glyph transform (not handled in attachPrecompLayer).
197 if (const skjson::ObjectValue* jtransform = jdata["ks"]) {
198 glyph_transform = abuilder->attachMatrix2D(*jtransform, std::move(glyph_transform));
199 }
200
201 return sksg::TransformEffect::Make(abuilder->attachPrecompLayer(jdata, &linfo),
202 std::move(glyph_transform));
203 }
204
205 std::unique_ptr<CustomFont> CustomFont::Builder::detach() {
206 return std::unique_ptr<CustomFont>(new CustomFont(std::move(fGlyphComps),
207 fCustomBuilder.detach()));
208 }
209
210 CustomFont::CustomFont(GlyphCompMap&& glyph_comps, sk_sp<SkTypeface> tf)
211 : fGlyphComps(std::move(glyph_comps))
212 , fTypeface(std::move(tf))
213 {}
214
215 CustomFont::~CustomFont() = default;
216
217 sk_sp<sksg::RenderNode> CustomFont::GlyphCompMapper::getGlyphComp(const SkTypeface* tf,
218 SkGlyphID gid) const {
219 for (const auto& font : fFonts) {
220 if (font->typeface().get() == tf) {
221 auto* comp_node = font->fGlyphComps.find(gid);
222 return comp_node ? *comp_node : nullptr;
223 }
224 }
225
226 return nullptr;
227 }
228
229 } // namespace skottie::internal