Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / src / hb-aat-layout-kerx-table.hh
index a64c807..0354b47 100644 (file)
@@ -82,8 +82,8 @@ struct KernPair
   }
 
   protected:
-  GlyphID      left;
-  GlyphID      right;
+  HBGlyphID16  left;
+  HBGlyphID16  right;
   FWORD                value;
   public:
   DEFINE_SIZE_STATIC (6);
@@ -229,9 +229,7 @@ struct KerxSubTableFormat1
 
     bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
                        const Entry<EntryData> &entry)
-    {
-      return Format1EntryT::performAction (entry);
-    }
+    { return Format1EntryT::performAction (entry); }
     void transition (StateTableDriver<Types, EntryData> *driver,
                     const Entry<EntryData> &entry)
     {
@@ -251,7 +249,7 @@ struct KerxSubTableFormat1
 
       if (Format1EntryT::performAction (entry) && depth)
       {
-       unsigned int tuple_count = MAX (1u, table->header.tuple_count ());
+       unsigned int tuple_count = hb_max (1u, table->header.tuple_count ());
 
        unsigned int kern_idx = Format1EntryT::kernActionIndex (entry);
        kern_idx = Types::byteOffsetToIndex (kern_idx, &table->machine, kernAction.arrayZ);
@@ -281,35 +279,28 @@ struct KerxSubTableFormat1
 
          hb_glyph_position_t &o = buffer->pos[idx];
 
-         /* Testing shows that CoreText only applies kern (cross-stream or not)
-          * if none has been applied by previous subtables.  That is, it does
-          * NOT seem to accumulate as otherwise implied by specs. */
-
-         /* The following flag is undocumented in the spec, but described
-          * in the 'kern' table example. */
-         if (v == -0x8000)
-         {
-           o.attach_type() = ATTACH_TYPE_NONE;
-           o.attach_chain() = 0;
-           o.x_offset = o.y_offset = 0;
-         }
-         else if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
+         if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
          {
            if (crossStream)
            {
-             if (buffer->pos[idx].attach_type() && !buffer->pos[idx].y_offset)
+             /* The following flag is undocumented in the spec, but described
+              * in the 'kern' table example. */
+             if (v == -0x8000)
              {
-               o.y_offset = c->font->em_scale_y (v);
+               o.attach_type() = ATTACH_TYPE_NONE;
+               o.attach_chain() = 0;
+               o.y_offset = 0;
+             }
+             else if (o.attach_type())
+             {
+               o.y_offset += c->font->em_scale_y (v);
                buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
              }
            }
            else if (buffer->info[idx].mask & kern_mask)
            {
-             if (!buffer->pos[idx].x_offset)
-             {
-               buffer->pos[idx].x_advance += c->font->em_scale_x (v);
-               buffer->pos[idx].x_offset += c->font->em_scale_x (v);
-             }
+             o.x_advance += c->font->em_scale_x (v);
+             o.x_offset += c->font->em_scale_x (v);
            }
          }
          else
@@ -317,19 +308,22 @@ struct KerxSubTableFormat1
            if (crossStream)
            {
              /* CoreText doesn't do crossStream kerning in vertical.  We do. */
-             if (buffer->pos[idx].attach_type() && !buffer->pos[idx].x_offset)
+             if (v == -0x8000)
              {
-               o.x_offset = c->font->em_scale_x (v);
+               o.attach_type() = ATTACH_TYPE_NONE;
+               o.attach_chain() = 0;
+               o.x_offset = 0;
+             }
+             else if (o.attach_type())
+             {
+               o.x_offset += c->font->em_scale_x (v);
                buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
              }
            }
            else if (buffer->info[idx].mask & kern_mask)
            {
-             if (!buffer->pos[idx].y_offset)
-             {
-               buffer->pos[idx].y_advance += c->font->em_scale_y (v);
-               buffer->pos[idx].y_offset += c->font->em_scale_y (v);
-             }
+             o.y_advance += c->font->em_scale_y (v);
+             o.y_offset += c->font->em_scale_y (v);
            }
          }
        }
@@ -392,7 +386,7 @@ struct KerxSubTableFormat2
 
     const UnsizedArrayOf<FWORD> &arrayZ = this+array;
     unsigned int kern_idx = l + r;
