hb-ot-layout-gpos-table.hh 53.8 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
					 * design units (only used for vertical
					 * writing) */
B
Comment  
Behdad Esfahbod 已提交
87
  OffsetTo<Device>	xPlaDevice;	/* Offset to Device table for
88 89
					 * horizontal placement--measured from
					 * beginning of PosTable (may be NULL) */
B
Comment  
Behdad Esfahbod 已提交
90
  OffsetTo<Device>	yPlaDevice;	/* Offset to Device table for vertical
91 92
					 * placement--measured from beginning
					 * of PosTable (may be NULL) */
B
Comment  
Behdad Esfahbod 已提交
93
  OffsetTo<Device>	xAdvDevice;	/* Offset to Device table for
94 95
					 * horizontal advance--measured from
					 * beginning of PosTable (may be NULL) */
B
Comment  
Behdad Esfahbod 已提交
96
  OffsetTo<Device>	yAdvDevice;	/* Offset to Device table for vertical
97 98 99
					 * 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
B
Behdad Esfahbod 已提交
475
  { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
476

477
  const Coverage &get_coverage () const { return this+coverage; }
478

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

486
    valueFormat.apply_value (c, this, values, buffer->cur_pos());
487

B
Behdad Esfahbod 已提交
488
    buffer->idx++;
B
Behdad Esfahbod 已提交
489
    return_trace (true);
B
Behdad Esfahbod 已提交
490 491
  }

492
  bool subset (hb_subset_context_t *c) const
493 494 495 496 497 498
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

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

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

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

526
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
B
Behdad Esfahbod 已提交
527
  { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
528

529
  const Coverage &get_coverage () const { return this+coverage; }
530

531
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
532
  {
B
Behdad Esfahbod 已提交
533
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
534 535
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
536
    if (likely (index == NOT_COVERED)) return_trace (false);
537

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

540
    valueFormat.apply_value (c, this,
B
Behdad Esfahbod 已提交
541
			     &values[index * valueFormat.get_len ()],
B
Behdad Esfahbod 已提交
542
			     buffer->cur_pos());
543

B
Behdad Esfahbod 已提交
544
    buffer->idx++;
B
Behdad Esfahbod 已提交
545
    return_trace (true);
B
Behdad Esfahbod 已提交
546 547
  }

548
  bool subset (hb_subset_context_t *c) const
549 550 551 552 553 554
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

555
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
556
  {
B
Behdad Esfahbod 已提交
557
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
558 559 560
    return_trace (c->check_struct (this) &&
		  coverage.sanitize (c, this) &&
		  valueFormat.sanitize_values (c, this, values, valueCount));
B
Behdad Esfahbod 已提交
561 562
  }

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

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

591
  protected:
B
Behdad Esfahbod 已提交
592
  union {
B
Behdad Esfahbod 已提交
593
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
594 595
  SinglePosFormat1	format1;
  SinglePosFormat2	format2;
B
Behdad Esfahbod 已提交
596 597 598 599
  } u;
};


B
Behdad Esfahbod 已提交
600 601
struct PairValueRecord
{
B
Behdad Esfahbod 已提交
602
  friend struct PairSet;
B
Behdad Esfahbod 已提交
603

604
  protected:
B
Behdad Esfahbod 已提交
605 606 607 608 609
  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 已提交
610
  public:
611
  DEFINE_SIZE_ARRAY (2, values);
B
Behdad Esfahbod 已提交
612 613
};

B
Behdad Esfahbod 已提交
614 615
struct PairSet
{
B
Behdad Esfahbod 已提交
616 617
  friend struct PairPosFormat1;

618
  bool intersects (const hb_set_t *glyphs,
619 620
			  const ValueFormat *valueFormats) const
  {
B
Behdad Esfahbod 已提交
621 622
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
623 624
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);

625
    const PairValueRecord *record = &firstPairValueRecord;
626 627 628 629
    unsigned int count = len;
    for (unsigned int i = 0; i < count; i++)
    {
      if (glyphs->has (record->secondGlyph))
E
Ebrahim Byagowi 已提交
630
	return true;
631 632 633 634 635
      record = &StructAtOffset<const PairValueRecord> (record, record_size);
    }
    return false;
  }

636
  void collect_glyphs (hb_collect_glyphs_context_t *c,
637 638
			      const ValueFormat *valueFormats) const
  {
B
Behdad Esfahbod 已提交
639 640
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
B
Behdad Esfahbod 已提交
641
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
642

643
    const PairValueRecord *record = &firstPairValueRecord;
644
    c->input->add_array (&record->secondGlyph, len, record_size);
645 646
  }

647
  bool apply (hb_ot_apply_context_t *c,
B
Behdad Esfahbod 已提交
648 649 650
		     const ValueFormat *valueFormats,
		     unsigned int pos) const
  {
B
Behdad Esfahbod 已提交
651
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
652
    hb_buffer_t *buffer = c->buffer;
B
Behdad Esfahbod 已提交
653 654
    unsigned int len1 = valueFormats[0].get_len ();
    unsigned int len2 = valueFormats[1].get_len ();
B
Behdad Esfahbod 已提交
655
    unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
B
Behdad Esfahbod 已提交
656

657
    unsigned int count = len;
658 659 660

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

B
Behdad Esfahbod 已提交
686
    return_trace (false);
B
Behdad Esfahbod 已提交
687 688
  }

689 690
  struct sanitize_closure_t
  {
B
Behdad Esfahbod 已提交
691 692
    const void *base;
    const ValueFormat *valueFormats;
B
Behdad Esfahbod 已提交
693
    unsigned int len1; /* valueFormats[0].get_len() */
B
Behdad Esfahbod 已提交
694 695 696
    unsigned int stride; /* 1 + len1 + len2 */
  };

697
  bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
B
Behdad Esfahbod 已提交
698
  {
B
Behdad Esfahbod 已提交
699
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
700
    if (!(c->check_struct (this)
701 702 703 704
       && c->check_range (&firstPairValueRecord,
			  len,
			  HBUINT16::static_size,
			  closure->stride))) return_trace (false);
B
Behdad Esfahbod 已提交
705 706

    unsigned int count = len;
707
    const PairValueRecord *record = &firstPairValueRecord;
B
Behdad Esfahbod 已提交
708 709
    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 已提交
710 711
  }

712
  protected:
713 714 715 716
  HBUINT16		len;	/* Number of PairValueRecords */
  PairValueRecord	firstPairValueRecord;
				/* Array of PairValueRecords--ordered
				 * by GlyphID of the second glyph */
B
Behdad Esfahbod 已提交
717
  public:
718
  DEFINE_SIZE_MIN (2);
B
Behdad Esfahbod 已提交
719
};
B
Behdad Esfahbod 已提交
720

B
Behdad Esfahbod 已提交
721 722
struct PairPosFormat1
{
723
  bool intersects (const hb_set_t *glyphs) const
724
  {
B
Behdad Esfahbod 已提交
725 726 727 728
    for (auto it = hb_zip (this+coverage, pairSet); it; ++it)
      if (glyphs->has (it->first))
        if ((this+it->second).intersects (glyphs, valueFormat))
	  return true;
729 730 731
    return false;
  }

732
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
733
  {
734
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
735 736
    unsigned int count = pairSet.len;
    for (unsigned int i = 0; i < count; i++)
737
      (this+pairSet[i]).collect_glyphs (c, valueFormat);
738 739
  }

740
  const Coverage &get_coverage () const { return this+coverage; }
741

742
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
743
  {
B
Behdad Esfahbod 已提交
744
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
745 746
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
747
    if (likely (index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
748

749
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
750
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
751
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
752

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

756
  bool subset (hb_subset_context_t *c) const
757 758 759 760 761 762
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

763
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
764
  {
B
Behdad Esfahbod 已提交
765
    TRACE_SANITIZE (this);
766

767 768
    if (!c->check_struct (this)) return_trace (false);

B
Behdad Esfahbod 已提交
769 770
    unsigned int len1 = valueFormat[0].get_len ();
    unsigned int len2 = valueFormat[1].get_len ();
771 772
    PairSet::sanitize_closure_t closure =
    {
B
Behdad Esfahbod 已提交
773
      this,
774
      valueFormat,
B
Behdad Esfahbod 已提交
775 776 777
      len1,
      1 + len1 + len2
    };
778

779
    return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
B
Behdad Esfahbod 已提交
780 781
  }

782
  protected:
B
Behdad Esfahbod 已提交
783
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
784 785 786
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
B
Behdad Esfahbod 已提交
787
  ValueFormat	valueFormat[2];		/* [0] Defines the types of data in
B
Behdad Esfahbod 已提交
788 789
					 * ValueRecord1--for the first glyph
					 * in the pair--may be zero (0) */
B
Behdad Esfahbod 已提交
790
					/* [1] Defines the types of data in
B
Behdad Esfahbod 已提交
791 792 793 794 795
					 * ValueRecord2--for the second glyph
					 * in the pair--may be zero (0) */
  OffsetArrayOf<PairSet>
		pairSet;		/* Array of PairSet tables
					 * ordered by Coverage Index */
796
  public:
797
  DEFINE_SIZE_ARRAY (10, pairSet);
B
Behdad Esfahbod 已提交
798 799
};

B
Behdad Esfahbod 已提交
800 801
struct PairPosFormat2
{
802
  bool intersects (const hb_set_t *glyphs) const
803 804 805 806 807
  {
    return (this+coverage).intersects (glyphs) &&
	   (this+classDef2).intersects (glyphs);
  }

808
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
809
  {
810
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
811
    if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
812 813
  }

814
  const Coverage &get_coverage () const { return this+coverage; }
815

816
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
817
  {
B
Behdad Esfahbod 已提交
818
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
819 820
    hb_buffer_t *buffer = c->buffer;
    unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
821
    if (likely (index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
822

823
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
824
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
825
    if (!skippy_iter.next ()) return_trace (false);
B
Behdad Esfahbod 已提交
826 827 828 829 830

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

B
Behdad Esfahbod 已提交
831 832
    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 已提交
833
    if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
B
Behdad Esfahbod 已提交
834

B
Behdad Esfahbod 已提交
835
    const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
836 837 838 839
    /* 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 已提交
840

B
Behdad Esfahbod 已提交
841
    buffer->idx = skippy_iter.idx;
B
Behdad Esfahbod 已提交
842
    if (len2)
B
Behdad Esfahbod 已提交
843
      buffer->idx++;
B
Behdad Esfahbod 已提交
844

B
Behdad Esfahbod 已提交
845
    return_trace (true);
B
Behdad Esfahbod 已提交
846 847
  }

848
  bool subset (hb_subset_context_t *c) const
849 850 851 852 853 854
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

855
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
856
  {
B
Behdad Esfahbod 已提交
857
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
858 859 860
    if (!(c->check_struct (this)
       && coverage.sanitize (c, this)
       && classDef1.sanitize (c, this)
B
Behdad Esfahbod 已提交
861
       && classDef2.sanitize (c, this))) return_trace (false);
862

863 864 865
    unsigned int len1 = valueFormat1.get_len ();
    unsigned int len2 = valueFormat2.get_len ();
    unsigned int stride = len1 + len2;
B
Behdad Esfahbod 已提交
866
    unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
867
    unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
868 869 870
    return_trace (c->check_range ((const void *) values,
				  count,
				  record_size) &&
B
Behdad Esfahbod 已提交
871
		  valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
B
Behdad Esfahbod 已提交
872
		  valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
B
Behdad Esfahbod 已提交
873
  }
B
Behdad Esfahbod 已提交
874

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

B
Behdad Esfahbod 已提交
905 906
struct PairPos
{
907
  template <typename context_t>
908
  typename context_t::return_t dispatch (context_t *c) const
909
  {
910
    TRACE_DISPATCH (this, u.format);
911
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
912
    switch (u.format) {
B
Behdad Esfahbod 已提交
913 914 915
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    default:return_trace (c->default_return_value ());
916 917 918
    }
  }

919
  protected:
B
Behdad Esfahbod 已提交
920
  union {
B
Behdad Esfahbod 已提交
921
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
922 923
  PairPosFormat1	format1;
  PairPosFormat2	format2;
B
Behdad Esfahbod 已提交
924 925 926 927
  } u;
};


B
Behdad Esfahbod 已提交
928 929
struct EntryExitRecord
{
B
Behdad Esfahbod 已提交
930 931
  friend struct CursivePosFormat1;

932
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
B
Behdad Esfahbod 已提交
933
  {
B
Behdad Esfahbod 已提交
934
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
935
    return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
B
Behdad Esfahbod 已提交
936 937
  }

938
  protected:
B
Behdad Esfahbod 已提交
939 940 941 942 943 944 945 946
  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 已提交
947 948
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
949 950
};

951 952 953
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 已提交
954 955
struct CursivePosFormat1
{
956
  bool intersects (const hb_set_t *glyphs) const
957 958
  { return (this+coverage).intersects (glyphs); }

959
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
B
Behdad Esfahbod 已提交
960
  { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
961

962
  const Coverage &get_coverage () const { return this+coverage; }
963

964
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
965
  {
B
Behdad Esfahbod 已提交
966
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
967
    hb_buffer_t *buffer = c->buffer;
B
Behdad Esfahbod 已提交
968

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

972
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
973
    skippy_iter.reset (buffer->idx, 1);
974
    if (!skippy_iter.prev ()) return_trace (false);
975

976 977
    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 已提交
978

979 980
    unsigned int i = skippy_iter.idx;
    unsigned int j = buffer->idx;
B
Behdad Esfahbod 已提交
981

982
    buffer->unsafe_to_break (i, j);
983
    float entry_x, entry_y, exit_x, exit_y;
984 985
    (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 已提交
986

B
Behdad Esfahbod 已提交
987
    hb_glyph_position_t *pos = buffer->pos;
B
Behdad Esfahbod 已提交
988 989 990 991 992

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

995
	d = round (entry_x) + pos[j].x_offset;
B
Behdad Esfahbod 已提交
996 997 998 999
	pos[j].x_advance -= d;
	pos[j].x_offset  -= d;
	break;
      case HB_DIRECTION_RTL:
1000
	d = round (exit_x) + pos[i].x_offset;
B
Behdad Esfahbod 已提交
1001 1002 1003
	pos[i].x_advance -= d;
	pos[i].x_offset  -= d;

1004
	pos[j].x_advance  = round (entry_x) + pos[j].x_offset;
B
Behdad Esfahbod 已提交
1005 1006
	break;
      case HB_DIRECTION_TTB:
1007
	pos[i].y_advance  = round (exit_y) + pos[i].y_offset;
B
Behdad Esfahbod 已提交
1008

1009
	d = round (entry_y) + pos[j].y_offset;
B
Behdad Esfahbod 已提交
1010 1011 1012 1013
	pos[j].y_advance -= d;
	pos[j].y_offset  -= d;
	break;
      case HB_DIRECTION_BTT:
1014
	d = round (exit_y) + pos[i].y_offset;
B
Behdad Esfahbod 已提交
1015 1016 1017
	pos[i].y_advance -= d;
	pos[i].y_offset  -= d;

1018
	pos[j].y_advance  = round (entry_y);
B
Behdad Esfahbod 已提交
1019 1020 1021 1022
	break;
      case HB_DIRECTION_INVALID:
      default:
	break;
B
Behdad Esfahbod 已提交
1023 1024
    }

B
Behdad Esfahbod 已提交
1025
    /* Cross-direction adjustment */
1026 1027 1028 1029 1030 1031

    /* 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
1032
     * Arabic. */
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    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 已提交
1044 1045
    }

1046 1047 1048 1049 1050 1051 1052
    /* 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);

1053 1054
    pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
    pos[child].attach_chain() = (int) parent - (int) child;
1055
    buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
1056 1057 1058 1059 1060
    if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
      pos[child].y_offset = y_offset;
    else
      pos[child].x_offset = x_offset;

1061
    buffer->idx++;
B
Behdad Esfahbod 已提交
1062
    return_trace (true);
B
Behdad Esfahbod 已提交
1063 1064
  }

1065
  bool subset (hb_subset_context_t *c) const
1066 1067 1068 1069 1070 1071
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1072
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1073
  {
B
Behdad Esfahbod 已提交
1074
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1075
    return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
B
Behdad Esfahbod 已提交
1076 1077
  }

1078
  protected:
B
Behdad Esfahbod 已提交
1079
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1080 1081 1082 1083 1084 1085
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of subtable */
  ArrayOf<EntryExitRecord>
		entryExitRecord;	/* Array of EntryExit records--in
					 * Coverage Index order */
1086
  public:
1087
  DEFINE_SIZE_ARRAY (6, entryExitRecord);
B
Behdad Esfahbod 已提交
1088 1089
};

B
Behdad Esfahbod 已提交
1090 1091
struct CursivePos
{
1092
  template <typename context_t>
1093
  typename context_t::return_t dispatch (context_t *c) const
1094
  {
1095
    TRACE_DISPATCH (this, u.format);
1096
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1097
    switch (u.format) {
B
Behdad Esfahbod 已提交
1098 1099
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1100 1101 1102
    }
  }

1103
  protected:
B
Behdad Esfahbod 已提交
1104
  union {
B
Behdad Esfahbod 已提交
1105
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1106
  CursivePosFormat1	format1;
B
Behdad Esfahbod 已提交
1107 1108 1109 1110
  } u;
};


1111 1112 1113
typedef AnchorMatrix BaseArray;		/* base-major--
					 * in order of BaseCoverage Index--,
					 * mark-minor--
B
Behdad Esfahbod 已提交
1114
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1115

B
Behdad Esfahbod 已提交
1116 1117
struct MarkBasePosFormat1
{
1118
  bool intersects (const hb_set_t *glyphs) const
1119 1120 1121
  { return (this+markCoverage).intersects (glyphs) &&
	   (this+baseCoverage).intersects (glyphs); }

1122
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1123
  {
1124 1125
    if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
    if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
1126 1127
  }

1128
  const Coverage &get_coverage () const { return this+markCoverage; }
1129

1130
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1131
  {
B
Behdad Esfahbod 已提交
1132
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1133 1134
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark_index = (this+markCoverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1135
    if (likely (mark_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1136

B
Behdad Esfahbod 已提交
1137
    /* Now we search backwards for a non-mark glyph */
1138
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1139
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
1140
    skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1141
    do {
B
Behdad Esfahbod 已提交
1142
      if (!skippy_iter.prev ()) return_trace (false);
1143 1144
      /* We only want to attach to the first of a MultipleSubst sequence.
       * https://github.com/harfbuzz/harfbuzz/issues/740
1145 1146 1147
       * Reject others...
       * ...but stop if we find a mark in the MultipleSubst sequence:
       * https://github.com/harfbuzz/harfbuzz/issues/1020 */
B
Behdad Esfahbod 已提交
1148
      if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
1149 1150
	  0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
	  (skippy_iter.idx == 0 ||
1151
	   _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
1152
	   _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
1153 1154 1155 1156
	   _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 已提交
1157
	break;
1158
      skippy_iter.reject ();
1159
    } while (true);
1160

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

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

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

1170
  bool subset (hb_subset_context_t *c) const
1171 1172 1173 1174 1175 1176
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1177
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1178
  {
B
Behdad Esfahbod 已提交
1179
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1180 1181 1182 1183 1184
    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 已提交
1185 1186
  }

1187
  protected:
B
Behdad Esfahbod 已提交
1188
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1189 1190
  OffsetTo<Coverage>
		markCoverage;		/* Offset to MarkCoverage table--from
B
Behdad Esfahbod 已提交
1191
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1192 1193
  OffsetTo<Coverage>
		baseCoverage;		/* Offset to BaseCoverage table--from
B
Behdad Esfahbod 已提交
1194
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1195
  HBUINT16	classCount;		/* Number of classes defined for marks */
B
Behdad Esfahbod 已提交
1196 1197
  OffsetTo<MarkArray>
		markArray;		/* Offset to MarkArray table--from
B
Behdad Esfahbod 已提交
1198
					 * beginning of MarkBasePos subtable */
B
Behdad Esfahbod 已提交
1199 1200
  OffsetTo<BaseArray>
		baseArray;		/* Offset to BaseArray table--from
B
Behdad Esfahbod 已提交
1201
					 * beginning of MarkBasePos subtable */
1202 1203
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1204 1205
};

B
Behdad Esfahbod 已提交
1206 1207
struct MarkBasePos
{
1208
  template <typename context_t>
1209
  typename context_t::return_t dispatch (context_t *c) const
1210
  {
1211
    TRACE_DISPATCH (this, u.format);
1212
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1213
    switch (u.format) {
B
Behdad Esfahbod 已提交
1214 1215
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1216 1217 1218
    }
  }

1219
  protected:
B
Behdad Esfahbod 已提交
1220
  union {
B
Behdad Esfahbod 已提交
1221
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1222
  MarkBasePosFormat1	format1;
B
Behdad Esfahbod 已提交
1223 1224 1225 1226
  } u;
};


1227 1228 1229
typedef AnchorMatrix LigatureAttach;	/* component-major--
					 * in order of writing direction--,
					 * mark-minor--
B
Behdad Esfahbod 已提交
1230
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1231

1232
typedef OffsetListOf<LigatureAttach> LigatureArray;
B
Behdad Esfahbod 已提交
1233
					/* Array of LigatureAttach
B
Behdad Esfahbod 已提交
1234 1235 1236
					 * tables ordered by
					 * LigatureCoverage Index */

B
Behdad Esfahbod 已提交
1237 1238
struct MarkLigPosFormat1
{
1239
  bool intersects (const hb_set_t *glyphs) const
1240 1241 1242
  { return (this+markCoverage).intersects (glyphs) &&
	   (this+ligatureCoverage).intersects (glyphs); }

1243
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1244
  {
1245 1246
    if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
    if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
1247 1248
  }

1249
  const Coverage &get_coverage () const { return this+markCoverage; }
1250

1251
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1252
  {
B
Behdad Esfahbod 已提交
1253
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1254 1255
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark_index = (this+markCoverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1256
    if (likely (mark_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1257

B
Behdad Esfahbod 已提交
1258
    /* Now we search backwards for a non-mark glyph */
1259
    hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
1260
    skippy_iter.reset (buffer->idx, 1);
B
Behdad Esfahbod 已提交
1261
    skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
B
Behdad Esfahbod 已提交
1262
    if (!skippy_iter.prev ()) return_trace (false);
1263

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

B
Behdad Esfahbod 已提交
1267
    unsigned int j = skippy_iter.idx;
B
Behdad Esfahbod 已提交
1268
    unsigned int lig_index = (this+ligatureCoverage).get_coverage  (buffer->info[j].codepoint);
B
Behdad Esfahbod 已提交
1269
    if (lig_index == NOT_COVERED) return_trace (false);
B
Behdad Esfahbod 已提交
1270 1271

    const LigatureArray& lig_array = this+ligatureArray;
1272
    const LigatureAttach& lig_attach = lig_array[lig_index];
1273 1274

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

B
Behdad Esfahbod 已提交
1278 1279 1280 1281
    /* 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 已提交
1282
    unsigned int comp_index;
B
Behdad Esfahbod 已提交
1283 1284 1285
    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 已提交
1286
    if (lig_id && lig_id == mark_id && mark_comp > 0)
B
Behdad Esfahbod 已提交
1287
      comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
B
Behdad Esfahbod 已提交
1288
    else
B
Behdad Esfahbod 已提交
1289
      comp_index = comp_count - 1;
B
Behdad Esfahbod 已提交
1290

B
Behdad Esfahbod 已提交
1291
    return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
B
Behdad Esfahbod 已提交
1292 1293
  }

1294
  bool subset (hb_subset_context_t *c) const
1295 1296 1297 1298 1299 1300
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1301
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1302
  {
B
Behdad Esfahbod 已提交
1303
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1304 1305 1306 1307 1308
    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 已提交
1309 1310
  }

1311
  protected:
B
Behdad Esfahbod 已提交
1312
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1313 1314
  OffsetTo<Coverage>
		markCoverage;		/* Offset to Mark Coverage table--from
B
Behdad Esfahbod 已提交
1315
					 * beginning of MarkLigPos subtable */
B
Behdad Esfahbod 已提交
1316 1317
  OffsetTo<Coverage>
		ligatureCoverage;	/* Offset to Ligature Coverage
B
Behdad Esfahbod 已提交
1318 1319
					 * table--from beginning of MarkLigPos
					 * subtable */
B
Behdad Esfahbod 已提交
1320
  HBUINT16	classCount;		/* Number of defined mark classes */
B
Behdad Esfahbod 已提交
1321 1322
  OffsetTo<MarkArray>
		markArray;		/* Offset to MarkArray table--from
B
Behdad Esfahbod 已提交
1323
					 * beginning of MarkLigPos subtable */
B
Behdad Esfahbod 已提交
1324 1325
  OffsetTo<LigatureArray>
		ligatureArray;		/* Offset to LigatureArray table--from
B
Behdad Esfahbod 已提交
1326
					 * beginning of MarkLigPos subtable */
1327 1328
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1329 1330
};

B
Behdad Esfahbod 已提交
1331 1332
struct MarkLigPos
{
1333
  template <typename context_t>
1334
  typename context_t::return_t dispatch (context_t *c) const
1335
  {
1336
    TRACE_DISPATCH (this, u.format);
1337
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1338
    switch (u.format) {
B
Behdad Esfahbod 已提交
1339 1340
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1341 1342 1343
    }
  }

1344
  protected:
B
Behdad Esfahbod 已提交
1345
  union {
B
Behdad Esfahbod 已提交
1346
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1347
  MarkLigPosFormat1	format1;
B
Behdad Esfahbod 已提交
1348 1349 1350 1351
  } u;
};


1352 1353 1354 1355
typedef AnchorMatrix Mark2Array;	/* mark2-major--
					 * in order of Mark2Coverage Index--,
					 * mark1-minor--
					 * ordered by class--zero-based. */
B
Behdad Esfahbod 已提交
1356

B
Behdad Esfahbod 已提交
1357 1358
struct MarkMarkPosFormat1
{
1359
  bool intersects (const hb_set_t *glyphs) const
1360 1361 1362
  { return (this+mark1Coverage).intersects (glyphs) &&
	   (this+mark2Coverage).intersects (glyphs); }

1363
  void collect_glyphs (hb_collect_glyphs_context_t *c) const
1364
  {
1365 1366
    if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
    if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
1367 1368
  }

1369
  const Coverage &get_coverage () const { return this+mark1Coverage; }
1370

1371
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1372
  {
B
Behdad Esfahbod 已提交
1373
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1374 1375
    hb_buffer_t *buffer = c->buffer;
    unsigned int mark1_index = (this+mark1Coverage).get_coverage  (buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1376
    if (likely (mark1_index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
1377 1378

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

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

B
Behdad Esfahbod 已提交
1386 1387
    unsigned int j = skippy_iter.idx;

B
Behdad Esfahbod 已提交
1388 1389 1390 1391
    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]);
1392 1393 1394 1395 1396

    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 已提交
1397
	goto good;
1398 1399 1400 1401 1402 1403 1404 1405
    } 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 已提交
1406
    return_trace (false);
B
Behdad Esfahbod 已提交
1407

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

B
Behdad Esfahbod 已提交
1412
    return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
B
Behdad Esfahbod 已提交
1413 1414
  }

1415
  bool subset (hb_subset_context_t *c) const
1416 1417 1418 1419 1420 1421
  {
    TRACE_SUBSET (this);
    // TODO(subset)
    return_trace (false);
  }

1422
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1423
  {
B
Behdad Esfahbod 已提交
1424
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1425 1426 1427 1428 1429
    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 已提交
1430 1431
  }

1432
  protected:
B
Behdad Esfahbod 已提交
1433
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
1434 1435
  OffsetTo<Coverage>
		mark1Coverage;		/* Offset to Combining Mark1 Coverage
B
Behdad Esfahbod 已提交
1436 1437
					 * table--from beginning of MarkMarkPos
					 * subtable */
B
Behdad Esfahbod 已提交
1438 1439
  OffsetTo<Coverage>
		mark2Coverage;		/* Offset to Combining Mark2 Coverage
B
Behdad Esfahbod 已提交
1440 1441
					 * table--from beginning of MarkMarkPos
					 * subtable */
B
Behdad Esfahbod 已提交
1442
  HBUINT16	classCount;		/* Number of defined mark classes */
B
Behdad Esfahbod 已提交
1443 1444 1445 1446 1447 1448
  OffsetTo<MarkArray>
		mark1Array;		/* Offset to Mark1Array table--from
					 * beginning of MarkMarkPos subtable */
  OffsetTo<Mark2Array>
		mark2Array;		/* Offset to Mark2Array table--from
					 * beginning of MarkMarkPos subtable */
1449 1450
  public:
  DEFINE_SIZE_STATIC (12);
B
Behdad Esfahbod 已提交
1451 1452
};

B
Behdad Esfahbod 已提交
1453 1454
struct MarkMarkPos
{
1455
  template <typename context_t>
1456
  typename context_t::return_t dispatch (context_t *c) const
1457
  {
1458
    TRACE_DISPATCH (this, u.format);
1459
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1460
    switch (u.format) {
B
Behdad Esfahbod 已提交
1461 1462
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1463 1464 1465
    }
  }

1466
  protected:
B
Behdad Esfahbod 已提交
1467
  union {
B
Behdad Esfahbod 已提交
1468
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1469
  MarkMarkPosFormat1	format1;
B
Behdad Esfahbod 已提交
1470 1471 1472 1473
  } u;
};


B
Minor  
Behdad Esfahbod 已提交
1474
struct ContextPos : Context {};
B
Behdad Esfahbod 已提交
1475

B
Minor  
Behdad Esfahbod 已提交
1476
struct ChainContextPos : ChainContext {};
B
Behdad Esfahbod 已提交
1477

B
Behdad Esfahbod 已提交
1478
struct ExtensionPos : Extension<ExtensionPos>
B
Behdad Esfahbod 已提交
1479
{
B
Behdad Esfahbod 已提交
1480
  typedef struct PosLookupSubTable SubTable;
B
Behdad Esfahbod 已提交
1481 1482 1483
};


1484

B
Behdad Esfahbod 已提交
1485 1486 1487 1488 1489
/*
 * PosLookup
 */


B
Behdad Esfahbod 已提交
1490 1491
struct PosLookupSubTable
{
B
Behdad Esfahbod 已提交
1492
  friend struct Lookup;
B
Behdad Esfahbod 已提交
1493 1494
  friend struct PosLookup;

B
Behdad Esfahbod 已提交
1495
  enum Type {
1496 1497 1498 1499 1500 1501 1502 1503
    Single		= 1,
    Pair		= 2,
    Cursive		= 3,
    MarkBase		= 4,
    MarkLig		= 5,
    MarkMark		= 6,
    Context		= 7,
    ChainContext	= 8,
1504
    Extension		= 9
1505 1506
  };

1507
  template <typename context_t>
1508
  typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1509
  {
1510
    TRACE_DISPATCH (this, lookup_type);
1511
    switch (lookup_type) {
B
Behdad Esfahbod 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    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 ());
1522
    }
1523 1524
  }

1525
  protected:
B
Behdad Esfahbod 已提交
1526
  union {
B
Behdad Esfahbod 已提交
1527 1528 1529 1530 1531 1532
  SinglePos		single;
  PairPos		pair;
  CursivePos		cursive;
  MarkBasePos		markBase;
  MarkLigPos		markLig;
  MarkMarkPos		markMark;
1533
  ContextPos		context;
B
Behdad Esfahbod 已提交
1534 1535
  ChainContextPos	chainContext;
  ExtensionPos		extension;
B
Behdad Esfahbod 已提交
1536
  } u;
B
Behdad Esfahbod 已提交
1537
  public:
B
Behdad Esfahbod 已提交
1538
  DEFINE_SIZE_MIN (0);
B
Behdad Esfahbod 已提交
1539 1540 1541
};


B
Behdad Esfahbod 已提交
1542 1543
struct PosLookup : Lookup
{
B
Behdad Esfahbod 已提交
1544 1545
  typedef struct PosLookupSubTable SubTable;

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

1549
  bool is_reverse () const
B
Behdad Esfahbod 已提交
1550 1551 1552 1553
  {
    return false;
  }

1554
  bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1555 1556
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1557
    return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1558 1559
  }

1560
  bool intersects (const hb_set_t *glyphs) const
1561 1562 1563 1564 1565
  {
    hb_intersects_context_t c (glyphs);
    return dispatch (&c);
  }

1566
  hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
B
Behdad Esfahbod 已提交
1567
  { return dispatch (c); }
1568

B
Behdad Esfahbod 已提交
1569
  template <typename set_t>
1570
  void add_coverage (set_t *glyphs) const
B
Behdad Esfahbod 已提交
1571
  {
1572 1573
    hb_add_coverage_context_t<set_t> c (glyphs);
    dispatch (&c);
B
Behdad Esfahbod 已提交
1574 1575
  }

1576
  static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1577

B
Behdad Esfahbod 已提交
1578
  template <typename context_t>
1579
  static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
1580 1581

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

1585
  bool subset (hb_subset_context_t *c) const
1586 1587
  { return Lookup::subset<SubTable> (c); }

1588
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
1589
  { return Lookup::sanitize<SubTable> (c); }
B
Behdad Esfahbod 已提交
1590 1591 1592
};

/*
1593 1594
 * GPOS -- Glyph Positioning
 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
B
Behdad Esfahbod 已提交
1595 1596
 */

B
Behdad Esfahbod 已提交
1597 1598
struct GPOS : GSUBGPOS
{
1599
  enum { tableTag = HB_OT_TAG_GPOS };
B
Behdad Esfahbod 已提交
1600

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

1604
  static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
1605 1606
  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 已提交
1607

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

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

1614 1615 1616
  HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
				   hb_face_t *face) const;

B
WIP  
Behdad Esfahbod 已提交
1617
  typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
B
Behdad Esfahbod 已提交
1618 1619
};

B
Behdad Esfahbod 已提交
1620

1621 1622 1623
static void
reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
{
1624 1625
  int chain = pos[i].attach_chain(), type = pos[i].attach_type();
  if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
1626 1627
    return;

1628
  pos[i].attach_chain() = 0;
1629

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

1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
  /* 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;

1643 1644
  pos[j].attach_chain() = -chain;
  pos[j].attach_type() = type;
1645
}
B
Behdad Esfahbod 已提交
1646
static void
1647 1648 1649 1650
propagate_attachment_offsets (hb_glyph_position_t *pos,
			      unsigned int len,
			      unsigned int i,
			      hb_direction_t direction)
B
Behdad Esfahbod 已提交
1651
{
1652 1653
  /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
   * offset of glyph they are attached to. */
1654
  int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1655
  if (likely (!chain))
B
Behdad Esfahbod 已提交
1656
    return;
B
Behdad Esfahbod 已提交
1657

1658 1659
  pos[i].attach_chain() = 0;

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

1662 1663
  if (unlikely (j >= len))
    return;
1664

1665
  propagate_attachment_offsets (pos, len, j, direction);
1666

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

1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
  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;
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691

    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;
      }
1692
  }
B
Behdad Esfahbod 已提交
1693 1694
}

B
Behdad Esfahbod 已提交
1695
void
1696
GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
B
Behdad Esfahbod 已提交
1697
{
1698 1699
  unsigned int count = buffer->len;
  for (unsigned int i = 0; i < count; i++)
1700
    buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
B
Behdad Esfahbod 已提交
1701 1702
}

B
Behdad Esfahbod 已提交
1703
void
1704
GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
1705 1706 1707 1708 1709 1710
{
  //_hb_buffer_assert_gsubgpos_vars (buffer);
}

void
GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
B
Behdad Esfahbod 已提交
1711
{
1712 1713
  _hb_buffer_assert_gsubgpos_vars (buffer);

1714 1715
  unsigned int len;
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
B
Behdad Esfahbod 已提交
1716 1717 1718
  hb_direction_t direction = buffer->props.direction;

  /* Handle attachments */
1719 1720
  if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
    for (unsigned int i = 0; i < len; i++)
1721
      propagate_attachment_offsets (pos, len, i, direction);
B
Behdad Esfahbod 已提交
1722 1723
}

B
Behdad Esfahbod 已提交
1724

B
Behdad Esfahbod 已提交
1725 1726 1727
struct GPOS_accelerator_t : GPOS::accelerator_t {};


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

1730
template <typename context_t>
1731
/*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1732
{
B
Behdad Esfahbod 已提交
1733
  const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1734
  return l.dispatch (c);
1735 1736
}

1737
/*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
B
Behdad Esfahbod 已提交
1738
{
B
Behdad Esfahbod 已提交
1739
  const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
1740
  unsigned int saved_lookup_props = c->lookup_props;
1741 1742 1743
  unsigned int saved_lookup_index = c->lookup_index;
  c->set_lookup_index (lookup_index);
  c->set_lookup_props (l.get_props ());
1744
  bool ret = l.dispatch (c);
1745
  c->set_lookup_index (saved_lookup_index);
1746
  c->set_lookup_props (saved_lookup_props);
1747
  return ret;
B
Behdad Esfahbod 已提交
1748 1749 1750
}


B
Behdad Esfahbod 已提交
1751
} /* namespace OT */
1752

B
Behdad Esfahbod 已提交
1753

1754
#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */