]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/layout/LayoutEngine.cpp
ICU-511.32.tar.gz
[apple/icu.git] / icuSources / layout / LayoutEngine.cpp
index 4a3d6bb0613706e3c9d19b9e448d381ab5f5f24f..ef67b4edc8f44185575ec72b087dd3fd678d193f 100644 (file)
@@ -1,21 +1,24 @@
-
 /*
- *
- * (C) Copyright IBM Corp. 1998-2004 - All Rights Reserved
- *
+ * (C) Copyright IBM Corp. and others 1998-2013 - All Rights Reserved
  */
 
 #include "LETypes.h"
 #include "LEScripts.h"
 #include "LELanguages.h"
+#include "LESwaps.h"
 
 #include "LayoutEngine.h"
 #include "ArabicLayoutEngine.h"
 #include "CanonShaping.h"
 #include "HanLayoutEngine.h"
+#include "HangulLayoutEngine.h"
 #include "IndicLayoutEngine.h"
+#include "KhmerLayoutEngine.h"
 #include "ThaiLayoutEngine.h"
+#include "TibetanLayoutEngine.h"
 #include "GXLayoutEngine.h"
+#include "GXLayoutEngine2.h"
+
 #include "ScriptAndLanguageTags.h"
 #include "CharSubstitutionFilter.h"
 
 
 #include "OpenTypeUtilities.h"
 #include "GlyphSubstitutionTables.h"
+#include "GlyphDefinitionTables.h"
 #include "MorphTables.h"
 
 #include "DefaultCharMapper.h"
 
+#include "KernTable.h"
+
 U_NAMESPACE_BEGIN
 
-#define ARRAY_SIZE(array) (sizeof array  / sizeof array[0])
+/* Leave this copyright notice here! It needs to go somewhere in this library. */
+static const char copyright[] = U_COPYRIGHT_STRING;
+
+/* TODO: remove these? */
+const le_int32 LayoutEngine::kTypoFlagKern = LE_Kerning_FEATURE_FLAG;
+const le_int32 LayoutEngine::kTypoFlagLiga = LE_Ligatures_FEATURE_FLAG;
 
 const LEUnicode32 DefaultCharMapper::controlChars[] = {
     0x0009, 0x000A, 0x000D,
@@ -38,29 +49,7 @@ const LEUnicode32 DefaultCharMapper::controlChars[] = {
     0x206A, 0x206B, 0x206C, 0x206D, 0x206E, 0x206F
 };
 
-const le_int32 DefaultCharMapper::controlCharsCount = ARRAY_SIZE(controlChars);
-
-const LEUnicode32 DefaultCharMapper::mirroredChars[] = {
-    0x0028, 0x0029, // ascii paired punctuation
-    0x003c, 0x003e,
-    0x005b, 0x005d,
-    0x007b, 0x007d,
-    0x2045, 0x2046, // math symbols (not complete)
-    0x207d, 0x207e,
-    0x208d, 0x208e,
-    0x2264, 0x2265,
-    0x3008, 0x3009, // chinese paired punctuation
-    0x300a, 0x300b,
-    0x300c, 0x300d,
-    0x300e, 0x300f,
-    0x3010, 0x3011,
-    0x3014, 0x3015,
-    0x3016, 0x3017,
-    0x3018, 0x3019,
-    0x301a, 0x301b
-};
-
-const le_int32 DefaultCharMapper::mirroredCharsCount = ARRAY_SIZE(mirroredChars);
+const le_int32 DefaultCharMapper::controlCharsCount = LE_ARRAY_SIZE(controlChars);
 
 LEUnicode32 DefaultCharMapper::mapChar(LEUnicode32 ch) const
 {
@@ -73,12 +62,10 @@ LEUnicode32 DefaultCharMapper::mapChar(LEUnicode32 ch) const
     }
 
     if (fMirror) {
-        le_int32 index = OpenTypeUtilities::search((le_uint32) ch, (le_uint32 *)mirroredChars, mirroredCharsCount);
+        le_int32 index = OpenTypeUtilities::search((le_uint32) ch, (le_uint32 *)DefaultCharMapper::mirroredChars, DefaultCharMapper::mirroredCharsCount);
 
         if (mirroredChars[index] == ch) {
-            le_int32 mirrorOffset = ((index & 1) == 0) ? 1 : -1;
-
-            return mirroredChars[index + mirrorOffset];
+            return DefaultCharMapper::srahCderorrim[index];
         }
     }
 
@@ -104,25 +91,75 @@ CharSubstitutionFilter::~CharSubstitutionFilter()
     // nothing to do
 }
 
