fontParts
fontParts copied to clipboard
baseGlyph that is missing in another layer when adding a component
Let’s say I want to copy the foreground components to the background layer. Since the Fontparts treats layers like as separate glyph set, if any baseGlyph from the foreground is missing in the background then this error will be raised:
Traceback (most recent call last):
File "copy to layer.py", line 23, in <module>
File "x/fontParts/base/base.py", line 590, in __set__
File "x/fontParts/base/glyph.py", line 354, in _set_base_leftMargin
File "lib/fontObjects/fontPartsWrappers.pyc", line 49, in wrapper
File "x/fontParts/base/glyph.py", line 378, in _set_leftMargin
File "x/fontParts/base/base.py", line 583, in __get__
File "x/fontParts/base/glyph.py", line 348, in _get_base_leftMargin
File "x/fontParts/base/glyph.py", line 364, in _get_leftMargin
File "x/fontParts/base/base.py", line 583, in __get__
File "x/fontParts/base/glyph.py", line 1672, in _get_bounds
File "x/fontParts/base/glyph.py", line 596, in draw
File "x/fontParts/base/component.py", line 260, in draw
File "x/fontParts/base/component.py", line 268, in _draw
File "x/fontParts/base/component.py", line 274, in drawPoints
File "x/fontParts/base/component.py", line 285, in _drawPoints
File "x/ufoLib/pointPen.py", line 196, in addComponent
File "x/fontTools/pens/basePen.py", line 177, in addComponent
File "x/fontParts/base/layer.py", line 80, in __getitem__
FontPartsError: No glyph named 'acute.uc'.
Which basically says the baseGlyph is missing and can't add the component. Not sure if user should avoid it or there should be way to avoid this error from happening in FontParts?
Yes, it should gracefully handle this. Thanks for raising it.
Could you show the code in copy to layer.py
or just the lines around where the traceback is triggered? It looks like something is trying to set the left margin in the new glyph, that triggers a request for the glyph bounds, that triggers a draw call, that triggers a request for the missing glyph. If the left margin isn't requested it should work.
Sorry I changed my code later and don't have a backup. But here is the script I use in Robofont for the same purpose:
import mojo
try:
from mojo.pens import DecomposePointPen
except:
pass
ROBOFONTMAINVER = int(mojo.roboFont.version[0])
f = CurrentFont()
cg = CurrentGlyph()
layers = f.layerOrder
glist = []
mLayer = 'background'
if len(layers) < 1 or mLayer not in layers:
f.layerOrder.append(mLayer)
if ROBOFONTMAINVER != 1:
f.changed()
else:
f.update()
if len(f.templateSelection) < 2 and cg != None:
glist.append(cg)
else:
for gl in f.templateSelection:
g = f[gl]
glist.append(g)
for g in glist:
g.prepareUndo(undoTitle = 'Copy to mask')
if ROBOFONTMAINVER != 1:
layer = g.newLayer(mLayer)
layer.prepareUndo(undoTitle = 'Copy to %s' %layer.name)
layer.clear()
layer.width = g.width
dstPen = layer.getPointPen()
decomposePen = DecomposePointPen(f, dstPen)
g.drawPoints(decomposePen)
layer.changed()
layer.performUndo()
g.changed()
else:
g.copyToLayer(mLayer, clear = True)
g.update()
g.performUndo()