Imported Upstream version 1.8.1
[platform/upstream/harfbuzz.git] / src / hb-ot-color-cpal-table.hh
index e364c8a..2c31274 100644 (file)
@@ -79,12 +79,12 @@ typedef enum { /*< flags >*/
 
 
 /*
- * Color Palette
- * http://www.microsoft.com/typography/otspec/cpal.htm
+ * CPAL -- Color Palette
+ * https://docs.microsoft.com/en-us/typography/opentype/spec/cpal
  */
-
 #define HB_OT_TAG_CPAL HB_TAG('C','P','A','L')
 
+
 namespace OT {
 
 
@@ -92,35 +92,44 @@ struct CPALV1Tail
 {
   friend struct CPAL;
 
-  inline bool sanitize (hb_sanitize_context_t *c, unsigned int palettes) const
+  inline bool
+  sanitize (hb_sanitize_context_t *c, const void *base, unsigned int palettes) const
   {
     TRACE_SANITIZE (this);
-    return_trace (
-      c->check_struct (this) &&
-      c->check_array ((const void*) &paletteFlags, sizeof (HBUINT32), palettes) &&
-      c->check_array ((const void*) &paletteLabel, sizeof (HBUINT16), palettes) &&
-      c->check_array ((const void*) &paletteEntryLabel, sizeof (HBUINT16), palettes));
+    return_trace (c->check_struct (this) &&
+                 (base+paletteFlagsZ).sanitize (c, palettes) &&
+                 (base+paletteLabelZ).sanitize (c, palettes) &&
+                 (base+paletteEntryLabelZ).sanitize (c, palettes));
   }
 
   private:
   inline hb_ot_color_palette_flags_t
   get_palette_flags (const void *base, unsigned int palette) const
   {
-    const HBUINT32* flags = &paletteFlags (base);
-    return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette];
+    // range checked at the CPAL caller
+    return (hb_ot_color_palette_flags_t) (uint32_t) (base+paletteFlagsZ)[palette];
   }
 
   inline unsigned int
   get_palette_name_id (const void *base, unsigned int palette) const
   {
-    const HBUINT16* name_ids = &paletteLabel (base);
-    return name_ids[palette];
+    // range checked at the CPAL caller
+    return (base+paletteLabelZ)[palette];
   }
 
   protected:
-  LOffsetTo<HBUINT32> paletteFlags;
-  LOffsetTo<HBUINT16> paletteLabel;
-  LOffsetTo<HBUINT16> paletteEntryLabel;
+  LOffsetTo<UnsizedArrayOf<HBUINT32> >
+               paletteFlagsZ;          /* Offset from the beginning of CPAL table to
+                                        * the Palette Type Array. Set to 0 if no array
+                                        * is provided. */
+  LOffsetTo<UnsizedArrayOf<HBUINT16> >
+               paletteLabelZ;          /* Offset from the beginning of CPAL table to
+                                        * the Palette Labels Array. Set to 0 if no
+                                        * array is provided. */
+  LOffsetTo<UnsizedArrayOf<HBUINT16> >
+               paletteEntryLabelZ;     /* Offset from the beginning of CPAL table to
+                                        * the Palette Entry Label Array. Set to 0
+                                        * if no array is provided. */
   public:
   DEFINE_SIZE_STATIC (12);
 };
@@ -134,21 +143,22 @@ struct CPAL
   inline bool sanitize (hb_sanitize_context_t *c) const
   {
     TRACE_SANITIZE (this);
-    if (!(c->check_struct (this) && // This checks colorRecordIndicesX sanity also, see #get_size
-        c->check_array ((const void*) &colorRecordsZ, sizeof (BGRAColor), numColorRecords)))
+    if (unlikely (!(c->check_struct (this) &&  // it checks colorRecordIndices also
+                                               // see #get_size
+                   (this+colorRecordsZ).sanitize (c, numColorRecords))))
       return_trace (false);
 
     // Check for indices sanity so no need for doing it runtime
     for (unsigned int i = 0; i < numPalettes; ++i)
-      if (colorRecordIndicesX[i] + numPaletteEntries > numColorRecords)
-        return_trace (false);
+      if (unlikely (colorRecordIndicesZ[i] + numPaletteEntries > numColorRecords))
+       return_trace (false);
 
     // If version is zero, we are done here; otherwise we need to check tail also
     if (version == 0)
       return_trace (true);
 
     const CPALV1Tail &v1 = StructAfter<CPALV1Tail> (*this);
-    return_trace (v1.sanitize (c, numPalettes));
+    return_trace (likely (v1.sanitize (c, this, numPalettes)));
   }
 
   inline unsigned int get_size (void) const
@@ -158,7 +168,7 @@ struct CPAL
 
   inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette) const
   {
-    if (version == 0 || palette >= numPalettes)
+    if (unlikely (version == 0 || palette >= numPalettes))
       return HB_OT_COLOR_PALETTE_FLAG_DEFAULT;
 
     const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
@@ -167,7 +177,7 @@ struct CPAL
 
   inline unsigned int get_palette_name_id (unsigned int palette) const
   {
-    if (version == 0 || palette >= numPalettes)
+    if (unlikely (version == 0 || palette >= numPalettes))
       return 0xFFFF;
 
     const CPALV1Tail& cpal1 = StructAfter<CPALV1Tail> (*this);
@@ -179,27 +189,33 @@ struct CPAL
     return numPalettes;
   }
 
-  inline hb_ot_color_t get_color_record_argb (unsigned int color_index, unsigned int palette) const
+  inline hb_ot_color_t
+  get_color_record_argb (unsigned int color_index, unsigned int palette) const
   {
-    if (color_index >= numPaletteEntries || palette >= numPalettes)
+    if (unlikely (color_index >= numPaletteEntries || palette >= numPalettes))
       return 0;
 
-    const BGRAColor* records = &colorRecordsZ(this);
     // No need for more range check as it is already done on #sanitize
-    return records[colorRecordIndicesX[palette] + color_index];
+    const UnsizedArrayOf<BGRAColor>& color_records = this+colorRecordsZ;
+    return color_records[colorRecordIndicesZ[palette] + color_index];
   }
 
   protected:
-  HBUINT16     version;
+  HBUINT16     version;                /* Table version number */
   /* Version 0 */
-  HBUINT16     numPaletteEntries;
-  HBUINT16     numPalettes;
-  HBUINT16     numColorRecords;
-  LOffsetTo<HBUINT32>  colorRecordsZ;
-  HBUINT16     colorRecordIndicesX[VAR];  // VAR=numPalettes
-/*CPALV1Tail   v1[VAR];*/
+  HBUINT16     numPaletteEntries;      /* Number of palette entries in each palette. */
+  HBUINT16     numPalettes;            /* Number of palettes in the table. */
+  HBUINT16     numColorRecords;        /* Total number of color records, combined for
+                                        * all palettes. */
+  LOffsetTo<UnsizedArrayOf<BGRAColor> >
+               colorRecordsZ;          /* Offset from the beginning of CPAL table to
+                                        * the first ColorRecord. */
+  UnsizedArrayOf<HBUINT16>
+               colorRecordIndicesZ;    /* Index of each paletteā€™s first color record in
+                                        * the combined color record array. */
+/*CPALV1Tail   v1;*/
   public:
-  DEFINE_SIZE_ARRAY (12, colorRecordIndicesX);
+  DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ);
 };
 
 } /* namespace OT */