#!/usr/bin/env python3
"""Build Full, Kana and Additions editions of Kureedo.

Historical forms are alternates. Default ネ and ヰ stay Klee's; `hist`, `ss01`
and the per-letter `cv01`/`cv02` switch to the 子-shaped ネ and 井-shaped ヰ.
Letters Klee One lacks are added at their code points; a digraph is also reachable
from its letters through `hlig`.
"""
import argparse
import hashlib
import json
import unicodedata
from itertools import product
from pathlib import Path
from urllib.request import urlopen
from xml.etree import ElementTree

import math

import pathops
from fontTools import subset
from fontTools.pens.areaPen import AreaPen
from fontTools.pens.perimeterPen import PerimeterPen
from fontTools.otlLib.builder import buildLigatureSubstSubtable, buildLookup, buildSingleSubstSubtable
from fontTools.pens.cu2quPen import Cu2QuPen
from fontTools.pens.transformPen import TransformPen
from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.svgLib.path import parse_path
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables import otTables
from wenkai import extend_coverage, font_buffer

ROOT = Path(__file__).resolve().parent.parent
GLYPHS = ROOT / "sources/glyphs"
FONTS = ROOT / "fonts"
KLEE_COMMIT = "8b0532731b63ad8a445ca341d8d7d941079b83ab"
KLEE_URL = f"https://raw.githubusercontent.com/fontworks-fonts/Klee/{KLEE_COMMIT}/fonts/ttf/KleeOne-Regular.ttf"
KLEE_SHA256 = "74cb0a6523cc22b221ceaa7b78b56cea66512ec14b4145fd0102ffe27c30d084"
KLEE_PATH = ROOT / "sources/klee/KleeOne-Regular.ttf"

VERSION = (0, 5, 0)  # release tag v0.5.0; name ID 5 and head.fontRevision carry 0.500
COPYRIGHT = ("Copyright 2020 The Klee Project Authors (https://github.com/fontworks-fonts/Klee); "
             "historical glyphs Copyright 2026 mkpoli (https://mkpo.li, https://github.com/mkpoli/kureedo)")
URL = "https://github.com/mkpoli/kureedo"
BASELINE = 880  # y of the em top in the SVG sources (1000-unit em, y down)

# Each historical form: the modern letter it is an alternate of, its own code point in
# Kana Extended-A (Unicode 18.0), the SVG source, and its character-variant feature.
HISTORICAL = [
    dict(code=0x30CD, historic=0x1B127, svg="ne.svg", cv="cv01", label="Katakana ne, 子-shaped"),
    dict(code=0x30F0, historic=0x1B128, svg="wi.svg", cv="cv02", label="Katakana wi, 井-shaped"),
]
# Letters Klee One lacks, each with its code point and SVG source; a digraph also names the
# letters it joins, and `hlig` forms it from them.
LETTERS = [
    dict(code=0x1B000, svg="archaic-e.svg", letters=None, label="Katakana letter archaic e"),
    dict(code=0x30A0, svg="double-hyphen.svg", vertical="double-hyphen-vert.svg", letters=None, label="Katakana-hiragana double hyphen"),
    dict(code=0x2A708, svg="tomo.svg", letters="トモ", label="Katakana tomo ligature"),
    dict(code=0x30FF, svg="koto.svg", letters="コト", label="Katakana digraph koto"),
    # The kana repeat mark spans two cells: in vertical text its box is two ems tall, reaching
    # half an em above and below the em box, as GenZui Serif sets it.
    dict(code=0x3031, svg="repeat-mark.svg", letters=None, vadvance=2000, label="Vertical kana repeat mark"),
    dict(code=0x3032, svg="repeat-mark-voiced.svg", letters=None, vadvance=2000, label="Vertical kana repeat mark with voiced sound mark"),
]
# The Kana edition covers the available forms in all seven kana blocks,
# halfwidth kana, kana punctuation, and Kureedo's CJK-encoded TOMO ligature.
KANA_UNICODES = [0x20, *range(0x3000, 0x3100), *range(0x31F0, 0x3200),
                 *range(0xFF61, 0xFFA0), *range(0x1AFF0, 0x1B170), 0x2A708]

