feat: a tab lands on the file's own grid, and can draw its leader
The default tab interval was fixed at 1.25cm, so a file's own w:defaultTabStop / style:tab-stop-distance was ignored and the DOCX export wrote 1.25 back over Word's 1.27. It is a document setting now. A stop can also carry the character it repeats across its gap. odf-kit has no leader option and mints one automatic style per option set, so the leader rides style:type behind a sentinel — matching on position and alignment alone gave every stop at 6cm the same leader, which LibreOffice drew back plain as day.
This commit is contained in:
1 parent
6accc7268f
commit
ea39dde58b
20 files changed
+319
-39
No files matched your search
@@ -243,4 +243,20 @@ describe.skipIf(!SOFFICE)('LibreOffice round-trip (needs soffice on PATH)', () =
|
||||
check('LO hf: header distance ≈0.8cm', Math.abs((hfRes.headerDistanceCm ?? 0) - 0.8) < 0.1, hfRes.headerDistanceCm);
|
||||
check('LO hf: footer distance ≈1.6cm', Math.abs((hfRes.footerDistanceCm ?? 0) - 1.6) < 0.1, hfRes.footerDistanceCm);
|
||||
});
|
||||
|
||||
it('survives a `soffice` re-save of the tab interval and a stop leader', { timeout: 180000 }, async () => {
|
||||
const tabDoc: N = { type: 'doc', content: [
|
||||
{ type: 'paragraph', attrs: { tabStops: '6l.;12r_' }, content: [T('Kapitel\t1\tS. 3')] },
|
||||
] };
|
||||
const bytes = await buildOdt(tabDoc, margins, 'portrait', undefined, undefined, 'A4', undefined, 1.27);
|
||||
mkdirSync('/tmp/lo-rt', { recursive: true });
|
||||
writeFileSync('/tmp/lo-rt/tab.odt', bytes);
|
||||
execSync('soffice --headless --convert-to odt --outdir /tmp/lo-rt/tabout /tmp/lo-rt/tab.odt', { stdio: 'pipe', timeout: 120000 });
|
||||
|
||||
const res = importOdt(new Uint8Array(readFileSync('/tmp/lo-rt/tabout/tab.odt')));
|
||||
check('LO tab: no warnings', res.warnings.length === 0, res.warnings);
|
||||
check('LO tab: interval ≈1.27cm', Math.abs((res.tabIntervalCm ?? 0) - 1.27) < 0.01, res.tabIntervalCm);
|
||||
const stops = (res.content.content ?? [])[0]?.attrs?.tabStops;
|
||||
check('LO tab: stops + leaders survive', stops === '6l.;12r_', stops);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
// The document's default tab interval: Word's w:defaultTabStop, ODF's
|
||||
// style:tab-stop-distance on the paragraph default-style.
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { unzipSync, strFromU8 } from 'fflate';
|
||||
import { buildOdt } from '../../src/lib/export/odt';
|
||||
import { importOdt } from '../../src/lib/import/odt';
|
||||
import { buildDocx } from '../../src/lib/export/docx';
|
||||
import { importDocx } from '../../src/lib/import/docx';
|
||||
import { DEFAULT_TAB_INTERVAL_CM, clampTabInterval } from '../../src/lib/storage/tabInterval';
|
||||
|
||||
const doc: any = { type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'a\tb' }] }] };
|
||||
const opts = [undefined, 'portrait', undefined, undefined, 'A4', undefined] as const;
|
||||
const odt = (cm?: number) => buildOdt(doc, ...(opts as any), cm);
|
||||
const docx = (cm?: number) => buildDocx(doc, ...(opts as any), cm);
|
||||
|
||||
describe('default tab interval', () => {
|
||||
it('rides an ODF default-style and comes back', async () => {
|
||||
const back = await importOdt(await odt(1.27));
|
||||
expect(back.tabIntervalCm).toBeCloseTo(1.27, 3);
|
||||
});
|
||||
|
||||
it('leaves LibreOffice’s own interval undeclared', async () => {
|
||||
const styles = strFromU8(unzipSync(await odt(DEFAULT_TAB_INTERVAL_CM))['styles.xml']);
|
||||
expect(styles).not.toContain('style:tab-stop-distance');
|
||||
expect((await importOdt(await odt(DEFAULT_TAB_INTERVAL_CM))).tabIntervalCm).toBe(null);
|
||||
});
|
||||
|
||||
it('rides a DOCX setting and comes back', async () => {
|
||||
// Word stores twips, so 1.27cm = 720tw is exact and 1.25cm rounds to 709tw.
|
||||
expect(importDocx(await docx(1.27)).tabIntervalCm).toBeCloseTo(1.27, 2);
|
||||
expect(importDocx(await docx(DEFAULT_TAB_INTERVAL_CM)).tabIntervalCm).toBeCloseTo(1.25, 2);
|
||||
});
|
||||
|
||||
it('clamps a nonsense value to something renderable', () => {
|
||||
expect(clampTabInterval(0)).toBe(0.05);
|
||||
expect(clampTabInterval(999)).toBe(10);
|
||||
expect(clampTabInterval(NaN)).toBe(DEFAULT_TAB_INTERVAL_CM);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
// A tab stop's leader: the character it repeats across its gap (Word w:tab w:leader,
|
||||
// ODF style:leader-text).
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { unzipSync, strFromU8 } from 'fflate';
|
||||
import { buildOdt } from '../../src/lib/export/odt';
|
||||
import { importOdt } from '../../src/lib/import/odt';
|
||||
import { buildDocx } from '../../src/lib/export/docx';
|
||||
import { importDocx } from '../../src/lib/import/docx';
|
||||
import { parseTabStops, formatTabStops } from '../../src/lib/editor/extensions/tabStops';
|
||||
|
||||
const doc: any = {
|
||||
type: 'doc',
|
||||
content: [
|
||||
{ type: 'paragraph', attrs: { tabStops: '6l.;12r_' }, content: [{ type: 'text', text: 'a\tb\tc' }] },
|
||||
{ type: 'paragraph', attrs: { tabStops: '4c' }, content: [{ type: 'text', text: 'x\ty' }] },
|
||||
],
|
||||
};
|
||||
|
||||
const stopsOfFirst = (d: any) => d.content?.[0]?.attrs?.tabStops ?? null;
|
||||
|
||||
describe('tab leaders', () => {
|
||||
it('round-trips through the attr form', () => {
|
||||
expect(parseTabStops('6l.;12r_')).toEqual([
|
||||
{ pos: 6, align: 'left', leader: '.' },
|
||||
{ pos: 12, align: 'right', leader: '_' },
|
||||
]);
|
||||
expect(formatTabStops(parseTabStops('6l.;12r_'))).toBe('6l.;12r_');
|
||||
// A character we can't render is dropped rather than approximated.
|
||||
expect(parseTabStops('6l~')[0].leader).toBe(null);
|
||||
});
|
||||
|
||||
it('rides ODF style:leader-text and comes back', async () => {
|
||||
const bytes = await buildOdt(doc);
|
||||
const content = strFromU8(unzipSync(bytes)['content.xml']);
|
||||
expect(content).toContain('style:leader-style="dotted" style:leader-text="."');
|
||||
expect(content).toContain('style:leader-style="solid" style:leader-text="_"');
|
||||
// The stop without one stays bare.
|
||||
expect(content).toContain('<style:tab-stop style:position="4cm" style:type="center"/>');
|
||||
expect(stopsOfFirst(importOdt(bytes).content)).toBe('6l.;12r_');
|
||||
});
|
||||
|
||||
it('keeps two stops at one position apart', async () => {
|
||||
// odf-kit mints one automatic style per option set, so without the leader riding
|
||||
// style:type both paragraphs would share a style — and with it one leader.
|
||||
const mixed: any = { type: 'doc', content: [
|
||||
{ type: 'paragraph', attrs: { tabStops: '6l.' }, content: [{ type: 'text', text: 'a\tb' }] },
|
||||
{ type: 'paragraph', attrs: { tabStops: '6l' }, content: [{ type: 'text', text: 'c\td' }] },
|
||||
] };
|
||||
const content = strFromU8(unzipSync(await buildOdt(mixed))['content.xml']);
|
||||
expect(content).toContain('<style:tab-stop style:position="6cm" style:type="left" style:leader-style="dotted" style:leader-text="."/>');
|
||||
expect(content).toContain('<style:tab-stop style:position="6cm" style:type="left"/>');
|
||||
});
|
||||
|
||||
it('rides a DOCX w:leader and comes back', async () => {
|
||||
const bytes = await buildDocx(doc);
|
||||
expect(strFromU8(unzipSync(bytes)['word/document.xml'])).toContain('w:leader="dot"');
|
||||
expect(stopsOfFirst(importDocx(bytes).content)).toBe('6l.;12r_');
|
||||
});
|
||||
});
|
||||
@@ -29,7 +29,9 @@ const stopsOf = (d: N, i: number) => d.content[i].attrs?.tabStops ?? null;
|
||||
describe('tab stops', () => {
|
||||
it('parses and formats the canonical attr form', () => {
|
||||
expect(parseTabStops('6c;12r;16d')).toEqual([
|
||||
{ pos: 6, align: 'center' }, { pos: 12, align: 'right' }, { pos: 16, align: 'decimal' },
|
||||
{ pos: 6, align: 'center', leader: null },
|
||||
{ pos: 12, align: 'right', leader: null },
|
||||
{ pos: 16, align: 'decimal', leader: null },
|
||||
]);
|
||||
// Sorted, deduped by position, rounded to 0.01cm; empty becomes null.
|
||||
expect(formatTabStops([{ pos: 12, align: 'right' }, { pos: 1.004, align: 'left' }])).toBe('1l;12r');
|
||||
|
||||
Reference in new issue
Block a user