+class CanonMarkFilter : public UMemory, public LEGlyphFilter
+{
+private:
+    const GlyphClassDefinitionTable *classDefTable;
+
+    CanonMarkFilter(const CanonMarkFilter &other); // forbid copying of this class
+    CanonMarkFilter &operator=(const CanonMarkFilter &other); // forbid copying of this class
+
+public:
+    CanonMarkFilter(const GlyphDefinitionTableHeader *gdefTable);
+    virtual ~CanonMarkFilter();
+
+    virtual le_bool accept(LEGlyphID glyph) const;
+};
+
+CanonMarkFilter::CanonMarkFilter(const GlyphDefinitionTableHeader *gdefTable)
+{
+    classDefTable = gdefTable->getMarkAttachClassDefinitionTable();
+}
+
+CanonMarkFilter::~CanonMarkFilter()
+{
+    // nothing to do?
+}
+
+le_bool CanonMarkFilter::accept(LEGlyphID glyph) const
+{
+    le_int32 glyphClass = classDefTable->getGlyphClass(glyph);
+
+    return glyphClass != 0;
+}
 
 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LayoutEngine)
 
-static const LETag emptyTag = 0x00000000;
+#define ccmpFeatureTag  LE_CCMP_FEATURE_TAG
+
+#define ccmpFeatureMask 0x80000000UL
 
-static const LETag ccmpFeatureTag = LE_CCMP_FEATURE_TAG;
+#define canonFeatures (ccmpFeatureMask)
 
-static const LETag canonFeatures[] = {ccmpFeatureTag, emptyTag};
+static const FeatureMap canonFeatureMap[] =
+{
+    {ccmpFeatureTag, ccmpFeatureMask}
+};
+
+static const le_int32 canonFeatureMapCount = LE_ARRAY_SIZE(canonFeatureMap);
 
