feat(slides): flag horizontal text overflow in the layout audit (#154)

The audit only compared laid-out content height against the box, so a line
wider than the box (wrap=false, or a single unbreakable token wider than
the available width) passed silently and then overlapped whatever sits
next to it. collectEntries now also records each text node's widest
laid-out line (run.x already includes marL/indent/alignment, so the
content-area comparison is exact), and auditSlideLayout reports a
'Text overflow (width)' issue past the same tolerance, prompting the AI
to widen the box, shrink the font, or enable wrapping.

Split out from #129 per maintainer feedback.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
konghaojie-k2
2026-08-26 11:34:09 +08:00
committed by GitHub
co-authored by Claude
parent 72a4a42db1
commit 9792915878
2 changed files with 48 additions and 1 deletions
+41 -1
View File
@@ -25,6 +25,8 @@ interface AuditEntry {
preview: string
/** Pixels by which the text content height exceeds the box height (only meaningful when >0) */
overflowPx: number
/** Pixels by which the widest laid-out line exceeds the box inner width (wrap=false lines, over-wide tokens) */
overflowXPx: number
}
const PREVIEW_MAX = 18
@@ -46,6 +48,7 @@ function collectEntries(nodes: RenderNode[]): AuditEntry[] {
let hasText = false
let preview = ''
let overflowPx = 0
let overflowXPx = 0
if (n.type === 'shape' || n.type === 'text') {
const sn = n as ShapeRenderNode
preview = textPreview(sn)
@@ -53,13 +56,38 @@ function collectEntries(nodes: RenderNode[]): AuditEntry[] {
if (sn.text && hasText) {
const inner = h - sn.text.insets.t - sn.text.insets.b
overflowPx = Math.round(sn.text.contentHeight - inner)
const innerW = w - sn.text.insets.l - sn.text.insets.r
// Laid-out right edge of the widest line (run.x already includes marL/indent/alignment,
// so the content-area comparison below is exact). Catches wrap=false lines and
// unbreakable tokens wider than the box -- the height-only check misses both, and
// the text then overlaps whatever sits next to it.
const widest = sn.text.lines.reduce(
(acc, ln) =>
Math.max(
acc,
ln.runs.reduce((m, r) => Math.max(m, r.x + r.widthPx), -Infinity),
),
-Infinity,
)
overflowXPx = Math.round(Number.isFinite(widest) ? widest - innerW : 0)
}
} else if (n.type === 'group') {
// If any child in the group has text, treat it as text content for overlap detection
hasText = groupHasText(n as GroupRenderNode)
preview = '(group)'
}
out.push({ id: n.sourceId, type: n.type, x, y, w, h, hasText, preview, overflowPx })
out.push({
id: n.sourceId,
type: n.type,
x,
y,
w,
h,
hasText,
preview,
overflowPx,
overflowXPx,
})
}
return out
}
@@ -127,6 +155,18 @@ export function auditSlideLayout(slide: RenderSlide): string[] {
}
}
// 2b. Horizontal overflow: a line wider than the box (wrap=false, or a single token wider
// than the available width) spills over the box and overlaps neighbors. PowerPoint renders
// nowrap overflow as-is, so this is an audit-only signal that lets the AI widen the box,
// shrink the font, or enable wrapping instead of leaving invisible overlap.
for (const e of entries) {
if (e.overflowXPx > OVERFLOW_TOLERANCE_PX) {
issues.push(
`Text overflow (width): ${label(e)} content exceeds the box width by ${e.overflowXPx}px (widen the box, reduce the font size, or turn on wrapping)`,
)
}
}
// 3. Pairwise overlap of content elements
const content = entries.filter((e) => isContent(e) && e.w * e.h < W * H * BACKGROUND_AREA_RATIO)
for (let i = 0; i < content.length; i++) {
+7
View File
@@ -190,6 +190,13 @@ describe('auditSlideLayout', () => {
expect(issues.some((s) => s.includes('Text overflow'))).toBe(true)
})
it('detects text overflow (laid-out line wider than the box inner width)', () => {
// Box width 200, insets 8 left/right -> inner width 184; run x=8 width 20*12=240 -> exceeds by 64px
const slide = slideOf([textNode('t1', box(80, 60, 200, 100), 'x'.repeat(20))])
const issues = auditSlideLayout(slide)
expect(issues.some((s) => s.includes('Text overflow (width)') && s.includes('64px'))).toBe(true)
})
it('decoration layers and large background blocks are excluded', () => {
const bg = textNode('bg', box(0, 0, 1280, 720), 'Background watermark')
const deco = { ...textNode('deco', box(100, 100, 400, 200), 'Decoration'), decoration: true }