From 6ad8d5f3c7028147b371137ae4bca6aae66b3489 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 25 May 2009 02:27:29 -0400 Subject: [PATCH] [HB] Simplify some basic things --- src/hb-ot-layout-open-private.h | 90 +++++++++++++++++++++++++++-------------- src/main.cc | 8 ++-- 2 files changed, 64 insertions(+), 34 deletions(-) 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 @@ struct Null \ { \ 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 @@ DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT); #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; + +/* 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 subclass of Offset that does the dereferencing. Use: (this+memberName) */ template struct OffsetTo : Offset { @@ -270,6 +274,19 @@ struct OffsetTo : Offset template inline const Type& operator + (const Base &base, OffsetTo offset) { return offset (base); } +template +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 +inline const Type& operator + (const Base &base, LongOffsetTo offset) { return offset (base); } + /* CheckSum */ struct CheckSum : ULONG @@ -293,9 +310,9 @@ ASSERT_SIZE (CheckSum, 4); 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 @@ struct HeadlessArrayOf Type array[]; }; +/* An array with a ULONG number of elements. */ +template +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 struct OffsetArrayOf : ArrayOf > {}; +/* Array of LongOffset's */ +template +struct LongOffsetArrayOf : ArrayOf > {}; + +/* LongArray of LongOffset's */ +template +struct LongOffsetLongArrayOf : LongArrayOf > {}; + /* 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 @@ struct TTCHeader; 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 @@ struct TTCHeader { 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 + table; /* Array of offsets to the OffsetTable for each font + * from the beginning of the file */ }; ASSERT_SIZE (TTCHeader, 12); @@ -446,11 +477,8 @@ struct OpenTypeFontFile 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 @@ struct OpenTypeFontFile 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 @@ struct OpenTypeFontFile 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 @@ main (int argc, char **argv) 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 @@ main (int argc, char **argv) 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: -- 2.7.4