[COLR/CPAL] Provide enough helper for rasterization (#855)
authorEbrahim Byagowi <ebrahim@gnu.org>
Sat, 3 Mar 2018 18:30:29 +0000 (22:00 +0330)
committerGitHub <noreply@github.com>
Sat, 3 Mar 2018 18:30:29 +0000 (22:00 +0330)
src/hb-ot-colr-table.hh
src/hb-ot-cpal-table.hh

index dae843e..0a2b80e 100644 (file)
@@ -39,12 +39,15 @@ namespace OT {
 
 struct LayerRecord
 {
+  friend struct COLR;
+
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this));
   }
 
+  protected:
   GlyphID gID;                 /* Glyph ID of layer glyph */
   HBUINT16 paletteIndex;       /* Index value to use with a selected color palette */
   public:
@@ -53,12 +56,15 @@ struct LayerRecord
 
 struct BaseGlyphRecord
 {
+  friend struct COLR;
+
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
     return_trace (c->check_struct (this));
   }
 
+  protected:
   GlyphID gID;                 /* Glyph ID of reference glyph */
   HBUINT16 firstLayerIndex;    /* Index to the layer record */
   HBUINT16 numLayers;          /* Number of color layers associated with this glyph */
@@ -73,9 +79,44 @@ struct COLR
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
-    return_trace (c->check_struct (this) &&
-      c->check_array ((const void*) &layerRecordsOffset, sizeof (LayerRecord), numLayerRecords) &&
-      c->check_array ((const void*) &baseGlyphRecords, sizeof (BaseGlyphRecord), numBaseGlyphRecords));
+    if (!(c->check_struct (this) &&
+        c->check_array ((const void*) &layerRecordsOffset, sizeof (LayerRecord), numLayerRecords) &&
+        c->check_array ((const void*) &baseGlyphRecords, sizeof (BaseGlyphRecord), numBaseGlyphRecords)))
+      return_trace (false);
+
+    const BaseGlyphRecord* base_glyph_records = &baseGlyphRecords (this);
+    for (unsigned int i = 0; i < numBaseGlyphRecords; ++i)
+      if (base_glyph_records[i].firstLayerIndex +
+          base_glyph_records[i].numLayers > numLayerRecords)
+        return_trace (false);
+
+    /* XXX values of LayerRecord structs should be sanitized */
+
+    return_trace (true);
+  }
+
+  inline const bool get_base_glyph_record (
+    hb_codepoint_t glyph_id, unsigned int &first_layer, unsigned int &num_layers) const
+  {
+    /* TODO replace with bsearch */
+    const BaseGlyphRecord* base_glyph_records = &baseGlyphRecords (this);
+    unsigned int records = numBaseGlyphRecords;
+    for (unsigned int i = 0; i < records; ++i)
+      if (base_glyph_records[i].gID == glyph_id)
+      {
+        first_layer = base_glyph_records[i].firstLayerIndex;
+        num_layers = base_glyph_records[i].numLayers;
+        return true;
+      }
+    return false;
+  }
+
+  inline void get_layer_record (int layer,
+    hb_codepoint_t &glyph_id, unsigned int &palette_index) const
+  {
+    const LayerRecord* records = &layerRecordsOffset (this);
+    glyph_id = records[layer].gID;
+    palette_index = records[layer].paletteIndex;
   }
 
   protected:
index 740a94b..5adbbea 100644 (file)
@@ -42,12 +42,15 @@ namespace OT {
 
 struct ColorRecord
 {
+  friend struct CPAL;
+
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
     return_trace (true);
   }
 
+  protected:
   HBUINT8 blue;
   HBUINT8 green;
   HBUINT8 red;
@@ -74,14 +77,14 @@ struct CPALV1Tail
   inline hb_ot_color_palette_flags_t
   get_palette_flags (const void *base, unsigned int palette) const
   {
-    const HBUINT32* flags = (const HBUINT32*) (const void*) &paletteFlags (base);
+    const HBUINT32* flags = &paletteFlags (base);
     return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette];
   }
 
   inline unsigned int
   get_palette_name_id (const void *base, unsigned int palette) const
   {
-    const HBUINT16* name_ids = (const HBUINT16*) (const void*) &paletteLabel (base);
+    const HBUINT16* name_ids = &paletteLabel (base);
     return name_ids[palette];
   }
 
@@ -148,9 +151,18 @@ struct CPAL
     return numPalettes;
   }
 
+  inline void get_color_record (int palette_index, uint8_t &r, uint8_t &g,
+                                              uint8_t &b, uint8_t &a) const
+  {
+    // We should check if palette_index is in range as it is not done on COLR sanitization
+    r = colorRecords[palette_index].red;
+    g = colorRecords[palette_index].green;
+    b = colorRecords[palette_index].blue;
+    a = colorRecords[palette_index].alpha;
+  }
+
   protected:
   HBUINT16     version;
-
   /* Version 0 */
   HBUINT16     numPaletteEntries;
   HBUINT16     numPalettes;