SOLVED unexpected behaviour when copying glyph in Python



  • Hello,
    I am no sure if I am doing something wrong but this behaviour is highly surprising. When I make a in-memory copy of a glyph that has a component, I can't work with the glyph as it is in the font. For example get bounds or area of the glyph. See example below. in My case i is made of two components, the same problem is when doing the same with f.e. questionmark = one contour, one component. None problem there when I chose glyph that is made completely out of contours.

    f = CurrentFont()
    gName = 'i'
    copied = f[gName].copy()
    print(f[gName].bounds)
    print(copied.bounds)
    

    gives error

    Traceback (most recent call last):
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/component.py", line 183, in drawPoints
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontTools/pens/pointPen.py", line 222, in addComponent
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontTools/pens/basePen.py", line 176, in addComponent
    TypeError: 'NoneType' object is not subscriptable
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<untitled>", line 6, in <module>
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontParts/base/base.py", line 90, in __get__
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontParts/fontshell/glyph.py", line 99, in _get_bounds
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/glyph.py", line 266, in _get_bounds
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/glyph.py", line 257, in _getContourComponentBounds
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/component.py", line 124, in _get_bounds
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/base.py", line 305, in getRepresentation
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/tools/representations.py", line 92, in componentBoundsRepresentationFactory
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/component.py", line 176, in draw
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/defcon/objects/component.py", line 185, in drawPoints
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontTools/pens/pointPen.py", line 222, in addComponent
      File "/Applications/RoboFont.app/Contents/Resources/lib/python3.7/fontTools/pens/basePen.py", line 176, in addComponent
    TypeError: 'NoneType' object is not subscriptable
    


  • @gferreira I see, thanks!!!



  • ps. if you need to make a ‘flat’ copy of a glyph, have a look at the DecomposePointPen.



  • hello @jansindl3r,

    this behavior makes sense, glyph.copy() returns an ‘orphan’ glyph which is detached from the current font:

    print(copied.font)
    print(copied.components)
    
    None
    (<RComponent baseGlyph='dotlessi' offset='(0.0, 0.0)' in glyph 'i' at 4783639504>,)
    

    as you can see the components are still there, but their base glyphs are not available because there is no parent font.