[HB] Simplify some basic things
diff --git a/src/hb-ot-layout-open-private.h b/src/hb-ot-layout-open-private.h index 2dabffd..65113bb 100644 --- a/src/hb-ot-layout-open-private.h +++ b/src/hb-ot-layout-open-private.h
@@ -179,7 +179,7 @@ { \ if (HB_UNLIKELY (data == NULL)) return Null(Type); \ const Type& t = (const Type&)*data; \ - if (HB_UNLIKELY (t.version.major != Major)) return Null(Type); \ + if (HB_UNLIKELY (!t.version.major || t.version.major > Major)) return Null(Type); \ return t; \ } @@ -251,12 +251,16 @@ #undef _NULL_TAG_INIT /* Glyph index number, same as uint16 (length = 16 bits) */ -DEFINE_INT_TYPE_STRUCT (GlyphID, u, 16); +typedef USHORT GlyphID; /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ -DEFINE_INT_TYPE_STRUCT (Offset, u, 16); +typedef USHORT Offset; -/* Template subclass of Offset that does the dereferencing. Use: (this+memberName) */ +/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ +typedef ULONG LongOffset; + +/* Template subclasses of Offset and LongOffset that do the dereferencing. Use: (this+memberName) */ + template <typename Type> struct OffsetTo : Offset { @@ -270,6 +274,19 @@ template <typename Base, typename Type> inline const Type& operator + (const Base &base, OffsetTo<Type> offset) { return offset (base); } +template <typename Type> +struct LongOffsetTo : LongOffset +{ + inline const Type& operator() (const void *base) const + { + unsigned int offset = *this; + if (HB_UNLIKELY (!offset)) return Null(Type); + return (const Type&)*((const char *) base + offset); + } +}; +template <typename Base, typename Type> +inline const Type& operator + (const Base &base, LongOffsetTo<Type> offset) { return offset (base); } + /* CheckSum */ struct CheckSum : ULONG @@ -293,9 +310,9 @@ struct FixedVersion { - inline operator uint32_t(void) const { return major << 16 + minor; } + inline operator uint32_t (void) const { return major << 16 + minor; } - SHORT major; + USHORT major; USHORT minor; }; ASSERT_SIZE (FixedVersion, 4); @@ -341,10 +358,36 @@ Type array[]; }; +/* An array with a ULONG number of elements. */ +template <typename Type> +struct LongArrayOf +{ + inline const Type& operator [] (unsigned int i) const + { + if (HB_UNLIKELY (i >= len)) return Null(Type); + return array[i]; + } + inline unsigned int get_size () const + { + return sizeof (len) + len * sizeof (array[0]); + } + + ULONG len; + Type array[]; +}; + /* Array of Offset's */ template <typename Type> struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; +/* Array of LongOffset's */ +template <typename Type> +struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; + +/* LongArray of LongOffset's */ +template <typename Type> +struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; + /* An array type is one that contains a variable number of objects * as its last item. An array object is extended with get_len() * methods, as well as overloaded [] operator. */ @@ -369,16 +412,6 @@ typedef struct TableDirectory { - friend struct OpenTypeFontFile; - friend struct OffsetTable; - - inline bool is_null (void) const { return length == 0; } - inline const Tag& get_tag (void) const { return tag; } - inline unsigned long get_checksum (void) const { return checkSum; } - inline unsigned long get_offset (void) const { return offset; } - inline unsigned long get_length (void) const { return length; } - - private: Tag tag; /* 4-byte identifier. */ CheckSum checkSum; /* CheckSum for this table. */ ULONG offset; /* Offset from beginning of TrueType font @@ -417,17 +450,15 @@ { friend struct OpenTypeFontFile; - private: - /* OpenTypeFontFaces, in no particular order */ - DEFINE_OFFSET_ARRAY_TYPE (OffsetTable, offsetTable, numFonts); + STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 2); private: - Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ - ULONG version; /* Version of the TTC Header (1.0 or 2.0), - * 0x00010000 or 0x00020000 */ - ULONG numFonts; /* Number of fonts in TTC */ - ULONG offsetTable[]; /* Array of offsets to the OffsetTable for each font - * from the beginning of the file */ + Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ + FixedVersion version; /* Version of the TTC Header (1.0 or 2.0), + * 0x00010000 or 0x00020000 */ + LongOffsetLongArrayOf<OffsetTable> + table; /* Array of offsets to the OffsetTable for each font + * from the beginning of the file */ }; ASSERT_SIZE (TTCHeader, 12); @@ -446,11 +477,8 @@ DEFINE_ARRAY_INTERFACE (OpenTypeFontFace, face); /* get_face_count(), get_face(i) */ - inline const Tag& get_tag (void) const { return tag; } - /* This is how you get a table */ inline const char* get_table_data (const OpenTypeTable& table) const { return (*this)[table]; } - inline char* get_table_data (const OpenTypeTable& table) { return (*this)[table]; } private: inline const char* operator[] (const OpenTypeTable& table) const @@ -469,7 +497,7 @@ switch (tag) { default: return 0; case TrueTypeTag: case CFFTag: return 1; - case TTCTag: return ((const TTCHeader&)*this).get_len(); + case TTCTag: return TTCHeader::get_for_data ((const char *) this).table.len; } } const OpenTypeFontFace& operator[] (unsigned int i) const @@ -478,11 +506,11 @@ switch (tag) { default: /* Never happens because of the if above */ case TrueTypeTag: case CFFTag: return (const OffsetTable&)*this; - case TTCTag: return ((const TTCHeader&)*this)[i]; + case TTCTag: return this+TTCHeader::get_for_data ((const char *) this).table[i]; } } - private: + public: Tag tag; /* 4-byte identifier. */ }; ASSERT_SIZE (OpenTypeFontFile, 4);
diff --git a/src/main.cc b/src/main.cc index 979ead8..9d1f8a0 100644 --- a/src/main.cc +++ b/src/main.cc
@@ -48,7 +48,7 @@ const OpenTypeFontFile &ot = OpenTypeFontFile::get_for_data (font_data); - switch (ot.get_tag()) { + switch (ot.tag) { case OpenTypeFontFile::TrueTypeTag: printf ("OpenType font with TrueType outlines\n"); break; @@ -74,9 +74,11 @@ for (int n_table = 0; n_table < num_tables; n_table++) { const OpenTypeTable &table = font.get_table (n_table); printf (" Table %2d of %2d: %.4s (0x%08lx+0x%08lx)\n", n_table, num_tables, - (const char *)table.get_tag(), table.get_offset(), table.get_length()); + (const char *)table.tag, + (unsigned int) table.offset, + (unsigned int) table.length); - switch (table.get_tag ()) { + switch (table.tag) { case GSUBGPOS::GSUBTag: case GSUBGPOS::GPOSTag: