2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2010,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_COMMON_HH
30 #define HB_OT_LAYOUT_COMMON_HH
33 #include "hb-ot-layout.hh"
34 #include "hb-open-type.hh"
38 #ifndef HB_MAX_NESTING_LEVEL
39 #define HB_MAX_NESTING_LEVEL 6
41 #ifndef HB_MAX_CONTEXT_LENGTH
42 #define HB_MAX_CONTEXT_LENGTH 64
44 #ifndef HB_CLOSURE_MAX_STAGES
46 * The maximum number of times a lookup can be applied during shaping.
47 * Used to limit the number of iterations of the closure algorithm.
48 * This must be larger than the number of times add_pause() is
49 * called in a collect_features call of any shaper.
51 #define HB_CLOSURE_MAX_STAGES 32
54 #ifndef HB_MAX_SCRIPTS
55 #define HB_MAX_SCRIPTS 500
58 #ifndef HB_MAX_LANGSYS
59 #define HB_MAX_LANGSYS 2000
66 #define NOT_COVERED ((unsigned int) -1)
72 * OpenType Layout Common Table Formats
78 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
81 struct Record_sanitize_closure_t {
83 const void *list_base;
86 template <typename Type>
89 int cmp (hb_tag_t a) const { return tag.cmp (a); }
91 bool sanitize (hb_sanitize_context_t *c, const void *base) const
93 TRACE_SANITIZE (this);
94 const Record_sanitize_closure_t closure = {tag, base};
95 return_trace (c->check_struct (this) && offset.sanitize (c, base, &closure));
98 Tag tag; /* 4-byte Tag identifier */
100 offset; /* Offset from beginning of object holding
103 DEFINE_SIZE_STATIC (6);
106 template <typename Type>
107 struct RecordArrayOf : SortedArrayOf<Record<Type> >
109 const OffsetTo<Type>& get_offset (unsigned int i) const
110 { return (*this)[i].offset; }
111 OffsetTo<Type>& get_offset (unsigned int i)
112 { return (*this)[i].offset; }
113 const Tag& get_tag (unsigned int i) const
114 { return (*this)[i].tag; }
115 unsigned int get_tags (unsigned int start_offset,
116 unsigned int *record_count /* IN/OUT */,
117 hb_tag_t *record_tags /* OUT */) const
120 const Record<Type> *arr = this->sub_array (start_offset, record_count);
121 unsigned int count = *record_count;
122 for (unsigned int i = 0; i < count; i++)
123 record_tags[i] = arr[i].tag;
127 bool find_index (hb_tag_t tag, unsigned int *index) const
129 return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX);
133 template <typename Type>
134 struct RecordListOf : RecordArrayOf<Type>
136 const Type& operator [] (unsigned int i) const
137 { return this+this->get_offset (i); }
139 bool subset (hb_subset_context_t *c) const
142 struct RecordListOf<Type> *out = c->serializer->embed (*this);
143 if (unlikely (!out)) return_trace (false);
144 unsigned int count = this->len;
145 for (unsigned int i = 0; i < count; i++)
146 out->get_offset (i).serialize_subset (c, (*this)[i], out);
150 bool sanitize (hb_sanitize_context_t *c) const
152 TRACE_SANITIZE (this);
153 return_trace (RecordArrayOf<Type>::sanitize (c, this));
160 int cmp (hb_codepoint_t g) const
161 { return g < start ? -1 : g <= end ? 0 : +1; }
163 bool sanitize (hb_sanitize_context_t *c) const
165 TRACE_SANITIZE (this);
166 return_trace (c->check_struct (this));
169 bool intersects (const hb_set_t *glyphs) const
170 { return glyphs->intersects (start, end); }
172 template <typename set_t>
173 bool add_coverage (set_t *glyphs) const
174 { return glyphs->add_range (start, end); }
176 GlyphID start; /* First GlyphID in the range */
177 GlyphID end; /* Last GlyphID in the range */
178 HBUINT16 value; /* Value */
180 DEFINE_SIZE_STATIC (6);
182 DECLARE_NULL_NAMESPACE_BYTES (OT, RangeRecord);
185 struct IndexArray : ArrayOf<Index>
187 unsigned int get_indexes (unsigned int start_offset,
188 unsigned int *_count /* IN/OUT */,
189 unsigned int *_indexes /* OUT */) const
192 const HBUINT16 *arr = this->sub_array (start_offset, _count);
193 unsigned int count = *_count;
194 for (unsigned int i = 0; i < count; i++)
195 _indexes[i] = arr[i];
200 void add_indexes_to (hb_set_t* output /* OUT */) const
202 output->add_array (arrayZ, len);
214 unsigned int get_feature_count () const
215 { return featureIndex.len; }
216 hb_tag_t get_feature_index (unsigned int i) const
217 { return featureIndex[i]; }
218 unsigned int get_feature_indexes (unsigned int start_offset,
219 unsigned int *feature_count /* IN/OUT */,
220 unsigned int *feature_indexes /* OUT */) const
221 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
222 void add_feature_indexes_to (hb_set_t *feature_indexes) const
223 { featureIndex.add_indexes_to (feature_indexes); }
225 bool has_required_feature () const { return reqFeatureIndex != 0xFFFFu; }
226 unsigned int get_required_feature_index () const
228 if (reqFeatureIndex == 0xFFFFu)
229 return Index::NOT_FOUND_INDEX;
230 return reqFeatureIndex;;
233 bool subset (hb_subset_context_t *c) const
236 return_trace (c->serializer->embed (*this));
239 bool sanitize (hb_sanitize_context_t *c,
240 const Record_sanitize_closure_t * = nullptr) const
242 TRACE_SANITIZE (this);
243 return_trace (c->check_struct (this) && featureIndex.sanitize (c));
246 Offset16 lookupOrderZ; /* = Null (reserved for an offset to a
247 * reordering table) */
248 HBUINT16 reqFeatureIndex;/* Index of a feature required for this
249 * language system--if no required features
251 IndexArray featureIndex; /* Array of indices into the FeatureList */
253 DEFINE_SIZE_ARRAY_SIZED (6, featureIndex);
255 DECLARE_NULL_NAMESPACE_BYTES (OT, LangSys);
259 unsigned int get_lang_sys_count () const
260 { return langSys.len; }
261 const Tag& get_lang_sys_tag (unsigned int i) const
262 { return langSys.get_tag (i); }
263 unsigned int get_lang_sys_tags (unsigned int start_offset,
264 unsigned int *lang_sys_count /* IN/OUT */,
265 hb_tag_t *lang_sys_tags /* OUT */) const
266 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
267 const LangSys& get_lang_sys (unsigned int i) const
269 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
270 return this+langSys[i].offset;
272 bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
273 { return langSys.find_index (tag, index); }
275 bool has_default_lang_sys () const { return defaultLangSys != 0; }
276 const LangSys& get_default_lang_sys () const { return this+defaultLangSys; }
278 bool subset (hb_subset_context_t *c) const
281 struct Script *out = c->serializer->embed (*this);
282 if (unlikely (!out)) return_trace (false);
283 out->defaultLangSys.serialize_subset (c, this+defaultLangSys, out);
284 unsigned int count = langSys.len;
285 for (unsigned int i = 0; i < count; i++)
286 out->langSys.arrayZ[i].offset.serialize_subset (c, this+langSys[i].offset, out);
290 bool sanitize (hb_sanitize_context_t *c,
291 const Record_sanitize_closure_t * = nullptr) const
293 TRACE_SANITIZE (this);
294 return_trace (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
299 defaultLangSys; /* Offset to DefaultLangSys table--from
300 * beginning of Script table--may be Null */
301 RecordArrayOf<LangSys>
302 langSys; /* Array of LangSysRecords--listed
303 * alphabetically by LangSysTag */
305 DEFINE_SIZE_ARRAY_SIZED (4, langSys);
308 typedef RecordListOf<Script> ScriptList;
311 /* https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#size */
312 struct FeatureParamsSize
314 bool sanitize (hb_sanitize_context_t *c) const
316 TRACE_SANITIZE (this);
317 if (unlikely (!c->check_struct (this))) return_trace (false);
319 /* This subtable has some "history", if you will. Some earlier versions of
320 * Adobe tools calculated the offset of the FeatureParams sutable from the
321 * beginning of the FeatureList table! Now, that is dealt with in the
322 * Feature implementation. But we still need to be able to tell junk from
323 * real data. Note: We don't check that the nameID actually exists.
325 * Read Roberts wrote on 9/15/06 on opentype-list@indx.co.uk :
327 * Yes, it is correct that a new version of the AFDKO (version 2.0) will be
328 * coming out soon, and that the makeotf program will build a font with a
329 * 'size' feature that is correct by the specification.
331 * The specification for this feature tag is in the "OpenType Layout Tag
332 * Registry". You can see a copy of this at:
333 * https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#tag-size
335 * Here is one set of rules to determine if the 'size' feature is built
336 * correctly, or as by the older versions of MakeOTF. You may be able to do
339 * Assume that the offset to the size feature is according to specification,
340 * and make the following value checks. If it fails, assume the size
341 * feature is calculated as versions of MakeOTF before the AFDKO 2.0 built it.
342 * If this fails, reject the 'size' feature. The older makeOTF's calculated the
343 * offset from the beginning of the FeatureList table, rather than from the
344 * beginning of the 'size' Feature table.
346 * If "design size" == 0:
349 * Else if ("subfamily identifier" == 0 and
350 * "range start" == 0 and
351 * "range end" == 0 and
352 * "range start" == 0 and
353 * "menu name ID" == 0)
354 * passes check: this is the format used when there is a design size
355 * specified, but there is no recommended size range.
357 * Else if ("design size" < "range start" or
358 * "design size" > "range end" or
359 * "range end" <= "range start" or
360 * "menu name ID" < 256 or
361 * "menu name ID" > 32767 or
362 * menu name ID is not a name ID which is actually in the name table)
369 return_trace (false);
370 else if (subfamilyID == 0 &&
371 subfamilyNameID == 0 &&
375 else if (designSize < rangeStart ||
376 designSize > rangeEnd ||
377 subfamilyNameID < 256 ||
378 subfamilyNameID > 32767)
379 return_trace (false);
384 HBUINT16 designSize; /* Represents the design size in 720/inch
385 * units (decipoints). The design size entry
386 * must be non-zero. When there is a design
387 * size but no recommended size range, the
388 * rest of the array will consist of zeros. */
389 HBUINT16 subfamilyID; /* Has no independent meaning, but serves
390 * as an identifier that associates fonts
391 * in a subfamily. All fonts which share a
392 * Preferred or Font Family name and which
393 * differ only by size range shall have the
394 * same subfamily value, and no fonts which
395 * differ in weight or style shall have the
396 * same subfamily value. If this value is
397 * zero, the remaining fields in the array
398 * will be ignored. */
399 NameID subfamilyNameID;/* If the preceding value is non-zero, this
400 * value must be set in the range 256 - 32767
401 * (inclusive). It records the value of a
402 * field in the name table, which must
403 * contain English-language strings encoded
404 * in Windows Unicode and Macintosh Roman,
405 * and may contain additional strings
406 * localized to other scripts and languages.
407 * Each of these strings is the name an
408 * application should use, in combination
409 * with the family name, to represent the
410 * subfamily in a menu. Applications will
411 * choose the appropriate version based on
412 * their selection criteria. */
413 HBUINT16 rangeStart; /* Large end of the recommended usage range
414 * (inclusive), stored in 720/inch units
416 HBUINT16 rangeEnd; /* Small end of the recommended usage range
417 (exclusive), stored in 720/inch units
420 DEFINE_SIZE_STATIC (10);
423 /* https://docs.microsoft.com/en-us/typography/opentype/spec/features_pt#ssxx */
424 struct FeatureParamsStylisticSet
426 bool sanitize (hb_sanitize_context_t *c) const
428 TRACE_SANITIZE (this);
429 /* Right now minorVersion is at zero. Which means, any table supports
430 * the uiNameID field. */
431 return_trace (c->check_struct (this));
434 HBUINT16 version; /* (set to 0): This corresponds to a “minor”
435 * version number. Additional data may be
436 * added to the end of this Feature Parameters
437 * table in the future. */
439 NameID uiNameID; /* The 'name' table name ID that specifies a
440 * string (or strings, for multiple languages)
441 * for a user-interface label for this
442 * feature. The values of uiLabelNameId and
443 * sampleTextNameId are expected to be in the
444 * font-specific name ID range (256-32767),
445 * though that is not a requirement in this
446 * Feature Parameters specification. The
447 * user-interface label for the feature can
448 * be provided in multiple languages. An
449 * English string should be included as a
450 * fallback. The string should be kept to a
451 * minimal length to fit comfortably with
452 * different application interfaces. */
454 DEFINE_SIZE_STATIC (4);
457 /* https://docs.microsoft.com/en-us/typography/opentype/spec/features_ae#cv01-cv99 */
458 struct FeatureParamsCharacterVariants
460 bool sanitize (hb_sanitize_context_t *c) const
462 TRACE_SANITIZE (this);
463 return_trace (c->check_struct (this) &&
464 characters.sanitize (c));
467 HBUINT16 format; /* Format number is set to 0. */
468 NameID featUILableNameID; /* The ‘name’ table name ID that
469 * specifies a string (or strings,
470 * for multiple languages) for a
471 * user-interface label for this
472 * feature. (May be NULL.) */
473 NameID featUITooltipTextNameID;/* The ‘name’ table name ID that
474 * specifies a string (or strings,
475 * for multiple languages) that an
476 * application can use for tooltip
477 * text for this feature. (May be
479 NameID sampleTextNameID; /* The ‘name’ table name ID that
480 * specifies sample text that
481 * illustrates the effect of this
482 * feature. (May be NULL.) */
483 HBUINT16 numNamedParameters; /* Number of named parameters. (May
485 NameID firstParamUILabelNameID;/* The first ‘name’ table name ID
486 * used to specify strings for
487 * user-interface labels for the
488 * feature parameters. (Must be zero
489 * if numParameters is zero.) */
491 characters; /* Array of the Unicode Scalar Value
492 * of the characters for which this
493 * feature provides glyph variants.
496 DEFINE_SIZE_ARRAY (14, characters);
501 bool sanitize (hb_sanitize_context_t *c, hb_tag_t tag) const
503 TRACE_SANITIZE (this);
504 if (tag == HB_TAG ('s','i','z','e'))
505 return_trace (u.size.sanitize (c));
506 if ((tag & 0xFFFF0000u) == HB_TAG ('s','s','\0','\0')) /* ssXX */
507 return_trace (u.stylisticSet.sanitize (c));
508 if ((tag & 0xFFFF0000u) == HB_TAG ('c','v','\0','\0')) /* cvXX */
509 return_trace (u.characterVariants.sanitize (c));
513 const FeatureParamsSize& get_size_params (hb_tag_t tag) const
515 if (tag == HB_TAG ('s','i','z','e'))
517 return Null (FeatureParamsSize);
520 const FeatureParamsStylisticSet& get_stylistic_set_params (hb_tag_t tag) const
522 if ((tag & 0xFFFF0000u) == HB_TAG ('s','s','\0','\0')) /* ssXX */
523 return u.stylisticSet;
524 return Null (FeatureParamsStylisticSet);
527 const FeatureParamsCharacterVariants& get_character_variants_params (hb_tag_t tag) const
529 if ((tag & 0xFFFF0000u) == HB_TAG ('c','v','\0','\0')) /* cvXX */
530 return u.characterVariants;
531 return Null (FeatureParamsCharacterVariants);
536 FeatureParamsSize size;
537 FeatureParamsStylisticSet stylisticSet;
538 FeatureParamsCharacterVariants characterVariants;
541 DEFINE_SIZE_STATIC (17);
546 unsigned int get_lookup_count () const
547 { return lookupIndex.len; }
548 hb_tag_t get_lookup_index (unsigned int i) const
549 { return lookupIndex[i]; }
550 unsigned int get_lookup_indexes (unsigned int start_index,
551 unsigned int *lookup_count /* IN/OUT */,
552 unsigned int *lookup_tags /* OUT */) const
553 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
554 void add_lookup_indexes_to (hb_set_t *lookup_indexes) const
555 { lookupIndex.add_indexes_to (lookup_indexes); }
557 const FeatureParams &get_feature_params () const
558 { return this+featureParams; }
560 bool subset (hb_subset_context_t *c) const
563 struct Feature *out = c->serializer->embed (*this);
564 if (unlikely (!out)) return_trace (false);
565 out->featureParams.set (0); /* TODO(subset) FeatureParams. */
569 bool sanitize (hb_sanitize_context_t *c,
570 const Record_sanitize_closure_t *closure = nullptr) const
572 TRACE_SANITIZE (this);
573 if (unlikely (!(c->check_struct (this) && lookupIndex.sanitize (c))))
574 return_trace (false);
576 /* Some earlier versions of Adobe tools calculated the offset of the
577 * FeatureParams subtable from the beginning of the FeatureList table!
579 * If sanitizing "failed" for the FeatureParams subtable, try it with the
580 * alternative location. We would know sanitize "failed" if old value
581 * of the offset was non-zero, but it's zeroed now.
583 * Only do this for the 'size' feature, since at the time of the faulty
584 * Adobe tools, only the 'size' feature had FeatureParams defined.
587 OffsetTo<FeatureParams> orig_offset = featureParams;
588 if (unlikely (!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE)))
589 return_trace (false);
591 if (likely (orig_offset.is_null ()))
594 if (featureParams == 0 && closure &&
595 closure->tag == HB_TAG ('s','i','z','e') &&
596 closure->list_base && closure->list_base < this)
598 unsigned int new_offset_int = (unsigned int) orig_offset -
599 (((char *) this) - ((char *) closure->list_base));
601 OffsetTo<FeatureParams> new_offset;
602 /* Check that it did not overflow. */
603 new_offset.set (new_offset_int);
604 if (new_offset == new_offset_int &&
605 c->try_set (&featureParams, new_offset) &&
606 !featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE))
607 return_trace (false);
613 OffsetTo<FeatureParams>
614 featureParams; /* Offset to Feature Parameters table (if one
615 * has been defined for the feature), relative
616 * to the beginning of the Feature Table; = Null
618 IndexArray lookupIndex; /* Array of LookupList indices */
620 DEFINE_SIZE_ARRAY_SIZED (4, lookupIndex);
623 typedef RecordListOf<Feature> FeatureList;
626 struct LookupFlag : HBUINT16
629 RightToLeft = 0x0001u,
630 IgnoreBaseGlyphs = 0x0002u,
631 IgnoreLigatures = 0x0004u,
632 IgnoreMarks = 0x0008u,
633 IgnoreFlags = 0x000Eu,
634 UseMarkFilteringSet = 0x0010u,
636 MarkAttachmentType = 0xFF00u
639 DEFINE_SIZE_STATIC (2);
643 /* This has to be outside the namespace. */
644 HB_MARK_AS_FLAG_T (OT::LookupFlag::Flags);
649 unsigned int get_subtable_count () const { return subTable.len; }
651 template <typename TSubTable>
652 const TSubTable& get_subtable (unsigned int i) const
653 { return this+CastR<OffsetArrayOf<TSubTable> > (subTable)[i]; }
655 template <typename TSubTable>
656 const OffsetArrayOf<TSubTable>& get_subtables () const
657 { return CastR<OffsetArrayOf<TSubTable> > (subTable); }
658 template <typename TSubTable>
659 OffsetArrayOf<TSubTable>& get_subtables ()
660 { return CastR<OffsetArrayOf<TSubTable> > (subTable); }
662 unsigned int get_size () const
664 const HBUINT16 &markFilteringSet = StructAfter<const HBUINT16> (subTable);
665 if (lookupFlag & LookupFlag::UseMarkFilteringSet)
666 return (const char *) &StructAfter<const char> (markFilteringSet) - (const char *) this;
667 return (const char *) &markFilteringSet - (const char *) this;
670 unsigned int get_type () const { return lookupType; }
672 /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
673 * higher 16-bit is mark-filtering-set if the lookup uses one.
674 * Not to be confused with glyph_props which is very similar. */
675 uint32_t get_props () const
677 unsigned int flag = lookupFlag;
678 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
680 const HBUINT16 &markFilteringSet = StructAfter<HBUINT16> (subTable);
681 flag += (markFilteringSet << 16);
686 template <typename TSubTable, typename context_t>
687 typename context_t::return_t dispatch (context_t *c) const
689 unsigned int lookup_type = get_type ();
690 TRACE_DISPATCH (this, lookup_type);
691 unsigned int count = get_subtable_count ();
692 for (unsigned int i = 0; i < count; i++) {
693 typename context_t::return_t r = get_subtable<TSubTable> (i).dispatch (c, lookup_type);
694 if (c->stop_sublookup_iteration (r))
697 return_trace (c->default_return_value ());
700 bool serialize (hb_serialize_context_t *c,
701 unsigned int lookup_type,
702 uint32_t lookup_props,
703 unsigned int num_subtables)
705 TRACE_SERIALIZE (this);
706 if (unlikely (!c->extend_min (*this))) return_trace (false);
707 lookupType.set (lookup_type);
708 lookupFlag.set (lookup_props & 0xFFFFu);
709 if (unlikely (!subTable.serialize (c, num_subtables))) return_trace (false);
710 if (lookupFlag & LookupFlag::UseMarkFilteringSet)
712 if (unlikely (!c->extend (*this))) return_trace (false);
713 HBUINT16 &markFilteringSet = StructAfter<HBUINT16> (subTable);
714 markFilteringSet.set (lookup_props >> 16);
719 /* Older compilers need this to NOT be locally defined in a function. */
720 template <typename TSubTable>
721 struct SubTableSubsetWrapper
723 SubTableSubsetWrapper (const TSubTable &subtable_,
724 unsigned int lookup_type_) :
725 subtable (subtable_),
726 lookup_type (lookup_type_) {}
728 bool subset (hb_subset_context_t *c) const
729 { return subtable.dispatch (c, lookup_type); }
732 const TSubTable &subtable;
733 unsigned int lookup_type;
736 template <typename TSubTable>
737 bool subset (hb_subset_context_t *c) const
740 struct Lookup *out = c->serializer->embed (*this);
741 if (unlikely (!out)) return_trace (false);
743 /* Subset the actual subtables. */
744 /* TODO Drop empty ones, either by calling intersects() beforehand,
745 * or just dropping null offsets after. */
746 const OffsetArrayOf<TSubTable>& subtables = get_subtables<TSubTable> ();
747 OffsetArrayOf<TSubTable>& out_subtables = out->get_subtables<TSubTable> ();
748 unsigned int count = subTable.len;
749 for (unsigned int i = 0; i < count; i++)
751 SubTableSubsetWrapper<TSubTable> wrapper (this+subtables[i], get_type ());
753 out_subtables[i].serialize_subset (c, wrapper, out);
759 /* Older compilers need this to NOT be locally defined in a function. */
760 template <typename TSubTable>
761 struct SubTableSanitizeWrapper : TSubTable
763 bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) const
764 { return this->dispatch (c, lookup_type); }
767 template <typename TSubTable>
768 bool sanitize (hb_sanitize_context_t *c) const
770 TRACE_SANITIZE (this);
771 if (!(c->check_struct (this) && subTable.sanitize (c))) return_trace (false);
772 if (lookupFlag & LookupFlag::UseMarkFilteringSet)
774 const HBUINT16 &markFilteringSet = StructAfter<HBUINT16> (subTable);
775 if (!markFilteringSet.sanitize (c)) return_trace (false);
778 if (unlikely (!CastR<OffsetArrayOf<SubTableSanitizeWrapper<TSubTable> > > (subTable)
779 .sanitize (c, this, get_type ())))
780 return_trace (false);
782 if (unlikely (get_type () == TSubTable::Extension))
784 /* The spec says all subtables of an Extension lookup should
785 * have the same type, which shall not be the Extension type
786 * itself (but we already checked for that).
787 * This is specially important if one has a reverse type! */
788 unsigned int type = get_subtable<TSubTable> (0).u.extension.get_type ();
789 unsigned int count = get_subtable_count ();
790 for (unsigned int i = 1; i < count; i++)
791 if (get_subtable<TSubTable> (i).u.extension.get_type () != type)
792 return_trace (false);
799 HBUINT16 lookupType; /* Different enumerations for GSUB and GPOS */
800 HBUINT16 lookupFlag; /* Lookup qualifiers */
802 subTable; /* Array of SubTables */
803 /*HBUINT16 markFilteringSetX[VAR];*//* Index (base 0) into GDEF mark glyph sets
804 * structure. This field is only present if bit
805 * UseMarkFilteringSet of lookup flags is set. */
807 DEFINE_SIZE_ARRAY (6, subTable);
810 typedef OffsetListOf<Lookup> LookupList;
817 struct CoverageFormat1
819 friend struct Coverage;
822 unsigned int get_coverage (hb_codepoint_t glyph_id) const
825 glyphArray.bfind (glyph_id, &i, HB_BFIND_NOT_FOUND_STORE, NOT_COVERED);
829 bool serialize (hb_serialize_context_t *c,
830 hb_array_t<const GlyphID> glyphs)
832 TRACE_SERIALIZE (this);
833 return_trace (glyphArray.serialize (c, glyphs));
836 bool sanitize (hb_sanitize_context_t *c) const
838 TRACE_SANITIZE (this);
839 return_trace (glyphArray.sanitize (c));
842 bool intersects (const hb_set_t *glyphs) const
844 /* TODO Speed up, using hb_set_next() and bsearch()? */
845 unsigned int count = glyphArray.len;
846 for (unsigned int i = 0; i < count; i++)
847 if (glyphs->has (glyphArray[i]))
851 bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const
852 { return glyphs->has (glyphArray[index]); }
854 template <typename set_t>
855 bool add_coverage (set_t *glyphs) const
857 return glyphs->add_sorted_array (glyphArray.arrayZ, glyphArray.len);
861 /* Older compilers need this to be public. */
863 void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; }
865 bool more () { return i < c->glyphArray.len; }
866 void next () { i++; }
867 hb_codepoint_t get_glyph () { return c->glyphArray[i]; }
868 unsigned int get_coverage () { return i; }
871 const struct CoverageFormat1 *c;
877 HBUINT16 coverageFormat; /* Format identifier--format = 1 */
878 SortedArrayOf<GlyphID>
879 glyphArray; /* Array of GlyphIDs--in numerical order */
881 DEFINE_SIZE_ARRAY (4, glyphArray);
884 struct CoverageFormat2
886 friend struct Coverage;
889 unsigned int get_coverage (hb_codepoint_t glyph_id) const
891 const RangeRecord &range = rangeRecord.bsearch (glyph_id);
892 return likely (range.start <= range.end) ?
893 (unsigned int) range.value + (glyph_id - range.start) :
897 bool serialize (hb_serialize_context_t *c,
898 hb_array_t<const GlyphID> glyphs)
900 TRACE_SERIALIZE (this);
901 if (unlikely (!c->extend_min (*this))) return_trace (false);
903 if (unlikely (!glyphs.length))
905 rangeRecord.len.set (0);
909 unsigned int num_ranges = 1;
910 for (unsigned int i = 1; i < glyphs.length; i++)
911 if (glyphs[i - 1] + 1 != glyphs[i])
913 rangeRecord.len.set (num_ranges);
914 if (unlikely (!c->extend (rangeRecord))) return_trace (false);
916 unsigned int range = 0;
917 rangeRecord[range].start = glyphs[0];
918 rangeRecord[range].value.set (0);
919 for (unsigned int i = 1; i < glyphs.length; i++)
921 if (glyphs[i - 1] + 1 != glyphs[i])
924 rangeRecord[range].start = glyphs[i];
925 rangeRecord[range].value.set (i);
927 rangeRecord[range].end = glyphs[i];
932 bool sanitize (hb_sanitize_context_t *c) const
934 TRACE_SANITIZE (this);
935 return_trace (rangeRecord.sanitize (c));
938 bool intersects (const hb_set_t *glyphs) const
940 /* TODO Speed up, using hb_set_next() and bsearch()? */
941 unsigned int count = rangeRecord.len;
942 for (unsigned int i = 0; i < count; i++)
943 if (rangeRecord[i].intersects (glyphs))
947 bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const
950 unsigned int count = rangeRecord.len;
951 for (i = 0; i < count; i++) {
952 const RangeRecord &range = rangeRecord[i];
953 if (range.value <= index &&
954 index < (unsigned int) range.value + (range.end - range.start) &&
955 range.intersects (glyphs))
957 else if (index < range.value)
963 template <typename set_t>
964 bool add_coverage (set_t *glyphs) const
966 unsigned int count = rangeRecord.len;
967 for (unsigned int i = 0; i < count; i++)
968 if (unlikely (!rangeRecord[i].add_coverage (glyphs)))
974 /* Older compilers need this to be public. */
977 void init (const CoverageFormat2 &c_)
982 j = c->rangeRecord.len ? c->rangeRecord[0].start : 0;
983 if (unlikely (c->rangeRecord[0].start > c->rangeRecord[0].end))
985 /* Broken table. Skip. */
986 i = c->rangeRecord.len;
990 bool more () { return i < c->rangeRecord.len; }
993 if (j >= c->rangeRecord[i].end)
998 hb_codepoint_t old = j;
999 j = c->rangeRecord[i].start;
1000 if (unlikely (j <= old))
1002 /* Broken table. Skip. Important to avoid DoS. */
1003 i = c->rangeRecord.len;
1006 coverage = c->rangeRecord[i].value;
1013 hb_codepoint_t get_glyph () { return j; }
1014 unsigned int get_coverage () { return coverage; }
1017 const struct CoverageFormat2 *c;
1018 unsigned int i, coverage;
1024 HBUINT16 coverageFormat; /* Format identifier--format = 2 */
1025 SortedArrayOf<RangeRecord>
1026 rangeRecord; /* Array of glyph ranges--ordered by
1027 * Start GlyphID. rangeCount entries
1030 DEFINE_SIZE_ARRAY (4, rangeRecord);
1035 unsigned int get_coverage (hb_codepoint_t glyph_id) const
1038 case 1: return u.format1.get_coverage (glyph_id);
1039 case 2: return u.format2.get_coverage (glyph_id);
1040 default:return NOT_COVERED;
1044 bool serialize (hb_serialize_context_t *c,
1045 hb_array_t<const GlyphID> glyphs)
1047 TRACE_SERIALIZE (this);
1048 if (unlikely (!c->extend_min (*this))) return_trace (false);
1050 unsigned int num_ranges = 1;
1051 for (unsigned int i = 1; i < glyphs.length; i++)
1052 if (glyphs[i - 1] + 1 != glyphs[i])
1054 u.format.set (glyphs.length * 2 < num_ranges * 3 ? 1 : 2);
1058 case 1: return_trace (u.format1.serialize (c, glyphs));
1059 case 2: return_trace (u.format2.serialize (c, glyphs));
1060 default:return_trace (false);
1064 bool sanitize (hb_sanitize_context_t *c) const
1066 TRACE_SANITIZE (this);
1067 if (!u.format.sanitize (c)) return_trace (false);
1070 case 1: return_trace (u.format1.sanitize (c));
1071 case 2: return_trace (u.format2.sanitize (c));
1072 default:return_trace (true);
1076 bool intersects (const hb_set_t *glyphs) const
1080 case 1: return u.format1.intersects (glyphs);
1081 case 2: return u.format2.intersects (glyphs);
1082 default:return false;
1085 bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const
1089 case 1: return u.format1.intersects_coverage (glyphs, index);
1090 case 2: return u.format2.intersects_coverage (glyphs, index);
1091 default:return false;
1095 /* Might return false if array looks unsorted.
1096 * Used for faster rejection of corrupt data. */
1097 template <typename set_t>
1098 bool add_coverage (set_t *glyphs) const
1102 case 1: return u.format1.add_coverage (glyphs);
1103 case 2: return u.format2.add_coverage (glyphs);
1104 default:return false;
1110 Iter (const Coverage &c_)
1112 memset (this, 0, sizeof (*this));
1113 format = c_.u.format;
1116 case 1: u.format1.init (c_.u.format1); return;
1117 case 2: u.format2.init (c_.u.format2); return;
1125 case 1: return u.format1.more ();
1126 case 2: return u.format2.more ();
1127 default:return false;
1134 case 1: u.format1.next (); break;
1135 case 2: u.format2.next (); break;
1139 hb_codepoint_t get_glyph ()
1143 case 1: return u.format1.get_glyph ();
1144 case 2: return u.format2.get_glyph ();
1148 unsigned int get_coverage ()
1152 case 1: return u.format1.get_coverage ();
1153 case 2: return u.format2.get_coverage ();
1159 unsigned int format;
1161 CoverageFormat2::Iter format2; /* Put this one first since it's larger; helps shut up compiler. */
1162 CoverageFormat1::Iter format1;
1168 HBUINT16 format; /* Format identifier */
1169 CoverageFormat1 format1;
1170 CoverageFormat2 format2;
1173 DEFINE_SIZE_UNION (2, format);
1178 * Class Definition Table
1181 static inline void ClassDef_serialize (hb_serialize_context_t *c,
1182 hb_array_t<const GlyphID> glyphs,
1183 hb_array_t<const HBUINT16> klasses);
1185 struct ClassDefFormat1
1187 friend struct ClassDef;
1190 unsigned int get_class (hb_codepoint_t glyph_id) const
1192 return classValue[(unsigned int) (glyph_id - startGlyph)];
1195 bool serialize (hb_serialize_context_t *c,
1196 hb_array_t<const HBUINT16> glyphs,
1197 hb_array_t<const HBUINT16> klasses)
1199 TRACE_SERIALIZE (this);
1200 if (unlikely (!c->extend_min (*this))) return_trace (false);
1202 if (unlikely (!glyphs.length))
1205 classValue.len.set (0);
1206 return_trace (true);
1209 hb_codepoint_t glyph_min = glyphs[0];
1210 hb_codepoint_t glyph_max = glyphs[glyphs.length - 1];
1212 startGlyph.set (glyph_min);
1213 classValue.len.set (glyph_max - glyph_min + 1);
1214 if (unlikely (!c->extend (classValue))) return_trace (false);
1216 for (unsigned int i = 0; i < glyphs.length; i++)
1217 classValue[glyphs[i] - glyph_min] = klasses[i];
1219 return_trace (true);
1222 bool subset (hb_subset_context_t *c) const
1224 TRACE_SUBSET (this);
1225 const hb_set_t &glyphset = *c->plan->glyphset ();
1226 const hb_map_t &glyph_map = *c->plan->glyph_map;
1227 hb_vector_t<GlyphID> glyphs;
1228 hb_vector_t<HBUINT16> klasses;
1230 hb_codepoint_t start = startGlyph;
1231 hb_codepoint_t end = start + classValue.len;
1232 for (hb_codepoint_t g = start; g < end; g++)
1234 unsigned int value = classValue[g - start];
1235 if (!value) continue;
1236 if (!glyphset.has (g)) continue;
1237 glyphs.push()->set (glyph_map[g]);
1238 klasses.push()->set (value);
1240 c->serializer->propagate_error (glyphs, klasses);
1241 ClassDef_serialize (c->serializer, glyphs, klasses);
1242 return_trace (glyphs.length);
1245 bool sanitize (hb_sanitize_context_t *c) const
1247 TRACE_SANITIZE (this);
1248 return_trace (c->check_struct (this) && classValue.sanitize (c));
1251 template <typename set_t>
1252 bool add_coverage (set_t *glyphs) const
1254 unsigned int start = 0;
1255 unsigned int count = classValue.len;
1256 for (unsigned int i = 0; i < count; i++)
1262 if (unlikely (!glyphs->add_range (startGlyph + start, startGlyph + i)))
1268 if (unlikely (!glyphs->add_range (startGlyph + start, startGlyph + count)))
1274 template <typename set_t>
1275 bool add_class (set_t *glyphs, unsigned int klass) const
1277 unsigned int count = classValue.len;
1278 for (unsigned int i = 0; i < count; i++)
1279 if (classValue[i] == klass) glyphs->add (startGlyph + i);
1283 bool intersects (const hb_set_t *glyphs) const
1285 /* TODO Speed up, using hb_set_next()? */
1286 hb_codepoint_t start = startGlyph;
1287 hb_codepoint_t end = startGlyph + classValue.len;
1288 for (hb_codepoint_t iter = startGlyph - 1;
1289 hb_set_next (glyphs, &iter) && iter < end;)
1290 if (classValue[iter - start]) return true;
1293 bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const
1295 unsigned int count = classValue.len;
1298 /* Match if there's any glyph that is not listed! */
1299 hb_codepoint_t g = HB_SET_VALUE_INVALID;
1300 if (!hb_set_next (glyphs, &g)) return false;
1301 if (g < startGlyph) return true;
1302 g = startGlyph + count - 1;
1303 if (hb_set_next (glyphs, &g)) return true;
1306 for (unsigned int i = 0; i < count; i++)
1307 if (classValue[i] == klass && glyphs->has (startGlyph + i))
1313 HBUINT16 classFormat; /* Format identifier--format = 1 */
1314 GlyphID startGlyph; /* First GlyphID of the classValueArray */
1316 classValue; /* Array of Class Values--one per GlyphID */
1318 DEFINE_SIZE_ARRAY (6, classValue);
1321 struct ClassDefFormat2
1323 friend struct ClassDef;
1326 unsigned int get_class (hb_codepoint_t glyph_id) const
1328 return rangeRecord.bsearch (glyph_id).value;
1331 bool serialize (hb_serialize_context_t *c,
1332 hb_array_t<const HBUINT16> glyphs,
1333 hb_array_t<const HBUINT16> klasses)
1335 TRACE_SERIALIZE (this);
1336 if (unlikely (!c->extend_min (*this))) return_trace (false);
1338 if (unlikely (!glyphs.length))
1340 rangeRecord.len.set (0);
1341 return_trace (true);
1344 unsigned int num_ranges = 1;
1345 for (unsigned int i = 1; i < glyphs.length; i++)
1346 if (glyphs[i - 1] + 1 != glyphs[i] ||
1347 klasses[i - 1] != klasses[i])
1349 rangeRecord.len.set (num_ranges);
1350 if (unlikely (!c->extend (rangeRecord))) return_trace (false);
1352 unsigned int range = 0;
1353 rangeRecord[range].start = glyphs[0];
1354 rangeRecord[range].value.set (klasses[0]);
1355 for (unsigned int i = 1; i < glyphs.length; i++)
1357 if (glyphs[i - 1] + 1 != glyphs[i] ||
1358 klasses[i - 1] != klasses[i])
1361 rangeRecord[range].start = glyphs[i];
1362 rangeRecord[range].value = klasses[i];
1364 rangeRecord[range].end = glyphs[i];
1366 return_trace (true);
1369 bool subset (hb_subset_context_t *c) const
1371 TRACE_SUBSET (this);
1372 const hb_set_t &glyphset = *c->plan->glyphset ();
1373 const hb_map_t &glyph_map = *c->plan->glyph_map;
1374 hb_vector_t<GlyphID> glyphs;
1375 hb_vector_t<HBUINT16> klasses;
1377 unsigned int count = rangeRecord.len;
1378 for (unsigned int i = 0; i < count; i++)
1380 unsigned int value = rangeRecord[i].value;
1381 if (!value) continue;
1382 hb_codepoint_t start = rangeRecord[i].start;
1383 hb_codepoint_t end = rangeRecord[i].end + 1;
1384 for (hb_codepoint_t g = start; g < end; g++)
1386 if (!glyphset.has (g)) continue;
1387 glyphs.push ()->set (glyph_map[g]);
1388 klasses.push ()->set (value);
1391 c->serializer->propagate_error (glyphs, klasses);
1392 ClassDef_serialize (c->serializer, glyphs, klasses);
1393 return_trace (glyphs.length);
1396 bool sanitize (hb_sanitize_context_t *c) const
1398 TRACE_SANITIZE (this);
1399 return_trace (rangeRecord.sanitize (c));
1402 template <typename set_t>
1403 bool add_coverage (set_t *glyphs) const
1405 unsigned int count = rangeRecord.len;
1406 for (unsigned int i = 0; i < count; i++)
1407 if (rangeRecord[i].value)
1408 if (unlikely (!rangeRecord[i].add_coverage (glyphs)))
1413 template <typename set_t>
1414 bool add_class (set_t *glyphs, unsigned int klass) const
1416 unsigned int count = rangeRecord.len;
1417 for (unsigned int i = 0; i < count; i++)
1419 if (rangeRecord[i].value == klass)
1420 if (unlikely (!rangeRecord[i].add_coverage (glyphs)))
1426 bool intersects (const hb_set_t *glyphs) const
1428 /* TODO Speed up, using hb_set_next() and bsearch()? */
1429 unsigned int count = rangeRecord.len;
1430 for (unsigned int i = 0; i < count; i++)
1431 if (rangeRecord[i].intersects (glyphs))
1435 bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const
1437 unsigned int count = rangeRecord.len;
1440 /* Match if there's any glyph that is not listed! */
1441 hb_codepoint_t g = HB_SET_VALUE_INVALID;
1442 for (unsigned int i = 0; i < count; i++)
1444 if (!hb_set_next (glyphs, &g))
1446 if (g < rangeRecord[i].start)
1448 g = rangeRecord[i].end;
1450 if (g != HB_SET_VALUE_INVALID && hb_set_next (glyphs, &g))
1454 for (unsigned int i = 0; i < count; i++)
1455 if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
1461 HBUINT16 classFormat; /* Format identifier--format = 2 */
1462 SortedArrayOf<RangeRecord>
1463 rangeRecord; /* Array of glyph ranges--ordered by
1466 DEFINE_SIZE_ARRAY (4, rangeRecord);
1471 unsigned int get_class (hb_codepoint_t glyph_id) const
1474 case 1: return u.format1.get_class (glyph_id);
1475 case 2: return u.format2.get_class (glyph_id);
1480 bool serialize (hb_serialize_context_t *c,
1481 hb_array_t<const GlyphID> glyphs,
1482 hb_array_t<const HBUINT16> klasses)
1484 TRACE_SERIALIZE (this);
1485 if (unlikely (!c->extend_min (*this))) return_trace (false);
1487 unsigned int format = 2;
1490 hb_codepoint_t glyph_min = glyphs[0];
1491 hb_codepoint_t glyph_max = glyphs[glyphs.length - 1];
1493 unsigned int num_ranges = 1;
1494 for (unsigned int i = 1; i < glyphs.length; i++)
1495 if (glyphs[i - 1] + 1 != glyphs[i] ||
1496 klasses[i - 1] != klasses[i])
1499 if (1 + (glyph_max - glyph_min + 1) < num_ranges * 3)
1502 u.format.set (format);
1506 case 1: return_trace (u.format1.serialize (c, glyphs, klasses));
1507 case 2: return_trace (u.format2.serialize (c, glyphs, klasses));
1508 default:return_trace (false);
1512 bool subset (hb_subset_context_t *c) const
1514 TRACE_SUBSET (this);
1516 case 1: return_trace (u.format1.subset (c));
1517 case 2: return_trace (u.format2.subset (c));
1518 default:return_trace (false);
1522 bool sanitize (hb_sanitize_context_t *c) const
1524 TRACE_SANITIZE (this);
1525 if (!u.format.sanitize (c)) return_trace (false);
1527 case 1: return_trace (u.format1.sanitize (c));
1528 case 2: return_trace (u.format2.sanitize (c));
1529 default:return_trace (true);
1533 /* Might return false if array looks unsorted.
1534 * Used for faster rejection of corrupt data. */
1535 template <typename set_t>
1536 bool add_coverage (set_t *glyphs) const
1539 case 1: return u.format1.add_coverage (glyphs);
1540 case 2: return u.format2.add_coverage (glyphs);
1541 default:return false;
1545 /* Might return false if array looks unsorted.
1546 * Used for faster rejection of corrupt data. */
1547 template <typename set_t>
1548 bool add_class (set_t *glyphs, unsigned int klass) const
1551 case 1: return u.format1.add_class (glyphs, klass);
1552 case 2: return u.format2.add_class (glyphs, klass);
1553 default:return false;
1557 bool intersects (const hb_set_t *glyphs) const
1560 case 1: return u.format1.intersects (glyphs);
1561 case 2: return u.format2.intersects (glyphs);
1562 default:return false;
1565 bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const
1568 case 1: return u.format1.intersects_class (glyphs, klass);
1569 case 2: return u.format2.intersects_class (glyphs, klass);
1570 default:return false;
1576 HBUINT16 format; /* Format identifier */
1577 ClassDefFormat1 format1;
1578 ClassDefFormat2 format2;
1581 DEFINE_SIZE_UNION (2, format);
1584 static inline void ClassDef_serialize (hb_serialize_context_t *c,
1585 hb_array_t<const GlyphID> glyphs,
1586 hb_array_t<const HBUINT16> klasses)
1587 { c->start_embed<ClassDef> ()->serialize (c, glyphs, klasses); }
1591 * Item Variation Store
1594 struct VarRegionAxis
1596 float evaluate (int coord) const
1598 int start = startCoord, peak = peakCoord, end = endCoord;
1600 /* TODO Move these to sanitize(). */
1601 if (unlikely (start > peak || peak > end))
1603 if (unlikely (start < 0 && end > 0 && peak != 0))
1606 if (peak == 0 || coord == peak)
1609 if (coord <= start || end <= coord)
1614 return float (coord - start) / (peak - start);
1616 return float (end - coord) / (end - peak);
1619 bool sanitize (hb_sanitize_context_t *c) const
1621 TRACE_SANITIZE (this);
1622 return_trace (c->check_struct (this));
1623 /* TODO Handle invalid start/peak/end configs, so we don't
1624 * have to do that at runtime. */
1632 DEFINE_SIZE_STATIC (6);
1635 struct VarRegionList
1637 float evaluate (unsigned int region_index,
1638 const int *coords, unsigned int coord_len) const
1640 if (unlikely (region_index >= regionCount))
1643 const VarRegionAxis *axes = axesZ.arrayZ + (region_index * axisCount);
1646 unsigned int count = axisCount;
1647 for (unsigned int i = 0; i < count; i++)
1649 int coord = i < coord_len ? coords[i] : 0;
1650 float factor = axes[i].evaluate (coord);
1658 bool sanitize (hb_sanitize_context_t *c) const
1660 TRACE_SANITIZE (this);
1661 return_trace (c->check_struct (this) &&
1662 axesZ.sanitize (c, (unsigned int) axisCount * (unsigned int) regionCount));
1665 unsigned int get_region_count () const { return regionCount; }
1669 HBUINT16 regionCount;
1670 UnsizedArrayOf<VarRegionAxis>
1673 DEFINE_SIZE_ARRAY (4, axesZ);
1678 unsigned int get_region_index_count () const
1679 { return regionIndices.len; }
1681 unsigned int get_row_size () const
1682 { return shortCount + regionIndices.len; }
1684 unsigned int get_size () const
1685 { return itemCount * get_row_size (); }
1687 float get_delta (unsigned int inner,
1688 const int *coords, unsigned int coord_count,
1689 const VarRegionList ®ions) const
1691 if (unlikely (inner >= itemCount))
1694 unsigned int count = regionIndices.len;
1695 unsigned int scount = shortCount;
1697 const HBUINT8 *bytes = &StructAfter<HBUINT8> (regionIndices);
1698 const HBUINT8 *row = bytes + inner * (scount + count);
1703 const HBINT16 *scursor = reinterpret_cast<const HBINT16 *> (row);
1704 for (; i < scount; i++)
1706 float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count);
1707 delta += scalar * *scursor++;
1709 const HBINT8 *bcursor = reinterpret_cast<const HBINT8 *> (scursor);
1710 for (; i < count; i++)
1712 float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count);
1713 delta += scalar * *bcursor++;
1719 void get_scalars (int *coords, unsigned int coord_count,
1720 const VarRegionList ®ions,
1721 float *scalars /*OUT */,
1722 unsigned int num_scalars) const
1724 assert (num_scalars == regionIndices.len);
1725 for (unsigned int i = 0; i < num_scalars; i++)
1727 scalars[i] = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count);
1731 bool sanitize (hb_sanitize_context_t *c) const
1733 TRACE_SANITIZE (this);
1734 return_trace (c->check_struct (this) &&
1735 regionIndices.sanitize (c) &&
1736 shortCount <= regionIndices.len &&
1737 c->check_range (&StructAfter<HBUINT8> (regionIndices),
1744 HBUINT16 shortCount;
1745 ArrayOf<HBUINT16> regionIndices;
1746 /*UnsizedArrayOf<HBUINT8>bytesX;*/
1748 DEFINE_SIZE_ARRAY (6, regionIndices);
1751 struct VariationStore
1753 float get_delta (unsigned int outer, unsigned int inner,
1754 const int *coords, unsigned int coord_count) const
1756 if (unlikely (outer >= dataSets.len))
1759 return (this+dataSets[outer]).get_delta (inner,
1760 coords, coord_count,
1764 float get_delta (unsigned int index,
1765 const int *coords, unsigned int coord_count) const
1767 unsigned int outer = index >> 16;
1768 unsigned int inner = index & 0xFFFF;
1769 return get_delta (outer, inner, coords, coord_count);
1772 bool sanitize (hb_sanitize_context_t *c) const
1774 TRACE_SANITIZE (this);
1775 return_trace (c->check_struct (this) &&
1777 regions.sanitize (c, this) &&
1778 dataSets.sanitize (c, this));
1781 unsigned int get_region_index_count (unsigned int ivs) const
1782 { return (this+dataSets[ivs]).get_region_index_count (); }
1784 void get_scalars (unsigned int ivs,
1785 int *coords, unsigned int coord_count,
1786 float *scalars /*OUT*/,
1787 unsigned int num_scalars) const
1789 (this+dataSets[ivs]).get_scalars (coords, coord_count, this+regions,
1790 &scalars[0], num_scalars);
1795 LOffsetTo<VarRegionList> regions;
1796 LOffsetArrayOf<VarData> dataSets;
1798 DEFINE_SIZE_ARRAY (8, dataSets);
1802 * Feature Variations
1805 struct ConditionFormat1
1807 friend struct Condition;
1810 bool evaluate (const int *coords, unsigned int coord_len) const
1812 int coord = axisIndex < coord_len ? coords[axisIndex] : 0;
1813 return filterRangeMinValue <= coord && coord <= filterRangeMaxValue;
1816 bool sanitize (hb_sanitize_context_t *c) const
1818 TRACE_SANITIZE (this);
1819 return_trace (c->check_struct (this));
1823 HBUINT16 format; /* Format identifier--format = 1 */
1825 F2DOT14 filterRangeMinValue;
1826 F2DOT14 filterRangeMaxValue;
1828 DEFINE_SIZE_STATIC (8);
1833 bool evaluate (const int *coords, unsigned int coord_len) const
1836 case 1: return u.format1.evaluate (coords, coord_len);
1837 default:return false;
1841 bool sanitize (hb_sanitize_context_t *c) const
1843 TRACE_SANITIZE (this);
1844 if (!u.format.sanitize (c)) return_trace (false);
1846 case 1: return_trace (u.format1.sanitize (c));
1847 default:return_trace (true);
1853 HBUINT16 format; /* Format identifier */
1854 ConditionFormat1 format1;
1857 DEFINE_SIZE_UNION (2, format);
1862 bool evaluate (const int *coords, unsigned int coord_len) const
1864 unsigned int count = conditions.len;
1865 for (unsigned int i = 0; i < count; i++)
1866 if (!(this+conditions.arrayZ[i]).evaluate (coords, coord_len))
1871 bool sanitize (hb_sanitize_context_t *c) const
1873 TRACE_SANITIZE (this);
1874 return_trace (conditions.sanitize (c, this));
1878 LOffsetArrayOf<Condition> conditions;
1880 DEFINE_SIZE_ARRAY (2, conditions);
1883 struct FeatureTableSubstitutionRecord
1885 friend struct FeatureTableSubstitution;
1887 bool sanitize (hb_sanitize_context_t *c, const void *base) const
1889 TRACE_SANITIZE (this);
1890 return_trace (c->check_struct (this) && feature.sanitize (c, base));
1894 HBUINT16 featureIndex;
1895 LOffsetTo<Feature> feature;
1897 DEFINE_SIZE_STATIC (6);
1900 struct FeatureTableSubstitution
1902 const Feature *find_substitute (unsigned int feature_index) const
1904 unsigned int count = substitutions.len;
1905 for (unsigned int i = 0; i < count; i++)
1907 const FeatureTableSubstitutionRecord &record = substitutions.arrayZ[i];
1908 if (record.featureIndex == feature_index)
1909 return &(this+record.feature);
1914 bool sanitize (hb_sanitize_context_t *c) const
1916 TRACE_SANITIZE (this);
1917 return_trace (version.sanitize (c) &&
1918 likely (version.major == 1) &&
1919 substitutions.sanitize (c, this));
1923 FixedVersion<> version; /* Version--0x00010000u */
1924 ArrayOf<FeatureTableSubstitutionRecord>
1927 DEFINE_SIZE_ARRAY (6, substitutions);
1930 struct FeatureVariationRecord
1932 friend struct FeatureVariations;
1934 bool sanitize (hb_sanitize_context_t *c, const void *base) const
1936 TRACE_SANITIZE (this);
1937 return_trace (conditions.sanitize (c, base) &&
1938 substitutions.sanitize (c, base));
1942 LOffsetTo<ConditionSet>
1944 LOffsetTo<FeatureTableSubstitution>
1947 DEFINE_SIZE_STATIC (8);
1950 struct FeatureVariations
1952 static constexpr unsigned NOT_FOUND_INDEX = 0xFFFFFFFFu;
1954 bool find_index (const int *coords, unsigned int coord_len,
1955 unsigned int *index) const
1957 unsigned int count = varRecords.len;
1958 for (unsigned int i = 0; i < count; i++)
1960 const FeatureVariationRecord &record = varRecords.arrayZ[i];
1961 if ((this+record.conditions).evaluate (coords, coord_len))
1967 *index = NOT_FOUND_INDEX;
1971 const Feature *find_substitute (unsigned int variations_index,
1972 unsigned int feature_index) const
1974 const FeatureVariationRecord &record = varRecords[variations_index];
1975 return (this+record.substitutions).find_substitute (feature_index);
1978 bool subset (hb_subset_context_t *c) const
1980 TRACE_SUBSET (this);
1981 return_trace (c->serializer->embed (*this));
1984 bool sanitize (hb_sanitize_context_t *c) const
1986 TRACE_SANITIZE (this);
1987 return_trace (version.sanitize (c) &&
1988 likely (version.major == 1) &&
1989 varRecords.sanitize (c, this));
1993 FixedVersion<> version; /* Version--0x00010000u */
1994 LArrayOf<FeatureVariationRecord>
1997 DEFINE_SIZE_ARRAY_SIZED (8, varRecords);
2005 struct HintingDevice
2007 friend struct Device;
2011 hb_position_t get_x_delta (hb_font_t *font) const
2012 { return get_delta (font->x_ppem, font->x_scale); }
2014 hb_position_t get_y_delta (hb_font_t *font) const
2015 { return get_delta (font->y_ppem, font->y_scale); }
2017 unsigned int get_size () const
2019 unsigned int f = deltaFormat;
2020 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * HBUINT16::static_size;
2021 return HBUINT16::static_size * (4 + ((endSize - startSize) >> (4 - f)));
2024 bool sanitize (hb_sanitize_context_t *c) const
2026 TRACE_SANITIZE (this);
2027 return_trace (c->check_struct (this) && c->check_range (this, this->get_size ()));
2032 int get_delta (unsigned int ppem, int scale) const
2034 if (!ppem) return 0;
2036 int pixels = get_delta_pixels (ppem);
2038 if (!pixels) return 0;
2040 return (int) (pixels * (int64_t) scale / ppem);
2042 int get_delta_pixels (unsigned int ppem_size) const
2044 unsigned int f = deltaFormat;
2045 if (unlikely (f < 1 || f > 3))
2048 if (ppem_size < startSize || ppem_size > endSize)
2051 unsigned int s = ppem_size - startSize;
2053 unsigned int byte = deltaValueZ[s >> (4 - f)];
2054 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
2055 unsigned int mask = (0xFFFFu >> (16 - (1 << f)));
2057 int delta = bits & mask;
2059 if ((unsigned int) delta >= ((mask + 1) >> 1))
2066 HBUINT16 startSize; /* Smallest size to correct--in ppem */
2067 HBUINT16 endSize; /* Largest size to correct--in ppem */
2068 HBUINT16 deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
2069 * 1 Signed 2-bit value, 8 values per uint16
2070 * 2 Signed 4-bit value, 4 values per uint16
2071 * 3 Signed 8-bit value, 2 values per uint16
2073 UnsizedArrayOf<HBUINT16>
2074 deltaValueZ; /* Array of compressed data */
2076 DEFINE_SIZE_ARRAY (6, deltaValueZ);
2079 struct VariationDevice
2081 friend struct Device;
2085 hb_position_t get_x_delta (hb_font_t *font, const VariationStore &store) const
2086 { return font->em_scalef_x (get_delta (font, store)); }
2088 hb_position_t get_y_delta (hb_font_t *font, const VariationStore &store) const
2089 { return font->em_scalef_y (get_delta (font, store)); }
2091 bool sanitize (hb_sanitize_context_t *c) const
2093 TRACE_SANITIZE (this);
2094 return_trace (c->check_struct (this));
2099 float get_delta (hb_font_t *font, const VariationStore &store) const
2101 return store.get_delta (outerIndex, innerIndex, font->coords, font->num_coords);
2105 HBUINT16 outerIndex;
2106 HBUINT16 innerIndex;
2107 HBUINT16 deltaFormat; /* Format identifier for this table: 0x0x8000 */
2109 DEFINE_SIZE_STATIC (6);
2118 HBUINT16 format; /* Format identifier */
2120 DEFINE_SIZE_STATIC (6);
2125 hb_position_t get_x_delta (hb_font_t *font, const VariationStore &store=Null (VariationStore)) const
2129 case 1: case 2: case 3:
2130 return u.hinting.get_x_delta (font);
2132 return u.variation.get_x_delta (font, store);
2137 hb_position_t get_y_delta (hb_font_t *font, const VariationStore &store=Null (VariationStore)) const
2141 case 1: case 2: case 3:
2142 return u.hinting.get_y_delta (font);
2144 return u.variation.get_y_delta (font, store);
2150 bool sanitize (hb_sanitize_context_t *c) const
2152 TRACE_SANITIZE (this);
2153 if (!u.b.format.sanitize (c)) return_trace (false);
2154 switch (u.b.format) {
2155 case 1: case 2: case 3:
2156 return_trace (u.hinting.sanitize (c));
2158 return_trace (u.variation.sanitize (c));
2160 return_trace (true);
2167 HintingDevice hinting;
2168 VariationDevice variation;
2171 DEFINE_SIZE_UNION (6, b);
2175 } /* namespace OT */
2178 #endif /* HB_OT_LAYOUT_COMMON_HH */