Skip to content

Commit 17cffcf

Browse files
wonjangcloud9claude
andcommitted
feat(check): warn when a line break cuts a Korean word in two
Korean puts spaces between words, but under the default `word-break: normal` Chrome may end a line between any two Hangul syllables, so a wrapped caption renders "줄바꿈되는 / 지" and `check` passed it with 0 layout issues. The layout audit now reports `text_word_split` (warning) when two adjacent characters of a Korean word sit on different lines: two Hangul syllables, or a Hangul particle and the Latin word or number it is attached to. The fix hint asks for `word-break: keep-all`, or for more room when keep-all is already set. Persistence-tiered like the other text findings, so a one-sample transient demotes to info. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0f008f9 commit 17cffcf

8 files changed

Lines changed: 238 additions & 1 deletion

File tree

‎docs/packages/cli.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,12 @@ Overlap is judged on the text a viewer can see: a part of a text block that an
14091409
overflow clip, a `clip-path` or a gradient `mask-image` (on the text or an ancestor)
14101410
keeps from painting does not count.
14111411

1412+
`text_word_split` (warning) reports Korean text where a line break lands inside
1413+
a word: between two Hangul syllables, or between a Hangul particle and the Latin
1414+
word or number it is attached to (`HyperFrames|로`). Chrome's default
1415+
`word-break: normal` allows those breaks; set `word-break: keep-all` on the text
1416+
or a parent so Korean lines break only at spaces.
1417+
14121418
#### Mark intentional layout choices
14131419

14141420
When the audit is wrong because the layout is deliberate, say so in the HTML and

‎packages/cli/src/commands/layout-audit.browser.js‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,98 @@
867867
return issues;
868868
}
869869

