|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { readFileSync } from "fs"; |
| 3 | +import { resolve } from "path"; |
| 4 | +import { parseFont } from "./parseFont"; |
| 5 | +import { composeText } from "./composeText"; |
| 6 | + |
| 7 | +function loadFixture(name: string): ArrayBuffer { |
| 8 | + const buf = readFileSync(resolve(__dirname, "../test/fixtures", name)); |
| 9 | + return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) as ArrayBuffer; |
| 10 | +} |
| 11 | + |
| 12 | +const roboto = parseFont(loadFixture("Roboto-Bold.ttf")); |
| 13 | + |
| 14 | +function bounds(polys: ReturnType<typeof composeText>) { |
| 15 | + let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity; |
| 16 | + for (const p of polys) for (const [x, y] of p.vertices) { |
| 17 | + minX = Math.min(minX, x); maxX = Math.max(maxX, x); |
| 18 | + minY = Math.min(minY, y); maxY = Math.max(maxY, y); |
| 19 | + } |
| 20 | + return { minX, maxX, minY, maxY }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("composeText", () => { |
| 24 | + it("renders a single line like textPolygons", () => { |
| 25 | + expect(composeText(roboto, "Poly").length).toBeGreaterThan(0); |
| 26 | + }); |
| 27 | + |
| 28 | + it("stacks multiple lines taller (world X = screen-down)", () => { |
| 29 | + const one = bounds(composeText(roboto, "Poly")); |
| 30 | + const three = bounds(composeText(roboto, "Poly\nCSS\nText")); |
| 31 | + expect(three.maxX - three.minX).toBeGreaterThan((one.maxX - one.minX) * 2); |
| 32 | + }); |
| 33 | + |
| 34 | + it("splits on \\n into independent lines", () => { |
| 35 | + const polys = composeText(roboto, "AB\nCD"); |
| 36 | + expect(polys.length).toBeGreaterThan(0); |
| 37 | + }); |
| 38 | + |
| 39 | + it("underline and strike add decoration polygons", () => { |
| 40 | + const plain = composeText(roboto, "Hi").length; |
| 41 | + const underlined = composeText(roboto, "Hi", { underline: true }).length; |
| 42 | + const struck = composeText(roboto, "Hi", { strike: true }).length; |
| 43 | + expect(underlined).toBeGreaterThan(plain); |
| 44 | + expect(struck).toBeGreaterThan(plain); |
| 45 | + }); |
| 46 | + |
| 47 | + it("alignment shifts the short line's horizontal position", () => { |
| 48 | + const sumY = (polys: ReturnType<typeof composeText>) => |
| 49 | + polys.reduce((s, p) => s + p.vertices.reduce((t, v) => t + v[1], 0), 0); |
| 50 | + const left = sumY(composeText(roboto, "wide line\nx", { align: "left" })); |
| 51 | + const right = sumY(composeText(roboto, "wide line\nx", { align: "right" })); |
| 52 | + // The short line slides right, so the total of world-Y (screen-right) grows. |
| 53 | + expect(right).toBeGreaterThan(left); |
| 54 | + }); |
| 55 | + |
| 56 | + it("arc warp spreads the text wider than unwarped", () => { |
| 57 | + const flat = bounds(composeText(roboto, "WordArt")); |
| 58 | + const arced = bounds(composeText(roboto, "WordArt", { warp: { shape: "arc", amount: 0.8 } })); |
| 59 | + // The arc bows letters up/down, so the vertical (world X) extent grows. |
| 60 | + expect(arced.maxX - arced.minX).toBeGreaterThan(flat.maxX - flat.minX); |
| 61 | + }); |
| 62 | + |
| 63 | + it("warp shapes change the geometry vs none", () => { |
| 64 | + const none = composeText(roboto, "Hi", { warp: { shape: "none" } }); |
| 65 | + const wave = composeText(roboto, "Hi", { warp: { shape: "wave", amount: 0.7 } }); |
| 66 | + const sum = (ps: ReturnType<typeof composeText>) => |
| 67 | + ps.reduce((s, p) => s + p.vertices.reduce((t, v) => t + v[0] + v[1], 0), 0); |
| 68 | + expect(Math.abs(sum(none) - sum(wave))).toBeGreaterThan(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it("larger lineHeight increases vertical extent", () => { |
| 72 | + const tight = bounds(composeText(roboto, "A\nB", { lineHeight: 1 })); |
| 73 | + const loose = bounds(composeText(roboto, "A\nB", { lineHeight: 2 })); |
| 74 | + expect(loose.maxX - loose.minX).toBeGreaterThan(tight.maxX - tight.minX); |
| 75 | + }); |
| 76 | + |
| 77 | + // ── regression: holes must never break ────────────────────────────────── |
| 78 | + it("never simplifies a glyph with holes, so the counter can't collapse", () => { |
| 79 | + // 'O' has a counter; its geometry must be identical at any simplify level. |
| 80 | + const exact = composeText(roboto, "O", { simplify: 0 }); |
| 81 | + const coarse = composeText(roboto, "O", { simplify: 8 }); |
| 82 | + expect(coarse.length).toBe(exact.length); |
| 83 | + }); |
| 84 | + |
| 85 | + it("still simplifies hole-less glyphs (poly reduction works)", () => { |
| 86 | + const exact = composeText(roboto, "M", { simplify: 0 }); |
| 87 | + const coarse = composeText(roboto, "M", { simplify: 8 }); |
| 88 | + expect(coarse.length).toBeLessThan(exact.length); |
| 89 | + }); |
| 90 | + |
| 91 | + it("round/bevel hold their counters too (inset never overruns the hole)", () => { |
| 92 | + for (const profile of ["round", "bevel"] as const) { |
| 93 | + expect(composeText(roboto, "o", { profile }).length).toBeGreaterThan(0); |
| 94 | + expect(composeText(roboto, "B", { profile, depth: 30 }).length).toBeGreaterThan(0); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + // ── regression: scale / merge / layered ───────────────────────────────── |
| 99 | + it("horizontal scale widens the run", () => { |
| 100 | + const a = bounds(composeText(roboto, "AV")); |
| 101 | + const b = bounds(composeText(roboto, "AV", { scaleX: 2 })); |
| 102 | + expect(b.maxY - b.minY).toBeGreaterThan((a.maxY - a.minY) * 1.6); |
| 103 | + }); |
| 104 | + |
| 105 | + it("vertical scale heightens the glyphs", () => { |
| 106 | + const a = bounds(composeText(roboto, "A")); |
| 107 | + const b = bounds(composeText(roboto, "A", { scaleY: 2 })); |
| 108 | + expect(b.maxX - b.minX).toBeGreaterThan((a.maxX - a.minX) * 1.6); |
| 109 | + }); |
| 110 | + |
| 111 | + it("merge reduces the polygon count", () => { |
| 112 | + const base = composeText(roboto, "Poly", { merge: false }); |
| 113 | + const merged = composeText(roboto, "Poly", { merge: true }); |
| 114 | + expect(merged.length).toBeLessThan(base.length); |
| 115 | + }); |
| 116 | + |
| 117 | + it("layered back color + oblique recolors and offsets the back", () => { |
| 118 | + const polys = composeText(roboto, "o", { depth: 10, color: "#ff0000", backColor: "#00ff00", oblique: [12, -12] }); |
| 119 | + const colors = new Set(polys.map((p) => p.color)); |
| 120 | + expect(colors.has("#ff0000")).toBe(true); // front cap |
| 121 | + expect(colors.has("#00ff00")).toBe(true); // back cap |
| 122 | + }); |
| 123 | +}); |
0 commit comments