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