Skip to content

Commit 873e669

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 070b964 commit 873e669

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
@@ -1396,6 +1396,12 @@ Overlap is judged on the text a viewer can see: a part of a text block that an
13961396
overflow clip, a `clip-path` or a gradient `mask-image` (on the text or an ancestor)
13971397
keeps from painting does not count.
13981398

1399+
`text_word_split` (warning) reports Korean text where a line break lands inside
1400+
a word: between two Hangul syllables, or between a Hangul particle and the Latin
1401+
word or number it is attached to (`HyperFrames|로`). Chrome's default
1402+
`word-break: normal` allows those breaks; set `word-break: keep-all` on the text
1403+
or a parent so Korean lines break only at spaces.
1404+
13991405
#### Mark intentional layout choices
14001406

14011407
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
@@ -863,6 +863,98 @@
863863
return issues;
864864
}
865865

866+
// Korean puts spaces between words, yet under the default `word-break: normal`
867+
// Chrome may end a line between any two Hangul syllables, or between a Hangul
868+
// particle and the Latin word or number it is attached to ("HyperFrames로"),
869+
// so a caption word wraps in half. `word-break: keep-all` keeps them together.
870+
// Hangul Jamo, Compatibility Jamo, Jamo Extended-A, Syllables, Jamo Extended-B.
871+
const HANGUL_CHAR = /[ᄀ-ᇿ㄰-㆏ꥠ-꥿가-힯ힰ-퟿]/;
872+
const WORD_CHAR = /[\p{L}\p{M}\p{N}]/u;
873+
const WORD_SPLIT_SCAN_LIMIT = 2000;
874+
875+
function joinsKoreanWord(left, right) {
876+
return (
877+
WORD_CHAR.test(left) &&
878+
WORD_CHAR.test(right) &&
879+
(HANGUL_CHAR.test(left) || HANGUL_CHAR.test(right))
880+
);
881+
}
882+
883+
function charRect(range, node, index) {
884+
range.setStart(node, index);
885+
range.setEnd(node, index + 1);
886+
const rect = range.getBoundingClientRect();
887+
return rect.width > 0.5 && rect.height > 0.5 ? rect : null;
888+
}
889+
890+
// Line boxes stack vertically: two adjacent characters whose centres sit more
891+
// than half a glyph apart are on different lines. A font-fallback fragment on
892+
// the same line shifts by a few px; a small caption tilt barely moves it.
893+
function onLaterLine(before, after) {
894+
const centre = (rect) => rect.top + rect.height / 2;
895+
return Math.abs(centre(after) - centre(before)) > Math.max(before.height, after.height) / 2;
896+
}
897+
898+
function wordAround(text, index) {
899+
let start = index;
900+
while (start > 0 && WORD_CHAR.test(text[start - 1])) start--;
901+
let end = index;
902+
while (end < text.length && WORD_CHAR.test(text[end])) end++;
903+
return `${text.slice(start, index)}|${text.slice(index, end)}`;
904+
}
905+
906+
/** Where a line break first cuts a Korean word in this text node ("줄바꿈되는|지"), or null. */
907+
function splitKoreanWord(node) {
908+
const text = (node.textContent || "").slice(0, WORD_SPLIT_SCAN_LIMIT);
909+
if (!HANGUL_CHAR.test(text)) return null;
910+
const range = document.createRange();
911+
try {
912+
range.selectNodeContents(node);
913+
// One client rect per line box: a node on a single line cannot split a word.
914+
const lines = Array.from(range.getClientRects()).filter(
915+
(rect) => rect.width > 0.5 && rect.height > 0.5,
916+
);
917+
if (lines.length < 2) return null;
918+
let previous = null;
919+
for (let index = 1; index < text.length; index++) {
920+
if (!joinsKoreanWord(text[index - 1], text[index])) continue;
921+
const before =
922+
previous && previous.index === index - 1
923+
? previous.rect
924+
: charRect(range, node, index - 1);
925+
const after = charRect(range, node, index);
926+
previous = { index, rect: after };
927+
if (before && after && onLaterLine(before, after)) return wordAround(text, index);
928+
}
929+
return null;
930+
} finally {
931+
range.detach();
932+
}
933+
}
934+
935+
function koreanWordSplitIssue(element, time) {
936+
for (const node of directTextNodes(element)) {
937+
const split = splitKoreanWord(node);
938+
if (!split) continue;
939+
const keepAll = getComputedStyle(element).wordBreak === "keep-all";
940+
return {
941+
code: "text_word_split",
942+
severity: "warning",
943+
time,
944+
selector: uniqueSelectorFor(element),
945+
text: textContentFor(element, true),
946+
message: `A line break cuts a Korean word in two ("${split}").`,
947+
rect:
948+
unionRects(textClientRects(element, true).map(toRect)) ||
949+
toRect(element.getBoundingClientRect()),
950+
fixHint: keepAll
951+
? "The word is wider than its line even with word-break: keep-all: widen the box or lower the font size."
952+
: "Set word-break: keep-all on the text or a parent so Korean lines break only at spaces.",
953+
};
954+
}
955+
return null;
956+
}
957+
866958
function isNowrapTextChild(child) {
867959
if (!isVisibleElement(child) || hasAllowOverflowFlag(child)) return false;
868960
if (getComputedStyle(child).whiteSpace !== "nowrap") return false;
@@ -2872,6 +2964,8 @@
28722964
const clipped = clippedTextIssue(element, time, tolerance);
28732965
if (clipped) issues.push(clipped);
28742966
issues.push(...textOverflowIssues(element, root, rootRect, time, tolerance, clipped));
2967+
const split = koreanWordSplitIssue(element, time);
2968+
if (split) issues.push(split);
28752969
const occluded = occludedTextIssue(element, time, proseCoverageFloor, matteResults);
28762970
if (occluded) {
28772971
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
@@ -4194,3 +4194,81 @@ function rect({ left, top, width, height }: RectInput): DOMRect {
41944194
},
41954195
} as DOMRect;
41964196
}
4197+
4198+
describe("layout-audit.browser Korean word splits", () => {
4199+
// One 60 px glyph per character; characters from `breakAt` on sit on a second line,
4200+
// and a range gets one client rect per line it spans, as in Chrome.
4201+
function auditKoreanCaption(
4202+
text: string,
4203+
breakAt: number,
4204+
style: Partial<CSSStyleDeclaration> = {},
4205+
): AuditIssue[] {
4206+
document.body.innerHTML = `<div id="root" data-composition-id="main" data-width="1080" data-height="1920"><p id="caption">${text}</p></div>`;
4207+
installGeometry(
4208+
{
4209+
root: rect({ left: 0, top: 0, width: 1080, height: 1920 }),
4210+
caption: rect({ left: 90, top: 600, width: 900, height: 144 }),
4211+
},
4212+
{ caption: style },
4213+
);
4214+
const glyph = (index: number) => {
4215+
const line = index < breakAt ? 0 : 1;
4216+
const column = line === 0 ? index : index - breakAt;
4217+
return rect({ left: 90 + column * 60, top: 600 + line * 72, width: 60, height: 60 });
4218+
};
4219+
vi.spyOn(document, "createRange").mockImplementation(() => {
4220+
let start = 0;
4221+
let end = 0;
4222+
const glyphs = () =>
4223+
Array.from({ length: Math.max(0, end - start) }, (_, i) => glyph(start + i));
4224+
const lineRects = () => {
4225+
const byLine = new Map<number, DOMRect[]>();
4226+
for (const item of glyphs()) byLine.set(item.top, [...(byLine.get(item.top) ?? []), item]);
4227+
return [...byLine.values()].map((line) => boundingTextRect(line) as DOMRect);
4228+
};
4229+
return {
4230+
selectNodeContents(node: Node) {
4231+
start = 0;
4232+
end = node.textContent?.length ?? 0;
4233+
},
4234+
setStart(_node: Node, offset: number) {
4235+
start = offset;
4236+
},
4237+
setEnd(_node: Node, offset: number) {
4238+
end = offset;
4239+
},
4240+
getClientRects: () => lineRects() as unknown as DOMRectList,
4241+
getBoundingClientRect: () =>
4242+
boundingTextRect(glyphs()) ?? rect({ left: 0, top: 0, width: 0, height: 0 }),
4243+
detach() {},
4244+
} as unknown as Range;
4245+
});
4246+
installAuditScript();
4247+
return runAudit().filter((issue) => issue.code === "text_word_split");
4248+
}
4249+
4250+
it("reports a Korean word that a line break cuts between syllables", () => {
4251+
const [issue] = auditKoreanCaption("자막이 줄바꿈되는지", 9);
4252+
expect(issue).toMatchObject({ selector: "#caption", text: "자막이 줄바꿈되는지" });
4253+
expect(issue?.message).toContain('"줄바꿈되는|지"');
4254+
expect(issue?.fixHint).toContain("word-break: keep-all");
4255+
});
4256+
4257+
it("reports a Korean particle cut from the Latin word it is attached to", () => {
4258+
const [issue] = auditKoreanCaption("HyperFrames로", 11);
4259+
expect(issue?.message).toContain('"HyperFrames|로"');
4260+
});
4261+
4262+
it("stays quiet when Korean lines break at spaces", () => {
4263+
expect(auditKoreanCaption("자막이 줄바꿈되는지", 4)).toEqual([]);
4264+
});
4265+
4266+
it("stays quiet for text without Hangul", () => {
4267+
expect(auditKoreanCaption("Launch faster", 3)).toEqual([]);
4268+
});
4269+
4270+
it("asks for room instead of keep-all when keep-all is already set", () => {
4271+
const [issue] = auditKoreanCaption("줄바꿈되는지", 5, { wordBreak: "keep-all" });
4272+
expect(issue?.fixHint).toContain("wider than its line");
4273+
});
4274+
});

‎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
@@ -2290,6 +2290,7 @@ const LAYOUT_ISSUE_CODES: readonly LayoutIssueCode[] = [
22902290
"canvas_covered",
22912291
"video_alpha_unread",
22922292
"text_not_painted",
2293+
"text_word_split",
22932294
"caption_zone_collision",
22942295
"frame_out_of_frame",
22952296
"escaped_container",

‎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
| "frame_out_of_frame"
2627
// Coordinate-frame findings — geometry computed in one frame, rendered in another.
@@ -210,6 +211,7 @@ const PERSISTENCE_TIERED_CODES: ReadonlySet<LayoutIssueCode> = new Set([
210211
"content_overlap",
211212
"visibility_escapes_segment",
212213
"text_occluded",
214+
"text_word_split",
213215
"escaped_container",
214216
"panel_out_of_canvas",
215217
"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": 2
77
},
88
"hyperframes": {
9-
"hash": "17e33f68596da873",
9+
"hash": "b02ec8f95a878732",
1010
"files": 78
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)