Bug 1228022 - part 2 - Add support for reading Mac OS Roman encoded names from SFNTNameTables draft
authorHaik Aftandilian <haftandilian@mozilla.com>
Wed, 07 Sep 2016 17:41:51 -0700
changeset 411534 50a8af29a51c1b3a3565c8728a51aba1fd02d77e
parent 410961 be0432b53495b349029a0007f43cad136ef405a6
child 411535 58df0f1b7defc24d6a2561035267cd79ce9d69a3
push id28918
push userhaftandilian@mozilla.com
push dateThu, 08 Sep 2016 07:58:17 +0000
bugs1228022
milestone51.0a1
Bug 1228022 - part 2 - Add support for reading Mac OS Roman encoded names from SFNTNameTables When reading a U16 font name from the SFNTNameTable, a name entry with platformID == 1 (Macintosh) and platformSpecificID (aka encodingID) == 0 (Roman) is read as Mac Roman and converted to U16. This patch refactors the matchers created in CreateCanonicalU16Matchers to return name encoding type instead of a boolean. The encoding type can then be used to call the appropriate decoding function. CreateCanonicalU16Matchers is also changed so that it doesn't enqueue unnecessary matchers on OS X. On OS X, if the nametable record's platformID field is PLATFORM_ID, IsUTF16Encoding() will always return false so matchers requiring both of those conditions will never match. There are several other platformSpecificID's in Mac SFNTameTables such as Japanese, Traditional Chinese, and Korean. Fonts with names in those encodings won't have their names properly encoded, but that should be OK as SFNTData::GetUniqueKey falls back to another scheme for hashing fonts if the GetU16FullName call fails. Tests on El Capitan and Sierra revealed Mac's use Microsoft/Unicode SFNTNameTable names as well as Mac/Roman. MozReview-Commit-ID: F8fyDVDwHs7
gfx/2d/SFNTNameTable.cpp
gfx/2d/SFNTNameTable.h
--- a/gfx/2d/SFNTNameTable.cpp
+++ b/gfx/2d/SFNTNameTable.cpp
@@ -22,16 +22,18 @@ static const BigEndianUint16 NAME_ID_FUL
 static const BigEndianUint16 PLATFORM_ID_UNICODE = 0;
 static const BigEndianUint16 PLATFORM_ID_MAC = 1;
 static const BigEndianUint16 PLATFORM_ID_MICROSOFT = 3;
 
 static const BigEndianUint16 ENCODING_ID_MICROSOFT_SYMBOL = 0;
 static const BigEndianUint16 ENCODING_ID_MICROSOFT_UNICODEBMP = 1;
 static const BigEndianUint16 ENCODING_ID_MICROSOFT_UNICODEFULL = 10;
 
+static const BigEndianUint16 ENCODING_ID_MAC_ROMAN = 0;
+
 static const BigEndianUint16 LANG_ID_MAC_ENGLISH = 0;
 
 static const BigEndianUint16 LANG_ID_MICROSOFT_EN_US = 0x0409;
 
 #pragma pack(push, 1)
 
 // Name table has a header, followed by name records, followed by string data.
 struct NameHeader
@@ -48,16 +50,25 @@ struct NameRecord
   BigEndianUint16 languageID;
   BigEndianUint16 nameID;
   BigEndianUint16 length;     // String length in bytes.
   BigEndianUint16 offset;     // String offset from start of storage in bytes.
 };
 
 #pragma pack(pop)
 
+enum ENameDecoder : int
+{
+  eNameDecoderUTF16,
+#if defined(XP_MACOSX)
+  eNameDecoderMacRoman,
+#endif
+  eNameDecoderNone
+};
+
 /* static */
 UniquePtr<SFNTNameTable>
 SFNTNameTable::Create(const uint8_t *aNameData, uint32_t aDataLength)
 {
   MOZ_ASSERT(aNameData);
 
   if (aDataLength < sizeof(NameHeader)) {
     gfxWarning() << "Name data too short to contain NameHeader.";
@@ -93,86 +104,112 @@ SFNTNameTable::SFNTNameTable(const NameH
                                                      + sizeof(NameHeader)))
   , mEndOfRecords(mFirstRecord + aNameHeader->count)
   , mStringData(aNameData + aNameHeader->stringOffset)
   , mStringDataLength(aDataLength - aNameHeader->stringOffset)
 {
   MOZ_ASSERT(reinterpret_cast<const uint8_t*>(aNameHeader) == aNameData);
 }
 
-#if defined(XP_MACOSX)
-static const BigEndianUint16 CANONICAL_LANG_ID = LANG_ID_MAC_ENGLISH;
-static const BigEndianUint16 PLATFORM_ID = PLATFORM_ID_MAC;
-#else
-static const BigEndianUint16 CANONICAL_LANG_ID = LANG_ID_MICROSOFT_EN_US;
-static const BigEndianUint16 PLATFORM_ID = PLATFORM_ID_MICROSOFT;
-#endif
-
 static bool
 IsUTF16Encoding(const NameRecord *aNameRecord)
 {
   if (aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
       (aNameRecord->encodingID == ENCODING_ID_MICROSOFT_UNICODEBMP ||
        aNameRecord->encodingID == ENCODING_ID_MICROSOFT_SYMBOL)) {
     return true;
   }
 
   if (aNameRecord->platformID == PLATFORM_ID_UNICODE) {
     return true;
   }
 
   return false;
 }
 
+#if defined(XP_MACOSX)
+static bool
+IsMacRomanEncoding(const NameRecord *aNameRecord)
+{
+  if (aNameRecord->platformID == PLATFORM_ID_MAC &&
+      aNameRecord->encodingID == ENCODING_ID_MAC_ROMAN) {
+    return true;
+  }
+
+  return false;
+}
+#endif
+
 static NameRecordMatchers*
 CreateCanonicalU16Matchers(const BigEndianUint16& aNameID)
 {
+  // For Windows, we return only Microsoft platform name record
+  // matchers. On Mac, we return matchers for both Microsoft platform
+  // records and Mac platform records.
   NameRecordMatchers *matchers = new NameRecordMatchers();
 
   // First, look for the English name (this will normally succeed).
   if (!matchers->append(
     [=](const NameRecord *aNameRecord) {
-        return aNameRecord->nameID == aNameID &&
-               aNameRecord->languageID == CANONICAL_LANG_ID &&
-               aNameRecord->platformID == PLATFORM_ID &&
-               IsUTF16Encoding(aNameRecord);
+        if (aNameRecord->nameID == aNameID &&
+            aNameRecord->languageID == LANG_ID_MICROSOFT_EN_US &&
+            aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
+            IsUTF16Encoding(aNameRecord)) {
+          return eNameDecoderUTF16;
+        } else {
+          return eNameDecoderNone;
+        }
     })) {
     MOZ_CRASH();
   }
 
   // Second, look for all languages.
   if (!matchers->append(
     [=](const NameRecord *aNameRecord) {
-        return aNameRecord->nameID == aNameID &&
-               aNameRecord->platformID == PLATFORM_ID &&
-               IsUTF16Encoding(aNameRecord);
+        if (aNameRecord->nameID == aNameID &&
+            aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
+            IsUTF16Encoding(aNameRecord)) {
+          return eNameDecoderUTF16;
+        } else {
+          return eNameDecoderNone;
+        }
     })) {
     MOZ_CRASH();
   }
 
 #if defined(XP_MACOSX)
-  // On Mac may be dealing with font that only has Microsoft name entries.
+  // First, look for the English name.
   if (!matchers->append(
     [=](const NameRecord *aNameRecord) {
-        return aNameRecord->nameID == aNameID &&
-               aNameRecord->languageID == LANG_ID_MICROSOFT_EN_US &&
-               aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
-               IsUTF16Encoding(aNameRecord);
+        if (aNameRecord->nameID == aNameID &&
+            aNameRecord->languageID == LANG_ID_MAC_ENGLISH &&
+            aNameRecord->platformID == PLATFORM_ID_MAC &&
+            IsMacRomanEncoding(aNameRecord)) {
+          return eNameDecoderMacRoman;
+        } else  {
+          return eNameDecoderNone;
+        }
     })) {
     MOZ_CRASH();
   }
+
+  // Second, look for all languages.
   if (!matchers->append(
     [=](const NameRecord *aNameRecord) {
-        return aNameRecord->nameID == aNameID &&
-               aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
-               IsUTF16Encoding(aNameRecord);
+        if (aNameRecord->nameID == aNameID &&
+            aNameRecord->platformID == PLATFORM_ID_MAC &&
+            IsMacRomanEncoding(aNameRecord)) {
+          return eNameDecoderMacRoman;
+        } else  {
+          return eNameDecoderNone;
+        }
     })) {
     MOZ_CRASH();
   }
-#endif
+#endif /* defined(XP_MACOSX) */
 
   return matchers;
 }
 
 static const NameRecordMatchers&
 FullNameMatchers()
 {
   static const NameRecordMatchers *sFullNameMatchers =
@@ -225,29 +262,39 @@ bool
 SFNTNameTable::ReadU16Name(const NameRecordMatchers& aMatchers,
                            mozilla::u16string& aU16Name)
 {
   MOZ_ASSERT(!aMatchers.empty());
 
   for (size_t i = 0; i < aMatchers.length(); ++i) {
     const NameRecord* record = mFirstRecord;
     while (record != mEndOfRecords) {
-      if (aMatchers[i](record)) {
-        return ReadU16NameFromRecord(record, aU16Name);
+      switch (aMatchers[i](record)) {
+        case eNameDecoderUTF16:
+          return ReadU16NameFromU16Record(record, aU16Name);
+#if defined(XP_MACOSX)
+        case eNameDecoderMacRoman:
+          return ReadU16NameFromMacRomanRecord(record, aU16Name);
+#endif
+        case eNameDecoderNone:
+          break;
+        default:
+          MOZ_CRASH("Invalid matcher encoding type");
+          break;
       }
       ++record;
     }
   }
 
   return false;
 }
 
 bool
-SFNTNameTable::ReadU16NameFromRecord(const NameRecord *aNameRecord,
-                                     mozilla::u16string& aU16Name)
+SFNTNameTable::ReadU16NameFromU16Record(const NameRecord *aNameRecord,
+                                        mozilla::u16string& aU16Name)
 {
   uint32_t offset = aNameRecord->offset;
   uint32_t length = aNameRecord->length;
   if (mStringDataLength < offset + length) {
     gfxWarning() << "Name data too short to contain name string.";
     return false;
   }
 
@@ -256,10 +303,55 @@ SFNTNameTable::ReadU16NameFromRecord(con
   UniquePtr<char16_t[]> nameData(new char16_t[actualLength]);
   NativeEndian::copyAndSwapFromBigEndian(nameData.get(), startOfName,
                                          actualLength);
 
   aU16Name.assign(nameData.get(), actualLength);
   return true;
 }
 
+#if defined(XP_MACOSX)
+bool
+SFNTNameTable::ReadU16NameFromMacRomanRecord(const NameRecord *aNameRecord,
+                                             mozilla::u16string& aU16Name)
+{
+  uint32_t offset = aNameRecord->offset;
+  uint32_t length = aNameRecord->length;
+  if (mStringDataLength < offset + length) {
+    gfxWarning() << "Name data too short to contain name string.";
+    return false;
+  }
+  if (length > INT_MAX) {
+    gfxWarning() << "Name record too long to decode.";
+    return false;
+  }
+
+  // length (in bytes) of the Mac Roman encoded string
+  int encodedLength = static_cast<int>(length);
+
+  // pointer to the Mac Roman encoded string in the name record
+  const char *encodedStr = (char *)(mStringData + offset);
+
+  CFStringRef cfString;
+  cfString = CFStringCreateWithBytes(kCFAllocatorDefault,
+                                     reinterpret_cast<const UInt8*>(encodedStr),
+                                     encodedLength, kCFStringEncodingMacRoman,
+                                     false);
+
+  // length (in UTF-16 code pairs) of the decoded string
+  CFIndex decodedLength = CFStringGetLength(cfString);
+
+  UniquePtr<UniChar[]> u16Buffer = MakeUnique<UniChar[]>(decodedLength);
+
+  // convert decoded string to u16 character array
+  CFStringGetCharacters(cfString, CFRangeMake(0, decodedLength),
+                        u16Buffer.get());
+
+  CFRelease(cfString);
+
+  aU16Name.assign(reinterpret_cast<char16_t*>(u16Buffer.get()), decodedLength);
+
+  return true;
+}
+#endif
+
 } // gfx
 } // mozilla
--- a/gfx/2d/SFNTNameTable.h
+++ b/gfx/2d/SFNTNameTable.h
@@ -12,18 +12,19 @@
 #include "mozilla/Vector.h"
 #include "u16string.h"
 
 namespace mozilla {
 namespace gfx {
 
 struct NameHeader;
 struct NameRecord;
+enum ENameDecoder : int;
 
-typedef Vector<function<bool(const NameRecord*)>> NameRecordMatchers;
+typedef Vector<function<ENameDecoder(const NameRecord*)>> NameRecordMatchers;
 
 class SFNTNameTable final
 {
 public:
 
   /**
    * Creates a SFNTNameTable if the header data is valid. Note that the data is
    * NOT copied, so must exist for the lifetime of the table.
@@ -33,32 +34,37 @@ public:
    * @return UniquePtr to a SFNTNameTable or nullptr if the header is invalid.
    */
   static UniquePtr<SFNTNameTable> Create(const uint8_t *aNameData,
                                          uint32_t aDataLength);
 
   /**
    * Gets the full name from the name table. If the full name string is not
    * present it will use the family space concatenated with the style.
-   * This will only read names that are already UTF16.
+   * This will only read names that are already UTF16 or Mac OS Roman.
    *
    * @param aU16FullName string to be populated with the full name.
    * @return true if the full name is successfully read.
    */
   bool GetU16FullName(mozilla::u16string& aU16FullName);
 
 private:
 
   SFNTNameTable(const NameHeader *aNameHeader, const uint8_t *aNameData,
                 uint32_t aDataLength);
 
   bool ReadU16Name(const NameRecordMatchers& aMatchers, mozilla::u16string& aU16Name);
 
-  bool ReadU16NameFromRecord(const NameRecord *aNameRecord,
-                             mozilla::u16string& aU16Name);
+  bool ReadU16NameFromU16Record(const NameRecord *aNameRecord,
+                                mozilla::u16string& aU16Name);
+
+#if defined(XP_MACOSX)
+  bool ReadU16NameFromMacRomanRecord(const NameRecord *aNameRecord,
+                                     mozilla::u16string& aU16Name);
+#endif
 
   const NameRecord *mFirstRecord;
   const NameRecord *mEndOfRecords;
   const uint8_t *mStringData;
   const uint32_t mStringDataLength;
 };
 
 } // gfx