#: Imported code points redrawn from Klee parts. Their outlines replace the
#: normalized WenKai glyph in Full; the other editions do not carry them.
DESIGNED_IMPORTS = {0x5458: "yuan.svg", 0x8D1D: "bei.svg", 0x8D21: "gong.svg", 0x8D23: "ze.svg", 0x9C7C: "yu.svg"}

# Ainu small kana follow Klee's own small-kana convention (ッ against ツ): 78% of the full-size
# letter, centred in the cell on the baseline, and shifted up and to the right in vertical text.
# `weight` is the stroke thickness the scaled glyph keeps, as a fraction of the full-size
# stroke; Klee's own small kana keep 0.89–0.95 (median 0.92), plain scaling would leave 0.78.
# The values were settled by blind comparison rounds (docs/methods.md).
SMALL = dict(scale=0.78, x=114, y=-25, vertTop=329, vertX=130, vertY=0, weight=0.92)
# Marks on a small base are scaled with the letter and set with their centre at
# (base.xMax + cx, base.yMax + cy): the direction Klee's プ uses for its handakuten, at
# プ's clearance from the stroke scaled to the small letter (19 units).
SMALL_MARK = dict(scale=0.78, cx=71, cy=39, weight=0.92)


def stroke_thickness(glyph_set, name, scale=1.0):
    """Mean stroke thickness of a glyph: twice its area over its perimeter."""
    area, perimeter = AreaPen(glyph_set), PerimeterPen(glyph_set)
    for pen in (area, perimeter):
        glyph_set[name].draw(TransformPen(pen, (scale, 0, 0, scale, 0, 0)))
    return abs(area.value) * 2 / perimeter.value


def dilated(draw, radius, steps=12):
    """Union of the outline with copies shifted around a circle: a dilation by `radius`.
    At isolated radii the union degenerates (a counter merges into the outer contour and
    slivers appear); the result is checked against the source's contour count and the
    radius nudged until it is sound."""
    source = pathops.Path()
    draw(source.getPen())
    contours = len(list(pathops.simplify(source, fix_winding=True).contours))
    for attempt in range(8):
        r = radius * (1 + 0.004 * attempt)
        result = pathops.Path()
        for i in range(steps):
            angle = 2 * math.pi * i / steps
            part = pathops.Path()
            draw(TransformPen(part.getPen(), (1, 0, 0, 1, r * math.cos(angle), r * math.sin(angle))))
            result = pathops.op(result, part, pathops.PathOp.UNION) if i else part
        result = pathops.simplify(result, fix_winding=True)
        if len(list(result.contours)) <= contours:
            return result
    raise ValueError("dilation did not converge")


def path_thickness(path):
    """Mean stroke thickness of a pathops path, as stroke_thickness measures a glyph."""
    area, perimeter = AreaPen(), PerimeterPen()
    path.draw(area)
    path.draw(perimeter)
    return abs(area.value) * 2 / perimeter.value


def scaled_glyph(glyph_set, name, scale, dx, dy, weight):
    """Scale a glyph and, when `weight` is set, thicken it back to that fraction of the
    full-size stroke. The dilation radius is solved on the measured result, since rounding
    of the corners makes a dilation by r add a little less than 2r to the measured stroke."""
    def draw(pen):
        glyph_set[name].draw(TransformPen(pen, (scale, 0, 0, scale, dx, dy)))
    tt = TTGlyphPen(None)
    target = weight * stroke_thickness(glyph_set, name) if weight else 0
    if target > stroke_thickness(glyph_set, name, scale):
        lo, hi = 0.0, 16.0
        for _ in range(7):
            mid = (lo + hi) / 2
            if path_thickness(dilated(draw, mid)) < target:
                lo = mid
            else:
                hi = mid
        path = dilated(draw, (lo + hi) / 2)
        path.draw(Cu2QuPen(tt, max_err=0.2, reverse_direction=not path.clockwise))
    else:
        draw(tt)
    return tt.glyph()


def fetch_klee():
    if not KLEE_PATH.exists():
        KLEE_PATH.parent.mkdir(parents=True, exist_ok=True)
        with urlopen(KLEE_URL, timeout=60) as response:
            KLEE_PATH.write_bytes(response.read())
    if hashlib.sha256(KLEE_PATH.read_bytes()).hexdigest() != KLEE_SHA256:
        raise ValueError("Klee One checksum mismatch")
    return TTFont(KLEE_PATH, recalcTimestamp=False)


def svg_glyph(path: Path, *, normalize=False):
    """Convert an SVG outline, optionally using the study's normalized path winding."""
    pen = TTGlyphPen(None)
    if normalize:
        outline = pathops.Path()
        target = TransformPen(outline.getPen(), (1, 0, 0, -1, 0, BASELINE))
        for element in ElementTree.parse(path).getroot().iter("{http://www.w3.org/2000/svg}path"):
            parse_path(element.attrib["d"], target)
        outline.draw(Cu2QuPen(pen, max_err=0.2, reverse_direction=not outline.clockwise))
        return pen.glyph()
    curves = Cu2QuPen(TransformPen(pen, (1, 0, 0, -1, 0, BASELINE)), max_err=0.2, reverse_direction=True)
    for element in ElementTree.parse(path).getroot().iter("{http://www.w3.org/2000/svg}path"):
        parse_path(element.attrib["d"], curves)
    return pen.glyph(dropImpliedOnCurves=True)


class Builder:
    def __init__(self, font: TTFont, small=None, small_mark=None, sources=None):
        self.small = {**SMALL, **(small or {})}
        self.small_mark = {**SMALL_MARK, **(small_mark or {})}
        self.sources = sources or {}  # SVG file name -> path replacing the one in sources/glyphs (candidate builds)
        self.vertical = {}  # glyph -> its vertical variant, registered under vert and vrt2
        self.font = font
        self.cmap = font.getBestCmap()
        self.order = list(font.getGlyphOrder())
        self.name_id = max(n.nameID for n in font["name"].names if n.nameID < 256) + 1
        self.name_id = max(self.name_id, 256)

    def source(self, svg):
        # Match the accepted study fonts' conversion, including quadratic rounding.
        return svg_glyph(Path(self.sources.get(svg, GLYPHS / svg)),
                         normalize=svg in ("tomo.svg", "tomo-straight.svg", "double-hyphen.svg", "double-hyphen-vert.svg", "archaic-e.svg",
                                          "repeat-mark.svg", "repeat-mark-voiced.svg", "koto.svg"))

    def put(self, name, glyph, advance=1000, origin=BASELINE, mark=False, vadvance=1000):
        glyph.recalcBounds(self.font["glyf"])
        self.font["glyf"][name] = glyph
        self.font["hmtx"][name] = (advance, glyph.xMin)
        self.font["vmtx"][name] = (0 if mark else vadvance, origin - glyph.yMax)
        self.order.append(name)
        if mark:
            self.font["GDEF"].table.GlyphClassDef.classDefs[name] = 3

    def encode(self, code, name):
        for table in self.font["cmap"].tables:
            # Format 4 subtables hold the BMP only; supplementary code points go to format 12.
            if table.isUnicode() and (code <= 0xFFFF or table.format == 12):
                table.cmap[code] = name

    def add_name(self, text):
        self.font["name"].setName(text, self.name_id, 3, 1, 0x409)
        self.name_id += 1
        return self.name_id - 1

    def add_feature(self, tag, lookup, params=None):
        """Register one lookup under `tag` for every language system.

        A language system that already lists the tag gets the lookup appended to
        that feature; the others get a new feature record.
        """
        gsub = self.font["GSUB"].table
        gsub.LookupList.Lookup.append(lookup)
        gsub.LookupList.LookupCount = len(gsub.LookupList.Lookup)
        lookup_index = gsub.LookupList.LookupCount - 1
        records = gsub.FeatureList.FeatureRecord
        extended = set()
        new_index = None
        for script in gsub.ScriptList.ScriptRecord:
            systems = [script.Script.DefaultLangSys] if script.Script.DefaultLangSys else []
            systems += [r.LangSys for r in script.Script.LangSysRecord]
            for system in systems:
                existing = [i for i in system.FeatureIndex if records[i].FeatureTag == tag]
                if existing:
                    feature = records[existing[0]].Feature
                    if id(feature) not in extended:
                        feature.LookupListIndex.append(lookup_index)
                        feature.LookupCount = len(feature.LookupListIndex)
                        extended.add(id(feature))
                    continue
                if new_index is None:
                    record = otTables.FeatureRecord()
                    record.FeatureTag = tag
                    record.Feature = otTables.Feature()
                    record.Feature.FeatureParams = params
                    record.Feature.LookupListIndex = [lookup_index]
                    record.Feature.LookupCount = 1
                    records.append(record)
                    new_index = len(records) - 1
                system.FeatureIndex.append(new_index)
                system.FeatureCount = len(system.FeatureIndex)
        gsub.FeatureList.FeatureCount = len(records)

    def add_historical(self):
        """Add the historical outlines and the features that reach them."""
        all_forms = {}
        for form in HISTORICAL:
            base = self.cmap[form["code"]]
            # The letter has its own code point in Kana Extended-A; the same glyph is
            # also the modern letter's historical alternate.
            name = f"uni{form['historic']:04X}"
            self.put(name, self.source(form["svg"]))
            self.encode(form["historic"], name)
            self.cmap[form["historic"]] = name
            # Klee's own horizontal/vertical alternates of the letter also yield
            # the historical form, whatever order the features apply in.
            mapping = {base: name}
            for suffix in (".vert", ".hori"):
                if base + suffix in self.order:
                    mapping[base + suffix] = name
            all_forms.update(mapping)
            params = otTables.FeatureParamsCharacterVariants()
            params.Format = 0
            params.FeatUILabelNameID = self.add_name(form["label"])
            params.FeatUITooltipTextNameID = 0
            params.SampleTextNameID = self.add_name(chr(form["code"]))
            params.NumNamedParameters = 0
            params.FirstParamUILabelNameID = 0
            params.CharCount = 1
            params.Character = [form["code"]]
            self.add_feature(form["cv"], buildLookup([buildSingleSubstSubtable(mapping)]), params)
        historical = buildLookup([buildSingleSubstSubtable(all_forms)])
        self.add_feature("hist", historical)
        self.extend_aalt({self.cmap[form["code"]]: f'uni{form["historic"]:04X}' for form in HISTORICAL})
        params = otTables.FeatureParamsStylisticSet()
        params.Version = 0
        params.UINameID = self.add_name("Edo-period printed forms")
        self.add_feature("ss01", historical, params)

    def add_letters(self):
        """Add the missing letters at their code points; `add_digraphs` forms the digraphs from their letters."""
        self.ligatures = {}
        for form in LETTERS:
            name = f"uni{form['code']:04X}"
            vadvance = form.get("vadvance", 1000)
            self.put(name, self.source(form["svg"]), vadvance=vadvance, origin=BASELINE + (vadvance - 1000) // 2)
            self.encode(form["code"], name)
            self.cmap[form["code"]] = name
            if form.get("vertical"):
                self.put(name + ".vert", self.source(form["vertical"]))
                self.vertical[name] = name + ".vert"
            if form["letters"]:
                variants = []
                for char in form["letters"]:
                    base = self.cmap[ord(char)]
                    variants.append([base + suffix for suffix in ("", ".hori", ".vert")
                                     if base + suffix in self.order])
                self.ligatures.update({letters: name for letters in product(*variants)})

    def add_digraphs(self):
        """`hlig` forming each digraph from its letters. Its lookup comes after the mark lookups, so `ccmp`
        has already composed a marked letter (ト゚) and コト゚ stays コ and ト゚."""
        self.add_feature("hlig", buildLookup([buildLigatureSubstSubtable(self.ligatures)]))

        # The accepted tomo has a curved default and a paired straight-left form; `ss02` comes after
        # `hlig`, so a 𪜈 formed from トモ takes it too.
        tomo = self.cmap[0x2A708]
        straight = tomo + ".straight"
        self.put(straight, self.source("tomo-straight.svg"))
        params = otTables.FeatureParamsStylisticSet()
        params.Version = 0
        params.UINameID = self.add_name("Tomo: straight left stroke")
        self.font["name"].setName("トモ合字の左画を直線に", params.UINameID, 3, 1, 0x411)
        self.add_feature("ss02", buildLookup([buildSingleSubstSubtable({tomo: straight})]), params)

    def add_marks(self):
        """Compose kana with combining dakuten and handakuten into one cell."""
        positions = json.loads((GLYPHS / "mark-positions.json").read_text())
        substitutions = {}
        for code in (0x309A, 0x3099):
            mark = f"kanaMark{code:04X}"
            self.put(mark, self.source(f"mark-{code:04x}.svg"), advance=0, mark=True)
            self.encode(code, mark)
            m = self.small_mark
            small_mark = mark
            if m["scale"] != 1 or m["weight"]:
                small_mark = mark + ".small"
                self.put(small_mark, scaled_glyph(self.font.getGlyphSet(), mark, m["scale"], 0, 0, m["weight"]), advance=0, mark=True)
            for base, base_name in self.cmap.items():
                if not (0x30A1 <= base <= 0x30FA or 0x31F0 <= base <= 0x31FF):
                    continue
                sequence = chr(base) + chr(code)
                nfc = unicodedata.normalize("NFC", sequence)
                if len(nfc) == 1 and ord(nfc) in self.cmap:
                    composed = self.cmap[ord(nfc)]
                elif sequence in positions:
                    composed = f"kanaComposite{base:04X}_{code:04X}"
                    dx, dy = positions[sequence]
                    pen = TTGlyphPen(self.font.getGlyphSet())
                    pen.addComponent(base_name, (1, 0, 0, 1, 0, 0))
                    if 0x31F0 <= base <= 0x31FF:
                        m = self.small_mark
                        box = self.font["glyf"][small_mark]
                        mx, my = (box.xMin + box.xMax) / 2, (box.yMin + box.yMax) / 2
                        b = self.font["glyf"][base_name]
                        tx, ty = b.xMax + m["cx"] - mx, b.yMax + m["cy"] - my
                        pen.addComponent(small_mark, (1, 0, 0, 1, round(tx), round(ty)))
                        if base_name in self.vertical:
                            vpen = TTGlyphPen(self.font.getGlyphSet())
                            vpen.addComponent(self.vertical[base_name], (1, 0, 0, 1, 0, 0))
                            vb = self.font["glyf"][self.vertical[base_name]]
                            vpen.addComponent(small_mark, (1, 0, 0, 1, round(tx + vb.xMax - b.xMax), round(ty + vb.yMax - b.yMax)))
                            self.put(composed + ".vert", vpen.glyph(), origin=vb.yMax + self.font["vmtx"][self.vertical[base_name]][1])
                            self.vertical[composed] = composed + ".vert"
                    else:
                        pen.addComponent(mark, (1, 0, 0, 1, dx, dy))
                    # Small kana sit lower than full-size kana in vertical text.
                    origin = self.font["glyf"][base_name].yMax + self.font["vmtx"][base_name][1]
                    self.put(composed, pen.glyph(), origin=origin)
                else:
                    continue
                substitutions[(base_name, mark)] = composed
        self.add_feature("ccmp", buildLookup([buildLigatureSubstSubtable(substitutions)]))
        if self.vertical:
            for tag in ("vert", "vrt2"):
                self.add_feature(tag, buildLookup([buildSingleSubstSubtable(dict(self.vertical))]))

    def add_small_kana(self):
        """Klee One lacks U+31F0–31FF. Scale its full-size kana to 65%, set at the lower right."""
        glyphs = self.font.getGlyphSet()
        sm = self.small
        for code, base in zip(range(0x31F0, 0x3200), "クシストヌハヒフヘホムラリルレロ"):
            name = f"uni{code:04X}"
            for suffix, dx, dy in (("", 0, 0), (".vert", sm["vertX"], sm["vertY"])):
                if suffix and not (dx or dy):
                    continue
                small = scaled_glyph(glyphs, self.cmap[ord(base)], sm["scale"], sm["x"] + dx, sm["y"] + dy, sm["weight"])
                small.recalcBounds(self.font["glyf"])
                self.font["glyf"][name + suffix] = small
                self.font["hmtx"][name + suffix] = (1000, small.xMin)
                self.font["vmtx"][name + suffix] = (1000, sm["vertTop"])
                self.order.append(name + suffix)
                if suffix:
                    self.vertical[name] = name + suffix
            self.encode(code, name)
            self.cmap[code] = name

    def sort_features(self):
        """Keep the feature list sorted by tag, as the layout specification asks."""
        gsub = self.font["GSUB"].table
        records = gsub.FeatureList.FeatureRecord
        ordered = sorted(range(len(records)), key=lambda i: (records[i].FeatureTag, i))
        remap = {old: new for new, old in enumerate(ordered)}
        gsub.FeatureList.FeatureRecord = [records[i] for i in ordered]
        for script in gsub.ScriptList.ScriptRecord:
            systems = [script.Script.DefaultLangSys] if script.Script.DefaultLangSys else []
            systems += [r.LangSys for r in script.Script.LangSysRecord]
            for system in systems:
                system.FeatureIndex = sorted(remap[i] for i in system.FeatureIndex)
                if system.ReqFeatureIndex != 0xFFFF:
                    system.ReqFeatureIndex = remap[system.ReqFeatureIndex]

    def extend_aalt(self, alternates):
        """Append the historical forms to Klee's `aalt` alternate sets."""
        gsub = self.font["GSUB"].table
        for record in gsub.FeatureList.FeatureRecord:
            if record.FeatureTag != "aalt":
                continue
            for index in record.Feature.LookupListIndex:
                for subtable in gsub.LookupList.Lookup[index].SubTable:
                    if subtable.LookupType == 3:
                        for base, name in alternates.items():
                            subtable.alternates.setdefault(base, []).append(name)

    def finish(self):
        self.sort_features()
        self.font.setGlyphOrder(self.order)


JAPANESE = {"Kureedo Japanese": "クレード 日本語",
            "Kureedo Full": "クレード フル", "Kureedo Kana": "クレード かな",
            "Kureedo Additions": "クレード 追加字"}


def set_names(font: TTFont, family: str):
    ps = family.replace(" ", "") + "-Regular"
    version = f"{VERSION[0]}.{VERSION[1]}{VERSION[2]:02d}"
    names = {0: COPYRIGHT, 1: family, 2: "Regular", 3: f"{version};KRDO;{ps}", 4: f"{family} Regular",
             5: f"Version {version}", 6: ps, 7: f"{family} is an independent derivative of Klee One; Klee is a trademark of Fontworks Inc.",
             8: "mkpoli", 9: "Fontworks Inc.; mkpoli", 11: "https://mkpo.li", 12: "https://mkpo.li"}
    table = font["name"]
    table.names = [n for n in table.names if n.nameID not in names or (n.platformID, n.platEncID) == (3, 1)]
    for record in table.names:
        if record.nameID in names and record.langID == 0x409:
            record.string = names[record.nameID].encode(record.getEncoding())
    table.names = [n for n in table.names if n.langID != 0x411 or n.nameID >= 256]
    ja = JAPANESE.get(family, family)
    table.setName(ja, 1, 3, 1, 0x411)
    table.setName("Regular", 2, 3, 1, 0x411)
    table.setName(ja + " Regular", 4, 3, 1, 0x411)
    font["head"].fontRevision = float(version)
    font["OS/2"].achVendID = "KRDO"


def subset_font(font, unicodes):
    options = subset.Options()
    options.hinting = False
    options.name_IDs = "*"
    options.name_legacy = True
    options.name_languages = "*"
    options.layout_features += ["hist", "hlig", "ss01", "ss02", "cv01", "cv02"]
    options.notdef_outline = True
    options.glyph_names = True
    result = TTFont(font_buffer(font), recalcTimestamp=False)
    sub = subset.Subsetter(options=options)
    sub.populate(unicodes=unicodes)
    sub.subset(result)
    return result


def apply_designed_imports(font):
    """Replace imported glyphs with outlines drawn from Klee sources."""
    for cp, svg in sorted(DESIGNED_IMPORTS.items()):
        name = font.getBestCmap().get(cp)
        if name is None:
            raise ValueError(f"U+{cp:04X} is not in the merged font")
        glyph = svg_glyph(GLYPHS / svg, normalize=True)
        glyph.recalcBounds(font["glyf"])
        # A retained composite may reference this glyph. Keep the donor outline
        # under its own name and point that composite at it, so only the encoded
        # character changes.
        references = []
        for glyph_name in font.getGlyphOrder():
            candidate = font["glyf"][glyph_name]
            if not candidate.isComposite():
                continue
            if name in candidate.getComponentNames(font["glyf"]):
                references.append(candidate)
        if references:
            preserved = name + ".donor"
            if preserved in font.getGlyphOrder():
                raise ValueError(f"Glyph name collision: {preserved}")
            font["glyf"][preserved] = font["glyf"][name]
            font["hmtx"][preserved] = font["hmtx"][name]
            if "vmtx" in font:
                font["vmtx"][preserved] = font["vmtx"][name]
            order = list(font.getGlyphOrder())
            if preserved not in order:
                order.append(preserved)
            font.setGlyphOrder(order)
            font["glyf"].glyphOrder = order
            for glyph_ref in references:
                for component in glyph_ref.components:
                    if component.glyphName == name:
                        component.glyphName = preserved
        old_ymax = font["glyf"][name].yMax
        font["glyf"][name] = glyph
        advance, _ = font["hmtx"][name]
        font["hmtx"][name] = (advance, glyph.xMin)
        vertical, top = font["vmtx"][name]
        font["vmtx"][name] = (vertical, top + old_ymax - glyph.yMax)
    if DESIGNED_IMPORTS:
        font["maxp"].recalc(font)


def save_edition(font, family, out_dir):
    result = []
    for flavor, suffix in ((None, ".ttf"), ("woff2", ".woff2")):
        font.flavor = flavor
        out = out_dir / (family.replace(" ", "") + "-Regular" + suffix)
        font.save(out)
        result.append(out)
        print(f"Built {out.name} ({out.stat().st_size:,} bytes)")
    font.flavor = None
    return result


def build(out_dir: Path = FONTS, small=None, small_mark=None, family_suffix="", sources=None):
    font = fetch_klee()
    original_points = set(font.getBestCmap())
    builder = Builder(font, small, small_mark, sources)
    builder.add_small_kana()
    builder.add_historical()
    builder.add_letters()
    builder.add_marks()
    builder.add_digraphs()
    builder.finish()
    out_dir.mkdir(parents=True, exist_ok=True)

    # Retain the Japanese reference for checks and derive the kana overlays.
    set_names(font, "Kureedo Japanese" + family_suffix)
    reference = ROOT / "build/japanese-base.ttf"
    reference.parent.mkdir(parents=True, exist_ok=True)
    font.save(reference)
    outputs = {}
    scopes = {"Kureedo Kana": KANA_UNICODES,
              "Kureedo Additions": set(font.getBestCmap()) - original_points}
    for family, points in scopes.items():
        edition = subset_font(font, points)
        set_names(edition, family + family_suffix)
        outputs[family] = save_edition(edition, family + family_suffix, out_dir)
    # Name the base before merging, so the merger can append donor attribution.
    set_names(font, "Kureedo Full" + family_suffix)
    full = extend_coverage(font)
    apply_designed_imports(full)
    outputs["Kureedo Full"] = save_edition(full, "Kureedo Full" + family_suffix, out_dir)
    return outputs["Kureedo Full"][0], outputs["Kureedo Kana"][1]


if __name__ == "__main__":
    argparse.ArgumentParser(description=__doc__).parse_args()
    build()
