[name] Hook things up
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 24 Oct 2018 03:51:53 +0000 (20:51 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Sat, 27 Oct 2018 04:22:26 +0000 (21:22 -0700)
Accept Mac Latin name entries as ASCII as well.

src/hb-ot-name-table.hh
src/hb-ot-name.cc
src/hb-utf.hh

index f2eb496..2690667 100644 (file)
@@ -84,7 +84,8 @@ struct NameRecord
     /* Symbol. */
     if (p == 3 && e ==  0) return 8;
 
-    /* TODO Apple MacRoman (0:1). */
+    /* We treat all Mac Latin names as ASCII only. */
+    if (p == 1 && e ==  0) return 10; /* 10 is magic number :| */
 
     return UNSUPPORTED;
   }
@@ -198,11 +199,15 @@ struct name
       }
 
       this->names.qsort (_hb_ot_name_entry_cmp);
-      /* Walk and pick best only for each name_id,language pair... */
+      /* Walk and pick best only for each name_id,language pair,
+       * while dropping unsupported encodings. */
       unsigned int j = 0;
-      for (unsigned int i = 1; i < this->names.len; i++)
+      for (unsigned int i = 0; i < this->names.len; i++)
       {
-        if (this->names[i - 1].name_id  == this->names[i].name_id &&
+        if (this->names[i].entry_score == UNSUPPORTED)
+         continue;
+        if (i &&
+           this->names[i - 1].name_id  == this->names[i].name_id &&
            this->names[i - 1].language == this->names[i].language)
          continue;
        this->names[j++] = this->names[i];
@@ -217,7 +222,8 @@ struct name
     }
 
     inline int get_index (hb_name_id_t   name_id,
-                         hb_language_t  language) const
+                         hb_language_t  language,
+                         unsigned int  *width=nullptr) const
     {
       const hb_ot_name_entry_t key = {name_id, {0}, language};
       const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *)
@@ -226,7 +232,13 @@ struct name
                                                    this->names.len,
                                                    sizeof (key),
                                                    _hb_ot_name_entry_cmp_key);
-      return entry ? entry->entry_index : -1;
+      if (!entry)
+        return -1;
+
+      if (width)
+        *width = entry->entry_score < 10 ? 2 : 1;
+
+      return entry->entry_index;
     }
 
     private:
index 626e063..56f8933 100644 (file)
@@ -106,13 +106,17 @@ hb_ot_name_get_utf (hb_face_t     *face,
 {
   const OT::name_accelerator_t &name = _get_name (face);
 
-  int idx = name.get_index (name_id, language);
+  unsigned int width;
+  int idx = name.get_index (name_id, language, &width);
   if (idx != -1)
   {
     hb_bytes_t bytes = name.table->get_name (idx);
 
-    if (true /*UTF16-BE*/)
+    if (width == 2) /* UTF16-BE */
       return hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (&bytes, text_size, text);
+
+    if (width == 1) /* ASCII */
+      return hb_ot_name_convert_utf<hb_ascii_t, utf_t> (&bytes, text_size, text);
   }
 
   if (text_size)
index 5e2faeb..f78d549 100644 (file)
@@ -398,4 +398,59 @@ struct hb_latin1_t
   }
 };
 
+
+struct hb_ascii_t
+{
+  typedef uint8_t codepoint_t;
+
+  static inline const codepoint_t *
+  next (const codepoint_t *text,
+       const codepoint_t *end HB_UNUSED,
+       hb_codepoint_t *unicode,
+       hb_codepoint_t replacement HB_UNUSED)
+  {
+    *unicode = *text++;
+    if (*unicode >= 0x100)
+      *unicode = replacement;
+    return text;
+  }
+
+  static inline const codepoint_t *
+  prev (const codepoint_t *text,
+       const codepoint_t *start HB_UNUSED,
+       hb_codepoint_t *unicode,
+       hb_codepoint_t replacement)
+  {
+    *unicode = *--text;
+    if (*unicode >= 0x0080)
+      *unicode = replacement;
+    return text;
+  }
+
+  static inline unsigned int
+  strlen (const codepoint_t *text)
+  {
+    unsigned int l = 0;
+    while (*text++) l++;
+    return l;
+  }
+
+  static inline unsigned int
+  encode_len (hb_codepoint_t unicode HB_UNUSED)
+  {
+    return 1;
+  }
+
+  static inline codepoint_t *
+  encode (codepoint_t *text,
+         const codepoint_t *end HB_UNUSED,
+         hb_codepoint_t unicode)
+  {
+    if (unlikely (unicode >= 0x0080u))
+      unicode = '?';
+    *text++ = unicode;
+    return text;
+  }
+};
+
 #endif /* HB_UTF_HH */