-LayoutEngine::LayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode)
-    : fGlyphStorage(NULL), fFontInstance(fontInstance), fScriptCode(scriptCode), fLanguageCode(languageCode)
+LayoutEngine::LayoutEngine(const LEFontInstance *fontInstance,
+                           le_int32 scriptCode,
+                           le_int32 languageCode,
+                           le_int32 typoFlags,
+                           LEErrorCode &success)
+  : fGlyphStorage(NULL), fFontInstance(fontInstance), fScriptCode(scriptCode), fLanguageCode(languageCode),
+    fTypoFlags(typoFlags), fFilterZeroWidth(TRUE)
 {
+    if (LE_FAILURE(success)) {
+        return;
+    }
+
     fGlyphStorage = new LEGlyphStorage();
+    if (fGlyphStorage == NULL) {
+        success = LE_MEMORY_ALLOCATION_ERROR;
+    }
 }
 
 le_int32 LayoutEngine::getGlyphCount() const
 {
     return fGlyphStorage->getGlyphCount();
-};
+}
 
 void LayoutEngine::getCharIndices(le_int32 charIndices[], le_int32 indexBase, LEErrorCode &success) const
 {
@@ -175,11 +212,42 @@ le_int32 LayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 off
 
     if (canonGSUBTable->coversScript(scriptTag)) {
         CharSubstitutionFilter *substitutionFilter = new CharSubstitutionFilter(fFontInstance);
+        if (substitutionFilter == NULL) {
+            success = LE_MEMORY_ALLOCATION_ERROR;
+            return 0;
+        }
+
+               const LEUnicode *inChars = &chars[offset];
+               LEUnicode *reordered = NULL;
+        LEGlyphStorage fakeGlyphStorage;
+
+        fakeGlyphStorage.allocateGlyphArray(count, rightToLeft, success);
+
+        if (LE_FAILURE(success)) {
+            delete substitutionFilter;
+            return 0;
+        }
+
+               // This is the cheapest way to get mark reordering only for Hebrew.
+               // We could just do the mark reordering for all scripts, but most
+               // of them probably don't need it...
+               if (fScriptCode == hebrScriptCode) {
+                       reordered = LE_NEW_ARRAY(LEUnicode, count);
+
+                       if (reordered == NULL) {
+                delete substitutionFilter;
+                               success = LE_MEMORY_ALLOCATION_ERROR;
+                               return 0;
+                       }
+
+                       CanonShaping::reorderMarks(&chars[offset], count, rightToLeft, reordered, fakeGlyphStorage);
+                       inChars = reordered;
+        }
 
-        glyphStorage.allocateGlyphArray(count, rightToLeft, success);
-        glyphStorage.allocateAuxData(success);
+        fakeGlyphStorage.allocateAuxData(success);
 
         if (LE_FAILURE(success)) {
+            delete substitutionFilter;
             return 0;
         }
 
@@ -189,17 +257,41 @@ le_int32 LayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 off
         }
 
         for (i = 0; i < count; i += 1, out += dir) {
-            glyphStorage[out] = (LEGlyphID) chars[offset + i];
-            glyphStorage.setAuxData(out, (void *) canonFeatures, success);
+            fakeGlyphStorage[out] = (LEGlyphID) inChars[i];
+            fakeGlyphStorage.setAuxData(out, canonFeatures, success);
         }
 
-        outCharCount = canonGSUBTable->process(glyphStorage, rightToLeft, scriptTag, langSysTag, NULL, substitutionFilter, NULL);
+               if (reordered != NULL) {
+                       LE_DELETE_ARRAY(reordered);
+               }
 
-        out = (rightToLeft? count - 1 : 0);
+        outCharCount = canonGSUBTable->process(fakeGlyphStorage, rightToLeft, scriptTag, langSysTag, NULL, substitutionFilter, canonFeatureMap, canonFeatureMapCount, FALSE, success);
+
+        if (LE_FAILURE(success)) {
+            delete substitutionFilter;
+            return 0;
+        }
+
+        out = (rightToLeft? outCharCount - 1 : 0);
+
+        /*
+         * The char indices array in fakeGlyphStorage has the correct mapping
+         * back to the original input characters. Save it in glyphStorage. The
+         * subsequent call to glyphStoratge.allocateGlyphArray will keep this
+         * array rather than allocating and initializing a new one.
+         */
+        glyphStorage.adoptCharIndicesArray(fakeGlyphStorage);
 
         outChars = LE_NEW_ARRAY(LEUnicode, outCharCount);
+
+        if (outChars == NULL) {
+            delete substitutionFilter;
+            success = LE_MEMORY_ALLOCATION_ERROR;
+            return 0;
+        }
+
         for (i = 0; i < outCharCount; i += 1, out += dir) {
-            outChars[out] = (LEUnicode) LE_GET_GLYPH(glyphStorage[i]);
+            outChars[out] = (LEUnicode) LE_GET_GLYPH(fakeGlyphStorage[i]);
         }
 
         delete substitutionFilter;
@@ -262,8 +354,8 @@ void LayoutEngine::positionGlyphs(LEGlyphStorage &glyphStorage, float x, float y
     glyphStorage.setPosition(glyphCount, x, y, success);
 }
 
-void LayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool /*reverse*/,
-                                        LEGlyphStorage &/*glyphStorage*/, LEErrorCode &success)
+void LayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool  reverse,
+                                        LEGlyphStorage &glyphStorage, LEErrorCode &success)
 {
     if (LE_FAILURE(success)) {
         return;
@@ -274,6 +366,18 @@ void LayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset
         return;
     }
 
+    GlyphDefinitionTableHeader *gdefTable = (GlyphDefinitionTableHeader *) CanonShaping::glyphDefinitionTable;
+    CanonMarkFilter filter(gdefTable);
+
+    adjustMarkGlyphs(&chars[offset], count, reverse, glyphStorage, &filter, success);
+
+    if (fTypoFlags & 0x1) { /* kerning enabled */
+      static const le_uint32 kernTableTag = LE_KERN_TABLE_TAG;
+
+      KernTable kt(fFontInstance, getFontTable(kernTableTag));
+      kt.process(glyphStorage);
+    }
+
     // default is no adjustments
     return;
 }
@@ -298,7 +402,7 @@ void LayoutEngine::adjustMarkGlyphs(LEGlyphStorage &glyphStorage, LEGlyphFilter
 
     for (p = 0; p < glyphCount; p += 1) {
         float next, xAdvance;
-        
+
         glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
 
         xAdvance = next - prev;
@@ -340,7 +444,7 @@ void LayoutEngine::adjustMarkGlyphs(const LEUnicode chars[], le_int32 charCount,
 
     for (p = 0; p < charCount; p += 1, c += direction) {
         float next, xAdvance;
-        
+
         glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
 
         xAdvance = next - prev;
@@ -372,7 +476,7 @@ void LayoutEngine::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le
 
     DefaultCharMapper charMapper(TRUE, mirror);
 
-    fFontInstance->mapCharsToGlyphs(chars, offset, count, reverse, &charMapper, glyphStorage);
+    fFontInstance->mapCharsToGlyphs(chars, offset, count, reverse, &charMapper, fFilterZeroWidth, glyphStorage);
 }
 
 // Input: characters, font?
@@ -391,7 +495,11 @@ le_int32 LayoutEngine::layoutChars(const LEUnicode chars[], le_int32 offset, le_
     }
 
     le_int32 glyphCount;
-    
+
+    if (fGlyphStorage->getGlyphCount() > 0) {
+        fGlyphStorage->reset();
+    }
+
     glyphCount = computeGlyphs(chars, offset, count, max, rightToLeft, *fGlyphStorage, success);
     positionGlyphs(*fGlyphStorage, x, y, success);
     adjustGlyphPositions(chars, offset, count, rightToLeft, *fGlyphStorage, success);
@@ -403,11 +511,18 @@ void LayoutEngine::reset()
 {
     fGlyphStorage->reset();
 }
-    
+
 LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, LEErrorCode &success)
+{
+  // 3 -> kerning and ligatures
+  return LayoutEngine::layoutEngineFactory(fontInstance, scriptCode, languageCode, 3, success);
+}
+
+LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags, LEErrorCode &success)
 {
     static const le_uint32 gsubTableTag = LE_GSUB_TABLE_TAG;
     static const le_uint32 mortTableTag = LE_MORT_TABLE_TAG;
+    static const le_uint32 morxTableTag = LE_MORX_TABLE_TAG;
 
     if (LE_FAILURE(success)) {
         return NULL;
@@ -417,8 +532,15 @@ LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstan
     LayoutEngine *result = NULL;
     LETag scriptTag   = 0x00000000;
     LETag languageTag = 0x00000000;
+       LETag v2ScriptTag = OpenTypeLayoutEngine::getV2ScriptTag(scriptCode);
+
+    // Right now, only invoke V2 processing for Devanagari.  TODO: Allow more V2 scripts as they are
+    // properly tested.
 
-    if (gsubTable != NULL && gsubTable->coversScript(scriptTag = OpenTypeLayoutEngine::getScriptTag(scriptCode))) {
+       if ( v2ScriptTag == dev2ScriptTag && gsubTable != NULL && gsubTable->coversScript( v2ScriptTag )) {
+               result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, TRUE, gsubTable, success);
+       }
+    else if (gsubTable != NULL && gsubTable->coversScript(scriptTag = OpenTypeLayoutEngine::getScriptTag(scriptCode))) {
         switch (scriptCode) {
         case bengScriptCode:
         case devaScriptCode:
@@ -429,11 +551,21 @@ LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstan
         case guruScriptCode:
         case tamlScriptCode:
         case teluScriptCode:
-            result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable);
+        case sinhScriptCode:
+            result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, FALSE, gsubTable, success);
             break;
 
         case arabScriptCode:
-            result = new ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable);
+            result = new ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
+            break;
+
+        case hebrScriptCode:
+            // Disable hebrew ligatures since they have only archaic uses, see ticket #8318
+            result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags & ~kTypoFlagLiga, gsubTable, success);
+            break;
+
+        case hangScriptCode:
+            result = new HangulOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
             break;
 
         case haniScriptCode:
@@ -445,62 +577,84 @@ LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstan
             case zhtLanguageCode:
             case zhsLanguageCode:
                 if (gsubTable->coversScriptAndLanguage(scriptTag, languageTag, TRUE)) {
-                    result = new HanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable);
+                    result = new HanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
                     break;
                 }
 
                 // note: falling through to default case.
             default:
-                result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable);
+                result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
                 break;
             }
 
             break;
 
+        case tibtScriptCode:
+            result = new TibetanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
+            break;
+
+        case khmrScriptCode:
+            result = new KhmerOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
+            break;
+
         default:
-            result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, gsubTable);
+            result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable, success);
             break;
         }
     } else {
-        const MorphTableHeader *morphTable = (MorphTableHeader *) fontInstance->getFontTable(mortTableTag);
-
-        if (morphTable != NULL) {
-            result = new GXLayoutEngine(fontInstance, scriptCode, languageCode, morphTable);
+        MorphTableHeader2 *morxTable = (MorphTableHeader2 *)fontInstance->getFontTable(morxTableTag);
+        if (morxTable != NULL) {
+            result = new GXLayoutEngine2(fontInstance, scriptCode, languageCode, morxTable, typoFlags, success);
         } else {
-            switch (scriptCode) {
-            case bengScriptCode:
-            case devaScriptCode:
-            case gujrScriptCode:
-            case kndaScriptCode:
-            case mlymScriptCode:
-            case oryaScriptCode:
-            case guruScriptCode:
-            case tamlScriptCode:
-            case teluScriptCode:
-            {
-                result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode);
-                break;
-            }
-
-            case arabScriptCode:
-            //case hebrScriptCode:
-                result = new UnicodeArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode);
-                break;
-
-            //case hebrScriptCode:
-            //    return new HebrewOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode);
-
-            case thaiScriptCode:
-                result = new ThaiLayoutEngine(fontInstance, scriptCode, languageCode);
-                break;
-
-            default:
-                result = new LayoutEngine(fontInstance, scriptCode, languageCode);
-                break;
+            const MorphTableHeader *mortTable = (MorphTableHeader *) fontInstance->getFontTable(mortTableTag);
+            if (mortTable != NULL) { // mort
+                result = new GXLayoutEngine(fontInstance, scriptCode, languageCode, mortTable, success);
+            } else {
+                switch (scriptCode) {
+                    case bengScriptCode:
+                    case devaScriptCode:
+                    case gujrScriptCode:
+                    case kndaScriptCode:
+                    case mlymScriptCode:
+                    case oryaScriptCode:
+                    case guruScriptCode:
+                    case tamlScriptCode:
+                    case teluScriptCode:
+                    case sinhScriptCode:
+                    {
+                        result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success);
+                        break;
+                    }
+
+                    case arabScriptCode:
+                        //case hebrScriptCode:
+                        result = new UnicodeArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success);
+                        break;
+
+                        //case hebrScriptCode:
+                        //    return new HebrewOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
+
+                    case thaiScriptCode:
+                        result = new ThaiLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success);
+                        break;
+
+                    case hangScriptCode:
+                        result = new HangulOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success);
+                        break;
+
+                    default:
+                        result = new LayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, success);
+                        break;
+                }
             }
         }
     }
 
+    if (result && LE_FAILURE(success)) {
+               delete result;
+               result = NULL;
+       }
+
     if (result == NULL) {
         success = LE_MEMORY_ALLOCATION_ERROR;
     }
@@ -509,7 +663,7 @@ LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstan
 }
 
 LayoutEngine::~LayoutEngine() {
-    reset();
+    delete fGlyphStorage;
 }
 
 U_NAMESPACE_END