hb-ot-layout-gdef-table.hh 12.6 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009  Red Hat, Inc.
3
 * Copyright © 2010,2011  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 ();
B
Behdad Esfahbod 已提交
74 75
    return coverage.sanitize (c, this)
	&& attachPoint.sanitize (c, this);
B
Behdad Esfahbod 已提交
76 77
  }

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

/*
 * Ligature Caret Table
 */

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

  private:
98
  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 已提交
99
  {
B
Minor  
Behdad Esfahbod 已提交
100
    return HB_DIRECTION_IS_HORIZONTAL (direction) ? font->em_scale_x (coordinate) : font->em_scale_y (coordinate);
B
Behdad Esfahbod 已提交
101
  }
B
Behdad Esfahbod 已提交
102

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

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

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

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

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

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

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

145
  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 已提交
146
  {
147
    return HB_DIRECTION_IS_HORIZONTAL (direction) ?
B
Minor  
Behdad Esfahbod 已提交
148 149
           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 已提交
150 151
  }

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

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

B
Behdad Esfahbod 已提交
169 170
struct CaretValue
{
171
  inline hb_position_t get_caret_value (hb_font_t *font, hb_direction_t direction, hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
172
  {
B
Behdad Esfahbod 已提交
173
    switch (u.format) {
174 175 176
    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 已提交
177 178 179 180
    default:return 0;
    }
  }

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

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

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

B
Behdad Esfahbod 已提交
219
    return carets.len;
B
Behdad Esfahbod 已提交
220
  }
B
Minor  
Behdad Esfahbod 已提交
221

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

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

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

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

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

273 274 275 276 277 278

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 已提交
279
  inline bool sanitize (hb_sanitize_context_t *c) {
280
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
281
    return coverage.sanitize (c, this);
B
Behdad Esfahbod 已提交
282 283
  }

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

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

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

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


322
/*
B
Behdad Esfahbod 已提交
323
 * GDEF -- The Glyph Definition Table
324
 */
B
Behdad Esfahbod 已提交
325

B
Behdad Esfahbod 已提交
326 327
struct GDEF
{
B
Behdad Esfahbod 已提交
328
  static const hb_tag_t Tag	= HB_OT_TAG_GDEF;
329

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

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

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

B
Behdad Esfahbod 已提交
346
  inline bool has_attach_points (void) const { return attachList != 0; }
B
Behdad Esfahbod 已提交
347 348 349 350 351
  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 已提交
352

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

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

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

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

  /* 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);
    }
  }


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

425

B
Behdad Esfahbod 已提交
426

427
#endif /* HB_OT_LAYOUT_GDEF_TABLE_HH */