comparison mupdf-source/thirdparty/harfbuzz/src/OT/glyf/CompositeGlyph.hh @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 #ifndef OT_GLYF_COMPOSITEGLYPH_HH
2 #define OT_GLYF_COMPOSITEGLYPH_HH
3
4
5 #include "../../hb-open-type.hh"
6 #include "composite-iter.hh"
7
8
9 namespace OT {
10 namespace glyf_impl {
11
12
13 struct CompositeGlyphRecord
14 {
15 protected:
16 enum composite_glyph_flag_t
17 {
18 ARG_1_AND_2_ARE_WORDS = 0x0001,
19 ARGS_ARE_XY_VALUES = 0x0002,
20 ROUND_XY_TO_GRID = 0x0004,
21 WE_HAVE_A_SCALE = 0x0008,
22 MORE_COMPONENTS = 0x0020,
23 WE_HAVE_AN_X_AND_Y_SCALE = 0x0040,
24 WE_HAVE_A_TWO_BY_TWO = 0x0080,
25 WE_HAVE_INSTRUCTIONS = 0x0100,
26 USE_MY_METRICS = 0x0200,
27 OVERLAP_COMPOUND = 0x0400,
28 SCALED_COMPONENT_OFFSET = 0x0800,
29 UNSCALED_COMPONENT_OFFSET = 0x1000,
30 #ifndef HB_NO_BEYOND_64K
31 GID_IS_24BIT = 0x2000
32 #endif
33 };
34
35 public:
36 unsigned int get_size () const
37 {
38 unsigned int size = min_size;
39 /* glyphIndex is 24bit instead of 16bit */
40 #ifndef HB_NO_BEYOND_64K
41 if (flags & GID_IS_24BIT) size += HBGlyphID24::static_size - HBGlyphID16::static_size;
42 #endif
43 /* arg1 and 2 are int16 */
44 if (flags & ARG_1_AND_2_ARE_WORDS) size += 4;
45 /* arg1 and 2 are int8 */
46 else size += 2;
47
48 /* One x 16 bit (scale) */
49 if (flags & WE_HAVE_A_SCALE) size += 2;
50 /* Two x 16 bit (xscale, yscale) */
51 else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4;
52 /* Four x 16 bit (xscale, scale01, scale10, yscale) */
53 else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8;
54
55 return size;
56 }
57
58 void drop_instructions_flag () { flags = (uint16_t) flags & ~WE_HAVE_INSTRUCTIONS; }
59 void set_overlaps_flag ()
60 {
61 flags = (uint16_t) flags | OVERLAP_COMPOUND;
62 }
63
64 bool has_instructions () const { return flags & WE_HAVE_INSTRUCTIONS; }
65
66 bool has_more () const { return flags & MORE_COMPONENTS; }
67 bool is_use_my_metrics () const { return flags & USE_MY_METRICS; }
68 bool is_anchored () const { return !(flags & ARGS_ARE_XY_VALUES); }
69 void get_anchor_points (unsigned int &point1, unsigned int &point2) const
70 {
71 const auto *p = &StructAfter<const HBUINT8> (flags);
72 #ifndef HB_NO_BEYOND_64K
73 if (flags & GID_IS_24BIT)
74 p += HBGlyphID24::static_size;
75 else
76 #endif
77 p += HBGlyphID16::static_size;
78 if (flags & ARG_1_AND_2_ARE_WORDS)
79 {
80 point1 = ((const HBUINT16 *) p)[0];
81 point2 = ((const HBUINT16 *) p)[1];
82 }
83 else
84 {
85 point1 = p[0];
86 point2 = p[1];
87 }
88 }
89
90 void transform_points (contour_point_vector_t &points) const
91 {
92 float matrix[4];
93 contour_point_t trans;
94 if (get_transformation (matrix, trans))
95 {
96 if (scaled_offsets ())
97 {
98 points.translate (trans);
99 points.transform (matrix);
100 }
101 else
102 {
103 points.transform (matrix);
104 points.translate (trans);
105 }
106 }
107 }
108
109 unsigned compile_with_deltas (const contour_point_t &p_delta,
110 char *out) const
111 {
112 const HBINT8 *p = &StructAfter<const HBINT8> (flags);
113 #ifndef HB_NO_BEYOND_64K
114 if (flags & GID_IS_24BIT)
115 p += HBGlyphID24::static_size;
116 else
117 #endif
118 p += HBGlyphID16::static_size;
119
120 unsigned len = get_size ();
121 unsigned len_before_val = (const char *)p - (const char *)this;
122 if (flags & ARG_1_AND_2_ARE_WORDS)
123 {
124 // no overflow, copy and update value with deltas
125 hb_memcpy (out, this, len);
126
127 const HBINT16 *px = reinterpret_cast<const HBINT16 *> (p);
128 HBINT16 *o = reinterpret_cast<HBINT16 *> (out + len_before_val);
129 o[0] = px[0] + roundf (p_delta.x);
130 o[1] = px[1] + roundf (p_delta.y);
131 }
132 else
133 {
134 int new_x = p[0] + roundf (p_delta.x);
135 int new_y = p[1] + roundf (p_delta.y);
136 if (new_x <= 127 && new_x >= -128 &&
137 new_y <= 127 && new_y >= -128)
138 {
139 hb_memcpy (out, this, len);
140 HBINT8 *o = reinterpret_cast<HBINT8 *> (out + len_before_val);
141 o[0] = new_x;
142 o[1] = new_y;
143 }
144 else
145 {
146 // int8 overflows after deltas applied
147 hb_memcpy (out, this, len_before_val);
148
149 //update flags
150 CompositeGlyphRecord *o = reinterpret_cast<CompositeGlyphRecord *> (out);
151 o->flags = flags | ARG_1_AND_2_ARE_WORDS;
152 out += len_before_val;
153
154 HBINT16 new_value;
155 new_value = new_x;
156 hb_memcpy (out, &new_value, HBINT16::static_size);
157 out += HBINT16::static_size;
158
159 new_value = new_y;
160 hb_memcpy (out, &new_value, HBINT16::static_size);
161 out += HBINT16::static_size;
162
163 hb_memcpy (out, p+2, len - len_before_val - 2);
164 len += 2;
165 }
166 }
167 return len;
168 }
169
170 protected:
171 bool scaled_offsets () const
172 { return (flags & (SCALED_COMPONENT_OFFSET | UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; }
173
174 bool get_transformation (float (&matrix)[4], contour_point_t &trans) const
175 {
176 matrix[0] = matrix[3] = 1.f;
177 matrix[1] = matrix[2] = 0.f;
178
179 const auto *p = &StructAfter<const HBINT8> (flags);
180 #ifndef HB_NO_BEYOND_64K
181 if (flags & GID_IS_24BIT)
182 p += HBGlyphID24::static_size;
183 else
184 #endif
185 p += HBGlyphID16::static_size;
186 int tx, ty;
187 if (flags & ARG_1_AND_2_ARE_WORDS)
188 {
189 tx = *(const HBINT16 *) p;
190 p += HBINT16::static_size;
191 ty = *(const HBINT16 *) p;
192 p += HBINT16::static_size;
193 }
194 else
195 {
196 tx = *p++;
197 ty = *p++;
198 }
199 if (is_anchored ()) tx = ty = 0;
200
201 trans.init ((float) tx, (float) ty);
202
203 {
204 const F2DOT14 *points = (const F2DOT14 *) p;
205 if (flags & WE_HAVE_A_SCALE)
206 {
207 matrix[0] = matrix[3] = points[0].to_float ();
208 return true;
209 }
210 else if (flags & WE_HAVE_AN_X_AND_Y_SCALE)
211 {
212 matrix[0] = points[0].to_float ();
213 matrix[3] = points[1].to_float ();
214 return true;
215 }
216 else if (flags & WE_HAVE_A_TWO_BY_TWO)
217 {
218 matrix[0] = points[0].to_float ();
219 matrix[1] = points[1].to_float ();
220 matrix[2] = points[2].to_float ();
221 matrix[3] = points[3].to_float ();
222 return true;
223 }
224 }
225 return tx || ty;
226 }
227
228 public:
229 hb_codepoint_t get_gid () const
230 {
231 #ifndef HB_NO_BEYOND_64K
232 if (flags & GID_IS_24BIT)
233 return StructAfter<const HBGlyphID24> (flags);
234 else
235 #endif
236 return StructAfter<const HBGlyphID16> (flags);
237 }
238 void set_gid (hb_codepoint_t gid)
239 {
240 #ifndef HB_NO_BEYOND_64K
241 if (flags & GID_IS_24BIT)
242 StructAfter<HBGlyphID24> (flags) = gid;
243 else
244 #endif
245 /* TODO assert? */
246 StructAfter<HBGlyphID16> (flags) = gid;
247 }
248
249 protected:
250 HBUINT16 flags;
251 HBUINT24 pad;
252 public:
253 DEFINE_SIZE_MIN (4);
254 };
255
256 using composite_iter_t = composite_iter_tmpl<CompositeGlyphRecord>;
257
258 struct CompositeGlyph
259 {
260 const GlyphHeader &header;
261 hb_bytes_t bytes;
262 CompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) :
263 header (header_), bytes (bytes_) {}
264
265 composite_iter_t iter () const
266 { return composite_iter_t (bytes, &StructAfter<CompositeGlyphRecord, GlyphHeader> (header)); }
267
268 unsigned int instructions_length (hb_bytes_t bytes) const
269 {
270 unsigned int start = bytes.length;
271 unsigned int end = bytes.length;
272 const CompositeGlyphRecord *last = nullptr;
273 for (auto &item : iter ())
274 last = &item;
275 if (unlikely (!last)) return 0;
276
277 if (last->has_instructions ())
278 start = (char *) last - &bytes + last->get_size ();
279 if (unlikely (start > end)) return 0;
280 return end - start;
281 }
282
283 /* Trimming for composites not implemented.
284 * If removing hints it falls out of that. */
285 const hb_bytes_t trim_padding () const { return bytes; }
286
287 void drop_hints ()
288 {
289 for (const auto &_ : iter ())
290 const_cast<CompositeGlyphRecord &> (_).drop_instructions_flag ();
291 }
292
293 /* Chop instructions off the end */
294 void drop_hints_bytes (hb_bytes_t &dest_start) const
295 { dest_start = bytes.sub_array (0, bytes.length - instructions_length (bytes)); }
296
297 void set_overlaps_flag ()
298 {
299 CompositeGlyphRecord& glyph_chain = const_cast<CompositeGlyphRecord &> (
300 StructAfter<CompositeGlyphRecord, GlyphHeader> (header));
301 if (!bytes.check_range(&glyph_chain, CompositeGlyphRecord::min_size))
302 return;
303 glyph_chain.set_overlaps_flag ();
304 }
305
306 bool compile_bytes_with_deltas (const hb_bytes_t &source_bytes,
307 const contour_point_vector_t &deltas,
308 hb_bytes_t &dest_bytes /* OUT */)
309 {
310 if (source_bytes.length <= GlyphHeader::static_size ||
311 header.numberOfContours != -1)
312 {
313 dest_bytes = hb_bytes_t ();
314 return true;
315 }
316
317 unsigned source_len = source_bytes.length - GlyphHeader::static_size;
318
319 /* try to allocate more memories than source glyph bytes
320 * in case that there might be an overflow for int8 value
321 * and we would need to use int16 instead */
322 char *o = (char *) hb_calloc (source_len + source_len/2, sizeof (char));
323 if (unlikely (!o)) return false;
324
325 const CompositeGlyphRecord *c = reinterpret_cast<const CompositeGlyphRecord *> (source_bytes.arrayZ + GlyphHeader::static_size);
326 auto it = composite_iter_t (hb_bytes_t ((const char *)c, source_len), c);
327
328 char *p = o;
329 unsigned i = 0, source_comp_len = 0;
330 for (const auto &component : it)
331 {
332 /* last 4 points in deltas are phantom points and should not be included */
333 if (i >= deltas.length - 4) return false;
334
335 unsigned comp_len = component.get_size ();
336 if (component.is_anchored ())
337 {
338 hb_memcpy (p, &component, comp_len);
339 p += comp_len;
340 }
341 else
342 {
343 unsigned new_len = component.compile_with_deltas (deltas[i], p);
344 p += new_len;
345 }
346 i++;
347 source_comp_len += comp_len;
348 }
349
350 //copy instructions if any
351 if (source_len > source_comp_len)
352 {
353 unsigned instr_len = source_len - source_comp_len;
354 hb_memcpy (p, (const char *)c + source_comp_len, instr_len);
355 p += instr_len;
356 }
357
358 unsigned len = p - o;
359 dest_bytes = hb_bytes_t (o, len);
360 return true;
361 }
362 };
363
364
365 } /* namespace glyf_impl */
366 } /* namespace OT */
367
368
369 #endif /* OT_GLYF_COMPOSITEGLYPH_HH */