codex/glyph-bearing-geometry.py

glyph-bearing-geometry.py
"""Original horizontal bearing schematic with an invented outline; PNG to cwd."""
from pathlib import Path

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Rectangle
import numpy as np


OUTLINE = np.array([
    (2, 0), (4, 0), (4, 3), (7, 3), (7, 5), (4, 5),
    (4, 6), (5, 7), (9, 7), (9, 9), (5, 9), (2, 7),
    (2, 5), (-2, 5), (-2, 3), (2, 3),
], dtype=float)
ADVANCE = 12


def dimension(ax, start, end, y, label, label_y, color):
    ax.annotate("", (end, y), (start, y),
                arrowprops={"arrowstyle": "<->", "color": color,
                            "linewidth": 1.4, "shrinkA": 0, "shrinkB": 0})
    ax.text((start + end) / 2, label_y, label, ha="center", va="center",
            fontsize=11, color=color)


def main():
    left, right = OUTLINE[:, 0].min(), OUTLINE[:, 0].max()
    bottom, top = OUTLINE[:, 1].min(), OUTLINE[:, 1].max()
    width, lsb, rsb = right - left, left, ADVANCE - right
    assert (left, right, width, lsb, rsb) == (-2, 9, 11, -2, 3)
    assert lsb + width + rsb == ADVANCE
    fig, ax = plt.subplots(figsize=(8, 4.7), dpi=100, facecolor="white")
    fig.subplots_adjust(left=0.04, right=0.98, bottom=0.12, top=0.79)
    fig.suptitle("Ink bounds and pen advance measure different things", y=0.96,
                 fontsize=15, weight="bold", color="#172330")
    fig.text(0.5, 0.88, "Chosen horizontal metrics; outline invented for this schematic",
             ha="center", fontsize=10.5, color="#45535d")
    ax.add_patch(Polygon(OUTLINE, closed=True, facecolor="#347ea8",
                         edgecolor="#286280", linewidth=1.2))
    ax.add_patch(Rectangle((left, bottom), width, top - bottom, fill=False,
                           edgecolor="#8fa4b0", linestyle="--", linewidth=1.3))
    ax.hlines(0, -3.2, 14, color="#788994", linewidth=1)
    ax.text(14.1, 0, "baseline", va="center", ha="left", fontsize=9, color="#45535d")
    for x in [left, right]:
        ax.vlines(x, -3.4, top + 1.1, color="#a0afb8", linewidth=0.9, linestyle=":")
    ax.vlines(0, -6.1, top + 1.1, color="#172330", linewidth=1, linestyle="--")
    ax.vlines(ADVANCE, -6.1, top + 1.1, color="#98355e", linewidth=1, linestyle="--")
    ax.plot(0, 0, "o", color="#172330", markersize=5)
    ax.plot(ADVANCE, 0, "s", color="#98355e", markersize=5)
    ax.text(-0.1, -0.85, "pen x = 0", ha="center", va="top", fontsize=10, color="#172330")
    ax.text(ADVANCE + 0.3, 0.7, "next pen\nx = 12", ha="left", fontsize=10, color="#98355e")
    ax.text(left - 0.2, 5.9, "x = -2", ha="right", fontsize=10, color="#45535d")
    ax.text(right + 0.15, 8, "x = 9", ha="left", fontsize=10, color="#45535d")
    dimension(ax, left, right, 10.7, "ink width = 11", 11.9, "#286280")
    dimension(ax, left, 0, -2.3, "lsb = -2", -3.45, "#9a5722")
    dimension(ax, right, ADVANCE, -2.3, "rsb = 3", -3.45, "#9a5722")
    dimension(ax, 0, ADVANCE, -5.4, "advance = 12", -6.55, "#98355e")
    ax.set_xlim(-4.5, 16.7)
    ax.set_ylim(-8, 13.3)
    ax.set_axis_off()
    fig.text(0.5, 0.045, "advance = left bearing + ink width + right bearing = -2 + 11 + 3 = 12",
             ha="center", fontsize=10, color="#172330")
    fig.savefig(Path.cwd() / (Path(__file__).stem + ".png"),
                dpi=100, facecolor="white", transparent=False)
    plt.close(fig)


if __name__ == "__main__":
    main()