Imported Upstream version 2.6.4
[platform/upstream/harfbuzz.git] / src / hb-ot-cff-common.hh
index 61e615c..6735c74 100644 (file)
@@ -27,6 +27,7 @@
 #define HB_OT_CFF_COMMON_HH
 
 #include "hb-open-type.hh"
+#include "hb-bimap.hh"
 #include "hb-ot-layout-common.hh"
 #include "hb-cff-interp-dict-common.hh"
 #include "hb-subset-plan.hh"
@@ -39,14 +40,14 @@ using namespace OT;
 
 /* utility macro */
 template<typename Type>
-static inline const Type& StructAtOffsetOrNull(const void *P, unsigned int offset)
-{ return offset? (* reinterpret_cast<const Type*> ((const char *) P + offset)): Null(Type); }
+static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
+{ return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
 
-inline unsigned int calcOffSize(unsigned int dataSize)
+inline unsigned int calcOffSize (unsigned int dataSize)
 {
   unsigned int size = 1;
   unsigned int offset = dataSize + 1;
-  while ((offset & ~0xFF) != 0)
+  while (offset & ~0xFF)
   {
     size++;
     offset >>= 8;
@@ -57,8 +58,8 @@ inline unsigned int calcOffSize(unsigned int dataSize)
 
 struct code_pair_t
 {
-  hb_codepoint_t  code;
-  hb_codepoint_t  glyph;
+  hb_codepoint_t code;
+  hb_codepoint_t glyph;
 };
 
 typedef hb_vector_t<unsigned char> str_buff_t;
@@ -82,27 +83,17 @@ struct str_buff_vec_t : hb_vector_t<str_buff_t>
 template <typename COUNT>
 struct CFFIndex
 {
-  bool sanitize (hb_sanitize_context_t *c) const
-  {
-    TRACE_SANITIZE (this);
-    return_trace (likely ((count.sanitize (c) && count == 0) || /* empty INDEX */
-                         (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
-                          c->check_array (offsets, offSize, count + 1) &&
-                          c->check_array ((const HBUINT8*)data_base (), 1, max_offset () - 1))));
-  }
-
   static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
   { return offSize * (count + 1); }
 
   unsigned int offset_array_size () const
   { return calculate_offset_array_size (offSize, count); }
 
-  static unsigned int calculate_serialized_size (unsigned int offSize, unsigned int count, unsigned int dataSize)
+  static unsigned int calculate_serialized_size (unsigned int offSize_, unsigned int count,
+                                                unsigned int dataSize)
   {
-    if (count == 0)
-      return COUNT::static_size;
-    else
-      return min_size + calculate_offset_array_size (offSize, count) + dataSize;
+    if (count == 0) return COUNT::static_size;
+    return min_size + calculate_offset_array_size (offSize_, count) + dataSize;
   }
 
   bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
@@ -124,15 +115,15 @@ struct CFFIndex
     {
       COUNT *dest = c->allocate_min<COUNT> ();
       if (unlikely (dest == nullptr)) return_trace (false);
-      dest->set (0);
+      *dest = 0;
     }
     else
     {
       /* serialize CFFIndex header */
       if (unlikely (!c->extend_min (*this))) return_trace (false);
-      this->count.set (byteArray.length);
-      this->offSize.set (offSize_);
-      if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (byteArray.length + 1))))
+      this->count = byteArray.length;
+      this->offSize = offSize_;
+      if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (byteArray.length + 1))))
        return_trace (false);
 
       /* serialize indices */
@@ -166,9 +157,7 @@ struct CFFIndex
     byteArray.init ();
     byteArray.resize (buffArray.length);
     for (unsigned int i = 0; i < byteArray.length; i++)
-    {
-      byteArray[i] = byte_str_t (buffArray[i].arrayZ (), buffArray[i].length);
-    }
+      byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length);
     bool result = this->serialize (c, offSize_, byteArray);
     byteArray.fini ();
     return result;
@@ -181,7 +170,7 @@ struct CFFIndex
     for (; size; size--)
     {
       --p;
-      p->set (offset & 0xFF);
+      *p = offset & 0xFF;
       offset >>= 8;
     }
   }
@@ -199,37 +188,38 @@ struct CFFIndex
 
   unsigned int length_at (unsigned int index) const
   {
-       if (likely ((offset_at (index + 1) >= offset_at (index)) &&
-                   (offset_at (index + 1) <= offset_at (count))))
-         return offset_at (index + 1) - offset_at (index);
-       else
-         return 0;
+    if (unlikely ((offset_at (index + 1) < offset_at (index)) ||
+                 (offset_at (index + 1) > offset_at (count))))
+      return 0;
+    return offset_at (index + 1) - offset_at (index);
   }
 
   const unsigned char *data_base () const
-  { return (const unsigned char *)this + min_size + offset_array_size (); }
+  { return (const unsigned char *) this + min_size + offset_array_size (); }
 
   unsigned int data_size () const { return HBINT8::static_size; }
 
   byte_str_t operator [] (unsigned int index) const
   {
-    if (likely (index < count))
-      return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
-    else
-      return Null(byte_str_t);
+    if (unlikely (index >= count)) return Null (byte_str_t);
+    return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
   }
 
   unsigned int get_size () const
   {
-    if (this != &Null(CFFIndex))
-    {
-      if (count > 0)
-       return min_size + offset_array_size () + (offset_at (count) - 1);
-      else
-       return count.static_size;  /* empty CFFIndex contains count only */
-    }
-    else
-      return 0;
+    if (this == &Null (CFFIndex)) return 0;
+    if (count > 0)
+      return min_size + offset_array_size () + (offset_at (count) - 1);
+    return count.static_size;  /* empty CFFIndex contains count only */
+  }
+
+  bool sanitize (hb_sanitize_context_t *c) const
+  {
+    TRACE_SANITIZE (this);
+    return_trace (likely ((c->check_struct (this) && count == 0) || /* empty INDEX */
+                         (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
+                          c->check_array (offsets, offSize, count + 1) &&
+                          c->check_array ((const HBUINT8*) data_base (), 1, max_offset () - 1))));
   }
 
   protected:
@@ -245,10 +235,10 @@ struct CFFIndex
   }
 
   public:
-  COUNT     count;     /* Number of object data. Note there are (count+1) offsets */
-  HBUINT8   offSize;      /* The byte size of each offset in the offsets array. */
-  HBUINT8   offsets[VAR]; /* The array of (count + 1) offsets into objects array (1-base). */
-  /* HBUINT8 data[VAR];      Object data */
+  COUNT                count;          /* Number of object data. Note there are (count+1) offsets */
+  HBUINT8      offSize;        /* The byte size of each offset in the offsets array. */
+  HBUINT8      offsets[HB_VAR_ARRAY];  /* The array of (count + 1) offsets into objects array (1-base). */
+  /* HBUINT8 data[HB_VAR_ARRAY];       Object data */
   public:
   DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
 };
@@ -275,9 +265,9 @@ struct CFFIndexOf : CFFIndex<COUNT>
     TRACE_SERIALIZE (this);
     /* serialize CFFIndex header */
     if (unlikely (!c->extend_min (*this))) return_trace (false);
-    this->count.set (dataArrayLen);
-    this->offSize.set (offSize_);
-    if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
+    this->count = dataArrayLen;
+    this->offSize = offSize_;
+    if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
       return_trace (false);
 
     /* serialize indices */
@@ -293,7 +283,7 @@ struct CFFIndexOf : CFFIndex<COUNT>
     /* serialize data */
     for (unsigned int i = 0; i < dataArrayLen; i++)
     {
-      TYPE  *dest = c->start_embed<TYPE> ();
+      TYPE *dest = c->start_embed<TYPE> ();
       if (unlikely (dest == nullptr ||
                    !dest->serialize (c, dataArray[i], param1, param2)))
        return_trace (false);
@@ -310,7 +300,7 @@ struct CFFIndexOf : CFFIndex<COUNT>
                                                 const PARAM &param)
   {
     /* determine offset size */
-    unsigned int  totalDataSize = 0;
+    unsigned int totalDataSize = 0;
     for (unsigned int i = 0; i < dataArrayLen; i++)
     {
       unsigned int dataSize = TYPE::calculate_serialized_size (dataArray[i], param);
@@ -334,10 +324,9 @@ struct Dict : UnsizedByteStr
   {
     TRACE_SERIALIZE (this);
     for (unsigned int i = 0; i < dictval.get_count (); i++)
-    {
       if (unlikely (!opszr.serialize (c, dictval[i], param)))
        return_trace (false);
-    }
+
     return_trace (true);
   }
 
@@ -376,11 +365,11 @@ struct Dict : UnsizedByteStr
     if (unlikely (p == nullptr)) return_trace (false);
     if (Is_OpCode_ESC (op))
     {
-      p->set (OpCode_escape);
+      *p = OpCode_escape;
       op = Unmake_OpCode_ESC (op);
       p++;
     }
-    p->set (op);
+    *p = op;
     return_trace (true);
   }
 
@@ -391,14 +380,10 @@ struct Dict : UnsizedByteStr
   { return serialize_int_op<HBUINT16, 0, 0x7FFF> (c, op, value, OpCode_shortint); }
 
   static bool serialize_offset4_op (hb_serialize_context_t *c, op_code_t op, int value)
-  {
-    return serialize_uint4_op (c, op, value);
-  }
+  { return serialize_uint4_op (c, op, value); }
 
   static bool serialize_offset2_op (hb_serialize_context_t *c, op_code_t op, int value)
-  {
-    return serialize_uint2_op (c, op, value);
-  }
+  { return serialize_uint2_op (c, op, value); }
 };
 
 struct TopDict : Dict {};
@@ -414,57 +399,6 @@ struct table_info_t
   unsigned int    offSize;
 };
 
-/* used to remap font index or SID from fullset to subset.
- * set to CFF_UNDEF_CODE if excluded from subset */
-struct remap_t : hb_vector_t<hb_codepoint_t>
-{
-  void init () { SUPER::init (); }
-
-  void fini () { SUPER::fini (); }
-
-  bool reset (unsigned int size)
-  {
-    if (unlikely (!SUPER::resize (size)))
-      return false;
-    for (unsigned int i = 0; i < length; i++)
-      (*this)[i] = CFF_UNDEF_CODE;
-    count = 0;
-    return true;
-  }
-
-  bool identity (unsigned int size)
-  {
-    if (unlikely (!SUPER::resize (size)))
-      return false;
-    unsigned int i;
-    for (i = 0; i < length; i++)
-      (*this)[i] = i;
-    count = i;
-    return true;
-  }
-
-  bool excludes (hb_codepoint_t id) const
-  { return (id < length) && ((*this)[id] == CFF_UNDEF_CODE); }
-
-  bool includes (hb_codepoint_t id) const
-  { return !excludes (id); }
-
-  unsigned int add (unsigned int i)
-  {
-    if ((*this)[i] == CFF_UNDEF_CODE)
-      (*this)[i] = count++;
-    return (*this)[i];
-  }
-
-  hb_codepoint_t get_count () const { return count; }
-
-  protected:
-  hb_codepoint_t  count;
-
-  private:
-  typedef hb_vector_t<hb_codepoint_t> SUPER;
-};
-
 template <typename COUNT>
 struct FDArray : CFFIndexOf<COUNT, FontDict>
 {
@@ -477,13 +411,13 @@ struct FDArray : CFFIndexOf<COUNT, FontDict>
   {
     TRACE_SERIALIZE (this);
     if (unlikely (!c->extend_min (*this))) return_trace (false);
-    this->count.set (fontDicts.length);
-    this->offSize.set (offSize_);
-    if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fontDicts.length + 1))))
+    this->count = fontDicts.length;
+    this->offSize = offSize_;
+    if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (fontDicts.length + 1))))
       return_trace (false);
 
     /* serialize font dict offsets */
-    unsigned int  offset = 1;
+    unsigned int offset = 1;
     unsigned int fid = 0;
     for (; fid < fontDicts.length; fid++)
     {
@@ -508,22 +442,22 @@ struct FDArray : CFFIndexOf<COUNT, FontDict>
                  unsigned int offSize_,
                  const hb_vector_t<DICTVAL> &fontDicts,
                  unsigned int fdCount,
-                 const remap_t &fdmap,
+                 const hb_inc_bimap_t &fdmap,
                  OP_SERIALIZER& opszr,
                  const hb_vector_t<table_info_t> &privateInfos)
   {
     TRACE_SERIALIZE (this);
     if (unlikely (!c->extend_min (*this))) return_trace (false);
-    this->count.set (fdCount);
-    this->offSize.set (offSize_);
-    if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1))))
+    this->count = fdCount;
+    this->offSize = offSize_;
+    if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1))))
       return_trace (false);
 
     /* serialize font dict offsets */
     unsigned int  offset = 1;
     unsigned int  fid = 0;
     for (unsigned i = 0; i < fontDicts.length; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
        if (unlikely (fid >= fdCount)) return_trace (false);
        CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset);
@@ -533,7 +467,7 @@ struct FDArray : CFFIndexOf<COUNT, FontDict>
 
     /* serialize font dicts */
     for (unsigned int i = 0; i < fontDicts.length; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
       {
        FontDict *dict = c->start_embed<FontDict> ();
        if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]])))
@@ -547,12 +481,12 @@ struct FDArray : CFFIndexOf<COUNT, FontDict>
   static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
                                                 const hb_vector_t<DICTVAL> &fontDicts,
                                                 unsigned int fdCount,
-                                                const remap_t &fdmap,
+                                                const hb_inc_bimap_t &fdmap,
                                                 OP_SERIALIZER& opszr)
   {
     unsigned int dictsSize = 0;
     for (unsigned int i = 0; i < fontDicts.len; i++)
-      if (fdmap.includes (i))
+      if (fdmap.has (i))
        dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr);
 
     offSize_ = calcOffSize (dictsSize);
@@ -575,21 +509,20 @@ struct FDSelect0 {
   }
 
   hb_codepoint_t get_fd (hb_codepoint_t glyph) const
-  {
-    return (hb_codepoint_t)fds[glyph];
-  }
+  { return (hb_codepoint_t) fds[glyph]; }
 
   unsigned int get_size (unsigned int num_glyphs) const
   { return HBUINT8::static_size * num_glyphs; }
 
-  HBUINT8     fds[VAR];
+  HBUINT8     fds[HB_VAR_ARRAY];
 
-  DEFINE_SIZE_MIN (1);
+  DEFINE_SIZE_MIN (0);
 };
 
 template <typename GID_TYPE, typename FD_TYPE>
-struct FDSelect3_4_Range {
-  bool sanitize (hb_sanitize_context_t *c, const void */*nullptr*/, unsigned int fdcount) const
+struct FDSelect3_4_Range
+{
+  bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
   {
     TRACE_SANITIZE (this);
     return_trace (first < c->get_num_glyphs () && (fd < fdcount));
@@ -597,12 +530,13 @@ struct FDSelect3_4_Range {
 
   GID_TYPE    first;
   FD_TYPE     fd;
-
+  public:
   DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
 };
 
 template <typename GID_TYPE, typename FD_TYPE>
-struct FDSelect3_4 {
+struct FDSelect3_4
+{
   unsigned int get_size () const
   { return GID_TYPE::static_size * 2 + ranges.get_size (); }
 
@@ -614,10 +548,8 @@ struct FDSelect3_4 {
       return_trace (false);
 
     for (unsigned int i = 1; i < nRanges (); i++)
-    {
       if (unlikely (ranges[i - 1].first >= ranges[i].first))
-         return_trace (false);
-    }
+       return_trace (false);
 
     if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
       return_trace (false);
@@ -632,13 +564,13 @@ struct FDSelect3_4 {
       if (glyph < ranges[i].first)
        break;
 
-    return (hb_codepoint_t)ranges[i - 1].fd;
+    return (hb_codepoint_t) ranges[i - 1].fd;
   }
 
-  GID_TYPE &nRanges () { return ranges.len; }
-  GID_TYPE nRanges () const { return ranges.len; }
-  GID_TYPE &sentinel ()  { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
-  const GID_TYPE &sentinel () const  { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
+  GID_TYPE        &nRanges ()       { return ranges.len; }
+  GID_TYPE         nRanges () const { return ranges.len; }
+  GID_TYPE       &sentinel ()       { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
+  const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
 
   ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
   /* GID_TYPE sentinel */
@@ -649,17 +581,8 @@ struct FDSelect3_4 {
 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
 
-struct FDSelect {
-  bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
-  {
-    TRACE_SANITIZE (this);
-
-    return_trace (likely (c->check_struct (this) && (format == 0 || format == 3) &&
-                         (format == 0)?
-                         u.format0.sanitize (c, fdcount):
-                         u.format3.sanitize (c, fdcount)));
-  }
-
+struct FDSelect
+{
   bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
   {
     TRACE_SERIALIZE (this);
@@ -675,30 +598,46 @@ struct FDSelect {
 
   unsigned int get_size (unsigned int num_glyphs) const
   {
-    unsigned int size = format.static_size;
-    if (format == 0)
-      size += u.format0.get_size (num_glyphs);
-    else
-      size += u.format3.get_size ();
-    return size;
+    switch (format)
+    {
+    case 0: return format.static_size + u.format0.get_size (num_glyphs);
+    case 3: return format.static_size + u.format3.get_size ();
+    default:return 0;
+    }
   }
 
   hb_codepoint_t get_fd (hb_codepoint_t glyph) const
   {
-    if (this == &Null(FDSelect))
-      return 0;
-    if (format == 0)
-      return u.format0.get_fd (glyph);
-    else
-      return u.format3.get_fd (glyph);
+    if (this == &Null (FDSelect)) return 0;
+
+    switch (format)
+    {
+    case 0: return u.format0.get_fd (glyph);
+    case 3: return u.format3.get_fd (glyph);
+    default:return 0;
+    }
   }
 
-  HBUINT8       format;
+  bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
+  {
+    TRACE_SANITIZE (this);
+    if (unlikely (!c->check_struct (this)))
+      return_trace (false);
+
+    switch (format)
+    {
+    case 0: return_trace (u.format0.sanitize (c, fdcount));
+    case 3: return_trace (u.format3.sanitize (c, fdcount));
+    default:return_trace (false);
+    }
+  }
+
+  HBUINT8      format;
   union {
-    FDSelect0   format0;
-    FDSelect3   format3;
+  FDSelect0    format0;
+  FDSelect3    format3;
   } u;
-
+  public:
   DEFINE_SIZE_MIN (1);
 };