diff --git a/.changeset/list-marker-rise-line-spacing.md b/.changeset/list-marker-rise-line-spacing.md new file mode 100644 index 0000000000..b642634368 --- /dev/null +++ b/.changeset/list-marker-rise-line-spacing.md @@ -0,0 +1,5 @@ +--- +'@docx-editor.dev/core': patch +--- + +A list marker whose face rises above the text grows its line once under auto line spacing, as in Word, instead of by the line spacing multiple. Lists with large bullets at 1.5 or double spacing no longer push later content down. diff --git a/docs/site/content/docx-fidelity.mdx b/docs/site/content/docx-fidelity.mdx index b3e7ee935c..edf1dd8b0f 100644 --- a/docs/site/content/docx-fidelity.mdx +++ b/docs/site/content/docx-fidelity.mdx @@ -79,6 +79,7 @@ The matrix reports editing, rendering, and round-trip support separately. A feat - Lists that share one `w:abstractNum` count on one number per level. - A `w:startOverride` restarts that number once, at the first item of its list, on the overridden level only. Other lists continue from the restarted number. When the same `w:lvlOverride` replaces the level, its `w:start` takes precedence. - In a centered or right-aligned paragraph, the marker moves with the first line's text. In a table cell, the marker takes the table style's run formatting. +- A marker in a face that rises above the text grows the first line by the difference. Automatic line spacing scales only the text, not that difference. - Header and footer lists share one count that the body and notes do not join. The count runs over each section's referenced parts, in order. Hidden parts still count, and each explicit reference counts again. An inherited part reuses the earlier section's numbers. ### Tables diff --git a/packages/core/src/layout/__tests__/list-marker-line-spacing.test.ts b/packages/core/src/layout/__tests__/list-marker-line-spacing.test.ts new file mode 100644 index 0000000000..06ff9fbcd4 --- /dev/null +++ b/packages/core/src/layout/__tests__/list-marker-line-spacing.test.ts @@ -0,0 +1,86 @@ +// Auto line spacing scales a list line's text band, and the marker's extra ascent adds once. +// +// Measured against Word: a 22pt Symbol bullet on an 11pt Calibri list at 1.5 and at double +// spacing grows each line by the marker's ascent over the text's, and the multiple applies to +// the 11pt band alone (17.3.1.33 spacing; 17.18.48 ST_LineSpacingRule `auto`). + +import { describe, expect, test } from 'bun:test'; +import { readOoxmlPart, type OoxmlPart } from '@docx-editor.dev/core/store'; +import { layoutSemanticDocument } from '../semantic-layout.ts'; +import { buildNumberingIndex } from '../numbering-index.ts'; +import { paragraphFragmentsOf } from '../semantic-records.ts'; +import type { ResolvedRunStyle } from '../run-style.ts'; +import type { TextMeasurer } from '../semantic-records.ts'; + +const W = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'; +const TEXT_ASCENT = 11; +const TEXT_DESCENT = 3; +const MARKER_RISE = 4; + +/** The paragraph face ascends 11 and descends 3; `Taller` ascends 4 further. */ +const measurer: TextMeasurer = { + measure: (text) => text.length * 6, + lineMetrics: (style: ResolvedRunStyle) => { + const ascent = style.fontFamily === 'Taller' ? TEXT_ASCENT + MARKER_RISE : TEXT_ASCENT; + return { height: ascent + TEXT_DESCENT, baseline: ascent }; + }, +}; + +function partOf(xml: string, name: string): OoxmlPart { + const part = readOoxmlPart(xml, { name, contentType: 'app/xml' }); + if (!part.ok) throw new Error(part.reason); + return part.part; +} + +const numbering = partOf( + `` + + `` + + `` + + `` + + ``, + '/word/numbering.xml' +); + +function firstLines(spacing: string, numbered: boolean) { + const numPr = numbered ? '' : ''; + const body = partOf( + `${numPr}${spacing}` + + `${'one two three four five six seven eight nine ten '.repeat(8)}` + + ``, + '/word/document.xml' + ); + const layout = layoutSemanticDocument(body, 1, { + measurer, + numberingIndex: buildNumberingIndex(numbering.root), + }); + return paragraphFragmentsOf(layout.pages[0]!)[0]!.lines; +} + +describe('a taller list marker under auto line spacing', () => { + for (const [line, multiple] of [ + ['360', 1.5], + ['480', 2], + ] as const) { + test(`at ${multiple}x the marker's rise adds once, unscaled`, () => { + const spacing = ``; + const listed = firstLines(spacing, true); + const plain = firstLines(spacing, false); + const band = (TEXT_ASCENT + TEXT_DESCENT) * multiple; + expect(plain[0]!.box.height).toBeCloseTo(band, 9); + expect(listed[0]!.box.height).toBeCloseTo(band + MARKER_RISE, 9); + // The baseline still clears the marker's ascent; later lines keep the plain height. + expect(listed[0]!.baseline).toBeCloseTo(TEXT_ASCENT + MARKER_RISE, 9); + expect(listed[1]!.box.height).toBeCloseTo(plain[1]!.box.height, 9); + }); + } + + test('single spacing still grows the line by the rise', () => { + const lines = firstLines('', true); + expect(lines[0]!.box.height).toBe(TEXT_ASCENT + TEXT_DESCENT + MARKER_RISE); + }); + + test('exact spacing keeps its authored height', () => { + const lines = firstLines('', true); + expect(lines[0]!.box.height).toBe(20); + }); +}); diff --git a/packages/core/src/layout/__tests__/list-marker-symbol-face.test.ts b/packages/core/src/layout/__tests__/list-marker-symbol-face.test.ts index 606c8f5e18..124c2020c5 100644 --- a/packages/core/src/layout/__tests__/list-marker-symbol-face.test.ts +++ b/packages/core/src/layout/__tests__/list-marker-symbol-face.test.ts @@ -27,7 +27,8 @@ const UNICODE_SMALL_SQUARE = '\u{25aa}'; /** * Real captured ascent/descent at 12 pt: the paragraph face, then the two symbol faces. * The document sets `w:spacing w:line="276"`, Word's 1.15 default, so a plain line is - * (11.520 + 2.268) x 1.15 = 15.856 and a Symbol-marked one (12.064 + 2.268) x 1.15 = 16.482. + * (11.520 + 2.268) x 1.15 = 15.856. A Symbol-marked one adds the marker's extra ascent once, + * unscaled: 15.856 + (12.064 - 11.520) = 16.400. */ const BANDS: Record = { '': [11.52, 2.268], @@ -157,7 +158,7 @@ describe('an admitted symbol face reaching a numbering marker', () => { // Symbol's taller ascent raises the baseline, and the box by the same amount. expect(plain!.lines[0]!.box.height).toBeCloseTo(15.856, 3); expect(symbol!.lines[0]!.baseline).toBeCloseTo(12.064, 3); - expect(symbol!.lines[0]!.box.height).toBeCloseTo(16.482, 3); + expect(symbol!.lines[0]!.box.height).toBeCloseTo(16.4, 3); // Wingdings ascends LESS than the paragraph face, so its line is untouched. expect(wingdings!.lines[0]!.baseline).toBeCloseTo(11.52, 3); expect(wingdings!.lines[0]!.box.height).toBeCloseTo(15.856, 3); diff --git a/packages/core/src/layout/paragraph-flow.ts b/packages/core/src/layout/paragraph-flow.ts index 539ba39f65..b983f0c7fa 100644 --- a/packages/core/src/layout/paragraph-flow.ts +++ b/packages/core/src/layout/paragraph-flow.ts @@ -820,11 +820,12 @@ export function breakParagraph( // text's alone. Only the paragraph's first line carries a marker. const firstLine = opensFirstLine(); if (firstLine && firstLineOffset !== 0) line.firstLineOffset = firstLineOffset; + let markerRise = 0; if (firstLine && markerAscent > line.baseline) { - const raised = markerAscent - line.baseline; + markerRise = markerAscent - line.baseline; line.baseline = markerAscent; - line.height += raised; - glyphBaseline += raised; + line.height += markerRise; + glyphBaseline += markerRise; } borders.closeLine(line); markSyntheticBoldSpans(line, measurer); @@ -843,7 +844,9 @@ export function breakParagraph( const equationBand = band?.equation ? band : undefined; const spacingBase = band?.height ?? naturalHeight; const bandBaseline = band?.baseline ?? line.baseline; - const spaced = spacedLine(lineSpacing, spacingBase, bandBaseline, line.spans, measurer); + const spaced = spacedLine(lineSpacing, spacingBase, bandBaseline, line.spans, measurer, { + unscaledRise: band ? 0 : markerRise, + }); if (!scalesTextBandOnly) line.baseline = spaced.baseline; else if (equationBand) line.baseline += spaced.baseline - bandBaseline; const floored = firstLine && lineSpacing.rule !== 'exact' ? markerBaselineFloor : 0; diff --git a/packages/core/src/layout/positioned-run-spacing.ts b/packages/core/src/layout/positioned-run-spacing.ts index c18b980abc..509f03203c 100644 --- a/packages/core/src/layout/positioned-run-spacing.ts +++ b/packages/core/src/layout/positioned-run-spacing.ts @@ -1,4 +1,5 @@ -// Auto multiple line spacing on a line that holds raised or lowered (`w:position`) text. +// Auto multiple line spacing on a line that holds raised or lowered (`w:position`) text, or a list +// marker whose face rises above the text. import { applyLineSpacing, type ParagraphLineSpacing } from './paragraph-style.ts'; import { isHeightlessWhitespace, lineBandText } from './pending-line.ts'; @@ -46,14 +47,41 @@ export function withPositionedRunSpacing( return { ...spaced, height: naturalHeight + band * (spacing.value / 240 - 1) }; } -/** {@link applyLineSpacing} for a laid-out line, with {@link withPositionedRunSpacing} applied. */ +/** + * Apply an auto multiple above single to a line that a list marker's taller face raised by + * `rise`: the multiple scales the text band, and the rise adds once. A 22pt bullet on 11pt text + * at 1.5 spacing grows the line by half of the 11pt band plus the bullet's extra ascent. + */ +function withUnscaledRise( + spaced: ReturnType, + spacing: ParagraphLineSpacing, + rise: number +): ReturnType { + if (!(rise > 0) || spacing.rule !== 'auto' || spacing.gridPitch !== undefined) return spaced; + const extra = rise * (spacing.value / 240 - 1); + if (!(extra > 0)) return spaced; + return { + ...spaced, + height: spaced.height - extra, + baseline: spacing.descentAnchored ? spaced.baseline - extra : spaced.baseline, + }; +} + +/** + * {@link applyLineSpacing} for a laid-out line, with {@link withPositionedRunSpacing} applied, + * or else the list marker's `unscaledRise`. + */ export function spacedLine( spacing: ParagraphLineSpacing, naturalHeight: number, naturalBaseline: number, spans: Parameters[3], - measurer: Pick + measurer: Pick, + options: { readonly unscaledRise?: number } = {} ): ReturnType { const spaced = applyLineSpacing(spacing, naturalHeight, naturalBaseline); - return withPositionedRunSpacing(spaced, spacing, naturalHeight, spans, measurer); + const positioned = withPositionedRunSpacing(spaced, spacing, naturalHeight, spans, measurer); + // A positioned line's band already leaves the marker out of the multiple. + if (positioned !== spaced) return positioned; + return withUnscaledRise(spaced, spacing, options.unscaledRise ?? 0); }