hb-ot-layout-gpos-table.hh 54.3 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3
 * Copyright © 2010,2012,2013  Google, Inc.
B
Behdad Esfahbod 已提交
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
27 28
 */

29 30
#ifndef HB_OT_LAYOUT_GPOS_TABLE_HH
#define HB_OT_LAYOUT_GPOS_TABLE_HH
B
Behdad Esfahbod 已提交
31

32
#include "hb-ot-layout-gsubgpos.hh"
B
Behdad Esfahbod 已提交
33

B
Behdad Esfahbod 已提交
34

35 36
namespace OT {

B
Behdad Esfahbod 已提交
37

38
/* buffer **position** var allocations */
39 40 41 42 43 44 45 46 47 48 49
#define attach_chain() var.i16[0] /* glyph to which this attaches to, relative to current glyphs; negative for going back, positive for forward. */
#define attach_type() var.u8[2] /* attachment type */
/* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */

enum attach_type_t {
  ATTACH_TYPE_NONE	= 0X00,

  /* Each attachment should be either a mark or a cursive; can't be both. */
  ATTACH_TYPE_MARK	= 0X01,
  ATTACH_TYPE_CURSIVE	= 0X02,
};
B
Behdad Esfahbod 已提交
50 51


B
Behdad Esfahbod 已提交
52 53
/* Shared Tables: ValueRecord, Anchor Table, and MarkArray */

B
Behdad Esfahbod 已提交
54
typedef HBUINT16 Value;
55

B
Behdad Esfahbod 已提交
56
typedef UnsizedArrayOf<Value> ValueRecord;
B
Behdad Esfahbod 已提交
57

B
Behdad Esfahbod 已提交
58
struct ValueFormat : HBUINT16
B
Behdad Esfahbod 已提交
59
{
B
Behdad Esfahbod 已提交
60
  enum Flags {
61 62 63 64 65 66 67 68 69 70
    xPlacement	= 0x0001u,	/* Includes horizontal adjustment for placement */
    yPlacement	= 0x0002u,	/* Includes vertical adjustment for placement */
    xAdvance	= 0x0004u,	/* Includes horizontal adjustment for advance */
    yAdvance	= 0x0008u,	/* Includes vertical adjustment for advance */
    xPlaDevice	= 0x0010u,	/* Includes horizontal Device table for placement */
    yPlaDevice	= 0x0020u,	/* Includes vertical Device table for placement */
    xAdvDevice	= 0x0040u,	/* Includes horizontal Device table for advance */
    yAdvDevice	= 0x0080u,	/* Includes vertical Device table for advance */
    ignored	= 0x0F00u,	/* Was used in TrueType Open for MM fonts */
    reserved	= 0xF000u,	/* For future use */
71

72
    devices	= 0x00F0u	/* Mask for having any Device table */
B
WIP  
Behdad Esfahbod 已提交
73
  };
74

B
WIP  
Behdad Esfahbod 已提交
75
/* All fields are options.  Only those available advance the value pointer. */
76
#if 0
B
Behdad Esfahbod 已提交
77
  HBINT16		xPlacement;		/* Horizontal adjustment for
78
					 * placement--in design units */
B
Behdad Esfahbod 已提交
79
  HBINT16		yPlacement;		/* Vertical adjustment for
80
					 * placement--in design units */
B
Behdad Esfahbod 已提交
81
  HBINT16		xAdvance;		/* Horizontal adjustment for
82 83
					 * advance--in design units (only used
					 * for horizontal writing) */
B
Behdad Esfahbod 已提交
84
  HBINT16		yAdvance;		/* Vertical adjustment for advance--in
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
					 * design units (only used for vertical
					 * writing) */
  Offset	xPlaDevice;		/* Offset to Device table for
					 * horizontal placement--measured from
					 * beginning of PosTable (may be NULL) */
  Offset	yPlaDevice;		/* Offset to Device table for vertical
					 * placement--measured from beginning
					 * of PosTable (may be NULL) */
  Offset	xAdvDevice;		/* Offset to Device table for
					 * horizontal advance--measured from
					 * beginning of PosTable (may be NULL) */
  Offset	yAdvDevice;		/* Offset to Device table for vertical
					 * advance--measured from beginning of
					 * PosTable (may be NULL) */
#endif
100

101 102
  unsigned int get_len () const  { return hb_popcount ((unsigned int) *this); }
  unsigned int get_size () const { return get_len () * Value::static_size; }
B
WIP  
Behdad Esfahbod 已提交
103

104
  bool apply_value (hb_ot_apply_context_t   *c,
105 106 107
		    const void           *base,
		    const Value          *values,
		    hb_glyph_position_t  &glyph_pos) const
B
WIP  
Behdad Esfahbod 已提交
108
  {
109
    bool ret = false;
B
WIP  
Behdad Esfahbod 已提交
110
    unsigned int format = *this;
111
    if (!format) return ret;
B
WIP  
Behdad Esfahbod 已提交
112

113
    hb_font_t *font = c->font;
114
    bool horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
115

116 117
    if (format & xPlacement) glyph_pos.x_offset  += font->em_scale_x (get_short (values++, &ret));
    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));
K
Konstantin Ritt 已提交
120
      values++;
121
    }
122
    /* y_advance values grow downward but font-space grows upward, hence negation */
123
    if (format & yAdvance) {
124
      if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values, &ret));
K
Konstantin Ritt 已提交
125
      values++;
126
    }
127

128
    if (!has_device ()) return ret;
B
Behdad Esfahbod 已提交
129

130 131
    bool use_x_device = font->x_ppem || font->num_coords;
    bool use_y_device = font->y_ppem || font->num_coords;
B
Behdad Esfahbod 已提交
132

133
    if (!use_x_device && !use_y_device) return ret;
B
Behdad Esfahbod 已提交
134

135
    const VariationStore &store = c->var_store;
136

137 138
    /* pixel -> fractional pixel */
    if (format & xPlaDevice) {
139
      if (use_x_device) glyph_pos.x_offset  += (base + get_device (values, &ret)).get_x_delta (font, store);
K
Konstantin Ritt 已提交
140
      values++;
141 142
    }
    if (format & yPlaDevice) {
143
      if (use_y_device) glyph_pos.y_offset  += (base + get_device (values, &ret)).get_y_delta (font, store);
K
Konstantin Ritt 已提交
144
      values++;
145 146
    }
    if (format & xAdvDevice) {
147
      if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store);
K
Konstantin Ritt 已提交
148
      values++;
149 150
    }
    if (format & yAdvDevice) {
151
      /* y_advance values grow downward but font-space grows upward, hence negation */
152
      if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values, &ret)).get_y_delta (font, store);
K
Konstantin Ritt 已提交
153
      values++;
154
    }
155
    return ret;
156
  }
B
WIP  
Behdad Esfahbod 已提交
157 158

  private:
159
  bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
B
Behdad Esfahbod 已提交
160
  {
B
WIP  
Behdad Esfahbod 已提交
161 162 163 164 165 166 167
    unsigned int format = *this;

    if (format & xPlacement) values++;
    if (format & yPlacement) values++;
    if (format & xAdvance)   values++;
    if (format & yAdvance)   values++;

B
Behdad Esfahbod 已提交
168 169 170 171
    if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
    if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
    if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
    if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
B
WIP  
Behdad Esfahbod 已提交
172 173 174 175

    return true;
  }

176
  static OffsetTo<Device>& get_device (Value* value)
B
Behdad Esfahbod 已提交
177
  { return *CastP<OffsetTo<Device> > (value); }
178
  static const OffsetTo<Device>& get_device (const Value* value, bool *worked=nullptr)
179
  {
B
Minor  
Behdad Esfahbod 已提交
180
    if (worked) *worked |= bool (*value);
181 182
    return *CastP<OffsetTo<Device> > (value);
  }
B
Behdad Esfahbod 已提交
183

184
  static const HBINT16& get_short (const Value* value, bool *worked=nullptr)
185
  {
B
Minor  
Behdad Esfahbod 已提交
186
    if (worked) *worked |= bool (*value);
187 188
    return *CastP<HBINT16> (value);
  }
B
Behdad Esfahbod 已提交
189

B
WIP  
Behdad Esfahbod 已提交
190 191
  public:

192
  bool has_device () const
193
  {
B
WIP  
Behdad Esfahbod 已提交
194 195 196 197
    unsigned int format = *this;
    return (format & devices) != 0;
  }

198
  bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
B
Behdad Esfahbod 已提交
199
  {
B
Behdad Esfahbod 已提交
200
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
201
    return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
B
WIP  
Behdad Esfahbod 已提交
202 203
  }

204
  bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
B
Behdad Esfahbod 已提交
205
  {
B
Behdad Esfahbod 已提交
206
    TRACE_SANITIZE (this);
B
WIP  
Behdad Esfahbod 已提交
207 208
    unsigned int len = get_len ();

209
    if (!c->check_range (values, count, get_size ())) return_trace (false);
B
WIP  
Behdad Esfahbod 已提交
210

B
Behdad Esfahbod 已提交
211
    if (!has_device ()) return_trace (true);
B
WIP  
Behdad Esfahbod 已提交
212 213

    for (unsigned int i = 0; i < count; i++) {
B
Behdad Esfahbod 已提交
214
      if (!sanitize_value_devices (c, base, values))
E
Ebrahim Byagowi 已提交
215
	return_trace (false);
B
WIP  
Behdad Esfahbod 已提交
216 217 218
      values += len;
    }

B
Behdad Esfahbod 已提交
219
    return_trace (true);
B
WIP  
Behdad Esfahbod 已提交
220 221
  }

B
Behdad Esfahbod 已提交
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
B
Behdad Esfahbod 已提交
224
  {
B
Behdad Esfahbod 已提交
225
    TRACE_SANITIZE (this);
B
WIP  
Behdad Esfahbod 已提交
226

B
Behdad Esfahbod 已提交
227
    if (!has_device ()) return_trace (true);
B
WIP  
Behdad Esfahbod 已提交
228 229

    for (unsigned int i = 0; i < count; i++) {
B
Behdad Esfahbod 已提交
230
      if (!sanitize_value_devices (c, base, values))
E
Ebrahim Byagowi 已提交
231
	return_trace (false);
B
WIP  
Behdad Esfahbod 已提交
232 233 234
      values += stride;
    }

B
Behdad Esfahbod 已提交
235
    return_trace (true);
B
WIP  
Behdad Esfahbod 已提交
236
  }
B
Behdad Esfahbod 已提交
237 238 239
};


B
Behdad Esfahbod 已提交
240 241
struct AnchorFormat1
{
242 243
  void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
		   float *x, float *y) const
B
Behdad Esfahbod 已提交
244
  {
245
    hb_font_t *font = c->font;
246 247
    *x = font->em_fscale_x (xCoordinate);
    *y = font->em_fscale_y (yCoordinate);
B
Behdad Esfahbod 已提交
248 249
  }

250
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
251
  {
B
Behdad Esfahbod 已提交
252
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
253
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
254 255
  }

256
  protected:
B
Behdad Esfahbod 已提交
257
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Minor  
Behdad Esfahbod 已提交
258 259
  FWORD		xCoordinate;		/* Horizontal value--in design units */
  FWORD		yCoordinate;		/* Vertical value--in design units */
260 261
  public:
  DEFINE_SIZE_STATIC (6);
B
Behdad Esfahbod 已提交
262 263
};

B
Behdad Esfahbod 已提交
264 265
struct AnchorFormat2
{
266 267
  void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
		   float *x, float *y) const
B
Behdad Esfahbod 已提交
268
  {
269 270 271
    hb_font_t *font = c->font;
    unsigned int x_ppem = font->x_ppem;
    unsigned int y_ppem = font->y_ppem;
272
    hb_position_t cx = 0, cy = 0;
273
    bool ret;
B
Behdad Esfahbod 已提交
274

275
    ret = (x_ppem || y_ppem) &&
276
	  font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
277 278
    *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
    *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
B
Behdad Esfahbod 已提交
279 280
  }

281
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
282
  {
B
Behdad Esfahbod 已提交
283
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
284
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
285 286
  }

287
  protected:
B
Behdad Esfahbod 已提交
288
  HBUINT16	format;			/* Format identifier--format = 2 */
B
Minor  
Behdad Esfahbod 已提交
289 290
  FWORD		xCoordinate;		/* Horizontal value--in design units */
  FWORD		yCoordinate;		/* Vertical value--in design units */
B
Behdad Esfahbod 已提交
291
  HBUINT16	anchorPoint;		/* Index to glyph contour point */
292 293
  public:
  DEFINE_SIZE_STATIC (8);
B
Behdad Esfahbod 已提交
294 295
};

B
Behdad Esfahbod 已提交
296 297
struct AnchorFormat3
{
298 299
  void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
		   float *x, float *y) const
B
Behdad Esfahbod 已提交
300
  {
301
    hb_font_t *font = c->font;
302 303
    *x = font->em_fscale_x (xCoordinate);
    *y = font->em_fscale_y (yCoordinate);
304

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)
B
Behdad Esfahbod 已提交
308
      *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
B
Behdad Esfahbod 已提交
309 310
  }

311
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
312
  {
B
Behdad Esfahbod 已提交
313
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
314
    return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
B
Behdad Esfahbod 已提交
315 316
  }

317
  protected:
B
Behdad Esfahbod 已提交
318
  HBUINT16	format;			/* Format identifier--format = 3 */
B
Minor  
Behdad Esfahbod 已提交
319 320
  FWORD		xCoordinate;		/* Horizontal value--in design units */
  FWORD		yCoordinate;		/* Vertical value--in design units */
B
Behdad Esfahbod 已提交
321 322 323 324 325 326 327 328
  OffsetTo<Device>
		xDeviceTable;		/* Offset to Device table for X
					 * coordinate-- from beginning of
					 * Anchor table (may be NULL) */
  OffsetTo<Device>
		yDeviceTable;		/* Offset to Device table for Y
					 * coordinate-- from beginning of
					 * Anchor table (may be NULL) */
329 330
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
331 332
};

B
Behdad Esfahbod 已提交
333 334
struct Anchor
{
335 336
  void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
		   float *x, float *y) const
B
Behdad Esfahbod 已提交
337
  {
B
Behdad Esfahbod 已提交
338 339
    *x = *y = 0;
    switch (u.format) {
340 341 342
    case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
    case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
    case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
343
    default:					      return;
B
Behdad Esfahbod 已提交
344 345 346
    }
  }

347
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
348
  {
B
Behdad Esfahbod 已提交
349
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
350
    if (!u.format.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
351
    switch (u.format) {
B
Behdad Esfahbod 已提交
352 353 354 355
    case 1: return_trace (u.format1.sanitize (c));
    case 2: return_trace (u.format2.sanitize (c));
    case 3: return_trace (u.format3.sanitize (c));
    default:return_trace (true);
B
Behdad Esfahbod 已提交
356 357 358
    }
  }

359
  protected:
B
Behdad Esfahbod 已提交
360
  union {
B
Behdad Esfahbod 已提交
361
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
362 363 364
  AnchorFormat1		format1;
  AnchorFormat2		format2;
  AnchorFormat3		format3;
B
Behdad Esfahbod 已提交
365
  } u;
B
Behdad Esfahbod 已提交
366
  public:
B
Behdad Esfahbod 已提交
367
  DEFINE_SIZE_UNION (2, format);
B
Behdad Esfahbod 已提交
368 369 370
};


371 372
struct AnchorMatrix
{
373 374 375
  const Anchor& get_anchor (unsigned int row, unsigned int col,
			    unsigned int cols, bool *found) const
  {
376
    *found = false;
377
    if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
378 379
    *found = !matrixZ[row * cols + col].is_null ();
    return this+matrixZ[row * cols + col];
380 381
  }

382
  bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
B
Behdad Esfahbod 已提交
383
  {
B
Behdad Esfahbod 已提交
384
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
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++)
B
Behdad Esfahbod 已提交
390 391
      if (!matrixZ[i].sanitize (c, this)) return_trace (false);
    return_trace (true);
392 393
  }

B
Behdad Esfahbod 已提交
394
  HBUINT16	rows;			/* Number of rows */
395
  protected:
396 397
  UnsizedArrayOf<OffsetTo<Anchor> >
		matrixZ;		/* Matrix of offsets to Anchor tables--
398
					 * from beginning of AnchorMatrix table */
B
Behdad Esfahbod 已提交
399
  public:
400
  DEFINE_SIZE_ARRAY (2, matrixZ);
401 402 403
};


B
Behdad Esfahbod 已提交
404 405
struct MarkRecord
{
406
  friend struct MarkArray;
B
Behdad Esfahbod 已提交
407

408
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
B
Behdad Esfahbod 已提交
409
  {
B
Behdad Esfahbod 已提交
410
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
411
    return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
B
Behdad Esfahbod 已提交
412 413
  }

414
  protected:
B
Behdad Esfahbod 已提交
415
  HBUINT16	klass;			/* Class defined for this mark */
B
Behdad Esfahbod 已提交
416 417 418
  OffsetTo<Anchor>
		markAnchor;		/* Offset to Anchor table--from
					 * beginning of MarkArray table */
B
Behdad Esfahbod 已提交
419 420
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
421 422
};

B
Behdad Esfahbod 已提交
423
struct MarkArray : ArrayOf<MarkRecord>	/* Array of MarkRecords--in Coverage order */
B
Behdad Esfahbod 已提交
424
{
425 426 427 428
  bool apply (hb_ot_apply_context_t *c,
	      unsigned int mark_index, unsigned int glyph_index,
	      const AnchorMatrix &anchors, unsigned int class_count,
	      unsigned int glyph_pos) const
429
  {
B
Behdad Esfahbod 已提交
430
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
431
    hb_buffer_t *buffer = c->buffer;
B
Behdad Esfahbod 已提交
432
    const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
433 434 435
    unsigned int mark_class = record.klass;

    const Anchor& mark_anchor = this + record.markAnchor;
436 437 438 439
    bool found;
    const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
    /* If this subtable doesn't have an anchor for this base and this class,
     * return false such that the subsequent subtables have a chance at it. */
B
Behdad Esfahbod 已提交
440
    if (unlikely (!found)) return_trace (false);
441

442
    float mark_x, mark_y, base_x, base_y;
443

444
    buffer->unsafe_to_break (glyph_pos, buffer->idx);
445 446
    mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
    glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
447

B
Behdad Esfahbod 已提交
448
    hb_glyph_position_t &o = buffer->cur_pos();
449 450
    o.x_offset = round (base_x - mark_x);
    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;
454

B
Behdad Esfahbod 已提交
455
    buffer->idx++;
B
Behdad Esfahbod 已提交
456
    return_trace (true);
457
  }
B
Behdad Esfahbod 已提交
458

459
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
460
  {
B
Behdad Esfahbod 已提交
461
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
462
    return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
B
Behdad Esfahbod 已提交
463
  }
B
Behdad Esfahbod 已提交
464 465 466 467 468
};


/* Lookups */

B
Behdad Esfahbod 已提交
469 470
struct SinglePosFormat1
{
471
  bool intersects (const hb_set_t *glyphs) const
472 473
  { return (this+coverage).intersects (glyphs); }

474
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
475 476
  {
    TRACE_COLLECT_GLYPHS (this);
477
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
478 479
  }

480
  const Coverage &get_coverage () const { return this+coverage; }
481

482
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
483
  {
B
Behdad Esfahbod 已提交
484
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
485 486
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
487
    if (likely (index == NOT_COVERED)) return_trace (false);
488

489
    valueFormat.apply_value (c, this, values, buffer->cur_pos());
490

B
Behdad Esfahbod 已提交
491
    buffer->idx++;
B
Behdad Esfahbod 已提交
492
    return_trace (true);
B
Behdad Esfahbod 已提交
493 494
  }

495
  bool subset (hb_subset_context_t *c) const
496 497 498 499 500 501
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

502
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
503
  {
B
Behdad Esfahbod 已提交
504
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
505 506 507
    return_trace (c->check_struct (this) &&
		  coverage.sanitize (c, this) &&
		  valueFormat.sanitize_value (c, this, values));
B
Behdad Esfahbod 已提交
508 509
  }

510
  protected:
B
Behdad Esfahbod 已提交
511
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
512 513 514
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
515
  ValueFormat	valueFormat;		/* Defines the types of data in the
B
Behdad Esfahbod 已提交
516 517 518 519
					 * ValueRecord */
  ValueRecord	values;			/* Defines positioning
					 * value(s)--applied to all glyphs in
					 * the Coverage table */
B
Behdad Esfahbod 已提交
520
  public:
521
  DEFINE_SIZE_ARRAY (6, values);
B
Behdad Esfahbod 已提交
522 523
};

B
Behdad Esfahbod 已提交
524 525
struct SinglePosFormat2
{
526
  bool intersects (const hb_set_t *glyphs) const
527 528
  { return (this+coverage).intersects (glyphs); }

529
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
530 531
  {
    TRACE_COLLECT_GLYPHS (this);
532
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
533 534
  }

535
  const Coverage &get_coverage () const { return this+coverage; }
536

537
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
538
  {
B
Behdad Esfahbod 已提交
539
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
540 541
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
542
    if (likely (index == NOT_COVERED)) return_trace (false);
543

B
Behdad Esfahbod 已提交
544
    if (likely (index >= valueCount)) return_trace (false);
545

546
    valueFormat.apply_value (c, this,
B
Behdad Esfahbod 已提交
547
			     &values[index * valueFormat.get_len ()],
B
Behdad Esfahbod 已提交
548
			     buffer->cur_pos());
549

B
Behdad Esfahbod 已提交
550
    buffer->idx++;
B
Behdad Esfahbod 已提交
551
    return_trace (true);
B
Behdad Esfahbod 已提交
552 553
  }

554
  bool subset (hb_subset_context_t *c) const
555 556 557 558 559 560
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

561
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
562
  {
B
Behdad Esfahbod 已提交
563
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
564 565 566
    return_trace (c->check_struct (this) &&
		  coverage.sanitize (c, this) &&
		  valueFormat.sanitize_values (c, this, values, valueCount));
B
Behdad Esfahbod 已提交
567 568
  }

569
  protected:
B
Behdad Esfahbod 已提交
570
  HBUINT16	format;			/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
571 572 573
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
574
  ValueFormat	valueFormat;		/* Defines the types of data in the
B
Behdad Esfahbod 已提交
575
					 * ValueRecord */
B
Behdad Esfahbod 已提交
576
  HBUINT16	valueCount;		/* Number of ValueRecords */
B
Behdad Esfahbod 已提交
577 578
  ValueRecord	values;			/* Array of ValueRecords--positioning
					 * values applied to glyphs */
B
Behdad Esfahbod 已提交
579
  public:
580
  DEFINE_SIZE_ARRAY (8, values);
B
Behdad Esfahbod 已提交
581 582
};

B
Behdad Esfahbod 已提交
583 584
struct SinglePos
{
585
  template <typename context_t>
586
  typename context_t::return_t dispatch (context_t *c) const
587
  {
588
    TRACE_DISPATCH (this, u.format);
589
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
590
    switch (u.format) {
B
Behdad Esfahbod 已提交
591 592 593
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    default:return_trace (c->default_return_value ());
594 595 596
    }
  }

597
  protected:
B
Behdad Esfahbod 已提交
598
  union {
B
Behdad Esfahbod 已提交
599
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
600 601
  SinglePosFormat1	format1;
  SinglePosFormat2	format2;
B
Behdad Esfahbod 已提交
602 603 604 605
  } u;
};


B
Behdad Esfahbod 已提交
606 607
struct PairValueRecord
{
B
Behdad Esfahbod 已提交
608
  friend struct PairSet;
B
Behdad Esfahbod 已提交
609

610
  protected:
B
Behdad Esfahbod 已提交
611 612 613 614 615
  GlyphID	secondGlyph;		/* GlyphID of second glyph in the
					 * pair--first glyph is listed in the
					 * Coverage table */
  ValueRecord	values;			/* Positioning data for the first glyph
					 * followed by for second glyph */
B
Behdad Esfahbod 已提交
616
  public:
617
  DEFINE_SIZE_ARRAY (2, values);
B
Behdad Esfahbod 已提交
618 619
};

B
Behdad Esfahbod 已提交
620 621
struct PairSet
{
B
Behdad Esfahbod 已提交
622 623
  friend struct PairPosFormat1;

624
  bool intersects (const hb_set_t *glyphs,
625 626
			  const ValueFormat *valueFormats) const
  {
B
Behdad Esfahbod 已提交
627 628
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
629 630
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);

631
    const PairValueRecord *record = &firstPairValueRecord;
632 633 634 635
    unsigned int count = len;
    for (unsigned int i = 0; i < count; i++)
    {
      if (glyphs->has (record->secondGlyph))
E
Ebrahim Byagowi 已提交
636
	return true;
637 638 639 640 641
      record = &StructAtOffset<const PairValueRecord> (record, record_size);
    }
    return false;
  }

642
  void collect_glyphs (hb_collect_glyphs_context_t *c,
643 644 645
			      const ValueFormat *valueFormats) const
  {
    TRACE_COLLECT_GLYPHS (this);
B
Behdad Esfahbod 已提交
646 647
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
B
Behdad Esfahbod 已提交
648
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
649

650
    const PairValueRecord *record = &firstPairValueRecord;
651
    c->input->add_array (&record->secondGlyph, len, record_size);
652 653
  }

654
  bool apply (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
655 656 657
		     const ValueFormat *valueFormats,
		     unsigned int pos) const
  {
B
Behdad Esfahbod 已提交
658
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
659
    hb_buffer_t *buffer = c->buffer;
B
Behdad Esfahbod 已提交
660 661
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
B
Behdad Esfahbod 已提交
662
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
B
Behdad Esfahbod 已提交
663

664
    unsigned int count = len;
665 666 667

    /* Hand-coded bsearch. */
    if (unlikely (!count))
B
Behdad Esfahbod 已提交
668
      return_trace (false);
669 670 671
    hb_codepoint_t x = buffer->info[pos].codepoint;
    int min = 0, max = (int) count - 1;
    while (min <= max)
B
Behdad Esfahbod 已提交
672
    {
673
      int mid = ((unsigned int) min + (unsigned int) max) / 2;
674
      const PairValueRecord *record = &StructAtOffset<PairValueRecord> (&firstPairValueRecord, record_size * mid);
675 676
      hb_codepoint_t mid_x = record->secondGlyph;
      if (x < mid_x)
E
Ebrahim Byagowi 已提交
677
	max = mid - 1;
678
      else if (x > mid_x)
E
Ebrahim Byagowi 已提交
679
	min = mid + 1;
680
      else
B
Behdad Esfahbod 已提交
681
      {
682
	/* Note the intentional use of "|" instead of short-circuit "||". */
B
Behdad Esfahbod 已提交
683 684
	if (valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos()) |
	    valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]))
685
	  buffer->unsafe_to_break (buffer->idx, pos + 1);
B
Behdad Esfahbod 已提交
686 687
	if (len2)
	  pos++;
B
Behdad Esfahbod 已提交
688
	buffer->idx = pos;
B
Behdad Esfahbod 已提交
689
	return_trace (true);
B
Behdad Esfahbod 已提交
690 691 692
      }
    }

B
Behdad Esfahbod 已提交
693
    return_trace (false);
B
Behdad Esfahbod 已提交
694 695
  }

696 697
  struct sanitize_closure_t
  {
B
Behdad Esfahbod 已提交
698 699
    const void *base;
    const ValueFormat *valueFormats;
B
Behdad Esfahbod 已提交
700
    unsigned int len1; /* valueFormats[0].get_len() */
B
Behdad Esfahbod 已提交
701 702 703
    unsigned int stride; /* 1 + len1 + len2 */
  };

704
  bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
B
Behdad Esfahbod 已提交
705
  {
B
Behdad Esfahbod 已提交
706
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
707
    if (!(c->check_struct (this)
708 709 710 711
       && c->check_range (&firstPairValueRecord,
			  len,
			  HBUINT16::static_size,
			  closure->stride))) return_trace (false);
B
Behdad Esfahbod 已提交
712 713

    unsigned int count = len;
714
    const PairValueRecord *record = &firstPairValueRecord;
B
Behdad Esfahbod 已提交
715 716
    return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
		  closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
B
Behdad Esfahbod 已提交
717 718
  }

719
  protected:
720 721 722 723
  HBUINT16		len;	/* Number of PairValueRecords */
  PairValueRecord	firstPairValueRecord;
				/* Array of PairValueRecords--ordered
				 * by GlyphID of the second glyph */
B
Behdad Esfahbod 已提交
724
  public:
725
  DEFINE_SIZE_MIN (2);
B
Behdad Esfahbod 已提交
726
};
B
Behdad Esfahbod 已提交
727

