hb-ot-layout-gpos-table.hh 55.1 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

56
typedef Value ValueRecord[VAR];
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

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

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

115 116 117
    hb_font_t *font = c->font;
    hb_bool_t horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);

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

130
    if (!has_device ()) return ret;
B
Behdad Esfahbod 已提交
131

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

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

137
    const VariationStore &store = c->var_store;
138

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

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

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

B
Behdad Esfahbod 已提交
170 171 172 173
    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 已提交
174 175 176 177

    return true;
  }

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

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

B
WIP  
Behdad Esfahbod 已提交
192 193
  public:

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

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

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

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

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

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

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

B
Behdad Esfahbod 已提交
223
  /* Just sanitize referenced Device tables.  Doesn't check the values themselves. */
B
Behdad Esfahbod 已提交
224 225
  inline 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 已提交
226
    TRACE_SANITIZE (this);
B
WIP  
Behdad Esfahbod 已提交
227

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

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

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


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

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

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

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

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

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

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

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

306
    if (font->x_ppem || font->num_coords)
307
      *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
308
    if (font->y_ppem || font->num_coords)
B
Behdad Esfahbod 已提交
309
      *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
B
Behdad Esfahbod 已提交
310 311
  }

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

318
  protected:
B
Behdad Esfahbod 已提交
319
  HBUINT16	format;			/* Format identifier--format = 3 */
B
Minor  
Behdad Esfahbod 已提交
320 321
  FWORD		xCoordinate;		/* Horizontal value--in design units */
  FWORD		yCoordinate;		/* Vertical value--in design units */
B
Behdad Esfahbod 已提交
322 323 324 325 326 327 328 329
  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) */
330 331
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
332 333
};

B
Behdad Esfahbod 已提交
334 335
struct Anchor
{
336
  inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
337
			  float *x, float *y) const
B
Behdad Esfahbod 已提交
338
  {
B
Behdad Esfahbod 已提交
339 340
    *x = *y = 0;
    switch (u.format) {
341 342 343
    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;
344
    default:					      return;
B
Behdad Esfahbod 已提交
345 346 347
    }
  }

B
Behdad Esfahbod 已提交
348 349
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
350
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
351
    if (!u.format.sanitize (c)) return_trace (false);
B
Behdad Esfahbod 已提交
352
    switch (u.format) {
B
Behdad Esfahbod 已提交
353 354 355 356
    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 已提交
357 358 359
    }
  }

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


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

B
Behdad Esfahbod 已提交
381 382
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
  {
B
Behdad Esfahbod 已提交
383
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
384
    if (!c->check_struct (this)) return_trace (false);
385
    if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
386
    unsigned int count = rows * cols;
387
    if (!c->check_array (matrixZ.arrayZ, count)) return_trace (false);
388
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
389 390
      if (!matrixZ[i].sanitize (c, this)) return_trace (false);
    return_trace (true);
391 392
  }

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


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

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

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

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

    const Anchor& mark_anchor = this + record.markAnchor;
435 436 437 438
    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 已提交
439
    if (unlikely (!found)) return_trace (false);
440

441
    float mark_x, mark_y, base_x, base_y;
442

443
    buffer->unsafe_to_break (glyph_pos, buffer->idx);
444 445
    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);
446

B
Behdad Esfahbod 已提交
447
    hb_glyph_position_t &o = buffer->cur_pos();
448 449
    o.x_offset = round (base_x - mark_x);
    o.y_offset = round (base_y - mark_y);
450
    o.attach_type() = ATTACH_TYPE_MARK;
451
    o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
452
    buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
453

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

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


/* Lookups */

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

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

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

482
  inline 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 496 497 498 499 500 501
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
502 503
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
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 527 528
  inline bool intersects (const hb_set_t *glyphs) const
  { return (this+coverage).intersects (glyphs); }

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

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

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

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

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

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

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

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

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

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

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


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

611
  protected:
B
Behdad Esfahbod 已提交
612 613 614 615 616
  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 已提交
617
  public:
618
  DEFINE_SIZE_ARRAY (2, values);
B
Behdad Esfahbod 已提交
619 620
};

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

625 626 627 628 629 630 631
  inline bool intersects (const hb_set_t *glyphs,
			  const ValueFormat *valueFormats) const
  {
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);

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

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

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

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

665
    unsigned int count = len;
666 667 668

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

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

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

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

    unsigned int count = len;
712
    const PairValueRecord *record = &firstPairValueRecord;
B
Behdad Esfahbod 已提交
713 714
    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 已提交
715 716
  }

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

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

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

751
  inline const Coverage &get_coverage (void) const
752
  { return this+coverage; }
753

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

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

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

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

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

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

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

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

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

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

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

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

830
  inline 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 863 864 865 866 867 868
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
869 870
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
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
    return_trace (c->check_array (values, count, record_size) &&
B
Behdad Esfahbod 已提交
883 884
		  valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
		  valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
B
Behdad Esfahbod 已提交
885
  }
B
Behdad Esfahbod 已提交
886

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

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

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


B
Behdad Esfahbod 已提交
940 941
struct EntryExitRecord
{
B
Behdad Esfahbod 已提交
942 943
  friend struct CursivePosFormat1;

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

950
  protected:
B
Behdad Esfahbod 已提交
951 952 953 954 955 956 957 958
  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 已提交
959 960
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
961 962
};

963 964 965
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 已提交
966 967
struct CursivePosFormat1
{
968 969 970
  inline bool intersects (const hb_set_t *glyphs) const
  { return (this+coverage).intersects (glyphs); }

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

977
  inline const Coverage &get_coverage (void) const
978
  { return this+coverage; }
979

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

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

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

992 993
    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 已提交
994

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

998
    buffer->unsafe_to_break (i, j);
999
    float entry_x, entry_y, exit_x, exit_y;
1000 1001
    (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 已提交
1002

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

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

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

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

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

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

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

    /* 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
1048
     * Arabic. */
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    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 已提交
1060 1061
    }

1062 1063 1064 1065 1066 1067 1068
    /* 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);

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

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

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

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

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

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

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


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

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

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

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

1148
  inline 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 1189 1190 1191 1192 1193 1194
  inline bool subset (hb_subset_context_t *c) const
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

B
Behdad Esfahbod 已提交
1195 1196
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
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
  inline 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 1258 1259 1260
  inline bool intersects (const hb_set_t *glyphs) const
  { return (this+markCoverage).intersects (glyphs) &&
	   (this+ligatureCoverage).intersects (glyphs); }

1261 1262 1263
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
    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
  inline const Coverage &get_coverage (void) const
1269
  { return this+markCoverage; }
1270

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

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

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

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

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

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

B
Behdad Esfahbod 已提交
1298 1299 1300 1301
    /* 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 已提交
1302
    unsigned int comp_index;
B
Behdad Esfahbod 已提交
1303 1304 1305
    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 已提交
1306
    if (lig_id && lig_id == mark_id && mark_comp > 0)
B
Behdad Esfahbod 已提交
1307
      comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
B
Behdad Esfahbod 已提交
1308
    else
B
Behdad Esfahbod 已提交
1309
      comp_index = comp_count - 1;
B
Behdad Esfahbod 已提交
1310

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

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

B
Behdad Esfahbod 已提交
1321 1322
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1323
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1324 1325 1326 1327 1328
    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 已提交
1329 1330
  }

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

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

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


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

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

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

1390
  inline const Coverage &get_coverage (void) const
1391
  { return this+mark1Coverage; }
1392

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

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

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

B
Behdad Esfahbod 已提交
1408 1409
    unsigned int j = skippy_iter.idx;

B
Behdad Esfahbod 已提交
1410 1411 1412 1413
    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]);
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427

    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. */
        goto good;
    } 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 已提交
1428
    return_trace (false);
B
Behdad Esfahbod 已提交
1429

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

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

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

B
Behdad Esfahbod 已提交
1444 1445
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1446
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1447 1448 1449 1450 1451
    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 已提交
1452 1453
  }

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

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

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


B
Minor  
Behdad Esfahbod 已提交
1496
struct ContextPos : Context {};
B
Behdad Esfahbod 已提交
1497

B
Minor  
Behdad Esfahbod 已提交
1498
struct ChainContextPos : ChainContext {};
B
Behdad Esfahbod 已提交
1499

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


1506

B
Behdad Esfahbod 已提交
1507 1508 1509 1510 1511
/*
 * PosLookup
 */


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

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

1529
  template <typename context_t>
1530
  inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1531
  {
1532
    TRACE_DISPATCH (this, lookup_type);
1533
    if (unlikely (!c->may_dispatch (this, &u.sub_format))) return_trace (c->no_dispatch_return_value ());
1534
    switch (lookup_type) {
B
Behdad Esfahbod 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
    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 ());
1545
    }
1546 1547
  }

1548
  protected:
B
Behdad Esfahbod 已提交
1549
  union {
B
Behdad Esfahbod 已提交
1550
  HBUINT16		sub_format;
B
Behdad Esfahbod 已提交
1551 1552 1553 1554 1555 1556
  SinglePos		single;
  PairPos		pair;
  CursivePos		cursive;
  MarkBasePos		markBase;
  MarkLigPos		markLig;
  MarkMarkPos		markMark;
1557
  ContextPos		context;
B
Behdad Esfahbod 已提交
1558 1559
  ChainContextPos	chainContext;
  ExtensionPos		extension;
B
Behdad Esfahbod 已提交
1560
  } u;
B
Behdad Esfahbod 已提交
1561
  public:
B
Minor  
Behdad Esfahbod 已提交
1562
  DEFINE_SIZE_UNION (2, sub_format);
B
Behdad Esfahbod 已提交
1563 1564 1565
};


B
Behdad Esfahbod 已提交
1566 1567
struct PosLookup : Lookup
{
B
Behdad Esfahbod 已提交
1568 1569 1570 1571
  typedef struct PosLookupSubTable SubTable;

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

B
Behdad Esfahbod 已提交
1573 1574 1575 1576 1577
  inline bool is_reverse (void) const
  {
    return false;
  }

1578
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1579 1580
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1581
    return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1582 1583
  }

1584 1585 1586 1587 1588 1589
  inline bool intersects (const hb_set_t *glyphs) const
  {
    hb_intersects_context_t c (glyphs);
    return dispatch (&c);
  }

B
Behdad Esfahbod 已提交
1590
  inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1591 1592
  {
    TRACE_COLLECT_GLYPHS (this);
B
Behdad Esfahbod 已提交
1593
    return_trace (dispatch (c));
1594 1595
  }

B
Behdad Esfahbod 已提交
1596 1597 1598
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const
  {
1599 1600
    hb_add_coverage_context_t<set_t> c (glyphs);
    dispatch (&c);
B
Behdad Esfahbod 已提交
1601 1602
  }

1603
  static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1604

B
Behdad Esfahbod 已提交
1605 1606 1607 1608 1609
  template <typename context_t>
  static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);

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

1612 1613 1614
  inline bool subset (hb_subset_context_t *c) const
  { return Lookup::subset<SubTable> (c); }

B
Behdad Esfahbod 已提交
1615
  inline bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1616
  { return Lookup::sanitize<SubTable> (c); }
B
Behdad Esfahbod 已提交
1617 1618 1619
};

/*
1620 1621
 * GPOS -- Glyph Positioning
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
B
Behdad Esfahbod 已提交
1622 1623
 */

B
Behdad Esfahbod 已提交
1624 1625
struct GPOS : GSUBGPOS
{
1626
  static const hb_tag_t tableTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
1627

B
Behdad Esfahbod 已提交
1628
  inline const PosLookup& get_lookup (unsigned int i) const
1629
  { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
B
Behdad Esfahbod 已提交
1630

1631
  static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1632 1633
  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 已提交
1634

1635 1636 1637
  inline bool subset (hb_subset_context_t *c) const
  { return GSUBGPOS::subset<PosLookup> (c); }

B
Behdad Esfahbod 已提交
1638
  inline bool sanitize (hb_sanitize_context_t *c) const
1639
  { return GSUBGPOS::sanitize<PosLookup> (c); }
B
WIP  
Behdad Esfahbod 已提交
1640 1641

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

B
Behdad Esfahbod 已提交
1644

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

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

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

1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
  /* 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;

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

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

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

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

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

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

1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
  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;
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

    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;
      }
1716
  }
B
Behdad Esfahbod 已提交
1717 1718
}

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

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

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

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

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

B
Behdad Esfahbod 已提交
1748

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


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

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

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


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

B
Behdad Esfahbod 已提交
1777

1778
#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */