]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/letest/PortableFontInstance.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / test / letest / PortableFontInstance.cpp
index b84102aad4edcb0a817c3509703469c4551f6715..14e3f520a311f07e3903a42c07024bc3b4b48f47 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *******************************************************************************
  *
- *   Copyright (C) 1999-2003, International Business Machines
+ *   Copyright (C) 1999-2008, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
@@ -19,6 +19,7 @@
 
 #include "PortableFontInstance.h"
 
+#include "letest.h"
 #include "sfnt.h"
 
 #include <string.h>
@@ -63,10 +64,9 @@ le_int8 PortableFontInstance::highBit(le_int32 value)
     return bit;
 }
 
-
-PortableFontInstance::PortableFontInstance(char *fileName, float pointSize, LEErrorCode &status)
-    : fFile(NULL), fUnitsPerEM(0), fPointSize(pointSize), fAscent(0), fDescent(0), fLeading(0),
-      fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(NULL), fNumGlyphs(0), fNumLongHorMetrics(0)
+PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize, LEErrorCode &status)
+    : fFile(NULL), fPointSize(pointSize), fUnitsPerEM(0), fFontChecksum(0), fAscent(0), fDescent(0), fLeading(0),
+      fDirectory(NULL), fNAMETable(NULL), fNameCount(0), fNameStringOffset(0), fCMAPMapper(NULL), fHMTXTable(NULL), fNumGlyphs(0), fNumLongHorMetrics(0)
 {
     if (LE_FAILURE(status)) {
         return;
@@ -90,9 +90,10 @@ PortableFontInstance::PortableFontInstance(char *fileName, float pointSize, LEEr
     const LETag hheaTag = LE_HHEA_TABLE_TAG;
     const HEADTable *headTable = NULL;
     const HHEATable *hheaTable = NULL;
+//  const NAMETable *nameTable = NULL;
     le_uint16 numTables = 0;
 
-    fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize);
+    fDirectory = (const SFNTDirectory *) NEW_ARRAY(char, dirSize);
 
     if (fDirectory == NULL) {
         status = LE_MEMORY_ALLOCATION_ERROR;
@@ -118,8 +119,25 @@ PortableFontInstance::PortableFontInstance(char *fileName, float pointSize, LEEr
         goto error_exit;
     }
 
-    fUnitsPerEM = SWAPW(headTable->unitsPerEm);
-    deleteTable(headTable);
+    fUnitsPerEM   = SWAPW(headTable->unitsPerEm);
+    fFontChecksum = SWAPL(headTable->checksumAdjustment);
+    freeFontTable(headTable);
+
+    //nameTable = (NAMETable *) readFontTable(nameTag);
+
+    //if (nameTable == NULL) {
+    //    status = LE_MISSING_FONT_TABLE_ERROR;
+    //    goto error_exit;
+    //}
+
+    //fFontVersionString = findName(nameTable, NAME_VERSION_STRING, PLATFORM_MACINTOSH, MACINTOSH_ROMAN, MACINTOSH_ENGLISH);
+
+    //if (fFontVersionString == NULL) {
+    //    status = LE_MISSING_FONT_TABLE_ERROR;
+    //    goto error_exit;
+    //}
+
+    //freeFontTable(nameTable);
 
     hheaTable = (HHEATable *) readFontTable(hheaTag);
 
@@ -134,7 +152,7 @@ PortableFontInstance::PortableFontInstance(char *fileName, float pointSize, LEEr
 
     fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
 
-    deleteTable((void *) hheaTable);
+    freeFontTable((void *) hheaTable);
 
     fCMAPMapper = findUnicodeMapper();
 
@@ -156,17 +174,13 @@ PortableFontInstance::~PortableFontInstance()
     if (fFile != NULL) {
         fclose(fFile);
 
-        deleteTable(fHMTXTable);
+        freeFontTable(fHMTXTable);
+        freeFontTable(fNAMETable);
 
         delete fCMAPMapper;
 
-        LE_DELETE_ARRAY(fDirectory);
+        DELETE_ARRAY(fDirectory);
     }
-};
-
-void PortableFontInstance::deleteTable(const void *table) const
-{
-    LE_DELETE_ARRAY(table);
 }
 
 const DirectoryEntry *PortableFontInstance::findTable(LETag tag) const
@@ -206,7 +220,7 @@ const void *PortableFontInstance::readTable(LETag tag, le_uint32 *length) const
 
     *length = SWAPL(entry->length);
 
-    void *table = LE_NEW_ARRAY(char, *length);
+    void *table = NEW_ARRAY(char, *length);
 
     if (table != NULL) {
         fseek(fFile, SWAPL(entry->offset), SEEK_SET);
@@ -240,6 +254,84 @@ CMAPMapper *PortableFontInstance::findUnicodeMapper()
     return CMAPMapper::createUnicodeMapper(cmap);
 }
 
+const char *PortableFontInstance::getNameString(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) {
+            char *name = ((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset);
+            le_uint16 length = SWAPW(nameRecord->length);
+            char *result = NEW_ARRAY(char, length + 2);
+
+            ARRAY_COPY(result, name, length);
+            result[length] = result[length + 1] = 0;
+
+            return result;
+        }
+    }
+
+    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 = 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);
+}
+
+void PortableFontInstance::deleteNameString(const LEUnicode16 *name) const
+{
+    DELETE_ARRAY(name);
+}
 
 void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
 {
@@ -253,7 +345,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);
@@ -274,8 +366,71 @@ void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) co
     advance.fY = 0;
 }
 
-le_bool PortableFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
+le_bool PortableFontInstance::getGlyphPoint(LEGlyphID /*glyph*/, le_int32 /*pointNumber*/, LEPoint &/*point*/) const
 {
     return FALSE;
 }
 
+le_int32 PortableFontInstance::getUnitsPerEM() const
+{
+    return fUnitsPerEM;
+}
+
+le_uint32 PortableFontInstance::getFontChecksum() const
+{
+    return fFontChecksum;
+}
+
+le_int32 PortableFontInstance::getAscent() const
+{
+    return fAscent;
+}
+
+le_int32 PortableFontInstance::getDescent() const
+{
+    return fDescent;
+}
+
+le_int32 PortableFontInstance::getLeading() const
+{
+    return fLeading;
+}
+
+// We really want to inherit this method from the superclass, but some compilers
+// issue a warning if we don't implement it...
+LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const
+{
+    return LEFontInstance::mapCharToGlyph(ch, mapper, filterZeroWidth);
+}
+
+// We really want to inherit this method from the superclass, but some compilers
+// issue a warning if we don't implement it...
+LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
+{
+    return LEFontInstance::mapCharToGlyph(ch, mapper);
+}
+
+LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch) const
+{
+    return fCMAPMapper->unicodeToGlyph(ch);
+}
+
+float PortableFontInstance::getXPixelsPerEm() const
+{
+    return fPointSize;
+}
+
+float PortableFontInstance::getYPixelsPerEm() const
+{
+    return fPointSize;
+}
+
+float PortableFontInstance::getScaleFactorX() const
+{
+    return 1.0;
+}
+
+float PortableFontInstance::getScaleFactorY() const
+{
+    return 1.0;
+}