hb-ot-layout-gdef-table.hh 13.1 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
namespace OT {

39

40 41 42
/*
 * Attachment List Table
 */
B
Behdad Esfahbod 已提交
43

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

B
Behdad Esfahbod 已提交
47 48
struct AttachList
{
B
Behdad Esfahbod 已提交
49 50 51 52
  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 已提交
53
  {
54
    unsigned int index = (this+coverage).get_coverage (glyph_id);
B
Behdad Esfahbod 已提交
55 56
    if (index == NOT_COVERED)
    {
B
Behdad Esfahbod 已提交
57 58 59
      if (point_count)
	*point_count = 0;
      return 0;
B
Behdad Esfahbod 已提交
60
    }
B
Behdad Esfahbod 已提交
61

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

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

B
Behdad Esfahbod 已提交
71
    return points.len;
B
Behdad Esfahbod 已提交
72
  }
B
Behdad Esfahbod 已提交
73

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

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

/*
 * Ligature Caret Table
 */

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

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

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

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

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

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

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

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

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

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

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

158
  protected:
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) {
B
Behdad Esfahbod 已提交
182
    TRACE_SANITIZE (this);
183
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
184
    switch (u.format) {
185 186 187 188
    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 已提交
189 190 191
    }
  }

192
  protected:
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) {
B
Behdad Esfahbod 已提交
223
    TRACE_SANITIZE (this);
224
    return TRACE_RETURN (carets.sanitize (c, this));
B
Behdad Esfahbod 已提交
225 226
  }

227
  protected:
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
  {
245
    unsigned int index = (this+coverage).get_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) {
B
Behdad Esfahbod 已提交
257
    TRACE_SANITIZE (this);
258
    return TRACE_RETURN (coverage.sanitize (c, this) && ligGlyph.sanitize (c, this));
B
Behdad Esfahbod 已提交
259 260
  }

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

272 273 274 275 276 277

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

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

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

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

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


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

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

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

B
Behdad Esfahbod 已提交
337
  inline bool has_glyph_classes (void) const { return glyphClassDef != 0; }
338
  inline unsigned int get_glyph_class (hb_codepoint_t glyph) const
B
Behdad Esfahbod 已提交
339
  { return (this+glyphClassDef).get_class (glyph); }
340 341
  inline void get_glyphs_in_class (unsigned int klass, hb_set_t *glyphs) const
  { (this+glyphClassDef).add_class (glyphs, klass); }
B
Behdad Esfahbod 已提交
342

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

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

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

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

B
Behdad Esfahbod 已提交
367
  inline bool sanitize (hb_sanitize_context_t *c) {
B
Behdad Esfahbod 已提交
368
    TRACE_SANITIZE (this);
369 370 371 372 373 374 375
    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 已提交
376 377
  }

B
Behdad Esfahbod 已提交
378 379 380 381 382 383 384 385 386 387

  /* 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:
388 389 390 391
    case UnclassifiedGlyph:	return HB_OT_LAYOUT_GLYPH_PROPS_UNCLASSIFIED;
    case BaseGlyph:		return HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH;
    case LigatureGlyph:		return HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
    case ComponentGlyph:	return HB_OT_LAYOUT_GLYPH_PROPS_COMPONENT;
B
Behdad Esfahbod 已提交
392 393
    case MarkGlyph:
	  klass = get_mark_attachment_type (glyph);
394
	  return HB_OT_LAYOUT_GLYPH_PROPS_MARK | (klass << 8);
B
Behdad Esfahbod 已提交
395 396 397 398
    }
  }


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

427

B
Behdad Esfahbod 已提交
428
} /* namespace OT */
429

B
Behdad Esfahbod 已提交
430

431
#endif /* HB_OT_LAYOUT_GDEF_TABLE_HH */