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
35namespace 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
43enum 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
54typedef HBUINT16 Value;
55
56typedef Value ValueRecord[VAR];
57
58struct 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 Offset xPlaDevice; /* Offset to Device table for
88 * horizontal placement--measured from
89 * beginning of PosTable (may be NULL) */
90 Offset yPlaDevice; /* Offset to Device table for vertical
91 * placement--measured from beginning
92 * of PosTable (may be NULL) */
93 Offset xAdvDevice; /* Offset to Device table for
94 * horizontal advance--measured from
95 * beginning of PosTable (may be NULL) */
96 Offset yAdvDevice; /* Offset to Device table for vertical
97 * advance--measured from beginning of
98 * PosTable (may be NULL) */
99#endif
100
101 inline unsigned int get_len (void) const
102 { return hb_popcount ((unsigned int) *this); }
103 inline unsigned int get_size (void) const
104 { return get_len () * Value::static_size; }
105
106 void apply_value (hb_ot_apply_context_t *c,
107 const void *base,
108 const Value *values,
109 hb_glyph_position_t &glyph_pos) const
110 {
111 unsigned int format = *this;
112 if (!format) return;
113
114 hb_font_t *font = c->font;
115 hb_bool_t horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
116
117 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++));
118 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++));
119 if (format & xAdvance) {
120 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values));
121 values++;
122 }
123 /* y_advance values grow downward but font-space grows upward, hence negation */
124 if (format & yAdvance) {
125 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values));
126 values++;
127 }
128
129 if (!has_device ()) return;
130
131 bool use_x_device = font->x_ppem || font->num_coords;
132 bool use_y_device = font->y_ppem || font->num_coords;
133
134 if (!use_x_device && !use_y_device) return;
135
136 const VariationStore &store = c->var_store;
137
138 /* pixel -> fractional pixel */
139 if (format & xPlaDevice) {
140 if (use_x_device) glyph_pos.x_offset += (base + get_device (values)).get_x_delta (font, store);
141 values++;
142 }
143 if (format & yPlaDevice) {
144 if (use_y_device) glyph_pos.y_offset += (base + get_device (values)).get_y_delta (font, store);
145 values++;
146 }
147 if (format & xAdvDevice) {
148 if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values)).get_x_delta (font, store);
149 values++;
150 }
151 if (format & yAdvDevice) {
152 /* y_advance values grow downward but font-space grows upward, hence negation */
153 if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values)).get_y_delta (font, store);
154 values++;
155 }
156 }
157
158 private:
159 inline 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
176 static inline OffsetTo<Device>& get_device (Value* value)
177 { return *CastP<OffsetTo<Device> > (value); }
178 static inline const OffsetTo<Device>& get_device (const Value* value)
179 { return *CastP<OffsetTo<Device> > (value); }
180
181 static inline const HBINT16& get_short (const Value* value)
182 { return *CastP<HBINT16> (value); }
183
184 public:
185
186 inline bool has_device (void) const {
187 unsigned int format = *this;
188 return (format & devices) != 0;
189 }
190
191 inline bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
192 {
193 TRACE_SANITIZE (this);
194 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
195 }
196
197 inline bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
198 {
199 TRACE_SANITIZE (this);
200 unsigned int len = get_len ();
201
202 if (!c->check_array (values, get_size (), count)) return_trace (false);
203
204 if (!has_device ()) return_trace (true);
205
206 for (unsigned int i = 0; i < count; i++) {
207 if (!sanitize_value_devices (c, base, values))
208 return_trace (false);
209 values += len;
210 }
211
212 return_trace (true);
213 }
214
215 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
216 inline bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count, unsigned int stride) const
217 {
218 TRACE_SANITIZE (this);
219
220 if (!has_device ()) return_trace (true);
221
222 for (unsigned int i = 0; i < count; i++) {
223 if (!sanitize_value_devices (c, base, values))
224 return_trace (false);
225 values += stride;
226 }
227
228 return_trace (true);
229 }
230};
231
232
233struct AnchorFormat1
234{
235 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
236 float *x, float *y) const
237 {
238 hb_font_t *font = c->font;
239 *x = font->em_fscale_x (xCoordinate);
240 *y = font->em_fscale_y (yCoordinate);
241 }
242
243 inline bool sanitize (hb_sanitize_context_t *c) const
244 {
245 TRACE_SANITIZE (this);
246 return_trace (c->check_struct (this));
247 }
248
249 protected:
250 HBUINT16 format; /* Format identifier--format = 1 */
251 FWORD xCoordinate; /* Horizontal value--in design units */
252 FWORD yCoordinate; /* Vertical value--in design units */
253 public:
254 DEFINE_SIZE_STATIC (6);
255};
256
257struct AnchorFormat2
258{
259 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
260 float *x, float *y) const
261 {
262 hb_font_t *font = c->font;
263 unsigned int x_ppem = font->x_ppem;
264 unsigned int y_ppem = font->y_ppem;
265 hb_position_t cx = 0, cy = 0;
266 hb_bool_t ret;
267
268 ret = (x_ppem || y_ppem) &&
269 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
270 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
271 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
272 }
273
274 inline bool sanitize (hb_sanitize_context_t *c) const
275 {
276 TRACE_SANITIZE (this);
277 return_trace (c->check_struct (this));
278 }
279
280 protected:
281 HBUINT16 format; /* Format identifier--format = 2 */
282 FWORD xCoordinate; /* Horizontal value--in design units */
283 FWORD yCoordinate; /* Vertical value--in design units */
284 HBUINT16 anchorPoint; /* Index to glyph contour point */
285 public:
286 DEFINE_SIZE_STATIC (8);
287};
288
289struct AnchorFormat3
290{
291 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
292 float *x, float *y) const
293 {
294 hb_font_t *font = c->font;
295 *x = font->em_fscale_x (xCoordinate);
296 *y = font->em_fscale_y (yCoordinate);
297
298 if (font->x_ppem || font->num_coords)
299 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
300 if (font->y_ppem || font->num_coords)
301 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
302 }
303
304 inline bool sanitize (hb_sanitize_context_t *c) const
305 {
306 TRACE_SANITIZE (this);
307 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
308 }
309
310 protected:
311 HBUINT16 format; /* Format identifier--format = 3 */
312 FWORD xCoordinate; /* Horizontal value--in design units */
313 FWORD yCoordinate; /* Vertical value--in design units */
314 OffsetTo<Device>
315 xDeviceTable; /* Offset to Device table for X
316 * coordinate-- from beginning of
317 * Anchor table (may be NULL) */
318 OffsetTo<Device>
319 yDeviceTable; /* Offset to Device table for Y
320 * coordinate-- from beginning of
321 * Anchor table (may be NULL) */
322 public:
323 DEFINE_SIZE_STATIC (10);
324};
325
326struct Anchor
327{
328 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
329 float *x, float *y) const
330 {
331 *x = *y = 0;
332 switch (u.format) {
333 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
334 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
335 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
336 default: return;
337 }
338 }
339
340 inline bool sanitize (hb_sanitize_context_t *c) const
341 {
342 TRACE_SANITIZE (this);
343 if (!u.format.sanitize (c)) return_trace (false);
344 switch (u.format) {
345 case 1: return_trace (u.format1.sanitize (c));
346 case 2: return_trace (u.format2.sanitize (c));
347 case 3: return_trace (u.format3.sanitize (c));
348 default:return_trace (true);
349 }
350 }
351
352 protected:
353 union {
354 HBUINT16 format; /* Format identifier */
355 AnchorFormat1 format1;
356 AnchorFormat2 format2;
357 AnchorFormat3 format3;
358 } u;
359 public:
360 DEFINE_SIZE_UNION (2, format);
361};
362
363
364struct AnchorMatrix
365{
366 inline const Anchor& get_anchor (unsigned int row, unsigned int col, unsigned int cols, bool *found) const {
367 *found = false;
368 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
369 *found = !matrixZ[row * cols + col].is_null ();
370 return this+matrixZ[row * cols + col];
371 }
372
373 inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
374 {
375 TRACE_SANITIZE (this);
376 if (!c->check_struct (this)) return_trace (false);
377 if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
378 unsigned int count = rows * cols;
379 if (!c->check_array (matrixZ, matrixZ[0].static_size, count)) return_trace (false);
380 for (unsigned int i = 0; i < count; i++)
381 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
382 return_trace (true);
383 }
384
385 HBUINT16 rows; /* Number of rows */
386 protected:
387 OffsetTo<Anchor>
388 matrixZ[VAR]; /* Matrix of offsets to Anchor tables--
389 * from beginning of AnchorMatrix table */
390 public:
391 DEFINE_SIZE_ARRAY (2, matrixZ);
392};
393
394
395struct MarkRecord
396{
397 friend struct MarkArray;
398
399 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
400 {
401 TRACE_SANITIZE (this);
402 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
403 }
404
405 protected:
406 HBUINT16 klass; /* Class defined for this mark */
407 OffsetTo<Anchor>
408 markAnchor; /* Offset to Anchor table--from
409 * beginning of MarkArray table */
410 public:
411 DEFINE_SIZE_STATIC (4);
412};
413
414struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
415{
416 inline bool apply (hb_ot_apply_context_t *c,
417 unsigned int mark_index, unsigned int glyph_index,
418 const AnchorMatrix &anchors, unsigned int class_count,
419 unsigned int glyph_pos) const
420 {
421 TRACE_APPLY (this);
422 hb_buffer_t *buffer = c->buffer;
423 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
424 unsigned int mark_class = record.klass;
425
426 const Anchor& mark_anchor = this + record.markAnchor;
427 bool found;
428 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
429 /* If this subtable doesn't have an anchor for this base and this class,
430 * return false such that the subsequent subtables have a chance at it. */
431 if (unlikely (!found)) return_trace (false);
432
433 float mark_x, mark_y, base_x, base_y;
434
435 buffer->unsafe_to_break (glyph_pos, buffer->idx);
436 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
437 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
438
439 hb_glyph_position_t &o = buffer->cur_pos();
440 o.x_offset = round (base_x - mark_x);
441 o.y_offset = round (base_y - mark_y);
442 o.attach_type() = ATTACH_TYPE_MARK;
443 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
444 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
445
446 buffer->idx++;
447 return_trace (true);
448 }
449
450 inline bool sanitize (hb_sanitize_context_t *c) const
451 {
452 TRACE_SANITIZE (this);
453 return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
454 }
455};
456
457
458/* Lookups */
459
460struct SinglePosFormat1
461{
462 inline bool intersects (const hb_set_t *glyphs) const
463 { return (this+coverage).intersects (glyphs); }
464
465 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
466 {
467 TRACE_COLLECT_GLYPHS (this);
468 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
469 }
470
471 inline const Coverage &get_coverage (void) const
472 { return this+coverage; }
473
474 inline bool apply (hb_ot_apply_context_t *c) const
475 {
476 TRACE_APPLY (this);
477 hb_buffer_t *buffer = c->buffer;
478 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
479 if (likely (index == NOT_COVERED)) return_trace (false);
480
481 valueFormat.apply_value (c, this, values, buffer->cur_pos());
482
483 buffer->idx++;
484 return_trace (true);
485 }
486
487 inline bool subset (hb_subset_context_t *c) const
488 {
489 TRACE_SUBSET (this);
490 // TODO(subset)
491 return_trace (false);
492 }
493
494 inline bool sanitize (hb_sanitize_context_t *c) const
495 {
496 TRACE_SANITIZE (this);
497 return_trace (c->check_struct (this) &&
498 coverage.sanitize (c, this) &&
499 valueFormat.sanitize_value (c, this, values));
500 }
501
502 protected:
503 HBUINT16 format; /* Format identifier--format = 1 */
504 OffsetTo<Coverage>
505 coverage; /* Offset to Coverage table--from
506 * beginning of subtable */
507 ValueFormat valueFormat; /* Defines the types of data in the
508 * ValueRecord */
509 ValueRecord values; /* Defines positioning
510 * value(s)--applied to all glyphs in
511 * the Coverage table */
512 public:
513 DEFINE_SIZE_ARRAY (6, values);
514};
515
516struct SinglePosFormat2
517{
518 inline bool intersects (const hb_set_t *glyphs) const
519 { return (this+coverage).intersects (glyphs); }
520
521 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
522 {
523 TRACE_COLLECT_GLYPHS (this);
524 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
525 }
526
527 inline const Coverage &get_coverage (void) const
528 { return this+coverage; }
529
530 inline bool apply (hb_ot_apply_context_t *c) const
531 {
532 TRACE_APPLY (this);
533 hb_buffer_t *buffer = c->buffer;
534 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
535 if (likely (index == NOT_COVERED)) return_trace (false);
536
537 if (likely (index >= valueCount)) return_trace (false);
538
539 valueFormat.apply_value (c, this,
540 &values[index * valueFormat.get_len ()],
541 buffer->cur_pos());
542
543 buffer->idx++;
544 return_trace (true);
545 }
546
547 inline bool subset (hb_subset_context_t *c) const
548 {
549 TRACE_SUBSET (this);
550 // TODO(subset)
551 return_trace (false);
552 }
553
554 inline bool sanitize (hb_sanitize_context_t *c) const
555 {
556 TRACE_SANITIZE (this);
557 return_trace (c->check_struct (this) &&
558 coverage.sanitize (c, this) &&
559 valueFormat.sanitize_values (c, this, values, valueCount));
560 }
561
562 protected:
563 HBUINT16 format; /* Format identifier--format = 2 */
564 OffsetTo<Coverage>
565 coverage; /* Offset to Coverage table--from
566 * beginning of subtable */
567 ValueFormat valueFormat; /* Defines the types of data in the
568 * ValueRecord */
569 HBUINT16 valueCount; /* Number of ValueRecords */
570 ValueRecord values; /* Array of ValueRecords--positioning
571 * values applied to glyphs */
572 public:
573 DEFINE_SIZE_ARRAY (8, values);
574};
575
576struct SinglePos
577{
578 template <typename context_t>
579 inline typename context_t::return_t dispatch (context_t *c) const
580 {
581 TRACE_DISPATCH (this, u.format);
582 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
583 switch (u.format) {
584 case 1: return_trace (c->dispatch (u.format1));
585 case 2: return_trace (c->dispatch (u.format2));
586 default:return_trace (c->default_return_value ());
587 }
588 }
589
590 protected:
591 union {
592 HBUINT16 format; /* Format identifier */
593 SinglePosFormat1 format1;
594 SinglePosFormat2 format2;
595 } u;
596};
597
598
599struct PairValueRecord
600{
601 friend struct PairSet;
602
603 protected:
604 GlyphID secondGlyph; /* GlyphID of second glyph in the
605 * pair--first glyph is listed in the
606 * Coverage table */
607 ValueRecord values; /* Positioning data for the first glyph
608 * followed by for second glyph */
609 public:
610 DEFINE_SIZE_ARRAY (2, values);
611};
612
613struct PairSet
614{
615 friend struct PairPosFormat1;
616
617 inline bool intersects (const hb_set_t *glyphs,
618 const ValueFormat *valueFormats) const
619 {
620 unsigned int len1 = valueFormats[0].get_len ();
621 unsigned int len2 = valueFormats[1].get_len ();
622 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
623
624 const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
625 unsigned int count = len;
626 for (unsigned int i = 0; i < count; i++)
627 {
628 if (glyphs->has (record->secondGlyph))
629 return true;
630 record = &StructAtOffset<const PairValueRecord> (record, record_size);
631 }
632 return false;
633 }
634
635 inline void collect_glyphs (hb_collect_glyphs_context_t *c,
636 const ValueFormat *valueFormats) const
637 {
638 TRACE_COLLECT_GLYPHS (this);
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);
642
643 const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
644 c->input->add_array (&record->secondGlyph, len, record_size);
645 }
646
647 inline bool apply (hb_ot_apply_context_t *c,
648 const ValueFormat *valueFormats,
649 unsigned int pos) const
650 {
651 TRACE_APPLY (this);
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);
656
657 const PairValueRecord *record_array = CastP<PairValueRecord> (arrayZ);
658 unsigned int count = len;
659
660 /* Hand-coded bsearch. */
661 if (unlikely (!count))
662 return_trace (false);
663 hb_codepoint_t x = buffer->info[pos].codepoint;
664 int min = 0, max = (int) count - 1;
665 while (min <= max)
666 {
667 int mid = (min + max) / 2;
668 const PairValueRecord *record = &StructAtOffset<PairValueRecord> (record_array, record_size * mid);
669 hb_codepoint_t mid_x = record->secondGlyph;
670 if (x < mid_x)
671 max = mid - 1;
672 else if (x > mid_x)
673 min = mid + 1;
674 else
675 {
676 buffer->unsafe_to_break (buffer->idx, pos + 1);
677 valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos());
678 valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]);
679 if (len2)
680 pos++;
681 buffer->idx = pos;
682 return_trace (true);
683 }
684 }
685
686 return_trace (false);
687 }
688
689 struct sanitize_closure_t
690 {
691 const void *base;
692 const ValueFormat *valueFormats;
693 unsigned int len1; /* valueFormats[0].get_len() */
694 unsigned int stride; /* 1 + len1 + len2 */
695 };
696
697 inline bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
698 {
699 TRACE_SANITIZE (this);
700 if (!(c->check_struct (this)
701 && c->check_array (arrayZ, HBUINT16::static_size * closure->stride, len))) return_trace (false);
702
703 unsigned int count = len;
704 const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
705 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
706 closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
707 }
708
709 protected:
710 HBUINT16 len; /* Number of PairValueRecords */
711 HBUINT16 arrayZ[VAR]; /* Array of PairValueRecords--ordered
712 * by GlyphID of the second glyph */
713 public:
714 DEFINE_SIZE_ARRAY (2, arrayZ);
715};
716
717struct PairPosFormat1
718{
719 inline bool intersects (const hb_set_t *glyphs) const
720 {
721 unsigned int count = pairSet.len;
722 for (hb_auto_t<Coverage::Iter> iter (this+coverage); iter.more (); iter.next ())
723 {
724 if (unlikely (iter.get_coverage () >= count))
725 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
726 if (glyphs->has (iter.get_glyph ()) &&
727 (this+pairSet[iter.get_coverage ()]).intersects (glyphs, valueFormat))
728 return true;
729 }
730 return false;
731 }
732
733 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
734 {
735 TRACE_COLLECT_GLYPHS (this);
736 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
737 unsigned int count = pairSet.len;
738 for (unsigned int i = 0; i < count; i++)
739 (this+pairSet[i]).collect_glyphs (c, valueFormat);
740 }
741
742 inline const Coverage &get_coverage (void) const
743 { return this+coverage; }
744
745 inline bool apply (hb_ot_apply_context_t *c) const
746 {
747 TRACE_APPLY (this);
748 hb_buffer_t *buffer = c->buffer;
749 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
750 if (likely (index == NOT_COVERED)) return_trace (false);
751
752 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
753 skippy_iter.reset (buffer->idx, 1);
754 if (!skippy_iter.next ()) return_trace (false);
755
756 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
757 }
758
759 inline bool subset (hb_subset_context_t *c) const
760 {
761 TRACE_SUBSET (this);
762 // TODO(subset)
763 return_trace (false);
764 }
765
766 inline bool sanitize (hb_sanitize_context_t *c) const
767 {
768 TRACE_SANITIZE (this);
769
770 if (!c->check_struct (this)) return_trace (false);
771
772 unsigned int len1 = valueFormat[0].get_len ();
773 unsigned int len2 = valueFormat[1].get_len ();
774 PairSet::sanitize_closure_t closure =
775 {
776 this,
777 valueFormat,
778 len1,
779 1 + len1 + len2
780 };
781
782 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
783 }
784
785 protected:
786 HBUINT16 format; /* Format identifier--format = 1 */
787 OffsetTo<Coverage>
788 coverage; /* Offset to Coverage table--from
789 * beginning of subtable */
790 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
791 * ValueRecord1--for the first glyph
792 * in the pair--may be zero (0) */
793 /* [1] Defines the types of data in
794 * ValueRecord2--for the second glyph
795 * in the pair--may be zero (0) */
796 OffsetArrayOf<PairSet>
797 pairSet; /* Array of PairSet tables
798 * ordered by Coverage Index */
799 public:
800 DEFINE_SIZE_ARRAY (10, pairSet);
801};
802
803struct PairPosFormat2
804{
805 inline bool intersects (const hb_set_t *glyphs) const
806 {
807 return (this+coverage).intersects (glyphs) &&
808 (this+classDef2).intersects (glyphs);
809 }
810
811 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
812 {
813 TRACE_COLLECT_GLYPHS (this);
814 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
815 if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
816 }
817
818 inline const Coverage &get_coverage (void) const
819 { return this+coverage; }
820
821 inline bool apply (hb_ot_apply_context_t *c) const
822 {
823 TRACE_APPLY (this);
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);
827
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);
831
832 unsigned int len1 = valueFormat1.get_len ();
833 unsigned int len2 = valueFormat2.get_len ();
834 unsigned int record_len = len1 + len2;
835
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);
839
840 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
841 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
842 valueFormat1.apply_value (c, this, v, buffer->cur_pos());
843 valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]);
844
845 buffer->idx = skippy_iter.idx;
846 if (len2)
847 buffer->idx++;
848
849 return_trace (true);
850 }
851
852 inline bool subset (hb_subset_context_t *c) const
853 {
854 TRACE_SUBSET (this);
855 // TODO(subset)
856 return_trace (false);
857 }
858
859 inline bool sanitize (hb_sanitize_context_t *c) const
860 {
861 TRACE_SANITIZE (this);
862 if (!(c->check_struct (this)
863 && coverage.sanitize (c, this)
864 && classDef1.sanitize (c, this)
865 && classDef2.sanitize (c, this))) return_trace (false);
866
867 unsigned int len1 = valueFormat1.get_len ();
868 unsigned int len2 = valueFormat2.get_len ();
869 unsigned int stride = len1 + len2;
870 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
871 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
872 return_trace (c->check_array (values, record_size, count) &&
873 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
874 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
875 }
876
877 protected:
878 HBUINT16 format; /* Format identifier--format = 2 */
879 OffsetTo<Coverage>
880 coverage; /* Offset to Coverage table--from
881 * beginning of subtable */
882 ValueFormat valueFormat1; /* ValueRecord definition--for the
883 * first glyph of the pair--may be zero
884 * (0) */
885 ValueFormat valueFormat2; /* ValueRecord definition--for the
886 * second glyph of the pair--may be
887 * zero (0) */
888 OffsetTo<ClassDef>
889 classDef1; /* Offset to ClassDef table--from
890 * beginning of PairPos subtable--for
891 * the first glyph of the pair */
892 OffsetTo<ClassDef>
893 classDef2; /* Offset to ClassDef table--from
894 * beginning of PairPos subtable--for
895 * the second glyph of the pair */
896 HBUINT16 class1Count; /* Number of classes in ClassDef1
897 * table--includes Class0 */
898 HBUINT16 class2Count; /* Number of classes in ClassDef2
899 * table--includes Class0 */
900 ValueRecord values; /* Matrix of value pairs:
901 * class1-major, class2-minor,
902 * Each entry has value1 and value2 */
903 public:
904 DEFINE_SIZE_ARRAY (16, values);
905};
906
907struct PairPos
908{
909 template <typename context_t>
910 inline typename context_t::return_t dispatch (context_t *c) const
911 {
912 TRACE_DISPATCH (this, u.format);
913 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
914 switch (u.format) {
915 case 1: return_trace (c->dispatch (u.format1));
916 case 2: return_trace (c->dispatch (u.format2));
917 default:return_trace (c->default_return_value ());
918 }
919 }
920
921 protected:
922 union {
923 HBUINT16 format; /* Format identifier */
924 PairPosFormat1 format1;
925 PairPosFormat2 format2;
926 } u;
927};
928
929
930struct EntryExitRecord
931{
932 friend struct CursivePosFormat1;
933
934 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
935 {
936 TRACE_SANITIZE (this);
937 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
938 }
939
940 protected:
941 OffsetTo<Anchor>
942 entryAnchor; /* Offset to EntryAnchor table--from
943 * beginning of CursivePos
944 * subtable--may be NULL */
945 OffsetTo<Anchor>
946 exitAnchor; /* Offset to ExitAnchor table--from
947 * beginning of CursivePos
948 * subtable--may be NULL */
949 public:
950 DEFINE_SIZE_STATIC (4);
951};
952
953static void
954reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
955
956struct CursivePosFormat1
957{
958 inline bool intersects (const hb_set_t *glyphs) const
959 { return (this+coverage).intersects (glyphs); }
960
961 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
962 {
963 TRACE_COLLECT_GLYPHS (this);
964 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
965 }
966
967 inline const Coverage &get_coverage (void) const
968 { return this+coverage; }
969
970 inline bool apply (hb_ot_apply_context_t *c) const
971 {
972 TRACE_APPLY (this);
973 hb_buffer_t *buffer = c->buffer;
974
975 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
976 if (!this_record.exitAnchor) return_trace (false);
977
978 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
979 skippy_iter.reset (buffer->idx, 1);
980 if (!skippy_iter.next ()) return_trace (false);
981
982 const EntryExitRecord &next_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
983 if (!next_record.entryAnchor) return_trace (false);
984
985 unsigned int i = buffer->idx;
986 unsigned int j = skippy_iter.idx;
987
988 buffer->unsafe_to_break (i, j);
989 float entry_x, entry_y, exit_x, exit_y;
990 (this+this_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
991 (this+next_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
992
993 hb_glyph_position_t *pos = buffer->pos;
994
995 hb_position_t d;
996 /* Main-direction adjustment */
997 switch (c->direction) {
998 case HB_DIRECTION_LTR:
999 pos[i].x_advance = round (exit_x) + pos[i].x_offset;
1000
1001 d = round (entry_x) + pos[j].x_offset;
1002 pos[j].x_advance -= d;
1003 pos[j].x_offset -= d;
1004 break;
1005 case HB_DIRECTION_RTL:
1006 d = round (exit_x) + pos[i].x_offset;
1007 pos[i].x_advance -= d;
1008 pos[i].x_offset -= d;
1009
1010 pos[j].x_advance = round (entry_x) + pos[j].x_offset;
1011 break;
1012 case HB_DIRECTION_TTB:
1013 pos[i].y_advance = round (exit_y) + pos[i].y_offset;
1014
1015 d = round (entry_y) + pos[j].y_offset;
1016 pos[j].y_advance -= d;
1017 pos[j].y_offset -= d;
1018 break;
1019 case HB_DIRECTION_BTT:
1020 d = round (exit_y) + pos[i].y_offset;
1021 pos[i].y_advance -= d;
1022 pos[i].y_offset -= d;
1023
1024 pos[j].y_advance = round (entry_y);
1025 break;
1026 case HB_DIRECTION_INVALID:
1027 default:
1028 break;
1029 }
1030
1031 /* Cross-direction adjustment */
1032
1033 /* We attach child to parent (think graph theory and rooted trees whereas
1034 * the root stays on baseline and each node aligns itself against its
1035 * parent.
1036 *
1037 * Optimize things for the case of RightToLeft, as that's most common in
1038 * Arabinc. */
1039 unsigned int child = i;
1040 unsigned int parent = j;
1041 hb_position_t x_offset = entry_x - exit_x;
1042 hb_position_t y_offset = entry_y - exit_y;
1043 if (!(c->lookup_props & LookupFlag::RightToLeft))
1044 {
1045 unsigned int k = child;
1046 child = parent;
1047 parent = k;
1048 x_offset = -x_offset;
1049 y_offset = -y_offset;
1050 }
1051
1052 /* If child was already connected to someone else, walk through its old
1053 * chain and reverse the link direction, such that the whole tree of its
1054 * previous connection now attaches to new parent. Watch out for case
1055 * where new parent is on the path from old chain...
1056 */
1057 reverse_cursive_minor_offset (pos, child, c->direction, parent);
1058
1059 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
1060 pos[child].attach_chain() = (int) parent - (int) child;
1061 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
1062 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
1063 pos[child].y_offset = y_offset;
1064 else
1065 pos[child].x_offset = x_offset;
1066
1067 buffer->idx = j;
1068 return_trace (true);
1069 }
1070
1071 inline bool subset (hb_subset_context_t *c) const
1072 {
1073 TRACE_SUBSET (this);
1074 // TODO(subset)
1075 return_trace (false);
1076 }
1077
1078 inline bool sanitize (hb_sanitize_context_t *c) const
1079 {
1080 TRACE_SANITIZE (this);
1081 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
1082 }
1083
1084 protected:
1085 HBUINT16 format; /* Format identifier--format = 1 */
1086 OffsetTo<Coverage>
1087 coverage; /* Offset to Coverage table--from
1088 * beginning of subtable */
1089 ArrayOf<EntryExitRecord>
1090 entryExitRecord; /* Array of EntryExit records--in
1091 * Coverage Index order */
1092 public:
1093 DEFINE_SIZE_ARRAY (6, entryExitRecord);
1094};
1095
1096struct CursivePos
1097{
1098 template <typename context_t>
1099 inline typename context_t::return_t dispatch (context_t *c) const
1100 {
1101 TRACE_DISPATCH (this, u.format);
1102 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1103 switch (u.format) {
1104 case 1: return_trace (c->dispatch (u.format1));
1105 default:return_trace (c->default_return_value ());
1106 }
1107 }
1108
1109 protected:
1110 union {
1111 HBUINT16 format; /* Format identifier */
1112 CursivePosFormat1 format1;
1113 } u;
1114};
1115
1116
1117typedef AnchorMatrix BaseArray; /* base-major--
1118 * in order of BaseCoverage Index--,
1119 * mark-minor--
1120 * ordered by class--zero-based. */
1121
1122struct MarkBasePosFormat1
1123{
1124 inline bool intersects (const hb_set_t *glyphs) const
1125 { return (this+markCoverage).intersects (glyphs) &&
1126 (this+baseCoverage).intersects (glyphs); }
1127
1128 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1129 {
1130 TRACE_COLLECT_GLYPHS (this);
1131 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1132 if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
1133 }
1134
1135 inline const Coverage &get_coverage (void) const
1136 { return this+markCoverage; }
1137
1138 inline bool apply (hb_ot_apply_context_t *c) const
1139 {
1140 TRACE_APPLY (this);
1141 hb_buffer_t *buffer = c->buffer;
1142 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1143 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1144
1145 /* Now we search backwards for a non-mark glyph */
1146 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1147 skippy_iter.reset (buffer->idx, 1);
1148 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1149 do {
1150 if (!skippy_iter.prev ()) return_trace (false);
1151 /* We only want to attach to the first of a MultipleSubst sequence.
1152 * https://github.com/harfbuzz/harfbuzz/issues/740
1153 * Reject others...
1154 * ...but stop if we find a mark in the MultipleSubst sequence:
1155 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
1156 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
1157 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
1158 (skippy_iter.idx == 0 ||
1159 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
1160 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
1161 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
1162 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
1163 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
1164 ))
1165 break;
1166 skippy_iter.reject ();
1167 } while (1);
1168
1169 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
1170 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1171
1172 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
1173 if (base_index == NOT_COVERED) return_trace (false);
1174
1175 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
1176 }
1177
1178 inline bool subset (hb_subset_context_t *c) const
1179 {
1180 TRACE_SUBSET (this);
1181 // TODO(subset)
1182 return_trace (false);
1183 }
1184
1185 inline bool sanitize (hb_sanitize_context_t *c) const
1186 {
1187 TRACE_SANITIZE (this);
1188 return_trace (c->check_struct (this) &&
1189 markCoverage.sanitize (c, this) &&
1190 baseCoverage.sanitize (c, this) &&
1191 markArray.sanitize (c, this) &&
1192 baseArray.sanitize (c, this, (unsigned int) classCount));
1193 }
1194
1195 protected:
1196 HBUINT16 format; /* Format identifier--format = 1 */
1197 OffsetTo<Coverage>
1198 markCoverage; /* Offset to MarkCoverage table--from
1199 * beginning of MarkBasePos subtable */
1200 OffsetTo<Coverage>
1201 baseCoverage; /* Offset to BaseCoverage table--from
1202 * beginning of MarkBasePos subtable */
1203 HBUINT16 classCount; /* Number of classes defined for marks */
1204 OffsetTo<MarkArray>
1205 markArray; /* Offset to MarkArray table--from
1206 * beginning of MarkBasePos subtable */
1207 OffsetTo<BaseArray>
1208 baseArray; /* Offset to BaseArray table--from
1209 * beginning of MarkBasePos subtable */
1210 public:
1211 DEFINE_SIZE_STATIC (12);
1212};
1213
1214struct MarkBasePos
1215{
1216 template <typename context_t>
1217 inline typename context_t::return_t dispatch (context_t *c) const
1218 {
1219 TRACE_DISPATCH (this, u.format);
1220 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1221 switch (u.format) {
1222 case 1: return_trace (c->dispatch (u.format1));
1223 default:return_trace (c->default_return_value ());
1224 }
1225 }
1226
1227 protected:
1228 union {
1229 HBUINT16 format; /* Format identifier */
1230 MarkBasePosFormat1 format1;
1231 } u;
1232};
1233
1234
1235typedef AnchorMatrix LigatureAttach; /* component-major--
1236 * in order of writing direction--,
1237 * mark-minor--
1238 * ordered by class--zero-based. */
1239
1240typedef OffsetListOf<LigatureAttach> LigatureArray;
1241 /* Array of LigatureAttach
1242 * tables ordered by
1243 * LigatureCoverage Index */
1244
1245struct MarkLigPosFormat1
1246{
1247 inline bool intersects (const hb_set_t *glyphs) const
1248 { return (this+markCoverage).intersects (glyphs) &&
1249 (this+ligatureCoverage).intersects (glyphs); }
1250
1251 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1252 {
1253 TRACE_COLLECT_GLYPHS (this);
1254 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1255 if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
1256 }
1257
1258 inline const Coverage &get_coverage (void) const
1259 { return this+markCoverage; }
1260
1261 inline bool apply (hb_ot_apply_context_t *c) const
1262 {
1263 TRACE_APPLY (this);
1264 hb_buffer_t *buffer = c->buffer;
1265 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
1266 if (likely (mark_index == NOT_COVERED)) return_trace (false);
1267
1268 /* Now we search backwards for a non-mark glyph */
1269 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1270 skippy_iter.reset (buffer->idx, 1);
1271 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1272 if (!skippy_iter.prev ()) return_trace (false);
1273
1274 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
1275 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1276
1277 unsigned int j = skippy_iter.idx;
1278 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
1279 if (lig_index == NOT_COVERED) return_trace (false);
1280
1281 const LigatureArray& lig_array = this+ligatureArray;
1282 const LigatureAttach& lig_attach = lig_array[lig_index];
1283
1284 /* Find component to attach to */
1285 unsigned int comp_count = lig_attach.rows;
1286 if (unlikely (!comp_count)) return_trace (false);
1287
1288 /* We must now check whether the ligature ID of the current mark glyph
1289 * is identical to the ligature ID of the found ligature. If yes, we
1290 * can directly use the component index. If not, we attach the mark
1291 * glyph to the last component of the ligature. */
1292 unsigned int comp_index;
1293 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1294 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1295 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
1296 if (lig_id && lig_id == mark_id && mark_comp > 0)
1297 comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
1298 else
1299 comp_index = comp_count - 1;
1300
1301 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
1302 }
1303
1304 inline bool subset (hb_subset_context_t *c) const
1305 {
1306 TRACE_SUBSET (this);
1307 // TODO(subset)
1308 return_trace (false);
1309 }
1310
1311 inline bool sanitize (hb_sanitize_context_t *c) const
1312 {
1313 TRACE_SANITIZE (this);
1314 return_trace (c->check_struct (this) &&
1315 markCoverage.sanitize (c, this) &&
1316 ligatureCoverage.sanitize (c, this) &&
1317 markArray.sanitize (c, this) &&
1318 ligatureArray.sanitize (c, this, (unsigned int) classCount));
1319 }
1320
1321 protected:
1322 HBUINT16 format; /* Format identifier--format = 1 */
1323 OffsetTo<Coverage>
1324 markCoverage; /* Offset to Mark Coverage table--from
1325 * beginning of MarkLigPos subtable */
1326 OffsetTo<Coverage>
1327 ligatureCoverage; /* Offset to Ligature Coverage
1328 * table--from beginning of MarkLigPos
1329 * subtable */
1330 HBUINT16 classCount; /* Number of defined mark classes */
1331 OffsetTo<MarkArray>
1332 markArray; /* Offset to MarkArray table--from
1333 * beginning of MarkLigPos subtable */
1334 OffsetTo<LigatureArray>
1335 ligatureArray; /* Offset to LigatureArray table--from
1336 * beginning of MarkLigPos subtable */
1337 public:
1338 DEFINE_SIZE_STATIC (12);
1339};
1340
1341struct MarkLigPos
1342{
1343 template <typename context_t>
1344 inline typename context_t::return_t dispatch (context_t *c) const
1345 {
1346 TRACE_DISPATCH (this, u.format);
1347 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1348 switch (u.format) {
1349 case 1: return_trace (c->dispatch (u.format1));
1350 default:return_trace (c->default_return_value ());
1351 }
1352 }
1353
1354 protected:
1355 union {
1356 HBUINT16 format; /* Format identifier */
1357 MarkLigPosFormat1 format1;
1358 } u;
1359};
1360
1361
1362typedef AnchorMatrix Mark2Array; /* mark2-major--
1363 * in order of Mark2Coverage Index--,
1364 * mark1-minor--
1365 * ordered by class--zero-based. */
1366
1367struct MarkMarkPosFormat1
1368{
1369 inline bool intersects (const hb_set_t *glyphs) const
1370 { return (this+mark1Coverage).intersects (glyphs) &&
1371 (this+mark2Coverage).intersects (glyphs); }
1372
1373 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1374 {
1375 TRACE_COLLECT_GLYPHS (this);
1376 if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
1377 if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
1378 }
1379
1380 inline const Coverage &get_coverage (void) const
1381 { return this+mark1Coverage; }
1382
1383 inline bool apply (hb_ot_apply_context_t *c) const
1384 {
1385 TRACE_APPLY (this);
1386 hb_buffer_t *buffer = c->buffer;
1387 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
1388 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
1389
1390 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
1391 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1392 skippy_iter.reset (buffer->idx, 1);
1393 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
1394 if (!skippy_iter.prev ()) return_trace (false);
1395
1396 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
1397
1398 unsigned int j = skippy_iter.idx;
1399
1400 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1401 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1402 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1403 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
1404
1405 if (likely (id1 == id2)) {
1406 if (id1 == 0) /* Marks belonging to the same base. */
1407 goto good;
1408 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1409 goto good;
1410 } else {
1411 /* If ligature ids don't match, it may be the case that one of the marks
1412 * itself is a ligature. In which case match. */
1413 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1414 goto good;
1415 }
1416
1417 /* Didn't match. */
1418 return_trace (false);
1419
1420 good:
1421 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
1422 if (mark2_index == NOT_COVERED) return_trace (false);
1423
1424 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
1425 }
1426
1427 inline bool subset (hb_subset_context_t *c) const
1428 {
1429 TRACE_SUBSET (this);
1430 // TODO(subset)
1431 return_trace (false);
1432 }
1433
1434 inline bool sanitize (hb_sanitize_context_t *c) const
1435 {
1436 TRACE_SANITIZE (this);
1437 return_trace (c->check_struct (this) &&
1438 mark1Coverage.sanitize (c, this) &&
1439 mark2Coverage.sanitize (c, this) &&
1440 mark1Array.sanitize (c, this) &&
1441 mark2Array.sanitize (c, this, (unsigned int) classCount));
1442 }
1443
1444 protected:
1445 HBUINT16 format; /* Format identifier--format = 1 */
1446 OffsetTo<Coverage>
1447 mark1Coverage; /* Offset to Combining Mark1 Coverage
1448 * table--from beginning of MarkMarkPos
1449 * subtable */
1450 OffsetTo<Coverage>
1451 mark2Coverage; /* Offset to Combining Mark2 Coverage
1452 * table--from beginning of MarkMarkPos
1453 * subtable */
1454 HBUINT16 classCount; /* Number of defined mark classes */
1455 OffsetTo<MarkArray>
1456 mark1Array; /* Offset to Mark1Array table--from
1457 * beginning of MarkMarkPos subtable */
1458 OffsetTo<Mark2Array>
1459 mark2Array; /* Offset to Mark2Array table--from
1460 * beginning of MarkMarkPos subtable */
1461 public:
1462 DEFINE_SIZE_STATIC (12);
1463};
1464
1465struct MarkMarkPos
1466{
1467 template <typename context_t>
1468 inline typename context_t::return_t dispatch (context_t *c) const
1469 {
1470 TRACE_DISPATCH (this, u.format);
1471 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1472 switch (u.format) {
1473 case 1: return_trace (c->dispatch (u.format1));
1474 default:return_trace (c->default_return_value ());
1475 }
1476 }
1477
1478 protected:
1479 union {
1480 HBUINT16 format; /* Format identifier */
1481 MarkMarkPosFormat1 format1;
1482 } u;
1483};
1484
1485
1486struct ContextPos : Context {};
1487
1488struct ChainContextPos : ChainContext {};
1489
1490struct ExtensionPos : Extension<ExtensionPos>
1491{
1492 typedef struct PosLookupSubTable SubTable;
1493};
1494
1495
1496
1497/*
1498 * PosLookup
1499 */
1500
1501
1502struct PosLookupSubTable
1503{
1504 friend struct Lookup;
1505 friend struct PosLookup;
1506
1507 enum Type {
1508 Single = 1,
1509 Pair = 2,
1510 Cursive = 3,
1511 MarkBase = 4,
1512 MarkLig = 5,
1513 MarkMark = 6,
1514 Context = 7,
1515 ChainContext = 8,
1516 Extension = 9
1517 };
1518
1519 template <typename context_t>
1520 inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1521 {
1522 TRACE_DISPATCH (this, lookup_type);
1523 if (unlikely (!c->may_dispatch (this, &u.sub_format))) return_trace (c->no_dispatch_return_value ());
1524 switch (lookup_type) {
1525 case Single: return_trace (u.single.dispatch (c));
1526 case Pair: return_trace (u.pair.dispatch (c));
1527 case Cursive: return_trace (u.cursive.dispatch (c));
1528 case MarkBase: return_trace (u.markBase.dispatch (c));
1529 case MarkLig: return_trace (u.markLig.dispatch (c));
1530 case MarkMark: return_trace (u.markMark.dispatch (c));
1531 case Context: return_trace (u.context.dispatch (c));
1532 case ChainContext: return_trace (u.chainContext.dispatch (c));
1533 case Extension: return_trace (u.extension.dispatch (c));
1534 default: return_trace (c->default_return_value ());
1535 }
1536 }
1537
1538 protected:
1539 union {
1540 HBUINT16 sub_format;
1541 SinglePos single;
1542 PairPos pair;
1543 CursivePos cursive;
1544 MarkBasePos markBase;
1545 MarkLigPos markLig;
1546 MarkMarkPos markMark;
1547 ContextPos context;
1548 ChainContextPos chainContext;
1549 ExtensionPos extension;
1550 } u;
1551 public:
1552 DEFINE_SIZE_UNION (2, sub_format);
1553};
1554
1555
1556struct PosLookup : Lookup
1557{
1558 typedef struct PosLookupSubTable SubTable;
1559
1560 inline const SubTable& get_subtable (unsigned int i) const
1561 { return Lookup::get_subtable<SubTable> (i); }
1562
1563 inline bool is_reverse (void) const
1564 {
1565 return false;
1566 }
1567
1568 inline bool apply (hb_ot_apply_context_t *c) const
1569 {
1570 TRACE_APPLY (this);
1571 return_trace (dispatch (c));
1572 }
1573
1574 inline bool intersects (const hb_set_t *glyphs) const
1575 {
1576 hb_intersects_context_t c (glyphs);
1577 return dispatch (&c);
1578 }
1579
1580 inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1581 {
1582 TRACE_COLLECT_GLYPHS (this);
1583 return_trace (dispatch (c));
1584 }
1585
1586 template <typename set_t>
1587 inline void add_coverage (set_t *glyphs) const
1588 {
1589 hb_add_coverage_context_t<set_t> c (glyphs);
1590 dispatch (&c);
1591 }
1592
1593 static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1594
1595 template <typename context_t>
1596 static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1597
1598 template <typename context_t>
1599 inline typename context_t::return_t dispatch (context_t *c) const
1600 { return Lookup::dispatch<SubTable> (c); }
1601
1602 inline bool subset (hb_subset_context_t *c) const
1603 { return Lookup::subset<SubTable> (c); }
1604
1605 inline bool sanitize (hb_sanitize_context_t *c) const
1606 { return Lookup::sanitize<SubTable> (c); }
1607};
1608
1609/*
1610 * GPOS -- Glyph Positioning
1611 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
1612 */
1613
1614struct GPOS : GSUBGPOS
1615{
1616 static const hb_tag_t tableTag = HB_OT_TAG_GPOS;
1617
1618 inline const PosLookup& get_lookup (unsigned int i) const
1619 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
1620
1621 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1622 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
1623 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
1624
1625 inline bool subset (hb_subset_context_t *c) const
1626 { return GSUBGPOS::subset<PosLookup> (c); }
1627
1628 inline bool sanitize (hb_sanitize_context_t *c) const
1629 { return GSUBGPOS::sanitize<PosLookup> (c); }
1630
1631 typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
1632};
1633
1634
1635static void
1636reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
1637{
1638 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1639 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
1640 return;
1641
1642 pos[i].attach_chain() = 0;
1643
1644 unsigned int j = (int) i + chain;
1645
1646 /* Stop if we see new parent in the chain. */
1647 if (j == new_parent)
1648 return;
1649
1650 reverse_cursive_minor_offset (pos, j, direction, new_parent);
1651
1652 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1653 pos[j].y_offset = -pos[i].y_offset;
1654 else
1655 pos[j].x_offset = -pos[i].x_offset;
1656
1657 pos[j].attach_chain() = -chain;
1658 pos[j].attach_type() = type;
1659}
1660static void
1661propagate_attachment_offsets (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
1662{
1663 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
1664 * offset of glyph they are attached to. */
1665 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1666 if (likely (!chain))
1667 return;
1668
1669 unsigned int j = (int) i + chain;
1670
1671 pos[i].attach_chain() = 0;
1672
1673 propagate_attachment_offsets (pos, j, direction);
1674
1675 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
1676
1677 if (type & ATTACH_TYPE_CURSIVE)
1678 {
1679 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1680 pos[i].y_offset += pos[j].y_offset;
1681 else
1682 pos[i].x_offset += pos[j].x_offset;
1683 }
1684 else /*if (type & ATTACH_TYPE_MARK)*/
1685 {
1686 pos[i].x_offset += pos[j].x_offset;
1687 pos[i].y_offset += pos[j].y_offset;
1688
1689 assert (j < i);
1690 if (HB_DIRECTION_IS_FORWARD (direction))
1691 for (unsigned int k = j; k < i; k++) {
1692 pos[i].x_offset -= pos[k].x_advance;
1693 pos[i].y_offset -= pos[k].y_advance;
1694 }
1695 else
1696 for (unsigned int k = j + 1; k < i + 1; k++) {
1697 pos[i].x_offset += pos[k].x_advance;
1698 pos[i].y_offset += pos[k].y_advance;
1699 }
1700 }
1701}
1702
1703void
1704GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1705{
1706 unsigned int count = buffer->len;
1707 for (unsigned int i = 0; i < count; i++)
1708 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
1709}
1710
1711void
1712GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1713{
1714 //_hb_buffer_assert_gsubgpos_vars (buffer);
1715}
1716
1717void
1718GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1719{
1720 _hb_buffer_assert_gsubgpos_vars (buffer);
1721
1722 unsigned int len;
1723 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
1724 hb_direction_t direction = buffer->props.direction;
1725
1726 /* Handle attachments */
1727 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
1728 for (unsigned int i = 0; i < len; i++)
1729 propagate_attachment_offsets (pos, i, direction);
1730}
1731
1732
1733/* Out-of-class implementation for methods recursing */
1734
1735template <typename context_t>
1736/*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1737{
1738 const PosLookup &l = _get_gpos_relaxed (c->face)->get_lookup (lookup_index);
1739 return l.dispatch (c);
1740}
1741
1742/*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
1743{
1744 const PosLookup &l = _get_gpos_relaxed (c->face).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);
1752 return ret;
1753}
1754
1755struct GPOS_accelerator_t : GPOS::accelerator_t {};
1756
1757
1758#undef attach_chain
1759#undef attach_type
1760
1761
1762} /* namespace OT */
1763
1764
1765#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */
1766