2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012,2013 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_GPOS_TABLE_HH
30 #define HB_OT_LAYOUT_GPOS_TABLE_HH
32 #include "hb-ot-layout-gsubgpos.hh"
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. */
44 ATTACH_TYPE_NONE = 0X00,
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,
52 /* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
54 typedef HBUINT16 Value;
56 typedef UnsizedArrayOf<Value> ValueRecord;
58 struct ValueFormat : HBUINT16
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 */
72 devices = 0x00F0u /* Mask for having any Device table */
75 /* All fields are options. Only those available advance the value pointer. */
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
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) */
101 unsigned int get_len () const { return hb_popcount ((unsigned int) *this); }
102 unsigned int get_size () const { return get_len () * Value::static_size; }
104 bool apply_value (hb_ot_apply_context_t *c,
107 hb_glyph_position_t &glyph_pos) const
110 unsigned int format = *this;
111 if (!format) return ret;
113 hb_font_t *font = c->font;
114 bool horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
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));
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));
128 if (!has_device ()) return ret;
130 bool use_x_device = font->x_ppem || font->num_coords;
131 bool use_y_device = font->y_ppem || font->num_coords;
133 if (!use_x_device && !use_y_device) return ret;
135 const VariationStore &store = c->var_store;
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);
142 if (format & yPlaDevice) {
143 if (use_y_device) glyph_pos.y_offset += (base + get_device (values, &ret)).get_y_delta (font, store);
146 if (format & xAdvDevice) {
147 if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store);
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);
159 bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
161 unsigned int format = *this;
163 if (format & xPlacement) values++;
164 if (format & yPlacement) values++;
165 if (format & xAdvance) values++;
166 if (format & yAdvance) values++;
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;
176 static OffsetTo<Device>& get_device (Value* value)
177 { return *CastP<OffsetTo<Device> > (value); }
178 static const OffsetTo<Device>& get_device (const Value* value, bool *worked=nullptr)
180 if (worked) *worked |= bool (*value);
181 return *CastP<OffsetTo<Device> > (value);
184 static const HBINT16& get_short (const Value* value, bool *worked=nullptr)
186 if (worked) *worked |= bool (*value);
187 return *CastP<HBINT16> (value);
192 bool has_device () const
194 unsigned int format = *this;
195 return (format & devices) != 0;
198 bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
200 TRACE_SANITIZE (this);
201 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
204 bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
206 TRACE_SANITIZE (this);
207 unsigned int len = get_len ();
209 if (!c->check_range (values, count, get_size ())) return_trace (false);
211 if (!has_device ()) return_trace (true);
213 for (unsigned int i = 0; i < count; i++) {
214 if (!sanitize_value_devices (c, base, values))
215 return_trace (false);
222 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
223 bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count, unsigned int stride) const
225 TRACE_SANITIZE (this);
227 if (!has_device ()) return_trace (true);
229 for (unsigned int i = 0; i < count; i++) {
230 if (!sanitize_value_devices (c, base, values))
231 return_trace (false);
242 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
243 float *x, float *y) const
245 hb_font_t *font = c->font;
246 *x = font->em_fscale_x (xCoordinate);
247 *y = font->em_fscale_y (yCoordinate);
250 bool sanitize (hb_sanitize_context_t *c) const
252 TRACE_SANITIZE (this);
253 return_trace (c->check_struct (this));
257 HBUINT16 format; /* Format identifier--format = 1 */
258 FWORD xCoordinate; /* Horizontal value--in design units */
259 FWORD yCoordinate; /* Vertical value--in design units */
261 DEFINE_SIZE_STATIC (6);
266 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
267 float *x, float *y) const
269 hb_font_t *font = c->font;
270 unsigned int x_ppem = font->x_ppem;
271 unsigned int y_ppem = font->y_ppem;
272 hb_position_t cx = 0, cy = 0;
275 ret = (x_ppem || y_ppem) &&
276 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
277 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
278 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
281 bool sanitize (hb_sanitize_context_t *c) const
283 TRACE_SANITIZE (this);
284 return_trace (c->check_struct (this));
288 HBUINT16 format; /* Format identifier--format = 2 */
289 FWORD xCoordinate; /* Horizontal value--in design units */
290 FWORD yCoordinate; /* Vertical value--in design units */
291 HBUINT16 anchorPoint; /* Index to glyph contour point */
293 DEFINE_SIZE_STATIC (8);
298 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
299 float *x, float *y) const
301 hb_font_t *font = c->font;
302 *x = font->em_fscale_x (xCoordinate);
303 *y = font->em_fscale_y (yCoordinate);
305 if (font->x_ppem || font->num_coords)
306 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
307 if (font->y_ppem || font->num_coords)
308 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
311 bool sanitize (hb_sanitize_context_t *c) const
313 TRACE_SANITIZE (this);
314 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
318 HBUINT16 format; /* Format identifier--format = 3 */
319 FWORD xCoordinate; /* Horizontal value--in design units */
320 FWORD yCoordinate; /* Vertical value--in design units */
322 xDeviceTable; /* Offset to Device table for X
323 * coordinate-- from beginning of
324 * Anchor table (may be NULL) */
326 yDeviceTable; /* Offset to Device table for Y
327 * coordinate-- from beginning of
328 * Anchor table (may be NULL) */
330 DEFINE_SIZE_STATIC (10);
335 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
336 float *x, float *y) const
340 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
341 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
342 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
347 bool sanitize (hb_sanitize_context_t *c) const
349 TRACE_SANITIZE (this);
350 if (!u.format.sanitize (c)) return_trace (false);
352 case 1: return_trace (u.format1.sanitize (c));
353 case 2: return_trace (u.format2.sanitize (c));
354 case 3: return_trace (u.format3.sanitize (c));
355 default:return_trace (true);
361 HBUINT16 format; /* Format identifier */
362 AnchorFormat1 format1;
363 AnchorFormat2 format2;
364 AnchorFormat3 format3;
367 DEFINE_SIZE_UNION (2, format);
373 const Anchor& get_anchor (unsigned int row, unsigned int col,
374 unsigned int cols, bool *found) const
377 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
378 *found = !matrixZ[row * cols + col].is_null ();
379 return this+matrixZ[row * cols + col];
382 bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
384 TRACE_SANITIZE (this);
385 if (!c->check_struct (this)) return_trace (false);
386 if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
387 unsigned int count = rows * cols;
388 if (!c->check_array (matrixZ.arrayZ, count)) return_trace (false);
389 for (unsigned int i = 0; i < count; i++)
390 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
394 HBUINT16 rows; /* Number of rows */
396 UnsizedArrayOf<OffsetTo<Anchor> >
397 matrixZ; /* Matrix of offsets to Anchor tables--
398 * from beginning of AnchorMatrix table */
400 DEFINE_SIZE_ARRAY (2, matrixZ);
406 friend struct MarkArray;
408 bool sanitize (hb_sanitize_context_t *c, const void *base) const
410 TRACE_SANITIZE (this);
411 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
415 HBUINT16 klass; /* Class defined for this mark */
417 markAnchor; /* Offset to Anchor table--from
418 * beginning of MarkArray table */
420 DEFINE_SIZE_STATIC (4);
423 struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
425 bool apply (hb_ot_apply_context_t *c,
426 unsigned int mark_index, unsigned int glyph_index,
427 const AnchorMatrix &anchors, unsigned int class_count,
428 unsigned int glyph_pos) const
431 hb_buffer_t *buffer = c->buffer;
432 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
433 unsigned int mark_class = record.klass;
435 const Anchor& mark_anchor = this + record.markAnchor;
437 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
438 /* If this subtable doesn't have an anchor for this base and this class,
439 * return false such that the subsequent subtables have a chance at it. */
440 if (unlikely (!found)) return_trace (false);
442 float mark_x, mark_y, base_x, base_y;
444 buffer->unsafe_to_break (glyph_pos, buffer->idx);
445 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
446 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
448 hb_glyph_position_t &o = buffer->cur_pos();
449 o.x_offset = round (base_x - mark_x);
450 o.y_offset = round (base_y - mark_y);
451 o.attach_type() = ATTACH_TYPE_MARK;
452 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
453 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
459 bool sanitize (hb_sanitize_context_t *c) const
461 TRACE_SANITIZE (this);
462 return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
469 struct SinglePosFormat1
471 bool intersects (const hb_set_t *glyphs) const
472 { return (this+coverage).intersects (glyphs); }
474 void collect_glyphs (hb_collect_glyphs_context_t *c) const
475 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
477 const Coverage &get_coverage () const { return this+coverage; }
479 bool apply (hb_ot_apply_context_t *c) const
482 hb_buffer_t *buffer = c->buffer;
483 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
484 if (likely (index == NOT_COVERED)) return_trace (false);
486 valueFormat.apply_value (c, this, values, buffer->cur_pos());
492 bool subset (hb_subset_context_t *c) const
496 return_trace (false);
499 bool sanitize (hb_sanitize_context_t *c) const
501 TRACE_SANITIZE (this);
502 return_trace (c->check_struct (this) &&
503 coverage.sanitize (c, this) &&
504 valueFormat.sanitize_value (c, this, values));
508 HBUINT16 format; /* Format identifier--format = 1 */
510 coverage; /* Offset to Coverage table--from
511 * beginning of subtable */
512 ValueFormat valueFormat; /* Defines the types of data in the
514 ValueRecord values; /* Defines positioning
515 * value(s)--applied to all glyphs in
516 * the Coverage table */
518 DEFINE_SIZE_ARRAY (6, values);
521 struct SinglePosFormat2
523 bool intersects (const hb_set_t *glyphs) const
524 { return (this+coverage).intersects (glyphs); }
526 void collect_glyphs (hb_collect_glyphs_context_t *c) const
527 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
529 const Coverage &get_coverage () const { return this+coverage; }
531 bool apply (hb_ot_apply_context_t *c) const
534 hb_buffer_t *buffer = c->buffer;
535 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
536 if (likely (index == NOT_COVERED)) return_trace (false);
538 if (likely (index >= valueCount)) return_trace (false);
540 valueFormat.apply_value (c, this,
541 &values[index * valueFormat.get_len ()],
548 bool subset (hb_subset_context_t *c) const
552 return_trace (false);
555 bool sanitize (hb_sanitize_context_t *c) const
557 TRACE_SANITIZE (this);
558 return_trace (c->check_struct (this) &&
559 coverage.sanitize (c, this) &&
560 valueFormat.sanitize_values (c, this, values, valueCount));
564 HBUINT16 format; /* Format identifier--format = 2 */
566 coverage; /* Offset to Coverage table--from
567 * beginning of subtable */
568 ValueFormat valueFormat; /* Defines the types of data in the
570 HBUINT16 valueCount; /* Number of ValueRecords */
571 ValueRecord values; /* Array of ValueRecords--positioning
572 * values applied to glyphs */
574 DEFINE_SIZE_ARRAY (8, values);
579 template <typename context_t>
580 typename context_t::return_t dispatch (context_t *c) const
582 TRACE_DISPATCH (this, u.format);
583 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
585 case 1: return_trace (c->dispatch (u.format1));
586 case 2: return_trace (c->dispatch (u.format2));
587 default:return_trace (c->default_return_value ());
593 HBUINT16 format; /* Format identifier */
594 SinglePosFormat1 format1;
595 SinglePosFormat2 format2;
600 struct PairValueRecord
602 friend struct PairSet;
605 GlyphID secondGlyph; /* GlyphID of second glyph in the
606 * pair--first glyph is listed in the
608 ValueRecord values; /* Positioning data for the first glyph
609 * followed by for second glyph */
611 DEFINE_SIZE_ARRAY (2, values);
616 friend struct PairPosFormat1;
618 bool intersects (const hb_set_t *glyphs,
619 const ValueFormat *valueFormats) const
621 unsigned int len1 = valueFormats[0].get_len ();
622 unsigned int len2 = valueFormats[1].get_len ();
623 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
625 const PairValueRecord *record = &firstPairValueRecord;
626 unsigned int count = len;
627 for (unsigned int i = 0; i < count; i++)
629 if (glyphs->has (record->secondGlyph))
631 record = &StructAtOffset<const PairValueRecord> (record, record_size);
636 void collect_glyphs (hb_collect_glyphs_context_t *c,
637 const ValueFormat *valueFormats) const
639 unsigned int len1 = valueFormats[0].get_len ();
640 unsigned int len2 = valueFormats[1].get_len ();
641 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
643 const PairValueRecord *record = &firstPairValueRecord;
644 c->input->add_array (&record->secondGlyph, len, record_size);
647 bool apply (hb_ot_apply_context_t *c,
648 const ValueFormat *valueFormats,
649 unsigned int pos) const
652 hb_buffer_t *buffer = c->buffer;
653 unsigned int len1 = valueFormats[0].get_len ();
654 unsigned int len2 = valueFormats[1].get_len ();
655 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
657 unsigned int count = len;
659 /* Hand-coded bsearch. */
660 if (unlikely (!count))
661 return_trace (false);
662 hb_codepoint_t x = buffer->info[pos].codepoint;
663 int min = 0, max = (int) count - 1;
666 int mid = ((unsigned int) min + (unsigned int) max) / 2;
667 const PairValueRecord *record = &StructAtOffset<PairValueRecord> (&firstPairValueRecord, record_size * mid);
668 hb_codepoint_t mid_x = record->secondGlyph;
675 /* Note the intentional use of "|" instead of short-circuit "||". */
676 if (valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos()) |
677 valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]))
678 buffer->unsafe_to_break (buffer->idx, pos + 1);
686 return_trace (false);
689 struct sanitize_closure_t
692 const ValueFormat *valueFormats;
693 unsigned int len1; /* valueFormats[0].get_len() */
694 unsigned int stride; /* 1 + len1 + len2 */
697 bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
699 TRACE_SANITIZE (this);
700 if (!(c->check_struct (this)
701 && c->check_range (&firstPairValueRecord,
703 HBUINT16::static_size,
704 closure->stride))) return_trace (false);
706 unsigned int count = len;
707 const PairValueRecord *record = &firstPairValueRecord;
708 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
709 closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
713 HBUINT16 len; /* Number of PairValueRecords */
714 PairValueRecord firstPairValueRecord;
715 /* Array of PairValueRecords--ordered
716 * by GlyphID of the second glyph */
721 struct PairPosFormat1
723 bool intersects (const hb_set_t *glyphs) const
725 unsigned int count = pairSet.len;
726 for (Coverage::Iter iter (this+coverage); iter.more (); iter.next ())
728 if (unlikely (iter.get_coverage () >= count))
729 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
730 if (glyphs->has (iter.get_glyph ()) &&
731 (this+pairSet[iter.get_coverage ()]).intersects (glyphs, valueFormat))
737 void collect_glyphs (hb_collect_glyphs_context_t *c) const
739 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
740 unsigned int count = pairSet.len;
741 for (unsigned int i = 0; i < count; i++)
742 (this+pairSet[i]).collect_glyphs (c, valueFormat);
745 const Coverage &get_coverage () const { return this+coverage; }
747 bool apply (hb_ot_apply_context_t *c) const
750 hb_buffer_t *buffer = c->buffer;
751 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
752 if (likely (index == NOT_COVERED)) return_trace (false);
754 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
755 skippy_iter.reset (buffer->idx, 1);
756 if (!skippy_iter.next ()) return_trace (false);
758 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
761 bool subset (hb_subset_context_t *c) const
765 return_trace (false);
768 bool sanitize (hb_sanitize_context_t *c) const
770 TRACE_SANITIZE (this);
772 if (!c->check_struct (this)) return_trace (false);
774 unsigned int len1 = valueFormat[0].get_len ();
775 unsigned int len2 = valueFormat[1].get_len ();
776 PairSet::sanitize_closure_t closure =
784 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
788 HBUINT16 format; /* Format identifier--format = 1 */
790 coverage; /* Offset to Coverage table--from
791 * beginning of subtable */
792 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
793 * ValueRecord1--for the first glyph
794 * in the pair--may be zero (0) */
795 /* [1] Defines the types of data in
796 * ValueRecord2--for the second glyph
797 * in the pair--may be zero (0) */
798 OffsetArrayOf<PairSet>
799 pairSet; /* Array of PairSet tables
800 * ordered by Coverage Index */
802 DEFINE_SIZE_ARRAY (10, pairSet);
805 struct PairPosFormat2
807 bool intersects (const hb_set_t *glyphs) const
809 return (this+coverage).intersects (glyphs) &&
810 (this+classDef2).intersects (glyphs);
813 void collect_glyphs (hb_collect_glyphs_context_t *c) const
815 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
816 if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
819 const Coverage &get_coverage () const { return this+coverage; }
821 bool apply (hb_ot_apply_context_t *c) const
824 hb_buffer_t *buffer = c->buffer;
825 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
826 if (likely (index == NOT_COVERED)) return_trace (false);
828 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
829 skippy_iter.reset (buffer->idx, 1);
830 if (!skippy_iter.next ()) return_trace (false);
832 unsigned int len1 = valueFormat1.get_len ();
833 unsigned int len2 = valueFormat2.get_len ();
834 unsigned int record_len = len1 + len2;
836 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
837 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
838 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
840 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
841 /* Note the intentional use of "|" instead of short-circuit "||". */
842 if (valueFormat1.apply_value (c, this, v, buffer->cur_pos()) |
843 valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]))
844 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
846 buffer->idx = skippy_iter.idx;
853 bool subset (hb_subset_context_t *c) const
857 return_trace (false);
860 bool sanitize (hb_sanitize_context_t *c) const
862 TRACE_SANITIZE (this);
863 if (!(c->check_struct (this)
864 && coverage.sanitize (c, this)
865 && classDef1.sanitize (c, this)
866 && classDef2.sanitize (c, this))) return_trace (false);
868 unsigned int len1 = valueFormat1.get_len ();
869 unsigned int len2 = valueFormat2.get_len ();
870 unsigned int stride = len1 + len2;
871 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
872 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
873 return_trace (c->check_range ((const void *) values,
876 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
877 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
881 HBUINT16 format; /* Format identifier--format = 2 */
883 coverage; /* Offset to Coverage table--from
884 * beginning of subtable */
885 ValueFormat valueFormat1; /* ValueRecord definition--for the
886 * first glyph of the pair--may be zero
888 ValueFormat valueFormat2; /* ValueRecord definition--for the
889 * second glyph of the pair--may be
892 classDef1; /* Offset to ClassDef table--from
893 * beginning of PairPos subtable--for
894 * the first glyph of the pair */
896 classDef2; /* Offset to ClassDef table--from
897 * beginning of PairPos subtable--for
898 * the second glyph of the pair */
899 HBUINT16 class1Count; /* Number of classes in ClassDef1
900 * table--includes Class0 */
901 HBUINT16 class2Count; /* Number of classes in ClassDef2
902 * table--includes Class0 */
903 ValueRecord values; /* Matrix of value pairs:
904 * class1-major, class2-minor,
905 * Each entry has value1 and value2 */
907 DEFINE_SIZE_ARRAY (16, values);
912 template <typename context_t>
913 typename context_t::return_t dispatch (context_t *c) const
915 TRACE_DISPATCH (this, u.format);
916 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
918 case 1: return_trace (c->dispatch (u.format1));
919 case 2: return_trace (c->dispatch (u.format2));
920 default:return_trace (c->default_return_value ());
926 HBUINT16 format; /* Format identifier */
927 PairPosFormat1 format1;
928 PairPosFormat2 format2;
933 struct EntryExitRecord
935 friend struct CursivePosFormat1;
937 bool sanitize (hb_sanitize_context_t *c, const void *base) const
939 TRACE_SANITIZE (this);
940 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
945 entryAnchor; /* Offset to EntryAnchor table--from
946 * beginning of CursivePos
947 * subtable--may be NULL */
949 exitAnchor; /* Offset to ExitAnchor table--from
950 * beginning of CursivePos
951 * subtable--may be NULL */
953 DEFINE_SIZE_STATIC (4);
957 reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
959 struct CursivePosFormat1
961 bool intersects (const hb_set_t *glyphs) const
962 { return (this+coverage).intersects (glyphs); }
964 void collect_glyphs (hb_collect_glyphs_context_t *c) const
965 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
967 const Coverage &get_coverage () const { return this+coverage; }
969 bool apply (hb_ot_apply_context_t *c) const
972 hb_buffer_t *buffer = c->buffer;
974 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
975 if (!this_record.entryAnchor) return_trace (false);
977 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
978 skippy_iter.reset (buffer->idx, 1);
979 if (!skippy_iter.prev ()) return_trace (false);
981 const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
982 if (!prev_record.exitAnchor) return_trace (false);
984 unsigned int i = skippy_iter.idx;
985 unsigned int j = buffer->idx;
987 buffer->unsafe_to_break (i, j);
988 float entry_x, entry_y, exit_x, exit_y;
989 (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
990 (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
992 hb_glyph_position_t *pos = buffer->pos;
995 /* Main-direction adjustment */
996 switch (c->direction) {
997 case HB_DIRECTION_LTR:
998 pos[i].x_advance = round (exit_x) + pos[i].x_offset;
1000 d = round (entry_x) + pos[j].x_offset;
1001 pos[j].x_advance -= d;
1002 pos[j].x_offset -= d;
1004 case HB_DIRECTION_RTL:
1005 d = round (exit_x) + pos[i].x_offset;
1006 pos[i].x_advance -= d;
1007 pos[i].x_offset -= d;
1009 pos[j].x_advance = round (entry_x) + pos[j].x_offset;
1011 case HB_DIRECTION_TTB:
1012 pos[i].y_advance = round (exit_y) + pos[i].y_offset;
1014 d = round (entry_y) + pos[j].y_offset;
1015 pos[j].y_advance -= d;
1016 pos[j].y_offset -= d;
1018 case HB_DIRECTION_BTT:
1019 d = round (exit_y) + pos[i].y_offset;
1020 pos[i].y_advance -= d;
1021 pos[i].y_offset -= d;
1023 pos[j].y_advance = round (entry_y);
1025 case HB_DIRECTION_INVALID:
1030 /* Cross-direction adjustment */
1032 /* We attach child to parent (think graph theory and rooted trees whereas
1033 * the root stays on baseline and each node aligns itself against its
1036 * Optimize things for the case of RightToLeft, as that's most common in
1038 unsigned int child = i;
1039 unsigned int parent = j;
1040 hb_position_t x_offset = entry_x - exit_x;
1041 hb_position_t y_offset = entry_y - exit_y;
1042 if (!(c->lookup_props & LookupFlag::RightToLeft))
1044 unsigned int k = child;
1047 x_offset = -x_offset;
1048 y_offset = -y_offset;
1051 /* If child was already connected to someone else, walk through its old
1052 * chain and reverse the link direction, such that the whole tree of its
1053 * previous connection now attaches to new parent. Watch out for case
1054 * where new parent is on the path from old chain...
1056 reverse_cursive_minor_offset (pos, child, c->direction, parent);
1058 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
1059 pos[child].attach_chain() = (int) parent - (int) child;
1060 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
1061 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
1062 pos[child].y_offset = y_offset;
1064 pos[child].x_offset = x_offset;
1067 return_trace (true);
1070 bool subset (hb_subset_context_t *c) const
1072 TRACE_SUBSET (this);
1074 return_trace (false);
1077 bool sanitize (hb_sanitize_context_t *c) const
1079 TRACE_SANITIZE (this);
1080 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
1084 HBUINT16 format; /* Format identifier--format = 1 */
1086 coverage; /* Offset to Coverage table--from
1087 * beginning of subtable */
1088 ArrayOf<EntryExitRecord>
1089 entryExitRecord; /* Array of EntryExit records--in
1090 * Coverage Index order */
1092 DEFINE_SIZE_ARRAY (6, entryExitRecord);
1097 template <typename context_t>
1098 typename context_t::return_t dispatch (context_t *c) const
1100 TRACE_DISPATCH (this, u.format);
1101 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1103 case 1: return_trace (c->dispatch (u.format1));
1104 default:return_trace (c->default_return_value ());
1110 HBUINT16 format; /* Format identifier */
1111 CursivePosFormat1 format1;
1116 typedef AnchorMatrix BaseArray; /* base-major--
1117 * in order of BaseCoverage Index--,
1119 * ordered by class--zero-based. */
1121 struct MarkBasePosFormat1
1123 bool intersects (const hb_set_t *glyphs) const
1124 { return (this+markCoverage).intersects (glyphs) &&
1125 (this+baseCoverage).intersects (glyphs); }
1127 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1129 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1130 if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
1133 const Coverage &get_coverage () const { return this+markCoverage; }
1135 bool apply (hb_ot_apply_context_t *c) const
1138 hb_buffer_t *buffer = c->buffer;
1139 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1140 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1142 /* Now we search backwards for a non-mark glyph */
1143 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1144 skippy_iter.reset (buffer->idx, 1);
1145 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1147 if (!skippy_iter.prev ()) return_trace (false);
1148 /* We only want to attach to the first of a MultipleSubst sequence.
1149 * https://github.com/harfbuzz/harfbuzz/issues/740
1151 * ...but stop if we find a mark in the MultipleSubst sequence:
1152 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
1153 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
1154 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
1155 (skippy_iter.idx == 0 ||
1156 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
1157 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
1158 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
1159 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
1160 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
1163 skippy_iter.reject ();
1166 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
1167 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1169 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
1170 if (base_index == NOT_COVERED) return_trace (false);
1172 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
1175 bool subset (hb_subset_context_t *c) const
1177 TRACE_SUBSET (this);
1179 return_trace (false);
1182 bool sanitize (hb_sanitize_context_t *c) const
1184 TRACE_SANITIZE (this);
1185 return_trace (c->check_struct (this) &&
1186 markCoverage.sanitize (c, this) &&
1187 baseCoverage.sanitize (c, this) &&
1188 markArray.sanitize (c, this) &&
1189 baseArray.sanitize (c, this, (unsigned int) classCount));
1193 HBUINT16 format; /* Format identifier--format = 1 */
1195 markCoverage; /* Offset to MarkCoverage table--from
1196 * beginning of MarkBasePos subtable */
1198 baseCoverage; /* Offset to BaseCoverage table--from
1199 * beginning of MarkBasePos subtable */
1200 HBUINT16 classCount; /* Number of classes defined for marks */
1202 markArray; /* Offset to MarkArray table--from
1203 * beginning of MarkBasePos subtable */
1205 baseArray; /* Offset to BaseArray table--from
1206 * beginning of MarkBasePos subtable */
1208 DEFINE_SIZE_STATIC (12);
1213 template <typename context_t>
1214 typename context_t::return_t dispatch (context_t *c) const
1216 TRACE_DISPATCH (this, u.format);
1217 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1219 case 1: return_trace (c->dispatch (u.format1));
1220 default:return_trace (c->default_return_value ());
1226 HBUINT16 format; /* Format identifier */
1227 MarkBasePosFormat1 format1;
1232 typedef AnchorMatrix LigatureAttach; /* component-major--
1233 * in order of writing direction--,
1235 * ordered by class--zero-based. */
1237 typedef OffsetListOf<LigatureAttach> LigatureArray;
1238 /* Array of LigatureAttach
1240 * LigatureCoverage Index */
1242 struct MarkLigPosFormat1
1244 bool intersects (const hb_set_t *glyphs) const
1245 { return (this+markCoverage).intersects (glyphs) &&
1246 (this+ligatureCoverage).intersects (glyphs); }
1248 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1250 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1251 if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
1254 const Coverage &get_coverage () const { return this+markCoverage; }
1256 bool apply (hb_ot_apply_context_t *c) const
1259 hb_buffer_t *buffer = c->buffer;
1260 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1261 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1263 /* Now we search backwards for a non-mark glyph */
1264 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1265 skippy_iter.reset (buffer->idx, 1);
1266 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1267 if (!skippy_iter.prev ()) return_trace (false);
1269 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
1270 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1272 unsigned int j = skippy_iter.idx;
1273 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
1274 if (lig_index == NOT_COVERED) return_trace (false);
1276 const LigatureArray& lig_array = this+ligatureArray;
1277 const LigatureAttach& lig_attach = lig_array[lig_index];
1279 /* Find component to attach to */
1280 unsigned int comp_count = lig_attach.rows;
1281 if (unlikely (!comp_count)) return_trace (false);
1283 /* We must now check whether the ligature ID of the current mark glyph
1284 * is identical to the ligature ID of the found ligature. If yes, we
1285 * can directly use the component index. If not, we attach the mark
1286 * glyph to the last component of the ligature. */
1287 unsigned int comp_index;
1288 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1289 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1290 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
1291 if (lig_id && lig_id == mark_id && mark_comp > 0)
1292 comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
1294 comp_index = comp_count - 1;
1296 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
1299 bool subset (hb_subset_context_t *c) const
1301 TRACE_SUBSET (this);
1303 return_trace (false);
1306 bool sanitize (hb_sanitize_context_t *c) const
1308 TRACE_SANITIZE (this);
1309 return_trace (c->check_struct (this) &&
1310 markCoverage.sanitize (c, this) &&
1311 ligatureCoverage.sanitize (c, this) &&
1312 markArray.sanitize (c, this) &&
1313 ligatureArray.sanitize (c, this, (unsigned int) classCount));
1317 HBUINT16 format; /* Format identifier--format = 1 */
1319 markCoverage; /* Offset to Mark Coverage table--from
1320 * beginning of MarkLigPos subtable */
1322 ligatureCoverage; /* Offset to Ligature Coverage
1323 * table--from beginning of MarkLigPos
1325 HBUINT16 classCount; /* Number of defined mark classes */
1327 markArray; /* Offset to MarkArray table--from
1328 * beginning of MarkLigPos subtable */
1329 OffsetTo<LigatureArray>
1330 ligatureArray; /* Offset to LigatureArray table--from
1331 * beginning of MarkLigPos subtable */
1333 DEFINE_SIZE_STATIC (12);
1338 template <typename context_t>
1339 typename context_t::return_t dispatch (context_t *c) const
1341 TRACE_DISPATCH (this, u.format);
1342 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1344 case 1: return_trace (c->dispatch (u.format1));
1345 default:return_trace (c->default_return_value ());
1351 HBUINT16 format; /* Format identifier */
1352 MarkLigPosFormat1 format1;
1357 typedef AnchorMatrix Mark2Array; /* mark2-major--
1358 * in order of Mark2Coverage Index--,
1360 * ordered by class--zero-based. */
1362 struct MarkMarkPosFormat1
1364 bool intersects (const hb_set_t *glyphs) const
1365 { return (this+mark1Coverage).intersects (glyphs) &&
1366 (this+mark2Coverage).intersects (glyphs); }
1368 void collect_glyphs (hb_collect_glyphs_context_t *c) const
1370 if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
1371 if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
1374 const Coverage &get_coverage () const { return this+mark1Coverage; }
1376 bool apply (hb_ot_apply_context_t *c) const
1379 hb_buffer_t *buffer = c->buffer;
1380 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
1381 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
1383 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
1384 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1385 skippy_iter.reset (buffer->idx, 1);
1386 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
1387 if (!skippy_iter.prev ()) return_trace (false);
1389 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1391 unsigned int j = skippy_iter.idx;
1393 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1394 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1395 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1396 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
1398 if (likely (id1 == id2)) {
1399 if (id1 == 0) /* Marks belonging to the same base. */
1401 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1404 /* If ligature ids don't match, it may be the case that one of the marks
1405 * itself is a ligature. In which case match. */
1406 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1411 return_trace (false);
1414 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
1415 if (mark2_index == NOT_COVERED) return_trace (false);
1417 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
1420 bool subset (hb_subset_context_t *c) const
1422 TRACE_SUBSET (this);
1424 return_trace (false);
1427 bool sanitize (hb_sanitize_context_t *c) const
1429 TRACE_SANITIZE (this);
1430 return_trace (c->check_struct (this) &&
1431 mark1Coverage.sanitize (c, this) &&
1432 mark2Coverage.sanitize (c, this) &&
1433 mark1Array.sanitize (c, this) &&
1434 mark2Array.sanitize (c, this, (unsigned int) classCount));
1438 HBUINT16 format; /* Format identifier--format = 1 */
1440 mark1Coverage; /* Offset to Combining Mark1 Coverage
1441 * table--from beginning of MarkMarkPos
1444 mark2Coverage; /* Offset to Combining Mark2 Coverage
1445 * table--from beginning of MarkMarkPos
1447 HBUINT16 classCount; /* Number of defined mark classes */
1449 mark1Array; /* Offset to Mark1Array table--from
1450 * beginning of MarkMarkPos subtable */
1451 OffsetTo<Mark2Array>
1452 mark2Array; /* Offset to Mark2Array table--from
1453 * beginning of MarkMarkPos subtable */
1455 DEFINE_SIZE_STATIC (12);
1460 template <typename context_t>
1461 typename context_t::return_t dispatch (context_t *c) const
1463 TRACE_DISPATCH (this, u.format);
1464 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1466 case 1: return_trace (c->dispatch (u.format1));
1467 default:return_trace (c->default_return_value ());
1473 HBUINT16 format; /* Format identifier */
1474 MarkMarkPosFormat1 format1;
1479 struct ContextPos : Context {};
1481 struct ChainContextPos : ChainContext {};
1483 struct ExtensionPos : Extension<ExtensionPos>
1485 typedef struct PosLookupSubTable SubTable;
1495 struct PosLookupSubTable
1497 friend struct Lookup;
1498 friend struct PosLookup;
1512 template <typename context_t>
1513 typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1515 TRACE_DISPATCH (this, lookup_type);
1516 switch (lookup_type) {
1517 case Single: return_trace (u.single.dispatch (c));
1518 case Pair: return_trace (u.pair.dispatch (c));
1519 case Cursive: return_trace (u.cursive.dispatch (c));
1520 case MarkBase: return_trace (u.markBase.dispatch (c));
1521 case MarkLig: return_trace (u.markLig.dispatch (c));
1522 case MarkMark: return_trace (u.markMark.dispatch (c));
1523 case Context: return_trace (u.context.dispatch (c));
1524 case ChainContext: return_trace (u.chainContext.dispatch (c));
1525 case Extension: return_trace (u.extension.dispatch (c));
1526 default: return_trace (c->default_return_value ());
1535 MarkBasePos markBase;
1537 MarkMarkPos markMark;
1539 ChainContextPos chainContext;
1540 ExtensionPos extension;
1543 DEFINE_SIZE_MIN (0);
1547 struct PosLookup : Lookup
1549 typedef struct PosLookupSubTable SubTable;
1551 const SubTable& get_subtable (unsigned int i) const
1552 { return Lookup::get_subtable<SubTable> (i); }
1554 bool is_reverse () const
1559 bool apply (hb_ot_apply_context_t *c) const
1562 return_trace (dispatch (c));
1565 bool intersects (const hb_set_t *glyphs) const
1567 hb_intersects_context_t c (glyphs);
1568 return dispatch (&c);
1571 hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1572 { return dispatch (c); }
1574 template <typename set_t>
1575 void add_coverage (set_t *glyphs) const
1577 hb_add_coverage_context_t<set_t> c (glyphs);
1581 static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1583 template <typename context_t>
1584 static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1586 template <typename context_t>
1587 typename context_t::return_t dispatch (context_t *c) const
1588 { return Lookup::dispatch<SubTable> (c); }
1590 bool subset (hb_subset_context_t *c) const
1591 { return Lookup::subset<SubTable> (c); }
1593 bool sanitize (hb_sanitize_context_t *c) const
1594 { return Lookup::sanitize<SubTable> (c); }
1598 * GPOS -- Glyph Positioning
1599 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
1602 struct GPOS : GSUBGPOS
1604 static constexpr hb_tag_t tableTag = HB_OT_TAG_GPOS;
1606 const PosLookup& get_lookup (unsigned int i) const
1607 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
1609 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1610 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
1611 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
1613 bool subset (hb_subset_context_t *c) const
1614 { return GSUBGPOS::subset<PosLookup> (c); }
1616 bool sanitize (hb_sanitize_context_t *c) const
1617 { return GSUBGPOS::sanitize<PosLookup> (c); }
1619 HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
1620 hb_face_t *face) const;
1622 typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
1627 reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
1629 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1630 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
1633 pos[i].attach_chain() = 0;
1635 unsigned int j = (int) i + chain;
1637 /* Stop if we see new parent in the chain. */
1638 if (j == new_parent)
1641 reverse_cursive_minor_offset (pos, j, direction, new_parent);
1643 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1644 pos[j].y_offset = -pos[i].y_offset;
1646 pos[j].x_offset = -pos[i].x_offset;
1648 pos[j].attach_chain() = -chain;
1649 pos[j].attach_type() = type;
1652 propagate_attachment_offsets (hb_glyph_position_t *pos,
1655 hb_direction_t direction)
1657 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
1658 * offset of glyph they are attached to. */
1659 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1660 if (likely (!chain))
1663 pos[i].attach_chain() = 0;
1665 unsigned int j = (int) i + chain;
1667 if (unlikely (j >= len))
1670 propagate_attachment_offsets (pos, len, j, direction);
1672 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
1674 if (type & ATTACH_TYPE_CURSIVE)
1676 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1677 pos[i].y_offset += pos[j].y_offset;
1679 pos[i].x_offset += pos[j].x_offset;
1681 else /*if (type & ATTACH_TYPE_MARK)*/
1683 pos[i].x_offset += pos[j].x_offset;
1684 pos[i].y_offset += pos[j].y_offset;
1687 if (HB_DIRECTION_IS_FORWARD (direction))
1688 for (unsigned int k = j; k < i; k++) {
1689 pos[i].x_offset -= pos[k].x_advance;
1690 pos[i].y_offset -= pos[k].y_advance;
1693 for (unsigned int k = j + 1; k < i + 1; k++) {
1694 pos[i].x_offset += pos[k].x_advance;
1695 pos[i].y_offset += pos[k].y_advance;
1701 GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1703 unsigned int count = buffer->len;
1704 for (unsigned int i = 0; i < count; i++)
1705 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
1709 GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
1711 //_hb_buffer_assert_gsubgpos_vars (buffer);
1715 GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1717 _hb_buffer_assert_gsubgpos_vars (buffer);
1720 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
1721 hb_direction_t direction = buffer->props.direction;
1723 /* Handle attachments */
1724 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
1725 for (unsigned int i = 0; i < len; i++)
1726 propagate_attachment_offsets (pos, len, i, direction);
1730 struct GPOS_accelerator_t : GPOS::accelerator_t {};
1733 /* Out-of-class implementation for methods recursing */
1735 template <typename context_t>
1736 /*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1738 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1739 return l.dispatch (c);
1742 /*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
1744 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1745 unsigned int saved_lookup_props = c->lookup_props;
1746 unsigned int saved_lookup_index = c->lookup_index;
1747 c->set_lookup_index (lookup_index);
1748 c->set_lookup_props (l.get_props ());
1749 bool ret = l.dispatch (c);
1750 c->set_lookup_index (saved_lookup_index);
1751 c->set_lookup_props (saved_lookup_props);
1756 } /* namespace OT */
1759 #endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */