hb-ot-layout-gdef-table.hh 12.8 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009  Red Hat, Inc.
3
 * Copyright © 2010,2011,2012  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_GDEF_TABLE_HH
#define HB_OT_LAYOUT_GDEF_TABLE_HH
B
Behdad Esfahbod 已提交
31

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

34
#include "hb-font-private.hh"
35

B
Behdad Esfahbod 已提交
36

37

38 39 40
/*
 * Attachment List Table
 */
B
Behdad Esfahbod 已提交
41

42
typedef ArrayOf<USHORT> AttachPoint;	/* Array of contour point indices--in
43
					 * increasing numerical order */
B
Behdad Esfahbod 已提交
44

B
Behdad Esfahbod 已提交
45 46
struct AttachList
{
B
Behdad Esfahbod 已提交
47 48 49 50
  inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
					 unsigned int start_offset,
					 unsigned int *point_count /* IN/OUT */,
					 unsigned int *point_array /* OUT */) const
B
Behdad Esfahbod 已提交
51
  {
B
Behdad Esfahbod 已提交
52
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
53 54
    if (index == NOT_COVERED)
    {
B
Behdad Esfahbod 已提交
55 56 57
      if (point_count)
	*point_count = 0;
      return 0;
B
Behdad Esfahbod 已提交
58
    }
B
Behdad Esfahbod 已提交
59

B
Behdad Esfahbod 已提交
60 61
    const AttachPoint &points = this+attachPoint[index];

B
Behdad Esfahbod 已提交
62
    if (point_count) {
63
      const USHORT *array = points.sub_array (start_offset, point_count);
B
Behdad Esfahbod 已提交
64
      unsigned int count = *point_count;
B
Behdad Esfahbod 已提交
65 66 67
      for (unsigned int i = 0; i < count; i++)
	point_array[i] = array[i];
    }
B
Behdad Esfahbod 已提交
68

B
Behdad Esfahbod 已提交
69
    return points.len;
B
Behdad Esfahbod 已提交
70
  }
B
Behdad Esfahbod 已提交
71

B
Behdad Esfahbod 已提交
72
  inline bool sanitize (hb_sanitize_context_t *c) {
73
    TRACE_SANITIZE ();
74
    return TRACE_RETURN (coverage.sanitize (c, this) && attachPoint.sanitize (c, this));
B
Behdad Esfahbod 已提交
75 76
  }

77
  protected:
78 79
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table -- from
B
Behdad Esfahbod 已提交
80
					 * beginning of AttachList table */
B
Behdad Esfahbod 已提交
81 82 83
  OffsetArrayOf<AttachPoint>
		attachPoint;		/* Array of AttachPoint tables
					 * in Coverage Index order */
84
  public:
85
  DEFINE_SIZE_ARRAY (4, attachPoint);
B
Behdad Esfahbod 已提交
86 87 88 89 90 91
};

/*
 * Ligature Caret Table
 */

B
Behdad Esfahbod 已提交
92 93
struct CaretValueFormat1
{
B
Minor  
Behdad Esfahbod 已提交
94 95 96
  friend struct CaretValue;

  private:
97
  inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id HB_UNUSED) const
B
Behdad Esfahbod 已提交
98
  {
B
Minor  
Behdad Esfahbod 已提交
99
    return HB_DIRECTION_IS_HORIZONTAL (direction) ? font->em_scale_x (coordinate) : font->em_scale_y (coordinate);
B
Behdad Esfahbod 已提交
100
  }
B
Behdad Esfahbod 已提交
101

B
Behdad Esfahbod 已提交
102
  inline bool sanitize (hb_sanitize_context_t *c) {
103
    TRACE_SANITIZE ();
104
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
105 106
  }

107
  protected:
B
Behdad Esfahbod 已提交
108 109
  USHORT	caretValueFormat;	/* Format identifier--format = 1 */
  SHORT		coordinate;		/* X or Y value, in design units */
110 111
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
112 113
};

B
Behdad Esfahbod 已提交
114 115
struct CaretValueFormat2
{
B
Minor  
Behdad Esfahbod 已提交
116 117 118
  friend struct CaretValue;

  private:
119
  inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
120
  {
B
Behdad Esfahbod 已提交
121
    hb_position_t x, y;
122
    if (hb_font_get_glyph_contour_point_for_origin (font, glyph_id, caretValuePoint, direction, &x, &y))
123
      return HB_DIRECTION_IS_HORIZONTAL (direction) ? x : y;
B
Behdad Esfahbod 已提交
124 125
    else
      return 0;
B
Behdad Esfahbod 已提交
126
  }
B
Behdad Esfahbod 已提交
127

B
Behdad Esfahbod 已提交
128
  inline bool sanitize (hb_sanitize_context_t *c) {
129
    TRACE_SANITIZE ();
130
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
131 132
  }

133
  protected:
B
Behdad Esfahbod 已提交
134 135
  USHORT	caretValueFormat;	/* Format identifier--format = 2 */
  USHORT	caretValuePoint;	/* Contour point index on glyph */
136 137
  public:
  DEFINE_SIZE_STATIC (4);
B
Behdad Esfahbod 已提交
138 139
};

B
Behdad Esfahbod 已提交
140 141
struct CaretValueFormat3
{
B
Minor  
Behdad Esfahbod 已提交
142 143
  friend struct CaretValue;

144
  inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id HB_UNUSED) const
B
Behdad Esfahbod 已提交
145
  {
146
    return HB_DIRECTION_IS_HORIZONTAL (direction) ?
B
Minor  
Behdad Esfahbod 已提交
147 148
           font->em_scale_x (coordinate) + (this+deviceTable).get_x_delta (font) :
           font->em_scale_y (coordinate) + (this+deviceTable).get_y_delta (font);
B
Behdad Esfahbod 已提交
149 150
  }

B
Behdad Esfahbod 已提交
151
  inline bool sanitize (hb_sanitize_context_t *c) {
152
    TRACE_SANITIZE ();
153
    return TRACE_RETURN (c->check_struct (this) && deviceTable.sanitize (c, this));
B
Behdad Esfahbod 已提交
154 155
  }

156
  protected:
B
Behdad Esfahbod 已提交
157 158
  USHORT	caretValueFormat;	/* Format identifier--format = 3 */
  SHORT		coordinate;		/* X or Y value, in design units */
B
Behdad Esfahbod 已提交
159 160
  OffsetTo<Device>
		deviceTable;		/* Offset to Device table for X or Y
B
Behdad Esfahbod 已提交
161 162
					 * value--from beginning of CaretValue
					 * table */
163 164
  public:
  DEFINE_SIZE_STATIC (6);
B
Behdad Esfahbod 已提交
165 166
};