-    kern_idx = Types::offsetToIndex (kern_idx, this, &arrayZ);
+    kern_idx = Types::offsetToIndex (kern_idx, this, arrayZ.arrayZ);
     const FWORD *v = &arrayZ[kern_idx];
     if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
 
@@ -488,7 +482,7 @@ struct KerxSubTableFormat4
     };
 
     driver_context_t (const KerxSubTableFormat4 *table,
-                            hb_aat_apply_context_t *c_) :
+                     hb_aat_apply_context_t *c_) :
        c (c_),
        action_type ((table->flags & ActionType) >> 30),
        ankrData ((HBUINT16 *) ((const char *) &table->machine + (table->flags & Offset))),
@@ -497,9 +491,7 @@ struct KerxSubTableFormat4
 
     bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
                        const Entry<EntryData> &entry)
-    {
-      return entry.data.ankrActionIndex != 0xFFFF;
-    }
+    { return entry.data.ankrActionIndex != 0xFFFF; }
     void transition (StateTableDriver<Types, EntryData> *driver,
                     const Entry<EntryData> &entry)
     {
@@ -512,11 +504,13 @@ struct KerxSubTableFormat4
        {
          case 0: /* Control Point Actions.*/
          {
-           /* indexed into glyph outline. */
-           const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex];
+           /* Indexed into glyph outline. */
+           /* Each action (record in ankrData) contains two 16-bit fields, so we must
+              double the ankrActionIndex to get the correct offset here. */
+           const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2];
            if (!c->sanitizer.check_array (data, 2)) return;
-           HB_UNUSED unsigned int markControlPoint = *data++;
-           HB_UNUSED unsigned int currControlPoint = *data++;
+           unsigned int markControlPoint = *data++;
+           unsigned int currControlPoint = *data++;
            hb_position_t markX = 0;
            hb_position_t markY = 0;
            hb_position_t currX = 0;
@@ -538,8 +532,10 @@ struct KerxSubTableFormat4
 
          case 1: /* Anchor Point Actions. */
          {
-          /* Indexed into 'ankr' table. */
-           const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex];
+           /* Indexed into 'ankr' table. */
+           /* Each action (record in ankrData) contains two 16-bit fields, so we must
+              double the ankrActionIndex to get the correct offset here. */
+           const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2];
            if (!c->sanitizer.check_array (data, 2)) return;
            unsigned int markAnchorPoint = *data++;
            unsigned int currAnchorPoint = *data++;
@@ -557,7 +553,9 @@ struct KerxSubTableFormat4
 
          case 2: /* Control Point Coordinate Actions. */
          {
-           const FWORD *data = (const FWORD *) &ankrData[entry.data.ankrActionIndex];
+           /* Each action contains four 16-bit fields, so we multiply the ankrActionIndex
+              by 4 to get the correct offset for the given action. */
+           const FWORD *data = (const FWORD *) &ankrData[entry.data.ankrActionIndex * 4];
            if (!c->sanitizer.check_array (data, 4)) return;
            int markX = *data++;
            int markY = *data++;
@@ -628,7 +626,7 @@ struct KerxSubTableFormat6
   bool is_long () const { return flags & ValuesAreLong; }
 
   int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
-                         hb_aat_apply_context_t *c) const
+                  hb_aat_apply_context_t *c) const
   {
     unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
     if (is_long ())
@@ -712,18 +710,18 @@ struct KerxSubTableFormat6
   {
     struct Long
     {
-      LNNOffsetTo<Lookup<HBUINT32> >           rowIndexTable;
-      LNNOffsetTo<Lookup<HBUINT32> >           columnIndexTable;
-      LNNOffsetTo<UnsizedArrayOf<FWORD32> >    array;
+      NNOffset32To<Lookup<HBUINT32>>           rowIndexTable;
+      NNOffset32To<Lookup<HBUINT32>>           columnIndexTable;
+      NNOffset32To<UnsizedArrayOf<FWORD32>>    array;
     } l;
     struct Short
     {
-      LNNOffsetTo<Lookup<HBUINT16> >           rowIndexTable;
-      LNNOffsetTo<Lookup<HBUINT16> >           columnIndexTable;
-      LNNOffsetTo<UnsizedArrayOf<FWORD> >      array;
+      NNOffset32To<Lookup<HBUINT16>>           rowIndexTable;
+      NNOffset32To<Lookup<HBUINT16>>           columnIndexTable;
+      NNOffset32To<UnsizedArrayOf<FWORD>>      array;
     } s;
   } u;
-  LNNOffsetTo<UnsizedArrayOf<FWORD> >  vector;
+  NNOffset32To<UnsizedArrayOf<FWORD>>  vector;
   public:
   DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 24);
 };
