feat: a table keeps the cell margins its file gave it
The cell padding was a constant in three places that disagreed with each other, so a narrow column lost 0.38cm it did not have: a bibliography marker column of 0.71cm wrapped "[2] " onto three lines and every entry grew with it, costing a fixture a whole page. The table now carries its own margins (w:tblCellMar, direct or from the table style; fo:padding on the ODF cell style) and both exports write them back. A side within a twentieth of a millimetre of the default is the default, or Word's 108 twips would tag every table.
This commit is contained in:
1 parent
e4b679ad8b
commit
db0fe71cdb
11 files changed
+174
-16
No files matched your search
+25
-1
@@ -19,6 +19,7 @@ import { languageFromOdf, NO_LANGUAGE, type DocumentLanguage } from '../storage/
|
||||
import type { HfDoc } from '../storage/headerFooter';
|
||||
import { applyUniformRunFont, type OdtImportResult } from './odt';
|
||||
import { deobfuscateOdttf, type EmbeddedFont } from '../fonts/embeddedFonts';
|
||||
import { cellPaddingAttr, type CellPadding } from '../editor/extensions/tableCellPadding';
|
||||
|
||||
// .docx → TipTap JSON, inverting export/docx.ts. Editor-expressible OOXML becomes its
|
||||
// native node/mark/attr; values matching the editor's defaults are suppressed so round
|
||||
@@ -1439,6 +1440,26 @@ function convertTable(tbl: Element, ctx: Ctx): Node | null {
|
||||
}
|
||||
}
|
||||
|
||||
// w:tblCellMar → [top, right, bottom, left] cm, direct before the table style's.
|
||||
// A side nobody declares stays the editor default (cellPaddingAttr fills it in).
|
||||
function docxCellPadding(tbl: Element, styleId: string | null, ctx: Ctx): CellPadding | null {
|
||||
const direct = fc(fc(tbl, 'tblPr'), 'tblCellMar');
|
||||
const fromStyle = ctx.styles.tableCellMar(styleId);
|
||||
const side = (...names: string[]): number | null => {
|
||||
for (const el of [direct, fromStyle]) {
|
||||
for (const name of names) {
|
||||
const s = el && fc(el, name);
|
||||
if (!s) continue;
|
||||
if (s.getAttributeNS(W, 'type') === 'nil') return 0;
|
||||
const w = intAttr(s, W, 'w');
|
||||
if (w != null) return twipToCm(w);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return cellPaddingAttr([side('top'), side('right', 'end'), side('bottom'), side('left', 'start')]);
|
||||
}
|
||||
|
||||
function buildTable(tbl: Element, ctx: Ctx): Node | null {
|
||||
const grid = fc(tbl, 'tblGrid');
|
||||
const weights = grid ? fcAll(grid, 'gridCol').map((g) => Math.max(1, intAttr(g, W, 'w') ?? 1)) : null;
|
||||
@@ -1507,8 +1528,11 @@ function buildTable(tbl: Element, ctx: Ctx): Node | null {
|
||||
// Word's w:tblStyle: only the name comes back — the look rides on the cell attrs, and
|
||||
// the editor re-derives the regions from its own registry (refreshTableStyles).
|
||||
const styleEl = fc(fc(tbl, 'tblPr'), 'tblStyle');
|
||||
const named = ctx.styles.tableStyleName(styleEl ? wVal(styleEl) : null);
|
||||
const styleId = styleEl ? wVal(styleEl) : null;
|
||||
const named = ctx.styles.tableStyleName(styleId);
|
||||
const attrs: Record<string, unknown> = { ...(tableMargins(tbl, useWeights, ctx) ?? {}) };
|
||||
const pad = docxCellPadding(tbl, styleId, ctx);
|
||||
if (pad) attrs.cellPadding = pad;
|
||||
if (named) {
|
||||
attrs.tableStyle = named;
|
||||
const look = docxTableLook(fc(tbl, 'tblPr'));
|
||||
|
||||
@@ -172,6 +172,7 @@ export class DocxStyles {
|
||||
private ownKeepNext = new Map<string, boolean>(); // style's own w:pPr/w:keepNext
|
||||
private ownKeepLines = new Map<string, boolean>(); // style's own w:pPr/w:keepLines
|
||||
private ownTabs = new Map<string, TabStop[]>(); // style's own w:pPr/w:tabs
|
||||
private ownCellMar = new Map<string, Element>(); // table style's own w:tblPr/w:tblCellMar
|
||||
private defaultsWidow: boolean | null = null; // docDefaults w:pPrDefault/w:widowControl
|
||||
private numToAbstract = new Map<string, string>();
|
||||
private abstractLevels = new Map<string, Map<number, LevelDef>>();
|
||||
@@ -246,6 +247,8 @@ export class DocxStyles {
|
||||
const left = parseInt(ind.getAttributeNS(W, 'left') ?? ind.getAttributeNS(W, 'start') ?? '', 10);
|
||||
if (Number.isFinite(left)) this.ownIndentTwip.set(id, left);
|
||||
}
|
||||
const cellMar = firstChild(style, 'tblPr') && firstChild(firstChild(style, 'tblPr')!, 'tblCellMar');
|
||||
if (cellMar) this.ownCellMar.set(id, cellMar);
|
||||
const kind = style.getAttributeNS(W, 'type');
|
||||
if (kind === 'paragraph' || kind === 'character' || kind === 'table') {
|
||||
const nameEl = firstChild(style, 'name');
|
||||
@@ -447,6 +450,14 @@ export class DocxStyles {
|
||||
return { ...this.defaultsSpacing, ...this.styleSpacing(pStyleId ?? this.defaultParaStyle) };
|
||||
}
|
||||
|
||||
// The table style's own w:tblCellMar along the w:basedOn chain (leaf wins). The
|
||||
// element itself, so the caller reads whichever sides it declares.
|
||||
tableCellMar(styleId: string | null | undefined, seen = new Set<string>()): Element | null {
|
||||
if (!styleId || seen.has(styleId)) return null;
|
||||
seen.add(styleId);
|
||||
return this.ownCellMar.get(styleId) ?? this.tableCellMar(this.basedOn.get(styleId) ?? null, seen);
|
||||
}
|
||||
|
||||
level(numId: number, ilvl: number): LevelDef {
|
||||
const abs = this.numToAbstract.get(String(numId));
|
||||
if (!abs) return {};
|
||||
|
||||
@@ -17,6 +17,7 @@ import { pageDimsCm, type PageFormat } from '../storage/pageFormat';
|
||||
import { languageFromOdf, NO_LANGUAGE, type DocumentLanguage } from '../storage/documentLanguage';
|
||||
import type { HfDoc } from '../storage/headerFooter';
|
||||
import type { EmbeddedFont } from '../fonts/embeddedFonts';
|
||||
import { cellPaddingAttr, type CellPadding } from '../editor/extensions/tableCellPadding';
|
||||
|
||||
// .odt → TipTap JSON, inverting export/odt.ts. Editor-expressible content becomes its
|
||||
// native node/mark/attr; values matching the editor's defaults are suppressed so round
|
||||
@@ -1536,6 +1537,7 @@ function convertTable(el: Element, ctx: Ctx): Node | null {
|
||||
const weights = columnWeights(el, ctx.resolver);
|
||||
|
||||
const rows: Node[] = [];
|
||||
let cellPad: CellPadding | null | undefined;
|
||||
const addRow = (rowEl: Element, header: boolean) => {
|
||||
const cells: Node[] = [];
|
||||
let colIndex = 0;
|
||||
@@ -1551,6 +1553,9 @@ function convertTable(el: Element, ctx: Ctx): Node | null {
|
||||
const colspan = parseInt(cellEl.getAttributeNS(NS.table, 'number-columns-spanned') ?? '1', 10) || 1;
|
||||
const rowspan = parseInt(cellEl.getAttributeNS(NS.table, 'number-rows-spanned') ?? '1', 10) || 1;
|
||||
const cellStyleName = cellEl.getAttributeNS(NS.table, 'style-name');
|
||||
// ODF has no table-level cell margin — it sits on each cell's style, and every
|
||||
// producer writes it uniformly, so the first cell's stands for the table.
|
||||
if (cellPad === undefined) cellPad = cellPaddingAttr(ctx.resolver.cellPadding(cellStyleName));
|
||||
const rawBg = ctx.resolver.cellBackgroundColor(cellStyleName);
|
||||
const backgroundColor = rawBg ? normalizeColor(rawBg) ?? rawBg : null;
|
||||
// Per-side borders; an undeclared side means no border in ODF → 'none'.
|
||||
@@ -1603,6 +1608,7 @@ function convertTable(el: Element, ctx: Ctx): Node | null {
|
||||
// the regions from the registry (refreshTableStyles).
|
||||
const named = ctx.resolver.namedAncestor(el.getAttributeNS(NS.table, 'style-name'), 'table');
|
||||
const attrs: Record<string, unknown> = { ...(tableMargins(el, ctx) ?? {}) };
|
||||
if (cellPad) attrs.cellPadding = cellPad;
|
||||
if (named) {
|
||||
attrs.tableStyle = displayStyleName(named);
|
||||
// Which conditional areas the table opts into (ODF's table template attributes).
|
||||
|
||||
@@ -371,6 +371,16 @@ export class StyleResolver {
|
||||
return out;
|
||||
}
|
||||
|
||||
// A cell style's padding (the cell margin) as [top, right, bottom, left] cm, per-side
|
||||
// overriding the fo:padding shorthand. A side nobody declares comes back null.
|
||||
cellPadding(styleName: string | null): (number | null)[] {
|
||||
if (!styleName) return [null, null, null, null];
|
||||
const misc = this.merged('table-cell', styleName).misc;
|
||||
const all = misc['fo:padding'];
|
||||
return (['top', 'right', 'bottom', 'left'] as const)
|
||||
.map((side) => lengthToCm(misc[`fo:padding-${side}`] ?? all));
|
||||
}
|
||||
|
||||
listStyle(name: string | null): Element | null {
|
||||
return name ? this.listStyles.get(name) ?? null : null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user