question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to decompose components of a glyph in a TTF?

See original GitHub issue

I am attempting to decompose components in glyphs within a TTF, but having trouble.

Is there a way to do this?

It seems that there is nothing direct, and that I may need to use pens. I am pretty new to pens, and not sure I understand them completely.

I think I would need to find a way to record points of each component to a pen object, and then draw those points into the ttGlyph with the .drawPoints() method. However, it doesn’t seem that the decomposingRecordingPen works with TTFont objects. I also can’t figure out whether I am using the ttGlyphPen correctly (can it even record points, or is it just a way to store points to later draw?).

def decomposeGlyph(font, glyphName):

    components = (font['glyf'][glyphName].getComponentNames(font['glyf']))

    ## BASIC IDEA (not yet working)
    # for component in glyph
        # record tt points of component base
        # draw points to glyph
    
    glyphset = font.getGlyphSet()

    for comp in components:
        glyph = glyphset[comp]
        pen = ttGlyphPen.TTGlyphPen(glyph)
        # font['glyf'][glyphName].drawPoints(pen, font['glyf'])
        font['glyf'][glyphName].draw(pen, font['glyf'])

It may or may not be a related goal, but I am also hoping to remove overlap in the glyphs in a TTF. If this is easy to point out, I would love to get a head start on it, but if it’s a totally separate thing, feel free to skip this part and I will look into it separately.

Thank you so much for any tips or insights!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
justvanrossumcommented, Jul 23, 2020

This snippet works:

from fontTools.ttLib import TTFont
from fontTools.pens.recordingPen import DecomposingRecordingPen
from fontTools.pens.ttGlyphPen import TTGlyphPen


src = "font.ttf"
dst = "font-decomposed.ttf"

f = TTFont(src)

glyfTable = f["glyf"]
gs = f.getGlyphSet()
for glyphName in gs.keys():
    if not glyfTable[glyphName].isComposite():
        continue
    dcPen = DecomposingRecordingPen(gs)
    gs[glyphName].draw(dcPen)
    ttPen = TTGlyphPen(None)
    dcPen.replay(ttPen)
    glyfTable[glyphName] = ttPen.glyph()

f.save(dst)
1reaction
anthrotypecommented, Jul 23, 2020

I will check out ufo2ft for a reference.

The way ufo2ft uses pathops.union() function will not work with a TTGlyph object as these don’t have getPen() method (unlike defcon or ufoLib2 Glyph). I have already updated Just’s script to remove overlaps using skia-pathops (via Path.simplify() which is the underlying method called upon by pathops.union). Just use that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How i can decompose all components in ttf export? - Glyphs
You could add a “Filter: Decompose” to your instances. Components have problems with some printers. Are you sure the components are to blame....
Read more >
should transformed components be decomposed in TTFs? #253
When generating TTFs, Glyphs.app decomposes all components which have any transforms (scale, flip, rotation, etc.) applied to them.
Read more >
Using Components - FontLab VI Help
To Decompose all components in the current layer, choose Glyph > Decompose or right-click in the Glyph window and choose Decompose from the...
Read more >
Composite Glyphs - Glyph Member Properties - High-Logic
Note: There is an option to decompose composite glyphs, on exporting a font, if one or more glyph members make use of scaled,...
Read more >
Outline Quality - Google Fonts documentation
The set of contours and components that defines the shape of a glyph is called ... cause the decomposition of the source's components...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found