Changeset 1029


Ignore:
Timestamp:
12/01/11 16:41:43 (18 months ago)
Author:
tal
Message:
Added decomposeComponent and decomposeAllComponents methods. This resolves ticket #12.
Location:
packages/defcon/branches/ufo3/Lib/defcon
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • packages/defcon/branches/ufo3/Lib/defcon/objects/glyph.py

    r1026 r1029  
    55from defcon.objects.point import Point 
    66from defcon.objects.component import Component 
     7from defcon.objects.component import _defaultTransformation as _defaultComponentTransformation 
    78from defcon.objects.anchor import Anchor 
    89from defcon.objects.lib import Lib 
     
    598599        self.releaseHeldNotifications() 
    599600 
     601    def decomposeComponent(self, component): 
     602        """ 
     603        Decompose **component**. This will preserve the identifiers 
     604        in the incoming contours and points unless there is a conflict. 
     605        In that case, the conflicting incoming identifier will be discarded. 
     606 
     607        This posts *Glyph.ComponentsChanged*, *Glyph.ContoursChanged* 
     608        and *Glyph.Changed* notifications. 
     609        """ 
     610        self.holdNotifications() 
     611        layer = self.layer 
     612        pen = self.getPointPen() 
     613        pointPen = self.getPointPen() 
     614        self._decomposeComponent(component, layer, pointPen) 
     615        self.releaseHeldNotifications() 
     616        self.postNotification(notification="Glyph.ContoursChanged") 
     617 
     618    def decomposeAllComponents(self): 
     619        """ 
     620        Decompose all components in this glyph. This will preserve the 
     621        identifiers in the incoming contours and points unless there is a 
     622        conflict. In that case, the conflicting incoming identifier will 
     623        be discarded. 
     624 
     625        This posts *Glyph.ComponentsChanged*, *Glyph.ContoursChanged* 
     626        and *Glyph.Changed* notifications. 
     627        """ 
     628        if not self.components: 
     629            return 
     630        self.holdNotifications() 
     631        layer = self.layer 
     632        pointPen = self.getPointPen() 
     633        for component in self.components: 
     634            self._decomposeComponent(component, layer, pointPen) 
     635        self.releaseHeldNotifications() 
     636        self.postNotification(notification="Glyph.ContoursChanged") 
     637 
     638    def _decomposeComponent(self, component, layer, pointPen): 
     639        from robofab.pens.adapterPens import TransformPointPen 
     640        pointPen.skipConflictingIdentifiers = True 
     641        baseGlyph = component.baseGlyph 
     642        if baseGlyph in layer: 
     643            baseGlyph = layer[baseGlyph] 
     644            if component.transformation == _defaultComponentTransformation: 
     645                baseGlyph.drawPoints(pointPen) 
     646            else: 
     647                transformPointPen = TransformPointPen(pointPen, component.transformation) 
     648                baseGlyph.drawPoints(transformPointPen) 
     649        self.removeComponent(component) 
     650 
    600651    # ------- 
    601652    # Anchors 
     
    16151666    """ 
    16161667 
     1668def _testDecomposeComponents(): 
     1669    """ 
     1670    >>> from defcon import Font 
     1671    >>> font = Font() 
     1672 
     1673    >>> font.newGlyph("baseGlyph") 
     1674    >>> baseGlyph = font["baseGlyph"] 
     1675    >>> pointPen = baseGlyph.getPointPen() 
     1676    >>> pointPen.beginPath(identifier="contour1") 
     1677    >>> pointPen.addPoint((0, 0), "move", identifier="point1") 
     1678    >>> pointPen.addPoint((0, 100), "line") 
     1679    >>> pointPen.addPoint((100, 100), "line") 
     1680    >>> pointPen.addPoint((100, 0), "line") 
     1681    >>> pointPen.addPoint((0, 0), "line") 
     1682    >>> pointPen.endPath() 
     1683 
     1684    >>> font.newGlyph("referenceGlyph") 
     1685    >>> referenceGlyph = font["referenceGlyph"] 
     1686    >>> pointPen = referenceGlyph.getPointPen() 
     1687    >>> pointPen.addComponent("baseGlyph", (1, 0, 0, 1, 0, 0)) 
     1688    >>> len(referenceGlyph.components) 
     1689    1 
     1690    >>> len(referenceGlyph) 
     1691    0 
     1692    >>> referenceGlyph.decomposeAllComponents() 
     1693    >>> len(referenceGlyph.components) 
     1694    0 
     1695    >>> len(referenceGlyph) 
     1696    1 
     1697    >>> referenceGlyph[0].identifier 
     1698    'contour1' 
     1699    >>> referenceGlyph[0][0].identifier 
     1700    'point1' 
     1701 
     1702    >>> pointPen.addComponent("baseGlyph", (1, 0, 0, 1, 100, 100)) 
     1703    >>> len(referenceGlyph.components) 
     1704    1 
     1705    >>> len(referenceGlyph) 
     1706    1 
     1707    >>> component = referenceGlyph.components[0] 
     1708    >>> referenceGlyph.decomposeComponent(component) 
     1709    >>> len(referenceGlyph.components) 
     1710    0 
     1711    >>> len(referenceGlyph) 
     1712    2 
     1713    >>> referenceGlyph[0].identifier 
     1714    'contour1' 
     1715    >>> referenceGlyph[0][0].identifier 
     1716    'point1' 
     1717    >>> referenceGlyph[1].identifier 
     1718    >>> referenceGlyph[1][0].identifier 
     1719    """ 
     1720 
    16171721def _testMove(): 
    16181722    """ 
  • packages/defcon/branches/ufo3/Lib/defcon/pens/glyphObjectPointPen.py

    r1022 r1029  
    66        self._glyph = glyph 
    77        self._contour = None 
     8        self.skipConflictingIdentifiers = False 
    89 
    910    def beginPath(self, identifier=None, **kwargs): 
    1011        self._contour = self._glyph.instantiateContour() 
    1112        self._contour.disableNotifications() 
    12         self._contour.identifier = identifier 
     13        if identifier is not None: 
     14            if self.skipConflictingIdentifiers and identifier in self._glyph.identifiers: 
     15                pass 
     16            else: 
     17                self._contour.identifier = identifier 
    1318 
    1419    def endPath(self): 
     
    1924 
    2025    def addPoint(self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs): 
     26        if self.skipConflictingIdentifiers and identifier in self._glyph.identifiers: 
     27            identifier = None 
    2128        self._contour.addPoint(pt, segmentType, smooth, name, identifier=identifier) 
    2229 
    2330    def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs): 
     31        if self.skipConflictingIdentifiers and identifier in self._glyph.identifiers: 
     32            identifier = None 
    2433        component = self._glyph.componentClass() 
    2534        component.baseGlyph = baseGlyphName 
Note: See TracChangeset for help on using the changeset viewer.