Source code for jitxlib.gerber.format.font

"""A single-stroke font for legend text, ported from the ODB++ exporter.

Gerber has no native text (5.7), so a reference designator on the silkscreen
is a run of `D01` draws with a small circular aperture. A stroke font is the
natural fit: Gerber draws already have round caps and joins, so a polyline
maps onto draws with no region or outline handling at all.

The glyphs are `jitxlib-odb`'s `format/font.py` taken as they stand. They are
honest placeholder work - crude, hand-authored, lowercase reusing the
uppercase strokes - and taking them as-is is deliberate: a legend with crude
reference designators is assembly documentation and a legend with none is
not. Replacing them with a real single-stroke font is self-contained and
touches nothing outside this module.

Glyph coordinates live in a cell with x in [L, R] and y in [B, T], which is
0.1 wide by 0.3 tall. `strokes` scales that cell so the glyph height is the
text size, and lays the characters out left to right.
"""

from __future__ import annotations

Point = tuple[float, float]
Stroke = list[Point]

L, R = -0.05, 0.05
B, M, T = -0.1, 0.05, 0.2

CELL_HEIGHT = T - B
"""Glyph height in glyph units. One text `size` maps onto this."""

CELL_WIDTH = R - L
"""Glyph width in glyph units."""

ADVANCE = 0.18
"""Pen advance per character in glyph units: the glyph plus its right gap.

Wider than the glyph on purpose - 0.1 of ink and 0.08 of air - so adjacent
characters do not touch once the stroke aperture is added on both sides.
"""

STROKE_RATIO = 0.1
"""Stroke width as a fraction of the text size.

The aperture diameter, not a glyph property, which is why it is not the
`_W` the ODB font carries: that value is an ODB++ line width in the font
file's own units. A tenth of the cap height is legible at silkscreen
resolution and stays above the 0.1 mm line width fabs typically floor at for
the text sizes a board actually uses.
"""

_GLYPHS: dict[str, list[Stroke]] = {
    " ": [],
    "0": [[(L, B), (R, B), (R, T), (L, T), (L, B)], [(L, B), (R, T)]],
    "1": [[(-0.03, 0.15), (0, T), (0, B)]],
    "2": [[(L, T), (R, T), (R, M), (L, M), (L, B), (R, B)]],
    "3": [[(L, T), (R, T), (R, B), (L, B)], [(L, M), (R, M)]],
    "4": [[(L, T), (L, M), (R, M)], [(R, T), (R, B)]],
    "5": [[(R, T), (L, T), (L, M), (R, M), (R, B), (L, B)]],
    "6": [[(R, T), (L, T), (L, B), (R, B), (R, M), (L, M)]],
    "7": [[(L, T), (R, T), (0, B)]],
    "8": [[(L, B), (R, B), (R, T), (L, T), (L, B)], [(L, M), (R, M)]],
    "9": [[(L, B), (R, B), (R, T), (L, T), (L, M), (R, M)]],
    "A": [[(L, B), (0, T), (R, B)], [(-0.03, 0), (0.03, 0)]],
    "B": [[(L, B), (L, T), (R, T), (R, M), (L, M), (R, M), (R, B), (L, B)]],
    "C": [[(R, T), (L, T), (L, B), (R, B)]],
    "D": [[(L, B), (L, T), (0.02, T), (R, 0.13), (R, -0.03), (0.02, B), (L, B)]],
    "E": [[(R, T), (L, T), (L, B), (R, B)], [(L, M), (0.02, M)]],
    "F": [[(R, T), (L, T), (L, B)], [(L, M), (0.02, M)]],
    "G": [[(R, T), (L, T), (L, B), (R, B), (R, M), (0, M)]],
    "H": [[(L, T), (L, B)], [(R, T), (R, B)], [(L, M), (R, M)]],
    "I": [[(0, T), (0, B)], [(L, T), (R, T)], [(L, B), (R, B)]],
    "J": [[(R, T), (R, B), (L, B), (L, -0.02)]],
    "K": [[(L, T), (L, B)], [(R, T), (L, M), (R, B)]],
    "L": [[(L, T), (L, B), (R, B)]],
    "M": [[(L, B), (L, T), (0, M), (R, T), (R, B)]],
    "N": [[(L, B), (L, T), (R, B), (R, T)]],
    "O": [[(L, B), (R, B), (R, T), (L, T), (L, B)]],
    "P": [[(L, B), (L, T), (R, T), (R, M), (L, M)]],
    "Q": [[(L, B), (R, B), (R, T), (L, T), (L, B)], [(0, -0.02), (0.06, -0.1)]],
    "R": [[(L, B), (L, T), (R, T), (R, M), (L, M)], [(0, M), (R, B)]],
    "S": [[(R, T), (L, T), (L, M), (R, M), (R, B), (L, B)]],
    "T": [[(L, T), (R, T)], [(0, T), (0, B)]],
    "U": [[(L, T), (L, B), (R, B), (R, T)]],
    "V": [[(L, T), (0, B), (R, T)]],
    "W": [[(L, T), (-0.025, B), (0, M), (0.025, B), (R, T)]],
    "X": [[(L, T), (R, B)], [(R, T), (L, B)]],
    "Y": [[(L, T), (0, M), (R, T)], [(0, M), (0, B)]],
    "Z": [[(L, T), (R, T), (L, B), (R, B)]],
    ".": [[(0, B), (0, B)]],
    ",": [[(0.01, -0.08), (-0.01, -0.13)]],
    "-": [[(L, M), (R, M)]],
    "_": [[(L, B), (R, B)]],
    "+": [[(L, M), (R, M)], [(0, -0.02), (0, 0.12)]],
    "/": [[(L, B), (R, T)]],
    "\\": [[(L, T), (R, B)]],
    ":": [[(0, 0.1), (0, 0.1)], [(0, -0.05), (0, -0.05)]],
    ";": [[(0, 0.1), (0, 0.1)], [(0.01, -0.05), (-0.01, -0.1)]],
    "(": [[(0.03, T), (-0.03, M), (0.03, B)]],
    ")": [[(-0.03, T), (0.03, M), (-0.03, B)]],
    "!": [[(0, T), (0, 0)], [(0, B), (0, B)]],
    "?": [
        [(L, 0.15), (0, T), (R, 0.15), (R, 0.1), (0, 0.05), (0, 0)],
        [(0, B), (0, B)],
    ],
    "'": [[(0, T), (0, 0.13)]],
    '"': [[(-0.03, T), (-0.03, 0.13)], [(0.03, T), (0.03, 0.13)]],
    "*": [[(L, 0), (R, 0.1)], [(L, 0.1), (R, 0)], [(0, -0.02), (0, 0.12)]],
    "=": [[(L, 0.08), (R, 0.08)], [(L, 0), (R, 0)]],
    "<": [[(R, T), (L, M), (R, B)]],
    ">": [[(L, T), (R, M), (L, B)]],
    "#": [
        [(-0.025, B), (-0.025, T)],
        [(0.025, B), (0.025, T)],
        [(L, 0), (R, 0)],
        [(L, 0.1), (R, 0.1)],
    ],
    "$": [[(R, T), (L, T), (L, M), (R, M), (R, B), (L, B)], [(0, -0.12), (0, 0.22)]],
    "%": [[(L, B), (R, T)], [(L, T), (L, T)], [(R, B), (R, B)]],
    "&": [[(R, B), (L, 0.1), (0, T), (0.03, 0.1), (L, -0.02), (0, B), (R, 0.02)]],
    "@": [[(R, B), (L, B), (L, T), (R, T), (R, 0), (0, 0), (0, 0.1)]],
}


GLYPHS: dict[str, list[Stroke]] = dict(_GLYPHS)
"""Every glyph, with the lowercase letters reusing the uppercase strokes."""

for _ch in "abcdefghijklmnopqrstuvwxyz":
    GLYPHS[_ch] = _GLYPHS[_ch.upper()]


[docs] def stroke_width(size: float) -> float: """The aperture diameter legend text of this size is stroked with.""" return size * STROKE_RATIO
[docs] def extents(text: str, size: float) -> tuple[float, float]: """The (width, height) of the box `text` occupies at this size. The box is the ink, not the cells: the inter-character gap after the last character is not part of it. That matters because the anchor is measured against this box, and a centred reference designator built from cell widths sits visibly off-centre by half a gap. """ scale = size / CELL_HEIGHT cells = max(len(text), 1) return (((cells - 1) * ADVANCE + CELL_WIDTH) * scale, size)
[docs] def strokes(text: str, size: float, origin: Point = (0.0, 0.0)) -> list[Stroke]: """The polylines that draw `text`, in the text shape's own frame. Args: size: the cap height. Glyphs scale uniformly, so the aspect ratio the font was drawn at is preserved. origin: the lower-left corner of the box `extents` describes. Characters the font has no glyph for are skipped rather than substituted, and the pen still advances, so a missing glyph leaves a gap instead of shifting the rest of the string. """ scale = size / CELL_HEIGHT out: list[Stroke] = [] for index, char in enumerate(text): glyph = GLYPHS.get(char) if glyph is None: glyph = GLYPHS.get(char.upper()) if not glyph: continue dx = origin[0] + index * ADVANCE * scale - L * scale dy = origin[1] - B * scale for stroke in glyph: out.append([(dx + x * scale, dy + y * scale) for x, y in stroke]) return out