1 /*
2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012,2013 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29 #ifndef HB_OT_LAYOUT_GPOS_TABLE_HH
30 #define HB_OT_LAYOUT_GPOS_TABLE_HH
31
32 #include "hb-ot-layout-gsubgpos.hh"
33
34
35 namespace OT {
36
37
38 /* buffer **position** var allocations */
39 #define attach_chain() var.i16[0] /* glyph to which this attaches to, relative to current glyphs; negative for going back, positive for forward. */
40 #define attach_type() var.u8[2] /* attachment type */
41 /* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */
42
43 enum attach_type_t {
44 ATTACH_TYPE_NONE = 0X00,
45
46 /* Each attachment should be either a mark or a cursive; can't be both. */
47 ATTACH_TYPE_MARK = 0X01,
48 ATTACH_TYPE_CURSIVE = 0X02,
49 };
50
51
52 /* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
53
54 typedef HBUINT16 Value;
55
56 typedef UnsizedArrayOf<Value> ValueRecord;
57
58 struct ValueFormat : HBUINT16
59 {
60 enum Flags {
61 xPlacement = 0x0001u, /* Includes horizontal adjustment for placement */
62 yPlacement = 0x0002u, /* Includes vertical adjustment for placement */
63 xAdvance = 0x0004u, /* Includes horizontal adjustment for advance */
64 yAdvance = 0x0008u, /* Includes vertical adjustment for advance */
65 xPlaDevice = 0x0010u, /* Includes horizontal Device table for placement */
66 yPlaDevice = 0x0020u, /* Includes vertical Device table for placement */
67 xAdvDevice = 0x0040u, /* Includes horizontal Device table for advance */
68 yAdvDevice = 0x0080u, /* Includes vertical Device table for advance */
69 ignored = 0x0F00u, /* Was used in TrueType Open for MM fonts */
70 reserved = 0xF000u, /* For future use */
71
72 devices = 0x00F0u /* Mask for having any Device table */
73 };
74
75 /* All fields are options. Only those available advance the value pointer. */
76 #if 0
77 HBINT16 xPlacement; /* Horizontal adjustment for
78 * placement--in design units */
79 HBINT16 yPlacement; /* Vertical adjustment for
80 * placement--in design units */
81 HBINT16 xAdvance; /* Horizontal adjustment for
82 * advance--in design units (only used
83 * for horizontal writing) */
84 HBINT16 yAdvance; /* Vertical adjustment for advance--in
85 * design units (only used for vertical
86 * writing) */
87 OffsetTo<Device> xPlaDevice; /* Offset to Device table for
88 * horizontal placement--measured from
89 * beginning of PosTable (may be NULL) */
90 OffsetTo<Device> yPlaDevice; /* Offset to Device table for vertical
91 * placement--measured from beginning
92 * of PosTable (may be NULL) */
93 OffsetTo<Device> xAdvDevice; /* Offset to Device table for
94 * horizontal advance--measured from
95 * beginning of PosTable (may be NULL) */
96 OffsetTo<Device> yAdvDevice; /* Offset to Device table for vertical
97 * advance--measured from beginning of
98 * PosTable (may be NULL) */
99 #endif
100
get_lenOT::ValueFormat101 unsigned int get_len () const { return hb_popcount ((unsigned int) *this); }
get_sizeOT::ValueFormat102 unsigned int get_size () const { return get_len () * Value::static_size; }
103
apply_valueOT::ValueFormat104 bool apply_value (hb_ot_apply_context_t *c,
105 const void *base,
106 const Value *values,
107 hb_glyph_position_t &glyph_pos) const
108 {
109 bool ret = false;
110 unsigned int format = *this;
111 if (!format) return ret;
112
113 hb_font_t *font = c->font;
114 bool horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
115
116 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++, &ret));
117 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++, &ret));
118 if (format & xAdvance) {
119 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values, &ret));
120 values++;
121 }
122 /* y_advance values grow downward but font-space grows upward, hence negation */
123 if (format & yAdvance) {
124 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values, &ret));
125 values++;
126 }
127
128 if (!has_device ()) return ret;
129
130 bool use_x_device = font->x_ppem || font->num_coords;
131 bool use_y_device = font->y_ppem || font->num_coords;
132
133 if (!use_x_device && !use_y_device) return ret;
134
135 const VariationStore &store = c->var_store;
136
137 /* pixel -> fractional pixel */
138 if (format & xPlaDevice) {
139 if (use_x_device) glyph_pos.x_offset += (base + get_device (values, &ret)).get_x_delta (font, store);
140 values++;
141 }
142 if (format & yPlaDevice) {
143 if (use_y_device) glyph_pos.y_offset += (base + get_device (values, &ret)).get_y_delta (font, store);
144 values++;
145 }
146 if (format & xAdvDevice) {
147 if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store);
148 values++;
149 }
150 if (format & yAdvDevice) {
151 /* y_advance values grow downward but font-space grows upward, hence negation */
152 if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values, &ret)).get_y_delta (font, store);
153 values++;
154 }
155 return ret;
156 }
157
158 private:
sanitize_value_devicesOT::ValueFormat159 bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
160 {
161 unsigned int format = *this;
162
163 if (format & xPlacement) values++;
164 if (format & yPlacement) values++;
165 if (format & xAdvance) values++;
166 if (format & yAdvance) values++;
167
168 if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
169 if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
170 if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
171 if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
172
173 return true;
174 }
175
get_deviceOT::ValueFormat176 HB_INTERNAL static OffsetTo<Device>& get_device (Value* value)
177 { return *CastP<OffsetTo<Device>> (value); }
get_deviceOT::ValueFormat178 HB_INTERNAL static const OffsetTo<Device>& get_device (const Value* value, bool *worked=nullptr)
179 {
180 if (worked) *worked |= bool (*value);
181 return *CastP<OffsetTo<Device>> (value);
182 }
183
get_shortOT::ValueFormat184 HB_INTERNAL static const HBINT16& get_short (const Value* value, bool *worked=nullptr)
185 {
186 if (worked) *worked |= bool (*value);
187 return *CastP<HBINT16> (value);
188 }
189
190 public:
191
has_deviceOT::ValueFormat192 bool has_device () const
193 {
194 unsigned int format = *this;
195 return (format & devices) != 0;
196 }
197
sanitize_valueOT::ValueFormat198 bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
199 {
200 TRACE_SANITIZE (this);
201 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
202 }
203
sanitize_valuesOT::ValueFormat204 bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
205 {
206 TRACE_SANITIZE (this);
207 unsigned int len = get_len ();
208
209 if (!c->check_range (values, count, get_size ())) return_trace (false);
210
211 if (!has_device ()) return_trace (true);
212
213 for (unsigned int i = 0; i < count; i++) {
214 if (!sanitize_value_devices (c, base, values))
215 return_trace (false);
216 values += len;
217 }
218
219 return_trace (true);
220 }
221
222 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
sanitize_values_stride_unsafeOT::ValueFormat223 bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count, unsigned int stride) const
224 {
225 TRACE_SANITIZE (this);
226
227 if (!has_device ()) return_trace (true);
228
229 for (unsigned int i = 0; i < count; i++) {
230 if (!sanitize_value_devices (c, base, values))
231 return_trace (false);
232 values += stride;
233 }
234
235 return_trace (true);
236 }
237 };
238
239 template<typename Iterator>
240 static inline void SinglePos_serialize (hb_serialize_context_t *c,
241 Iterator it,
242 ValueFormat valFormat);
243
244
245 struct AnchorFormat1
246 {
get_anchorOT::AnchorFormat1247 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
248 float *x, float *y) const
249 {
250 hb_font_t *font = c->font;
251 *x = font->em_fscale_x (xCoordinate);
252 *y = font->em_fscale_y (yCoordinate);
253 }
254
sanitizeOT::AnchorFormat1255 bool sanitize (hb_sanitize_context_t *c) const
256 {
257 TRACE_SANITIZE (this);
258 return_trace (c->check_struct (this));
259 }
260
copyOT::AnchorFormat1261 AnchorFormat1* copy (hb_serialize_context_t *c) const
262 {
263 TRACE_SERIALIZE (this);
264 return_trace (c->embed<AnchorFormat1> (this));
265 }
266
267 protected:
268 HBUINT16 format; /* Format identifier--format = 1 */
269 FWORD xCoordinate; /* Horizontal value--in design units */
270 FWORD yCoordinate; /* Vertical value--in design units */
271 public:
272 DEFINE_SIZE_STATIC (6);
273 };
274
275 struct AnchorFormat2
276 {
get_anchorOT::AnchorFormat2277 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
278 float *x, float *y) const
279 {
280 hb_font_t *font = c->font;
281
282 #ifdef HB_NO_HINTING
283 *x = font->em_fscale_x (xCoordinate);
284 *y = font->em_fscale_y (yCoordinate);
285 return;
286 #endif
287
288 unsigned int x_ppem = font->x_ppem;
289 unsigned int y_ppem = font->y_ppem;
290 hb_position_t cx = 0, cy = 0;
291 bool ret;
292
293 ret = (x_ppem || y_ppem) &&
294 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
295 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
296 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
297 }
298
sanitizeOT::AnchorFormat2299 bool sanitize (hb_sanitize_context_t *c) const
300 {
301 TRACE_SANITIZE (this);
302 return_trace (c->check_struct (this));
303 }
304
copyOT::AnchorFormat2305 AnchorFormat2* copy (hb_serialize_context_t *c) const
306 {
307 TRACE_SERIALIZE (this);
308 return_trace (c->embed<AnchorFormat2> (this));
309 }
310
311 protected:
312 HBUINT16 format; /* Format identifier--format = 2 */
313 FWORD xCoordinate; /* Horizontal value--in design units */
314 FWORD yCoordinate; /* Vertical value--in design units */
315 HBUINT16 anchorPoint; /* Index to glyph contour point */
316 public:
317 DEFINE_SIZE_STATIC (8);
318 };
319
320 struct AnchorFormat3
321 {
get_anchorOT::AnchorFormat3322 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
323 float *x, float *y) const
324 {
325 hb_font_t *font = c->font;
326 *x = font->em_fscale_x (xCoordinate);
327 *y = font->em_fscale_y (yCoordinate);
328
329 if (font->x_ppem || font->num_coords)
330 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
331 if (font->y_ppem || font->num_coords)
332 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
333 }
334
sanitizeOT::AnchorFormat3335 bool sanitize (hb_sanitize_context_t *c) const
336 {
337 TRACE_SANITIZE (this);
338 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
339 }
340
copyOT::AnchorFormat3341 AnchorFormat3* copy (hb_serialize_context_t *c) const
342 {
343 TRACE_SERIALIZE (this);
344 auto *out = c->embed<AnchorFormat3> (this);
345 if (unlikely (!out)) return_trace (nullptr);
346
347 out->xDeviceTable.serialize_copy (c, xDeviceTable, this, out);
348 out->yDeviceTable.serialize_copy (c, yDeviceTable, this, out);
349 return_trace (out);
350 }
351
352 protected:
353 HBUINT16 format; /* Format identifier--format = 3 */
354 FWORD xCoordinate; /* Horizontal value--in design units */
355 FWORD yCoordinate; /* Vertical value--in design units */
356 OffsetTo<Device>
357 xDeviceTable; /* Offset to Device table for X
358 * coordinate-- from beginning of
359 * Anchor table (may be NULL) */
360 OffsetTo<Device>
361 yDeviceTable; /* Offset to Device table for Y
362 * coordinate-- from beginning of
363 * Anchor table (may be NULL) */
364 public:
365 DEFINE_SIZE_STATIC (10);
366 };
367
368 struct Anchor
369 {
get_anchorOT::Anchor370 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
371 float *x, float *y) const
372 {
373 *x = *y = 0;
374 switch (u.format) {
375 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
376 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
377 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
378 default: return;
379 }
380 }
381
sanitizeOT::Anchor382 bool sanitize (hb_sanitize_context_t *c) const
383 {
384 TRACE_SANITIZE (this);
385 if (!u.format.sanitize (c)) return_trace (false);
386 switch (u.format) {
387 case 1: return_trace (u.format1.sanitize (c));
388 case 2: return_trace (u.format2.sanitize (c));
389 case 3: return_trace (u.format3.sanitize (c));
390 default:return_trace (true);
391 }
392 }
393
copyOT::Anchor394 Anchor* copy (hb_serialize_context_t *c) const
395 {
396 TRACE_SERIALIZE (this);
397 switch (u.format) {
398 case 1: return_trace (reinterpret_cast<Anchor *> (u.format1.copy (c)));
399 case 2: return_trace (reinterpret_cast<Anchor *> (u.format2.copy (c)));
400 case 3: return_trace (reinterpret_cast<Anchor *> (u.format3.copy (c)));
401 default:return_trace (nullptr);
402 }
403 }
404
405 protected:
406 union {
407 HBUINT16 format; /* Format identifier */
408 AnchorFormat1 format1;
409 AnchorFormat2 format2;
410 AnchorFormat3 format3;
411 } u;
412 public:
413 DEFINE_SIZE_UNION (2, format);
414 };
415
416
417 struct AnchorMatrix
418 {
get_anchorOT::AnchorMatrix419 const Anchor& get_anchor (unsigned int row, unsigned int col,
420 unsigned int cols, bool *found) const
421 {
422 *found = false;
423 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
424 *found = !matrixZ[row * cols + col].is_null ();
425 return this+matrixZ[row * cols + col];
426 }
427
sanitizeOT::AnchorMatrix428 bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
429 {
430 TRACE_SANITIZE (this);
431 if (!c->check_struct (this)) return_trace (false);
432 if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
433 unsigned int count = rows * cols;
434 if (!c->check_array (matrixZ.arrayZ, count)) return_trace (false);
435 for (unsigned int i = 0; i < count; i++)
436 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
437 return_trace (true);
438 }
439
440 HBUINT16 rows; /* Number of rows */
441 protected:
442 UnsizedArrayOf<OffsetTo<Anchor>>
443 matrixZ; /* Matrix of offsets to Anchor tables--
444 * from beginning of AnchorMatrix table */
445 public:
446 DEFINE_SIZE_ARRAY (2, matrixZ);
447 };
448
449
450 struct MarkRecord
451 {
452 friend struct MarkArray;
453
sanitizeOT::MarkRecord454 bool sanitize (hb_sanitize_context_t *c, const void *base) const
455 {
456 TRACE_SANITIZE (this);
457 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
458 }
459
460 protected:
461 HBUINT16 klass; /* Class defined for this mark */
462 OffsetTo<Anchor>
463 markAnchor; /* Offset to Anchor table--from
464 * beginning of MarkArray table */
465 public:
466 DEFINE_SIZE_STATIC (4);
467 };
468
469 struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
470 {
applyOT::MarkArray471 bool apply (hb_ot_apply_context_t *c,
472 unsigned int mark_index, unsigned int glyph_index,
473 const AnchorMatrix &anchors, unsigned int class_count,
474 unsigned int glyph_pos) const
475 {
476 TRACE_APPLY (this);
477 hb_buffer_t *buffer = c->buffer;
478 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
479 unsigned int mark_class = record.klass;
480
481 const Anchor& mark_anchor = this + record.markAnchor;
482 bool found;
483 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
484 /* If this subtable doesn't have an anchor for this base and this class,
485 * return false such that the subsequent subtables have a chance at it. */
486 if (unlikely (!found)) return_trace (false);
487
488 float mark_x, mark_y, base_x, base_y;
489
490 buffer->unsafe_to_break (glyph_pos, buffer->idx);
491 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
492 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
493
494 hb_glyph_position_t &o = buffer->cur_pos();
495 o.x_offset = roundf (base_x - mark_x);
496 o.y_offset = roundf (base_y - mark_y);
497 o.attach_type() = ATTACH_TYPE_MARK;
498 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
499 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
500
501 buffer->idx++;
502 return_trace (true);
503 }
504
sanitizeOT::MarkArray505 bool sanitize (hb_sanitize_context_t *c) const
506 {
507 TRACE_SANITIZE (this);
508 return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
509 }
510 };
511
512
513 /* Lookups */
514
515 struct SinglePosFormat1
516 {
intersectsOT::SinglePosFormat1517 bool intersects (const hb_set_t *glyphs) const
518 { return (this+coverage).intersects (glyphs); }
519
collect_glyphsOT::SinglePosFormat1520 void collect_glyphs (hb_collect_glyphs_context_t *c) const
521 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
522
get_coverageOT::SinglePosFormat1523 const Coverage &get_coverage () const { return this+coverage; }
524
applyOT::SinglePosFormat1525 bool apply (hb_ot_apply_context_t *c) const
526 {
527 TRACE_APPLY (this);
528 hb_buffer_t *buffer = c->buffer;
529 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
530 if (likely (index == NOT_COVERED)) return_trace (false);
531
532 valueFormat.apply_value (c, this, values, buffer->cur_pos());
533
534 buffer->idx++;
535 return_trace (true);
536 }
537
538 template<typename Iterator,
539 hb_requires (hb_is_iterator (Iterator))>
serializeOT::SinglePosFormat1540 void serialize (hb_serialize_context_t *c,
541 Iterator it,
542 ValueFormat valFormat)
543 {
544 if (unlikely (!c->extend_min (*this))) return;
545 if (unlikely (!c->check_assign (valueFormat, valFormat))) return;
546
547 for (const auto &_ : hb_second (*it))
548 c->copy (_);
549
550 auto glyphs =
551 + it
552 | hb_map_retains_sorting (hb_first)
553 ;
554
555 coverage.serialize (c, this).serialize (c, glyphs);
556 }
557
subsetOT::SinglePosFormat1558 bool subset (hb_subset_context_t *c) const
559 {
560 TRACE_SUBSET (this);
561 const hb_set_t &glyphset = *c->plan->glyphset ();
562 const hb_map_t &glyph_map = *c->plan->glyph_map;
563
564 auto it =
565 + hb_iter (this+coverage)
566 | hb_filter (glyphset)
567 | hb_map_retains_sorting (glyph_map)
568 | hb_zip (hb_repeat (values.as_array (valueFormat.get_len ())))
569 ;
570
571 bool ret = bool (it);
572 SinglePos_serialize (c->serializer, it, valueFormat);
573 return_trace (ret);
574 }
575
sanitizeOT::SinglePosFormat1576 bool sanitize (hb_sanitize_context_t *c) const
577 {
578 TRACE_SANITIZE (this);
579 return_trace (c->check_struct (this) &&
580 coverage.sanitize (c, this) &&
581 valueFormat.sanitize_value (c, this, values));
582 }
583
584 protected:
585 HBUINT16 format; /* Format identifier--format = 1 */
586 OffsetTo<Coverage>
587 coverage; /* Offset to Coverage table--from
588 * beginning of subtable */
589 ValueFormat valueFormat; /* Defines the types of data in the
590 * ValueRecord */
591 ValueRecord values; /* Defines positioning
592 * value(s)--applied to all glyphs in
593 * the Coverage table */
594 public:
595 DEFINE_SIZE_ARRAY (6, values);
596 };
597
598 struct SinglePosFormat2
599 {
intersectsOT::SinglePosFormat2600 bool intersects (const hb_set_t *glyphs) const
601 { return (this+coverage).intersects (glyphs); }
602
collect_glyphsOT::SinglePosFormat2603 void collect_glyphs (hb_collect_glyphs_context_t *c) const
604 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
605
get_coverageOT::SinglePosFormat2606 const Coverage &get_coverage () const { return this+coverage; }
607
applyOT::SinglePosFormat2608 bool apply (hb_ot_apply_context_t *c) const
609 {
610 TRACE_APPLY (this);
611 hb_buffer_t *buffer = c->buffer;
612 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
613 if (likely (index == NOT_COVERED)) return_trace (false);
614
615 if (likely (index >= valueCount)) return_trace (false);
616
617 valueFormat.apply_value (c, this,
618 &values[index * valueFormat.get_len ()],
619 buffer->cur_pos());
620
621 buffer->idx++;
622 return_trace (true);
623 }
624
625 template<typename Iterator,
626 hb_requires (hb_is_iterator (Iterator))>
serializeOT::SinglePosFormat2627 void serialize (hb_serialize_context_t *c,
628 Iterator it,
629 ValueFormat valFormat)
630 {
631 if (unlikely (!c->extend_min (*this))) return;
632 if (unlikely (!c->check_assign (valueFormat, valFormat))) return;
633 if (unlikely (!c->check_assign (valueCount, it.len ()))) return;
634
635 for (const auto iter : it)
636 for (const auto &_ : iter.second)
637 c->copy (_);
638
639 auto glyphs =
640 + it
641 | hb_map_retains_sorting (hb_first)
642 ;
643
644 coverage.serialize (c, this).serialize (c, glyphs);
645 }
646
subsetOT::SinglePosFormat2647 bool subset (hb_subset_context_t *c) const
648 {
649 TRACE_SUBSET (this);
650 const hb_set_t &glyphset = *c->plan->glyphset ();
651 const hb_map_t &glyph_map = *c->plan->glyph_map;
652
653 unsigned sub_length = valueFormat.get_len ();
654 auto values_array = values.as_array (valueCount * sub_length);
655
656 auto it =
657 + hb_zip (this+coverage, hb_range ((unsigned) valueCount))
658 | hb_filter (glyphset, hb_first)
659 | hb_map_retains_sorting ([&] (const hb_pair_t<hb_codepoint_t, unsigned>& _)
660 {
661 return hb_pair (glyph_map[_.first],
662 values_array.sub_array (_.second * sub_length,
663 sub_length));
664 })
665 ;
666
667 bool ret = bool (it);
668 SinglePos_serialize (c->serializer, it, valueFormat);
669 return_trace (ret);
670 }
671
sanitizeOT::SinglePosFormat2672 bool sanitize (hb_sanitize_context_t *c) const
673 {
674 TRACE_SANITIZE (this);
675 return_trace (c->check_struct (this) &&
676 coverage.sanitize (c, this) &&
677 valueFormat.sanitize_values (c, this, values, valueCount));
678 }
679
680 protected:
681 HBUINT16 format; /* Format identifier--format = 2 */
682 OffsetTo<Coverage>
683 coverage; /* Offset to Coverage table--from
684 * beginning of subtable */
685 ValueFormat valueFormat; /* Defines the types of data in the
686 * ValueRecord */
687 HBUINT16 valueCount; /* Number of ValueRecords */
688 ValueRecord values; /* Array of ValueRecords--positioning
689 * values applied to glyphs */
690 public:
691 DEFINE_SIZE_ARRAY (8, values);
692 };
693
694 struct SinglePos
695 {
696 template<typename Iterator,
697 hb_requires (hb_is_iterator (Iterator))>
get_formatOT::SinglePos698 unsigned get_format (Iterator glyph_val_iter_pairs)
699 {
700 hb_array_t<const Value> first_val_iter = hb_second (*glyph_val_iter_pairs);
701
702 for (const auto iter : glyph_val_iter_pairs)
703 for (const auto _ : hb_zip (iter.second, first_val_iter))
704 if (_.first != _.second)
705 return 2;
706
707 return 1;
708 }
709
710
711 template<typename Iterator,
712 hb_requires (hb_is_iterator (Iterator))>
serializeOT::SinglePos713 void serialize (hb_serialize_context_t *c,
714 Iterator glyph_val_iter_pairs,
715 ValueFormat valFormat)
716 {
717 if (unlikely (!c->extend_min (u.format))) return;
718 unsigned format = 2;
719
720 if (glyph_val_iter_pairs) format = get_format (glyph_val_iter_pairs);
721
722 u.format = format;
723 switch (u.format) {
724 case 1: u.format1.serialize (c, glyph_val_iter_pairs, valFormat);
725 return;
726 case 2: u.format2.serialize (c, glyph_val_iter_pairs, valFormat);
727 return;
728 default:return;
729 }
730 }
731
732 template <typename context_t, typename ...Ts>
dispatchOT::SinglePos733 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
734 {
735 TRACE_DISPATCH (this, u.format);
736 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
737 switch (u.format) {
738 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
739 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
740 default:return_trace (c->default_return_value ());
741 }
742 }
743
744 protected:
745 union {
746 HBUINT16 format; /* Format identifier */
747 SinglePosFormat1 format1;
748 SinglePosFormat2 format2;
749 } u;
750 };
751
752 template<typename Iterator>
753 static inline void
SinglePos_serialize(hb_serialize_context_t * c,Iterator it,ValueFormat valFormat)754 SinglePos_serialize (hb_serialize_context_t *c,
755 Iterator it,
756 ValueFormat valFormat)
757 { c->start_embed<SinglePos> ()->serialize (c, it, valFormat); }
758
759
760 struct PairValueRecord
761 {
762 friend struct PairSet;
763
serializeOT::PairValueRecord764 bool serialize (hb_serialize_context_t *c,
765 unsigned length,
766 const hb_map_t &glyph_map) const
767 {
768 TRACE_SERIALIZE (this);
769 auto *out = c->start_embed (*this);
770 if (unlikely (!c->extend_min (out))) return_trace (false);
771
772 out->secondGlyph = glyph_map[secondGlyph];
773 return_trace (c->copy (values, length));
774 }
775
776 protected:
777 HBGlyphID secondGlyph; /* GlyphID of second glyph in the
778 * pair--first glyph is listed in the
779 * Coverage table */
780 ValueRecord values; /* Positioning data for the first glyph
781 * followed by for second glyph */
782 public:
783 DEFINE_SIZE_ARRAY (2, values);
784 };
785
786 struct PairSet
787 {
788 friend struct PairPosFormat1;
789
intersectsOT::PairSet790 bool intersects (const hb_set_t *glyphs,
791 const ValueFormat *valueFormats) const
792 {
793 unsigned int len1 = valueFormats[0].get_len ();
794 unsigned int len2 = valueFormats[1].get_len ();
795 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
796
797 const PairValueRecord *record = &firstPairValueRecord;
798 unsigned int count = len;
799 for (unsigned int i = 0; i < count; i++)
800 {
801 if (glyphs->has (record->secondGlyph))
802 return true;
803 record = &StructAtOffset<const PairValueRecord> (record, record_size);
804 }
805 return false;
806 }
807
collect_glyphsOT::PairSet808 void collect_glyphs (hb_collect_glyphs_context_t *c,
809 const ValueFormat *valueFormats) const
810 {
811 unsigned int len1 = valueFormats[0].get_len ();
812 unsigned int len2 = valueFormats[1].get_len ();
813 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
814
815 const PairValueRecord *record = &firstPairValueRecord;
816 c->input->add_array (&record->secondGlyph, len, record_size);
817 }
818
applyOT::PairSet819 bool apply (hb_ot_apply_context_t *c,
820 const ValueFormat *valueFormats,
821 unsigned int pos) const
822 {
823 TRACE_APPLY (this);
824 hb_buffer_t *buffer = c->buffer;
825 unsigned int len1 = valueFormats[0].get_len ();
826 unsigned int len2 = valueFormats[1].get_len ();
827 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
828
829 unsigned int count = len;
830
831 /* Hand-coded bsearch. */
832 if (unlikely (!count))
833 return_trace (false);
834 hb_codepoint_t x = buffer->info[pos].codepoint;
835 int min = 0, max = (int) count - 1;
836 while (min <= max)
837 {
838 int mid = ((unsigned int) min + (unsigned int) max) / 2;
839 const PairValueRecord *record = &StructAtOffset<PairValueRecord> (&firstPairValueRecord, record_size * mid);
840 hb_codepoint_t mid_x = record->secondGlyph;
841 if (x < mid_x)
842 max = mid - 1;
843 else if (x > mid_x)
844 min = mid + 1;
845 else
846 {
847 /* Note the intentional use of "|" instead of short-circuit "||". */
848 if (valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos()) |
849 valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]))
850 buffer->unsafe_to_break (buffer->idx, pos + 1);
851 if (len2)
852 pos++;
853 buffer->idx = pos;
854 return_trace (true);
855 }
856 }
857
858 return_trace (false);
859 }
860
subsetOT::PairSet861 bool subset (hb_subset_context_t *c,
862 const ValueFormat valueFormats[2]) const
863 {
864 TRACE_SUBSET (this);
865 auto snap = c->serializer->snapshot ();
866
867 auto *out = c->serializer->start_embed (*this);
868 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
869 out->len = 0;
870
871 const hb_set_t &glyphset = *c->plan->glyphset ();
872 const hb_map_t &glyph_map = *c->plan->glyph_map;
873
874 unsigned len1 = valueFormats[0].get_len ();
875 unsigned len2 = valueFormats[1].get_len ();
876 unsigned record_size = HBUINT16::static_size + Value::static_size * (len1 + len2);
877
878 const PairValueRecord *record = &firstPairValueRecord;
879 unsigned count = len, num = 0;
880 for (unsigned i = 0; i < count; i++)
881 {
882 if (!glyphset.has (record->secondGlyph)) continue;
883 if (record->serialize (c->serializer, len1 + len2, glyph_map)) num++;
884 record = &StructAtOffset<const PairValueRecord> (record, record_size);
885 }
886
887 out->len = num;
888 if (!num) c->serializer->revert (snap);
889 return_trace (num);
890 }
891
892 struct sanitize_closure_t
893 {
894 const void *base;
895 const ValueFormat *valueFormats;
896 unsigned int len1; /* valueFormats[0].get_len() */
897 unsigned int stride; /* 1 + len1 + len2 */
898 };
899
sanitizeOT::PairSet900 bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
901 {
902 TRACE_SANITIZE (this);
903 if (!(c->check_struct (this)
904 && c->check_range (&firstPairValueRecord,
905 len,
906 HBUINT16::static_size,
907 closure->stride))) return_trace (false);
908
909 unsigned int count = len;
910 const PairValueRecord *record = &firstPairValueRecord;
911 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
912 closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
913 }
914
915 protected:
916 HBUINT16 len; /* Number of PairValueRecords */
917 PairValueRecord firstPairValueRecord;
918 /* Array of PairValueRecords--ordered
919 * by GlyphID of the second glyph */
920 public:
921 DEFINE_SIZE_MIN (2);
922 };
923
924 struct PairPosFormat1
925 {
intersectsOT::PairPosFormat1926 bool intersects (const hb_set_t *glyphs) const
927 {
928 return
929 + hb_zip (this+coverage, pairSet)
930 | hb_filter (*glyphs, hb_first)
931 | hb_map (hb_second)
932 | hb_map ([glyphs, this] (const OffsetTo<PairSet> &_)
933 { return (this+_).intersects (glyphs, valueFormat); })
934 | hb_any
935 ;
936 }
937
collect_glyphsOT::PairPosFormat1938 void collect_glyphs (hb_collect_glyphs_context_t *c) const
939 {
940 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
941 unsigned int count = pairSet.len;
942 for (unsigned int i = 0; i < count; i++)
943 (this+pairSet[i]).collect_glyphs (c, valueFormat);
944 }
945
get_coverageOT::PairPosFormat1946 const Coverage &get_coverage () const { return this+coverage; }
947
applyOT::PairPosFormat1948 bool apply (hb_ot_apply_context_t *c) const
949 {
950 TRACE_APPLY (this);
951 hb_buffer_t *buffer = c->buffer;
952 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
953 if (likely (index == NOT_COVERED)) return_trace (false);
954
955 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
956 skippy_iter.reset (buffer->idx, 1);
957 if (!skippy_iter.next ()) return_trace (false);
958
959 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
960 }
961
subsetOT::PairPosFormat1962 bool subset (hb_subset_context_t *c) const
963 {
964 TRACE_SUBSET (this);
965
966 const hb_set_t &glyphset = *c->plan->glyphset ();
967 const hb_map_t &glyph_map = *c->plan->glyph_map;
968
969 auto *out = c->serializer->start_embed (*this);
970 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
971 out->format = format;
972 out->valueFormat[0] = valueFormat[0];
973 out->valueFormat[1] = valueFormat[1];
974
975 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
976
977 + hb_zip (this+coverage, pairSet)
978 | hb_filter (glyphset, hb_first)
979 | hb_filter ([this, c, out] (const OffsetTo<PairSet>& _)
980 {
981 auto *o = out->pairSet.serialize_append (c->serializer);
982 if (unlikely (!o)) return false;
983 auto snap = c->serializer->snapshot ();
984 bool ret = o->serialize_subset (c, _, this, out, valueFormat);
985 if (!ret)
986 {
987 out->pairSet.pop ();
988 c->serializer->revert (snap);
989 }
990 return ret;
991 },
992 hb_second)
993 | hb_map (hb_first)
994 | hb_map (glyph_map)
995 | hb_sink (new_coverage)
996 ;
997
998 out->coverage.serialize (c->serializer, out)
999 .serialize (c->serializer, new_coverage.iter ());
1000
1001 return_trace (bool (new_coverage));
1002 }
1003
sanitizeOT::PairPosFormat11004 bool sanitize (hb_sanitize_context_t *c) const
1005 {
1006 TRACE_SANITIZE (this);
1007
1008 if (!c->check_struct (this)) return_trace (false);
1009
1010 unsigned int len1 = valueFormat[0].get_len ();
1011 unsigned int len2 = valueFormat[1].get_len ();
1012 PairSet::sanitize_closure_t closure =
1013 {
1014 this,
1015 valueFormat,
1016 len1,
1017 1 + len1 + len2
1018 };
1019
1020 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
1021 }
1022
1023 protected:
1024 HBUINT16 format; /* Format identifier--format = 1 */
1025 OffsetTo<Coverage>
1026 coverage; /* Offset to Coverage table--from
1027 * beginning of subtable */
1028 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
1029 * ValueRecord1--for the first glyph
1030 * in the pair--may be zero (0) */
1031 /* [1] Defines the types of data in
1032 * ValueRecord2--for the second glyph
1033 * in the pair--may be zero (0) */
1034 OffsetArrayOf<PairSet>
1035 pairSet; /* Array of PairSet tables
1036 * ordered by Coverage Index */
1037 public:
1038 DEFINE_SIZE_ARRAY (10, pairSet);
1039 };
1040
1041 struct PairPosFormat2
1042 {
intersectsOT::PairPosFormat21043 bool intersects (const hb_set_t *glyphs) const
1044 {
1045 return (this+coverage).intersects (glyphs) &&
1046 (this+classDef2).intersects (glyphs);
1047 }
1048
collect_glyphsOT::PairPosFormat21049 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1050 {
1051 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
1052 if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
1053 }
1054
get_coverageOT::PairPosFormat21055 const Coverage &get_coverage () const { return this+coverage; }
1056
applyOT::PairPosFormat21057 bool apply (hb_ot_apply_context_t *c) const
1058 {
1059 TRACE_APPLY (this);
1060 hb_buffer_t *buffer = c->buffer;
1061 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
1062 if (likely (index == NOT_COVERED)) return_trace (false);
1063
1064 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1065 skippy_iter.reset (buffer->idx, 1);
1066 if (!skippy_iter.next ()) return_trace (false);
1067
1068 unsigned int len1 = valueFormat1.get_len ();
1069 unsigned int len2 = valueFormat2.get_len ();
1070 unsigned int record_len = len1 + len2;
1071
1072 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
1073 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
1074 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
1075
1076 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
1077 /* Note the intentional use of "|" instead of short-circuit "||". */
1078 if (valueFormat1.apply_value (c, this, v, buffer->cur_pos()) |
1079 valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]))
1080 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
1081
1082 buffer->idx = skippy_iter.idx;
1083 if (len2)
1084 buffer->idx++;
1085
1086 return_trace (true);
1087 }
1088
subsetOT::PairPosFormat21089 bool subset (hb_subset_context_t *c) const
1090 {
1091 TRACE_SUBSET (this);
1092 auto *out = c->serializer->start_embed (*this);
1093 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
1094 out->format = format;
1095 out->valueFormat1 = valueFormat1;
1096 out->valueFormat2 = valueFormat2;
1097
1098 hb_map_t klass1_map;
1099 out->classDef1.serialize_subset (c, classDef1, this, out, &klass1_map);
1100 out->class1Count = klass1_map.get_population ();
1101
1102 hb_map_t klass2_map;
1103 out->classDef2.serialize_subset (c, classDef2, this, out, &klass2_map);
1104 out->class2Count = klass2_map.get_population ();
1105
1106 unsigned record_len = valueFormat1.get_len () + valueFormat2.get_len ();
1107
1108 + hb_range ((unsigned) class1Count)
1109 | hb_filter (klass1_map)
1110 | hb_apply ([&] (const unsigned class1_idx)
1111 {
1112 + hb_range ((unsigned) class2Count)
1113 | hb_filter (klass2_map)
1114 | hb_apply ([&] (const unsigned class2_idx)
1115 {
1116 unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * record_len;
1117 for (unsigned i = 0; i < record_len; i++)
1118 c->serializer->copy (values[idx+i]);
1119 })
1120 ;
1121 })
1122 ;
1123
1124 const hb_set_t &glyphset = *c->plan->glyphset ();
1125 const hb_map_t &glyph_map = *c->plan->glyph_map;
1126
1127 auto it =
1128 + hb_iter (this+coverage)
1129 | hb_filter (glyphset)
1130 | hb_map_retains_sorting (glyph_map)
1131 ;
1132
1133 out->coverage.serialize (c->serializer, out).serialize (c->serializer, it);
1134 return_trace (out->class1Count && out->class2Count && bool (it));
1135 }
1136
sanitizeOT::PairPosFormat21137 bool sanitize (hb_sanitize_context_t *c) const
1138 {
1139 TRACE_SANITIZE (this);
1140 if (!(c->check_struct (this)
1141 && coverage.sanitize (c, this)
1142 && classDef1.sanitize (c, this)
1143 && classDef2.sanitize (c, this))) return_trace (false);
1144
1145 unsigned int len1 = valueFormat1.get_len ();
1146 unsigned int len2 = valueFormat2.get_len ();
1147 unsigned int stride = len1 + len2;
1148 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
1149 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
1150 return_trace (c->check_range ((const void *) values,
1151 count,
1152 record_size) &&
1153 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
1154 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
1155 }
1156
1157 protected:
1158 HBUINT16 format; /* Format identifier--format = 2 */
1159 OffsetTo<Coverage>
1160 coverage; /* Offset to Coverage table--from
1161 * beginning of subtable */
1162 ValueFormat valueFormat1; /* ValueRecord definition--for the
1163 * first glyph of the pair--may be zero
1164 * (0) */
1165 ValueFormat valueFormat2; /* ValueRecord definition--for the
1166 * second glyph of the pair--may be
1167 * zero (0) */
1168 OffsetTo<ClassDef>
1169 classDef1; /* Offset to ClassDef table--from
1170 * beginning of PairPos subtable--for
1171 * the first glyph of the pair */
1172 OffsetTo<ClassDef>
1173 classDef2; /* Offset to ClassDef table--from
1174 * beginning of PairPos subtable--for
1175 * the second glyph of the pair */
1176 HBUINT16 class1Count; /* Number of classes in ClassDef1
1177 * table--includes Class0 */
1178 HBUINT16 class2Count; /* Number of classes in ClassDef2
1179 * table--includes Class0 */
1180 ValueRecord values; /* Matrix of value pairs:
1181 * class1-major, class2-minor,
1182 * Each entry has value1 and value2 */
1183 public:
1184 DEFINE_SIZE_ARRAY (16, values);
1185 };
1186
1187 struct PairPos
1188 {
1189 template <typename context_t, typename ...Ts>
dispatchOT::PairPos1190 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1191 {
1192 TRACE_DISPATCH (this, u.format);
1193 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1194 switch (u.format) {
1195 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1196 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
1197 default:return_trace (c->default_return_value ());
1198 }
1199 }
1200
1201 protected:
1202 union {
1203 HBUINT16 format; /* Format identifier */
1204 PairPosFormat1 format1;
1205 PairPosFormat2 format2;
1206 } u;
1207 };
1208
1209
1210 struct EntryExitRecord
1211 {
1212 friend struct CursivePosFormat1;
1213
sanitizeOT::EntryExitRecord1214 bool sanitize (hb_sanitize_context_t *c, const void *base) const
1215 {
1216 TRACE_SANITIZE (this);
1217 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
1218 }
1219
copyOT::EntryExitRecord1220 EntryExitRecord* copy (hb_serialize_context_t *c,
1221 const void *src_base,
1222 const void *dst_base) const
1223 {
1224 TRACE_SERIALIZE (this);
1225 auto *out = c->embed (this);
1226 if (unlikely (!out)) return_trace (nullptr);
1227
1228 out->entryAnchor.serialize_copy (c, entryAnchor, src_base, dst_base);
1229 out->exitAnchor.serialize_copy (c, exitAnchor, src_base, dst_base);
1230 return_trace (out);
1231 }
1232
1233 protected:
1234 OffsetTo<Anchor>
1235 entryAnchor; /* Offset to EntryAnchor table--from
1236 * beginning of CursivePos
1237 * subtable--may be NULL */
1238 OffsetTo<Anchor>
1239 exitAnchor; /* Offset to ExitAnchor table--from
1240 * beginning of CursivePos
1241 * subtable--may be NULL */
1242 public:
1243 DEFINE_SIZE_STATIC (4);
1244 };
1245
1246 static void
1247 reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
1248
1249 struct CursivePosFormat1
1250 {
intersectsOT::CursivePosFormat11251 bool intersects (const hb_set_t *glyphs) const
1252 { return (this+coverage).intersects (glyphs); }
1253
collect_glyphsOT::CursivePosFormat11254 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1255 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
1256
get_coverageOT::CursivePosFormat11257 const Coverage &get_coverage () const { return this+coverage; }
1258
applyOT::CursivePosFormat11259 bool apply (hb_ot_apply_context_t *c) const
1260 {
1261 TRACE_APPLY (this);
1262 hb_buffer_t *buffer = c->buffer;
1263
1264 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
1265 if (!this_record.entryAnchor) return_trace (false);
1266
1267 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1268 skippy_iter.reset (buffer->idx, 1);
1269 if (!skippy_iter.prev ()) return_trace (false);
1270
1271 const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
1272 if (!prev_record.exitAnchor) return_trace (false);
1273
1274 unsigned int i = skippy_iter.idx;
1275 unsigned int j = buffer->idx;
1276
1277 buffer->unsafe_to_break (i, j);
1278 float entry_x, entry_y, exit_x, exit_y;
1279 (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
1280 (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
1281
1282 hb_glyph_position_t *pos = buffer->pos;
1283
1284 hb_position_t d;
1285 /* Main-direction adjustment */
1286 switch (c->direction) {
1287 case HB_DIRECTION_LTR:
1288 pos[i].x_advance = roundf (exit_x) + pos[i].x_offset;
1289
1290 d = roundf (entry_x) + pos[j].x_offset;
1291 pos[j].x_advance -= d;
1292 pos[j].x_offset -= d;
1293 break;
1294 case HB_DIRECTION_RTL:
1295 d = roundf (exit_x) + pos[i].x_offset;
1296 pos[i].x_advance -= d;
1297 pos[i].x_offset -= d;
1298
1299 pos[j].x_advance = roundf (entry_x) + pos[j].x_offset;
1300 break;
1301 case HB_DIRECTION_TTB:
1302 pos[i].y_advance = roundf (exit_y) + pos[i].y_offset;
1303
1304 d = roundf (entry_y) + pos[j].y_offset;
1305 pos[j].y_advance -= d;
1306 pos[j].y_offset -= d;
1307 break;
1308 case HB_DIRECTION_BTT:
1309 d = roundf (exit_y) + pos[i].y_offset;
1310 pos[i].y_advance -= d;
1311 pos[i].y_offset -= d;
1312
1313 pos[j].y_advance = roundf (entry_y);
1314 break;
1315 case HB_DIRECTION_INVALID:
1316 default:
1317 break;
1318 }
1319
1320 /* Cross-direction adjustment */
1321
1322 /* We attach child to parent (think graph theory and rooted trees whereas
1323 * the root stays on baseline and each node aligns itself against its
1324 * parent.
1325 *
1326 * Optimize things for the case of RightToLeft, as that's most common in
1327 * Arabic. */
1328 unsigned int child = i;
1329 unsigned int parent = j;
1330 hb_position_t x_offset = entry_x - exit_x;
1331 hb_position_t y_offset = entry_y - exit_y;
1332 if (!(c->lookup_props & LookupFlag::RightToLeft))
1333 {
1334 unsigned int k = child;
1335 child = parent;
1336 parent = k;
1337 x_offset = -x_offset;
1338 y_offset = -y_offset;
1339 }
1340
1341 /* If child was already connected to someone else, walk through its old
1342 * chain and reverse the link direction, such that the whole tree of its
1343 * previous connection now attaches to new parent. Watch out for case
1344 * where new parent is on the path from old chain...
1345 */
1346 reverse_cursive_minor_offset (pos, child, c->direction, parent);
1347
1348 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
1349 pos[child].attach_chain() = (int) parent - (int) child;
1350 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
1351 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
1352 pos[child].y_offset = y_offset;
1353 else
1354 pos[child].x_offset = x_offset;
1355
1356 buffer->idx++;
1357 return_trace (true);
1358 }
1359
1360 template <typename Iterator,
1361 hb_requires (hb_is_iterator (Iterator))>
serializeOT::CursivePosFormat11362 void serialize (hb_serialize_context_t *c,
1363 Iterator it,
1364 const void *src_base)
1365 {
1366 if (unlikely (!c->extend_min ((*this)))) return;
1367 this->format = 1;
1368 this->entryExitRecord.len = it.len ();
1369
1370 for (const EntryExitRecord& entry_record : + it
1371 | hb_map (hb_second))
1372 c->copy (entry_record, src_base, this);
1373
1374 auto glyphs =
1375 + it
1376 | hb_map_retains_sorting (hb_first)
1377 ;
1378
1379 coverage.serialize (c, this).serialize (c, glyphs);
1380 }
1381
subsetOT::CursivePosFormat11382 bool subset (hb_subset_context_t *c) const
1383 {
1384 TRACE_SUBSET (this);
1385 const hb_set_t &glyphset = *c->plan->glyphset ();
1386 const hb_map_t &glyph_map = *c->plan->glyph_map;
1387
1388 auto *out = c->serializer->start_embed (*this);
1389 if (unlikely (!out)) return_trace (false);
1390
1391 auto it =
1392 + hb_zip (this+coverage, entryExitRecord)
1393 | hb_filter (glyphset, hb_first)
1394 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const EntryExitRecord&> p) -> hb_pair_t<hb_codepoint_t, const EntryExitRecord&>
1395 { return hb_pair (glyph_map[p.first], p.second);})
1396 ;
1397
1398 bool ret = bool (it);
1399 out->serialize (c->serializer, it, this);
1400 return_trace (ret);
1401 }
1402
sanitizeOT::CursivePosFormat11403 bool sanitize (hb_sanitize_context_t *c) const
1404 {
1405 TRACE_SANITIZE (this);
1406 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
1407 }
1408
1409 protected:
1410 HBUINT16 format; /* Format identifier--format = 1 */
1411 OffsetTo<Coverage>
1412 coverage; /* Offset to Coverage table--from
1413 * beginning of subtable */
1414 ArrayOf<EntryExitRecord>
1415 entryExitRecord; /* Array of EntryExit records--in
1416 * Coverage Index order */
1417 public:
1418 DEFINE_SIZE_ARRAY (6, entryExitRecord);
1419 };
1420
1421 struct CursivePos
1422 {
1423 template <typename context_t, typename ...Ts>
dispatchOT::CursivePos1424 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1425 {
1426 TRACE_DISPATCH (this, u.format);
1427 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1428 switch (u.format) {
1429 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1430 default:return_trace (c->default_return_value ());
1431 }
1432 }
1433
1434 protected:
1435 union {
1436 HBUINT16 format; /* Format identifier */
1437 CursivePosFormat1 format1;
1438 } u;
1439 };
1440
1441
1442 typedef AnchorMatrix BaseArray; /* base-major--
1443 * in order of BaseCoverage Index--,
1444 * mark-minor--
1445 * ordered by class--zero-based. */
1446
1447 struct MarkBasePosFormat1
1448 {
intersectsOT::MarkBasePosFormat11449 bool intersects (const hb_set_t *glyphs) const
1450 { return (this+markCoverage).intersects (glyphs) &&
1451 (this+baseCoverage).intersects (glyphs); }
1452
collect_glyphsOT::MarkBasePosFormat11453 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1454 {
1455 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1456 if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
1457 }
1458
get_coverageOT::MarkBasePosFormat11459 const Coverage &get_coverage () const { return this+markCoverage; }
1460
applyOT::MarkBasePosFormat11461 bool apply (hb_ot_apply_context_t *c) const
1462 {
1463 TRACE_APPLY (this);
1464 hb_buffer_t *buffer = c->buffer;
1465 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1466 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1467
1468 /* Now we search backwards for a non-mark glyph */
1469 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1470 skippy_iter.reset (buffer->idx, 1);
1471 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1472 do {
1473 if (!skippy_iter.prev ()) return_trace (false);
1474 /* We only want to attach to the first of a MultipleSubst sequence.
1475 * https://github.com/harfbuzz/harfbuzz/issues/740
1476 * Reject others...
1477 * ...but stop if we find a mark in the MultipleSubst sequence:
1478 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
1479 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
1480 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
1481 (skippy_iter.idx == 0 ||
1482 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
1483 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
1484 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
1485 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
1486 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
1487 ))
1488 break;
1489 skippy_iter.reject ();
1490 } while (true);
1491
1492 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
1493 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1494
1495 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
1496 if (base_index == NOT_COVERED) return_trace (false);
1497
1498 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
1499 }
1500
subsetOT::MarkBasePosFormat11501 bool subset (hb_subset_context_t *c) const
1502 {
1503 TRACE_SUBSET (this);
1504 // TODO(subset)
1505 return_trace (false);
1506 }
1507
sanitizeOT::MarkBasePosFormat11508 bool sanitize (hb_sanitize_context_t *c) const
1509 {
1510 TRACE_SANITIZE (this);
1511 return_trace (c->check_struct (this) &&
1512 markCoverage.sanitize (c, this) &&
1513 baseCoverage.sanitize (c, this) &&
1514 markArray.sanitize (c, this) &&
1515 baseArray.sanitize (c, this, (unsigned int) classCount));
1516 }
1517
1518 protected:
1519 HBUINT16 format; /* Format identifier--format = 1 */
1520 OffsetTo<Coverage>
1521 markCoverage; /* Offset to MarkCoverage table--from
1522 * beginning of MarkBasePos subtable */
1523 OffsetTo<Coverage>
1524 baseCoverage; /* Offset to BaseCoverage table--from
1525 * beginning of MarkBasePos subtable */
1526 HBUINT16 classCount; /* Number of classes defined for marks */
1527 OffsetTo<MarkArray>
1528 markArray; /* Offset to MarkArray table--from
1529 * beginning of MarkBasePos subtable */
1530 OffsetTo<BaseArray>
1531 baseArray; /* Offset to BaseArray table--from
1532 * beginning of MarkBasePos subtable */
1533 public:
1534 DEFINE_SIZE_STATIC (12);
1535 };
1536
1537 struct MarkBasePos
1538 {
1539 template <typename context_t, typename ...Ts>
dispatchOT::MarkBasePos1540 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1541 {
1542 TRACE_DISPATCH (this, u.format);
1543 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1544 switch (u.format) {
1545 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1546 default:return_trace (c->default_return_value ());
1547 }
1548 }
1549
1550 protected:
1551 union {
1552 HBUINT16 format; /* Format identifier */
1553 MarkBasePosFormat1 format1;
1554 } u;
1555 };
1556
1557
1558 typedef AnchorMatrix LigatureAttach; /* component-major--
1559 * in order of writing direction--,
1560 * mark-minor--
1561 * ordered by class--zero-based. */
1562
1563 typedef OffsetListOf<LigatureAttach> LigatureArray;
1564 /* Array of LigatureAttach
1565 * tables ordered by
1566 * LigatureCoverage Index */
1567
1568 struct MarkLigPosFormat1
1569 {
intersectsOT::MarkLigPosFormat11570 bool intersects (const hb_set_t *glyphs) const
1571 { return (this+markCoverage).intersects (glyphs) &&
1572 (this+ligatureCoverage).intersects (glyphs); }
1573
collect_glyphsOT::MarkLigPosFormat11574 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1575 {
1576 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1577 if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
1578 }
1579
get_coverageOT::MarkLigPosFormat11580 const Coverage &get_coverage () const { return this+markCoverage; }
1581
applyOT::MarkLigPosFormat11582 bool apply (hb_ot_apply_context_t *c) const
1583 {
1584 TRACE_APPLY (this);
1585 hb_buffer_t *buffer = c->buffer;
1586 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1587 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1588
1589 /* Now we search backwards for a non-mark glyph */
1590 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1591 skippy_iter.reset (buffer->idx, 1);
1592 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1593 if (!skippy_iter.prev ()) return_trace (false);
1594
1595 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
1596 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1597
1598 unsigned int j = skippy_iter.idx;
1599 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
1600 if (lig_index == NOT_COVERED) return_trace (false);
1601
1602 const LigatureArray& lig_array = this+ligatureArray;
1603 const LigatureAttach& lig_attach = lig_array[lig_index];
1604
1605 /* Find component to attach to */
1606 unsigned int comp_count = lig_attach.rows;
1607 if (unlikely (!comp_count)) return_trace (false);
1608
1609 /* We must now check whether the ligature ID of the current mark glyph
1610 * is identical to the ligature ID of the found ligature. If yes, we
1611 * can directly use the component index. If not, we attach the mark
1612 * glyph to the last component of the ligature. */
1613 unsigned int comp_index;
1614 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1615 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1616 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
1617 if (lig_id && lig_id == mark_id && mark_comp > 0)
1618 comp_index = hb_min (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
1619 else
1620 comp_index = comp_count - 1;
1621
1622 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
1623 }
1624
subsetOT::MarkLigPosFormat11625 bool subset (hb_subset_context_t *c) const
1626 {
1627 TRACE_SUBSET (this);
1628 // TODO(subset)
1629 return_trace (false);
1630 }
1631
sanitizeOT::MarkLigPosFormat11632 bool sanitize (hb_sanitize_context_t *c) const
1633 {
1634 TRACE_SANITIZE (this);
1635 return_trace (c->check_struct (this) &&
1636 markCoverage.sanitize (c, this) &&
1637 ligatureCoverage.sanitize (c, this) &&
1638 markArray.sanitize (c, this) &&
1639 ligatureArray.sanitize (c, this, (unsigned int) classCount));
1640 }
1641
1642 protected:
1643 HBUINT16 format; /* Format identifier--format = 1 */
1644 OffsetTo<Coverage>
1645 markCoverage; /* Offset to Mark Coverage table--from
1646 * beginning of MarkLigPos subtable */
1647 OffsetTo<Coverage>
1648 ligatureCoverage; /* Offset to Ligature Coverage
1649 * table--from beginning of MarkLigPos
1650 * subtable */
1651 HBUINT16 classCount; /* Number of defined mark classes */
1652 OffsetTo<MarkArray>
1653 markArray; /* Offset to MarkArray table--from
1654 * beginning of MarkLigPos subtable */
1655 OffsetTo<LigatureArray>
1656 ligatureArray; /* Offset to LigatureArray table--from
1657 * beginning of MarkLigPos subtable */
1658 public:
1659 DEFINE_SIZE_STATIC (12);
1660 };
1661
1662 struct MarkLigPos
1663 {
1664 template <typename context_t, typename ...Ts>
dispatchOT::MarkLigPos1665 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1666 {
1667 TRACE_DISPATCH (this, u.format);
1668 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1669 switch (u.format) {
1670 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1671 default:return_trace (c->default_return_value ());
1672 }
1673 }
1674
1675 protected:
1676 union {
1677 HBUINT16 format; /* Format identifier */
1678 MarkLigPosFormat1 format1;
1679 } u;
1680 };
1681
1682
1683 typedef AnchorMatrix Mark2Array; /* mark2-major--
1684 * in order of Mark2Coverage Index--,
1685 * mark1-minor--
1686 * ordered by class--zero-based. */
1687
1688 struct MarkMarkPosFormat1
1689 {
intersectsOT::MarkMarkPosFormat11690 bool intersects (const hb_set_t *glyphs) const
1691 { return (this+mark1Coverage).intersects (glyphs) &&
1692 (this+mark2Coverage).intersects (glyphs); }
1693
collect_glyphsOT::MarkMarkPosFormat11694 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1695 {
1696 if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
1697 if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
1698 }
1699
get_coverageOT::MarkMarkPosFormat11700 const Coverage &get_coverage () const { return this+mark1Coverage; }
1701
applyOT::MarkMarkPosFormat11702 bool apply (hb_ot_apply_context_t *c) const
1703 {
1704 TRACE_APPLY (this);
1705 hb_buffer_t *buffer = c->buffer;
1706 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
1707 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
1708
1709 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
1710 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1711 skippy_iter.reset (buffer->idx, 1);
1712 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
1713 if (!skippy_iter.prev ()) return_trace (false);
1714
1715 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1716
1717 unsigned int j = skippy_iter.idx;
1718
1719 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1720 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1721 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1722 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
1723
1724 if (likely (id1 == id2)) {
1725 if (id1 == 0) /* Marks belonging to the same base. */
1726 goto good;
1727 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1728 goto good;
1729 } else {
1730 /* If ligature ids don't match, it may be the case that one of the marks
1731 * itself is a ligature. In which case match. */
1732 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1733 goto good;
1734 }
1735
1736 /* Didn't match. */
1737 return_trace (false);
1738
1739 good:
1740 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
1741 if (mark2_index == NOT_COVERED) return_trace (false);
1742
1743 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
1744 }
1745
subsetOT::MarkMarkPosFormat11746 bool subset (hb_subset_context_t *c) const
1747 {
1748 TRACE_SUBSET (this);
1749 // TODO(subset)
1750 return_trace (false);
1751 }
1752
sanitizeOT::MarkMarkPosFormat11753 bool sanitize (hb_sanitize_context_t *c) const
1754 {
1755 TRACE_SANITIZE (this);
1756 return_trace (c->check_struct (this) &&
1757 mark1Coverage.sanitize (c, this) &&
1758 mark2Coverage.sanitize (c, this) &&
1759 mark1Array.sanitize (c, this) &&
1760 mark2Array.sanitize (c, this, (unsigned int) classCount));
1761 }
1762
1763 protected:
1764 HBUINT16 format; /* Format identifier--format = 1 */
1765 OffsetTo<Coverage>
1766 mark1Coverage; /* Offset to Combining Mark1 Coverage
1767 * table--from beginning of MarkMarkPos
1768 * subtable */
1769 OffsetTo<Coverage>
1770 mark2Coverage; /* Offset to Combining Mark2 Coverage
1771 * table--from beginning of MarkMarkPos
1772 * subtable */
1773 HBUINT16 classCount; /* Number of defined mark classes */
1774 OffsetTo<MarkArray>
1775 mark1Array; /* Offset to Mark1Array table--from
1776 * beginning of MarkMarkPos subtable */
1777 OffsetTo<Mark2Array>
1778 mark2Array; /* Offset to Mark2Array table--from
1779 * beginning of MarkMarkPos subtable */
1780 public:
1781 DEFINE_SIZE_STATIC (12);
1782 };
1783
1784 struct MarkMarkPos
1785 {
1786 template <typename context_t, typename ...Ts>
dispatchOT::MarkMarkPos1787 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1788 {
1789 TRACE_DISPATCH (this, u.format);
1790 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1791 switch (u.format) {
1792 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1793 default:return_trace (c->default_return_value ());
1794 }
1795 }
1796
1797 protected:
1798 union {
1799 HBUINT16 format; /* Format identifier */
1800 MarkMarkPosFormat1 format1;
1801 } u;
1802 };
1803
1804
1805 struct ContextPos : Context {};
1806
1807 struct ChainContextPos : ChainContext {};
1808
1809 struct ExtensionPos : Extension<ExtensionPos>
1810 {
1811 typedef struct PosLookupSubTable SubTable;
1812 };
1813
1814
1815
1816 /*
1817 * PosLookup
1818 */
1819
1820
1821 struct PosLookupSubTable
1822 {
1823 friend struct Lookup;
1824 friend struct PosLookup;
1825
1826 enum Type {
1827 Single = 1,
1828 Pair = 2,
1829 Cursive = 3,
1830 MarkBase = 4,
1831 MarkLig = 5,
1832 MarkMark = 6,
1833 Context = 7,
1834 ChainContext = 8,
1835 Extension = 9
1836 };
1837
1838 template <typename context_t, typename ...Ts>
dispatchOT::PosLookupSubTable1839 typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type, Ts&&... ds) const
1840 {
1841 TRACE_DISPATCH (this, lookup_type);
1842 switch (lookup_type) {
1843 case Single: return_trace (u.single.dispatch (c, hb_forward<Ts> (ds)...));
1844 case Pair: return_trace (u.pair.dispatch (c, hb_forward<Ts> (ds)...));
1845 case Cursive: return_trace (u.cursive.dispatch (c, hb_forward<Ts> (ds)...));
1846 case MarkBase: return_trace (u.markBase.dispatch (c, hb_forward<Ts> (ds)...));
1847 case MarkLig: return_trace (u.markLig.dispatch (c, hb_forward<Ts> (ds)...));
1848 case MarkMark: return_trace (u.markMark.dispatch (c, hb_forward<Ts> (ds)...));
1849 case Context: return_trace (u.context.dispatch (c, hb_forward<Ts> (ds)...));
1850 case ChainContext: return_trace (u.chainContext.dispatch (c, hb_forward<Ts> (ds)...));
1851 case Extension: return_trace (u.extension.dispatch (c, hb_forward<Ts> (ds)...));
1852 default: return_trace (c->default_return_value ());
1853 }
1854 }
1855
1856 protected:
1857 union {
1858 SinglePos single;
1859 PairPos pair;
1860 CursivePos cursive;
1861 MarkBasePos markBase;
1862 MarkLigPos markLig;
1863 MarkMarkPos markMark;
1864 ContextPos context;
1865 ChainContextPos chainContext;
1866 ExtensionPos extension;
1867 } u;
1868 public:
1869 DEFINE_SIZE_MIN (0);
1870 };
1871
1872
1873 struct PosLookup : Lookup
1874 {
1875 typedef struct PosLookupSubTable SubTable;
1876
get_subtableOT::PosLookup1877 const SubTable& get_subtable (unsigned int i) const
1878 { return Lookup::get_subtable<SubTable> (i); }
1879
is_reverseOT::PosLookup1880 bool is_reverse () const
1881 {
1882 return false;
1883 }
1884
applyOT::PosLookup1885 bool apply (hb_ot_apply_context_t *c) const
1886 {
1887 TRACE_APPLY (this);
1888 return_trace (dispatch (c));
1889 }
1890
intersectsOT::PosLookup1891 bool intersects (const hb_set_t *glyphs) const
1892 {
1893 hb_intersects_context_t c (glyphs);
1894 return dispatch (&c);
1895 }
1896
collect_glyphsOT::PosLookup1897 hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1898 { return dispatch (c); }
1899
1900 template <typename set_t>
add_coverageOT::PosLookup1901 void add_coverage (set_t *glyphs) const
1902 {
1903 hb_add_coverage_context_t<set_t> c (glyphs);
1904 dispatch (&c);
1905 }
1906
1907 HB_INTERNAL static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1908
1909 template <typename context_t>
1910 static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1911
1912 template <typename context_t, typename ...Ts>
dispatchOT::PosLookup1913 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
1914 { return Lookup::dispatch<SubTable> (c, hb_forward<Ts> (ds)...); }
1915
subsetOT::PosLookup1916 bool subset (hb_subset_context_t *c) const
1917 { return Lookup::subset<SubTable> (c); }
1918
sanitizeOT::PosLookup1919 bool sanitize (hb_sanitize_context_t *c) const
1920 { return Lookup::sanitize<SubTable> (c); }
1921 };
1922
1923 /*
1924 * GPOS -- Glyph Positioning
1925 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
1926 */
1927
1928 struct GPOS : GSUBGPOS
1929 {
1930 static constexpr hb_tag_t tableTag = HB_OT_TAG_GPOS;
1931
get_lookupOT::GPOS1932 const PosLookup& get_lookup (unsigned int i) const
1933 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
1934
1935 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1936 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
1937 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
1938
subsetOT::GPOS1939 bool subset (hb_subset_context_t *c) const
1940 { return GSUBGPOS::subset<PosLookup> (c); }
1941
sanitizeOT::GPOS1942 bool sanitize (hb_sanitize_context_t *c) const
1943 { return GSUBGPOS::sanitize<PosLookup> (c); }
1944
1945 HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
1946 hb_face_t *face) const;
1947
1948 typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
1949 };
1950
1951
1952 static void
reverse_cursive_minor_offset(hb_glyph_position_t * pos,unsigned int i,hb_direction_t direction,unsigned int new_parent)1953 reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
1954 {
1955 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1956 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
1957 return;
1958
1959 pos[i].attach_chain() = 0;
1960
1961 unsigned int j = (int) i + chain;
1962
1963 /* Stop if we see new parent in the chain. */
1964 if (j == new_parent)
1965 return;
1966
1967 reverse_cursive_minor_offset (pos, j, direction, new_parent);
1968
1969 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1970 pos[j].y_offset = -pos[i].y_offset;
1971 else
1972 pos[j].x_offset = -pos[i].x_offset;
1973
1974 pos[j].attach_chain() = -chain;
1975 pos[j].attach_type() = type;
1976 }
1977 static void
propagate_attachment_offsets(hb_glyph_position_t * pos,unsigned int len,unsigned int i,hb_direction_t direction)1978 propagate_attachment_offsets (hb_glyph_position_t *pos,
1979 unsigned int len,
1980 unsigned int i,
1981 hb_direction_t direction)
1982 {
1983 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
1984 * offset of glyph they are attached to. */
1985 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1986 if (likely (!chain))
1987 return;
1988
1989 pos[i].attach_chain() = 0;
1990
1991 unsigned int j = (int) i + chain;
1992
1993 if (unlikely (j >= len))
1994 return;
1995
1996 propagate_attachment_offsets (pos, len, j, direction);
1997
1998 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
1999
2000 if (type & ATTACH_TYPE_CURSIVE)
2001 {
2002 if (HB_DIRECTION_IS_HORIZONTAL (direction))
2003 pos[i].y_offset += pos[j].y_offset;
2004 else
2005 pos[i].x_offset += pos[j].x_offset;
2006 }
2007 else /*if (type & ATTACH_TYPE_MARK)*/
2008 {
2009 pos[i].x_offset += pos[j].x_offset;
2010 pos[i].y_offset += pos[j].y_offset;
2011
2012 assert (j < i);
2013 if (HB_DIRECTION_IS_FORWARD (direction))
2014 for (unsigned int k = j; k < i; k++) {
2015 pos[i].x_offset -= pos[k].x_advance;
2016 pos[i].y_offset -= pos[k].y_advance;
2017 }
2018 else
2019 for (unsigned int k = j + 1; k < i + 1; k++) {
2020 pos[i].x_offset += pos[k].x_advance;
2021 pos[i].y_offset += pos[k].y_advance;
2022 }
2023 }
2024 }
2025
2026 void
position_start(hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)2027 GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
2028 {
2029 unsigned int count = buffer->len;
2030 for (unsigned int i = 0; i < count; i++)
2031 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
2032 }
2033
2034 void
position_finish_advances(hb_font_t * font HB_UNUSED,hb_buffer_t * buffer HB_UNUSED)2035 GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
2036 {
2037 //_hb_buffer_assert_gsubgpos_vars (buffer);
2038 }
2039
2040 void
position_finish_offsets(hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)2041 GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
2042 {
2043 _hb_buffer_assert_gsubgpos_vars (buffer);
2044
2045 unsigned int len;
2046 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
2047 hb_direction_t direction = buffer->props.direction;
2048
2049 /* Handle attachments */
2050 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
2051 for (unsigned int i = 0; i < len; i++)
2052 propagate_attachment_offsets (pos, len, i, direction);
2053 }
2054
2055
2056 struct GPOS_accelerator_t : GPOS::accelerator_t {};
2057
2058
2059 /* Out-of-class implementation for methods recursing */
2060
2061 #ifndef HB_NO_OT_LAYOUT
2062 template <typename context_t>
dispatch_recurse_func(context_t * c,unsigned int lookup_index)2063 /*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
2064 {
2065 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
2066 return l.dispatch (c);
2067 }
apply_recurse_func(hb_ot_apply_context_t * c,unsigned int lookup_index)2068 /*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
2069 {
2070 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
2071 unsigned int saved_lookup_props = c->lookup_props;
2072 unsigned int saved_lookup_index = c->lookup_index;
2073 c->set_lookup_index (lookup_index);
2074 c->set_lookup_props (l.get_props ());
2075 bool ret = l.dispatch (c);
2076 c->set_lookup_index (saved_lookup_index);
2077 c->set_lookup_props (saved_lookup_props);
2078 return ret;
2079 }
2080 #endif
2081
2082
2083 } /* namespace OT */
2084
2085
2086 #endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */
2087