#!/usr/bin/env python3 # mkfont.py - build the squeezed TallClock font for the KWGT clock # Copyright (C) 2026 Danilo M. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """Derive the clock font from an OFL sans: pin variable axes, squeeze it horizontally to match the very narrow iOS lock-screen digits, and set vertical metrics so every source font gives the same layout in the preset (room for the date above the digits, nothing below them). Needs fontTools (build-time only). usage: mkfont.py [axes] [xscale] axes: variable-font settings, e.g. "wght=350" or "wght=300,wdth=25"; "" for static fonts xscale: horizontal squeeze, e.g. 0.56 """ import math import sys from fontTools.pens.ttGlyphPen import TTGlyphPen from fontTools.ttLib import TTFont from fontTools.varLib.instancer import instantiateVariableFont src, out = sys.argv[1], sys.argv[2] axes = sys.argv[3] if len(sys.argv) > 3 else "" sx = float(sys.argv[4]) if len(sys.argv) > 4 else 1.0 font = TTFont(src) if "fvar" in font: loc = {k: float(v) for k, v in (a.split("=") for a in axes.split(",") if a)} font = instantiateVariableFont(font, loc) glyf, hmtx = font["glyf"], font["hmtx"] def round_dots(g): """Redraw each contour of a simple glyph (the colon) as a circle of the same centre and size: round dots like the reference, whatever the font.""" pen, k = TTGlyphPen(None), 1 / math.cos(math.pi / 8) start = 0 for end in g.endPtsOfContours: pts = g.coordinates[start:end + 1] start = end + 1 xs, ys = [p[0] for p in pts], [p[1] for p in pts] cx, cy = (min(xs) + max(xs)) / 2, (min(ys) + max(ys)) / 2 r = (max(xs) - min(xs) + max(ys) - min(ys)) / 4 # 8 off-curve points on a circumscribed octagon, clockwise (TrueType outer contour). pen.qCurveTo(*[(round(cx + r * k * math.cos(-i * math.pi / 4)), round(cy + r * k * math.sin(-i * math.pi / 4))) for i in range(8)], None) pen.closePath() return pen.glyph() for name in font.getGlyphOrder(): g = glyf[name] if name == "colon" and not g.isComposite() and g.numberOfContours > 0: # Dots keep their shape (made round, like the reference); only the # spacing is squeezed, with the dots re-centred in the narrower advance. adv, lsb = hmtx[name] csx = (1 + sx) / 2 # half the squeeze: full squeeze crowds the dots dx = round((adv * csx - adv) / 2) # Centre the dot pair on the digits' vertical midpoint (iOS style). zero = glyf[font.getBestCmap()[ord("0")]] g.recalcBounds(glyf) dy = round((zero.yMin + zero.yMax) / 2 - (g.yMin + g.yMax) / 2) g.coordinates.translate((dx, dy)) g = glyf[name] = round_dots(g) g.recalcBounds(glyf) hmtx[name] = (round(adv * csx), g.xMin) continue if g.isComposite(): for c in g.components: c.x = round(c.x * sx) elif g.numberOfContours > 0: g.coordinates.scale((sx, 1)) g.coordinates.toInt() adv, lsb = hmtx[name] hmtx[name] = (round(adv * sx), round(lsb * sx)) g.recalcBounds(glyf) # Same layout for every font: digits fill [0.02 .. 0.02+h] from the bottom, # 0.24 em of room above them for the date, ~nothing below (no descenders). # The preset's date size and anchors assume these numbers. upm = font["head"].unitsPerEm top = glyf[font.getBestCmap()[ord("0")]].yMax asc, desc = top + round(0.24 * upm), round(0.02 * upm) os2, hhea = font["OS/2"], font["hhea"] hhea.ascent, hhea.descent, hhea.lineGap = asc, -desc, 0 os2.sTypoAscender, os2.sTypoDescender, os2.sTypoLineGap = asc, -desc, 0 os2.usWinAscent, os2.usWinDescent = asc, desc # OFL: a modified font must not reuse the original name. for rec in font["name"].names: if rec.nameID in (1, 3, 4, 6, 16): rec.string = "TallClock" if rec.nameID != 6 else "TallClock-Regular" font.save(out)