B
Behdad Esfahbod 已提交
167 168
struct CaretValue
{
169
  inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
170
  {
B
Behdad Esfahbod 已提交
171
    switch (u.format) {
172 173 174
    case 1: return u.format1.get_caret_value (font, direction, glyph_id);
    case 2: return u.format2.get_caret_value (font, direction, glyph_id);
    case 3: return u.format3.get_caret_value (font, direction, glyph_id);
B
Behdad Esfahbod 已提交
175 176 177 178
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
179
  inline bool sanitize (hb_sanitize_context_t *c) {
180
    TRACE_SANITIZE ();
181
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
182
    switch (u.format) {
183 184 185 186
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    case 2: return TRACE_RETURN (u.format2.sanitize (c));
    case 3: return TRACE_RETURN (u.format3.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
187 188 189
    }
  }

190
  protected:
B
Behdad Esfahbod 已提交
191
  union {
B
Behdad Esfahbod 已提交
192
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
193 194 195
  CaretValueFormat1	format1;
  CaretValueFormat2	format2;
  CaretValueFormat3	format3;
B
Behdad Esfahbod 已提交
196
  } u;
B
Behdad Esfahbod 已提交
197
  public:
B
Behdad Esfahbod 已提交
198
  DEFINE_SIZE_UNION (2, format);
B
Behdad Esfahbod 已提交
199
};
200

B
Behdad Esfahbod 已提交
201 202
struct LigGlyph
{
203
  inline unsigned int get_lig_carets (hb_font_t *font,
204
				      hb_direction_t direction,
B
Behdad Esfahbod 已提交
205 206 207
				      hb_codepoint_t glyph_id,
				      unsigned int start_offset,
				      unsigned int *caret_count /* IN/OUT */,
208
				      hb_position_t *caret_array /* OUT */) const
B
Behdad Esfahbod 已提交
209
  {
B
Behdad Esfahbod 已提交
210
    if (caret_count) {
211
      const OffsetTo<CaretValue> *array = carets.sub_array (start_offset, caret_count);
B
Behdad Esfahbod 已提交
212
      unsigned int count = *caret_count;
B
Behdad Esfahbod 已提交
213
      for (unsigned int i = 0; i < count; i++)
214
	caret_array[i] = (this+array[i]).get_caret_value (font, direction, glyph_id);
B
Behdad Esfahbod 已提交
215
    }
B
Behdad Esfahbod 已提交
216

B
Behdad Esfahbod 已提交
217
    return carets.len;
B
Behdad Esfahbod 已提交
218
  }
B
Minor  
Behdad Esfahbod 已提交
219

B
Behdad Esfahbod 已提交
220
  inline bool sanitize (hb_sanitize_context_t *c) {
221
    TRACE_SANITIZE ();
222
    return TRACE_RETURN (carets.sanitize (c, this));
B
Behdad Esfahbod 已提交
223 224
  }

225
  protected:
B
Behdad Esfahbod 已提交
226
  OffsetArrayOf<CaretValue>
227
		carets;			/* Offset array of CaretValue tables
228 229
					 * --from beginning of LigGlyph table
					 * --in increasing coordinate order */
230
  public:
231
  DEFINE_SIZE_ARRAY (2, carets);
232
};
B
Behdad Esfahbod 已提交
233

B
Behdad Esfahbod 已提交
234 235
struct LigCaretList
{
236
  inline unsigned int get_lig_carets (hb_font_t *font,
237
				      hb_direction_t direction,
B
Behdad Esfahbod 已提交
238 239 240
				      hb_codepoint_t glyph_id,
				      unsigned int start_offset,
				      unsigned int *caret_count /* IN/OUT */,
241
				      hb_position_t *caret_array /* OUT */) const
B
Behdad Esfahbod 已提交
242
  {
B
Behdad Esfahbod 已提交
243
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
244 245
    if (index == NOT_COVERED)
    {
B
Behdad Esfahbod 已提交
246 247 248
      if (caret_count)
	*caret_count = 0;
      return 0;
B
Behdad Esfahbod 已提交
249 250
    }
    const LigGlyph &lig_glyph = this+ligGlyph[index];
251
    return lig_glyph.get_lig_carets (font, direction, glyph_id, start_offset, caret_count, caret_array);
B
Behdad Esfahbod 已提交
252
  }
253

B
Behdad Esfahbod 已提交
254
  inline bool sanitize (hb_sanitize_context_t *c) {
255
    TRACE_SANITIZE ();
256
    return TRACE_RETURN (coverage.sanitize (c, this) && ligGlyph.sanitize (c, this));
B
Behdad Esfahbod 已提交
257 258
  }

259
  protected:
260 261
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
262
					 * beginning of LigCaretList table */
B
Behdad Esfahbod 已提交
263 264 265
  OffsetArrayOf<LigGlyph>
		ligGlyph;		/* Array of LigGlyph tables
					 * in Coverage Index order */
266
  public:
267
  DEFINE_SIZE_ARRAY (4, ligGlyph);
268 269
};

270 271 272 273 274 275

struct MarkGlyphSetsFormat1
{
  inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
  { return (this+coverage[set_index]).get_coverage (glyph_id) != NOT_COVERED; }

B
Behdad Esfahbod 已提交
276
  inline bool sanitize (hb_sanitize_context_t *c) {
277
    TRACE_SANITIZE ();
278
    return TRACE_RETURN (coverage.sanitize (c, this));
B
Behdad Esfahbod 已提交
279 280
  }

281
  protected:
282 283 284 285
  USHORT	format;			/* Format identifier--format = 1 */
  LongOffsetArrayOf<Coverage>
		coverage;		/* Array of long offsets to mark set
					 * coverage tables */
286
  public:
287
  DEFINE_SIZE_ARRAY (4, coverage);
288 289 290 291 292 293 294
};

struct MarkGlyphSets
{
  inline bool covers (unsigned int set_index, hb_codepoint_t glyph_id) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
295
    case 1: return u.format1.covers (set_index, glyph_id);
296 297 298 299
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
300
  inline bool sanitize (hb_sanitize_context_t *c) {
301
    TRACE_SANITIZE ();
302
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
303
    switch (u.format) {
304 305
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
306 307 308
    }
  }

309
  protected:
310 311
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
312
  MarkGlyphSetsFormat1	format1;
313
  } u;
B
Behdad Esfahbod 已提交
314
  public:
B
Behdad Esfahbod 已提交
315
  DEFINE_SIZE_UNION (2, format);
316 317 318
};


319
/*
B
Behdad Esfahbod 已提交
320
 * GDEF -- The Glyph Definition Table
321
 */
B
Behdad Esfahbod 已提交
322

B
Behdad Esfahbod 已提交
323 324
struct GDEF
{
B
Behdad Esfahbod 已提交
325
  static const hb_tag_t Tag	= HB_OT_TAG_GDEF;
326

B
Behdad Esfahbod 已提交
327
  enum GlyphClasses {
B
Behdad Esfahbod 已提交
328 329 330 331
    UnclassifiedGlyph	= 0,
    BaseGlyph		= 1,
    LigatureGlyph	= 2,
    MarkGlyph		= 3,
332
    ComponentGlyph	= 4
B
Behdad Esfahbod 已提交
333
  };
B
Behdad Esfahbod 已提交
334

B
Behdad Esfahbod 已提交
335
  inline bool has_glyph_classes (void) const { return glyphClassDef != 0; }
336
  inline unsigned int get_glyph_class (hb_codepoint_t glyph) const
B
Behdad Esfahbod 已提交
337
  { return (this+glyphClassDef).get_class (glyph); }
B
Behdad Esfahbod 已提交
338

B
Behdad Esfahbod 已提交
339
  inline bool has_mark_attachment_types (void) const { return markAttachClassDef != 0; }
340
  inline unsigned int get_mark_attachment_type (hb_codepoint_t glyph) const
B
Behdad Esfahbod 已提交
341 342
  { return (this+markAttachClassDef).get_class (glyph); }

B
Behdad Esfahbod 已提交
343
  inline bool has_attach_points (void) const { return attachList != 0; }
B
Behdad Esfahbod 已提交
344 345 346 347 348
  inline unsigned int get_attach_points (hb_codepoint_t glyph_id,
					 unsigned int start_offset,
					 unsigned int *point_count /* IN/OUT */,
					 unsigned int *point_array /* OUT */) const
  { return (this+attachList).get_attach_points (glyph_id, start_offset, point_count, point_array); }
B
Behdad Esfahbod 已提交
349

B
Behdad Esfahbod 已提交
350
  inline bool has_lig_carets (void) const { return ligCaretList != 0; }
351
  inline unsigned int get_lig_carets (hb_font_t *font,
352
				      hb_direction_t direction,
B
Behdad Esfahbod 已提交
353 354 355
				      hb_codepoint_t glyph_id,
				      unsigned int start_offset,
				      unsigned int *caret_count /* IN/OUT */,
356
				      hb_position_t *caret_array /* OUT */) const
357
  { return (this+ligCaretList).get_lig_carets (font, direction, glyph_id, start_offset, caret_count, caret_array); }
B
Behdad Esfahbod 已提交
358

B
Behdad Esfahbod 已提交
359
  inline bool has_mark_sets (void) const { return version.to_int () >= 0x00010002 && markGlyphSetsDef[0] != 0; }
360
  inline bool mark_set_covers (unsigned int set_index, hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
361
  { return version.to_int () >= 0x00010002 && (this+markGlyphSetsDef[0]).covers (set_index, glyph_id); }
362

B
Behdad Esfahbod 已提交
363
  inline bool sanitize (hb_sanitize_context_t *c) {
364
    TRACE_SANITIZE ();
365 366 367 368 369 370 371
    return TRACE_RETURN (version.sanitize (c) &&
			 likely (version.major == 1) &&
			 glyphClassDef.sanitize (c, this) &&
			 attachList.sanitize (c, this) &&
			 ligCaretList.sanitize (c, this) &&
			 markAttachClassDef.sanitize (c, this) &&
			 (version.to_int () < 0x00010002 || markGlyphSetsDef[0].sanitize (c, this)));
B
Behdad Esfahbod 已提交
372 373
  }

B
Behdad Esfahbod 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

  /* glyph_props is a 16-bit integer where the lower 8-bit have bits representing
   * glyph class and other bits, and high 8-bit gthe mark attachment type (if any).
   * Not to be confused with lookup_props which is very similar. */
  inline unsigned int get_glyph_props (hb_codepoint_t glyph) const
  {
    unsigned int klass = get_glyph_class (glyph);

    switch (klass) {
    default:
    case UnclassifiedGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
    case BaseGlyph:		return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
    case LigatureGlyph:		return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
    case ComponentGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
    case MarkGlyph:
	  klass = get_mark_attachment_type (glyph);
	  return HB_OT_LAYOUT_GLYPH_CLASS_MARK | (klass << 8);
    }
  }


395
  protected:
396 397
  FixedVersion	version;		/* Version of the GDEF table--currently
					 * 0x00010002 */
B
Behdad Esfahbod 已提交
398 399
  OffsetTo<ClassDef>
		glyphClassDef;		/* Offset to class definition table
B
Behdad Esfahbod 已提交
400
					 * for glyph type--from beginning of
401
					 * GDEF header (may be Null) */
B
Behdad Esfahbod 已提交
402 403
  OffsetTo<AttachList>
		attachList;		/* Offset to list of glyphs with
B
Behdad Esfahbod 已提交
404
					 * attachment points--from beginning
405
					 * of GDEF header (may be Null) */
B
Behdad Esfahbod 已提交
406 407
  OffsetTo<LigCaretList>
		ligCaretList;		/* Offset to list of positioning points
B
Behdad Esfahbod 已提交
408
					 * for ligature carets--from beginning
409
					 * of GDEF header (may be Null) */
B
Behdad Esfahbod 已提交
410 411
  OffsetTo<ClassDef>
		markAttachClassDef;	/* Offset to class definition table for
B
Behdad Esfahbod 已提交
412
					 * mark attachment type--from beginning
413
					 * of GDEF header (may be Null) */
414
  OffsetTo<MarkGlyphSets>
B
Behdad Esfahbod 已提交
415
		markGlyphSetsDef[VAR];	/* Offset to the table of mark set
416 417 418
					 * definitions--from beginning of GDEF
					 * header (may be NULL).  Introduced
					 * in version 00010002. */
B
Behdad Esfahbod 已提交
419
  public:
420
  DEFINE_SIZE_ARRAY (12, markGlyphSetsDef);
B
Behdad Esfahbod 已提交
421 422
};

423

B
Behdad Esfahbod 已提交
424

425
#endif /* HB_OT_LAYOUT_GDEF_TABLE_HH */