870+
// Korean puts spaces between words, yet under the default `word-break: normal`
871+
// Chrome may end a line between any two Hangul syllables, or between a Hangul
872+
// particle and the Latin word or number it is attached to ("HyperFrames로"),
873+
// so a caption word wraps in half. `word-break: keep-all` keeps them together.
874+
// Hangul Jamo, Compatibility Jamo, Jamo Extended-A, Syllables, Jamo Extended-B.
875+
const HANGUL_CHAR = /[ᄀ-ᇿ㄰-㆏ꥠ-꥿가-힯ힰ-퟿]/;
876+
const WORD_CHAR = /[\p{L}\p{M}\p{N}]/u;
877+
const WORD_SPLIT_SCAN_LIMIT = 2000;
878+
879+
function joinsKoreanWord(left, right) {
880+
return (
881+
WORD_CHAR.test(left) &&
882+
WORD_CHAR.test(right) &&
883+
(HANGUL_CHAR.test(left) || HANGUL_CHAR.test(right))
884+
);
885+
}
886+
887+
function charRect(range, node, index) {
888+
range.setStart(node, index);
889+
range.setEnd(node, index + 1);
890+
const rect = range.getBoundingClientRect();
891+
return rect.width > 0.5 && rect.height > 0.5 ? rect : null;
892+
}
893+
894+
// Line boxes stack vertically: two adjacent characters whose centres sit more
895+
// than half a glyph apart are on different lines. A font-fallback fragment on
896+
// the same line shifts by a few px; a small caption tilt barely moves it.
897+
function onLaterLine(before, after) {
898+
const centre = (rect) => rect.top + rect.height / 2;
899+
return Math.abs(centre(after) - centre(before)) > Math.max(before.height, after.height) / 2;
900+
}
901+
902+
function wordAround(text, index) {
903+
let start = index;
904+
while (start > 0 && WORD_CHAR.test(text[start - 1])) start--;
905+
let end = index;
906+
while (end < text.length && WORD_CHAR.test(text[end])) end++;
907+
return `${text.slice(start, index)}|${text.slice(index, end)}`;
908+
}
909+
910+
/** Where a line break first cuts a Korean word in this text node ("줄바꿈되는|지"), or null. */
911+
function splitKoreanWord(node) {
912+
const text = (node.textContent || "").slice(0, WORD_SPLIT_SCAN_LIMIT);
913+
if (!HANGUL_CHAR.test(text)) return null;
914+
const range = document.createRange();
915+
try {
916+
range.selectNodeContents(node);
917+
// One client rect per line box: a node on a single line cannot split a word.
918+
const lines = Array.from(range.getClientRects()).filter(
919+
(rect) => rect.width > 0.5 && rect.height > 0.5,
920+
);
921+
if (lines.length < 2) return null;
922+
let previous = null;
923+
for (let index = 1; index < text.length; index++) {
924+
if (!joinsKoreanWord(text[index - 1], text[index])) continue;
925+
const before =
926+
previous && previous.index === index - 1
927+
? previous.rect
928+
: charRect(range, node, index - 1);
929+
const after = charRect(range, node, index);
930+
previous = { index, rect: after };
931+
if (before && after && onLaterLine(before, after)) return wordAround(text, index);
932+
}
933+
return null;
934+
} finally {
935+
range.detach();
936+
}
937+
}
938+
939+
function koreanWordSplitIssue(element, time) {
940+
for (const node of directTextNodes(element)) {
941+
const split = splitKoreanWord(node);
942+
if (!split) continue;
943+
const keepAll = getComputedStyle(element).wordBreak === "keep-all";
944+
return {
945+
code: "text_word_split",
946+
severity: "warning",
947+
time,
948+
selector: uniqueSelectorFor(element),
949+
text: textContentFor(element, true),
950+
message: `A line break cuts a Korean word in two ("${split}").`,
951+
rect:
952+
unionRects(textClientRects(element, true).map(toRect)) ||
953+
toRect(element.getBoundingClientRect()),
954+
fixHint: keepAll
955+
? "The word is wider than its line even with word-break: keep-all: widen the box or lower the font size."
956+
: "Set word-break: keep-all on the text or a parent so Korean lines break only at spaces.",
957+
};
958+
}
959+
return null;
960+
}
961+
870962
function isNowrapTextChild(child) {
871963
if (!isVisibleElement(child) || hasAllowOverflowFlag(child)) return false;
872964
if (getComputedStyle(child).whiteSpace !== "nowrap") return false;
@@ -2904,6 +2996,8 @@
29042996
const clipped = clippedTextIssue(element, time, tolerance);
29052997
if (clipped) issues.push(clipped);
29062998
issues.push(...textOverflowIssues(element, root, rootRect, time, tolerance, clipped));
2999+
const split = koreanWordSplitIssue(element, time);
3000+
if (split) issues.push(split);
29073001
const occluded = occludedTextIssue(element, time, proseCoverageFloor, matteResults);
29083002
if (occluded) {
29093003
const lifetime = clipLifetime(element);

‎packages/cli/src/commands/layout-audit.browser.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,3 +4226,81 @@ function rect({ left, top, width, height }: RectInput): DOMRect {
42264226
},
42274227
} as DOMRect;
42284228
}
4229+
4230+
describe("layout-audit.browser Korean word splits", () => {
4231+
// One 60 px glyph per character; characters from `breakAt` on sit on a second line,
4232+
// and a range gets one client rect per line it spans, as in Chrome.
4233+
function auditKoreanCaption(
4234+
text: string,
4235+
breakAt: number,
4236+
style: Partial<CSSStyleDeclaration> = {},
4237+
): AuditIssue[] {
4238+
document.body.innerHTML = `<div id="root" data-composition-id="main" data-width="1080" data-height="1920"><p id="caption">${text}</p></div>`;
4239+
installGeometry(
4240+
{
4241+
root: rect({ left: 0, top: 0, width: 1080, height: 1920 }),
4242+
caption: rect({ left: 90, top: 600, width: 900, height: 144 }),
4243+
},
4244+
{ caption: style },
4245+
);
4246+
const glyph = (index: number) => {
4247+
const line = index < breakAt ? 0 : 1;
4248+
const column = line === 0 ? index : index - breakAt;
4249+
return rect({ left: 90 + column * 60, top: 600 + line * 72, width: 60, height: 60 });
4250+
};
4251+
vi.spyOn(document, "createRange").mockImplementation(() => {
4252+
let start = 0;
4253+
let end = 0;
4254+
const glyphs = () =>
4255+
Array.from({ length: Math.max(0, end - start) }, (_, i) => glyph(start + i));
4256+
const lineRects = () => {
4257+
const byLine = new Map<number, DOMRect[]>();
4258+
for (const item of glyphs()) byLine.set(item.top, [...(byLine.get(item.top) ?? []), item]);
4259+
return [...byLine.values()].map((line) => boundingTextRect(line) as DOMRect);
4260+
};
4261+
return {
4262+
selectNodeContents(node: Node) {
4263+
start = 0;
4264+
end = node.textContent?.length ?? 0;
4265+
},
4266+
setStart(_node: Node, offset: number) {
4267+
start = offset;
4268+
},
4269+
setEnd(_node: Node, offset: number) {
4270+
end = offset;
4271+
},
4272+
getClientRects: () => lineRects() as unknown as DOMRectList,
4273+
getBoundingClientRect: () =>
4274+
boundingTextRect(glyphs()) ?? rect({ left: 0, top: 0, width: 0, height: 0 }),
4275+
detach() {},
4276+
} as unknown as Range;
4277+
});
4278+
installAuditScript();
4279+
return runAudit().filter((issue) => issue.code === "text_word_split");
4280+
}
4281+
4282+
it("reports a Korean word that a line break cuts between syllables", () => {
4283+
const [issue] = auditKoreanCaption("자막이 줄바꿈되는지", 9);
4284+
expect(issue).toMatchObject({ selector: "#caption", text: "자막이 줄바꿈되는지" });
4285+
expect(issue?.message).toContain('"줄바꿈되는|지"');
4286+
expect(issue?.fixHint).toContain("word-break: keep-all");
4287+
});
4288+
4289+
it("reports a Korean particle cut from the Latin word it is attached to", () => {
4290+
const [issue] = auditKoreanCaption("HyperFrames로", 11);
4291+
expect(issue?.message).toContain('"HyperFrames|로"');
4292+
});
4293+
4294+
it("stays quiet when Korean lines break at spaces", () => {
4295+
expect(auditKoreanCaption("자막이 줄바꿈되는지", 4)).toEqual([]);
4296+
});
4297+
4298+
it("stays quiet for text without Hangul", () => {
4299+
expect(auditKoreanCaption("Launch faster", 3)).toEqual([]);
4300+
});
4301+
4302+
it("asks for room instead of keep-all when keep-all is already set", () => {
4303+
const [issue] = auditKoreanCaption("줄바꿈되는지", 5, { wordBreak: "keep-all" });
4304+
expect(issue?.fixHint).toContain("wider than its line");
4305+
});
4306+
});

