File: fix_varfont.py

package info (click to toggle)
fonts-homecomputer 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,848 kB
  • sloc: python: 150; makefile: 22
file content (87 lines) | stat: -rw-r--r-- 2,872 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# coding: utf-8

import struct
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._g_l_y_f import ROUND_XY_TO_GRID, USE_MY_METRICS
from fontTools.ttLib.tables.DefaultTable import DefaultTable


def fix_font(path, outpath, name0, name1, name3):
    f = TTFont(path)
    glyf_table = f["glyf"]

    # Fix glyf table

    for name, glyph in glyf_table.glyphs.items():
        glyph.expand(glyf_table)
        found_metrics = False
        _lsb, width = f["hmtx"][name]
        if glyph.isComposite() and hasattr(glyph, "components"):
            for c in glyph.components:

                # Unset the component rounding flag
                c.flags &= ~ROUND_XY_TO_GRID

                # Set the use my metrics flag if appropriate
                c.flags &= ~USE_MY_METRICS
                if not found_metrics and f["hmtx"][c.glyphName][1] == width:
                    c.flags |= USE_MY_METRICS
                    found_metrics = True

    # Merge STAT, gasp and name tables

    patch = TTFont()
    patch.importXML("patch/patch.ttx")

    # Copy and patch name table
    name = patch["name"]
    name.setName(name0, 0, 3, 1, 0x409)
    name.setName(name1, 1, 3, 1, 0x409)
    name.setName(name3, 3, 3, 1, 0x409)
    name.setName(name1, 4, 3, 1, 0x409)
    name.setName(name1, 6, 3, 1, 0x409)
    f["name"] = name

    # Copy gasp, prep and STAT tables
    f["gasp"] = patch["gasp"]
    f["prep"] = patch["prep"]
    f["STAT"] = patch["STAT"]

    # The HVAR patch is not needed for building with fontmake
    # hvar = DefaultTable("HVAR")
    # glyphs = len(f.getGlyphOrder())
    # hvar.data = struct.pack(
    #     ">HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH",
    #     0x0001, 0x0000, 0x0000, 0x0014, 0x0000, 0x0000, 0x0000, 0x0000,
    #     0x0000, 0x0000, 0x0001, 0x0000, 0x000c, 0x0001, 0x0000, 0x0034,
    #     0x0002, 0x0003, 0x0000, 0x4000, 0x4000, 0x0000, 0x0000, 0x0000,
    #     0x0000, 0x0000, 0x0000, 0x0000, 0x4000, 0x4000, 0x0000, 0x4000,
    #     0x4000, 0x0000, 0x4000, 0x4000, glyphs, 0x0000, 0x0000
    # )
    # f["HVAR"] = hvar

    # Glyphs does not set the weight class of the default master
    # f["OS/2"].usWeightClass = 200

    f.save(outpath)
    f.close()
    f = TTFont(outpath)
    f.saveXML(outpath + ".ttx")


fix_font(
    "temp_out/Sixtyfour-VF.ttf",
    "fonts/Sixtyfour[wdth,wght].ttf",
    u"Copyright 2019 The Homecomputer Fonts Project Authors (https://github.com/jenskutilek/homecomputer-fonts). Based on the Commodore 64 character set.",
    u"Sixtyfour",
    u"2.000;jens;Sixtyfour",
)

fix_font(
    "temp_out/Workbench-VF.ttf",
    "fonts/Workbench[wdth,wght].ttf",
    u"Copyright 2019 The Homecomputer Fonts Project Authors (https://github.com/jenskutilek/homecomputer-fonts). Based on the Amiga 500 Workbench 1.3 character set.",
    u"Workbench",
    u"2.000;jens;Workbench",
)