Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
/**
|
|
* One text-wrapping routine for both the on-screen editor and the PNG export,
|
|
* so what you see is what gets exported. Measurement uses a 2D canvas context
|
|
* with the same font string the export draws with.
|
|
*/
|
|
|
|
import type { TextProps } from "./types";
|
|
|
|
let measureCtx: CanvasRenderingContext2D | null = null;
|
|
|
|
function ctx(): CanvasRenderingContext2D | null {
|
|
if (measureCtx) return measureCtx;
|
|
if (typeof document === "undefined") return null;
|
|
measureCtx = document.createElement("canvas").getContext("2d");
|
|
return measureCtx;
|
|
}
|
|
|
|
export function fontString(p: Pick<TextProps, "fontFamily" | "fontSize" | "fontWeight" | "italic">): string {
|
|
const family = p.fontFamily.includes(" ") ? `"${p.fontFamily}"` : p.fontFamily;
|
|
return `${p.italic ? "italic " : ""}${p.fontWeight} ${p.fontSize}px ${family}, system-ui, sans-serif`;
|
|
}
|
|
|
|
export function displayText(p: Pick<TextProps, "text" | "uppercase">): string {
|
|
return p.uppercase ? p.text.toUpperCase() : p.text;
|
|
}
|
|
|
|
/** Break text into lines that fit `width` (explicit newlines are kept). */
|
|
export function wrapText(p: TextProps, width: number, context?: CanvasRenderingContext2D | null): string[] {
|
|
const c = context ?? ctx();
|
|
const text = displayText(p);
|
|
if (!c) return text.split("\n");
|
|
c.font = fontString(p);
|
|
const spacing = p.letterSpacing ?? 0;
|
|
const measure = (s: string) => c.measureText(s).width + Math.max(0, s.length - 1) * spacing;
|
|
const out: string[] = [];
|
|
for (const paragraph of text.split("\n")) {
|
|
const words = paragraph.split(/\s+/).filter(Boolean);
|
|
if (words.length === 0) {
|
|
out.push("");
|
|
continue;
|
|
}
|
|
let line = "";
|
|
for (const word of words) {
|
|
const candidate = line ? `${line} ${word}` : word;
|
|
if (!line || measure(candidate) <= width) {
|
|
line = candidate;
|
|
continue;
|
|
}
|
|
out.push(line);
|
|
line = word;
|
|
}
|
|
// Hard-break a single word longer than the box.
|
|
while (measure(line) > width && line.length > 1) {
|
|
let cut = line.length - 1;
|
|
while (cut > 1 && measure(line.slice(0, cut)) > width) cut -= 1;
|
|
out.push(line.slice(0, cut));
|
|
line = line.slice(cut);
|
|
}
|
|
out.push(line);
|
|
}
|
|
return out;
|
|
}
|