[HB] Cleanup format unions
authorBehdad Esfahbod <behdad@behdad.org>
Sun, 17 May 2009 23:47:54 +0000 (19:47 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Mon, 2 Nov 2009 19:40:10 +0000 (14:40 -0500)
src/hb-ot-layout-gdef-private.h
src/hb-ot-layout-gsub-private.h
src/hb-ot-layout-gsubgpos-private.h
src/hb-ot-layout-open-private.h

index 36471a8..b0e54c3 100644 (file)
@@ -129,23 +129,23 @@ ASSERT_SIZE (CaretValueFormat3, 6);
 struct CaretValue {
   /* XXX  we need access to a load-contour-point vfunc here */
   int get_caret_value (int ppem) const {
-    switch (u.caretValueFormat) {
-    case 1: return u.format1.get_caret_value(ppem);
-    case 2: return u.format2.get_caret_value(ppem);
-    case 3: return u.format3.get_caret_value(ppem);
+    switch (u.format) {
+    case 1: return u.format1->get_caret_value(ppem);
+    case 2: return u.format2->get_caret_value(ppem);
+    case 3: return u.format3->get_caret_value(ppem);
     default:return 0;
     }
   }
 
   private:
   union {
-  USHORT       caretValueFormat;       /* Format identifier */
-  CaretValueFormat1    format1;
-  CaretValueFormat2    format2;
-  CaretValueFormat3    format3;
+  USHORT               format;         /* Format identifier */
+  CaretValueFormat1    format1[];
+  CaretValueFormat2    format2[];
+  CaretValueFormat3    format3[];
   } u;
 };
-DEFINE_NULL (CaretValue, 2);
+ASSERT_SIZE (CaretValue, 2);
 
 struct LigGlyph {
 
index 89f4fd8..8722105 100644 (file)
@@ -91,9 +91,9 @@ struct SingleSubst {
   private:
 
   inline bool single_substitute (hb_codepoint_t &glyph_id) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.single_substitute (glyph_id);
-    case 2: return u.format2.single_substitute (glyph_id);
+    switch (u.format) {
+    case 1: return u.format1->single_substitute (glyph_id);
+    case 2: return u.format2->single_substitute (glyph_id);
     default:return false;
     }
   }
@@ -118,12 +118,12 @@ struct SingleSubst {
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  SingleSubstFormat1   format1;
-  SingleSubstFormat2   format2;
+  USHORT               format;         /* Format identifier */
+  SingleSubstFormat1   format1[];
+  SingleSubstFormat2   format2[];
   } u;
 };
-DEFINE_NULL (SingleSubst, 2);
+ASSERT_SIZE (SingleSubst, 2);
 
 
 struct Sequence {
@@ -197,19 +197,19 @@ struct MultipleSubst {
   private:
 
   inline bool substitute (LOOKUP_ARGS_DEF) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.substitute (LOOKUP_ARGS);
+    switch (u.format) {
+    case 1: return u.format1->substitute (LOOKUP_ARGS);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  MultipleSubstFormat1 format1;
+  USHORT               format;         /* Format identifier */
+  MultipleSubstFormat1 format1[];
   } u;
 };
-DEFINE_NULL (MultipleSubst, 2);
+ASSERT_SIZE (MultipleSubst, 2);
 
 
 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
@@ -275,19 +275,19 @@ struct AlternateSubst {
   private:
 
   inline bool substitute (LOOKUP_ARGS_DEF) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.substitute (LOOKUP_ARGS);
+    switch (u.format) {
+    case 1: return u.format1->substitute (LOOKUP_ARGS);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  AlternateSubstFormat1        format1;
+  USHORT               format;         /* Format identifier */
+  AlternateSubstFormat1        format1[];
   } u;
 };
-DEFINE_NULL (AlternateSubst, 2);
+ASSERT_SIZE (AlternateSubst, 2);
 
 
 struct Ligature {
@@ -432,19 +432,19 @@ struct LigatureSubst {
   private:
 
   inline bool substitute (LOOKUP_ARGS_DEF) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.substitute (LOOKUP_ARGS);
+    switch (u.format) {
+    case 1: return u.format1->substitute (LOOKUP_ARGS);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  LigatureSubstFormat1 format1;
+  USHORT               format;         /* Format identifier */
+  LigatureSubstFormat1 format1[];
   } u;
 };
-DEFINE_NULL (LigatureSubst, 2);
+ASSERT_SIZE (LigatureSubst, 2);
 
 
 
@@ -456,7 +456,7 @@ struct ContextSubst : Context {
     return this->apply (LOOKUP_ARGS, substitute_lookup);
   }
 };
-DEFINE_NULL (ContextSubst, 2);
+ASSERT_SIZE (ContextSubst, 2);
 
 
 struct ChainSubRule {
@@ -622,23 +622,23 @@ struct ChainContextSubst {
   private:
 
   inline bool substitute (LOOKUP_ARGS_DEF) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.substitute (LOOKUP_ARGS);
-    case 2: return u.format2.substitute (LOOKUP_ARGS);
-    case 3: return u.format3.substitute (LOOKUP_ARGS);
+    switch (u.format) {
+    case 1: return u.format1->substitute (LOOKUP_ARGS);
+    case 2: return u.format2->substitute (LOOKUP_ARGS);
+    case 3: return u.format3->substitute (LOOKUP_ARGS);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  ChainContextSubstFormat1     format1;
-  ChainContextSubstFormat2     format2;
-  ChainContextSubstFormat3     format3;
+  USHORT                       format; /* Format identifier */
+  ChainContextSubstFormat1     format1[];
+  ChainContextSubstFormat2     format2[];
+  ChainContextSubstFormat3     format3[];
   } u;
 };
-DEFINE_NULL (ChainContextSubst, 2);
+ASSERT_SIZE (ChainContextSubst, 2);
 
 
 struct ExtensionSubstFormat1 {
@@ -647,6 +647,7 @@ struct ExtensionSubstFormat1 {
 
   private:
   inline unsigned int get_type (void) const { return extensionLookupType; }
+  inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
   inline bool substitute (LOOKUP_ARGS_DEF) const;
 
   private:
@@ -654,8 +655,10 @@ struct ExtensionSubstFormat1 {
   USHORT       extensionLookupType;    /* Lookup type of subtable referenced
                                         * by ExtensionOffset (i.e. the
                                         * extension subtable). */
-  ULONG                extensionOffset;        /* Offset to the extension subtable,
-                                        * of lookup type  subtable. */
+  USHORT       extensionOffset[2];     /* Offset to the extension subtable,
+                                        * of lookup type subtable.
+                                        * Defined as two shorts to avoid
+                                        * alignment requirements. */
 };
 ASSERT_SIZE (ExtensionSubstFormat1, 8);
 
@@ -667,26 +670,26 @@ struct ExtensionSubst {
   private:
 
   inline unsigned int get_type (void) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.get_type ();
+    switch (u.format) {
+    case 1: return u.format1->get_type ();
     default:return 0;
     }
   }
 
   inline bool substitute (LOOKUP_ARGS_DEF) const {
-    switch (u.substFormat) {
-    case 1: return u.format1.substitute (LOOKUP_ARGS);
+    switch (u.format) {
+    case 1: return u.format1->substitute (LOOKUP_ARGS);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT       substFormat;    /* Format identifier */
-  ExtensionSubstFormat1        format1;
+  USHORT               format;         /* Format identifier */
+  ExtensionSubstFormat1        format1[];
   } u;
 };
-DEFINE_NULL (ExtensionSubst, 2);
+ASSERT_SIZE (ExtensionSubst, 2);
 
 
 
@@ -905,7 +908,7 @@ struct GSUB : GSUBGPOS {
 inline bool ExtensionSubstFormat1::substitute (LOOKUP_ARGS_DEF) const {
   /* XXX either check in sanitize or here that the lookuptype is not 7 again,
    * or we can loop indefinitely. */
-  return (*(SubstLookupSubTable *)(((char *) this) + extensionOffset)).substitute (LOOKUP_ARGS,
+  return (*(SubstLookupSubTable *)(((char *) this) + get_offset ())).substitute (LOOKUP_ARGS,
                                                                                   get_type ());
 }
 
index 033078b..43f81c9 100644 (file)
@@ -315,22 +315,22 @@ struct Context {
   protected:
   bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
     switch (u.format) {
-    case 1: return u.format1.apply (LOOKUP_ARGS, apply_func);
-    case 2: return u.format2.apply (LOOKUP_ARGS, apply_func);
-    case 3: return u.format3.apply (LOOKUP_ARGS, apply_func);
+    case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
+    case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
+    case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
     default:return false;
     }
   }
 
   private:
   union {
-  USHORT               format; /* Format identifier */
-  ContextFormat1       format1;
-  ContextFormat2       format2;
-  ContextFormat3       format3;
+  USHORT               format;         /* Format identifier */
+  ContextFormat1       format1[];
+  ContextFormat2       format2[];
+  ContextFormat3       format3[];
   } u;
 };
-DEFINE_NULL (Context, 2);
+ASSERT_SIZE (Context, 2);
 
 
 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */
index 4ae7a89..1876057 100644 (file)
@@ -167,22 +167,11 @@ static const char NullPool[16] = "";
 
 /* Generic template for nul-content sizeof-sized Null objects. */
 template <typename Type>
-struct NullSize {
-  char bytes[sizeof (Type)];
-};
-template <typename Type>
 struct Null {
-  ASSERT_STATIC (sizeof (NullSize<Type>) <= sizeof (NullPool));
+  ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
   static inline const Type &get () { return (const Type&) *(const Type*) NullPool; }
 };
 
-/* Specializaiton for nul-content arbitrary-sized Null objects. */
-#define DEFINE_NULL(Type, size) \
-template <> \
-struct NullSize <Type> { \
-  char bytes[size]; \
-}
-
 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
 #define DEFINE_NULL_DATA(Type, size, data) \
 template <> \
@@ -785,9 +774,9 @@ ASSERT_SIZE (CoverageFormat2, 4);
 
 struct Coverage {
   unsigned int get_coverage (hb_codepoint_t glyph_id) const {
-    switch (u.coverageFormat) {
-    case 1: return u.format1.get_coverage(glyph_id);
-    case 2: return u.format2.get_coverage(glyph_id);
+    switch (u.format) {
+    case 1: return u.format1->get_coverage(glyph_id);
+    case 2: return u.format2->get_coverage(glyph_id);
     default:return NOT_COVERED;
     }
   }
@@ -798,12 +787,12 @@ struct Coverage {
 
   private:
   union {
-  USHORT               coverageFormat; /* Format identifier */
-  CoverageFormat1      format1;
-  CoverageFormat2      format2;
+  USHORT               format;         /* Format identifier */
+  CoverageFormat1      format1[];
+  CoverageFormat2      format2[];
   } u;
 };
-DEFINE_NULL (Coverage, 2);
+ASSERT_SIZE (Coverage, 2);
 
 /*
  * Class Definition Table
@@ -870,21 +859,21 @@ ASSERT_SIZE (ClassDefFormat2, 4);
 
 struct ClassDef {
   hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const {
-    switch (u.classFormat) {
-    case 1: return u.format1.get_class(glyph_id);
-    case 2: return u.format2.get_class(glyph_id);
+    switch (u.format) {
+    case 1: return u.format1->get_class(glyph_id);
+    case 2: return u.format2->get_class(glyph_id);
     default:return 0;
     }
   }
 
   private:
   union {
-  USHORT               classFormat;    /* Format identifier */
-  ClassDefFormat1      format1;
-  ClassDefFormat2      format2;
+  USHORT               format;         /* Format identifier */
+  ClassDefFormat1      format1[];
+  ClassDefFormat2      format2[];
   } u;
 };
-DEFINE_NULL (ClassDef, 2);
+ASSERT_SIZE (ClassDef, 2);
 
 /*
  * Device Tables