feat: a chart is drawn from its own part instead of a placeholder

word/charts/chartN.xml → an SVG picture of the frame's size: series from the
producer's numCache, theme accent colours, titles, gridlines and the axis
bounds the file fixes. Bar, line, area, scatter and pie; the rest keep the box.
This commit is contained in:
Steffen Becker committed 2026-08-08 23:49:13 +00:00
1 parent 4088bffbaf
commit bf98014a3a
5 files changed
+380 -12

No files matched your search

+5 -4
View File
@@ -111,10 +111,11 @@ with 51 line-level differences — most of them the two engine rules below, not
paragraph mark is an `::after` on the block, so the same rule takes the *last* line's
height away with it. An empty widget span at the block's end gets that line back but not
to the same value. Whatever replaces the strut has to carry both.
- **Charts and metafiles are placeholders.** No renderer exists for either, so an
undrawable frame keeps its box and its label but not its picture (`imageFormats.ts`).
Exporting writes the placeholder back out — the original is gone from the moment it
is imported.
- **Metafiles are placeholders, and a re-export keeps the placeholder.** EMF/WMF/SVM have
no JS decoder, so such a frame keeps its box and its label but not its picture
(`imageFormats.ts`) — and what is exported is that placeholder. **Charts** are drawn
now (`import/chart.ts`), but as a *picture*: a re-export carries the drawing, not a
chart object, since the editor has none.
- **A list level's own hanging indent is not read**, only the 0.635cm both exports
write (`LIST_HANGING_CM`). A **left**-set label wider than that overflows into the text,
where Word moves the text to the next list tab — reading `w:lvl/w:pPr/w:ind w:hanging`
+59
View File
@@ -0,0 +1,59 @@
// 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';
const C = 'xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"';
const A = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"';
const points = (vals: number[]) =>
`<c:numCache><c:ptCount val="${vals.length}"/>` +
vals.map((v, i) => `<c:pt idx="${i}"><c:v>${v}</c:v></c:pt>`).join('') + '</c:numCache>';
const chartXml = (plot: string, opts: { max?: string; cats?: string[] } = {}) =>
`<c:chartSpace ${C} ${A}><c:chart>` +
`<c:title><c:tx><c:rich><a:p><a:r><a:t>Phase Std</a:t></a:r></a:p></c:rich></c:tx></c:title>` +
`<c:plotArea><${plot}><c:ser><c:spPr><a:solidFill><a:schemeClr val="accent2"/></a:solidFill></c:spPr>` +
(opts.cats ? `<c:cat><c:strCache><c:ptCount val="${opts.cats.length}"/>` +
opts.cats.map((t, i) => `<c:pt idx="${i}"><c:v>${t}</c:v></c:pt>`).join('') + '</c:strCache></c:cat>' : '') +
`<c:val><c:numRef>${points([1, 2, 3])}</c:numRef></c:val></c:ser></${plot}>` +
`<c:catAx><c:title><c:tx><c:rich><a:p><a:r><a:t>interferogram</a:t></a:r></a:p></c:rich></c:tx></c:title></c:catAx>` +
`<c:valAx><c:scaling><c:orientation val="minMax"/>${opts.max ? `<c:max val="${opts.max}"/>` : ''}</c:scaling>` +
`<c:majorGridlines/></c:valAx>` +
`</c:plotArea></c:chart></c:chartSpace>`;
const svgOf = (xml: string, accents: string[] = []) => {
const url = chartDataUrl(xml, 400, 300, accents);
expect(url?.startsWith('data:image/svg+xml')).toBe(true);
return decodeURIComponent(url!.replace(/^data:image\/svg\+xml;charset=utf-8,/, ''));
};
describe('a chart part', () => {
it('draws its bars, title, axis title and the theme accent it names', () => {
const svg = svgOf(chartXml('c:barChart'), ['#111111', '#22AA33']);
expect(svg).toContain('Phase Std');
expect(svg).toContain('interferogram');
expect(svg).toContain('#22AA33');
expect((svg.match(/<rect /g) ?? []).length).toBe(4); // one background + three bars
});
it('takes the value axis bound from the file, not from the data', () => {
expect(svgOf(chartXml('c:barChart', { max: '10' }))).toContain('>10<');
expect(svgOf(chartXml('c:barChart'))).not.toContain('>10<');
});
it('numbers the categories 1..n when the file names none, and keeps the ones it does', () => {
const plain = svgOf(chartXml('c:barChart'));
expect(plain).toContain('>1<');
expect(plain).toContain('>3<');
expect(svgOf(chartXml('c:barChart', { cats: ['Mo', 'Di', 'Mi'] }))).toContain('>Di<');
});
it('draws a line and a pie chart, and declines a plot it cannot draw', () => {
expect(svgOf(chartXml('c:lineChart'))).toContain('<polyline');
expect(svgOf(chartXml('c:pieChart'))).toContain('<path');
expect(chartDataUrl(chartXml('c:surfaceChart'), 400, 300, [])).toBeNull();
expect(chartDataUrl('not xml at all', 400, 300, [])).toBeNull();
});
});