Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): a word too long to fit breaks instead of leaving the frame [rebuild]

David Leb7188c53563f
diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.ts
index 30f6ad48..bfdcbba2 100644
--- a/site/packages/effects-kit/src/render.ts
+++ b/site/packages/effects-kit/src/render.ts
@@ -367,6 +367,30 @@ export function textRoom(x: number, frameWidth: number): number {
* A line already inside the room is handed back exactly as written, so the
* spacing an author typed survives everywhere it fits.
*/
+/**
+ * Break one token across lines, at the last character that still fits.
+ *
+ * Walks code points rather than code units, so a break never lands inside an
+ * emoji or a surrogate pair. A single character wider than the room is emitted
+ * on its own — there is nothing narrower to fall back to, and the alternative
+ * is a loop with no end.
+ */
+function breakToken(word: string, room: number, measure: (line: string) => number): string[] {
+ const parts: string[] = [];
+ let line = "";
+ for (const ch of Array.from(word)) {
+ const cand = line + ch;
+ if (line && measure(cand) > room) {
+ parts.push(line);
+ line = ch;
+ } else {
+ line = cand;
+ }
+ }
+ if (line) parts.push(line);
+ return parts;
+}
+
export function wrapTextToRoom(
text: string,
room: number,
@@ -380,6 +404,17 @@ export function wrapTextToRoom(
const lines: string[] = [];
let line = "";
for (const w of words) {
+ // A token with nowhere to break that is still too wide — a URL, a long
+ // compound, a hashtag. Every surface draws these lines as they are
+ // given, so a line left over the room is a line that runs off the frame
+ // in the preview and in the rendered file alike.
+ if (measure(w) > room) {
+ if (line) lines.push(line);
+ const parts = breakToken(w, room, measure);
+ lines.push(...parts.slice(0, -1));
+ line = parts[parts.length - 1] ?? "";
+ continue;
+ }
const cand = line ? `${line} ${w}` : w;
if (line && measure(cand) > room) {
lines.push(line);
diff --git a/site/packages/effects-kit/src/textFit.test.ts b/site/packages/effects-kit/src/textFit.test.ts
index d86449e2..cda7ad58 100644
--- a/site/packages/effects-kit/src/textFit.test.ts
+++ b/site/packages/effects-kit/src/textFit.test.ts
@@ -42,12 +42,44 @@ describe("wrapTextToRoom", () => {
expect(wrapTextToRoom("one\ntwo", 1000, measure)).toBe("one\ntwo");
});
- test("a word wider than the room stands alone rather than being cut", () => {
+ test("a word wider than the room is broken inside", () => {
+ // Every surface draws these lines as they are given and none of them
+ // reflow, so a word left whole here runs off the frame in the preview and
+ // in the rendered file alike.
expect(wrapTextToRoom("a supercalifragilistic b", 60, measure)).toBe(
- "a\nsupercalifragilistic\nb"
+ "a\nsuperc\nalifra\ngilist\nic b"
);
});
+ test("no line comes back wider than the room", () => {
+ // The guarantee the caption box leans on: it draws with `max-content` and
+ // no wrapping, so anything over the room is pixels outside the frame.
+ const room = 60;
+ for (const text of [
+ "https://example.test/a/very/long/path/that/never/breaks",
+ "#oneenormoushashtagwithnospaces",
+ "short then aterriblylongcompoundword after",
+ "🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂",
+ ]) {
+ for (const line of wrapTextToRoom(text, room, measure).split("\n")) {
+ expect(measure(line)).toBeLessThanOrEqual(room);
+ }
+ }
+ });
+
+ test("a break never lands inside an emoji", () => {
+ // Code points, not code units: half a surrogate pair draws as a tofu box.
+ for (const line of wrapTextToRoom("🙂🙂🙂🙂", 20, measure).split("\n")) {
+ expect(line).toBe(Array.from(line).join(""));
+ expect(line.includes("\uFFFD")).toBe(false);
+ }
+ });
+
+ test("a single character wider than the room stands alone", () => {
+ // Nothing narrower to fall back to; the alternative is a loop with no end.
+ expect(wrapTextToRoom("abc", 5, measure)).toBe("a\nb\nc");
+ });
+
test("no room to speak of leaves the text alone", () => {
expect(wrapTextToRoom("keep me", 0, measure)).toBe("keep me");
});