Subset a TTF font. Reads the named fontfile and restricts the font to the glyphs. Parameters ---------- fontfile : FontPath Path to the font file glyphs : set[GlyphIndexType] Set of glyph indices to include in subset. Returns ------- fontTools.
(fontfile: FontPath, glyphs: set[GlyphIndexType])
| 36 | |
| 37 | |
| 38 | def get_glyphs_subset(fontfile: FontPath, glyphs: set[GlyphIndexType]) -> TTFont: |
| 39 | """ |
| 40 | Subset a TTF font. |
| 41 | |
| 42 | Reads the named fontfile and restricts the font to the glyphs. |
| 43 | |
| 44 | Parameters |
| 45 | ---------- |
| 46 | fontfile : FontPath |
| 47 | Path to the font file |
| 48 | glyphs : set[GlyphIndexType] |
| 49 | Set of glyph indices to include in subset. |
| 50 | |
| 51 | Returns |
| 52 | ------- |
| 53 | fontTools.ttLib.ttFont.TTFont |
| 54 | An open font object representing the subset, which needs to |
| 55 | be closed by the caller. |
| 56 | """ |
| 57 | options = subset.Options(glyph_names=True, recommended_glyphs=True, |
| 58 | retain_gids=True) |
| 59 | |
| 60 | # Prevent subsetting extra tables. |
| 61 | options.drop_tables += [ |
| 62 | 'FFTM', # FontForge Timestamp. |
| 63 | 'PfEd', # FontForge personal table. |
| 64 | 'BDF', # X11 BDF header. |
| 65 | 'meta', # Metadata stores design/supported languages (meaningless for subsets). |
| 66 | 'MERG', # Merge Table. |
| 67 | 'TSIV', # Microsoft Visual TrueType extension. |
| 68 | 'Zapf', # Information about the individual glyphs in the font. |
| 69 | 'bdat', # The bitmap data table. |
| 70 | 'bloc', # The bitmap location table. |
| 71 | 'cidg', # CID to Glyph ID table (Apple Advanced Typography). |
| 72 | 'fdsc', # The font descriptors table. |
| 73 | 'feat', # Feature name table (Apple Advanced Typography). |
| 74 | 'fmtx', # The Font Metrics Table. |
| 75 | 'fond', # Data-fork font information (Apple Advanced Typography). |
| 76 | 'just', # The justification table (Apple Advanced Typography). |
| 77 | 'kerx', # An extended kerning table (Apple Advanced Typography). |
| 78 | 'ltag', # Language Tag. |
| 79 | 'morx', # Extended Glyph Metamorphosis Table. |
| 80 | 'trak', # Tracking table. |
| 81 | 'xref', # The cross-reference table (some Apple font tooling information). |
| 82 | ] |
| 83 | # if fontfile is a ttc, specify font number |
| 84 | options.font_number = fontfile.face_index |
| 85 | |
| 86 | font = subset.load_font(fontfile, options) |
| 87 | subsetter = subset.Subsetter(options=options) |
| 88 | subsetter.populate(gids=glyphs) |
| 89 | subsetter.subset(font) |
| 90 | return font |
| 91 | |
| 92 | |
| 93 | def font_as_file(font): |
searching dependent graphs…