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_PRIVATE_HH
30 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
32 #include "hb-ot-layout-private.hh"
33 #include "hb-open-type-private.hh"
34 #include "hb-set-private.hh"
40 #define NOT_COVERED ((unsigned int) -1)
41 #define MAX_NESTING_LEVEL 8
47 * OpenType Layout Common Table Formats
53 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
56 template <typename Type>
59 inline int cmp (hb_tag_t a) const {
63 struct sanitize_closure_t {
67 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
68 TRACE_SANITIZE (this);
69 const sanitize_closure_t closure = {tag, base};
70 return TRACE_RETURN (c->check_struct (this) && offset.sanitize (c, base, &closure));
73 Tag tag; /* 4-byte Tag identifier */
75 offset; /* Offset from beginning of object holding
78 DEFINE_SIZE_STATIC (6);
81 template <typename Type>
82 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
83 inline const Tag& get_tag (unsigned int i) const
85 /* We cheat slightly and don't define separate Null objects
86 * for Record types. Instead, we return the correct Null(Tag)
88 if (unlikely (i >= this->len)) return Null(Tag);
89 return (*this)[i].tag;
91 inline unsigned int get_tags (unsigned int start_offset,
92 unsigned int *record_count /* IN/OUT */,
93 hb_tag_t *record_tags /* OUT */) const
96 const Record<Type> *arr = this->sub_array (start_offset, record_count);
97 unsigned int count = *record_count;
98 for (unsigned int i = 0; i < count; i++)
99 record_tags[i] = arr[i].tag;
103 inline bool find_index (hb_tag_t tag, unsigned int *index) const
105 int i = this->search (tag);
107 if (index) *index = i;
110 if (index) *index = Index::NOT_FOUND_INDEX;
116 template <typename Type>
117 struct RecordListOf : RecordArrayOf<Type>
119 inline const Type& operator [] (unsigned int i) const
120 { return this+RecordArrayOf<Type>::operator [](i).offset; }
122 inline bool sanitize (hb_sanitize_context_t *c) {
123 TRACE_SANITIZE (this);
124 return TRACE_RETURN (RecordArrayOf<Type>::sanitize (c, this));
131 inline int cmp (hb_codepoint_t g) const {
132 return g < start ? -1 : g <= end ? 0 : +1 ;
135 inline bool sanitize (hb_sanitize_context_t *c) {
136 TRACE_SANITIZE (this);
137 return TRACE_RETURN (c->check_struct (this));
140 inline bool intersects (const hb_set_t *glyphs) const {
141 return glyphs->intersects (start, end);
144 template <typename set_t>
145 inline void add_coverage (set_t *glyphs) const {
146 glyphs->add_range (start, end);
149 GlyphID start; /* First GlyphID in the range */
150 GlyphID end; /* Last GlyphID in the range */
151 USHORT value; /* Value */
153 DEFINE_SIZE_STATIC (6);
155 DEFINE_NULL_DATA (RangeRecord, "\000\001");
158 struct IndexArray : ArrayOf<Index>
160 inline unsigned int get_indexes (unsigned int start_offset,
161 unsigned int *_count /* IN/OUT */,
162 unsigned int *_indexes /* OUT */) const
165 const USHORT *arr = this->sub_array (start_offset, _count);
166 unsigned int count = *_count;
167 for (unsigned int i = 0; i < count; i++)
168 _indexes[i] = arr[i];
182 inline unsigned int get_feature_count (void) const
183 { return featureIndex.len; }
184 inline hb_tag_t get_feature_index (unsigned int i) const
185 { return featureIndex[i]; }
186 inline unsigned int get_feature_indexes (unsigned int start_offset,
187 unsigned int *feature_count /* IN/OUT */,
188 unsigned int *feature_indexes /* OUT */) const
189 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
191 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
192 inline unsigned int get_required_feature_index (void) const
194 if (reqFeatureIndex == 0xffff)
195 return Index::NOT_FOUND_INDEX;
196 return reqFeatureIndex;;
199 inline bool sanitize (hb_sanitize_context_t *c,
200 const Record<LangSys>::sanitize_closure_t * = NULL) {
201 TRACE_SANITIZE (this);
202 return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c));
205 Offset lookupOrder; /* = Null (reserved for an offset to a
206 * reordering table) */
207 USHORT reqFeatureIndex;/* Index of a feature required for this
208 * language system--if no required features
210 IndexArray featureIndex; /* Array of indices into the FeatureList */
212 DEFINE_SIZE_ARRAY (6, featureIndex);
214 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
219 inline unsigned int get_lang_sys_count (void) const
220 { return langSys.len; }
221 inline const Tag& get_lang_sys_tag (unsigned int i) const
222 { return langSys.get_tag (i); }
223 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
224 unsigned int *lang_sys_count /* IN/OUT */,
225 hb_tag_t *lang_sys_tags /* OUT */) const
226 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
227 inline const LangSys& get_lang_sys (unsigned int i) const
229 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
230 return this+langSys[i].offset;
232 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
233 { return langSys.find_index (tag, index); }
235 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
236 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
238 inline bool sanitize (hb_sanitize_context_t *c,
239 const Record<Script>::sanitize_closure_t * = NULL) {
240 TRACE_SANITIZE (this);
241 return TRACE_RETURN (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
246 defaultLangSys; /* Offset to DefaultLangSys table--from
247 * beginning of Script table--may be Null */
248 RecordArrayOf<LangSys>
249 langSys; /* Array of LangSysRecords--listed
250 * alphabetically by LangSysTag */
252 DEFINE_SIZE_ARRAY (4, langSys);
255 typedef RecordListOf<Script> ScriptList;
258 /* http://www.microsoft.com/typography/otspec/features_pt.htm#size */
259 struct FeatureParamsSize
261 inline bool sanitize (hb_sanitize_context_t *c) {
262 TRACE_SANITIZE (this);
263 if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
265 /* This subtable has some "history", if you will. Some earlier versions of
266 * Adobe tools calculated the offset of the FeatureParams sutable from the
267 * beginning of the FeatureList table! Now, that is dealt with in the
268 * Feature implementation. But we still need to be able to tell junk from
269 * real data. Note: We don't check that the nameID actually exists.
271 * Read Roberts wrote on 9/15/06 on opentype-list@indx.co.uk :
273 * Yes, it is correct that a new version of the AFDKO (version 2.0) will be
274 * coming out soon, and that the makeotf program will build a font with a
275 * 'size' feature that is correct by the specification.
277 * The specification for this feature tag is in the "OpenType Layout Tag
278 * Registry". You can see a copy of this at:
279 * http://partners.adobe.com/public/developer/opentype/index_tag8.html#size
281 * Here is one set of rules to determine if the 'size' feature is built
282 * correctly, or as by the older versions of MakeOTF. You may be able to do
285 * Assume that the offset to the size feature is according to specification,
286 * and make the following value checks. If it fails, assume the the size
287 * feature is calculated as versions of MakeOTF before the AFDKO 2.0 built it.
288 * If this fails, reject the 'size' feature. The older makeOTF's calculated the
289 * offset from the beginning of the FeatureList table, rather than from the
290 * beginning of the 'size' Feature table.
292 * If "design size" == 0:
295 * Else if ("subfamily identifier" == 0 and
296 * "range start" == 0 and
297 * "range end" == 0 and
298 * "range start" == 0 and
299 * "menu name ID" == 0)
300 * passes check: this is the format used when there is a design size
301 * specified, but there is no recommended size range.
303 * Else if ("design size" < "range start" or
304 * "design size" > "range end" or
305 * "range end" <= "range start" or
306 * "menu name ID" < 256 or
307 * "menu name ID" > 32767 or
308 * menu name ID is not a name ID which is actually in the name table)
315 return TRACE_RETURN (false);
316 else if (subfamilyID == 0 &&
317 subfamilyNameID == 0 &&
320 return TRACE_RETURN (true);
321 else if (designSize < rangeStart ||
322 designSize > rangeEnd ||
323 subfamilyNameID < 256 ||
324 subfamilyNameID > 32767)
325 return TRACE_RETURN (false);
327 return TRACE_RETURN (true);
330 USHORT designSize; /* Represents the design size in 720/inch
331 * units (decipoints). The design size entry
332 * must be non-zero. When there is a design
333 * size but no recommended size range, the
334 * rest of the array will consist of zeros. */
335 USHORT subfamilyID; /* Has no independent meaning, but serves
336 * as an identifier that associates fonts
337 * in a subfamily. All fonts which share a
338 * Preferred or Font Family name and which
339 * differ only by size range shall have the
340 * same subfamily value, and no fonts which
341 * differ in weight or style shall have the
342 * same subfamily value. If this value is
343 * zero, the remaining fields in the array
344 * will be ignored. */
345 USHORT subfamilyNameID;/* If the preceding value is non-zero, this
346 * value must be set in the range 256 - 32767
347 * (inclusive). It records the value of a
348 * field in the name table, which must
349 * contain English-language strings encoded
350 * in Windows Unicode and Macintosh Roman,
351 * and may contain additional strings
352 * localized to other scripts and languages.
353 * Each of these strings is the name an
354 * application should use, in combination
355 * with the family name, to represent the
356 * subfamily in a menu. Applications will
357 * choose the appropriate version based on
358 * their selection criteria. */
359 USHORT rangeStart; /* Large end of the recommended usage range
360 * (inclusive), stored in 720/inch units
362 USHORT rangeEnd; /* Small end of the recommended usage range
363 (exclusive), stored in 720/inch units
366 DEFINE_SIZE_STATIC (10);
369 /* http://www.microsoft.com/typography/otspec/features_pt.htm#ssxx */
370 struct FeatureParamsStylisticSet
372 inline bool sanitize (hb_sanitize_context_t *c) {
373 TRACE_SANITIZE (this);
374 /* Right now minorVersion is at zero. Which means, any table supports
375 * the uiNameID field. */
376 return TRACE_RETURN (c->check_struct (this));
379 USHORT minorVersion; /* (set to 0): This corresponds to a “minor”
380 * version number. Additional data may be
381 * added to the end of this Feature Parameters
382 * table in the future. */
384 USHORT uiNameID; /* The 'name' table name ID that specifies a
385 * string (or strings, for multiple languages)
386 * for a user-interface label for this
387 * feature. The values of uiLabelNameId and
388 * sampleTextNameId are expected to be in the
389 * font-specific name ID range (256-32767),
390 * though that is not a requirement in this
391 * Feature Parameters specification. The
392 * user-interface label for the feature can
393 * be provided in multiple languages. An
394 * English string should be included as a
395 * fallback. The string should be kept to a
396 * minimal length to fit comfortably with
397 * different application interfaces. */
399 DEFINE_SIZE_STATIC (4);
402 struct FeatureParamsCharacterVariants
404 inline bool sanitize (hb_sanitize_context_t *c) {
405 TRACE_SANITIZE (this);
406 return TRACE_RETURN (c->check_struct (this) &&
407 characters.sanitize (c));
410 USHORT format; /* Format number is set to 0. */
411 USHORT featUILableNameID; /* The ‘name’ table name ID that
412 * specifies a string (or strings,
413 * for multiple languages) for a
414 * user-interface label for this
415 * feature. (May be NULL.) */
416 USHORT featUITooltipTextNameID;/* The ‘name’ table name ID that
417 * specifies a string (or strings,
418 * for multiple languages) that an
419 * application can use for tooltip
420 * text for this feature. (May be
422 USHORT sampleTextNameID; /* The ‘name’ table name ID that
423 * specifies sample text that
424 * illustrates the effect of this
425 * feature. (May be NULL.) */
426 USHORT numNamedParameters; /* Number of named parameters. (May
428 USHORT firstParamUILabelNameID;/* The first ‘name’ table name ID
429 * used to specify strings for
430 * user-interface labels for the
431 * feature parameters. (Must be zero
432 * if numParameters is zero.) */
434 characters; /* Array of the Unicode Scalar Value
435 * of the characters for which this
436 * feature provides glyph variants.
439 DEFINE_SIZE_ARRAY (14, characters);
444 inline bool sanitize (hb_sanitize_context_t *c, hb_tag_t tag) {
445 TRACE_SANITIZE (this);
446 if (tag == HB_TAG ('s','i','z','e'))
447 return TRACE_RETURN (u.size.sanitize (c));
448 if ((tag & 0xFFFF0000) == HB_TAG ('s','s','\0','\0')) /* ssXX */
449 return TRACE_RETURN (u.stylisticSet.sanitize (c));
450 if ((tag & 0xFFFF0000) == HB_TAG ('c','v','\0','\0')) /* cvXX */
451 return TRACE_RETURN (u.characterVariants.sanitize (c));
452 return TRACE_RETURN (true);
455 inline const FeatureParamsSize& get_size_params (hb_tag_t tag) const
457 if (tag == HB_TAG ('s','i','z','e'))
459 return Null(FeatureParamsSize);
464 FeatureParamsSize size;
465 FeatureParamsStylisticSet stylisticSet;
466 FeatureParamsCharacterVariants characterVariants;
468 DEFINE_SIZE_STATIC (17);
473 inline unsigned int get_lookup_count (void) const
474 { return lookupIndex.len; }
475 inline hb_tag_t get_lookup_index (unsigned int i) const
476 { return lookupIndex[i]; }
477 inline unsigned int get_lookup_indexes (unsigned int start_index,
478 unsigned int *lookup_count /* IN/OUT */,
479 unsigned int *lookup_tags /* OUT */) const
480 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
482 inline const FeatureParams &get_feature_params (void) const
483 { return this+featureParams; }
485 inline bool sanitize (hb_sanitize_context_t *c,
486 const Record<Feature>::sanitize_closure_t *closure) {
487 TRACE_SANITIZE (this);
488 if (unlikely (!(c->check_struct (this) && lookupIndex.sanitize (c))))
489 return TRACE_RETURN (false);
491 /* Some earlier versions of Adobe tools calculated the offset of the
492 * FeatureParams subtable from the beginning of the FeatureList table!
494 * If sanitizing "failed" for the FeatureParams subtable, try it with the
495 * alternative location. We would know sanitize "failed" if old value
496 * of the offset was non-zero, but it's zeroed now.
498 * Only do this for the 'size' feature, since at the time of the faulty
499 * Adobe tools, only the 'size' feature had FeatureParams defined.
502 Offset orig_offset = featureParams;
503 if (unlikely (!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE)))
504 return TRACE_RETURN (false);
506 if (likely (!orig_offset))
507 return TRACE_RETURN (true);
509 if (featureParams == 0 && closure &&
510 closure->tag == HB_TAG ('s','i','z','e') &&
511 closure->list_base && closure->list_base < this)
513 unsigned int new_offset_int = (unsigned int) orig_offset -
514 ((char *) this - (char *) closure->list_base);
517 /* Check that it did not overflow. */
518 new_offset.set (new_offset_int);
519 if (new_offset == new_offset_int &&
520 featureParams.try_set (c, new_offset) &&
521 !featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE))
522 return TRACE_RETURN (false);
525 return TRACE_RETURN (true);
528 OffsetTo<FeatureParams>
529 featureParams; /* Offset to Feature Parameters table (if one
530 * has been defined for the feature), relative
531 * to the beginning of the Feature Table; = Null
533 IndexArray lookupIndex; /* Array of LookupList indices */
535 DEFINE_SIZE_ARRAY (4, lookupIndex);
538 typedef RecordListOf<Feature> FeatureList;
541 struct LookupFlag : USHORT
544 RightToLeft = 0x0001u,
545 IgnoreBaseGlyphs = 0x0002u,
546 IgnoreLigatures = 0x0004u,
547 IgnoreMarks = 0x0008u,
548 IgnoreFlags = 0x000Eu,
549 UseMarkFilteringSet = 0x0010u,
551 MarkAttachmentType = 0xFF00u
554 DEFINE_SIZE_STATIC (2);
559 inline unsigned int get_subtable_count (void) const { return subTable.len; }
561 inline unsigned int get_type (void) const { return lookupType; }
563 /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
564 * higher 16-bit is mark-filtering-set if the lookup uses one.
565 * Not to be confused with glyph_props which is very similar. */
566 inline uint32_t get_props (void) const
568 unsigned int flag = lookupFlag;
569 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
571 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
572 flag += (markFilteringSet << 16);
577 inline bool serialize (hb_serialize_context_t *c,
578 unsigned int lookup_type,
579 uint32_t lookup_props,
580 unsigned int num_subtables)
582 TRACE_SERIALIZE (this);
583 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
584 lookupType.set (lookup_type);
585 lookupFlag.set (lookup_props & 0xFFFF);
586 if (unlikely (!subTable.serialize (c, num_subtables))) return TRACE_RETURN (false);
587 if (lookupFlag & LookupFlag::UseMarkFilteringSet)
589 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
590 markFilteringSet.set (lookup_props >> 16);
592 return TRACE_RETURN (true);
595 inline bool sanitize (hb_sanitize_context_t *c) {
596 TRACE_SANITIZE (this);
597 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
598 if (!(c->check_struct (this) && subTable.sanitize (c))) return TRACE_RETURN (false);
599 if (lookupFlag & LookupFlag::UseMarkFilteringSet)
601 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
602 if (!markFilteringSet.sanitize (c)) return TRACE_RETURN (false);
604 return TRACE_RETURN (true);
607 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
608 USHORT lookupFlag; /* Lookup qualifiers */
610 subTable; /* Array of SubTables */
611 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
612 * structure. This field is only present if bit
613 * UseMarkFilteringSet of lookup flags is set. */
615 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
618 typedef OffsetListOf<Lookup> LookupList;
625 struct CoverageFormat1
627 friend struct Coverage;
630 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
632 int i = glyphArray.search (glyph_id);
633 ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
637 inline bool serialize (hb_serialize_context_t *c,
638 Supplier<GlyphID> &glyphs,
639 unsigned int num_glyphs)
641 TRACE_SERIALIZE (this);
642 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
643 glyphArray.len.set (num_glyphs);
644 if (unlikely (!c->extend (glyphArray))) return TRACE_RETURN (false);
645 for (unsigned int i = 0; i < num_glyphs; i++)
646 glyphArray[i] = glyphs[i];
647 glyphs.advance (num_glyphs);
648 return TRACE_RETURN (true);
651 inline bool sanitize (hb_sanitize_context_t *c) {
652 TRACE_SANITIZE (this);
653 return TRACE_RETURN (glyphArray.sanitize (c));
656 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
657 return glyphs->has (glyphArray[index]);
660 template <typename set_t>
661 inline void add_coverage (set_t *glyphs) const {
662 unsigned int count = glyphArray.len;
663 for (unsigned int i = 0; i < count; i++)
664 glyphs->add (glyphArray[i]);
668 /* Older compilers need this to be public. */
670 inline void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; };
671 inline bool more (void) { return i < c->glyphArray.len; }
672 inline void next (void) { i++; }
673 inline uint16_t get_glyph (void) { return c->glyphArray[i]; }
674 inline uint16_t get_coverage (void) { return i; }
677 const struct CoverageFormat1 *c;
683 USHORT coverageFormat; /* Format identifier--format = 1 */
684 SortedArrayOf<GlyphID>
685 glyphArray; /* Array of GlyphIDs--in numerical order */
687 DEFINE_SIZE_ARRAY (4, glyphArray);
690 struct CoverageFormat2
692 friend struct Coverage;
695 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
697 int i = rangeRecord.search (glyph_id);
699 const RangeRecord &range = rangeRecord[i];
700 return (unsigned int) range.value + (glyph_id - range.start);
705 inline bool serialize (hb_serialize_context_t *c,
706 Supplier<GlyphID> &glyphs,
707 unsigned int num_glyphs)
709 TRACE_SERIALIZE (this);
710 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
712 if (unlikely (!num_glyphs)) return TRACE_RETURN (true);
714 unsigned int num_ranges = 1;
715 for (unsigned int i = 1; i < num_glyphs; i++)
716 if (glyphs[i - 1] + 1 != glyphs[i])
718 rangeRecord.len.set (num_ranges);
719 if (unlikely (!c->extend (rangeRecord))) return TRACE_RETURN (false);
721 unsigned int range = 0;
722 rangeRecord[range].start = glyphs[0];
723 rangeRecord[range].value.set (0);
724 for (unsigned int i = 1; i < num_glyphs; i++)
725 if (glyphs[i - 1] + 1 != glyphs[i]) {
727 rangeRecord[range].start = glyphs[i];
728 rangeRecord[range].value.set (i);
729 rangeRecord[range].end = glyphs[i];
731 rangeRecord[range].end = glyphs[i];
733 glyphs.advance (num_glyphs);
734 return TRACE_RETURN (true);
737 inline bool sanitize (hb_sanitize_context_t *c) {
738 TRACE_SANITIZE (this);
739 return TRACE_RETURN (rangeRecord.sanitize (c));
742 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
744 unsigned int count = rangeRecord.len;
745 for (i = 0; i < count; i++) {
746 const RangeRecord &range = rangeRecord[i];
747 if (range.value <= index &&
748 index < (unsigned int) range.value + (range.end - range.start) &&
749 range.intersects (glyphs))
751 else if (index < range.value)
757 template <typename set_t>
758 inline void add_coverage (set_t *glyphs) const {
759 unsigned int count = rangeRecord.len;
760 for (unsigned int i = 0; i < count; i++)
761 rangeRecord[i].add_coverage (glyphs);
765 /* Older compilers need this to be public. */
767 inline void init (const CoverageFormat2 &c_) {
771 j = c->rangeRecord.len ? c_.rangeRecord[0].start : 0;
773 inline bool more (void) { return i < c->rangeRecord.len; }
774 inline void next (void) {
776 if (j == c->rangeRecord[i].end) {
779 j = c->rangeRecord[i].start;
784 inline uint16_t get_glyph (void) { return j; }
785 inline uint16_t get_coverage (void) { return coverage; }
788 const struct CoverageFormat2 *c;
789 unsigned int i, j, coverage;
794 USHORT coverageFormat; /* Format identifier--format = 2 */
795 SortedArrayOf<RangeRecord>
796 rangeRecord; /* Array of glyph ranges--ordered by
797 * Start GlyphID. rangeCount entries
800 DEFINE_SIZE_ARRAY (4, rangeRecord);
805 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
808 case 1: return u.format1.get_coverage(glyph_id);
809 case 2: return u.format2.get_coverage(glyph_id);
810 default:return NOT_COVERED;
814 inline bool serialize (hb_serialize_context_t *c,
815 Supplier<GlyphID> &glyphs,
816 unsigned int num_glyphs)
818 TRACE_SERIALIZE (this);
819 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
820 unsigned int num_ranges = 1;
821 for (unsigned int i = 1; i < num_glyphs; i++)
822 if (glyphs[i - 1] + 1 != glyphs[i])
824 u.format.set (num_glyphs * 2 < num_ranges * 3 ? 1 : 2);
826 case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs));
827 case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, num_glyphs));
828 default:return TRACE_RETURN (false);
832 inline bool sanitize (hb_sanitize_context_t *c) {
833 TRACE_SANITIZE (this);
834 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
836 case 1: return TRACE_RETURN (u.format1.sanitize (c));
837 case 2: return TRACE_RETURN (u.format2.sanitize (c));
838 default:return TRACE_RETURN (true);
842 inline bool intersects (const hb_set_t *glyphs) const {
843 /* TODO speed this up */
845 for (iter.init (*this); iter.more (); iter.next ()) {
846 if (glyphs->has (iter.get_glyph ()))
852 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
854 case 1: return u.format1.intersects_coverage (glyphs, index);
855 case 2: return u.format2.intersects_coverage (glyphs, index);
856 default:return false;
860 template <typename set_t>
861 inline void add_coverage (set_t *glyphs) const {
863 case 1: u.format1.add_coverage (glyphs); break;
864 case 2: u.format2.add_coverage (glyphs); break;
870 Iter (void) : format (0) {};
871 inline void init (const Coverage &c_) {
872 format = c_.u.format;
874 case 1: return u.format1.init (c_.u.format1);
875 case 2: return u.format2.init (c_.u.format2);
879 inline bool more (void) {
881 case 1: return u.format1.more ();
882 case 2: return u.format2.more ();
886 inline void next (void) {
888 case 1: u.format1.next (); break;
889 case 2: u.format2.next (); break;
893 inline uint16_t get_glyph (void) {
895 case 1: return u.format1.get_glyph ();
896 case 2: return u.format2.get_glyph ();
900 inline uint16_t get_coverage (void) {
902 case 1: return u.format1.get_coverage ();
903 case 2: return u.format2.get_coverage ();
911 CoverageFormat1::Iter format1;
912 CoverageFormat2::Iter format2;
918 USHORT format; /* Format identifier */
919 CoverageFormat1 format1;
920 CoverageFormat2 format2;
923 DEFINE_SIZE_UNION (2, format);
928 * Class Definition Table
931 struct ClassDefFormat1
933 friend struct ClassDef;
936 inline unsigned int get_class (hb_codepoint_t glyph_id) const
938 if (unlikely ((unsigned int) (glyph_id - startGlyph) < classValue.len))
939 return classValue[glyph_id - startGlyph];
943 inline bool sanitize (hb_sanitize_context_t *c) {
944 TRACE_SANITIZE (this);
945 return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
948 template <typename set_t>
949 inline void add_class (set_t *glyphs, unsigned int klass) const {
950 unsigned int count = classValue.len;
951 for (unsigned int i = 0; i < count; i++)
952 if (classValue[i] == klass)
953 glyphs->add (startGlyph + i);
956 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
957 unsigned int count = classValue.len;
958 for (unsigned int i = 0; i < count; i++)
959 if (classValue[i] == klass && glyphs->has (startGlyph + i))
965 USHORT classFormat; /* Format identifier--format = 1 */
966 GlyphID startGlyph; /* First GlyphID of the classValueArray */
968 classValue; /* Array of Class Values--one per GlyphID */
970 DEFINE_SIZE_ARRAY (6, classValue);
973 struct ClassDefFormat2
975 friend struct ClassDef;
978 inline unsigned int get_class (hb_codepoint_t glyph_id) const
980 int i = rangeRecord.search (glyph_id);
982 return rangeRecord[i].value;
986 inline bool sanitize (hb_sanitize_context_t *c) {
987 TRACE_SANITIZE (this);
988 return TRACE_RETURN (rangeRecord.sanitize (c));
991 template <typename set_t>
992 inline void add_class (set_t *glyphs, unsigned int klass) const {
993 unsigned int count = rangeRecord.len;
994 for (unsigned int i = 0; i < count; i++)
995 if (rangeRecord[i].value == klass)
996 rangeRecord[i].add_coverage (glyphs);
999 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
1000 unsigned int count = rangeRecord.len;
1001 for (unsigned int i = 0; i < count; i++)
1002 if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
1008 USHORT classFormat; /* Format identifier--format = 2 */
1009 SortedArrayOf<RangeRecord>
1010 rangeRecord; /* Array of glyph ranges--ordered by
1013 DEFINE_SIZE_ARRAY (4, rangeRecord);
1018 inline unsigned int get_class (hb_codepoint_t glyph_id) const
1021 case 1: return u.format1.get_class(glyph_id);
1022 case 2: return u.format2.get_class(glyph_id);
1027 inline bool sanitize (hb_sanitize_context_t *c) {
1028 TRACE_SANITIZE (this);
1029 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1031 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1032 case 2: return TRACE_RETURN (u.format2.sanitize (c));
1033 default:return TRACE_RETURN (true);
1037 inline void add_class (hb_set_t *glyphs, unsigned int klass) const {
1039 case 1: u.format1.add_class (glyphs, klass); return;
1040 case 2: u.format2.add_class (glyphs, klass); return;
1045 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
1047 case 1: return u.format1.intersects_class (glyphs, klass);
1048 case 2: return u.format2.intersects_class (glyphs, klass);
1049 default:return false;
1055 USHORT format; /* Format identifier */
1056 ClassDefFormat1 format1;
1057 ClassDefFormat2 format2;
1060 DEFINE_SIZE_UNION (2, format);
1071 inline hb_position_t get_x_delta (hb_font_t *font) const
1072 { return get_delta (font->x_ppem, font->x_scale); }
1074 inline hb_position_t get_y_delta (hb_font_t *font) const
1075 { return get_delta (font->y_ppem, font->y_scale); }
1077 inline int get_delta (unsigned int ppem, int scale) const
1079 if (!ppem) return 0;
1081 int pixels = get_delta_pixels (ppem);
1083 if (!pixels) return 0;
1085 return pixels * (int64_t) scale / ppem;
1089 inline int get_delta_pixels (unsigned int ppem_size) const
1091 unsigned int f = deltaFormat;
1092 if (unlikely (f < 1 || f > 3))
1095 if (ppem_size < startSize || ppem_size > endSize)
1098 unsigned int s = ppem_size - startSize;
1100 unsigned int byte = deltaValue[s >> (4 - f)];
1101 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
1102 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
1104 int delta = bits & mask;
1106 if ((unsigned int) delta >= ((mask + 1) >> 1))
1112 inline unsigned int get_size (void) const
1114 unsigned int f = deltaFormat;
1115 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
1116 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
1119 inline bool sanitize (hb_sanitize_context_t *c) {
1120 TRACE_SANITIZE (this);
1121 return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
1125 USHORT startSize; /* Smallest size to correct--in ppem */
1126 USHORT endSize; /* Largest size to correct--in ppem */
1127 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
1128 * 1 Signed 2-bit value, 8 values per uint16
1129 * 2 Signed 4-bit value, 4 values per uint16
1130 * 3 Signed 8-bit value, 2 values per uint16
1132 USHORT deltaValue[VAR]; /* Array of compressed data */
1134 DEFINE_SIZE_ARRAY (6, deltaValue);
1138 } /* namespace OT */
1141 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */