Imported Upstream version 2.3.1
[platform/upstream/harfbuzz.git] / src / hb-aat-layout-trak-table.hh
index 0617e23..0c8e455 100644 (file)
@@ -28,9 +28,9 @@
 #ifndef HB_AAT_LAYOUT_TRAK_TABLE_HH
 #define HB_AAT_LAYOUT_TRAK_TABLE_HH
 
-#include "hb-aat-layout-common-private.hh"
-#include "hb-ot-layout-private.hh"
-#include "hb-open-type-private.hh"
+#include "hb-aat-layout-common.hh"
+#include "hb-ot-layout.hh"
+#include "hb-open-type.hh"
 
 /*
  * trak -- Tracking
@@ -46,29 +46,27 @@ struct TrackTableEntry
 {
   friend struct TrackData;
 
-  inline bool sanitize (hb_sanitize_context_t *c, const void *base,
-                       unsigned int size) const
-  {
-    TRACE_SANITIZE (this);
-    return_trace (likely (c->check_struct (this) &&
-                         (valuesZ.sanitize (c, base, size))));
-  }
+  float get_track_value () const { return track.to_float (); }
 
-  private:
-  inline float get_track_value () const
-  {
-    return track.to_float ();
-  }
+  int get_value (const void *base, unsigned int index,
+                unsigned int table_size) const
+  { return (base+valuesZ).as_array (table_size)[index]; }
 
-  inline int get_value (const void *base, unsigned int index) const
+  public:
+  bool sanitize (hb_sanitize_context_t *c, const void *base,
+                unsigned int table_size) const
   {
-    return (base+valuesZ)[index];
+    TRACE_SANITIZE (this);
+    return_trace (likely (c->check_struct (this) &&
+                         (valuesZ.sanitize (c, base, table_size))));
   }
 
   protected:
   Fixed                track;          /* Track value for this record. */
-  NameID       trackNameID;    /* The 'name' table index for this track */
-  OffsetTo<UnsizedArrayOf<FWORD> >
+  NameID       trackNameID;    /* The 'name' table index for this track.
+                                * (a short word or phrase like "loose"
+                                * or "very tight") */
+  NNOffsetTo<UnsizedArrayOf<FWORD> >
                valuesZ;        /* Offset from start of tracking table to
                                 * per-size tracking values for this track. */
 
