]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/letest/PortableFontInstance.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / letest / PortableFontInstance.cpp
index c96b6ed762c5fe59c97cb7f612dd886843d94b4c..26c7fbd80e16643a7bea8d9a56ce2668460ceff5 100644 (file)
@@ -1,7 +1,9 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
  *******************************************************************************
  *
- *   Copyright (C) 1999-2006, International Business Machines
+ *   Copyright (C) 1999-2015, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
 
 #include "PortableFontInstance.h"
 
-#include "letest.h"
+//#include "letest.h"
 #include "sfnt.h"
 
 #include <string.h>
+#include <stdio.h>
+
+#if 0
+static const char *letagToStr(LETag tag, char *str) {
+  str[0]= 0xFF & (tag>>24);
+  str[1]= 0xFF & (tag>>16);
+  str[2]= 0xFF & (tag>>8);
+  str[3]= 0xFF & (tag>>0);
+  str[4]= 0;
+  return str;
+}
+#endif
 
 //
 // Finds the high bit by binary searching
@@ -74,8 +88,10 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
 
     // open the font file
     fFile = fopen(fileName, "rb");
+    //printf("Open Font: %s\n", fileName);
 
     if (fFile == NULL) {
+        printf("%s:%d: %s: FNF\n", __FILE__, __LINE__, fileName);
         status = LE_FONT_FILE_NOT_FOUND_ERROR;
         return;
     }
@@ -83,7 +99,8 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
     // read in the directory
     SFNTDirectory tempDir;
 
-    fread(&tempDir, sizeof tempDir, 1, fFile);
+    size_t numRead = fread(&tempDir, sizeof tempDir, 1, fFile);
+    (void)numRead;
 
     le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry));
     const LETag headTag = LE_HEAD_TABLE_TAG;
@@ -93,15 +110,16 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
 //  const NAMETable *nameTable = NULL;
     le_uint16 numTables = 0;
 
-    fDirectory = (const SFNTDirectory *) NEW_ARRAY(char, dirSize);
+    fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize);
 
     if (fDirectory == NULL) {
+        printf("%s:%d: %s: malloc err\n", __FILE__, __LINE__, fileName);
         status = LE_MEMORY_ALLOCATION_ERROR;
         goto error_exit;
     }
 
     fseek(fFile, 0L, SEEK_SET);
-    fread((void *) fDirectory, sizeof(char), dirSize, fFile);
+    numRead = fread((void *) fDirectory, sizeof(char), dirSize, fFile);
 
     //
     // We calculate these numbers 'cause some fonts
@@ -116,12 +134,13 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
 
     if (headTable == NULL) {
         status = LE_MISSING_FONT_TABLE_ERROR;
+        printf("%s:%d: %s: missing head table\n", __FILE__, __LINE__, fileName);
         goto error_exit;
     }
 
     fUnitsPerEM   = SWAPW(headTable->unitsPerEm);
     fFontChecksum = SWAPL(headTable->checksumAdjustment);
-    deleteTable(headTable);
+    freeFontTable(headTable);
 
     //nameTable = (NAMETable *) readFontTable(nameTag);
 
@@ -137,11 +156,12 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
     //    goto error_exit;
     //}
 
-    //deleteTable(nameTable);
+    //freeFontTable(nameTable);
 
     hheaTable = (HHEATable *) readFontTable(hheaTag);
 
     if (hheaTable == NULL) {
+        printf("%s:%d: %s: missing hhea table\n", __FILE__, __LINE__, fileName);
         status = LE_MISSING_FONT_TABLE_ERROR;
         goto error_exit;
     }
@@ -152,11 +172,12 @@ PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize
 
     fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
 
-    deleteTable((void *) hheaTable);
+    freeFontTable((void *) hheaTable);
 
     fCMAPMapper = findUnicodeMapper();
 
     if (fCMAPMapper == NULL) {
+        printf("%s:%d: %s: can't load cmap\n", __FILE__, __LINE__, fileName);
         status = LE_MISSING_FONT_TABLE_ERROR;
         goto error_exit;
     }
@@ -174,20 +195,15 @@ PortableFontInstance::~PortableFontInstance()
     if (fFile != NULL) {
         fclose(fFile);
 
-        deleteTable(fHMTXTable);
-        deleteTable(fNAMETable);
+        freeFontTable(fHMTXTable);
+        freeFontTable(fNAMETable);
 
         delete fCMAPMapper;
 
-        DELETE_ARRAY(fDirectory);
+        LE_DELETE_ARRAY(fDirectory);
     }
 }
 
-void PortableFontInstance::deleteTable(const void *table) const
-{
-    DELETE_ARRAY(table);
-}
-
 const DirectoryEntry *PortableFontInstance::findTable(LETag tag) const
 {
     if (fDirectory != NULL) {
@@ -225,26 +241,31 @@ const void *PortableFontInstance::readTable(LETag tag, le_uint32 *length) const
 
     *length = SWAPL(entry->length);
 
-    void *table = NEW_ARRAY(char, *length);
+    void *table = LE_NEW_ARRAY(char, *length);
 
     if (table != NULL) {
         fseek(fFile, SWAPL(entry->offset), SEEK_SET);
-        fread(table, sizeof(char), *length, fFile);
+        size_t numRead = fread(table, sizeof(char), *length, fFile);
+        (void)numRead;
     }
 
     return table;
 }
 
-const void *PortableFontInstance::getFontTable(LETag tableTag) const
+const void *PortableFontInstance::getFontTable(LETag tableTag, size_t &length) const
 {
-    return FontTableCache::find(tableTag);
+  return FontTableCache::find(tableTag, length);
 }
 
-const void *PortableFontInstance::readFontTable(LETag tableTag) const
+const void *PortableFontInstance::readFontTable(LETag tableTag, size_t &length) const
 {
     le_uint32 len;
 
-    return readTable(tableTag, &len);
+    const void *data= readTable(tableTag, &len);
+    length = len;
+    //char tag5[5];
+    //printf("Read %s, result %p #%d\n", letagToStr(tableTag,tag5), data,len);
+    return data;
 }
 
 CMAPMapper *PortableFontInstance::findUnicodeMapper()
@@ -276,13 +297,13 @@ const char *PortableFontInstance::getNameString(le_uint16 nameID, le_uint16 plat
     for(le_int32 i = 0; i < fNameCount; i += 1) {
         const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
         
-        if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID == encodingID) &&
+        if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
             SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
             char *name = ((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset);
             le_uint16 length = SWAPW(nameRecord->length);
-            char *result = NEW_ARRAY(char, length + 2);
+            char *result = LE_NEW_ARRAY(char, length + 2);
 
-            ARRAY_COPY(result, name, length);
+            LE_ARRAY_COPY(result, name, length);
             result[length] = result[length + 1] = 0;
 
             return result;
@@ -292,9 +313,50 @@ const char *PortableFontInstance::getNameString(le_uint16 nameID, le_uint16 plat
     return NULL;
 }
 
+const LEUnicode16 *PortableFontInstance::getUnicodeNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
+{
+    if (fNAMETable == NULL) {
+        LETag nameTag = LE_NAME_TABLE_TAG;
+        PortableFontInstance *realThis = (PortableFontInstance *) this;
+
+        realThis->fNAMETable = (const NAMETable *) readFontTable(nameTag);
+
+        if (realThis->fNAMETable != NULL) {
+            realThis->fNameCount        = SWAPW(realThis->fNAMETable->count);
+            realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
+        }
+    }
+
+    for(le_int32 i = 0; i < fNameCount; i += 1) {
+        const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
+        
+        if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
+            SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
+            LEUnicode16 *name = (LEUnicode16 *) (((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset));
+            le_uint16 length = SWAPW(nameRecord->length) / 2;
+            LEUnicode16 *result = LE_NEW_ARRAY(LEUnicode16, length + 2);
+
+            for (le_int32 c = 0; c < length; c += 1) {
+                result[c] = SWAPW(name[c]);
+            }
+
+            result[length] = 0;
+
+            return result;
+        }
+    }
+
+    return NULL;
+}
+
 void PortableFontInstance::deleteNameString(const char *name) const
 {
-    DELETE_ARRAY(name);
+    LE_DELETE_ARRAY(name);
+}
+
+void PortableFontInstance::deleteNameString(const LEUnicode16 *name) const
+{
+    LE_DELETE_ARRAY(name);
 }
 
 void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
@@ -309,7 +371,7 @@ void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) co
 
         if (maxpTable != NULL) {
             realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
-            deleteTable(maxpTable);
+            freeFontTable(maxpTable);
         }
 
         realThis->fHMTXTable = (const HMTXTable *) readFontTable(hmtxTag);
@@ -345,6 +407,23 @@ le_uint32 PortableFontInstance::getFontChecksum() const
     return fFontChecksum;
 }
 
+le_uint32 PortableFontInstance::getRawChecksum() const
+{
+  // how big is it?
+  //  fseek(fFile, 0L, SEEK_END);
+  //  long size = ftell(fFile);
+  le_int32 chksum = 0;
+  // now, calculate
+  fseek(fFile, 0L, SEEK_SET);
+  int r;
+  int count =0;
+  while((r = fgetc(fFile)) != EOF) {
+    chksum += r;
+    count ++;
+  }
+  return (le_uint32) chksum; // cast to signed
+}
+
 le_int32 PortableFontInstance::getAscent() const
 {
     return fAscent;