Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/list-marker-rise-line-spacing.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions docs/site/content/docx-fidelity.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
`<w:numbering xmlns:w="${W}"><w:abstractNum w:abstractNumId="1"><w:lvl w:ilvl="0">` +
`<w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val="o"/><w:lvlJc w:val="left"/>` +
`<w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr>` +
`<w:rPr><w:rFonts w:ascii="Taller" w:hAnsi="Taller"/></w:rPr></w:lvl></w:abstractNum>` +
`<w:num w:numId="1"><w:abstractNumId w:val="1"/></w:num></w:numbering>`,
'/word/numbering.xml'
);

function firstLines(spacing: string, numbered: boolean) {
const numPr = numbered ? '<w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr>' : '';
const body = partOf(
`<w:document xmlns:w="${W}"><w:body><w:p><w:pPr>${numPr}${spacing}</w:pPr>` +
`<w:r><w:t>${'one two three four five six seven eight nine ten '.repeat(8)}</w:t></w:r>` +
`</w:p></w:body></w:document>`,
'/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 = `<w:spacing w:line="${line}" w:lineRule="auto"/>`;
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('<w:spacing w:line="240" w:lineRule="auto"/>', true);
expect(lines[0]!.box.height).toBe(TEXT_ASCENT + TEXT_DESCENT + MARKER_RISE);
});

test('exact spacing keeps its authored height', () => {
const lines = firstLines('<w:spacing w:line="400" w:lineRule="exact"/>', true);
expect(lines[0]!.box.height).toBe(20);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, readonly [number, number]> = {
'': [11.52, 2.268],
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/layout/paragraph-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
36 changes: 32 additions & 4 deletions packages/core/src/layout/positioned-run-spacing.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<typeof applyLineSpacing>,
spacing: ParagraphLineSpacing,
rise: number
): ReturnType<typeof applyLineSpacing> {
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<typeof withPositionedRunSpacing>[3],
measurer: Pick<TextMeasurer, 'lineMetrics'>
measurer: Pick<TextMeasurer, 'lineMetrics'>,
options: { readonly unscaledRise?: number } = {}
): ReturnType<typeof applyLineSpacing> {
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);
}
Loading