B
Behdad Esfahbod 已提交
728 729
struct PairPosFormat1
{
730
  bool intersects (const hb_set_t *glyphs) const
731 732
  {
    unsigned int count = pairSet.len;
733
    for (Coverage::Iter iter (this+coverage); iter.more (); iter.next ())
734 735
    {
      if (unlikely (iter.get_coverage () >= count))
E
Ebrahim Byagowi 已提交
736
	break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
737 738
      if (glyphs->has (iter.get_glyph ()) &&
	  (this+pairSet[iter.get_coverage ()]).intersects (glyphs, valueFormat))
E
Ebrahim Byagowi 已提交
739
	return true;
740 741 742 743
    }
    return false;
  }

744
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
745 746
  {
    TRACE_COLLECT_GLYPHS (this);
747
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
748 749
    unsigned int count = pairSet.len;
    for (unsigned int i = 0; i < count; i++)
750
      (this+pairSet[i]).collect_glyphs (c, valueFormat);
751 752
  }

753
  const Coverage &get_coverage () const { return this+coverage; }
754

755
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
756
  {
B
Behdad Esfahbod 已提交
757
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
758 759
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
760
    if (likely (index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
761

762
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
763
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
764
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
765

766
    return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
B
Behdad Esfahbod 已提交
767 768
  }

769
  bool subset (hb_subset_context_t *c) const
770 771 772 773 774 775
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

776
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
777
  {
B
Behdad Esfahbod 已提交
778
    TRACE_SANITIZE (this);
779

780 781
    if (!c->check_struct (this)) return_trace (false);

B
Behdad Esfahbod 已提交
782 783
    unsigned int len1 = valueFormat[0].get_len ();
    unsigned int len2 = valueFormat[1].get_len ();
784 785
    PairSet::sanitize_closure_t closure =
    {
B
Behdad Esfahbod 已提交
786
      this,
787
      valueFormat,
B
Behdad Esfahbod 已提交
788 789 790
      len1,
      1 + len1 + len2
    };
791

792
    return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
B
Behdad Esfahbod 已提交
793 794
  }

795
  protected:
B
Behdad Esfahbod 已提交
796
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
797 798 799
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
B
Behdad Esfahbod 已提交
800
  ValueFormat	valueFormat[2];		/* [0] Defines the types of data in
B
Behdad Esfahbod 已提交
801 802
					 * ValueRecord1--for the first glyph
					 * in the pair--may be zero (0) */
B
Behdad Esfahbod 已提交
803
					/* [1] Defines the types of data in
B
Behdad Esfahbod 已提交
804 805 806 807 808
					 * ValueRecord2--for the second glyph
					 * in the pair--may be zero (0) */
  OffsetArrayOf<PairSet>
		pairSet;		/* Array of PairSet tables
					 * ordered by Coverage Index */
809
  public:
810
  DEFINE_SIZE_ARRAY (10, pairSet);
B
Behdad Esfahbod 已提交
811 812
};

B
Behdad Esfahbod 已提交
813 814
struct PairPosFormat2
{
815
  bool intersects (const hb_set_t *glyphs) const
816 817 818 819 820
  {
    return (this+coverage).intersects (glyphs) &&
	   (this+classDef2).intersects (glyphs);
  }

821
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
822 823
  {
    TRACE_COLLECT_GLYPHS (this);
824
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
825
    if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
826 827
  }

828
  const Coverage &get_coverage () const { return this+coverage; }
829

830
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
831
  {
B
Behdad Esfahbod 已提交
832
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
833 834
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
835
    if (likely (index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
836

837
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
838
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
839
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
840 841 842 843 844

    unsigned int len1 = valueFormat1.get_len ();
    unsigned int len2 = valueFormat2.get_len ();
    unsigned int record_len = len1 + len2;

B
Behdad Esfahbod 已提交
845 846
    unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
    unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
B
Behdad Esfahbod 已提交
847
    if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
B
Behdad Esfahbod 已提交
848

B
Behdad Esfahbod 已提交
849
    const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
850 851 852 853
    /* Note the intentional use of "|" instead of short-circuit "||". */
    if (valueFormat1.apply_value (c, this, v, buffer->cur_pos()) |
	valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]))
      buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
B
Behdad Esfahbod 已提交
854

B
Behdad Esfahbod 已提交
855
    buffer->idx = skippy_iter.idx;
B
Behdad Esfahbod 已提交
856
    if (len2)
B
Behdad Esfahbod 已提交
857
      buffer->idx++;
B
Behdad Esfahbod 已提交
858

B
Behdad Esfahbod 已提交
859
    return_trace (true);
B
Behdad Esfahbod 已提交
860 861
  }

862
  bool subset (hb_subset_context_t *c) const
863 864 865 866 867 868
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

869
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
870
  {
B
Behdad Esfahbod 已提交
871
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
872 873 874
    if (!(c->check_struct (this)
       && coverage.sanitize (c, this)
       && classDef1.sanitize (c, this)
B
Behdad Esfahbod 已提交
875
       && classDef2.sanitize (c, this))) return_trace (false);
876

877 878 879
    unsigned int len1 = valueFormat1.get_len ();
    unsigned int len2 = valueFormat2.get_len ();
    unsigned int stride = len1 + len2;
B
Behdad Esfahbod 已提交
880
    unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
881
    unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
882 883 884
    return_trace (c->check_range ((const void *) values,
				  count,
				  record_size) &&
B
Behdad Esfahbod 已提交
885
		  valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
B
Behdad Esfahbod 已提交
886
		  valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
B
Behdad Esfahbod 已提交
887
  }
B
Behdad Esfahbod 已提交
888

889
  protected:
B
Behdad Esfahbod 已提交
890
  HBUINT16	format;			/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
891 892 893
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
894
  ValueFormat	valueFormat1;		/* ValueRecord definition--for the
B
Behdad Esfahbod 已提交
895 896
					 * first glyph of the pair--may be zero
					 * (0) */
897
  ValueFormat	valueFormat2;		/* ValueRecord definition--for the
B
Behdad Esfahbod 已提交
898 899 900 901 902 903 904 905 906 907
					 * second glyph of the pair--may be
					 * zero (0) */
  OffsetTo<ClassDef>
		classDef1;		/* Offset to ClassDef table--from
					 * beginning of PairPos subtable--for
					 * the first glyph of the pair */
  OffsetTo<ClassDef>
		classDef2;		/* Offset to ClassDef table--from
					 * beginning of PairPos subtable--for
					 * the second glyph of the pair */
B
Behdad Esfahbod 已提交
908
  HBUINT16	class1Count;		/* Number of classes in ClassDef1
B
Behdad Esfahbod 已提交
909
					 * table--includes Class0 */
B
Behdad Esfahbod 已提交
910
  HBUINT16	class2Count;		/* Number of classes in ClassDef2
B
Behdad Esfahbod 已提交
911 912 913 914
					 * table--includes Class0 */
  ValueRecord	values;			/* Matrix of value pairs:
					 * class1-major, class2-minor,
					 * Each entry has value1 and value2 */
B
Behdad Esfahbod 已提交
915
  public:
916
  DEFINE_SIZE_ARRAY (16, values);
B
Behdad Esfahbod 已提交
917 918
};

B
Behdad Esfahbod 已提交
919 920
struct PairPos
{
921
  template <typename context_t>
922
  typename context_t::return_t dispatch (context_t *c) const
923
  {
924
    TRACE_DISPATCH (this, u.format);
925
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
926
    switch (u.format) {
B
Behdad Esfahbod 已提交
927 928 929
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    default:return_trace (c->default_return_value ());
930 931 932
    }
  }

933
  protected:
B
Behdad Esfahbod 已提交
934
  union {
B
Behdad Esfahbod 已提交
935
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
936 937
  PairPosFormat1	format1;
  PairPosFormat2	format2;
B
Behdad Esfahbod 已提交
938 939 940 941
  } u;
};


B
Behdad Esfahbod 已提交
942 943
struct EntryExitRecord
{
B
Behdad Esfahbod 已提交
944 945
  friend struct CursivePosFormat1;

946
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
B
Behdad Esfahbod 已提交
947
  {
B
Behdad Esfahbod 已提交
948
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
949
    return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
B
Behdad Esfahbod 已提交
950 951
  }

952
  protected:
B
Behdad Esfahbod 已提交
953 954 955 956 957 958 959 960
  OffsetTo<Anchor>
		entryAnchor;		/* Offset to EntryAnchor table--from
					 * beginning of CursivePos
					 * subtable--may be NULL */
  OffsetTo<Anchor>
		exitAnchor;		/* Offset to ExitAnchor table--from
					 * beginning of CursivePos
					 * subtable--may be NULL */
B
Behdad Esfahbod 已提交
961 962
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
963 964
};

965 966 967
static void
reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);

B
Behdad Esfahbod 已提交
968 969
struct CursivePosFormat1
{
970
  bool intersects (const hb_set_t *glyphs) const
971 972
  { return (this+coverage).intersects (glyphs); }

973
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
974 975
  {
    TRACE_COLLECT_GLYPHS (this);
976
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
977 978
  }

979
  const Coverage &get_coverage () const { return this+coverage; }
980

981
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
982
  {
B
Behdad Esfahbod 已提交
983
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
984
    hb_buffer_t *buffer = c->buffer;
B
Behdad Esfahbod 已提交
985

B
Behdad Esfahbod 已提交
986
    const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage  (buffer->cur().codepoint)];
987
    if (!this_record.entryAnchor) return_trace (false);
988

989
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
990
    skippy_iter.reset (buffer->idx, 1);
991
    if (!skippy_iter.prev ()) return_trace (false);
992

993 994
    const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage  (buffer->info[skippy_iter.idx].codepoint)];
    if (!prev_record.exitAnchor) return_trace (false);
B
Behdad Esfahbod 已提交
995

996 997
    unsigned int i = skippy_iter.idx;
    unsigned int j = buffer->idx;
B
Behdad Esfahbod 已提交
998

999
    buffer->unsafe_to_break (i, j);
1000
    float entry_x, entry_y, exit_x, exit_y;
1001 1002
    (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
    (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
B
Behdad Esfahbod 已提交
1003

B
Behdad Esfahbod 已提交
1004
    hb_glyph_position_t *pos = buffer->pos;
B
Behdad Esfahbod 已提交
1005 1006 1007 1008 1009

    hb_position_t d;
    /* Main-direction adjustment */
    switch (c->direction) {
      case HB_DIRECTION_LTR:
1010
	pos[i].x_advance  = round (exit_x) + pos[i].x_offset;
B
Behdad Esfahbod 已提交
1011

1012
	d = round (entry_x) + pos[j].x_offset;
B
Behdad Esfahbod 已提交
1013 1014 1015 1016
	pos[j].x_advance -= d;
	pos[j].x_offset  -= d;
	break;
      case HB_DIRECTION_RTL:
1017
	d = round (exit_x) + pos[i].x_offset;
B
Behdad Esfahbod 已提交
1018 1019 1020
	pos[i].x_advance -= d;
	pos[i].x_offset  -= d;

1021
	pos[j].x_advance  = round (entry_x) + pos[j].x_offset;
B
Behdad Esfahbod 已提交
1022 1023
	break;
      case HB_DIRECTION_TTB:
1024
	pos[i].y_advance  = round (exit_y) + pos[i].y_offset;
B
Behdad Esfahbod 已提交
1025

1026
	d = round (entry_y) + pos[j].y_offset;
B
Behdad Esfahbod 已提交
1027 1028 1029 1030
	pos[j].y_advance -= d;
	pos[j].y_offset  -= d;
	break;
      case HB_DIRECTION_BTT:
1031
	d = round (exit_y) + pos[i].y_offset;
B
Behdad Esfahbod 已提交
1032 1033 1034
	pos[i].y_advance -= d;
	pos[i].y_offset  -= d;

1035
	pos[j].y_advance  = round (entry_y);
B
Behdad Esfahbod 已提交
1036 1037 1038 1039
	break;
      case HB_DIRECTION_INVALID:
      default:
	break;
B
Behdad Esfahbod 已提交
1040 1041
    }

B
Behdad Esfahbod 已提交
1042
    /* Cross-direction adjustment */
1043 1044 1045 1046 1047 1048

    /* We attach child to parent (think graph theory and rooted trees whereas
     * the root stays on baseline and each node aligns itself against its
     * parent.
     *
     * Optimize things for the case of RightToLeft, as that's most common in
1049
     * Arabic. */
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    unsigned int child  = i;
    unsigned int parent = j;
    hb_position_t x_offset = entry_x - exit_x;
    hb_position_t y_offset = entry_y - exit_y;
    if  (!(c->lookup_props & LookupFlag::RightToLeft))
    {
      unsigned int k = child;
      child = parent;
      parent = k;
      x_offset = -x_offset;
      y_offset = -y_offset;
B
Behdad Esfahbod 已提交
1061 1062
    }

1063 1064 1065 1066 1067 1068 1069
    /* If child was already connected to someone else, walk through its old
     * chain and reverse the link direction, such that the whole tree of its
     * previous connection now attaches to new parent.  Watch out for case
     * where new parent is on the path from old chain...
     */
    reverse_cursive_minor_offset (pos, child, c->direction, parent);

1070 1071
    pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
    pos[child].attach_chain() = (int) parent - (int) child;
1072
    buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
1073 1074 1075 1076 1077
    if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
      pos[child].y_offset = y_offset;
    else
      pos[child].x_offset = x_offset;

1078
    buffer->idx++;
B
Behdad Esfahbod 已提交
1079
    return_trace (true);
B
Behdad Esfahbod 已提交
1080 1081
  }

1082
  bool subset (hb_subset_context_t *c) const
1083 1084 1085 1086 1087 1088
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1089
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1090
  {
B
Behdad Esfahbod 已提交
1091
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1092
    return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
B
Behdad Esfahbod 已提交
1093 1094
  }

1095
  protected:
B
Behdad Esfahbod 已提交
1096
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1097 1098 1099 1100 1101 1102
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
  ArrayOf<EntryExitRecord>
		entryExitRecord;	/* Array of EntryExit records--in
					 * Coverage Index order */
1103
  public:
1104
  DEFINE_SIZE_ARRAY (6, entryExitRecord);
B
Behdad Esfahbod 已提交
1105 1106
};

B
Behdad Esfahbod 已提交
1107 1108
struct CursivePos
{
1109
  template <typename context_t>
1110
  typename context_t::return_t dispatch (context_t *c) const
1111
  {
1112
    TRACE_DISPATCH (this, u.format);
1113
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1114
    switch (u.format) {
B
Behdad Esfahbod 已提交
1115 1116
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1117 1118 1119
    }
  }

1120
  protected:
B
Behdad Esfahbod 已提交
1121
  union {
B
Behdad Esfahbod 已提交
1122
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1123
  CursivePosFormat1	format1;
B
Behdad Esfahbod 已提交
1124 1125 1126 1127
  } u;
};


1128 1129 1130
typedef AnchorMatrix BaseArray;		/* base-major--
					 * in order of BaseCoverage Index--,
					 * mark-minor--
B
Behdad Esfahbod 已提交
1131
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1132

B
Behdad Esfahbod 已提交
1133 1134
struct MarkBasePosFormat1
{
1135
  bool intersects (const hb_set_t *glyphs) const
1136 1137 1138
  { return (this+markCoverage).intersects (glyphs) &&
	   (this+baseCoverage).intersects (glyphs); }

1139
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1140 1141
  {
    TRACE_COLLECT_GLYPHS (this);
1142 1143
    if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
    if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
1144 1145
  }

1146
  const Coverage &get_coverage () const { return this+markCoverage; }
1147

1148
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1149
  {
B
Behdad Esfahbod 已提交
1150
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1151 1152
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark_index = (this+markCoverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1153
    if (likely (mark_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1154

B
Behdad Esfahbod 已提交
1155
    /* Now we search backwards for a non-mark glyph */
1156
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1157
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
1158
    skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1159
    do {
B
Behdad Esfahbod 已提交
1160
      if (!skippy_iter.prev ()) return_trace (false);
1161 1162
      /* We only want to attach to the first of a MultipleSubst sequence.
       * https://github.com/harfbuzz/harfbuzz/issues/740
1163 1164 1165
       * Reject others...
       * ...but stop if we find a mark in the MultipleSubst sequence:
       * https://github.com/harfbuzz/harfbuzz/issues/1020 */
B
Behdad Esfahbod 已提交
1166
      if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
1167 1168
	  0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
	  (skippy_iter.idx == 0 ||
1169
	   _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
1170
	   _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
1171 1172 1173 1174
	   _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
	   _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
	   _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
	   ))
B
Behdad Esfahbod 已提交
1175
	break;
1176
      skippy_iter.reject ();
1177
    } while (true);
1178

B
Behdad Esfahbod 已提交
1179
    /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
B
Behdad Esfahbod 已提交
1180
    //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
B
Behdad Esfahbod 已提交
1181

B
Behdad Esfahbod 已提交
1182
    unsigned int base_index = (this+baseCoverage).get_coverage  (buffer->info[skippy_iter.idx].codepoint);
B
Behdad Esfahbod 已提交
1183
    if (base_index == NOT_COVERED) return_trace (false);
B
Behdad Esfahbod 已提交
1184

B
Behdad Esfahbod 已提交
1185
    return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
B
Behdad Esfahbod 已提交
1186 1187
  }

1188
  bool subset (hb_subset_context_t *c) const
1189 1190 1191 1192 1193 1194
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1195
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1196
  {
B
Behdad Esfahbod 已提交
1197
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1198 1199 1200 1201 1202
    return_trace (c->check_struct (this) &&
		  markCoverage.sanitize (c, this) &&
		  baseCoverage.sanitize (c, this) &&
		  markArray.sanitize (c, this) &&
		  baseArray.sanitize (c, this, (unsigned int) classCount));
B
Behdad Esfahbod 已提交
1203 1204
  }

1205
  protected:
B
Behdad Esfahbod 已提交
1206
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1207 1208
  OffsetTo<Coverage>
		markCoverage;		/* Offset to MarkCoverage table--from
B
Behdad Esfahbod 已提交
1209
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1210 1211
  OffsetTo<Coverage>
		baseCoverage;		/* Offset to BaseCoverage table--from
B
Behdad Esfahbod 已提交
1212
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1213
  HBUINT16	classCount;		/* Number of classes defined for marks */
B
Behdad Esfahbod 已提交
1214 1215
  OffsetTo<MarkArray>
		markArray;		/* Offset to MarkArray table--from
B
Behdad Esfahbod 已提交
1216
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1217 1218
  OffsetTo<BaseArray>
		baseArray;		/* Offset to BaseArray table--from
B
Behdad Esfahbod 已提交
1219
					 * beginning of MarkBasePos subtable */
1220 1221
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1222 1223
};

B
Behdad Esfahbod 已提交
1224 1225
struct MarkBasePos
{
1226
  template <typename context_t>
1227
  typename context_t::return_t dispatch (context_t *c) const
1228
  {
1229
    TRACE_DISPATCH (this, u.format);
1230
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1231
    switch (u.format) {
B
Behdad Esfahbod 已提交
1232 1233
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1234 1235 1236
    }
  }

1237
  protected:
B
Behdad Esfahbod 已提交
1238
  union {
B
Behdad Esfahbod 已提交
1239
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1240
  MarkBasePosFormat1	format1;
B
Behdad Esfahbod 已提交
1241 1242 1243 1244
  } u;
};


1245 1246 1247
typedef AnchorMatrix LigatureAttach;	/* component-major--
					 * in order of writing direction--,
					 * mark-minor--
B
Behdad Esfahbod 已提交
1248
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1249

1250
typedef OffsetListOf<LigatureAttach> LigatureArray;
B
Behdad Esfahbod 已提交
1251
					/* Array of LigatureAttach
B
Behdad Esfahbod 已提交
1252 1253 1254
					 * tables ordered by
					 * LigatureCoverage Index */

B
Behdad Esfahbod 已提交
1255 1256
struct MarkLigPosFormat1
{
1257
  bool intersects (const hb_set_t *glyphs) const
1258 1259 1260
  { return (this+markCoverage).intersects (glyphs) &&
	   (this+ligatureCoverage).intersects (glyphs); }

1261
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1262 1263
  {
    TRACE_COLLECT_GLYPHS (this);
1264 1265
    if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
    if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
1266 1267
  }

1268
  const Coverage &get_coverage () const { return this+markCoverage; }
1269

1270
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1271
  {
B
Behdad Esfahbod 已提交
1272
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1273 1274
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark_index = (this+markCoverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1275
    if (likely (mark_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1276

B
Behdad Esfahbod 已提交
1277
    /* Now we search backwards for a non-mark glyph */
1278
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1279
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
1280
    skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
B
Behdad Esfahbod 已提交
1281
    if (!skippy_iter.prev ()) return_trace (false);
1282

B
Behdad Esfahbod 已提交
1283
    /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
B
Behdad Esfahbod 已提交
1284
    //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
B
Behdad Esfahbod 已提交
1285

B
Behdad Esfahbod 已提交
1286
    unsigned int j = skippy_iter.idx;
B
Behdad Esfahbod 已提交
1287
    unsigned int lig_index = (this+ligatureCoverage).get_coverage  (buffer->info[j].codepoint);
B
Behdad Esfahbod 已提交
1288
    if (lig_index == NOT_COVERED) return_trace (false);
B
Behdad Esfahbod 已提交
1289 1290

    const LigatureArray& lig_array = this+ligatureArray;
1291
    const LigatureAttach& lig_attach = lig_array[lig_index];
1292 1293

    /* Find component to attach to */
B
Behdad Esfahbod 已提交
1294
    unsigned int comp_count = lig_attach.rows;
B
Behdad Esfahbod 已提交
1295
    if (unlikely (!comp_count)) return_trace (false);
1296

B
Behdad Esfahbod 已提交
1297 1298 1299 1300
    /* We must now check whether the ligature ID of the current mark glyph
     * is identical to the ligature ID of the found ligature.  If yes, we
     * can directly use the component index.  If not, we attach the mark
     * glyph to the last component of the ligature. */
B
Behdad Esfahbod 已提交
1301
    unsigned int comp_index;
B
Behdad Esfahbod 已提交
1302 1303 1304
    unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
    unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
    unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
B
Behdad Esfahbod 已提交
1305
    if (lig_id && lig_id == mark_id && mark_comp > 0)
B
Behdad Esfahbod 已提交
1306
      comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
B
Behdad Esfahbod 已提交
1307
    else
B
Behdad Esfahbod 已提交
1308
      comp_index = comp_count - 1;
B
Behdad Esfahbod 已提交
1309

B
Behdad Esfahbod 已提交
1310
    return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
B
Behdad Esfahbod 已提交
1311 1312
  }

1313
  bool subset (hb_subset_context_t *c) const
1314 1315 1316 1317 1318 1319
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1320
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1321
  {
B
Behdad Esfahbod 已提交
1322
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1323 1324 1325 1326 1327
    return_trace (c->check_struct (this) &&
		  markCoverage.sanitize (c, this) &&
		  ligatureCoverage.sanitize (c, this) &&
		  markArray.sanitize (c, this) &&
		  ligatureArray.sanitize (c, this, (unsigned int) classCount));
B
Behdad Esfahbod 已提交
1328 1329
  }

1330
  protected:
B
Behdad Esfahbod 已提交
1331
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1332 1333
  OffsetTo<Coverage>
		markCoverage;		/* Offset to Mark Coverage table--from
B
Behdad Esfahbod 已提交
1334
					 * beginning of MarkLigPos subtable */
B
Behdad Esfahbod 已提交
1335 1336
  OffsetTo<Coverage>
		ligatureCoverage;	/* Offset to Ligature Coverage
B
Behdad Esfahbod 已提交
1337 1338
					 * table--from beginning of MarkLigPos
					 * subtable */
B
Behdad Esfahbod 已提交
1339
  HBUINT16	classCount;		/* Number of defined mark classes */
B
Behdad Esfahbod 已提交
1340 1341
  OffsetTo<MarkArray>
		markArray;		/* Offset to MarkArray table--from
B
Behdad Esfahbod 已提交
1342
					 * beginning of MarkLigPos subtable */
B
Behdad Esfahbod 已提交
1343 1344
  OffsetTo<LigatureArray>
		ligatureArray;		/* Offset to LigatureArray table--from
B
Behdad Esfahbod 已提交
1345
					 * beginning of MarkLigPos subtable */
1346 1347
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1348 1349
};

B
Behdad Esfahbod 已提交
1350 1351
struct MarkLigPos
{
1352
  template <typename context_t>
1353
  typename context_t::return_t dispatch (context_t *c) const
1354
  {
1355
    TRACE_DISPATCH (this, u.format);
1356
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1357
    switch (u.format) {
B
Behdad Esfahbod 已提交
1358 1359
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1360 1361 1362
    }
  }

1363
  protected:
B
Behdad Esfahbod 已提交
1364
  union {
B
Behdad Esfahbod 已提交
1365
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1366
  MarkLigPosFormat1	format1;
B
Behdad Esfahbod 已提交
1367 1368 1369 1370
  } u;
};


1371 1372 1373 1374
typedef AnchorMatrix Mark2Array;	/* mark2-major--
					 * in order of Mark2Coverage Index--,
					 * mark1-minor--
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1375

B
Behdad Esfahbod 已提交
1376 1377
struct MarkMarkPosFormat1
{
1378
  bool intersects (const hb_set_t *glyphs) const
1379 1380 1381
  { return (this+mark1Coverage).intersects (glyphs) &&
	   (this+mark2Coverage).intersects (glyphs); }

1382
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1383 1384
  {
    TRACE_COLLECT_GLYPHS (this);
1385 1386
    if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
    if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
1387 1388
  }

1389
  const Coverage &get_coverage () const { return this+mark1Coverage; }
1390

1391
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1392
  {
B
Behdad Esfahbod 已提交
1393
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1394 1395
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark1_index = (this+mark1Coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1396
    if (likely (mark1_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1397 1398

    /* now we search backwards for a suitable mark glyph until a non-mark glyph */
1399
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1400
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
1401
    skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
B
Behdad Esfahbod 已提交
1402
    if (!skippy_iter.prev ()) return_trace (false);
1403

B
Behdad Esfahbod 已提交
1404
    if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
B
Behdad Esfahbod 已提交
1405

B
Behdad Esfahbod 已提交
1406 1407
    unsigned int j = skippy_iter.idx;

B
Behdad Esfahbod 已提交
1408 1409 1410 1411
    unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
    unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
    unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
    unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
1412 1413 1414 1415 1416

    if (likely (id1 == id2)) {
      if (id1 == 0) /* Marks belonging to the same base. */
	goto good;
      else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
E
Ebrahim Byagowi 已提交
1417
	goto good;
1418 1419 1420 1421 1422 1423 1424 1425
    } else {
      /* If ligature ids don't match, it may be the case that one of the marks
       * itself is a ligature.  In which case match. */
      if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
	goto good;
    }

    /* Didn't match. */
B
Behdad Esfahbod 已提交
1426
    return_trace (false);
B
Behdad Esfahbod 已提交
1427

1428
    good:
B
Behdad Esfahbod 已提交
1429
    unsigned int mark2_index = (this+mark2Coverage).get_coverage  (buffer->info[j].codepoint);
B
Behdad Esfahbod 已提交
1430
    if (mark2_index == NOT_COVERED) return_trace (false);
B
Behdad Esfahbod 已提交
1431

B
Behdad Esfahbod 已提交
1432
    return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
B
Behdad Esfahbod 已提交
1433 1434
  }

1435
  bool subset (hb_subset_context_t *c) const
1436 1437 1438 1439 1440 1441
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1442
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1443
  {
B
Behdad Esfahbod 已提交
1444
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1445 1446 1447 1448 1449
    return_trace (c->check_struct (this) &&
		  mark1Coverage.sanitize (c, this) &&
		  mark2Coverage.sanitize (c, this) &&
		  mark1Array.sanitize (c, this) &&
		  mark2Array.sanitize (c, this, (unsigned int) classCount));
B
Behdad Esfahbod 已提交
1450 1451
  }

1452
  protected:
B
Behdad Esfahbod 已提交
1453
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1454 1455
  OffsetTo<Coverage>
		mark1Coverage;		/* Offset to Combining Mark1 Coverage
B
Behdad Esfahbod 已提交
1456 1457
					 * table--from beginning of MarkMarkPos
					 * subtable */
B
Behdad Esfahbod 已提交
1458 1459
  OffsetTo<Coverage>
		mark2Coverage;		/* Offset to Combining Mark2 Coverage
B
Behdad Esfahbod 已提交
1460 1461
					 * table--from beginning of MarkMarkPos
					 * subtable */
B
Behdad Esfahbod 已提交
1462
  HBUINT16	classCount;		/* Number of defined mark classes */
B
Behdad Esfahbod 已提交
1463 1464 1465 1466 1467 1468
  OffsetTo<MarkArray>
		mark1Array;		/* Offset to Mark1Array table--from
					 * beginning of MarkMarkPos subtable */
  OffsetTo<Mark2Array>
		mark2Array;		/* Offset to Mark2Array table--from
					 * beginning of MarkMarkPos subtable */
1469 1470
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1471 1472
};

B
Behdad Esfahbod 已提交
1473 1474
struct MarkMarkPos
{
1475
  template <typename context_t>
1476
  typename context_t::return_t dispatch (context_t *c) const
1477
  {
1478
    TRACE_DISPATCH (this, u.format);
1479
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1480
    switch (u.format) {
B
Behdad Esfahbod 已提交
1481 1482
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1483 1484 1485
    }
  }

1486
  protected:
B
Behdad Esfahbod 已提交
1487
  union {
B
Behdad Esfahbod 已提交
1488
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1489
  MarkMarkPosFormat1	format1;
B
Behdad Esfahbod 已提交
1490 1491 1492 1493
  } u;
};


B
Minor  
Behdad Esfahbod 已提交
1494
struct ContextPos : Context {};
B
Behdad Esfahbod 已提交
1495

B
Minor  
Behdad Esfahbod 已提交
1496
struct ChainContextPos : ChainContext {};
B
Behdad Esfahbod 已提交
1497

B
Behdad Esfahbod 已提交
1498
struct ExtensionPos : Extension<ExtensionPos>
B
Behdad Esfahbod 已提交
1499
{
B
Behdad Esfahbod 已提交
1500
  typedef struct PosLookupSubTable SubTable;
B
Behdad Esfahbod 已提交
1501 1502 1503
};


1504

B
Behdad Esfahbod 已提交
1505 1506 1507 1508 1509
/*
 * PosLookup
 */


B
Behdad Esfahbod 已提交
1510 1511
struct PosLookupSubTable
{
B
Behdad Esfahbod 已提交
1512
  friend struct Lookup;
B
Behdad Esfahbod 已提交
1513 1514
  friend struct PosLookup;

B
Behdad Esfahbod 已提交
1515
  enum Type {
1516 1517 1518 1519 1520 1521 1522 1523
    Single		= 1,
    Pair		= 2,
    Cursive		= 3,
    MarkBase		= 4,
    MarkLig		= 5,
    MarkMark		= 6,
    Context		= 7,
    ChainContext	= 8,
1524
    Extension		= 9
1525 1526
  };

1527
  template <typename context_t>
1528
  typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1529
  {
1530
    TRACE_DISPATCH (this, lookup_type);
1531
    switch (lookup_type) {
B
Behdad Esfahbod 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    case Single:		return_trace (u.single.dispatch (c));
    case Pair:			return_trace (u.pair.dispatch (c));
    case Cursive:		return_trace (u.cursive.dispatch (c));
    case MarkBase:		return_trace (u.markBase.dispatch (c));
    case MarkLig:		return_trace (u.markLig.dispatch (c));
    case MarkMark:		return_trace (u.markMark.dispatch (c));
    case Context:		return_trace (u.context.dispatch (c));
    case ChainContext:		return_trace (u.chainContext.dispatch (c));
    case Extension:		return_trace (u.extension.dispatch (c));
    default:			return_trace (c->default_return_value ());
1542
    }
1543 1544
  }

1545
  protected:
B
Behdad Esfahbod 已提交
1546
  union {
B
Behdad Esfahbod 已提交
1547 1548 1549 1550 1551 1552
  SinglePos		single;
  PairPos		pair;
  CursivePos		cursive;
  MarkBasePos		markBase;
  MarkLigPos		markLig;
  MarkMarkPos		markMark;
1553
  ContextPos		context;
B
Behdad Esfahbod 已提交
1554 1555
  ChainContextPos	chainContext;
  ExtensionPos		extension;
B
Behdad Esfahbod 已提交
1556
  } u;
B
Behdad Esfahbod 已提交
1557
  public:
B
Behdad Esfahbod 已提交
1558
  DEFINE_SIZE_MIN (0);
B
Behdad Esfahbod 已提交
1559 1560 1561
};


B
Behdad Esfahbod 已提交
1562 1563
struct PosLookup : Lookup
{
B
Behdad Esfahbod 已提交
1564 1565
  typedef struct PosLookupSubTable SubTable;

1566
  const SubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
1567
  { return Lookup::get_subtable<SubTable> (i); }
B
Behdad Esfahbod 已提交
1568

1569
  bool is_reverse () const
B
Behdad Esfahbod 已提交
1570 1571 1572 1573
  {
    return false;
  }

1574
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1575 1576
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1577
    return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1578 1579
  }

1580
  bool intersects (const hb_set_t *glyphs) const
1581 1582 1583 1584 1585
  {
    hb_intersects_context_t c (glyphs);
    return dispatch (&c);
  }

1586
  hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1587 1588
  {
    TRACE_COLLECT_GLYPHS (this);
B
Behdad Esfahbod 已提交
1589
    return_trace (dispatch (c));
1590 1591
  }

B
Behdad Esfahbod 已提交
1592
  template <typename set_t>
1593
  void add_coverage (set_t *glyphs) const
B
Behdad Esfahbod 已提交
1594
  {
1595 1596
    hb_add_coverage_context_t<set_t> c (glyphs);
    dispatch (&c);
B
Behdad Esfahbod 已提交
1597 1598
  }

1599
  static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1600

B
Behdad Esfahbod 已提交
1601
  template <typename context_t>
1602
  static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
1603 1604

  template <typename context_t>
1605
  typename context_t::return_t dispatch (context_t *c) const
B
Behdad Esfahbod 已提交
1606
  { return Lookup::dispatch<SubTable> (c); }
B
Behdad Esfahbod 已提交
1607

1608
  bool subset (hb_subset_context_t *c) const
1609 1610
  { return Lookup::subset<SubTable> (c); }

1611
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1612
  { return Lookup::sanitize<SubTable> (c); }
B
Behdad Esfahbod 已提交
1613 1614 1615
};

/*
1616 1617
 * GPOS -- Glyph Positioning
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
B
Behdad Esfahbod 已提交
1618 1619
 */

B
Behdad Esfahbod 已提交
1620 1621
struct GPOS : GSUBGPOS
{
1622
  enum { tableTag = HB_OT_TAG_GPOS };
B
Behdad Esfahbod 已提交
1623

1624
  const PosLookup& get_lookup (unsigned int i) const
1625
  { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
B
Behdad Esfahbod 已提交
1626

1627
  static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1628 1629
  static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
  static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
B
Behdad Esfahbod 已提交
1630

1631
  bool subset (hb_subset_context_t *c) const
1632 1633
  { return GSUBGPOS::subset<PosLookup> (c); }

1634
  bool sanitize (hb_sanitize_context_t *c) const
1635
  { return GSUBGPOS::sanitize<PosLookup> (c); }
B
WIP  
Behdad Esfahbod 已提交
1636

1637 1638 1639
  HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
				   hb_face_t *face) const;

B
WIP  
Behdad Esfahbod 已提交
1640
  typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
B
Behdad Esfahbod 已提交
1641 1642
};

B
Behdad Esfahbod 已提交
1643

1644 1645 1646
static void
reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
{
1647 1648
  int chain = pos[i].attach_chain(), type = pos[i].attach_type();
  if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
1649 1650
    return;

1651
  pos[i].attach_chain() = 0;
1652

1653
  unsigned int j = (int) i + chain;
B
Behdad Esfahbod 已提交
1654

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
  /* Stop if we see new parent in the chain. */
  if (j == new_parent)
    return;

  reverse_cursive_minor_offset (pos, j, direction, new_parent);

  if (HB_DIRECTION_IS_HORIZONTAL (direction))
    pos[j].y_offset = -pos[i].y_offset;
  else
    pos[j].x_offset = -pos[i].x_offset;

1666 1667
  pos[j].attach_chain() = -chain;
  pos[j].attach_type() = type;
1668
}
B
Behdad Esfahbod 已提交
1669
static void
1670 1671 1672 1673
propagate_attachment_offsets (hb_glyph_position_t *pos,
			      unsigned int len,
			      unsigned int i,
			      hb_direction_t direction)
B
Behdad Esfahbod 已提交
1674
{
1675 1676
  /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
   * offset of glyph they are attached to. */
1677
  int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1678
  if (likely (!chain))
B
Behdad Esfahbod 已提交
1679
    return;
B
Behdad Esfahbod 已提交
1680

1681 1682
  pos[i].attach_chain() = 0;

1683
  unsigned int j = (int) i + chain;
B
Behdad Esfahbod 已提交
1684

1685 1686
  if (unlikely (j >= len))
    return;
1687

1688
  propagate_attachment_offsets (pos, len, j, direction);
1689

1690
  assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
B
Behdad Esfahbod 已提交
1691

1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
  if (type & ATTACH_TYPE_CURSIVE)
  {
    if (HB_DIRECTION_IS_HORIZONTAL (direction))
      pos[i].y_offset += pos[j].y_offset;
    else
      pos[i].x_offset += pos[j].x_offset;
  }
  else /*if (type & ATTACH_TYPE_MARK)*/
  {
    pos[i].x_offset += pos[j].x_offset;
    pos[i].y_offset += pos[j].y_offset;
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

    assert (j < i);
    if (HB_DIRECTION_IS_FORWARD (direction))
      for (unsigned int k = j; k < i; k++) {
	pos[i].x_offset -= pos[k].x_advance;
	pos[i].y_offset -= pos[k].y_advance;
      }
    else
      for (unsigned int k = j + 1; k < i + 1; k++) {
	pos[i].x_offset += pos[k].x_advance;
	pos[i].y_offset += pos[k].y_advance;
      }
1715
  }
B
Behdad Esfahbod 已提交
1716 1717
}

B
Behdad Esfahbod 已提交
1718
void
1719
GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
B
Behdad Esfahbod 已提交
1720
{
1721 1722
  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++)
1723
    buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
B
Behdad Esfahbod 已提交
1724 1725
}

B
Behdad Esfahbod 已提交
1726
void
1727
GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
1728 1729 1730 1731 1732 1733
{
  //_hb_buffer_assert_gsubgpos_vars (buffer);
}

void
GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
B
Behdad Esfahbod 已提交
1734
{
1735 1736
  _hb_buffer_assert_gsubgpos_vars (buffer);

1737 1738
  unsigned int len;
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
B
Behdad Esfahbod 已提交
1739 1740 1741
  hb_direction_t direction = buffer->props.direction;

  /* Handle attachments */
1742 1743
  if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
    for (unsigned int i = 0; i < len; i++)
1744
      propagate_attachment_offsets (pos, len, i, direction);
B
Behdad Esfahbod 已提交
1745 1746
}

B
Behdad Esfahbod 已提交
1747

B
Behdad Esfahbod 已提交
1748 1749 1750
struct GPOS_accelerator_t : GPOS::accelerator_t {};


B
Behdad Esfahbod 已提交
1751 1752
/* Out-of-class implementation for methods recursing */

1753
template <typename context_t>
1754
/*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1755
{
B
Behdad Esfahbod 已提交
1756
  const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1757
  return l.dispatch (c);
1758 1759
}

1760
/*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
B
Behdad Esfahbod 已提交
1761
{
B
Behdad Esfahbod 已提交
1762
  const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1763
  unsigned int saved_lookup_props = c->lookup_props;
1764 1765 1766
  unsigned int saved_lookup_index = c->lookup_index;
  c->set_lookup_index (lookup_index);
  c->set_lookup_props (l.get_props ());
1767
  bool ret = l.dispatch (c);
1768
  c->set_lookup_index (saved_lookup_index);
1769
  c->set_lookup_props (saved_lookup_props);
1770
  return ret;
B
Behdad Esfahbod 已提交
1771 1772 1773
}


B
Behdad Esfahbod 已提交
1774
} /* namespace OT */
1775

B
Behdad Esfahbod 已提交
1776

1777
#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */