Changeset 704

Show
Ignore:
Timestamp:
03/01/10 21:33:49 (5 months ago)
Author:
tal
Message:
Added support for GPOS Lookup Type 9.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • packages/extractor/trunk/Lib/extractor/formats/opentype.py

    r703 r704  
    433433        (lookup index, subtable index, class index) 
    434434    """ 
    435     kerning = {} 
     435    allKerning = {} 
    436436    allLeftClasses = {} 
    437437    allRightClasses = {} 
    438438    lookup = gpos.LookupList.Lookup[lookupIndex] 
    439     # only handle pair positioning 
    440     if lookup.LookupType != 2
     439    # only handle pair positioning and extension 
     440    if lookup.LookupType not in (2, 9)
    441441        return 
    442442    for subtableIndex, subtable in enumerate(lookup.SubTable): 
    443         format = subtable.Format 
    444         if format == 1: 
    445             coverage = subtable.Coverage.glyphs 
     443        if lookup.LookupType == 2: 
     444            format = subtable.Format 
     445            if format == 1: 
     446                kerning = _handleLookupType2Format1(subtable) 
     447                allKerning.update(kerning) 
     448            elif format == 2: 
     449                kerning, leftClasses, rightClasses = _handleLookupType2Format2(subtable, lookupIndex, subtableIndex) 
     450                allKerning.update(kerning) 
     451                allLeftClasses.update(leftClasses) 
     452                allRightClasses.update(rightClasses) 
     453        elif lookup.LookupType == 9: 
     454            extSubtable = subtable.ExtSubTable 
     455            if extSubtable.Format == 1: 
     456                kerning = _handleLookupType2Format1(extSubtable) 
     457                allKerning.update(kerning) 
     458            elif extSubtable.Format == 2: 
     459                kerning, leftClasses, rightClasses = _handleLookupType2Format2(extSubtable, lookupIndex, subtableIndex) 
     460                allKerning.update(kerning) 
     461                allLeftClasses.update(leftClasses) 
     462                allRightClasses.update(rightClasses) 
     463    # done 
     464    return allKerning, allLeftClasses, allRightClasses 
     465 
     466def _handleLookupType2Format1(subtable): 
     467    kerning = {} 
     468    coverage = subtable.Coverage.glyphs 
     469    valueFormat1 = subtable.ValueFormat1 
     470    pairSets = subtable.PairSet 
     471    for index, leftGlyphName in enumerate(coverage): 
     472        pairSet = pairSets[index] 
     473        for pairValueRecord in pairSet.PairValueRecord: 
     474            rightGlyphName = pairValueRecord.SecondGlyph 
     475            if valueFormat1: 
     476                value = pairValueRecord.Value1 
     477            else: 
     478                value = pairValueRecord.Value2 
     479            if hasattr(value, "XAdvance"): 
     480                value = value.XAdvance 
     481                kerning[leftGlyphName, rightGlyphName] = value 
     482    return kerning 
     483 
     484def _handleLookupType2Format2(subtable, lookupIndex, subtableIndex): 
     485    # extract the classes 
     486    leftClasses = _extractFeatureClasses(lookupIndex=lookupIndex, subtableIndex=subtableIndex, classDefs=subtable.ClassDef1.classDefs, coverage=subtable.Coverage.glyphs) 
     487    rightClasses = _extractFeatureClasses(lookupIndex=lookupIndex, subtableIndex=subtableIndex, classDefs=subtable.ClassDef2.classDefs) 
     488    # extract the pairs 
     489    kerning = {} 
     490    for class1RecordIndex, class1Record in enumerate(subtable.Class1Record): 
     491        for class2RecordIndex, class2Record in enumerate(class1Record.Class2Record): 
     492            leftClass = (lookupIndex, subtableIndex, class1RecordIndex) 
     493            rightClass = (lookupIndex, subtableIndex, class2RecordIndex) 
    446494            valueFormat1 = subtable.ValueFormat1 
    447             pairSets = subtable.PairSet 
    448             for index, leftGlyphName in enumerate(coverage): 
    449                 pairSet = pairSets[index] 
    450                 for pairValueRecord in pairSet.PairValueRecord: 
    451                     rightGlyphName = pairValueRecord.SecondGlyph 
    452                     if valueFormat1: 
    453                         value = pairValueRecord.Value1 
    454                     else: 
    455                         value = pairValueRecord.Value2 
    456                     if hasattr(value, "XAdvance"): 
    457                         value = value.XAdvance 
    458                         kerning[leftGlyphName, rightGlyphName] = value 
    459         elif format == 2: 
    460             # extract the classes 
    461             leftClasses = _extractFeatureClasses(lookupIndex=lookupIndex, subtableIndex=subtableIndex, classDefs=subtable.ClassDef1.classDefs, coverage=subtable.Coverage.glyphs) 
    462             allLeftClasses.update(leftClasses) 
    463             rightClasses = _extractFeatureClasses(lookupIndex=lookupIndex, subtableIndex=subtableIndex, classDefs=subtable.ClassDef2.classDefs) 
    464             allRightClasses.update(rightClasses) 
    465             # extract the pairs 
    466             for class1RecordIndex, class1Record in enumerate(subtable.Class1Record): 
    467                 for class2RecordIndex, class2Record in enumerate(class1Record.Class2Record): 
    468                     leftClass = (lookupIndex, subtableIndex, class1RecordIndex) 
    469                     rightClass = (lookupIndex, subtableIndex, class2RecordIndex) 
    470                     valueFormat1 = subtable.ValueFormat1 
    471                     if valueFormat1: 
    472                         value = class2Record.Value1 
    473                     else: 
    474                         value = class2Record.Value2 
    475                     if hasattr(value, "XAdvance") and value.XAdvance != 0: 
    476                         value = value.XAdvance 
    477                         kerning[leftClass, rightClass] = value 
    478     # done 
    479     return kerning, allLeftClasses, allRightClasses 
     495            if valueFormat1: 
     496                value = class2Record.Value1 
     497            else: 
     498                value = class2Record.Value2 
     499            if hasattr(value, "XAdvance") and value.XAdvance != 0: 
     500                value = value.XAdvance 
     501                kerning[leftClass, rightClass] = value 
     502    return kerning, leftClasses, rightClasses 
    480503 
    481504def _mergeKerningDictionaries(kerningDictionaries):