@@ -78,15 +76,22 @@ struct TrackTableEntry
 
 struct TrackData
 {
-  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
+  float interpolate_at (unsigned int idx,
+                       float target_size,
+                       const TrackTableEntry &trackTableEntry,
+                       const void *base) const
   {
-    TRACE_SANITIZE (this);
-    return_trace (c->check_struct (this) &&
-                 sizeTable.sanitize (c, base, nSizes) &&
-                 trackTable.sanitize (c, nTracks, base, nSizes));
+    unsigned int sizes = nSizes;
+    hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
+
+    float s0 = size_table[idx].to_float ();
+    float s1 = size_table[idx + 1].to_float ();
+    float t = unlikely (s0 == s1) ? 0.f : (target_size - s0) / (s1 - s0);
+    return t * trackTableEntry.get_value (base, idx + 1, sizes) +
+          (1.f - t) * trackTableEntry.get_value (base, idx, sizes);
   }
 
-  inline float get_tracking (const void *base, float ptem) const
+  int get_tracking (const void *base, float ptem) const
   {
     /* CoreText points are CSS pixels (96 per inch),
      * NOT typographic points (72 per inch).
@@ -94,48 +99,58 @@ struct TrackData
      * https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Explained/Explained.html
      */
     float csspx = ptem * 96.f / 72.f;
-    Fixed fixed_size;
-    fixed_size.set_float (csspx);
 
-    /* XXX Clean this up. Make it work with nSizes==1 and 0. */
+    /*
+     * Choose track.
+     */
+    const TrackTableEntry *trackTableEntry = nullptr;
+    unsigned int count = nTracks;
+    for (unsigned int i = 0; i < count; i++)
+    {
+      /* Note: Seems like the track entries are sorted by values.  But the
+       * spec doesn't explicitly say that.  It just mentions it in the example. */
 
-    unsigned int sizes = nSizes;
+      /* For now we only seek for track entries with zero tracking value */
 
-    const TrackTableEntry *trackTableEntry = nullptr;
-    for (unsigned int i = 0; i < sizes; ++i)
-      // For now we only seek for track entries with zero tracking value
       if (trackTable[i].get_track_value () == 0.f)
-        trackTableEntry = &trackTable[0];
-
-    // We couldn't match any, exit
+      {
+       trackTableEntry = &trackTable[i];
+       break;
+      }
+    }
     if (!trackTableEntry) return 0.;
 
-    /* TODO bfind() */
+    /*
+     * Choose size.
+     */
+    unsigned int sizes = nSizes;
+    if (!sizes) return 0.;
+    if (sizes == 1) return trackTableEntry->get_value (base, 0, sizes);
+
+    hb_array_t<const Fixed> size_table ((base+sizeTable).arrayZ, sizes);
     unsigned int size_index;
-    UnsizedArrayOf<Fixed> size_table = base+sizeTable;
-    for (size_index = 0; size_index < sizes; ++size_index)
-      if (size_table[size_index] >= fixed_size)
+    for (size_index = 0; size_index < sizes - 1; size_index++)
+      if (size_table[size_index].to_float () >= csspx)
         break;
 
-    // TODO(ebraminio): We don't attempt to extrapolate to larger or
-    // smaller values for now but we should do, per spec
-    if (size_index == sizes)
-      return trackTableEntry->get_value (base, sizes - 1);
-    if (size_index == 0 || size_table[size_index] == fixed_size)
-      return trackTableEntry->get_value (base, size_index);
-
-    float s0 = size_table[size_index - 1].to_float ();
-    float s1 = size_table[size_index].to_float ();
-    float t = (csspx - s0) / (s1 - s0);
-    return (float) t * trackTableEntry->get_value (base, size_index) +
-          ((float) 1.0 - t) * trackTableEntry->get_value (base, size_index - 1);
+    return round (interpolate_at (size_index ? size_index - 1 : 0, csspx,
+                                 *trackTableEntry, base));
+  }
+
+  bool sanitize (hb_sanitize_context_t *c, const void *base) const
+  {
+    TRACE_SANITIZE (this);
+    return_trace (likely (c->check_struct (this) &&
+                         sizeTable.sanitize (c, base, nSizes) &&
+                         trackTable.sanitize (c, nTracks, base, nSizes)));
   }
 
   protected:
   HBUINT16     nTracks;        /* Number of separate tracks included in this table. */
   HBUINT16     nSizes;         /* Number of point sizes included in this table. */
-  LOffsetTo<UnsizedArrayOf<Fixed> >
-               sizeTable;      /* Offset to array[nSizes] of size values. */
+  LOffsetTo<UnsizedArrayOf<Fixed>, false>
+               sizeTable;      /* Offset from start of the tracking table to
+                                * Array[nSizes] of size values.. */
   UnsizedArrayOf<TrackTableEntry>
                trackTable;     /* Array[nTracks] of TrackTableEntry records. */
 
@@ -145,21 +160,16 @@ struct TrackData
 
 struct trak
 {
-  static const hb_tag_t tableTag = HB_AAT_TAG_trak;
-
-  inline bool sanitize (hb_sanitize_context_t *c) const
-  {
-    TRACE_SANITIZE (this);
+  static constexpr hb_tag_t tableTag = HB_AAT_TAG_trak;
 
-    return_trace (unlikely (c->check_struct (this) &&
-                           horizData.sanitize (c, this, this) &&
-                           vertData.sanitize (c, this, this)));
-  }
+  bool has_data () const { return version.to_int (); }
 
-  inline bool apply (hb_aat_apply_context_t *c) const
+  bool apply (hb_aat_apply_context_t *c) const
   {
     TRACE_APPLY (this);
 
+    hb_mask_t trak_mask = c->plan->trak_mask;
+
     const float ptem = c->font->ptem;
     if (unlikely (ptem <= 0.f))
       return_trace (false);
@@ -168,41 +178,57 @@ struct trak
     if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
     {
       const TrackData &trackData = this+horizData;
-      float tracking = trackData.get_tracking (this, ptem);
-      hb_position_t advance_to_add = c->font->em_scalef_x (tracking / 2);
+      int tracking = trackData.get_tracking (this, ptem);
+      hb_position_t offset_to_add = c->font->em_scalef_x (tracking / 2);
+      hb_position_t advance_to_add = c->font->em_scalef_x (tracking);
       foreach_grapheme (buffer, start, end)
       {
-       /* TODO This is wrong. */
+        if (!(buffer->info[start].mask & trak_mask)) continue;
        buffer->pos[start].x_advance += advance_to_add;
-       buffer->pos[end].x_advance += advance_to_add;
+       buffer->pos[start].x_offset += offset_to_add;
       }
     }
     else
     {
       const TrackData &trackData = this+vertData;
-      float tracking = trackData.get_tracking (this, ptem);
-      hb_position_t advance_to_add = c->font->em_scalef_y (tracking / 2);
+      int tracking = trackData.get_tracking (this, ptem);
+      hb_position_t offset_to_add = c->font->em_scalef_y (tracking / 2);
+      hb_position_t advance_to_add = c->font->em_scalef_y (tracking);
       foreach_grapheme (buffer, start, end)
       {
-       /* TODO This is wrong. */
+        if (!(buffer->info[start].mask & trak_mask)) continue;
        buffer->pos[start].y_advance += advance_to_add;
-       buffer->pos[end].y_advance += advance_to_add;
+       buffer->pos[start].y_offset += offset_to_add;
       }
     }
 
     return_trace (true);
   }
 
+  bool sanitize (hb_sanitize_context_t *c) const
+  {
+    TRACE_SANITIZE (this);
+
+    return_trace (likely (c->check_struct (this) &&
+                         version.major == 1 &&
+                         horizData.sanitize (c, this, this) &&
+                         vertData.sanitize (c, this, this)));
+  }
+
   protected:
-  FixedVersion<>       version;        /* Version of the tracking table--currently
-                                        * 0x00010000u for version 1.0. */
-  HBUINT16             format;         /* Format of the tracking table */
-  OffsetTo<TrackData>  horizData;      /* TrackData for horizontal text */
-  OffsetTo<TrackData>  vertData;       /* TrackData for vertical text */
-  HBUINT16             reserved;       /* Reserved. Set to 0. */
+  FixedVersion<>version;       /* Version of the tracking table
+                                        * (0x00010000u for version 1.0). */
+  HBUINT16     format;         /* Format of the tracking table (set to 0). */
+  OffsetTo<TrackData>
+               horizData;      /* Offset from start of tracking table to TrackData
+                                * for horizontal text (or 0 if none). */
+  OffsetTo<TrackData>
+               vertData;       /* Offset from start of tracking table to TrackData
+                                * for vertical text (or 0 if none). */
+  HBUINT16     reserved;       /* Reserved. Set to 0. */
 
   public:
-  DEFINE_SIZE_MIN (12);
+  DEFINE_SIZE_STATIC (12);
 };
 
 } /* namespace AAT */