Fichiers
loksi/mobile/ios/fonts.py
T
Aleop c3608b3938 Loksi 2026.10.0 (Android 3) : source correspondante
Export du dépôt interne d'Aleop, révision 5fbb921.
2026-10-09 22:14:30 +02:00

113 lignes
4.6 KiB
Python

#!/usr/bin/env python3
"""Police de l'application iOS Loksi, dérivée de Raleway (OFL-1.1). GPL non applicable.
fonts.py <raleway romain> <raleway italique> <dossier de sortie>
Six instances statiques, aux graisses des six fichiers DM Sans qu'elles remplacent :
400, 600 et 700, romain et italique. Les chiffres alignés de la fonction lnum sont
posés dans le cmap : ceux de Raleway sont bas-de-casse par défaut, illisibles dans un
code TOTP.
Raleway porte le nom réservé « Raleway » : une version modifiée ne peut pas le garder
(OFL-1.1, conditions 3). Les fichiers s'appellent donc « Loksi ». Le copyright et
la licence d'origine restent dans la table name.
Lancé par loksi.py avec le Python de mobile/.cache/venv (fontTools épinglé).
"""
import sys
from pathlib import Path
from fontTools.ttLib import TTFont
from fontTools.varLib import instancer
FAMILY = "Loksi"
POSTSCRIPT = "Loksi"
STYLES = [("Regular", 400, False), ("SemiBold", 600, False), ("Bold", 700, False),
("Italic", 400, True), ("SemiBold Italic", 600, True), ("Bold Italic", 700, True)]
DIGITS = range(0x30, 0x3A)
def lining_digits(font):
"""Table des chiffres bas-de-casse vers alignés, lue dans la fonction lnum."""
gsub = font["GSUB"].table
lookups = {i for record in gsub.FeatureList.FeatureRecord if record.FeatureTag == "lnum"
for i in record.Feature.LookupListIndex}
mapping = {}
for index in lookups:
for subtable in gsub.LookupList.Lookup[index].SubTable:
if hasattr(subtable, "ExtSubTable"):
subtable = subtable.ExtSubTable
mapping.update(getattr(subtable, "mapping", {}))
if not mapping:
# Raleway italique n'a pas de fonction lnum mais porte les mêmes glyphes .lf
# que le romain (relevé le 29/09/2026 au commit épinglé de google/fonts).
order = set(font.getGlyphOrder())
cmap = font.getBestCmap()
mapping = {cmap[c]: cmap[c] + ".lf" for c in DIGITS if cmap[c] + ".lf" in order}
return mapping
def build(source, weight, style, italic, out):
font = instancer.instantiateVariableFont(TTFont(source), {"wght": weight})
mapping = lining_digits(font)
cmap = dict(font.getBestCmap()) # copie : getBestCmap rend la table elle-même
moved = 0
for table in font["cmap"].tables:
if not table.isUnicode():
continue
for code in DIGITS:
glyph = table.cmap.get(code)
if glyph in mapping:
table.cmap[code] = mapping[glyph]
moved += 1
if not all(cmap[code] in mapping for code in DIGITS):
raise RuntimeError(f"{style} : chiffres sans équivalent lnum")
name = font["name"]
copyright_ = name.getDebugName(0)
license_ = name.getDebugName(13)
if "Raleway" not in (name.getDebugName(1) or ""):
raise RuntimeError("canari : la source ne s'appelle pas Raleway")
ribbi = style in ("Regular", "Italic", "Bold", "Bold Italic")
postscript = f"{POSTSCRIPT}-{style.replace(' ', '')}"
names = {
1: FAMILY if ribbi else f"{FAMILY} {style.replace(' Italic', '')}",
2: style if ribbi else ("Italic" if italic else "Regular"),
3: f"Aleop: {postscript}",
4: f"{FAMILY} {style}".replace(" Regular", ""),
6: postscript,
10: "Instance statique de Raleway, chiffres alignés, renommée pour Loksi (OFL-1.1).",
16: FAMILY,
17: style,
}
for record in list(name.names):
if record.nameID in (1, 2, 3, 4, 6, 10, 16, 17, 21, 22, 25):
name.removeNames(nameID=record.nameID)
for identifier, value in names.items():
name.setName(value, identifier, 3, 1, 0x409)
name.setName(value, identifier, 1, 0, 0)
if copyright_:
name.setName(copyright_, 0, 3, 1, 0x409)
if license_:
name.setName(license_, 13, 3, 1, 0x409)
font["OS/2"].usWeightClass = weight
path = out / f"{postscript}.ttf"
font.save(path)
return path, moved
def main():
roman, italic, out = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])
out.mkdir(parents=True, exist_ok=True)
for style, weight, is_italic in STYLES:
path, moved = build(italic if is_italic else roman, weight, style, is_italic, out)
check = TTFont(path)
family = " ".join(filter(None, (check["name"].getDebugName(i) for i in (1, 4, 6, 16))))
if "Raleway" in family:
raise RuntimeError(f"{path.name} : nom réservé Raleway encore présent")
print(f"{path.name} : graisse {weight}, {moved} chiffres alignés posés")
if __name__ == "__main__":
main()