"""Ergänzt Cheltenham Classic (TTF) um Ä Ö Ü ä ö ü. Die Schrift bringt keine Umlaute mit. Die Glyphen entstehen als Komposita: Grundbuchstabe + zwei Punkte, die aus dem i-Punkt kopiert sind. Bei Versalien sitzen die Punkte um CAP_LIFT höher. python3 -m venv /tmp/ft && /tmp/ft/bin/pip install fonttools /tmp/ft/bin/python3 tools/cheltenham-umlaute.py IN.ttf OUT.ttf """ import sys from fontTools.pens.ttGlyphPen import TTGlyphPen from fontTools.ttLib import TTFont from fontTools.ttLib.tables._g_l_y_f import Glyph, GlyphComponent GAP = 60 # Abstand zwischen den beiden Punkten CAP_LIFT = 245 # Versalien: Punkte so viel höher als beim i # Optische Mitte, falls die Mitte der Umrisse nicht passt (Abweichung in x). NUDGE = {"a": -15, "A": 0} PAIRS = {"Ä": "A", "Ö": "O", "Ü": "U", "ä": "a", "ö": "o", "ü": "u"} def main(src, dst): f = TTFont(src) glyf, hmtx = f["glyf"], f["hmtx"] # i-Punkt: die obere der beiden Konturen von "i". i = glyf["i"] coords, ends, flags = i.getCoordinates(glyf) start = ends[0] + 1 if coords[ends[0]][1] < coords[-1][1] else 0 end = ends[0] if start == 0 else ends[1] dot = [coords[k] for k in range(start, end + 1)] dx0 = min(x for x, _ in dot) w = max(x for x, _ in dot) - dx0 dot = [(x - dx0, y) for x, y in dot] dflags = [flags[k] & 1 for k in range(start, end + 1)] # Glyphe "dieresis": zwei i-Punkte nebeneinander, Ursprung links. pen = TTGlyphPen(None) for off in (0, w + GAP): _draw(pen, [(x + off, y) for x, y in dot], dflags) _add(f, "dieresis", pen.glyph(), 0) width = 2 * w + GAP for ch, base in PAIRS.items(): name = base + "dieresis" b = glyf[base] b.recalcBounds(glyf) x = round((b.xMin + b.xMax - width) / 2) + NUDGE.get(base, 0) y = CAP_LIFT if base.isupper() else 0 g = Glyph() g.numberOfContours = -1 g.components = [] for comp, cx, cy in ((base, 0, 0), ("dieresis", x, y)): c = GlyphComponent() c.glyphName, c.x, c.y, c.flags = comp, cx, cy, 0 g.components.append(c) g.components[0].flags = 0x200 # USE_MY_METRICS _add(f, name, g, hmtx[base][0], ord(ch)) # OS/2: Latin-1-Ergänzung als abgedeckt markieren. f["OS/2"].ulUnicodeRange1 |= 1 << 1 f.save(dst) def _draw(pen, pts, onflags): # Eine geschlossene TrueType-Kontur aus Punkten + On-Curve-Flags zeichnen. n = len(pts) k0 = onflags.index(1) pts = pts[k0:] + pts[:k0] onflags = onflags[k0:] + onflags[:k0] pen.moveTo(pts[0]) buf = [] for p, on in zip(pts[1:] + pts[:1], onflags[1:] + onflags[:1]): if on: if buf: pen.qCurveTo(*buf, p) buf = [] else: pen.lineTo(p) else: buf.append(p) pen.closePath() def _add(f, name, glyph, adv, cp=None): glyf = f["glyf"] order = list(f.getGlyphOrder()) if name not in order: order.append(name) f.setGlyphOrder(order) glyf.glyphOrder = order glyf[name] = glyph glyph.recalcBounds(glyf) f["hmtx"][name] = (adv, glyph.xMin if hasattr(glyph, "xMin") else 0) if cp is not None: for t in f["cmap"].tables: if t.isUnicode(): t.cmap[cp] = name gdef = f["GDEF"].table.GlyphClassDef if gdef is not None: gdef.classDefs[name] = 3 if cp is None else 1 if __name__ == "__main__": main(sys.argv[1], sys.argv[2])