1 | #ifndef OT_GLYF_VARCOMPOSITEGLYPH_HH |
2 | #define OT_GLYF_VARCOMPOSITEGLYPH_HH |
3 | |
4 | |
5 | #include "../../hb-open-type.hh" |
6 | #include "coord-setter.hh" |
7 | |
8 | |
9 | namespace OT { |
10 | namespace glyf_impl { |
11 | |
12 | |
13 | struct VarCompositeGlyphRecord |
14 | { |
15 | protected: |
16 | enum var_composite_glyph_flag_t |
17 | { |
18 | USE_MY_METRICS = 0x0001, |
19 | AXIS_INDICES_ARE_SHORT = 0x0002, |
20 | UNIFORM_SCALE = 0x0004, |
21 | HAVE_TRANSLATE_X = 0x0008, |
22 | HAVE_TRANSLATE_Y = 0x0010, |
23 | HAVE_ROTATION = 0x0020, |
24 | HAVE_SCALE_X = 0x0040, |
25 | HAVE_SCALE_Y = 0x0080, |
26 | HAVE_SKEW_X = 0x0100, |
27 | HAVE_SKEW_Y = 0x0200, |
28 | HAVE_TCENTER_X = 0x0400, |
29 | HAVE_TCENTER_Y = 0x0800, |
30 | GID_IS_24BIT = 0x1000, |
31 | AXES_HAVE_VARIATION = 0x2000, |
32 | RESET_UNSPECIFIED_AXES = 0x4000, |
33 | }; |
34 | |
35 | public: |
36 | |
37 | unsigned int get_size () const |
38 | { |
39 | unsigned fl = flags; |
40 | unsigned int size = min_size; |
41 | |
42 | unsigned axis_width = (fl & AXIS_INDICES_ARE_SHORT) ? 4 : 3; |
43 | size += numAxes * axis_width; |
44 | |
45 | if (fl & GID_IS_24BIT) size += 1; |
46 | |
47 | // 2 bytes each for the following flags |
48 | fl = fl & (HAVE_TRANSLATE_X | HAVE_TRANSLATE_Y | |
49 | HAVE_ROTATION | |
50 | HAVE_SCALE_X | HAVE_SCALE_Y | |
51 | HAVE_SKEW_X | HAVE_SKEW_Y | |
52 | HAVE_TCENTER_X | HAVE_TCENTER_Y); |
53 | size += hb_popcount (fl) * 2; |
54 | |
55 | return size; |
56 | } |
57 | |
58 | bool has_more () const { return true; } |
59 | |
60 | bool is_use_my_metrics () const { return flags & USE_MY_METRICS; } |
61 | bool is_reset_unspecified_axes () const { return flags & RESET_UNSPECIFIED_AXES; } |
62 | |
63 | hb_codepoint_t get_gid () const |
64 | { |
65 | if (flags & GID_IS_24BIT) |
66 | return * (const HBGlyphID24 *) &pad; |
67 | else |
68 | return * (const HBGlyphID16 *) &pad; |
69 | } |
70 | |
71 | void set_gid (hb_codepoint_t gid) |
72 | { |
73 | if (flags & GID_IS_24BIT) |
74 | * (HBGlyphID24 *) &pad = gid; |
75 | else |
76 | * (HBGlyphID16 *) &pad = gid; |
77 | } |
78 | |
79 | unsigned get_numAxes () const |
80 | { |
81 | return numAxes; |
82 | } |
83 | |
84 | unsigned get_num_points () const |
85 | { |
86 | unsigned fl = flags; |
87 | unsigned num = 0; |
88 | if (fl & AXES_HAVE_VARIATION) num += numAxes; |
89 | |
90 | /* Hopefully faster code, relying on the value of the flags. */ |
91 | fl = (((fl & (HAVE_TRANSLATE_Y | HAVE_SCALE_Y | HAVE_SKEW_Y | HAVE_TCENTER_Y)) >> 1) | fl) & |
92 | (HAVE_TRANSLATE_X | HAVE_ROTATION | HAVE_SCALE_X | HAVE_SKEW_X | HAVE_TCENTER_X); |
93 | num += hb_popcount (fl); |
94 | return num; |
95 | |
96 | /* Slower but more readable code. */ |
97 | if (fl & (HAVE_TRANSLATE_X | HAVE_TRANSLATE_Y)) num++; |
98 | if (fl & HAVE_ROTATION) num++; |
99 | if (fl & (HAVE_SCALE_X | HAVE_SCALE_Y)) num++; |
100 | if (fl & (HAVE_SKEW_X | HAVE_SKEW_Y)) num++; |
101 | if (fl & (HAVE_TCENTER_X | HAVE_TCENTER_Y)) num++; |
102 | return num; |
103 | } |
104 | |
105 | void transform_points (hb_array_t<const contour_point_t> record_points, |
106 | hb_array_t<contour_point_t> points) const |
107 | { |
108 | float matrix[4]; |
109 | contour_point_t trans; |
110 | |
111 | get_transformation_from_points (record_points.arrayZ, matrix, trans); |
112 | |
113 | auto arrayZ = points.arrayZ; |
114 | unsigned count = points.length; |
115 | |
116 | if (matrix[0] != 1.f || matrix[1] != 0.f || |
117 | matrix[2] != 0.f || matrix[3] != 1.f) |
118 | for (unsigned i = 0; i < count; i++) |
119 | arrayZ[i].transform (matrix); |
120 | |
121 | if (trans.x != 0.f || trans.y != 0.f) |
122 | for (unsigned i = 0; i < count; i++) |
123 | arrayZ[i].translate (trans); |
124 | } |
125 | |
126 | static inline void transform (float (&matrix)[4], contour_point_t &trans, |
127 | float (other)[6]) |
128 | { |
129 | // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L268 |
130 | float xx1 = other[0]; |
131 | float xy1 = other[1]; |
132 | float yx1 = other[2]; |
133 | float yy1 = other[3]; |
134 | float dx1 = other[4]; |
135 | float dy1 = other[5]; |
136 | float xx2 = matrix[0]; |
137 | float xy2 = matrix[1]; |
138 | float yx2 = matrix[2]; |
139 | float yy2 = matrix[3]; |
140 | float dx2 = trans.x; |
141 | float dy2 = trans.y; |
142 | |
143 | matrix[0] = xx1*xx2 + xy1*yx2; |
144 | matrix[1] = xx1*xy2 + xy1*yy2; |
145 | matrix[2] = yx1*xx2 + yy1*yx2; |
146 | matrix[3] = yx1*xy2 + yy1*yy2; |
147 | trans.x = xx2*dx1 + yx2*dy1 + dx2; |
148 | trans.y = xy2*dx1 + yy2*dy1 + dy2; |
149 | } |
150 | |
151 | static void translate (float (&matrix)[4], contour_point_t &trans, |
152 | float translateX, float translateY) |
153 | { |
154 | if (!translateX && !translateY) |
155 | return; |
156 | |
157 | trans.x += matrix[0] * translateX + matrix[2] * translateY; |
158 | trans.y += matrix[1] * translateX + matrix[3] * translateY; |
159 | } |
160 | |
161 | static void scale (float (&matrix)[4], contour_point_t &trans, |
162 | float scaleX, float scaleY) |
163 | { |
164 | if (scaleX == 1.f && scaleY == 1.f) |
165 | return; |
166 | |
167 | matrix[0] *= scaleX; |
168 | matrix[1] *= scaleX; |
169 | matrix[2] *= scaleY; |
170 | matrix[3] *= scaleY; |
171 | } |
172 | |
173 | static void rotate (float (&matrix)[4], contour_point_t &trans, |
174 | float rotation) |
175 | { |
176 | if (!rotation) |
177 | return; |
178 | |
179 | // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L240 |
180 | rotation = rotation * HB_PI; |
181 | float c; |
182 | float s; |
183 | #ifdef HAVE_SINCOSF |
184 | sincosf (rotation, &s, &c); |
185 | #else |
186 | c = cosf (rotation); |
187 | s = sinf (rotation); |
188 | #endif |
189 | float other[6] = {c, s, -s, c, 0.f, 0.f}; |
190 | transform (matrix, trans, other); |
191 | } |
192 | |
193 | static void skew (float (&matrix)[4], contour_point_t &trans, |
194 | float skewX, float skewY) |
195 | { |
196 | if (!skewX && !skewY) |
197 | return; |
198 | |
199 | // https://github.com/fonttools/fonttools/blob/f66ee05f71c8b57b5f519ee975e95edcd1466e14/Lib/fontTools/misc/transform.py#L255 |
200 | skewX = skewX * HB_PI; |
201 | skewY = skewY * HB_PI; |
202 | float other[6] = {1.f, |
203 | skewY ? tanf (skewY) : 0.f, |
204 | skewX ? tanf (skewX) : 0.f, |
205 | 1.f, |
206 | 0.f, 0.f}; |
207 | transform (matrix, trans, other); |
208 | } |
209 | |
210 | bool get_points (contour_point_vector_t &points) const |
211 | { |
212 | unsigned num_points = get_num_points (); |
213 | |
214 | points.alloc (points.length + num_points + 4); // For phantom points |
215 | if (unlikely (!points.resize (points.length + num_points, false))) return false; |
216 | contour_point_t *rec_points = points.arrayZ + (points.length - num_points); |
217 | hb_memset (rec_points, 0, num_points * sizeof (rec_points[0])); |
218 | |
219 | unsigned fl = flags; |
220 | |
221 | unsigned num_axes = numAxes; |
222 | unsigned axis_width = (fl & AXIS_INDICES_ARE_SHORT) ? 2 : 1; |
223 | unsigned axes_size = num_axes * axis_width; |
224 | |
225 | const F2DOT14 *q = (const F2DOT14 *) (axes_size + |
226 | (fl & GID_IS_24BIT ? 3 : 2) + |
227 | (const HBUINT8 *) &pad); |
228 | |
229 | unsigned count = num_axes; |
230 | if (fl & AXES_HAVE_VARIATION) |
231 | { |
232 | for (unsigned i = 0; i < count; i++) |
233 | rec_points++->x = q++->to_int (); |
234 | } |
235 | else |
236 | q += count; |
237 | |
238 | const HBUINT16 *p = (const HBUINT16 *) q; |
239 | |
240 | if (fl & (HAVE_TRANSLATE_X | HAVE_TRANSLATE_Y)) |
241 | { |
242 | int translateX = (fl & HAVE_TRANSLATE_X) ? * (const FWORD *) p++ : 0; |
243 | int translateY = (fl & HAVE_TRANSLATE_Y) ? * (const FWORD *) p++ : 0; |
244 | rec_points->x = translateX; |
245 | rec_points->y = translateY; |
246 | rec_points++; |
247 | } |
248 | if (fl & HAVE_ROTATION) |
249 | { |
250 | int rotation = (fl & HAVE_ROTATION) ? ((const F4DOT12 *) p++)->to_int () : 0; |
251 | rec_points->x = rotation; |
252 | rec_points++; |
253 | } |
254 | if (fl & (HAVE_SCALE_X | HAVE_SCALE_Y)) |
255 | { |
256 | int scaleX = (fl & HAVE_SCALE_X) ? ((const F6DOT10 *) p++)->to_int () : 1 << 10; |
257 | int scaleY = (fl & HAVE_SCALE_Y) ? ((const F6DOT10 *) p++)->to_int () : 1 << 10; |
258 | if ((fl & UNIFORM_SCALE) && !(fl & HAVE_SCALE_Y)) |
259 | scaleY = scaleX; |
260 | rec_points->x = scaleX; |
261 | rec_points->y = scaleY; |
262 | rec_points++; |
263 | } |
264 | if (fl & (HAVE_SKEW_X | HAVE_SKEW_Y)) |
265 | { |
266 | int skewX = (fl & HAVE_SKEW_X) ? ((const F4DOT12 *) p++)->to_int () : 0; |
267 | int skewY = (fl & HAVE_SKEW_Y) ? ((const F4DOT12 *) p++)->to_int () : 0; |
268 | rec_points->x = skewX; |
269 | rec_points->y = skewY; |
270 | rec_points++; |
271 | } |
272 | if (fl & (HAVE_TCENTER_X | HAVE_TCENTER_Y)) |
273 | { |
274 | int tCenterX = (fl & HAVE_TCENTER_X) ? * (const FWORD *) p++ : 0; |
275 | int tCenterY = (fl & HAVE_TCENTER_Y) ? * (const FWORD *) p++ : 0; |
276 | rec_points->x = tCenterX; |
277 | rec_points->y = tCenterY; |
278 | rec_points++; |
279 | } |
280 | |
281 | return true; |
282 | } |
283 | |
284 | void get_transformation_from_points (const contour_point_t *rec_points, |
285 | float (&matrix)[4], contour_point_t &trans) const |
286 | { |
287 | unsigned fl = flags; |
288 | |
289 | if (fl & AXES_HAVE_VARIATION) |
290 | rec_points += numAxes; |
291 | |
292 | matrix[0] = matrix[3] = 1.f; |
293 | matrix[1] = matrix[2] = 0.f; |
294 | trans.init (0.f, 0.f); |
295 | |
296 | float translateX = 0.f; |
297 | float translateY = 0.f; |
298 | float rotation = 0.f; |
299 | float scaleX = 1.f; |
300 | float scaleY = 1.f; |
301 | float skewX = 0.f; |
302 | float skewY = 0.f; |
303 | float tCenterX = 0.f; |
304 | float tCenterY = 0.f; |
305 | |
306 | if (fl & (HAVE_TRANSLATE_X | HAVE_TRANSLATE_Y)) |
307 | { |
308 | translateX = rec_points->x; |
309 | translateY = rec_points->y; |
310 | rec_points++; |
311 | } |
312 | if (fl & HAVE_ROTATION) |
313 | { |
314 | rotation = rec_points->x / (1 << 12); |
315 | rec_points++; |
316 | } |
317 | if (fl & (HAVE_SCALE_X | HAVE_SCALE_Y)) |
318 | { |
319 | scaleX = rec_points->x / (1 << 10); |
320 | scaleY = rec_points->y / (1 << 10); |
321 | rec_points++; |
322 | } |
323 | if (fl & (HAVE_SKEW_X | HAVE_SKEW_Y)) |
324 | { |
325 | skewX = rec_points->x / (1 << 12); |
326 | skewY = rec_points->y / (1 << 12); |
327 | rec_points++; |
328 | } |
329 | if (fl & (HAVE_TCENTER_X | HAVE_TCENTER_Y)) |
330 | { |
331 | tCenterX = rec_points->x; |
332 | tCenterY = rec_points->y; |
333 | rec_points++; |
334 | } |
335 | |
336 | translate (matrix, trans, translateX + tCenterX, translateY + tCenterY); |
337 | rotate (matrix, trans, rotation); |
338 | scale (matrix, trans, scaleX, scaleY); |
339 | skew (matrix, trans, -skewX, skewY); |
340 | translate (matrix, trans, -tCenterX, -tCenterY); |
341 | } |
342 | |
343 | void set_variations (coord_setter_t &setter, |
344 | hb_array_t<contour_point_t> rec_points) const |
345 | { |
346 | bool have_variations = flags & AXES_HAVE_VARIATION; |
347 | unsigned axis_width = (flags & AXIS_INDICES_ARE_SHORT) ? 2 : 1; |
348 | unsigned num_axes = numAxes; |
349 | |
350 | const HBUINT8 *p = (const HBUINT8 *) (((HBUINT8 *) &numAxes) + numAxes.static_size + (flags & GID_IS_24BIT ? 3 : 2)); |
351 | const HBUINT16 *q = (const HBUINT16 *) (((HBUINT8 *) &numAxes) + numAxes.static_size + (flags & GID_IS_24BIT ? 3 : 2)); |
352 | |
353 | const F2DOT14 *a = (const F2DOT14 *) ((HBUINT8 *) (axis_width == 1 ? (p + num_axes) : (HBUINT8 *) (q + num_axes))); |
354 | |
355 | unsigned count = num_axes; |
356 | for (unsigned i = 0; i < count; i++) |
357 | { |
358 | unsigned axis_index = axis_width == 1 ? (unsigned) *p++ : (unsigned) *q++; |
359 | |
360 | signed v = have_variations ? rec_points.arrayZ[i].x : a++->to_int (); |
361 | |
362 | v = hb_clamp (v, -(1<<14), (1<<14)); |
363 | setter[axis_index] = v; |
364 | } |
365 | } |
366 | |
367 | protected: |
368 | HBUINT16 flags; |
369 | HBUINT8 numAxes; |
370 | HBUINT16 pad; |
371 | public: |
372 | DEFINE_SIZE_MIN (5); |
373 | }; |
374 | |
375 | using var_composite_iter_t = composite_iter_tmpl<VarCompositeGlyphRecord>; |
376 | |
377 | struct VarCompositeGlyph |
378 | { |
379 | const GlyphHeader &; |
380 | hb_bytes_t bytes; |
381 | (const GlyphHeader &, hb_bytes_t bytes_) : |
382 | header (header_), bytes (bytes_) {} |
383 | |
384 | var_composite_iter_t iter () const |
385 | { return var_composite_iter_t (bytes, &StructAfter<VarCompositeGlyphRecord, GlyphHeader> (header)); } |
386 | |
387 | const hb_bytes_t trim_padding () const |
388 | { |
389 | unsigned length = GlyphHeader::static_size; |
390 | for (auto &comp : iter ()) |
391 | length += comp.get_size (); |
392 | return bytes.sub_array (0, length); |
393 | } |
394 | }; |
395 | |
396 | |
397 | } /* namespace glyf_impl */ |
398 | } /* namespace OT */ |
399 | |
400 | |
401 | #endif /* OT_GLYF_VARCOMPOSITEGLYPH_HH */ |
402 | |