Changeset 217

Show
Ignore:
Timestamp:
01/11/10 14:55:42 (2 years ago)
Author:
erik
Message:

This compileGlyph isn't limited to specific anchor names.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Lib/robofab/objects/objectsBase.py

    r171 r217  
    755755                adjustWidth=False, preflight=False, printErrors=True): 
    756756                """Compile components into a new glyph using components and anchorpoints. 
    757                 font: the font 
    758757                glyphName: the name of the glyph where it all needs to go 
    759758                baseName: the name of the base glyph 
    760759                accentNames: a list of accentName, anchorName tuples, [('acute', 'top'), etc] 
    761760                """ 
    762                 baseAnchors = ['left', 'right', 'top', 'bottom'] 
    763                 accentAnchors = ['_left', '_right', '_top', '_bottom'] 
    764761                anchors = {} 
    765762                errors = {} 
    766                 #grab the baseGlyph 
    767763                baseGlyph = self[baseName] 
    768                 #store the anchors in the baseGlyph 
    769764                for anchor in baseGlyph.getAnchors(): 
    770                         if anchor.name in baseAnchors: 
    771                                 anchors[anchor.name] = anchor.position 
    772                 #make a destination glyph if it doesn't exist already 
     765                        anchors[anchor.name] = anchor.position 
    773766                destGlyph = self.newGlyph(glyphName, clear=True) 
    774                 if not preflight: 
    775                         #add the baseGlyph as a component to the destGlyph and set the width 
    776                         destGlyph.appendComponent(baseName) 
    777                         destGlyph.width = baseGlyph.width 
    778                 #go through the list of provided accentNames 
    779                 for accentName, accentPosition in accentNames: 
    780                         #grab the accent if it exists 
     767                destGlyph.appendComponent(baseName) 
     768                destGlyph.width = baseGlyph.width 
     769                for accentName, anchorName in accentNames: 
    781770                        try: 
    782771                                accent = self[accentName] 
    783772                        except IndexError: 
    784                                 errors["glyph '%s' is missing in font %s"%(accentName, self.postscriptFullName)] =  1 
     773                                errors["glyph '%s' is missing in font %s"%(accentName, self.info.fullName)] =  1 
    785774                                continue 
    786                         localAnchors = {} 
    787                         foundAnchor = None 
    788                         accX = accent.getAnchors() 
    789                         #temporarily store the anchors found in the accent 
    790                         for anchor in accX: 
    791                                 localAnchors[anchor.name] = anchor.position 
    792                         #look through the list of possible accent positions 
    793                         for anchorName in accentAnchors: 
    794                                 #if we have an anchor in the accent that matches something in the list 
    795                                 if anchorName in localAnchors: 
    796                                         #if this anchor name matches an anchor position that the user has requested, we have a winner 
    797                                         if ''.join(['_', accentPosition]) == anchorName: 
    798                                                 foundAnchor = anchorName 
    799                                                 break 
    800                         if foundAnchor: 
    801                                 #grab the coordinates of the found anchor 
    802                                 accentAnchorX, accentAnchorY = localAnchors[foundAnchor] 
    803                                 #get the coordinates for the cooresponding anchor in the baseGlyph 
    804                                 try: 
    805                                         baseX, baseY = anchors[foundAnchor[1:]] 
    806                                 except KeyError: 
    807                                         errors["anchor '%s' not found in glyph '%s' of font %s"%(foundAnchor[1:], baseName, self.info.postscriptFullName)]=1 
    808                                         continue 
    809                                 #calculate the accent componet offset values 
    810                                 xShift = baseX - accentAnchorX 
    811                                 yShift = baseY - accentAnchorY 
    812                                 #add the accent to the destination glyph 
    813                                 if not preflight: 
    814                                         destGlyph.appendComponent(accentName, offset=(xShift, yShift)) 
    815                                 #if the found anchor the anchor that it was just aligned to, make the values for that anchor the new standard 
    816                                 if foundAnchor[1:] in localAnchors: 
    817                                         newX, newY = localAnchors[foundAnchor[1:]] 
    818                                         newX = newX+xShift 
    819                                         newY = newY+yShift 
    820                                         anchors[foundAnchor[1:]] = (newX, newY) 
    821                 #adjust the width if the user has requested it 
    822                 if adjustWidth and not preflight: 
    823                         for accentName, accentPosition in accentNames: 
    824                                 #accent might not be present in the font -- the user has been warned already 
    825                                 try: 
    826                                         accent = self[accentName] 
    827                                 except IndexError: 
    828                                         continue 
    829                                 if accent is None: continue 
    830                                 #not sure what this does... 
    831                                 #if accentPosition == 'right' or accentPosition == '_left': 
    832                                 #       for component in destGlyph.getComponents(): 
    833                                 #               if component.baseName == accentName: 
    834                                  
    835                                 #set the right and left margins only if the accent has been added to the right or left 
    836                                 if accentPosition == 'right': 
    837                                         destGlyph.rightMargin = self[accentName].rightMargin 
    838                                 elif accentPosition == 'left': 
    839                                         destGlyph.leftMargin = self[accentName].leftMargin 
    840                         if preflight: 
    841                                 return errors.keys() 
    842                         if printErrors: 
    843                                 for px in errors.keys(): 
    844                                         print px 
     775                        shift = None 
     776                        for accentAnchor in accent.getAnchors(): 
     777                                if '_'+anchorName == accentAnchor.name: 
     778                                        shift = anchors[anchorName][0] - accentAnchor.position[0], anchors[anchorName][1] - accentAnchor.position[1] 
     779                                        destGlyph.appendComponent(accentName, offset=shift) 
     780                                        break 
     781                        if shift is not None: 
     782                                for accentAnchor in accent.getAnchors(): 
     783                                        if accentAnchor.name in anchors: 
     784                                                anchors[accentAnchor.name] = shift[0]+accentAnchor.position[0], shift[1]+accentAnchor.position[1] 
     785                if printErrors: 
     786                        for px in errors.keys(): 
     787                                print px 
    845788                return destGlyph 
    846789