Ignore:
Timestamp:
02/25/08 11:35:12 (5 years ago)
Author:
erik
Message:

This adds support for glyph.psHints in objectsFL.RGlyph. The psHints object is in this case a wrapper for FontLab?'s glyph hint objects. Hints are represented as simple [position, width] pairs, but they're converted to FL Hint objects when writing back to the font. Some changes to the way the hints are written to the font.lib upon export to UFO: hints used to be written as a list of {"position":100, "width":20} dicts. This is now just a list of lists [[100, 20], ]. The names of the entries now match the robofabInterCapSpellingRule, so hHint and vHint rather than hhint and vhint. This also applies to hLink, vLink and replaceTable.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/robofab/Lib/robofab/objects/objectsFL.py

    r42 r51  
    1010                MOVE, LINE, CORNER, CURVE, QCURVE, OFFCURVE,\ 
    1111                relativeBCPIn, relativeBCPOut, absoluteBCPIn, absoluteBCPOut,\ 
    12                 BasePostScriptFontHintValues, postScriptHintDataLibKey 
     12                BasePostScriptFontHintValues, postScriptHintDataLibKey, BasePostScriptGlyphHintValues 
    1313from fontTools.misc import arrayTools 
    1414from robofab.pens.flPen import FLPointPen 
     
    6767""" 
    6868 
    69         FontLab implementation of psHints object 
     69        FontLab implementation of psHints objects 
    7070         
    7171        Most of the FL methods relating to ps hints return a list of 16 items. 
     
    223223        hStems = property(_getHStems, _setHStems, doc="postscript hints: horizontal stem values") 
    224224         
    225                  
    226  
    227 def getPostScriptHintDataFromLib(aFont, fontLib): 
     225         
     226def getPostScriptFontHintDataFromLib(aFont, fontLib): 
    228227        hintData = fontLib.get(postScriptHintDataLibKey) 
    229228        psh = PostScriptFontHintValues(aFont) 
    230229        psh.fromDict(hintData) 
     230 
     231 
     232class PostScriptGlyphHintValues(BasePostScriptGlyphHintValues): 
     233        """     Wrapper for glyph-level PostScript hinting information for FontLab. 
     234                vStems, hStems 
     235        """ 
     236        def __init__(self, glyph=None): 
     237                self._object = glyph.naked() 
     238 
     239        def copy(self): 
     240                from robofab.objects.objectsRF import PostScriptGlyphHintValues as _PostScriptGlyphHintValues 
     241                return _PostScriptGlyphHintValues(data=self.asDict()) 
     242 
     243        def _hintObjectsToList(self, item): 
     244                data = [] 
     245                done = [] 
     246                for hint in item: 
     247                        p = (hint.position, hint.width) 
     248                        if p in done: 
     249                                continue 
     250                        data.append(p) 
     251                        done.append(p) 
     252                data.sort() 
     253                return data 
     254                 
     255        def _listToHintObjects(self, item): 
     256                hints = [] 
     257                done = [] 
     258                for pos, width in item: 
     259                        if (pos, width) in done: 
     260                                # we don't want to set duplicates 
     261                                continue 
     262                        hints.append(Hint(pos, width)) 
     263                        done.append((pos,width)) 
     264                return hints 
     265 
     266        def _getVHints(self): 
     267                return self._hintObjectsToList(self._object.vhints) 
     268 
     269        def _setVHints(self, values): 
     270                # 1 = horizontal hints and links, 
     271                # 2 = vertical hints and links 
     272                # 3 = all hints and links 
     273                self._object.RemoveHints(2) 
     274                values.sort() 
     275                for hint in self._listToHintObjects(values): 
     276                        self._object.vhints.append(hint) 
     277 
     278        def _getHHints(self): 
     279                return self._hintObjectsToList(self._object.hhints) 
     280 
     281        def _setHHints(self, values): 
     282                # 1 = horizontal hints and links, 
     283                # 2 = vertical hints and links 
     284                # 3 = all hints and links 
     285                self._object.RemoveHints(1) 
     286                values.sort() 
     287                for hint in self._listToHintObjects(values): 
     288                        self._object.hhints.append(hint) 
     289 
     290        vHints = property(_getVHints, _setVHints, doc="postscript hints: vertical hint zones") 
     291        hHints = property(_getHHints, _setHHints, doc="postscript hints: horizontal hint zones") 
     292 
    231293 
    232294 
     
    238300        # glyph.hhints and glyph.vhints returns a list of Hint objects. 
    239301        # Hint objects have position and width attributes. 
    240         data['hhints'] = [] 
     302        data['hHints'] = [] 
    241303        for index in xrange(len(glyph.hhints)): 
    242304                hint = glyph.hhints[index] 
    243                 d = {   'position' : hint.position, 
    244                         'width' : hint.width, 
    245                         } 
    246                 data['hhints'].append(d) 
    247         if not data['hhints']: 
    248                 del data['hhints'] 
    249         data['vhints'] = [] 
     305                data['hHints'].append((hint.position, hint.width)) 
     306        if not data['hHints']: 
     307                del data['hHints'] 
     308        data['vHints'] = [] 
    250309        for index in xrange(len(glyph.vhints)): 
    251310                hint = glyph.vhints[index] 
    252                 d = {   'position' : hint.position, 
    253                         'width' : hint.width, 
    254                         } 
    255                 data['vhints'].append(d) 
    256         if not data['vhints']: 
    257                 del data['vhints'] 
     311                data['vHints'].append((hint.position, hint.width)) 
     312        if not data['vHints']: 
     313                del data['vHints'] 
    258314        ## 
    259315        ## horizontal and vertical links 
     
    261317        # glyph.hlinks and glyph.vlinks returns a list of Link objects. 
    262318        # Link objects have node1 and node2 attributes. 
    263         data['hlinks'] = [] 
     319        data['hLinks'] = [] 
    264320        for index in xrange(len(glyph.hlinks)): 
    265321                link = glyph.hlinks[index] 
     
    267323                        'node2' : link.node2, 
    268324                        } 
    269                 data['hlinks'].append(d) 
    270         if not data['hlinks']: 
    271                 del data['hlinks'] 
    272         data['vlinks'] = [] 
     325                data['hLinks'].append(d) 
     326        if not data['hLinks']: 
     327                del data['hLinks'] 
     328        data['vLinks'] = [] 
    273329        for index in xrange(len(glyph.vlinks)): 
    274330                link = glyph.vlinks[index] 
     
    276332                        'node2' : link.node2, 
    277333                        } 
    278                 data['vlinks'].append(d) 
    279         if not data['vlinks']: 
    280                 del data['vlinks'] 
     334                data['vLinks'].append(d) 
     335        if not data['vLinks']: 
     336                del data['vLinks'] 
    281337        ## 
    282338        ## replacement table 
     
    284340        # glyph.replace_table returns a list of Replace objects. 
    285341        # Replace objects have type and index attributes. 
    286         data['replace_table'] = [] 
     342        data['replaceTable'] = [] 
    287343        for index in xrange(len(glyph.replace_table)): 
    288344                replace = glyph.replace_table[index] 
     
    290346                        'index' : replace.index, 
    291347                        } 
    292                 data['replace_table'].append(d) 
    293         if not data['replace_table']: 
    294                 del data['replace_table'] 
     348                data['replaceTable'].append(d) 
     349        if not data['replaceTable']: 
     350                del data['replaceTable'] 
    295351        # XXX 
    296352        # need to support glyph.instructions and glyph.hdmx? 
     
    310366        ## horizontal and vertical hints 
    311367        ## 
    312         if aDict.has_key('hhints'): 
    313                 for d in aDict['hhints']: 
     368        if aDict.has_key('hHints'): 
     369                for d in aDict['hHints']: 
    314370                        glyph.hhints.append(Hint(d['position'], d['width'])) 
    315         if aDict.has_key('vhints'): 
    316                 for d in aDict['vhints']: 
     371        if aDict.has_key('vHints'): 
     372                for d in aDict['vHints']: 
    317373                        glyph.vhints.append(Hint(d['position'], d['width'])) 
    318374        ## 
    319375        ## horizontal and vertical links 
    320376        ## 
    321         if aDict.has_key('hlinks'): 
    322                 for d in aDict['hlinks']: 
     377        if aDict.has_key('hLinks'): 
     378                for d in aDict['hLinks']: 
    323379                        glyph.hlinks.append(Link(d['node1'], d['node2'])) 
    324         if aDict.has_key('vlinks'): 
    325                 for d in aDict['vlinks']: 
     380        if aDict.has_key('vLinks'): 
     381                for d in aDict['vLinks']: 
    326382                        glyph.vlinks.append(Link(d['node1'], d['node2'])) 
    327383        ## 
    328384        ## replacement table 
    329385        ## 
    330         if aDict.has_key('replace_table'): 
    331                 for d in aDict['replace_table']: 
     386        if aDict.has_key('replaceTable'): 
     387                for d in aDict['replaceTable']: 
    332388                        glyph.replace_table.append(Replace(d['type'], d['index'])) 
    333389         
     
    11131169                                count = count + 1 
    11141170                        # import postscript font hint data 
    1115                         getPostScriptHintDataFromLib(self, fontLib) 
     1171                        getPostScriptFontHintDataFromLib(self, fontLib) 
    11161172                        self.kerning.clear() 
    11171173                        self.kerning.update(u.readKerning()) 
     
    12601316        note = property(_get_note, _set_note, doc="note") 
    12611317         
    1262         #def _get_psHints(self): 
    1263         #       # get an object representing the postscript zone information 
    1264         #       # thes 
    1265         #       raise NotImplementedError 
    1266                  
    1267         #psHints = property(_get_psHints, doc="postscript hint data") 
     1318        def _get_psHints(self): 
     1319                # get an object representing the postscript zone information 
     1320                return PostScriptGlyphHintValues(self) 
     1321                 
     1322        psHints = property(_get_psHints, doc="postscript hint data") 
    12681323         
    12691324        # 
Note: See TracChangeset for help on using the changeset viewer.