@@ -733,8 +731,8 @@ struct KerxSubTableHeader
 {
   typedef ExtendedTypes Types;
 
-  unsigned int tuple_count () const { return tupleCount; }
-  bool is_horizontal () const       { return !(coverage & Vertical); }
+  unsigned   tuple_count () const { return tupleCount; }
+  bool     is_horizontal () const { return !(coverage & Vertical); }
 
   enum Coverage
   {
@@ -771,17 +769,17 @@ struct KerxSubTable
   unsigned int get_size () const { return u.header.length; }
   unsigned int get_type () const { return u.header.coverage & u.header.SubtableType; }
 
-  template <typename context_t>
-  typename context_t::return_t dispatch (context_t *c) const
+  template <typename context_t, typename ...Ts>
+  typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
   {
     unsigned int subtable_type = get_type ();
     TRACE_DISPATCH (this, subtable_type);
     switch (subtable_type) {
-    case 0:    return_trace (c->dispatch (u.format0));
-    case 1:    return_trace (c->dispatch (u.format1));
-    case 2:    return_trace (c->dispatch (u.format2));
-    case 4:    return_trace (c->dispatch (u.format4));
-    case 6:    return_trace (c->dispatch (u.format6));
+    case 0:    return_trace (c->dispatch (u.format0, std::forward<Ts> (ds)...));
+    case 1:    return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
+    case 2:    return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...));
+    case 4:    return_trace (c->dispatch (u.format4, std::forward<Ts> (ds)...));
+    case 6:    return_trace (c->dispatch (u.format6, std::forward<Ts> (ds)...));
     default:   return_trace (c->default_return_value ());
     }
   }
@@ -830,7 +828,7 @@ struct KerxTable
     for (unsigned int i = 0; i < count; i++)
     {
       if (st->get_type () == 1)
-        return true;
+       return true;
       st = &StructAfter<SubTable> (*st);
     }
     return false;
@@ -845,7 +843,7 @@ struct KerxTable
     for (unsigned int i = 0; i < count; i++)
     {
       if (st->u.header.coverage & st->u.header.CrossStream)
-        return true;
+       return true;
       st = &StructAfter<SubTable> (*st);
     }
     return false;
@@ -862,7 +860,7 @@ struct KerxTable
     {
       if ((st->u.header.coverage & (st->u.header.Variation | st->u.header.CrossStream)) ||
          !st->u.header.is_horizontal ())
-        continue;
+       continue;
       v += st->get_kerning (left, right);
       st = &StructAfter<SubTable> (*st);
     }
@@ -883,7 +881,7 @@ struct KerxTable
       bool reverse;
 
       if (!T::Types::extended && (st->u.header.coverage & st->u.header.Variation))
-        goto skip;
+       goto skip;
 
       if (HB_DIRECTION_IS_HORIZONTAL (c->buffer->props.direction) != st->u.header.is_horizontal ())
        goto skip;
@@ -891,14 +889,14 @@ struct KerxTable
       reverse = bool (st->u.header.coverage & st->u.header.Backwards) !=
                HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);
 
-      if (!c->buffer->message (c->font, "start %c%c%c%c subtable %d", HB_UNTAG (thiz()->tableTag), c->lookup_index))
+      if (!c->buffer->message (c->font, "start subtable %d", c->lookup_index))
        goto skip;
 
       if (!seenCrossStream &&
          (st->u.header.coverage & st->u.header.CrossStream))
       {
-        /* Attach all glyphs into a chain. */
-        seenCrossStream = true;
+       /* Attach all glyphs into a chain. */
+       seenCrossStream = true;
        hb_glyph_position_t *pos = c->buffer->pos;
        unsigned int count = c->buffer->len;
        for (unsigned int i = 0; i < count; i++)
@@ -923,7 +921,7 @@ struct KerxTable
       if (reverse)
        c->buffer->reverse ();
 
-      (void) c->buffer->message (c->font, "end %c%c%c%c subtable %d", HB_UNTAG (thiz()->tableTag), c->lookup_index);
+      (void) c->buffer->message (c->font, "end subtable %d", c->lookup_index);
 
     skip:
       st = &StructAfter<SubTable> (*st);