‎packages/cli/src/commands/layout-audit.chromium.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,61 @@ describe.runIf(executablePath)("layout audit in Chromium", () => {
316316
expect(codes).toContain("text_not_painted");
317317
});
318318

319+
// Chrome may end a line between any two Hangul syllables unless word-break is keep-all.
320+
it.each([
321+
{
322+
name: "a Korean word wrapped between syllables",
323+
text: "가나다라마바사아",
324+
css: "",
325+
split: true,
326+
},
327+
{
328+
name: "that word under a caption tilt",
329+
text: "가나다라마바사아",
330+
css: "transform:rotate(-4deg)",
331+
split: true,
332+
},
333+
{
334+
name: "a Latin word with its Korean particle wrapped apart",
335+
text: "HyperFrames로",
336+
css: "width:6.9em;font-family:Arial",
337+
split: true,
338+
},
339+
{
340+
name: "Korean wrapped at spaces with keep-all",
341+
text: "가나다 라마바 사아자",
342+
css: "word-break:keep-all",
343+
split: false,
344+
},
345+
{
346+
name: "a Latin face with Korean fallback, wrapped at spaces",
347+
text: "HyperFrames로 만든 자막",
348+
css: "width:8.5em;word-break:keep-all;font-family:Arial",
349+
split: false,
350+
},
351+
{
352+
name: "Korean on one line",
353+
text: "가나다라마바사아",
354+
css: "width:auto;white-space:nowrap",
355+
split: false,
356+
},
357+
{
358+
name: "Korean lines split by <br>",
359+
text: "가나다<br>라마바",
360+
css: "width:auto",
361+
split: false,
362+
},
363+
{ name: "wrapped English", text: "Launch faster ship sooner", css: "", split: false },
364+
{ name: "wrapped Japanese", text: "日本語の字幕はどこでも改行できます", css: "", split: false },
365+
])("text_word_split: $name", async ({ text, css, split }) => {
366+
const codes = await auditCodes(
367+
stage(
368+
`<p style="position:absolute;left:100px;top:100px;width:3.5em;margin:0;font:700 80px/1.2 sans-serif;${css}">${text}</p>`,
369+
),
370+
);
371+
expect(codes.includes("text_word_split")).toBe(split);
372+
});
373+
319374
it.each([
320375
{ name: "padding", css: "padding-top:150px" },
321376
{ name: "text indentation", css: "text-indent:1000px;white-space:nowrap" },

‎packages/cli/src/utils/checkBrowser.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,7 @@ const LAYOUT_ISSUE_CODES: readonly LayoutIssueCode[] = [
22912291
"canvas_covered",
22922292
"video_alpha_unread",
22932293
"text_not_painted",
2294+
"text_word_split",
22942295
"caption_zone_collision",
22952296
"outside_safe_zone",
22962297
"frame_out_of_frame",

‎packages/cli/src/utils/layoutAudit.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type LayoutIssueCode =
2121
| "canvas_covered"
2222
| "video_alpha_unread"
2323
| "text_not_painted"
24+
| "text_word_split"
2425
| "caption_zone_collision"
2526
| "outside_safe_zone"
2627
| "frame_out_of_frame"
@@ -211,6 +212,7 @@ const PERSISTENCE_TIERED_CODES: ReadonlySet<LayoutIssueCode> = new Set([
211212
"content_overlap",
212213
"visibility_escapes_segment",
213214
"text_occluded",
215+
"text_word_split",
214216
"escaped_container",
215217
"panel_out_of_canvas",
216218
"canvas_content_at_edge",

‎skills-manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"files": 3
77
},
88
"hyperframes": {
9-
"hash": "6be2c44bf6fc87de",
9+
"hash": "2b03bf094d346c8d",
1010
"files": 80
1111
},
1212
"hyperframes-cli": {

‎skills/hyperframes/references/captions.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tl.set(group, { opacity: 0, visibility: "hidden" }, end);
3030
## Fit and place
3131

3232
- Size each group to fit: `window.__hyperframes.fitTextFontSize(text, { fontFamily, fontWeight, maxWidth })` returns `{ fontSize }`. Defaults: `maxWidth` 1600 landscape, 900 portrait; base 78, minimum 42, step 2. When a word scales up for emphasis, divide `maxWidth` by that scale.
33+
- Korean caption text takes `word-break: keep-all`. Chrome otherwise wraps between any two syllables and cuts a word in half; `check` reports it as `text_word_split`.
3334
- A full-width absolute container, centred with text alignment. Never `left: 50%; transform: translateX(-50%)`, which clips at the frame edges.
3435
- `overflow: visible`, not `hidden`, which cuts off scaled words and glows.
3536
- Landscape: 80–120 px from the bottom. Portrait: about 600–700 px from the bottom. Never over the speaker's face; inside the 80% title-safe box.

0 commit comments

Comments
 (0)