Initial Commit

This commit is contained in:
2026-05-27 23:47:53 +01:00
parent fbc70f96e6
commit 9b28db724f
18103 changed files with 1600554 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
import { BinaryFontPacker } from "@suaveplan/font-compress";
import type { TypefaceFont } from "@suaveplan/font-convert";
import { describe, it } from "@suaveplan/testing/runner";
import fc from "fast-check";
import { BinaryFontUnpacker } from "../src/index.js";
const packer = new BinaryFontPacker();
const unpacker = new BinaryFontUnpacker();
// Restrict coordinates to Int16 range (BTF dict uses Int16Array)
const coordArb = fc.integer({ min: -32768, max: 32767 });
const glyphArb = fc.record({
ha: coordArb,
x_min: coordArb,
x_max: coordArb,
o: fc.constant("m 0 0 z "),
});
const fontArb: fc.Arbitrary<TypefaceFont> = fc.record({
familyName: fc.string({ minLength: 1, maxLength: 30 }),
cssFontWeight: fc.constantFrom("100", "400", "700", "bold", "normal"),
cssFontStyle: fc.constantFrom("normal", "italic", "oblique"),
reversed: fc.boolean(),
ascender: coordArb,
descender: coordArb,
underlinePosition: coordArb,
underlineThickness: coordArb,
resolution: coordArb,
boundingBox: fc.record({
yMin: coordArb,
xMin: coordArb,
yMax: coordArb,
xMax: coordArb,
}),
original_font_information: fc.constant({}),
glyphs: fc.dictionary(
fc
.string({ minLength: 1, maxLength: 1 })
.filter((s) => s.charCodeAt(0) < 65536),
glyphArb,
{ minKeys: 0, maxKeys: 20 }
),
});
describe("property tests: pack/unpack roundtrip", () => {
it("familyName round-trips for all valid inputs", {
timeout: 30000,
}, () => {
fc.assert(
fc.property(fontArb, (font) => {
const result = unpacker.unpack(packer.pack(font));
return result.familyName === font.familyName;
}),
{ numRuns: 100 }
);
});
it("metrics round-trip for all valid inputs", { timeout: 30000 }, () => {
fc.assert(
fc.property(fontArb, (font) => {
const result = unpacker.unpack(packer.pack(font));
return (
result.ascender === font.ascender &&
result.descender === font.descender &&
result.resolution === font.resolution &&
result.reversed === font.reversed
);
}),
{ numRuns: 100 }
);
});
it("glyph count round-trips for all valid inputs", {
timeout: 30000,
}, () => {
fc.assert(
fc.property(fontArb, (font) => {
const result = unpacker.unpack(packer.pack(font));
return (
Object.keys(result.glyphs).length ===
Object.keys(font.glyphs).length
);
}),
{ numRuns: 100 }
);
});
});

View File

@@ -0,0 +1,113 @@
import { BinaryFontPacker } from "@suaveplan/font-compress";
import type { TypefaceFont } from "@suaveplan/font-convert";
import { describe, expect, it } from "@suaveplan/testing/runner";
import { BinaryFontUnpacker } from "../src/index.js";
function makeFont(overrides: Partial<TypefaceFont> = {}): TypefaceFont {
return {
familyName: "TestFont",
cssFontWeight: "400",
cssFontStyle: "normal",
reversed: false,
ascender: 800,
descender: -200,
underlinePosition: -100,
underlineThickness: 50,
resolution: 1000,
boundingBox: { yMin: -200, xMin: 0, yMax: 800, xMax: 1000 },
original_font_information: { source: "test" },
glyphs: {
A: { ha: 600, x_min: 10, x_max: 590, o: "m 0 0 l 100 0 z " },
B: {
ha: 560,
x_min: 20,
x_max: 540,
o: "m 0 0 b 10 20 30 40 50 60 z ",
},
Q: { ha: 700, x_min: 5, x_max: 695, o: "m 0 0 q 50 100 100 0 z " },
},
...overrides,
};
}
describe("roundtrip: BinaryFontPacker ↔ BinaryFontUnpacker", () => {
it("preserves all scalar font metrics", { timeout: 5000 }, () => {
const font = makeFont();
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(result.ascender).toBe(font.ascender);
expect(result.descender).toBe(font.descender);
expect(result.underlinePosition).toBe(font.underlinePosition);
expect(result.underlineThickness).toBe(font.underlineThickness);
expect(result.resolution).toBe(font.resolution);
});
it("preserves bounding box", { timeout: 5000 }, () => {
const font = makeFont();
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(result.boundingBox).toEqual(font.boundingBox);
});
it("preserves all glyph outline strings", { timeout: 5000 }, () => {
const font = makeFont();
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
for (const [char, glyph] of Object.entries(font.glyphs)) {
expect(result.glyphs[char]?.o).toBe(glyph.o);
}
});
it("preserves reversed=true flag", { timeout: 5000 }, () => {
const font = makeFont({ reversed: true });
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(result.reversed).toBe(true);
});
it("preserves reversed=false flag", { timeout: 5000 }, () => {
const font = makeFont({ reversed: false });
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(result.reversed).toBe(false);
});
it("handles empty glyph set", { timeout: 5000 }, () => {
const font = makeFont({ glyphs: {} });
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(Object.keys(result.glyphs)).toHaveLength(0);
});
it("handles large glyph with all command types", { timeout: 5000 }, () => {
const font = makeFont({
glyphs: {
X: {
ha: 800,
x_min: -50,
x_max: 850,
o: "m 0 0 l 100 200 b -10 -20 30 40 50 60 q 5 -10 15 20 z m 300 400 l 500 400 z ",
},
},
});
const result = new BinaryFontUnpacker().unpack(
new BinaryFontPacker().pack(font)
);
expect(result.glyphs["X"]!.o).toBe(font.glyphs["X"]!.o);
});
it("produces a smaller buffer than naive JSON", { timeout: 5000 }, () => {
const font = makeFont();
const packed = new BinaryFontPacker().pack(font);
const jsonBytes = new TextEncoder().encode(
JSON.stringify(font)
).byteLength;
expect(packed.byteLength).toBeLessThan(jsonBytes);
});
});