Files
suaveplan/packages/media/fonts/font-convert/src/utils/outline-to-string.ts
2026-05-27 23:47:53 +01:00

32 lines
1.0 KiB
TypeScript

import type { PathCommand } from "../types/typeface.js";
/**
* Serialises an array of PathCommand objects to the typeface.js outline string format.
* The outline string is space-separated tokens: "type [x y] [x1 y1] [x2 y2]".
* An optional scale factor is applied and coordinates are rounded to integers.
*/
export function outlineToString(commands: PathCommand[], scale = 1): string {
let result = "";
for (const cmd of commands) {
result += `${cmd.type} `;
if (cmd.x !== undefined && cmd.y !== undefined) {
result += `${Math.round(cmd.x * scale)} `;
result += `${Math.round(cmd.y * scale)} `;
}
if (cmd.x1 !== undefined && cmd.y1 !== undefined) {
result += `${Math.round(cmd.x1 * scale)} `;
result += `${Math.round(cmd.y1 * scale)} `;
}
if (cmd.x2 !== undefined && cmd.y2 !== undefined) {
result += `${Math.round(cmd.x2 * scale)} `;
result += `${Math.round(cmd.y2 * scale)} `;
}
}
return result;
}