2 * Copyright © 2016 Elie Roux <elie.roux@telecom-bretagne.eu>
3 * Copyright © 2018 Google, Inc.
4 * Copyright © 2018-2019 Ebrahim Byagowi
6 * This is part of HarfBuzz, a text shaping library.
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_BASE_TABLE_HH
30 #define HB_OT_LAYOUT_BASE_TABLE_HH
32 #include "hb-open-type.hh"
33 #include "hb-ot-layout-common.hh"
39 * https://docs.microsoft.com/en-us/typography/opentype/spec/base
42 struct BaseCoordFormat1
44 hb_position_t get_coord () const { return coordinate; }
46 bool sanitize (hb_sanitize_context_t *c) const
48 TRACE_SANITIZE (this);
49 return_trace (likely (c->check_struct (this)));
53 HBUINT16 format; /* Format identifier--format = 1 */
54 FWORD coordinate; /* X or Y value, in design units */
56 DEFINE_SIZE_STATIC (4);
59 struct BaseCoordFormat2
61 hb_position_t get_coord () const
67 bool sanitize (hb_sanitize_context_t *c) const
69 TRACE_SANITIZE (this);
70 return_trace (c->check_struct (this));
74 HBUINT16 format; /* Format identifier--format = 2 */
75 FWORD coordinate; /* X or Y value, in design units */
76 HBGlyphID16 referenceGlyph; /* Glyph ID of control glyph */
77 HBUINT16 coordPoint; /* Index of contour point on the
80 DEFINE_SIZE_STATIC (8);
83 struct BaseCoordFormat3
85 hb_position_t get_coord (hb_font_t *font,
86 const VariationStore &var_store,
87 hb_direction_t direction) const
89 const Device &device = this+deviceTable;
90 return coordinate + (HB_DIRECTION_IS_VERTICAL (direction) ?
91 device.get_y_delta (font, var_store) :
92 device.get_x_delta (font, var_store));
96 bool sanitize (hb_sanitize_context_t *c) const
98 TRACE_SANITIZE (this);
99 return_trace (likely (c->check_struct (this) &&
100 deviceTable.sanitize (c, this)));
104 HBUINT16 format; /* Format identifier--format = 3 */
105 FWORD coordinate; /* X or Y value, in design units */
107 deviceTable; /* Offset to Device table for X or
108 * Y value, from beginning of
109 * BaseCoord table (may be NULL). */
111 DEFINE_SIZE_STATIC (6);
116 bool has_data () const { return u.format; }
118 hb_position_t get_coord (hb_font_t *font,
119 const VariationStore &var_store,
120 hb_direction_t direction) const
123 case 1: return u.format1.get_coord ();
124 case 2: return u.format2.get_coord ();
125 case 3: return u.format3.get_coord (font, var_store, direction);
130 bool sanitize (hb_sanitize_context_t *c) const
132 TRACE_SANITIZE (this);
133 if (unlikely (!u.format.sanitize (c))) return_trace (false);
135 case 1: return_trace (u.format1.sanitize (c));
136 case 2: return_trace (u.format2.sanitize (c));
137 case 3: return_trace (u.format3.sanitize (c));
138 default:return_trace (false);
145 BaseCoordFormat1 format1;
146 BaseCoordFormat2 format2;
147 BaseCoordFormat3 format3;
150 DEFINE_SIZE_UNION (2, format);
153 struct FeatMinMaxRecord
155 int cmp (hb_tag_t key) const { return tag.cmp (key); }
157 bool has_data () const { return tag; }
159 void get_min_max (const BaseCoord **min, const BaseCoord **max) const
161 if (likely (min)) *min = &(this+minCoord);
162 if (likely (max)) *max = &(this+maxCoord);
165 bool sanitize (hb_sanitize_context_t *c, const void *base) const
167 TRACE_SANITIZE (this);
168 return_trace (likely (c->check_struct (this) &&
169 minCoord.sanitize (c, this) &&
170 maxCoord.sanitize (c, this)));
174 Tag tag; /* 4-byte feature identification tag--must
175 * match feature tag in FeatureList */
176 Offset16To<BaseCoord>
177 minCoord; /* Offset to BaseCoord table that defines
178 * the minimum extent value, from beginning
179 * of MinMax table (may be NULL) */
180 Offset16To<BaseCoord>
181 maxCoord; /* Offset to BaseCoord table that defines
182 * the maximum extent value, from beginning
183 * of MinMax table (may be NULL) */
185 DEFINE_SIZE_STATIC (8);
191 void get_min_max (hb_tag_t feature_tag,
192 const BaseCoord **min,
193 const BaseCoord **max) const
195 const FeatMinMaxRecord &minMaxCoord = featMinMaxRecords.bsearch (feature_tag);
196 if (minMaxCoord.has_data ())
197 minMaxCoord.get_min_max (min, max);
200 if (likely (min)) *min = &(this+minCoord);
201 if (likely (max)) *max = &(this+maxCoord);
205 bool sanitize (hb_sanitize_context_t *c) const
207 TRACE_SANITIZE (this);
208 return_trace (likely (c->check_struct (this) &&
209 minCoord.sanitize (c, this) &&
210 maxCoord.sanitize (c, this) &&
211 featMinMaxRecords.sanitize (c, this)));
215 Offset16To<BaseCoord>
216 minCoord; /* Offset to BaseCoord table that defines
217 * minimum extent value, from the beginning
218 * of MinMax table (may be NULL) */
219 Offset16To<BaseCoord>
220 maxCoord; /* Offset to BaseCoord table that defines
221 * maximum extent value, from the beginning
222 * of MinMax table (may be NULL) */
223 SortedArray16Of<FeatMinMaxRecord>
225 /* Array of FeatMinMaxRecords, in alphabetical
226 * order by featureTableTag */
228 DEFINE_SIZE_ARRAY (6, featMinMaxRecords);
233 const BaseCoord &get_base_coord (int baseline_tag_index) const
235 if (baseline_tag_index == -1) baseline_tag_index = defaultIndex;
236 return this+baseCoords[baseline_tag_index];
239 bool sanitize (hb_sanitize_context_t *c) const
241 TRACE_SANITIZE (this);
242 return_trace (likely (c->check_struct (this) &&
243 baseCoords.sanitize (c, this)));
247 Index defaultIndex; /* Index number of default baseline for this
248 * script — equals index position of baseline tag
249 * in baselineTags array of the BaseTagList */
250 Array16OfOffset16To<BaseCoord>
251 baseCoords; /* Number of BaseCoord tables defined — should equal
252 * baseTagCount in the BaseTagList
254 * Array of offsets to BaseCoord tables, from beginning of
255 * BaseValues table — order matches baselineTags array in
258 DEFINE_SIZE_ARRAY (4, baseCoords);
261 struct BaseLangSysRecord
263 int cmp (hb_tag_t key) const { return baseLangSysTag.cmp (key); }
265 bool has_data () const { return baseLangSysTag; }
267 const MinMax &get_min_max () const { return this+minMax; }
269 bool sanitize (hb_sanitize_context_t *c, const void *base) const
271 TRACE_SANITIZE (this);
272 return_trace (likely (c->check_struct (this) &&
273 minMax.sanitize (c, this)));
277 Tag baseLangSysTag; /* 4-byte language system identification tag */
279 minMax; /* Offset to MinMax table, from beginning
280 * of BaseScript table */
282 DEFINE_SIZE_STATIC (6);
287 const MinMax &get_min_max (hb_tag_t language_tag) const
289 const BaseLangSysRecord& record = baseLangSysRecords.bsearch (language_tag);
290 return record.has_data () ? record.get_min_max () : this+defaultMinMax;
293 const BaseCoord &get_base_coord (int baseline_tag_index) const
294 { return (this+baseValues).get_base_coord (baseline_tag_index); }
296 bool has_data () const { return baseValues; }
298 bool sanitize (hb_sanitize_context_t *c) const
300 TRACE_SANITIZE (this);
301 return_trace (likely (c->check_struct (this) &&
302 baseValues.sanitize (c, this) &&
303 defaultMinMax.sanitize (c, this) &&
304 baseLangSysRecords.sanitize (c, this)));
308 Offset16To<BaseValues>
309 baseValues; /* Offset to BaseValues table, from beginning
310 * of BaseScript table (may be NULL) */
312 defaultMinMax; /* Offset to MinMax table, from beginning of
313 * BaseScript table (may be NULL) */
314 SortedArray16Of<BaseLangSysRecord>
316 /* Number of BaseLangSysRecords
317 * defined — may be zero (0) */
320 DEFINE_SIZE_ARRAY (6, baseLangSysRecords);
323 struct BaseScriptList;
324 struct BaseScriptRecord
326 int cmp (hb_tag_t key) const { return baseScriptTag.cmp (key); }
328 bool has_data () const { return baseScriptTag; }
330 const BaseScript &get_base_script (const BaseScriptList *list) const
331 { return list+baseScript; }
333 bool sanitize (hb_sanitize_context_t *c, const void *base) const
335 TRACE_SANITIZE (this);
336 return_trace (likely (c->check_struct (this) &&
337 baseScript.sanitize (c, base)));
341 Tag baseScriptTag; /* 4-byte script identification tag */
342 Offset16To<BaseScript>
343 baseScript; /* Offset to BaseScript table, from beginning
344 * of BaseScriptList */
347 DEFINE_SIZE_STATIC (6);
350 struct BaseScriptList
352 const BaseScript &get_base_script (hb_tag_t script) const
354 const BaseScriptRecord *record = &baseScriptRecords.bsearch (script);
355 if (!record->has_data ()) record = &baseScriptRecords.bsearch (HB_TAG ('D','F','L','T'));
356 return record->has_data () ? record->get_base_script (this) : Null (BaseScript);
359 bool sanitize (hb_sanitize_context_t *c) const
361 TRACE_SANITIZE (this);
362 return_trace (c->check_struct (this) &&
363 baseScriptRecords.sanitize (c, this));
367 SortedArray16Of<BaseScriptRecord>
371 DEFINE_SIZE_ARRAY (2, baseScriptRecords);
376 bool get_baseline (hb_tag_t baseline_tag,
378 hb_tag_t language_tag,
379 const BaseCoord **coord) const
381 const BaseScript &base_script = (this+baseScriptList).get_base_script (script_tag);
382 if (!base_script.has_data ())
390 unsigned int tag_index = 0;
391 if (!(this+baseTagList).bfind (baseline_tag, &tag_index))
396 *coord = &base_script.get_base_coord (tag_index);
402 bool get_min_max (hb_tag_t script_tag,
403 hb_tag_t language_tag,
404 hb_tag_t feature_tag,
405 const BaseCoord **min_coord,
406 const BaseCoord **max_coord) const
408 const BaseScript &base_script = (this+baseScriptList).get_base_script (script_tag);
409 if (!base_script.has_data ())
411 *min_coord = *max_coord = nullptr;
415 base_script.get_min_max (language_tag).get_min_max (feature_tag, min_coord, max_coord);
420 bool sanitize (hb_sanitize_context_t *c) const
422 TRACE_SANITIZE (this);
423 return_trace (likely (c->check_struct (this) &&
424 (this+baseTagList).sanitize (c) &&
425 (this+baseScriptList).sanitize (c)));
429 Offset16To<SortedArray16Of<Tag>>
430 baseTagList; /* Offset to BaseTagList table, from beginning
431 * of Axis table (may be NULL)
432 * Array of 4-byte baseline identification tags — must
433 * be in alphabetical order */
434 Offset16To<BaseScriptList>
435 baseScriptList; /* Offset to BaseScriptList table, from beginning
437 * Array of BaseScriptRecords, in alphabetical order
438 * by baseScriptTag */
441 DEFINE_SIZE_STATIC (4);
446 static constexpr hb_tag_t tableTag = HB_OT_TAG_BASE;
448 const Axis &get_axis (hb_direction_t direction) const
449 { return HB_DIRECTION_IS_VERTICAL (direction) ? this+vAxis : this+hAxis; }
451 const VariationStore &get_var_store () const
452 { return version.to_int () < 0x00010001u ? Null (VariationStore) : this+varStore; }
454 bool get_baseline (hb_font_t *font,
455 hb_tag_t baseline_tag,
456 hb_direction_t direction,
458 hb_tag_t language_tag,
459 hb_position_t *base) const
461 const BaseCoord *base_coord = nullptr;
462 if (unlikely (!get_axis (direction).get_baseline (baseline_tag, script_tag, language_tag, &base_coord) ||
463 !base_coord || !base_coord->has_data ()))
467 *base = base_coord->get_coord (font, get_var_store (), direction);
472 /* TODO: Expose this separately sometime? */
473 bool get_min_max (hb_font_t *font,
474 hb_direction_t direction,
476 hb_tag_t language_tag,
477 hb_tag_t feature_tag,
481 const BaseCoord *min_coord, *max_coord;
482 if (!get_axis (direction).get_min_max (script_tag, language_tag, feature_tag,
483 &min_coord, &max_coord))
486 const VariationStore &var_store = get_var_store ();
487 if (likely (min && min_coord)) *min = min_coord->get_coord (font, var_store, direction);
488 if (likely (max && max_coord)) *max = max_coord->get_coord (font, var_store, direction);
492 bool sanitize (hb_sanitize_context_t *c) const
494 TRACE_SANITIZE (this);
495 return_trace (likely (c->check_struct (this) &&
496 likely (version.major == 1) &&
497 hAxis.sanitize (c, this) &&
498 vAxis.sanitize (c, this) &&
499 (version.to_int () < 0x00010001u || varStore.sanitize (c, this))));
503 FixedVersion<>version; /* Version of the BASE table */
504 Offset16To<Axis>hAxis; /* Offset to horizontal Axis table, from beginning
505 * of BASE table (may be NULL) */
506 Offset16To<Axis>vAxis; /* Offset to vertical Axis table, from beginning
507 * of BASE table (may be NULL) */
508 Offset32To<VariationStore>
509 varStore; /* Offset to the table of Item Variation
510 * Store--from beginning of BASE
511 * header (may be NULL). Introduced
512 * in version 0x00010001. */
521 #endif /* HB_OT_LAYOUT_BASE_TABLE_HH */