Sync snapshot (2026-09-02) (#181)
- Docs: DOCX rendering and pagination fidelity (page borders, VML pictures, header/footer images, cross-page tables, anchored floats, character-unit indents, CJK typography) - Docs: more reliable PDF export for huge documents; AI rewrites keep the replaced content's formatting - Sheets: merge workbooks; Excel-standard keyboard shortcuts; PDF export with header/footer variants and pictures - Sheets: rendering, conditional formatting, chart and formula fidelity fixes; right-to-left sheet support - Slides: WordArt text warp, improved text layout and font substitution; AI table edits keep formatting Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
93b8938c45
commit
9f971edafa
@@ -125,6 +125,8 @@ function charAdvanceEm(code: number): number {
|
||||
// substitutes a CJK font where these draw full-width. Over-estimating only widens
|
||||
// a gap; under-estimating makes bullet glyphs overlap the text they precede.
|
||||
if ((code >= 0x25a0 && code <= 0x25ff) || code === 0x203b) return 1.0
|
||||
// Enclosed alphanumerics (① … ⑸ … ⓩ): same ambiguous-width fallback story as above
|
||||
if (code >= 0x2460 && code <= 0x24ff) return 1.0
|
||||
// narrow characters
|
||||
if ("iIlj.,:;'!|".includes(String.fromCharCode(code))) return 0.28
|
||||
if (' ftr'.includes(String.fromCharCode(code))) return 0.32
|
||||
|
||||
@@ -215,6 +215,8 @@ export interface TextLine {
|
||||
paraStart?: boolean
|
||||
/** Trailing whitespace swallowed when wrapping (the editor re-adds a space when joining lines; hard breaks/CJK wrapping don't set it) */
|
||||
trailingSpace?: boolean
|
||||
/** The exact swallowed whitespace (spaces / U+3000 / tabs); absent on stored decks → a single space */
|
||||
trailingText?: string
|
||||
/** The line ends with an <a:br/> soft break; value = the sentinel run's model index (the editor round-trips soft breaks with it) */
|
||||
softBreakAfter?: number
|
||||
/** Paragraph horizontal alignment (editor display) */
|
||||
@@ -256,6 +258,8 @@ export interface RenderTextLayout {
|
||||
vert?: 'eaVert' | 'vert' | 'vert270' | 'wordArtVert'
|
||||
/** WordArt text extrusion: glyphs get offset copies in this color behind them (px) */
|
||||
extrusion?: { color: string; dx: number; dy: number }
|
||||
/** WordArt envelope warp: the renderer bends glyphs along the preset's curves */
|
||||
txWarp?: { prst: string; adj?: Record<string, number> }
|
||||
}
|
||||
|
||||
/** Connector/line endpoint arrow description (for rendering, sizes converted to px). */
|
||||
|
||||
@@ -49,6 +49,10 @@ interface Token {
|
||||
breakable: boolean
|
||||
/** Whether it is whitespace (trailing whitespace may be swallowed when wrapping) */
|
||||
isSpace: boolean
|
||||
/** Tab character: width comes from the paragraph's tab stops, not the font */
|
||||
isTab?: boolean
|
||||
/** Layout-computed width (px) overriding the font measurement (tab advance) */
|
||||
wOverride?: number
|
||||
/** Forced line break (<a:br/> soft-break sentinel "\n") */
|
||||
isBreak?: boolean
|
||||
/** Source model run index (index into Paragraph.runs) */
|
||||
@@ -120,6 +124,7 @@ function substituteKerning(tok: Token, metrics: FontMetricsProvider): Token {
|
||||
|
||||
/** Token width = font advance width + letter spacing × char count (matches canvas letterSpacing: appended after each char) */
|
||||
function tokenWidth(tok: Token, metrics: FontMetricsProvider): number {
|
||||
if (tok.wOverride != null) return tok.wOverride
|
||||
const w = metrics.measure(tok.text, tok.style)
|
||||
return tok.ls ? w + tok.ls * [...tok.text].length : w
|
||||
}
|
||||
@@ -381,9 +386,13 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
if (ch === '\n' || ch === '\v') {
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: '\n', breakable: true, isSpace: false, isBreak: true })
|
||||
} else if (ch === ' ' || ch === '\t') {
|
||||
} else if (ch === ' ' || ch === ' ') {
|
||||
// U+3000 ideographic space wraps like whitespace (PowerPoint swallows it at line ends)
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: ch, breakable: true, isSpace: true })
|
||||
} else if (ch === '\t') {
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: ch, breakable: true, isSpace: true, isTab: true })
|
||||
} else if (ch === ' ') {
|
||||
// NBSP: glues neighbors into one token but draws/measures at plain-space
|
||||
// width (fonts like Carlito have no U+00A0 glyph → the missing-glyph
|
||||
@@ -392,6 +401,11 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
} else if (isWideChar(cp)) {
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: ch, breakable: true, isSpace: false })
|
||||
} else if (BREAK_AFTER_DASH.has(cp) && buf) {
|
||||
// Hyphen/dash after word characters is a break-after opportunity (PowerPoint
|
||||
// wraps "Cloud–Edge" as "Cloud–" + "Edge", never mid-word)
|
||||
buf += ch
|
||||
flushWord()
|
||||
} else {
|
||||
buf += ch
|
||||
}
|
||||
@@ -499,6 +513,9 @@ const KINSOKU_NO_END = new Set('([{$([{$〈《「『【〔〝「£¥£
|
||||
const kinsokuNoStart = (t: Token) => KINSOKU_NO_START.has(t.text)
|
||||
const kinsokuNoEnd = (t: Token) => KINSOKU_NO_END.has(t.text)
|
||||
|
||||
/** Dashes that allow a break after them (U+2011 non-breaking hyphen intentionally absent). */
|
||||
const BREAK_AFTER_DASH = new Set([0x2d, 0x2010, 0x2012, 0x2013, 0x2014])
|
||||
|
||||
/** Southeast Asian scripts without spaces (Thai/Burmese/Khmer/Lao); word boundaries need ICU dictionary segmentation. */
|
||||
const SEA_RE = /[-က-႟ក-]/
|
||||
const WORD_SEG: Intl.Segmenter | null =
|
||||
@@ -599,6 +616,8 @@ interface LaidLine {
|
||||
leadAbove?: number
|
||||
/** Trailing whitespace was swallowed when wrapping (tells the editor to re-add a space when joining lines) */
|
||||
trailingSpace?: boolean
|
||||
/** The exact swallowed whitespace (may be U+3000 / tabs; the editor re-adds it verbatim) */
|
||||
trailingText?: string
|
||||
/** The line ends at a soft-break sentinel; value = the sentinel run's model index */
|
||||
softBreakAfter?: number
|
||||
}
|
||||
@@ -612,9 +631,13 @@ function layoutParagraph(
|
||||
scale: number,
|
||||
fontScale: number,
|
||||
lnSpcRed = 0,
|
||||
/** First-line width reduction (px): a bullet glyph overflowing the hanging indent
|
||||
* pushes the first line's text start right, so it must wrap that much earlier. */
|
||||
/** First-line x shift (px): the first-line indent, or the push of a bullet glyph
|
||||
* overflowing the hanging indent. Shrinks (negative: widens) the first line's wrap
|
||||
* budget and offsets its tab cursor by the same amount. */
|
||||
firstLineShrinkPx = 0,
|
||||
/** Tab geometry: stops + default grid in px measured from the text-frame left inset,
|
||||
* originPx = the line's x offset (marL) in that same space. */
|
||||
tabs?: { stopsPx: number[]; defaultPx: number; originPx: number },
|
||||
): LaidLine[] {
|
||||
const tokens = applyBidi(tokenizeParagraph(p, scale, fontScale), p.rtl).map(
|
||||
(tok, logicalOrder) => ({
|
||||
@@ -627,10 +650,11 @@ function layoutParagraph(
|
||||
let curW = 0
|
||||
|
||||
const pushLine = (toks: Token[]) => {
|
||||
// Strip trailing whitespace (recorded: the editor re-adds a space when joining wrapped lines back into a paragraph)
|
||||
// Strip trailing whitespace (recorded verbatim: the editor re-adds it when joining wrapped lines back into a paragraph)
|
||||
let trailingSpace = false
|
||||
let trailingText = ''
|
||||
while (toks.length && toks[toks.length - 1]!.isSpace) {
|
||||
toks.pop()
|
||||
trailingText = toks.pop()!.text + trailingText
|
||||
trailingSpace = true
|
||||
}
|
||||
if (!toks.length) {
|
||||
@@ -679,12 +703,15 @@ function layoutParagraph(
|
||||
ascent: m.ascent,
|
||||
descent: m.descent,
|
||||
singleH: PPT_SINGLE * st.fontSizePx,
|
||||
...(trailingSpace ? { trailingSpace } : {}),
|
||||
...(trailingSpace ? { trailingSpace, trailingText } : {}),
|
||||
})
|
||||
return
|
||||
}
|
||||
const line = buildLine(toks, metrics, p, scale, lnSpcRed)
|
||||
if (trailingSpace) line.trailingSpace = true
|
||||
if (trailingSpace) {
|
||||
line.trailingSpace = true
|
||||
line.trailingText = trailingText
|
||||
}
|
||||
lines.push(line)
|
||||
}
|
||||
|
||||
@@ -700,6 +727,15 @@ function layoutParagraph(
|
||||
continue
|
||||
}
|
||||
endedWithBreak = false
|
||||
if (tok.isTab && tabs) {
|
||||
// Tab advances to the next stop past the cursor; past the last stop, to the default grid
|
||||
const cursor = tabs.originPx + (lines.length === 0 ? firstLineShrinkPx : 0) + curW
|
||||
const stop = tabs.stopsPx.find((s) => s > cursor + 0.5)
|
||||
tok.wOverride = Math.max(
|
||||
(stop ?? (Math.floor(cursor / tabs.defaultPx) + 1) * tabs.defaultPx) - cursor,
|
||||
0,
|
||||
)
|
||||
}
|
||||
const w = tokenWidth(tok, metrics)
|
||||
// The first line loses firstLineShrinkPx to the overflowing bullet glyph; evaluated
|
||||
// lazily because the soft wrap right below can end line 0 for this same token
|
||||
@@ -994,13 +1030,29 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
// offset that clips the top. Equal to contentHeight under single spacing.
|
||||
const extraH = availHeight - (result.inkBottom ?? result.contentHeight)
|
||||
const dy = anchor === 'middle' ? extraH / 2 : anchor === 'bottom' ? extraH : 0
|
||||
const lines = dy
|
||||
? result.lines.map((ln) => ({
|
||||
...ln,
|
||||
top: ln.top + dy,
|
||||
runs: ln.runs.map((r) => ({ ...r, baselineY: r.baselineY + dy })),
|
||||
}))
|
||||
: result.lines
|
||||
// bodyPr anchorCtr="1": the whole text block (widest-line bounding box) centers
|
||||
// horizontally in the text area; paragraph alignment stays relative within the block.
|
||||
let dxCtr = 0
|
||||
if (body.anchorCtr && numCol === 1) {
|
||||
let minX = Infinity
|
||||
let maxX = -Infinity
|
||||
for (const ln of result.lines) {
|
||||
for (const r of ln.runs) {
|
||||
if (!r.text) continue
|
||||
if (r.x < minX) minX = r.x
|
||||
if (r.x + r.widthPx > maxX) maxX = r.x + r.widthPx
|
||||
}
|
||||
}
|
||||
if (maxX > minX) dxCtr = (availWidth - (maxX - minX)) / 2 - minX
|
||||
}
|
||||
const lines =
|
||||
dy || dxCtr
|
||||
? result.lines.map((ln) => ({
|
||||
...ln,
|
||||
top: ln.top + dy,
|
||||
runs: ln.runs.map((r) => ({ ...r, x: r.x + dxCtr, baselineY: r.baselineY + dy })),
|
||||
}))
|
||||
: result.lines
|
||||
|
||||
// WordArt text extrusion: depth projected by the body camera tilt (same screen basis
|
||||
// as the scene3d shape pipeline: depth leans (sin lon, -sin lat·cos lon) per unit)
|
||||
@@ -1028,6 +1080,7 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
wrap,
|
||||
autofit: body.autofit ?? 'none',
|
||||
...(extrusion ? { extrusion } : {}),
|
||||
...(body.txWarp ? { txWarp: body.txWarp } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1424,7 +1477,8 @@ function layoutAll(
|
||||
if (symText) bulletText = symText
|
||||
|
||||
const textX = marLPx
|
||||
const avail = Math.max(availWidth - textX, 1)
|
||||
const marRPx = emuToPx(p.marR ?? 0, scale)
|
||||
const avail = Math.max(availWidth - textX - marRPx, 1)
|
||||
// RTL base direction mirrors the paragraph box (PowerPoint semantics, probe-measured):
|
||||
// marL/indent measure from the RIGHT edge, the bullet glyph hangs on the right, and the
|
||||
// default alignment is right. Explicit algn values stay physical (l = left edge).
|
||||
@@ -1451,16 +1505,14 @@ function layoutAll(
|
||||
// Glyph wider than the hanging indent: the first line's text start shifts right by
|
||||
// this much, so the first line must also wrap that much earlier
|
||||
const bulletOverflowPx = hasBullet ? Math.max(bulletX + bulletW - textX, 0) : 0
|
||||
const laid = layoutParagraph(
|
||||
p,
|
||||
avail,
|
||||
wrap,
|
||||
metrics,
|
||||
scale,
|
||||
fontScale,
|
||||
lnSpcRed,
|
||||
bulletOverflowPx,
|
||||
)
|
||||
// The first line's x shift: bullet-overflow push, or the first-line indent itself —
|
||||
// it consumes (negative: adds) that much of the first line's wrap budget
|
||||
const firstLineDx = hasBullet ? bulletOverflowPx : indentPx
|
||||
const laid = layoutParagraph(p, avail, wrap, metrics, scale, fontScale, lnSpcRed, firstLineDx, {
|
||||
stopsPx: (p.tabStops ?? []).map((t) => emuToPx(t.pos, scale)),
|
||||
defaultPx: Math.max(emuToPx(p.defTabSz ?? 914400, scale), 1),
|
||||
originPx: textX,
|
||||
})
|
||||
// Space before/after: spcPts is absolute pt; spcPct is a percentage of the paragraph's single line height (100 = one line).
|
||||
// PowerPoint ignores space-before on a text frame's FIRST paragraph in every body
|
||||
// (0047 measured: defaultTextStyle spcBef 50% shifted no first line), not just table cells.
|
||||
@@ -1557,11 +1609,12 @@ function layoutAll(
|
||||
const rightEdge = availWidth - textX - firstShift - bulletShift
|
||||
const mirrorSpread = mirror && align === 'justify' && lineRuns !== ln.runs
|
||||
if (mirror) {
|
||||
// Mirrored: marL measures from the right edge, marR from the left
|
||||
dx =
|
||||
mirrorSpread || align === 'left'
|
||||
? 0
|
||||
? marRPx
|
||||
: align === 'center'
|
||||
? (rightEdge - lineWidth) / 2
|
||||
? marRPx + (rightEdge - marRPx - lineWidth) / 2
|
||||
: rightEdge - lineWidth
|
||||
}
|
||||
const runs = lineRuns.map((r) => ({
|
||||
@@ -1600,6 +1653,7 @@ function layoutAll(
|
||||
...(ln.leadAbove ? { leadAbove: ln.leadAbove } : {}),
|
||||
paraStart: li === 0,
|
||||
...(ln.trailingSpace ? { trailingSpace: true } : {}),
|
||||
...(ln.trailingText ? { trailingText: ln.trailingText } : {}),
|
||||
...(ln.softBreakAfter != null ? { softBreakAfter: ln.softBreakAfter } : {}),
|
||||
...(p.align ? { align: p.align } : {}),
|
||||
...(paraBaseRtl(p) ? { rtl: true } : {}),
|
||||
|
||||
@@ -1498,3 +1498,130 @@ describe('kerning threshold (rPr kern)', () => {
|
||||
expect(l2.lines[0]!.runs[0]!.kerningOff).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('paragraph marR / tab stops / dash & wide-space wrapping', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const lay = (paragraphs: Paragraph[], boxWidthPx = 200) =>
|
||||
layoutText({ body: body({ paragraphs }), boxWidthPx, boxHeightPx: 400, metrics: m, vp })
|
||||
|
||||
it('marR narrows the wrap width from the right', () => {
|
||||
const p: Paragraph = { runs: [{ text: '一二三四五六七八', fontSize: 18 }] } // 8 × 24px
|
||||
expect(lay([p]).lines).toHaveLength(1)
|
||||
// marR = 100px → avail 100px → 4 chars per line
|
||||
const wrapped = lay([{ ...p, marR: 952500 }]).lines
|
||||
expect(wrapped).toHaveLength(2)
|
||||
expect(wrapped[0]!.runs[0]!.x).toBe(0) // LTR: marR only narrows, the left edge stays
|
||||
})
|
||||
|
||||
it('tab advances to the next custom stop, then to the default grid', () => {
|
||||
const runs = [
|
||||
{ text: 'ab', fontSize: 18 },
|
||||
{ text: '\t', fontSize: 18 },
|
||||
{ text: 'cd', fontSize: 18 },
|
||||
]
|
||||
const stopped = lay([{ runs, tabStops: [{ pos: 952500 }] }], 400).lines[0]!
|
||||
expect(stopped.runs.find((r) => r.text === 'cd')!.x).toBeCloseTo(100, 0)
|
||||
// No custom stop → the 1" (96px) default grid
|
||||
const grid = lay([{ runs }], 400).lines[0]!
|
||||
expect(grid.runs.find((r) => r.text === 'cd')!.x).toBeCloseTo(96, 0)
|
||||
})
|
||||
|
||||
it('breaks after a dash instead of mid-word', () => {
|
||||
const p: Paragraph = { runs: [{ text: 'Cloud–Edge', fontSize: 18 }] }
|
||||
const lines = lay([p], 100).lines
|
||||
expect(lines).toHaveLength(2)
|
||||
expect(lines[0]!.runs.map((r) => r.text).join('')).toBe('Cloud–')
|
||||
expect(lines[1]!.runs.map((r) => r.text).join('')).toBe('Edge')
|
||||
})
|
||||
|
||||
it('ideographic spaces at a wrap point are swallowed, recorded verbatim, and never indent the next line', () => {
|
||||
const p: Paragraph = { runs: [{ text: '一二三四 五六七八', fontSize: 18 }] }
|
||||
const lines = lay([p], 100).lines
|
||||
expect(lines).toHaveLength(2)
|
||||
expect(lines[0]!.trailingSpace).toBe(true)
|
||||
expect(lines[0]!.trailingText).toBe(' ')
|
||||
expect(lines[1]!.runs[0]!.x).toBe(0)
|
||||
expect(lines[1]!.runs.map((r) => r.text).join('')).toBe('五六七八')
|
||||
})
|
||||
})
|
||||
|
||||
it('first-line tabs land on inset-absolute stops despite a first-line indent', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const lines = layoutText({
|
||||
body: body({
|
||||
paragraphs: [
|
||||
{
|
||||
runs: [
|
||||
{ text: 'ab', fontSize: 18 },
|
||||
{ text: '\t', fontSize: 18 },
|
||||
{ text: 'cd', fontSize: 18 },
|
||||
],
|
||||
indent: 190500, // 20px first-line indent
|
||||
tabStops: [{ pos: 952500 }], // 100px from the inset edge
|
||||
},
|
||||
],
|
||||
}),
|
||||
boxWidthPx: 400,
|
||||
boxHeightPx: 100,
|
||||
metrics: m,
|
||||
vp,
|
||||
}).lines
|
||||
// drawn x = line-local x + first-line shift; the stop is absolute, so 'cd' sits at 100px
|
||||
expect(lines[0]!.runs.find((r) => r.text === 'cd')!.x).toBeCloseTo(100, 0)
|
||||
})
|
||||
|
||||
it('a positive first-line indent consumes first-line wrap budget (hanging widens it)', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const lay = (indent?: number) =>
|
||||
layoutText({
|
||||
body: body({
|
||||
paragraphs: [
|
||||
{
|
||||
runs: [{ text: '一二三四五六七八', fontSize: 18 }],
|
||||
...(indent != null ? { indent } : {}),
|
||||
},
|
||||
],
|
||||
}),
|
||||
boxWidthPx: 100,
|
||||
boxHeightPx: 400,
|
||||
metrics: m,
|
||||
vp,
|
||||
}).lines
|
||||
// 24px/char, 100px box: no indent → 4 chars on line 1
|
||||
expect(lay()[0]!.runs.length).toBe(4)
|
||||
// 20px first-line indent → 80px budget → 3 chars, drawn starting at 20px
|
||||
const indented = lay(190500)
|
||||
expect(indented[0]!.runs.length).toBe(3)
|
||||
expect(indented[0]!.runs[0]!.x).toBeCloseTo(20, 0)
|
||||
expect(indented[1]!.runs[0]!.x).toBe(0)
|
||||
})
|
||||
|
||||
it('anchorCtr centers the text block bounding box horizontally (alignment stays within)', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const lay = (anchorCtr?: boolean) =>
|
||||
layoutText({
|
||||
body: body({
|
||||
paragraphs: [
|
||||
{ runs: [{ text: '一二三四', fontSize: 18 }] }, // 96px
|
||||
{ runs: [{ text: '一二', fontSize: 18 }] }, // 48px
|
||||
],
|
||||
...(anchorCtr ? { anchorCtr } : {}),
|
||||
}),
|
||||
boxWidthPx: 200,
|
||||
boxHeightPx: 200,
|
||||
metrics: m,
|
||||
vp,
|
||||
}).lines
|
||||
expect(lay()[0]!.runs[0]!.x).toBe(0)
|
||||
const centered = lay(true)
|
||||
// block width 96 → dx = (200-96)/2 = 52; the narrower line keeps its left alignment within the block
|
||||
expect(centered[0]!.runs[0]!.x).toBeCloseTo(52, 0)
|
||||
expect(centered[1]!.runs[0]!.x).toBeCloseTo(52, 0)
|
||||
})
|
||||
|
||||
it('enclosed alphanumerics measure full-width in the missing-glyph fallback', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const style = { fontFamily: 'Century Gothic', fontSizePx: 100, bold: false, italic: false }
|
||||
// ⑤ falls back to a CJK font at draw time; measuring it narrow overlaps the next run
|
||||
expect(m.measure('⑤', style)).toBeCloseTo(100, 0)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user