feat: an ODF chart object is drawn too

<chart:chart> in the object's sub-document, read through the same picture as
the DOCX leg: series values by cell range out of its own local-table, colours
from the series style, bounds from the axis style.
This commit is contained in:
Steffen Becker committed 2026-08-08 23:54:59 +00:00
1 parent bf98014a3a
commit 2380ed699b
4 files changed
+210 -9

No files matched your search

+1 -1
View File
@@ -12,7 +12,7 @@ The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(
- **Default suppression** keeps round trips (editor → LibreOffice → editor) from accreting explicit attrs: values matching the editor's defaults (12pt body, `HEADING_STYLE_OVERRIDES` sizes/margins, **0 paragraph spacing**, list margin 0, Times New Roman/Liberation Serif, `#000000` text) are imported as `null`/no mark, with small tolerances for producer unit rounding (pt↔cm↔twips). This is why the editor's defaults must equal Word's/LibreOffice's (below) — an editor-only default is indistinguishable from a failed style resolution and silently lands in every imported document.
- **Mapping:** `text:h` → heading (level clamped to `MAX_HEADING_LEVEL`), per-depth `text:list-level-style-*` defs decide bullet vs ordered and `listStyleType` (reverse lookup `orderedTypeFromFormat`), tables handle covered cells / `number-columns-repeated` / header rows / colspans / cell shading (`StyleResolver.cellBackgroundColor` → `backgroundColor`) / cell borders (`StyleResolver.cellBorders` + `borderAttrFromOdf` → the per-side border attrs) / table margins (`tableMargins` reads the table style's `fo:margin-*`, `style:width`/`style:rel-width` and `table:align` against `Ctx.contentWidthCm` — so a table narrower than the text width stays narrow instead of being stretched; DOCX does the same from `w:tblInd` + the `w:tblGrid`/`w:tblW` width), `text:s`/`text:tab`/`text:line-break` become spaces/`\t`/`hardBreak`, `draw:frame`/`draw:image` → an `image` node (`convertFrame`: bytes read from the zip's `Pictures/` entry into a data-URI, `svg:width`/`-height` cm → px, `svg:title` → `alt`, `draw:transform` rotate → `rotation`; a non-`as-char` anchor or a graphic style's `style:wrap` → floating `wrap` mode via `StyleResolver.graphicProps` — an **explicit `as-char` anchor is always inline**: LibreOffice's named Graphics style carries a `style:wrap` that would otherwise float every inherited frame), text fields keep their stored value. **Text boxes / shapes** (`convertDrawElement`): `draw:frame`+`draw:text-box` → a `textBox` node (`convertTextBoxFrame`: height = `fo:min-height` else `svg:height`; fill/stroke read straight from `graphicProps`' `draw:fill(-color)`/`draw:stroke`/`svg:stroke-*`, values equal to the editor defaults suppressed, none → explicit `null`); `draw:rect`/`draw:ellipse`/`draw:custom-shape` with a rect/round-rectangle/ellipse preset → a `textBox` with the matching `shapeKind` (`convertShape`; other presets → "Unsupported shapes were removed"). A box is a block node but its frame sits inside a `text:p`, so converted boxes ride `Ctx.pendingBlocks` and `convertBlocks` flushes them after the anchor paragraph (dropping our own export's empty anchor); in cells/other boxes their blocks are unwrapped in place with a warning. The DOCX importer mirrors all of this (`convertWpsShape` for DrawingML `wps:wsp` — probed before the image `a:blip` path, `convertPict` for legacy VML; Word's `mc:AlternateContent` uses only the `mc:Choice` branch so the VML fallback can't double-import).
- **Image formats (`import/imageFormats.ts`, shared by both importers):** `<img>` only renders a fixed set of formats, so `imageDataUrl(bytes, path)` resolves the mime by extension **then** magic-number (mislabelled/extensionless entries still work) and returns `null` for anything unrenderable — the importer then skips it with a specific "format the browser can't display" warning instead of a broken `<img>`. A frame's multiple `<draw:image>` alternatives (ODF) and a Word `a:blip`'s `svgBlip` alternative are probed for the first *displayable* one, and `<draw:a>`-wrapped frames are unwrapped (the hyperlink dropped). **Non-web formats we can decode client-side** (currently **TIFF**) are converted to PNG up front by `convertUnsupportedImages(bytes)` — an async pass `applyImport` runs before the synchronous import, passing a `path → PNG data-URI` map (`ConvertedImages`) into `importOdt`/`importDocx` (default empty, so tests stay sync). The decoder (`utif2`) is a **dynamic `import()`** → its own lazy chunk, loaded only when a convertible image is actually present, so ordinary docs pay nothing. **SVM/WMF/EMF** have no JS decoder (SVM is StarOffice-proprietary) → skipped with the format warning; LibreOffice/WASM conversion was ruled out (no backend; WASM LO is ~300MB).
- **Charts (`import/chart.ts`, DOCX)**: `word/charts/chartN.xml` is parsed and **drawn as an SVG picture** of the frame's own size — series values from the `numCache` the producer left, colours from the named theme accent (`themeAccents`), title/axis titles/gridlines, and the value axis' own `c:scaling` bounds where the file fixes them. Bar (incl. stacked and `barDir="bar"`), line, area, scatter and pie; anything else keeps the placeholder. Read-only — the editor has no chart object, so a re-export carries the picture, not the chart.
- **Charts (`import/chart.ts`, DOCX)**: `word/charts/chartN.xml` is parsed and **drawn as an SVG picture** of the frame's own size — series values from the `numCache` the producer left, colours from the named theme accent (`themeAccents`), title/axis titles/gridlines, and the value axis' own `c:scaling` bounds where the file fixes them. Bar (incl. stacked and `barDir="bar"`), line, area, scatter and pie; anything else keeps the placeholder. Read-only — the editor has no chart object, so a re-export carries the picture, not the chart. **ODF** charts go through the same drawing (`odfChartDataUrl`, `convertChartFrame`): `<chart:chart>` in the `draw:object`'s sub-document, series values read out of its own `local-table` by cell range, colours from the series' `draw:fill-color`, bounds from the y-axis style's `chart:maximum`/`-minimum`.
- **Content controls** (`inlineChildren`, DOCX): a `w:sdt` inside a paragraph — Word wraps every citation and bibliography entry in one — is walked through to its `w:sdtContent`, or the runs inside it never reach the paragraph (the thesis fixture lost every `[7]`-style reference and broke its lines one page short).
- **Formulas** (`convertFormulaFrame`): a `<draw:frame>` whose `<draw:object>` resolves to a sub-document with a `math:math` root (or holds one inline) → a `formula` node. Our own `<annotation encoding="application/x-tex">` is preferred; LibreOffice strips it, so the MathML is parsed back to LaTeX instead. **`display` comes from the paragraph** (`aloneInParagraph`), not the object — LibreOffice writes `display="block"` on every formula it re-saves. DOCX: `m:oMath`/`m:oMathPara` sit beside the `w:r` runs in their own namespace, so `convertInline` probes them ahead of its `namespaceURI !== W` guard (that guard silently dropped every Word formula). See `docs/architecture/formulas.md`.
- **Graceful degradation** (collected as `warnings`, surfaced once): footnotes/comments dropped, hyperlinks flattened to text, nested tables flattened to paragraphs, non-`Pictures` drawings dropped, unreadable pictures skipped (each with a warning). Page size is normalized to A4. Floating/wrapped images are imported as left/right/top-bottom wrap (free x/y positions collapse to the nearest side; see `image.ts`).
+120 -2
View File
@@ -277,6 +277,124 @@ export function chartDataUrl(xml: string, widthPx: number, heightPx: number, acc
} catch { return null; }
if (doc.getElementsByTagName('parsererror').length) return null;
const chart = parseChart(doc, accents.length ? accents : DEFAULT_ACCENTS);
if (!chart) return null;
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(chartSvg(chart, w, h))}`;
return chart ? svgUrl(chart, w, h) : null;
}
const svgUrl = (chart: Chart, widthPx: number, heightPx: number) =>
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(
chartSvg(chart, Math.max(80, Math.round(widthPx)), Math.max(60, Math.round(heightPx))))}`;
// ---- ODF charts ------------------------------------------------------------
// An embedded ODF chart document (LibreOffice's "Object N/content.xml"): the same
// picture, read from <chart:chart> and the local table that holds its numbers.
const NS_CHART = 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0';
const NS_TABLE = 'urn:oasis:names:tc:opendocument:xmlns:table:1.0';
const NS_OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
const NS_STYLE = 'urn:oasis:names:tc:opendocument:xmlns:style:1.0';
const NS_DRAW = 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0';
const ODF_PLOTS: Record<string, Chart['kind']> = {
bar: 'bar', line: 'line', circle: 'pie', ring: 'pie', area: 'area', scatter: 'scatter',
};
// "local-table.$B$2:.$B$24" → the column index and the row span it covers (1-based,
// as the sheet counts them). Only single-column ranges occur in a chart's own table.
function cellRange(addr: string | null): { col: number; from: number; to: number } | null {
const m = /\.?\$?([A-Z]+)\$?(\d+)(?::\.?\$?[A-Z]+\$?(\d+))?/.exec(addr ?? '');
if (!m) return null;
const col = [...m[1]].reduce((a, ch) => a * 26 + (ch.charCodeAt(0) - 64), 0);
return { col, from: +m[2], to: +(m[3] ?? m[2]) };
}
// The chart's own <table:table> as rows of cells, repeats expanded. A cell's number is
// office:value; its label is the text, which is what a category axis shows.
function localTable(chart: Element): { num: (number | null)[]; text: string[] }[] {
const table = chart.getElementsByTagNameNS(NS_TABLE, 'table')[0];
const rows: { num: (number | null)[]; text: string[] }[] = [];
for (const tr of Array.from(table?.getElementsByTagNameNS(NS_TABLE, 'table-row') ?? [])) {
const num: (number | null)[] = [];
const text: string[] = [];
for (const td of Array.from(tr.children)) {
if (td.namespaceURI !== NS_TABLE || !td.localName.startsWith('table-cell')) continue;
const repeat = Math.min(64, parseInt(td.getAttributeNS(NS_TABLE, 'number-columns-repeated') ?? '1', 10) || 1);
const raw = td.getAttributeNS(NS_OFFICE, 'value');
const v = raw != null ? parseFloat(raw) : NaN;
for (let i = 0; i < repeat; i++) {
num.push(Number.isFinite(v) ? v : null);
text.push((td.textContent ?? '').trim());
}
}
rows.push({ num, text });
}
return rows;
}
const odfStyleProp = (styles: Map<string, Element>, name: string | null, ns: string, family: string, attr: string) => {
const props = styles.get(name ?? '')?.getElementsByTagNameNS(NS_STYLE, family)[0];
return props?.getAttributeNS(ns, attr) ?? null;
};
function parseOdfChart(doc: Document): Chart | null {
const chart = doc.getElementsByTagNameNS(NS_CHART, 'chart')[0];
const kind = ODF_PLOTS[(chart?.getAttributeNS(NS_CHART, 'class') ?? '').replace(/^chart:/, '')];
if (!chart || !kind) return null;
const styles = new Map<string, Element>();
for (const st of Array.from(doc.getElementsByTagNameNS(NS_STYLE, 'style'))) {
const n = st.getAttributeNS(NS_STYLE, 'name');
if (n) styles.set(n, st);
}
const plotArea = chart.getElementsByTagNameNS(NS_CHART, 'plot-area')[0] ?? null;
const rows = localTable(chart);
const column = (addr: string | null, pick: 'num' | 'text') => {
const range = cellRange(addr);
if (!range) return [];
return rows.slice(range.from - 1, range.to).map((row) => (row?.[pick][range.col - 1] ?? null));
};
const axis = (dim: string) => Array.from(plotArea?.getElementsByTagNameNS(NS_CHART, 'axis') ?? [])
.find((a) => a.getAttributeNS(NS_CHART, 'dimension') === dim) ?? null;
const titleOf = (el: Element | null) =>
(el?.getElementsByTagNameNS(NS_CHART, 'title')[0]?.textContent ?? '').trim();
const yStyle = axis('y')?.getAttributeNS(NS_CHART, 'style-name') ?? null;
const bound = (attr: string) => {
const v = parseFloat(odfStyleProp(styles, yStyle, NS_CHART, 'chart-properties', attr) ?? '');
return Number.isFinite(v) ? v : null;
};
const series: Series[] = Array.from(plotArea?.getElementsByTagNameNS(NS_CHART, 'series') ?? []).map((ser, i) => {
const fill = odfStyleProp(styles, ser.getAttributeNS(NS_CHART, 'style-name'), NS_DRAW, 'graphic-properties', 'fill-color');
const label = column(ser.getAttributeNS(NS_CHART, 'label-cell-address'), 'text')[0];
return {
name: (typeof label === 'string' && label) || `Series ${i + 1}`,
color: fill ?? DEFAULT_ACCENTS[i % DEFAULT_ACCENTS.length],
values: column(ser.getAttributeNS(NS_CHART, 'values-cell-range-address'), 'num') as (number | null)[],
};
});
if (!series.length) return null;
const catAddr = plotArea?.getElementsByTagNameNS(NS_CHART, 'categories')[0]
?.getAttributeNS(NS_TABLE, 'cell-range-address') ?? null;
return {
kind: kind === 'bar' && odfStyleProp(styles, plotArea?.getAttributeNS(NS_CHART, 'style-name') ?? null,
NS_CHART, 'chart-properties', 'vertical') === 'true' ? 'hbar' : kind,
stacked: odfStyleProp(styles, plotArea?.getAttributeNS(NS_CHART, 'style-name') ?? null,
NS_CHART, 'chart-properties', 'stacked') === 'true',
title: titleOf(chart),
cats: column(catAddr, 'text').map((v) => (typeof v === 'string' ? v : '')),
series,
legend: !!chart.getElementsByTagNameNS(NS_CHART, 'legend').length,
grid: !!axis('y')?.getElementsByTagNameNS(NS_CHART, 'grid').length,
catTitle: titleOf(axis('x')),
valTitle: titleOf(axis('y')),
axMin: bound('minimum'),
axMax: bound('maximum'),
};
}
// An embedded ODF chart document as an SVG data-URI, or null when it holds no plot
// this can draw — the caller then keeps its placeholder.
export function odfChartDataUrl(doc: Document, widthPx: number, heightPx: number): string | null {
const chart = parseOdfChart(doc);
return chart ? svgUrl(chart, widthPx, heightPx) : null;
}
+33 -5
View File
@@ -4,6 +4,7 @@ import { HEADING_STYLE_OVERRIDES, MAX_HEADING_LEVEL, ODF_LOOK_ATTRS, normalizeCo
import { builtinStyleSheet, DEFAULT_STYLE, type ParaProps, type Style, type StyleSheet, type TextProps } from '../styles/styleSheet';
import { HEADER_SHADE } from '../editor/extensions/tableHeaderRow';
import { fitInlineImage } from '../editor/extensions/image';
import { odfChartDataUrl } from './chart';
import { formatTabStops } from '../editor/extensions/tabStops';
import type { CapsMode, LineStyle } from '../editor/extensions/textEffects';
import { TABLE_REGIONS, tableLookAttr, type TableLook, type TableRegion } from '../styles/tableStyles';
@@ -313,6 +314,25 @@ function convertFormulaFrame(frame: Element, ctx: Ctx): Node | null {
return { type: 'formula', attrs: { latex, display: aloneInParagraph(frame) } };
}
// A <draw:frame> holding a chart object → an image node drawing it, at the frame's own
// size. Read-only, like the DOCX leg: the editor has no chart object, so a re-export
// carries the picture (see import/chart.ts).
function convertChartFrame(frame: Element, ctx: Ctx): Node | null {
const obj = Array.from(frame.children).find(
c => c.namespaceURI === NS.draw && (c.localName === 'object' || c.localName === 'object-ole'),
);
const wCm = lengthToCm(frame.getAttributeNS(NS.svg, 'width'));
const hCm = lengthToCm(frame.getAttributeNS(NS.svg, 'height'));
if (!obj || wCm == null || hCm == null) return null;
const doc = loadObjectDoc(obj.getAttributeNS(NS.xlink, 'href'), ctx);
const src = doc && odfChartDataUrl(doc, cmToPx(wCm), cmToPx(hCm));
if (!src) return null;
const attrs: Record<string, unknown> = { src, width: Math.round(cmToPx(wCm)), height: Math.round(cmToPx(hCm)), alt: 'Chart' };
applyFrameRotationAndWrap(frame, attrs, ctx.resolver.graphicProps(frame.getAttributeNS(NS.draw, 'style-name')));
if (!attrs.wrap || attrs.wrap === 'inline') fitInlineImage(attrs, Math.floor(cmToPx(ctx.contentWidthCm)));
return { type: 'image', attrs };
}
// A display formula is one that owns its line — ODF has no flag for it (LibreOffice
// writes display="block" on every formula object it re-saves), so it is read off the
// paragraph: nothing but this frame in it.
@@ -326,16 +346,22 @@ function aloneInParagraph(frame: Element): boolean {
// The embedded object's content.xml — an ODF formula document's root is the MathML
// itself. A non-formula object (chart, spreadsheet) has no math root and falls through.
function loadFormulaObject(href: string | null, ctx: Ctx): Element | null {
const doc = loadObjectDoc(href, ctx);
const root = doc?.documentElement;
if (!root) return null;
return root.namespaceURI === NS.math && root.localName === 'math'
? root
: doc!.getElementsByTagNameNS(NS.math, 'math')[0] ?? null;
}
// The sub-document a draw:object points at ("./Object 1" → "Object 1/content.xml").
function loadObjectDoc(href: string | null, ctx: Ctx): Document | null {
const dir = (href ?? '').replace(/^\.\//, '').replace(/\/$/, '');
if (!dir) return null;
const bytes = ctx.files[`${dir}/content.xml`] ?? ctx.files[dir];
if (!bytes) return null;
const doc = new DOMParser().parseFromString(strFromU8(bytes), 'text/xml');
const root = doc.documentElement;
if (!root || doc.getElementsByTagName('parsererror').length) return null;
return root.namespaceURI === NS.math && root.localName === 'math'
? root
: doc.getElementsByTagNameNS(NS.math, 'math')[0] ?? null;
return doc.documentElement && !doc.getElementsByTagName('parsererror').length ? doc : null;
}
// draw:rect / draw:ellipse / draw:custom-shape (rect/round-rect/ellipse preset) → a
@@ -378,6 +404,8 @@ function convertDrawElement(e: Element, ctx: Ctx): { inline?: Node; block?: Node
if (textBoxEl) return { block: convertTextBoxFrame(e, textBoxEl, ctx) };
const formula = convertFormulaFrame(e, ctx);
if (formula) return { inline: formula };
const chart = convertChartFrame(e, ctx);
if (chart) return { inline: chart };
const hasImage = !!e.getElementsByTagNameNS(NS.draw, 'image')[0];
const img = convertFrame(e, ctx);
if (img) return { inline: img };
+56 -1
View File
@@ -1,7 +1,7 @@
// A DrawingML chart part drawn as an SVG picture (import/chart.ts): series values,
// the file's own axis bounds, and the 1..n categories a chart without any gets.
import { describe, it, expect } from 'vitest';
import { chartDataUrl } from '../../src/lib/import/chart';
import { chartDataUrl, odfChartDataUrl } from '../../src/lib/import/chart';
const C = 'xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"';
const A = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"';
@@ -57,3 +57,58 @@ describe('a chart part', () => {
});
});
// ---- ODF charts: <chart:chart> plus the local table that holds its numbers ----
const NSDECL = 'xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"'
+ ' xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"'
+ ' xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"'
+ ' xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"'
+ ' xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"'
+ ' xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"';
const row = (cells: string[]) => `<table:table-row>${cells.map((c) => c).join('')}</table:table-row>`;
const strCell = (s: string) => `<table:table-cell office:value-type="string"><text:p>${s}</text:p></table:table-cell>`;
const numCell = (v: number) => `<table:table-cell office:value-type="float" office:value="${v}"><text:p>${v}</text:p></table:table-cell>`;
const odfChart = (cls: string) =>
`<office:document-content ${NSDECL}><office:automatic-styles>` +
`<style:style style:name="ch6" style:family="chart"><style:chart-properties chart:maximum="4"/></style:style>` +
`<style:style style:name="ch9" style:family="chart"><style:graphic-properties draw:fill-color="#22AA33"/></style:style>` +
`</office:automatic-styles><office:body><office:chart>` +
`<chart:chart chart:class="${cls}"><chart:title><text:p>Phase Std</text:p></chart:title>` +
`<chart:plot-area>` +
`<chart:axis chart:dimension="x"><chart:title><text:p>interferogram</text:p></chart:title>` +
`<chart:categories table:cell-range-address="local-table.$A$2:.$A$4"/></chart:axis>` +
`<chart:axis chart:dimension="y" chart:style-name="ch6"><chart:grid chart:class="major"/></chart:axis>` +
`<chart:series chart:style-name="ch9" chart:values-cell-range-address="local-table.$B$2:.$B$4"` +
` chart:label-cell-address="local-table.$B$1"/>` +
`</chart:plot-area>` +
`<table:table table:name="local-table">` +
row([strCell(''), strCell('Series1')]) +
row([strCell('Mo'), numCell(1)]) + row([strCell('Di'), numCell(2)]) + row([strCell('Mi'), numCell(3)]) +
`</table:table></chart:chart></office:chart></office:body></office:document-content>`;
const odfSvg = (cls: string) => {
const doc = new DOMParser().parseFromString(odfChart(cls), 'application/xml');
const url = odfChartDataUrl(doc, 400, 300);
expect(url?.startsWith('data:image/svg+xml')).toBe(true);
return decodeURIComponent(url!.replace(/^data:image\/svg\+xml;charset=utf-8,/, ''));
};
describe('an ODF chart object', () => {
it('draws its series from the local table, in the file’s own colour', () => {
const svg = odfSvg('chart:bar');
expect(svg).toContain('Phase Std');
expect(svg).toContain('interferogram');
expect(svg).toContain('#22AA33');
expect(svg).toContain('>Di<');
expect((svg.match(/<rect /g) ?? []).length).toBe(4); // one background + three bars
});
it('takes the value axis bound from the axis style, and declines an unknown class', () => {
expect(odfSvg('chart:bar')).toContain('>4<');
const doc = new DOMParser().parseFromString(odfChart('chart:surface'), 'application/xml');
expect(odfChartDataUrl(doc, 400, 300)).toBeNull();
});
});