pythonfontsfontforge

How do I generate a stroked font using python-fontforge


I'm using python/fontforge to convert a CAD font to Truetype so that I can integrate it with SVG.

 import fontforge
 font=fontforge.font()
 font.strokedfont=1
 char=font.createChar(65)
 char.stroke("circular",5)
 pen=font[65].glyphPen()
 pen.moveTo(26,102)
 pen.lineTo(26,204)
 pen.lineTo(128,409)
 pen.lineTo(231,204)
 pen.lineTo(231,102)
 pen.endPath()
 pen.moveTo(26,204)
 pen.lineTo(231,204)
 pen.endPath()
 char.width=235
 font.generate('/tmp/testfont.ttf')

This gives me the letter "Capital A", but the font is filled in.

I'm expecting to see a vertical stroked line from (26,102) up to (26,204), rising to the apex at (128,409), falling to (231,204) and dropping vertically to (231,102). Then a horizontal stroke from (26,204) to (231,204).

Instead I have a blacked-out shape.

blacked out shape editor showing the lines defining the blacked out shape

Obviously, pen.endPath() is not leaving the path open as expected and charstroke("circular",5) is not setting the path's thickness.

Can anyone tell me what I'm doing wrong?


Solution

  • You need to run the char.stroke("circular",20) method after drawing the path. (after the second pen.endPath())

    Tested on fontforge V20201107

    import fontforge
    font=fontforge.font()
    font.strokedfont=1
    char=font.createChar(65)
    pen=font[65].glyphPen()
    pen.moveTo(26,102)
    pen.lineTo(26,204)
    pen.lineTo(128,409)
    pen.lineTo(231,204)
    pen.lineTo(231,102)
    pen.endPath()
    pen.moveTo(26,204)
    pen.lineTo(231,204)
    pen.endPath()
    char.width=235
    char.stroke("circular",20)
    font.generate('/tmp/testfont.ttf')