@ggraphic/editor 0.1.0-alpha.5 → 0.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CanvasEditorProvider.d.ts +6 -1
- package/dist/components/CanvasEditor/CanvasEditorShell.js +139 -36
- package/dist/components/CanvasEditor/editorShell.d.ts +6 -0
- package/dist/components/CanvasEditor/types.d.ts +28 -0
- package/dist/components/GenericCanvas.d.ts +19 -2
- package/dist/components/GenericCanvas.js +28 -3
- package/dist/components/PagePreview.d.ts +8 -1
- package/dist/components/PagePreview.js +4 -1
- package/dist/components/TopBar/DownloadSection.d.ts +24 -3
- package/dist/components/TopBar/DownloadSection.js +45 -7
- package/dist/components/TopBar/ShareDropdown.d.ts +4 -2
- package/dist/components/TopBar/ShareDropdown.js +2 -2
- package/dist/entries/export.d.ts +14 -1
- package/dist/entries/export.js +14 -1
- package/dist/entries/services.d.ts +1 -1
- package/dist/export/fonts/fontFiles.d.ts +100 -0
- package/dist/export/fonts/fontFiles.js +333 -0
- package/dist/export/fonts/glyphs.d.ts +113 -0
- package/dist/export/fonts/glyphs.js +120 -0
- package/dist/export/fonts/variation.d.ts +23 -0
- package/dist/export/fonts/variation.js +43 -0
- package/dist/export/pdf/pdfContent.d.ts +108 -0
- package/dist/export/pdf/pdfContent.js +566 -0
- package/dist/export/pdf/pdfFontkit.d.ts +54 -0
- package/dist/export/pdf/pdfFontkit.js +159 -0
- package/dist/export/pdf/pdfGeometry.d.ts +54 -0
- package/dist/export/pdf/pdfGeometry.js +228 -0
- package/dist/export/pdf/pdfResources.d.ts +109 -0
- package/dist/export/pdf/pdfResources.js +314 -0
- package/dist/export/pdf/writePdf.d.ts +34 -0
- package/dist/export/pdf/writePdf.js +165 -0
- package/dist/export/pptx/exportPptxDeck.d.ts +10 -2
- package/dist/export/pptx/exportPptxDeck.js +5 -2
- package/dist/export/pptx/svgPicture.d.ts +19 -0
- package/dist/export/pptx/svgPicture.js +64 -0
- package/dist/export/pptx/vectorizeElement.d.ts +9 -0
- package/dist/export/pptx/vectorizeElement.js +118 -0
- package/dist/export/pptx/writePptx.d.ts +13 -0
- package/dist/export/pptx/writePptx.js +17 -0
- package/dist/export/printPdf.d.ts +1 -0
- package/dist/export/printPdf.js +1 -1
- package/dist/export/scene/browserDeps.d.ts +32 -0
- package/dist/export/scene/browserDeps.js +174 -0
- package/dist/export/scene/imageBytes.d.ts +79 -0
- package/dist/export/scene/imageBytes.js +330 -0
- package/dist/export/scene/itemBounds.d.ts +9 -0
- package/dist/export/scene/itemBounds.js +40 -0
- package/dist/export/scene/matrix.d.ts +13 -0
- package/dist/export/scene/matrix.js +32 -0
- package/dist/export/scene/rasterLimit.d.ts +13 -0
- package/dist/export/scene/rasterLimit.js +24 -0
- package/dist/export/scene/rebaseOps.d.ts +8 -0
- package/dist/export/scene/rebaseOps.js +51 -0
- package/dist/export/scene/recordPageScene.d.ts +28 -0
- package/dist/export/scene/recordPageScene.js +224 -0
- package/dist/export/scene/recordingCanvas.d.ts +55 -0
- package/dist/export/scene/recordingCanvas.js +71 -0
- package/dist/export/scene/recordingContext.d.ts +162 -0
- package/dist/export/scene/recordingContext.js +1031 -0
- package/dist/export/scene/replayScene.d.ts +31 -0
- package/dist/export/scene/replayScene.js +213 -0
- package/dist/export/scene/resolveScene.d.ts +48 -0
- package/dist/export/scene/resolveScene.js +236 -0
- package/dist/export/scene/trustedSvgImages.d.ts +3 -0
- package/dist/export/scene/trustedSvgImages.js +21 -0
- package/dist/export/scene/types.d.ts +241 -0
- package/dist/export/scene/types.js +38 -0
- package/dist/export/slidesPdf.d.ts +10 -2
- package/dist/export/slidesPdf.js +8 -4
- package/dist/export/svg/__fixtures__/barChart.svg +1 -0
- package/dist/export/svg/__golden__/fixture-editable.svg +87 -0
- package/dist/export/svg/__golden__/fixture-outlines.svg +88 -0
- package/dist/export/svg/inlineSvg.d.ts +10 -0
- package/dist/export/svg/inlineSvg.js +119 -0
- package/dist/export/svg/scopeCss.d.ts +45 -0
- package/dist/export/svg/scopeCss.js +214 -0
- package/dist/export/svg/svgColor.d.ts +8 -0
- package/dist/export/svg/svgColor.js +46 -0
- package/dist/export/svg/svgImageScene.d.ts +41 -0
- package/dist/export/svg/svgImageScene.js +128 -0
- package/dist/export/svg/svgPathData.d.ts +31 -0
- package/dist/export/svg/svgPathData.js +466 -0
- package/dist/export/svg/svgToScene.d.ts +19 -0
- package/dist/export/svg/svgToScene.js +1183 -0
- package/dist/export/svg/writeSvg.d.ts +33 -0
- package/dist/export/svg/writeSvg.js +594 -0
- package/dist/export/svg/xml.d.ts +23 -0
- package/dist/export/svg/xml.js +49 -0
- package/dist/export/vectorDeps.d.ts +27 -0
- package/dist/export/vectorDeps.js +37 -0
- package/dist/export/vectorExport.d.ts +85 -0
- package/dist/export/vectorExport.js +287 -0
- package/dist/hostServices.d.ts +15 -0
- package/dist/i18n/de.d.ts +26 -9
- package/dist/i18n/de.js +26 -10
- package/dist/i18n/en.js +24 -0
- package/dist/primitives/AssetPrimitive.js +7 -2
- package/dist/primitives/CanvasIcons.js +6 -0
- package/dist/primitives/CanvasStage.js +2 -2
- package/dist/primitives/ChartPrimitive.d.ts +11 -1
- package/dist/primitives/ChartPrimitive.js +73 -19
- package/dist/primitives/IconPrimitive.js +3 -0
- package/dist/primitives/IllustrationPrimitive.js +14 -1
- package/dist/primitives/chartFont.d.ts +30 -0
- package/dist/primitives/chartFont.js +127 -0
- package/dist/sidebar/sections/ImageAdjustSection.js +5 -4
- package/dist/sidebar/sections/assets/ElementsPanel.js +4 -2
- package/dist/sidebar/sections/assets/assetAudience.d.ts +12 -0
- package/dist/sidebar/sections/assets/assetAudience.js +18 -0
- package/dist/sidebar/sections/assets/useElementCatalog.js +3 -2
- package/dist/styles/compiled.css +1 -1
- package/dist/utils/canvasAssets.d.ts +11 -1
- package/dist/utils/canvasAssets.js +8 -0
- package/dist/utils/canvasIcons.js +5 -3
- package/dist/utils/captureStage.d.ts +19 -0
- package/dist/utils/captureStage.js +14 -1
- package/dist/utils/externalIcons.d.ts +7 -0
- package/dist/utils/externalIcons.js +70 -3
- package/dist/utils/illustrations/svgCache.js +6 -1
- package/dist/utils/imageFilters.d.ts +13 -0
- package/dist/utils/imageFilters.js +11 -0
- package/dist/utils/zipPageImages.d.ts +8 -0
- package/dist/utils/zipPageImages.js +16 -4
- package/package.json +6 -2
- package/skills/ggraphic-embed/SKILL.md +33 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes a resolved export scene (`ExportScene`) as one SVG document that opens as layers in
|
|
3
|
+
* Illustrator, Inkscape and Affinity.
|
|
4
|
+
*
|
|
5
|
+
* - One `<g id="el-…" data-element-id="…">` per item, with the layer label as its `<title>`.
|
|
6
|
+
* - Every op keeps the matrix it was drawn with, as the `transform` of its element; the clip stack
|
|
7
|
+
* becomes nested `<g clip-path>` around the ops that share it, in page px. A clip that clips
|
|
8
|
+
* everything away drops the op.
|
|
9
|
+
* - Gradients, patterns, clip paths and shadow filters are written once into `<defs>`.
|
|
10
|
+
* - Text is glyph outlines, one `<path>` per run of code points a font file covers, placed at the
|
|
11
|
+
* x the browser measured. A run no file covers, and all text in `editable` mode, is a `<text>`
|
|
12
|
+
* with the font family: fonts are never embedded.
|
|
13
|
+
* - An SVG source is inlined as a nested `<svg>` with its ids, classes and style rules scoped to
|
|
14
|
+
* the instance (`inlineSvg.ts`); one whose CSS cannot be scoped, and any other image, is a data
|
|
15
|
+
* URL. In outline mode a source with text of its own (a chart) is not inlined: its shapes and
|
|
16
|
+
* text are written as scene ops (`svgImageScene.ts`), so its text is outlined like the page's
|
|
17
|
+
* and the `@font-face` it carries stays out of the file. A source the converter cannot read
|
|
18
|
+
* completely is inlined after all, and `report.unknownCalls` says its text was not outlined.
|
|
19
|
+
*
|
|
20
|
+
* Numbers have three decimals. The linear part of a matrix has six: a rotation that is off by
|
|
21
|
+
* 0.0005 in its cosine moves a point 1000 px from the origin by half a pixel. The output depends
|
|
22
|
+
* on nothing but the scene and the options: same input, same bytes.
|
|
23
|
+
*/
|
|
24
|
+
import type { FontFileSet } from '../fonts/fontFiles.js';
|
|
25
|
+
import type { ExportReport, ExportScene } from '../scene/types.js';
|
|
26
|
+
export interface WriteSvgOptions {
|
|
27
|
+
text: 'outlines' | 'editable';
|
|
28
|
+
/** Resolved per FontSpec key `${family}|${weight}|${style}`; absent → <text>. */
|
|
29
|
+
fonts: ReadonlyMap<string, FontFileSet>;
|
|
30
|
+
report: ExportReport;
|
|
31
|
+
page: number;
|
|
32
|
+
}
|
|
33
|
+
export declare function writeSvg(scene: ExportScene, options: WriteSvgOptions): string;
|
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
import { fakeBoldWidth, glyphOutline, splitRuns } from '../fonts/glyphs.js';
|
|
2
|
+
import { inlineSvg } from './inlineSvg.js';
|
|
3
|
+
import { mostUsedFont, svgDefaultsFor, svgHasText, svgImageGroup, } from './svgImageScene.js';
|
|
4
|
+
import { escapeXml, formatNumber, withoutIllegalXml } from './xml.js';
|
|
5
|
+
export function writeSvg(scene, options) {
|
|
6
|
+
return new SvgWriter(scene, options).write();
|
|
7
|
+
}
|
|
8
|
+
const num = (value) => formatNumber(value);
|
|
9
|
+
function attributeText(attrs) {
|
|
10
|
+
let text = '';
|
|
11
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
12
|
+
if (value !== undefined)
|
|
13
|
+
text += ` ${name}="${escapeXml(value)}"`;
|
|
14
|
+
}
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* An element as lines, children indented. `children` is a string for inline content (already
|
|
19
|
+
* escaped markup) and lines for nested elements; nothing at all makes it self-closing.
|
|
20
|
+
*/
|
|
21
|
+
function el(name, attrs, children) {
|
|
22
|
+
const open = `<${name}${attributeText(attrs)}`;
|
|
23
|
+
if (children === undefined || (Array.isArray(children) && children.length === 0)) {
|
|
24
|
+
return [`${open}/>`];
|
|
25
|
+
}
|
|
26
|
+
if (typeof children === 'string')
|
|
27
|
+
return [`${open}>${children}</${name}>`];
|
|
28
|
+
return [`${open}>`, ...children.map((line) => ` ${line}`), `</${name}>`];
|
|
29
|
+
}
|
|
30
|
+
/** `el('g', …)` unless there is nothing to say about the group. */
|
|
31
|
+
function wrap(attrs, lines) {
|
|
32
|
+
return Object.values(attrs).some((value) => value !== undefined) ? el('g', attrs, lines) : lines;
|
|
33
|
+
}
|
|
34
|
+
/** Nothing for the identity, `translate` for a move, `matrix` otherwise. */
|
|
35
|
+
function transformOf(matrix) {
|
|
36
|
+
const [a, b, c, d] = [matrix[0], matrix[1], matrix[2], matrix[3]].map((v) => formatNumber(v, 6));
|
|
37
|
+
const [e, f] = [num(matrix[4]), num(matrix[5])];
|
|
38
|
+
if (a === '1' && b === '0' && c === '0' && d === '1') {
|
|
39
|
+
return e === '0' && f === '0' ? undefined : `translate(${e},${f})`;
|
|
40
|
+
}
|
|
41
|
+
return `matrix(${a},${b},${c},${d},${e},${f})`;
|
|
42
|
+
}
|
|
43
|
+
/** Compact path data: the command letter, then its numbers; `dx`/`dy` move every point. */
|
|
44
|
+
function pathData(commands, dx = 0, dy = 0) {
|
|
45
|
+
let d = '';
|
|
46
|
+
for (const c of commands) {
|
|
47
|
+
switch (c.op) {
|
|
48
|
+
case 'M':
|
|
49
|
+
case 'L':
|
|
50
|
+
d += `${c.op}${num(c.x + dx)} ${num(c.y + dy)}`;
|
|
51
|
+
break;
|
|
52
|
+
case 'C':
|
|
53
|
+
d += `C${num(c.x1 + dx)} ${num(c.y1 + dy)} ${num(c.x2 + dx)} ${num(c.y2 + dy)} ${num(c.x + dx)} ${num(c.y + dy)}`;
|
|
54
|
+
break;
|
|
55
|
+
case 'Q':
|
|
56
|
+
d += `Q${num(c.x1 + dx)} ${num(c.y1 + dy)} ${num(c.x + dx)} ${num(c.y + dy)}`;
|
|
57
|
+
break;
|
|
58
|
+
case 'Z':
|
|
59
|
+
d += 'Z';
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return d;
|
|
64
|
+
}
|
|
65
|
+
function toBase64(bytes) {
|
|
66
|
+
let binary = '';
|
|
67
|
+
for (let i = 0; i < bytes.length; i += 0x8000) {
|
|
68
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
69
|
+
}
|
|
70
|
+
return btoa(binary);
|
|
71
|
+
}
|
|
72
|
+
function dataUrl(image) {
|
|
73
|
+
return image.kind === 'svg'
|
|
74
|
+
? `data:image/svg+xml;base64,${toBase64(new TextEncoder().encode(image.markup))}`
|
|
75
|
+
: `data:${image.mime};base64,${toBase64(image.bytes)}`;
|
|
76
|
+
}
|
|
77
|
+
const CSS_WORD = /^-?[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
78
|
+
/** A family list as CSS: a name of plain words as it is, any other name quoted. */
|
|
79
|
+
function fontFamily(font) {
|
|
80
|
+
return [font.family, ...(font.fallback ?? [])]
|
|
81
|
+
.map((name) => {
|
|
82
|
+
const words = name.trim().split(/\s+/);
|
|
83
|
+
return words.every((word) => CSS_WORD.test(word))
|
|
84
|
+
? words.join(' ')
|
|
85
|
+
: `'${name.replace(/(['\\])/g, '\\$1')}'`;
|
|
86
|
+
})
|
|
87
|
+
.join(', ');
|
|
88
|
+
}
|
|
89
|
+
const PLAIN_STROKE = {
|
|
90
|
+
width: 1,
|
|
91
|
+
cap: 'butt',
|
|
92
|
+
join: 'miter',
|
|
93
|
+
miterLimit: 10,
|
|
94
|
+
dash: [],
|
|
95
|
+
dashOffset: 0,
|
|
96
|
+
};
|
|
97
|
+
/** SVG's defaults differ from the canvas's only in the miter limit (4 against 10): both are explicit. */
|
|
98
|
+
function strokeAttrs(stroke) {
|
|
99
|
+
const s = stroke ?? PLAIN_STROKE;
|
|
100
|
+
return {
|
|
101
|
+
'stroke-width': num(s.width),
|
|
102
|
+
'stroke-linecap': s.cap === 'butt' ? undefined : s.cap,
|
|
103
|
+
'stroke-linejoin': s.join === 'miter' ? undefined : s.join,
|
|
104
|
+
'stroke-miterlimit': s.join === 'miter' && s.miterLimit !== 4 ? num(s.miterLimit) : undefined,
|
|
105
|
+
'stroke-dasharray': s.dash.length > 0 ? s.dash.map(num).join(' ') : undefined,
|
|
106
|
+
'stroke-dashoffset': s.dashOffset !== 0 ? num(s.dashOffset) : undefined,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function viewBoxOf(value) {
|
|
110
|
+
const parts = value
|
|
111
|
+
?.trim()
|
|
112
|
+
.split(/[\s,]+/)
|
|
113
|
+
.map(Number) ?? [];
|
|
114
|
+
if (parts.length !== 4 ||
|
|
115
|
+
parts.some((p) => !Number.isFinite(p)) ||
|
|
116
|
+
parts[2] <= 0 ||
|
|
117
|
+
parts[3] <= 0) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
return parts;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Attributes of an inlined root that the writer sets itself, or that mean nothing nested. `style`
|
|
124
|
+
* is one of them: a chart's `width: 100%; height: 100%` sizes it in CSS terms where it stands
|
|
125
|
+
* alone, and nested it would fight the `width` and `height` the writer places it with (Firefox
|
|
126
|
+
* and Illustrator read it).
|
|
127
|
+
*/
|
|
128
|
+
const OWN_ROOT_ATTRS = new Set([
|
|
129
|
+
'style',
|
|
130
|
+
'x',
|
|
131
|
+
'y',
|
|
132
|
+
'width',
|
|
133
|
+
'height',
|
|
134
|
+
'viewBox',
|
|
135
|
+
'preserveAspectRatio',
|
|
136
|
+
'version',
|
|
137
|
+
'baseProfile',
|
|
138
|
+
'zoomAndPan',
|
|
139
|
+
]);
|
|
140
|
+
// ---------------------------------------------------------------------------------------------
|
|
141
|
+
class SvgWriter {
|
|
142
|
+
scene;
|
|
143
|
+
options;
|
|
144
|
+
/** The lines of `<defs>`, in the order of first use. */
|
|
145
|
+
defs = [];
|
|
146
|
+
defIds = new Map();
|
|
147
|
+
clipIds = new Map();
|
|
148
|
+
tileIds = new Map();
|
|
149
|
+
counters = new Map();
|
|
150
|
+
usedIds = new Set();
|
|
151
|
+
inlined = 0;
|
|
152
|
+
elementId = null;
|
|
153
|
+
svgTextDefaults = null;
|
|
154
|
+
constructor(scene, options) {
|
|
155
|
+
this.scene = scene;
|
|
156
|
+
this.options = options;
|
|
157
|
+
}
|
|
158
|
+
write() {
|
|
159
|
+
const body = this.scene.items.flatMap((item) => this.item(item));
|
|
160
|
+
const { width, height } = this.scene;
|
|
161
|
+
const root = el('svg', {
|
|
162
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
163
|
+
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
|
|
164
|
+
viewBox: `0 0 ${num(width)} ${num(height)}`,
|
|
165
|
+
width: num(width),
|
|
166
|
+
height: num(height),
|
|
167
|
+
}, [...(this.defs.length > 0 ? el('defs', {}, this.defs) : []), ...body]);
|
|
168
|
+
return `${root.join('\n')}\n`;
|
|
169
|
+
}
|
|
170
|
+
// -- structure ---------------------------------------------------------------------------
|
|
171
|
+
item(item) {
|
|
172
|
+
this.elementId = item.elementId;
|
|
173
|
+
const base = `el-${(item.elementId ?? 'page').replace(/[^A-Za-z0-9_-]/g, '_')}`;
|
|
174
|
+
let id = base;
|
|
175
|
+
for (let n = 2; this.usedIds.has(id); n++)
|
|
176
|
+
id = `${base}-${n}`;
|
|
177
|
+
this.usedIds.add(id);
|
|
178
|
+
const title = item.label ? el('title', {}, escapeXml(item.label)) : [];
|
|
179
|
+
return el('g', { id, 'data-element-id': item.elementId ?? undefined }, [
|
|
180
|
+
...title,
|
|
181
|
+
...this.ops(item.ops),
|
|
182
|
+
]);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The ops in order. Neighbours whose clip stacks begin with the same clips (by reference) sit
|
|
186
|
+
* in the same `<g clip-path>`, one level per clip.
|
|
187
|
+
*/
|
|
188
|
+
ops(ops) {
|
|
189
|
+
const out = [];
|
|
190
|
+
const open = [];
|
|
191
|
+
const sink = () => open.at(-1)?.lines ?? out;
|
|
192
|
+
const closeTo = (depth) => {
|
|
193
|
+
while (open.length > depth) {
|
|
194
|
+
const frame = open.pop();
|
|
195
|
+
sink().push(...el('g', { 'clip-path': `url(#${frame.id})` }, frame.lines));
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
for (const op of ops) {
|
|
199
|
+
if (op.clips.some((clip) => clip.commands.length === 0))
|
|
200
|
+
continue;
|
|
201
|
+
const lines = this.op(op);
|
|
202
|
+
if (lines.length === 0)
|
|
203
|
+
continue;
|
|
204
|
+
let shared = 0;
|
|
205
|
+
while (shared < open.length &&
|
|
206
|
+
shared < op.clips.length &&
|
|
207
|
+
open[shared].clip === op.clips[shared]) {
|
|
208
|
+
shared++;
|
|
209
|
+
}
|
|
210
|
+
closeTo(shared);
|
|
211
|
+
for (const clip of op.clips.slice(shared)) {
|
|
212
|
+
open.push({ clip, id: this.clipId(clip), lines: [] });
|
|
213
|
+
}
|
|
214
|
+
sink().push(...lines);
|
|
215
|
+
}
|
|
216
|
+
closeTo(0);
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
/** The op, and its shadow filter around it: the filter works in page px, outside the matrix. */
|
|
220
|
+
op(op) {
|
|
221
|
+
const lines = this.content(op);
|
|
222
|
+
if (lines.length === 0 || !op.shadow)
|
|
223
|
+
return lines;
|
|
224
|
+
return el('g', { filter: `url(#${this.shadowId(op.shadow)})` }, lines);
|
|
225
|
+
}
|
|
226
|
+
content(op) {
|
|
227
|
+
const common = {
|
|
228
|
+
transform: transformOf(op.matrix),
|
|
229
|
+
opacity: op.alpha < 1 ? num(op.alpha) : undefined,
|
|
230
|
+
};
|
|
231
|
+
switch (op.kind) {
|
|
232
|
+
case 'path':
|
|
233
|
+
return this.path(op, common);
|
|
234
|
+
case 'text':
|
|
235
|
+
return this.text(op, common);
|
|
236
|
+
case 'image':
|
|
237
|
+
return this.image(op, common);
|
|
238
|
+
case 'group': {
|
|
239
|
+
const children = this.ops(op.ops);
|
|
240
|
+
return children.length > 0 ? wrap(common, children) : [];
|
|
241
|
+
}
|
|
242
|
+
case 'raster':
|
|
243
|
+
if (op.png) {
|
|
244
|
+
return el('image', {
|
|
245
|
+
x: num(op.x),
|
|
246
|
+
y: num(op.y),
|
|
247
|
+
width: num(op.width),
|
|
248
|
+
height: num(op.height),
|
|
249
|
+
preserveAspectRatio: 'none',
|
|
250
|
+
'xlink:href': `data:image/png;base64,${toBase64(op.png)}`,
|
|
251
|
+
...common,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// Without pixels, the ops the island was made of; each carries its own state.
|
|
255
|
+
return op.source ? this.ops(op.source) : [];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// -- shapes and text ---------------------------------------------------------------------
|
|
259
|
+
path(op, common) {
|
|
260
|
+
if (op.commands.length === 0)
|
|
261
|
+
return [];
|
|
262
|
+
const paint = this.shapePaint(op.mode, op.paint, op.stroke, op.rule);
|
|
263
|
+
return paint ? el('path', { d: pathData(op.commands), ...paint, ...common }) : [];
|
|
264
|
+
}
|
|
265
|
+
shapePaint(mode, paint, stroke, rule) {
|
|
266
|
+
const painted = this.paint(mode, paint);
|
|
267
|
+
if (!painted)
|
|
268
|
+
return null;
|
|
269
|
+
return mode === 'fill'
|
|
270
|
+
? { ...painted, 'fill-rule': rule === 'evenodd' ? 'evenodd' : undefined }
|
|
271
|
+
: { fill: 'none', ...painted, ...strokeAttrs(stroke) };
|
|
272
|
+
}
|
|
273
|
+
paint(kind, paint) {
|
|
274
|
+
switch (paint.type) {
|
|
275
|
+
case 'solid':
|
|
276
|
+
return {
|
|
277
|
+
[kind]: paint.hex,
|
|
278
|
+
[`${kind}-opacity`]: paint.alpha < 1 ? num(paint.alpha) : undefined,
|
|
279
|
+
};
|
|
280
|
+
case 'linear':
|
|
281
|
+
case 'radial':
|
|
282
|
+
return { [kind]: `url(#${this.gradientId(paint)})` };
|
|
283
|
+
case 'pattern': {
|
|
284
|
+
const id = this.patternId(paint);
|
|
285
|
+
return id ? { [kind]: `url(#${id})` } : null;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
text(op, common) {
|
|
290
|
+
const paint = this.shapePaint(op.mode, op.paint, op.stroke, 'nonzero');
|
|
291
|
+
if (!paint)
|
|
292
|
+
return [];
|
|
293
|
+
const whole = { text: op.text, xs: op.xs };
|
|
294
|
+
if (this.options.text === 'editable')
|
|
295
|
+
return this.textElement(op, whole, paint, common);
|
|
296
|
+
const set = this.options.fonts.get(`${op.font.family}|${op.font.weight}|${op.font.style}`);
|
|
297
|
+
const runs = set
|
|
298
|
+
? splitRuns(set, op.text, op.xs)
|
|
299
|
+
: op.text === ''
|
|
300
|
+
? []
|
|
301
|
+
: [{ file: null, ...whole }];
|
|
302
|
+
const parts = [];
|
|
303
|
+
for (const run of runs) {
|
|
304
|
+
if (run.file === null) {
|
|
305
|
+
this.options.report.missingGlyphs.push({
|
|
306
|
+
page: this.options.page,
|
|
307
|
+
elementId: this.elementId,
|
|
308
|
+
text: run.text,
|
|
309
|
+
});
|
|
310
|
+
parts.push((extra) => this.textElement(op, run, paint, extra));
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const { defaultInstanceFonts } = this.options.report;
|
|
314
|
+
if (run.file.variation === 'default-only' && !defaultInstanceFonts.includes(op.font.family)) {
|
|
315
|
+
defaultInstanceFonts.push(op.font.family);
|
|
316
|
+
}
|
|
317
|
+
const glyphs = glyphOutline(run.file, run.text, op.font.size);
|
|
318
|
+
const d = glyphs.map((glyph, i) => pathData(glyph.path, run.xs[i] ?? 0, op.y)).join('');
|
|
319
|
+
// A regular cut standing in for a bold: the outline grows by a stroke in the fill's colour.
|
|
320
|
+
// Fill and stroke are painted opaque and the alpha sits on a group around them, or the
|
|
321
|
+
// inner half of the stroke band would be painted twice and come out darker than on canvas.
|
|
322
|
+
const grows = run.file.fakeBold && op.mode === 'fill' && paint['fill'] !== undefined;
|
|
323
|
+
if (d === '')
|
|
324
|
+
continue;
|
|
325
|
+
if (!grows) {
|
|
326
|
+
parts.push((extra) => el('path', { d, ...paint, ...extra }));
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const { 'fill-opacity': alpha, ...opaque } = paint;
|
|
330
|
+
const grown = {
|
|
331
|
+
d,
|
|
332
|
+
...opaque,
|
|
333
|
+
stroke: paint['fill'],
|
|
334
|
+
'stroke-width': num(fakeBoldWidth(op.font.size)),
|
|
335
|
+
'stroke-linejoin': 'round',
|
|
336
|
+
};
|
|
337
|
+
parts.push((extra) => alpha === undefined
|
|
338
|
+
? el('path', { ...grown, ...extra })
|
|
339
|
+
: el('g', { opacity: alpha }, el('path', { ...grown, ...extra })));
|
|
340
|
+
}
|
|
341
|
+
if (parts.length === 0)
|
|
342
|
+
return [];
|
|
343
|
+
if (parts.length === 1)
|
|
344
|
+
return parts[0](common);
|
|
345
|
+
return wrap(common, parts.flatMap((part) => part({})));
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* A `<text>` with one x per code point; `xml:space` keeps the spaces the x list counts. A code
|
|
349
|
+
* point XML forbids is left out of the text and of the list alike.
|
|
350
|
+
*/
|
|
351
|
+
textElement(op, whole, paint, extra) {
|
|
352
|
+
const run = withoutIllegalXml(whole);
|
|
353
|
+
if (run.text === '')
|
|
354
|
+
return [];
|
|
355
|
+
return el('text', {
|
|
356
|
+
x: run.xs.map(num).join(' '),
|
|
357
|
+
y: num(op.y),
|
|
358
|
+
'font-family': fontFamily(op.font),
|
|
359
|
+
'font-size': num(op.font.size),
|
|
360
|
+
'font-weight': String(op.font.weight),
|
|
361
|
+
'font-style': op.font.style,
|
|
362
|
+
'xml:space': 'preserve',
|
|
363
|
+
...paint,
|
|
364
|
+
...extra,
|
|
365
|
+
}, escapeXml(run.text));
|
|
366
|
+
}
|
|
367
|
+
// -- images ------------------------------------------------------------------------------
|
|
368
|
+
image(op, common) {
|
|
369
|
+
const image = op.image;
|
|
370
|
+
if (image.kind === 'element')
|
|
371
|
+
return []; // not resolved: nothing to embed
|
|
372
|
+
if (op.sw === 0 || op.sh === 0 || op.dw === 0 || op.dh === 0)
|
|
373
|
+
return [];
|
|
374
|
+
if (image.kind === 'svg') {
|
|
375
|
+
const outlined = this.options.text === 'outlines' && svgHasText(image.markup)
|
|
376
|
+
? this.outlinedSvg(op, image)
|
|
377
|
+
: null;
|
|
378
|
+
if (outlined)
|
|
379
|
+
return outlined;
|
|
380
|
+
const inlined = inlineSvg(image.markup, `i${this.inlined}-`);
|
|
381
|
+
if (inlined) {
|
|
382
|
+
this.inlined++;
|
|
383
|
+
return this.nestedSvg(op, image, inlined, common);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return this.embeddedImage(op, image, common);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The picture's shapes and text as ops, written like any others, or null when the converter
|
|
390
|
+
* cannot read all of it: the picture is then inlined as it is, with live text, and the report
|
|
391
|
+
* says which constructs stopped it. The op's matrix, clips and shadow are the caller's; the
|
|
392
|
+
* ops carry the matrix themselves, and the group the op's alpha and the crop.
|
|
393
|
+
*/
|
|
394
|
+
outlinedSvg(op, source) {
|
|
395
|
+
this.svgTextDefaults ??= svgDefaultsFor(this.options.fonts, mostUsedFont([this.scene]));
|
|
396
|
+
const { group, unsupported } = svgImageGroup(op, source, this.svgTextDefaults, []);
|
|
397
|
+
if (unsupported.length > 0) {
|
|
398
|
+
this.options.report.unknownCalls.push({
|
|
399
|
+
page: this.options.page,
|
|
400
|
+
elementId: this.elementId,
|
|
401
|
+
call: `svg text not outlined: ${unsupported.join(', ')}`,
|
|
402
|
+
});
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
return this.ops([group]);
|
|
406
|
+
}
|
|
407
|
+
/** The picture as an `<image>`; a source rect smaller than the picture is a clip on a larger one. */
|
|
408
|
+
embeddedImage(op, image, common) {
|
|
409
|
+
const whole = num(op.sx) === '0' &&
|
|
410
|
+
num(op.sy) === '0' &&
|
|
411
|
+
num(op.sw) === num(image.width) &&
|
|
412
|
+
num(op.sh) === num(image.height);
|
|
413
|
+
const kx = op.dw / op.sw;
|
|
414
|
+
const ky = op.dh / op.sh;
|
|
415
|
+
const box = whole
|
|
416
|
+
? [op.dx, op.dy, op.dw, op.dh]
|
|
417
|
+
: [op.dx - op.sx * kx, op.dy - op.sy * ky, image.width * kx, image.height * ky];
|
|
418
|
+
return el('image', {
|
|
419
|
+
x: num(box[0]),
|
|
420
|
+
y: num(box[1]),
|
|
421
|
+
width: num(box[2]),
|
|
422
|
+
height: num(box[3]),
|
|
423
|
+
preserveAspectRatio: 'none',
|
|
424
|
+
'xlink:href': dataUrl(image),
|
|
425
|
+
'clip-path': whole ? undefined : `url(#${this.rectClipId(op.dx, op.dy, op.dw, op.dh)})`,
|
|
426
|
+
...common,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* The SVG as a nested `<svg>` over the destination rect. The matrix goes on a group around it:
|
|
431
|
+
* SVG 1.1 has no `transform` on `<svg>`, and the tools that read 1.1 ignore it.
|
|
432
|
+
*
|
|
433
|
+
* The canvas draws the image at its natural size (`image.width` × `image.height`) and then
|
|
434
|
+
* crops and stretches that bitmap. When the viewBox has the shape of the natural size the two
|
|
435
|
+
* steps are one move of the viewBox; when it does not (and the root does not say `none`) the
|
|
436
|
+
* SVG is first fitted into its natural size by an inner viewport, then the outer one crops.
|
|
437
|
+
*/
|
|
438
|
+
nestedSvg(op, image, inlined, common) {
|
|
439
|
+
const { attrs, inner } = inlined;
|
|
440
|
+
const passed = {};
|
|
441
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
442
|
+
if (!OWN_ROOT_ATTRS.has(name))
|
|
443
|
+
passed[name] = value;
|
|
444
|
+
}
|
|
445
|
+
const viewBox = viewBoxOf(attrs.viewBox) ?? [0, 0, image.width, image.height];
|
|
446
|
+
const stretches = /^\s*(defer\s+)?none\b/.test(attrs.preserveAspectRatio ?? '');
|
|
447
|
+
const sameShape = Math.abs(viewBox[2] * image.height - viewBox[3] * image.width) <=
|
|
448
|
+
1e-3 * viewBox[3] * image.width;
|
|
449
|
+
const place = { x: num(op.dx), y: num(op.dy), width: num(op.dw), height: num(op.dh) };
|
|
450
|
+
if (sameShape || stretches) {
|
|
451
|
+
const kx = viewBox[2] / image.width;
|
|
452
|
+
const ky = viewBox[3] / image.height;
|
|
453
|
+
const cropped = [viewBox[0] + op.sx * kx, viewBox[1] + op.sy * ky, op.sw * kx, op.sh * ky];
|
|
454
|
+
return wrap(common, el('svg', { ...place, viewBox: cropped.map(num).join(' '), preserveAspectRatio: 'none', ...passed }, inner));
|
|
455
|
+
}
|
|
456
|
+
const fitted = el('svg', {
|
|
457
|
+
x: '0',
|
|
458
|
+
y: '0',
|
|
459
|
+
width: num(image.width),
|
|
460
|
+
height: num(image.height),
|
|
461
|
+
viewBox: attrs.viewBox,
|
|
462
|
+
preserveAspectRatio: attrs.preserveAspectRatio,
|
|
463
|
+
...passed,
|
|
464
|
+
}, inner);
|
|
465
|
+
return wrap(common, el('svg', {
|
|
466
|
+
...place,
|
|
467
|
+
viewBox: [op.sx, op.sy, op.sw, op.sh].map(num).join(' '),
|
|
468
|
+
preserveAspectRatio: 'none',
|
|
469
|
+
}, fitted));
|
|
470
|
+
}
|
|
471
|
+
// -- definitions -------------------------------------------------------------------------
|
|
472
|
+
newId(kind) {
|
|
473
|
+
const n = this.counters.get(kind) ?? 0;
|
|
474
|
+
this.counters.set(kind, n + 1);
|
|
475
|
+
return `${kind}-${n}`;
|
|
476
|
+
}
|
|
477
|
+
/** One definition per `key`: the id of the first, or a new one built by `build`. */
|
|
478
|
+
define(kind, key, build) {
|
|
479
|
+
const known = this.defIds.get(`${kind}\0${key}`);
|
|
480
|
+
if (known)
|
|
481
|
+
return known;
|
|
482
|
+
const id = this.newId(kind);
|
|
483
|
+
this.defIds.set(`${kind}\0${key}`, id);
|
|
484
|
+
this.defs.push(...build(id));
|
|
485
|
+
return id;
|
|
486
|
+
}
|
|
487
|
+
gradientId(paint) {
|
|
488
|
+
return this.define('grad', JSON.stringify(paint), (id) => {
|
|
489
|
+
const stops = paint.stops.flatMap((stop) => el('stop', {
|
|
490
|
+
offset: num(stop.offset),
|
|
491
|
+
'stop-color': stop.hex,
|
|
492
|
+
'stop-opacity': stop.alpha < 1 ? num(stop.alpha) : undefined,
|
|
493
|
+
}));
|
|
494
|
+
// The canvas's two circles: the start one is SVG's focal circle, the end one the gradient's.
|
|
495
|
+
return paint.type === 'linear'
|
|
496
|
+
? el('linearGradient', {
|
|
497
|
+
id,
|
|
498
|
+
gradientUnits: 'userSpaceOnUse',
|
|
499
|
+
x1: num(paint.x0),
|
|
500
|
+
y1: num(paint.y0),
|
|
501
|
+
x2: num(paint.x1),
|
|
502
|
+
y2: num(paint.y1),
|
|
503
|
+
}, stops)
|
|
504
|
+
: el('radialGradient', {
|
|
505
|
+
id,
|
|
506
|
+
gradientUnits: 'userSpaceOnUse',
|
|
507
|
+
cx: num(paint.x1),
|
|
508
|
+
cy: num(paint.y1),
|
|
509
|
+
r: num(paint.r1),
|
|
510
|
+
fx: num(paint.x0),
|
|
511
|
+
fy: num(paint.y0),
|
|
512
|
+
fr: paint.r0 !== 0 ? num(paint.r0) : undefined,
|
|
513
|
+
}, stops);
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
/** One pattern per tile (by identity) and pattern matrix; null for a tile that was never resolved. */
|
|
517
|
+
patternId(paint) {
|
|
518
|
+
const tile = paint.image;
|
|
519
|
+
if (tile.kind === 'element')
|
|
520
|
+
return null;
|
|
521
|
+
let tileId = this.tileIds.get(tile);
|
|
522
|
+
if (tileId === undefined) {
|
|
523
|
+
tileId = this.tileIds.size;
|
|
524
|
+
this.tileIds.set(tile, tileId);
|
|
525
|
+
}
|
|
526
|
+
return this.define('pattern', `${tileId}\0${paint.matrix.join(',')}`, (id) => el('pattern', {
|
|
527
|
+
id,
|
|
528
|
+
patternUnits: 'userSpaceOnUse',
|
|
529
|
+
width: num(tile.width),
|
|
530
|
+
height: num(tile.height),
|
|
531
|
+
patternTransform: transformOf(paint.matrix),
|
|
532
|
+
}, el('image', {
|
|
533
|
+
width: num(tile.width),
|
|
534
|
+
height: num(tile.height),
|
|
535
|
+
preserveAspectRatio: 'none',
|
|
536
|
+
'xlink:href': dataUrl(tile),
|
|
537
|
+
})));
|
|
538
|
+
}
|
|
539
|
+
clipId(clip) {
|
|
540
|
+
let id = this.clipIds.get(clip);
|
|
541
|
+
if (id === undefined) {
|
|
542
|
+
id = this.newId('clip');
|
|
543
|
+
this.clipIds.set(clip, id);
|
|
544
|
+
this.defs.push(...el('clipPath', { id }, el('path', {
|
|
545
|
+
d: pathData(clip.commands),
|
|
546
|
+
transform: transformOf(clip.matrix),
|
|
547
|
+
'clip-rule': clip.rule === 'evenodd' ? 'evenodd' : undefined,
|
|
548
|
+
})));
|
|
549
|
+
}
|
|
550
|
+
return id;
|
|
551
|
+
}
|
|
552
|
+
rectClipId(x, y, width, height) {
|
|
553
|
+
const rect = { x: num(x), y: num(y), width: num(width), height: num(height) };
|
|
554
|
+
return this.define('clip', `rect\0${Object.values(rect).join(',')}`, (id) => el('clipPath', { id }, el('rect', rect)));
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* A canvas shadow is a Gaussian of sigma blur / 2, moved by the offsets, in page px. The
|
|
558
|
+
* region is the page plus the reach of the shadow, in user space: a percentage of the shape's
|
|
559
|
+
* box would be empty for a hairline and too small for a small shape with a long shadow.
|
|
560
|
+
*/
|
|
561
|
+
shadowId(shadow) {
|
|
562
|
+
return this.define('shadow', JSON.stringify(shadow), (id) => {
|
|
563
|
+
const sigma = shadow.blur / 2;
|
|
564
|
+
const reach = Math.ceil(Math.hypot(shadow.offsetX, shadow.offsetY) + 3 * sigma);
|
|
565
|
+
return el('filter', {
|
|
566
|
+
id,
|
|
567
|
+
filterUnits: 'userSpaceOnUse',
|
|
568
|
+
x: num(-reach),
|
|
569
|
+
y: num(-reach),
|
|
570
|
+
width: num(this.scene.width + 2 * reach),
|
|
571
|
+
height: num(this.scene.height + 2 * reach),
|
|
572
|
+
'color-interpolation-filters': 'sRGB',
|
|
573
|
+
}, [
|
|
574
|
+
...el('feGaussianBlur', { in: 'SourceAlpha', stdDeviation: num(sigma), result: 'blur' }),
|
|
575
|
+
...el('feOffset', {
|
|
576
|
+
in: 'blur',
|
|
577
|
+
dx: num(shadow.offsetX),
|
|
578
|
+
dy: num(shadow.offsetY),
|
|
579
|
+
result: 'offset',
|
|
580
|
+
}),
|
|
581
|
+
...el('feFlood', {
|
|
582
|
+
'flood-color': shadow.hex,
|
|
583
|
+
'flood-opacity': num(shadow.alpha),
|
|
584
|
+
result: 'flood',
|
|
585
|
+
}),
|
|
586
|
+
...el('feComposite', { in: 'flood', in2: 'offset', operator: 'in', result: 'shadow' }),
|
|
587
|
+
...el('feMerge', {}, [
|
|
588
|
+
...el('feMergeNode', { in: 'shadow' }),
|
|
589
|
+
...el('feMergeNode', { in: 'SourceGraphic' }),
|
|
590
|
+
]),
|
|
591
|
+
]);
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The few XML helpers the SVG writer and the SVG inliner share. */
|
|
2
|
+
/**
|
|
3
|
+
* For text and attribute values alike: all five characters, so no caller has to know which. The
|
|
4
|
+
* characters XML forbids are dropped.
|
|
5
|
+
*/
|
|
6
|
+
export declare function escapeXml(text: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* A text run as `escapeXml` writes it, with the x of every code point it drops dropped too: a
|
|
9
|
+
* `<text x="…">` takes one x per character, so a list that still counted the dropped ones would
|
|
10
|
+
* move every glyph after it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function withoutIllegalXml(run: {
|
|
13
|
+
text: string;
|
|
14
|
+
xs: number[];
|
|
15
|
+
}): {
|
|
16
|
+
text: string;
|
|
17
|
+
xs: number[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* At most `digits` decimals with the trailing zeros trimmed. Never `-0` (`String(-0)` is `'0'`)
|
|
21
|
+
* and never `NaN`: a non-finite value is a bug upstream, but it must not make the file invalid.
|
|
22
|
+
*/
|
|
23
|
+
export declare function formatNumber(value: number, digits?: number): string;
|