src/hb-ot-font.cc       |    9 +--
 src/hb-ot-post-table.hh |  139 ++++++++++++++++++++++++++----------------------
 2 files changed, 81 insertions(+), 67 deletions(-)

New commits:
commit 543bd58d7a4fbe285ec9304ff3223a728dbdbb76
Author: Behdad Esfahbod <[email protected]>
Date:   Wed Oct 25 17:40:50 2017 -0600

    [post] Touch up and fixes
    
    The get_glyph_from_name() is incorrect; needs fixing.

diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index dff27bf4..c0ce89df 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -320,18 +320,15 @@ struct hb_ot_face_post_accelerator_t
   }
 
   inline bool get_glyph_name (hb_codepoint_t glyph,
-                              char *name, unsigned int size) const
+                             char *name, unsigned int size) const
   {
-    if (unlikely (!name) || unlikely(!size))
-      return false;
-
     return this->post->get_glyph_name (glyph, name, size, this->post_len);
   }
 
   inline bool get_glyph_from_name (const char *name, int len,
-                                   hb_codepoint_t *glyph) const
+                                  hb_codepoint_t *glyph) const
   {
-    if (unlikely (!name) || unlikely(!len))
+    if (unlikely (!len))
       return false;
 
     return this->post->get_glyph_from_name (name, len, glyph, this->post_len);
diff --git a/src/hb-ot-post-table.hh b/src/hb-ot-post-table.hh
index c0d25f2b..293dda62 100644
--- a/src/hb-ot-post-table.hh
+++ b/src/hb-ot-post-table.hh
@@ -113,69 +113,80 @@ struct post
       return_trace (false);
     if (version.to_int () == 0x00020000)
     {
-      const postV2Tail &v2 = StructAfter<postV2Tail>(*this);
+      const postV2Tail &v2 = StructAfter<postV2Tail> (*this);
       return_trace (v2.sanitize (c));
     }
     return_trace (true);
   }
 
   inline bool get_glyph_name (hb_codepoint_t glyph,
-                              char *buffer, unsigned int buffer_length,
-                              unsigned int blob_len) const
+                             char *buffer, unsigned int buffer_length,
+                             unsigned int blob_len) const
   {
     if (version.to_int () == 0x00010000)
     {
       if (glyph >= NUM_FORMAT1_NAMES)
-        return false;
+       return false;
 
-      strncpy(buffer, format1_names[glyph], buffer_length);
-      buffer[buffer_length] = '\0';
+      if (!buffer_length)
+       return true;
+      strncpy (buffer, format1_names[glyph], buffer_length);
+      buffer[buffer_length - 1] = '\0';
       return true;
     }
 
     if (version.to_int () == 0x00020000)
     {
-      const postV2Tail &v2 = StructAfter<postV2Tail>(*this);
+      const postV2Tail &v2 = StructAfter<postV2Tail> (*this);
 
       if (glyph >= v2.numberOfGlyphs)
-        return false;
+       return false;
+
+      if (!buffer_length)
+       return true;
 
       unsigned int index = v2.glyphNameIndex[glyph];
-      if (index >= NUM_FORMAT1_NAMES)
+      if (index < NUM_FORMAT1_NAMES)
       {
-        unsigned int offset = min_size + v2.min_size + 2 * v2.numberOfGlyphs;
-        char* data = (char*) this + offset;
-        for (unsigned int i = 0; data < (char*) this + blob_len; i++)
-        {
-          unsigned int name_length = data[0];
-          data++;
-          if (i == index - NUM_FORMAT1_NAMES)
-          {
-            unsigned int remaining = (char*) this + blob_len - data;
-            name_length = MIN (name_length, buffer_length);
-            name_length = MIN (name_length, remaining);
-            memcpy (buffer, data, name_length);
-            buffer[name_length] = '\0';
-            return true;
-          }
-          data += name_length;
-        }
-        return false;
+       if (!buffer_length)
+         return true;
+       strncpy (buffer, format1_names[index], buffer_length);
+       buffer[buffer_length - 1] = '\0';
+       return true;
       }
-      else
+      index -= NUM_FORMAT1_NAMES;
+
+      unsigned int offset = min_size + v2.min_size + 2 * v2.numberOfGlyphs;
+      unsigned char *data = (unsigned char *) this + offset;
+      unsigned char *end = (unsigned char *) this + blob_len;
+      for (unsigned int i = 0; data < end; i++)
       {
-        strncpy(buffer, format1_names[index], buffer_length);
-        buffer[buffer_length] = '\0';
-        return true;
+       unsigned int name_length = data[0];
+       data++;
+       if (i == index)
+       {
+         if (unlikely (!name_length))
+           return false;
+
+         unsigned int remaining = end - data;
+         name_length = MIN (name_length, buffer_length - 1);
+         name_length = MIN (name_length, remaining);
+         memcpy (buffer, data, name_length);
+         buffer[name_length] = '\0';
+         return true;
+       }
+       data += name_length;
       }
+
+      return false;
     }
 
     return false;
   }
 
   inline bool get_glyph_from_name (const char *name, int len,
-                                   hb_codepoint_t *glyph,
-                                   unsigned int blob_len) const
+                                  hb_codepoint_t *glyph,
+                                  unsigned int blob_len) const
   {
     if (len < 0)
       len = strlen (name);
@@ -184,45 +195,51 @@ struct post
     {
       for (int i = 0; i < NUM_FORMAT1_NAMES; i++)
       {
-        if (strncmp (name, format1_names[i], len) == 0)
-        {
-          *glyph = i;
-          return true;
-        }
+       if (strncmp (name, format1_names[i], len) == 0 && format1_names[len] == 
'\0')
+       {
+         *glyph = i;
+         return true;
+       }
       }
       return false;
     }
 
     if (version.to_int () == 0x00020000)
     {
-      const postV2Tail &v2 = StructAfter<postV2Tail>(*this);
+      const postV2Tail &v2 = StructAfter<postV2Tail> (*this);
       unsigned int offset = min_size + v2.min_size + 2 * v2.numberOfGlyphs;
       char* data = (char*) this + offset;
 
+
+      /* XXX The following code is wrong. */
+      return false;
       for (hb_codepoint_t gid = 0; gid < v2.numberOfGlyphs; gid++)
       {
-        unsigned int index = v2.glyphNameIndex[gid];
-        if (index >= NUM_FORMAT1_NAMES)
-        {
-          for (unsigned int i = 0; data < (char*) this + blob_len; i++)
-          {
-            unsigned int name_length = data[0];
-            unsigned int remaining = (char*) this + blob_len - data - 1;
-            name_length = MIN (name_length, remaining);
-            if (name_length == len && strncmp (name, data + 1, len) == 0)
-            {
-              *glyph = gid;
-              return true;
-            }
-            data += name_length + 1;
-          }
-          return false;
-        }
-        else if (strncmp (name, format1_names[index], len) == 0)
-        {
-          *glyph = gid;
-          return true;
-        }
+       unsigned int index = v2.glyphNameIndex[gid];
+       if (index < NUM_FORMAT1_NAMES)
+       {
+         if (strncmp (name, format1_names[index], len) == 0 && 
format1_names[len] == '\0')
+         {
+           *glyph = gid;
+           return true;
+         }
+         continue;
+       }
+       index -= NUM_FORMAT1_NAMES;
+
+       for (unsigned int i = 0; data < (char*) this + blob_len; i++)
+       {
+         unsigned int name_length = data[0];
+         unsigned int remaining = (char*) this + blob_len - data - 1;
+         name_length = MIN (name_length, remaining);
+         if (name_length == (unsigned int) len && strncmp (name, data + 1, 
len) == 0)
+         {
+           *glyph = gid;
+           return true;
+         }
+         data += name_length + 1;
+       }
+       return false;
       }
 
       return false;
_______________________________________________
HarfBuzz mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/harfbuzz

Reply via email to