codexglyph-side-bearing-geometry.py
glyph-side-bearing-geometry.py
"""Draw a hypothetical glyph's horizontal metrics; output PNG in cwd."""
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def main():
advance, left, ink_width = 600, 40, 500
right = advance - left - ink_width
assert right == 60
fig, ax = plt.subplots(figsize=(8.6, 3.8), dpi=100, facecolor="white")
fig.subplots_adjust(left=0.065, right=0.97, bottom=0.2, top=0.8)
fig.suptitle("Glyph spacing: ink width and advance differ", y=0.96,
fontsize=16, weight="bold", color="#172330")
ax.add_patch(Rectangle((left, 0), ink_width, 1.6,
facecolor="#deedf5", edgecolor="#326581",
linewidth=2))
ax.text(left + ink_width / 2, 0.8, "hypothetical ink bounding box",
ha="center", va="center", fontsize=11, color="#183c51")
ax.axhline(0, color="#677580", linewidth=1)
for x, label in [(0, "pen origin"), (advance, "next pen origin")]:
ax.axvline(x, color="#833b2b", linewidth=1.4, linestyle="--")
ax.plot(x, 0, "o", color="#833b2b", markersize=5)
ax.text(x, -0.2, label, ha="center", va="top", fontsize=10)
for start, end, label in [
(0, left, "left bearing = 40"),
(left, left + ink_width, "ink width = 500"),
(left + ink_width, advance, "right bearing = 60"),
]:
ax.annotate("", xy=(start, 1.95), xytext=(end, 1.95),
arrowprops={"arrowstyle": "<->", "color": "#274d63"})
ax.text((start + end) / 2, 2.13, label, ha="center", va="bottom",
fontsize=10)
ax.annotate("", xy=(0, -0.75), xytext=(advance, -0.75),
arrowprops={"arrowstyle": "<->", "color": "#833b2b"})
ax.text(advance / 2, -0.98, "advance = 40 + 500 + 60 = 600",
ha="center", va="top", fontsize=12, color="#833b2b")
ax.set_xlim(-95, 715)
ax.set_ylim(-1.25, 2.55)
ax.axis("off")
fig.text(0.5, 0.07,
"All lengths in font units; unpositioned left-to-right layout, not a measured font.",
ha="center", fontsize=10, color="#45535d")
fig.savefig(Path.cwd() / (Path(__file__).stem + ".png"), dpi=100,
facecolor="white", transparent=False)
plt.close(fig)
if __name__ == "__main__":
main()