@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,119 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* Prepares the markup of an SVG image for placing inside another SVG document.
|
|
4
|
+
*
|
|
5
|
+
* Two inlined icons that both say `<linearGradient id="a">` would otherwise share one gradient,
|
|
6
|
+
* so every `id` is prefixed, and every reference to one is rewritten with it: `url(#a)` in any
|
|
7
|
+
* attribute or `<style>`, `href="#a"` and `xlink:href="#a"`. A reference to an id the markup does
|
|
8
|
+
* not define is rewritten too: left alone it could point at one of the document's own ids.
|
|
9
|
+
*
|
|
10
|
+
* Classes and `<style>` rules are scoped to the instance as well (`scopeCss.ts`); the root gets an
|
|
11
|
+
* id (its own, prefixed, or `<prefix>root`) for the rules to hang under. Markup whose CSS cannot
|
|
12
|
+
* be scoped safely is not inlined at all.
|
|
13
|
+
*
|
|
14
|
+
* The markup is serialised here and not by `XMLSerializer`, which writes `xmlns` on every top
|
|
15
|
+
* level node it is handed; this one gives the same bytes in every DOM.
|
|
16
|
+
*/
|
|
17
|
+
import { keyframeNames, rewriteUrls, scopeClassList, scopeDeclarations, scopeStylesheet, UnsafeCss, } from './scopeCss.js';
|
|
18
|
+
import { escapeXml } from './xml.js';
|
|
19
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
20
|
+
const XLINK_NS = 'http://www.w3.org/1999/xlink';
|
|
21
|
+
/** Elements whose text is content: whitespace between their children is not layout. */
|
|
22
|
+
const TEXTUAL = new Set(['text', 'tspan', 'textPath', 'style', 'script', 'title', 'desc']);
|
|
23
|
+
const ELEMENT_NODE = 1;
|
|
24
|
+
const TEXT_NODE = 3;
|
|
25
|
+
const CDATA_NODE = 4;
|
|
26
|
+
/**
|
|
27
|
+
* `attrs` are the attributes of the root element, with an `id` always (the caller places the
|
|
28
|
+
* viewport itself, from `viewBox`, `preserveAspectRatio`, `width` and `height`, and passes the
|
|
29
|
+
* rest on); `inner` is its content. Null when there is no `DOMParser`, the markup is not an SVG
|
|
30
|
+
* document, or its CSS cannot be scoped to the instance: the caller embeds it as an `<image>`.
|
|
31
|
+
*/
|
|
32
|
+
export function inlineSvg(markup, prefix) {
|
|
33
|
+
if (typeof DOMParser === 'undefined')
|
|
34
|
+
return null;
|
|
35
|
+
let document;
|
|
36
|
+
try {
|
|
37
|
+
document = new DOMParser().parseFromString(markup, 'image/svg+xml');
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const root = document.documentElement;
|
|
43
|
+
if (!root ||
|
|
44
|
+
root.namespaceURI !== SVG_NS ||
|
|
45
|
+
root.localName !== 'svg' ||
|
|
46
|
+
document.getElementsByTagName('parsererror').length > 0) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const scope = scopeOf(root, prefix);
|
|
50
|
+
try {
|
|
51
|
+
return {
|
|
52
|
+
attrs: { id: scope.scopeId, ...Object.fromEntries(attributesOf(root, scope)) },
|
|
53
|
+
inner: Array.from(root.childNodes, (child) => serialize(child, root, scope)).join(''),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (error instanceof UnsafeCss)
|
|
58
|
+
return null;
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function scopeOf(root, prefix) {
|
|
63
|
+
let name = root.getAttribute('id');
|
|
64
|
+
if (name === null) {
|
|
65
|
+
// A root without an id gets one that no element of the markup has.
|
|
66
|
+
const taken = new Set(Array.from(root.querySelectorAll('[id]'), (e) => e.getAttribute('id')));
|
|
67
|
+
name = 'root';
|
|
68
|
+
while (taken.has(name))
|
|
69
|
+
name += '_';
|
|
70
|
+
}
|
|
71
|
+
const scopeId = prefix + name;
|
|
72
|
+
const keyframes = new Set(Array.from(root.getElementsByTagName('style')).flatMap((style) => keyframeNames(style.textContent ?? '')));
|
|
73
|
+
return { prefix, scopeId, keyframes };
|
|
74
|
+
}
|
|
75
|
+
function attributesOf(element, scope) {
|
|
76
|
+
const kept = [];
|
|
77
|
+
for (const { name, value } of Array.from(element.attributes)) {
|
|
78
|
+
// The writer's root declares both namespaces; any other declaration stays where it is.
|
|
79
|
+
if (name === 'xmlns' && value === SVG_NS)
|
|
80
|
+
continue;
|
|
81
|
+
if (name === 'xmlns:xlink' && value === XLINK_NS)
|
|
82
|
+
continue;
|
|
83
|
+
if (name === 'id')
|
|
84
|
+
kept.push([name, scope.prefix + value]);
|
|
85
|
+
else if ((name === 'href' || name === 'xlink:href') && value.startsWith('#')) {
|
|
86
|
+
kept.push([name, `#${scope.prefix}${value.slice(1)}`]);
|
|
87
|
+
}
|
|
88
|
+
else if (name === 'class')
|
|
89
|
+
kept.push([name, scopeClassList(value, scope.prefix)]);
|
|
90
|
+
else if (name === 'style')
|
|
91
|
+
kept.push([name, scopeDeclarations(value, scope)]);
|
|
92
|
+
else
|
|
93
|
+
kept.push([name, rewriteUrls(value, scope.prefix)]);
|
|
94
|
+
}
|
|
95
|
+
return kept;
|
|
96
|
+
}
|
|
97
|
+
function serialize(node, parent, scope) {
|
|
98
|
+
if (node.nodeType === ELEMENT_NODE)
|
|
99
|
+
return serializeElement(node, scope);
|
|
100
|
+
if (node.nodeType !== TEXT_NODE && node.nodeType !== CDATA_NODE)
|
|
101
|
+
return ''; // comments, instructions
|
|
102
|
+
const text = node.nodeValue ?? '';
|
|
103
|
+
if (text.trim() === '' && !TEXTUAL.has(parent.localName))
|
|
104
|
+
return '';
|
|
105
|
+
return escapeXml(text);
|
|
106
|
+
}
|
|
107
|
+
function serializeElement(element, scope) {
|
|
108
|
+
const attributes = attributesOf(element, scope)
|
|
109
|
+
.map(([name, value]) => ` ${name}="${escapeXml(value)}"`)
|
|
110
|
+
.join('');
|
|
111
|
+
// A style sheet is scoped whole: a rule can straddle the text and CDATA nodes it is made of.
|
|
112
|
+
const children = element.localName === 'style'
|
|
113
|
+
? escapeXml(scopeStylesheet(element.textContent ?? '', scope))
|
|
114
|
+
: Array.from(element.childNodes, (child) => serialize(child, element, scope)).join('');
|
|
115
|
+
const name = element.tagName;
|
|
116
|
+
return children === ''
|
|
117
|
+
? `<${name}${attributes}/>`
|
|
118
|
+
: `<${name}${attributes}>${children}</${name}>`;
|
|
119
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the CSS of an inlined SVG to that SVG.
|
|
3
|
+
*
|
|
4
|
+
* A `<style>` in a nested `<svg>` is a style sheet of the whole output document, so two inlined
|
|
5
|
+
* illustrations that both say `.cls-1 { fill: … }`, or one that says `path { fill: red }`, would
|
|
6
|
+
* recolour each other and the rest of the page. Per instance (`prefix`, `i0-`):
|
|
7
|
+
*
|
|
8
|
+
* - a class is renamed, in `class` attributes and in selectors: `.a.b:hover` → `.i0-a.i0-b:hover`;
|
|
9
|
+
* - an id selector is renamed like the id it names;
|
|
10
|
+
* - every selector is put under the instance's root, so a type selector hits only its own
|
|
11
|
+
* elements: `path` → `#i0-root path`, and `svg` (the root itself) → `#i0-root`. Class and id
|
|
12
|
+
* selectors are scoped too, not only the type ones: a selector gains the same one id of
|
|
13
|
+
* specificity as every other, so rules of the instance keep their order among themselves;
|
|
14
|
+
* - `@keyframes` names and the `animation` values that use them are renamed, `@media` and
|
|
15
|
+
* `@supports` are rewritten inside, and `@font-face` stays as it is (a font name is global).
|
|
16
|
+
*
|
|
17
|
+
* This is a tokenizer for the shapes SVG tools write, not a CSS parser. What it cannot rewrite
|
|
18
|
+
* without guessing throws `UnsafeCss`, and the caller does not inline that SVG: embedded as an
|
|
19
|
+
* `<image>` it is still vector and cannot touch anything. That is: attribute selectors on `class`
|
|
20
|
+
* or `id`, `:is()`, `:where()`, `:not()`, `:has()`, `:root`, escapes, namespaces, nesting, `@import`
|
|
21
|
+
* and at-rules that are none of the above.
|
|
22
|
+
*
|
|
23
|
+
* A class or id selector that targets the root element itself (`.cls-0` on `<svg class="cls-0">`)
|
|
24
|
+
* no longer matches: its selector now asks for an element inside the root.
|
|
25
|
+
*/
|
|
26
|
+
export declare class UnsafeCss extends Error {
|
|
27
|
+
constructor(reason: string);
|
|
28
|
+
}
|
|
29
|
+
export interface CssScope {
|
|
30
|
+
/** Put before every class, id and keyframes name: `i0-`. */
|
|
31
|
+
prefix: string;
|
|
32
|
+
/** The id of the instance's root element, as written into the output (prefix included). */
|
|
33
|
+
scopeId: string;
|
|
34
|
+
/** Names of the `@keyframes` of the whole instance, as the markup wrote them. */
|
|
35
|
+
keyframes: ReadonlySet<string>;
|
|
36
|
+
}
|
|
37
|
+
/** `url(#a)`, `url('#a')` and `url("#a")` in a property value or a `style` attribute. */
|
|
38
|
+
export declare function rewriteUrls(value: string, prefix: string): string;
|
|
39
|
+
/** `class="a b"` → `class="i0-a i0-b"`. */
|
|
40
|
+
export declare function scopeClassList(value: string, prefix: string): string;
|
|
41
|
+
/** The names `@keyframes` declares in a style sheet. */
|
|
42
|
+
export declare function keyframeNames(css: string): string[];
|
|
43
|
+
/** Declarations (a rule body or a `style` attribute): url references and animation names. */
|
|
44
|
+
export declare function scopeDeclarations(declarations: string, scope: CssScope): string;
|
|
45
|
+
export declare function scopeStylesheet(css: string, scope: CssScope): string;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* Keeps the CSS of an inlined SVG to that SVG.
|
|
4
|
+
*
|
|
5
|
+
* A `<style>` in a nested `<svg>` is a style sheet of the whole output document, so two inlined
|
|
6
|
+
* illustrations that both say `.cls-1 { fill: … }`, or one that says `path { fill: red }`, would
|
|
7
|
+
* recolour each other and the rest of the page. Per instance (`prefix`, `i0-`):
|
|
8
|
+
*
|
|
9
|
+
* - a class is renamed, in `class` attributes and in selectors: `.a.b:hover` → `.i0-a.i0-b:hover`;
|
|
10
|
+
* - an id selector is renamed like the id it names;
|
|
11
|
+
* - every selector is put under the instance's root, so a type selector hits only its own
|
|
12
|
+
* elements: `path` → `#i0-root path`, and `svg` (the root itself) → `#i0-root`. Class and id
|
|
13
|
+
* selectors are scoped too, not only the type ones: a selector gains the same one id of
|
|
14
|
+
* specificity as every other, so rules of the instance keep their order among themselves;
|
|
15
|
+
* - `@keyframes` names and the `animation` values that use them are renamed, `@media` and
|
|
16
|
+
* `@supports` are rewritten inside, and `@font-face` stays as it is (a font name is global).
|
|
17
|
+
*
|
|
18
|
+
* This is a tokenizer for the shapes SVG tools write, not a CSS parser. What it cannot rewrite
|
|
19
|
+
* without guessing throws `UnsafeCss`, and the caller does not inline that SVG: embedded as an
|
|
20
|
+
* `<image>` it is still vector and cannot touch anything. That is: attribute selectors on `class`
|
|
21
|
+
* or `id`, `:is()`, `:where()`, `:not()`, `:has()`, `:root`, escapes, namespaces, nesting, `@import`
|
|
22
|
+
* and at-rules that are none of the above.
|
|
23
|
+
*
|
|
24
|
+
* A class or id selector that targets the root element itself (`.cls-0` on `<svg class="cls-0">`)
|
|
25
|
+
* no longer matches: its selector now asks for an element inside the root.
|
|
26
|
+
*/
|
|
27
|
+
export class UnsafeCss extends Error {
|
|
28
|
+
constructor(reason) {
|
|
29
|
+
super(`css of an inlined svg cannot be scoped: ${reason}`);
|
|
30
|
+
this.name = 'UnsafeCss';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** `url(#a)`, `url('#a')` and `url("#a")` in a property value or a `style` attribute. */
|
|
34
|
+
export function rewriteUrls(value, prefix) {
|
|
35
|
+
return value.replace(/url\(\s*(['"]?)#([^)'"\s]*)\1\s*\)/g, (_match, quote, id) => `url(${quote}#${prefix}${id}${quote})`);
|
|
36
|
+
}
|
|
37
|
+
/** `class="a b"` → `class="i0-a i0-b"`. */
|
|
38
|
+
export function scopeClassList(value, prefix) {
|
|
39
|
+
return value
|
|
40
|
+
.split(/\s+/)
|
|
41
|
+
.filter((name) => name !== '')
|
|
42
|
+
.map((name) => prefix + name)
|
|
43
|
+
.join(' ');
|
|
44
|
+
}
|
|
45
|
+
/** The names `@keyframes` declares in a style sheet. */
|
|
46
|
+
export function keyframeNames(css) {
|
|
47
|
+
return Array.from(css.matchAll(/@(?:-[a-z]+-)?keyframes\s+([\w-]+)/gi), (match) => match[1]);
|
|
48
|
+
}
|
|
49
|
+
/** Declarations (a rule body or a `style` attribute): url references and animation names. */
|
|
50
|
+
export function scopeDeclarations(declarations, scope) {
|
|
51
|
+
return rewriteUrls(declarations, scope.prefix).replace(/(animation(?:-name)?\s*:)([^;}]*)/gi, (_match, head, value) => head +
|
|
52
|
+
value.replace(/[\w-]+/g, (word) => (scope.keyframes.has(word) ? scope.prefix + word : word)));
|
|
53
|
+
}
|
|
54
|
+
export function scopeStylesheet(css, scope) {
|
|
55
|
+
const text = css.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
56
|
+
const rules = [];
|
|
57
|
+
let at = 0;
|
|
58
|
+
while (at < text.length) {
|
|
59
|
+
if (/\s/.test(text[at])) {
|
|
60
|
+
at++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const open = find(text, at, '{;');
|
|
64
|
+
if (open < 0 || text[open] === ';')
|
|
65
|
+
throw new UnsafeCss('a statement, not a rule');
|
|
66
|
+
const close = blockEnd(text, open);
|
|
67
|
+
if (close < 0)
|
|
68
|
+
throw new UnsafeCss('an unclosed block');
|
|
69
|
+
rules.push(scopeRule(text.slice(at, open).trim(), text.slice(open + 1, close), scope));
|
|
70
|
+
at = close + 1;
|
|
71
|
+
}
|
|
72
|
+
return rules.join(' ');
|
|
73
|
+
}
|
|
74
|
+
function scopeRule(prelude, body, scope) {
|
|
75
|
+
if (prelude.startsWith('@')) {
|
|
76
|
+
const match = /^@(?:-[a-z]+-)?([\w-]+)\s*([\s\S]*)$/i.exec(prelude);
|
|
77
|
+
const name = match?.[1]?.toLowerCase();
|
|
78
|
+
const rest = match?.[2] ?? '';
|
|
79
|
+
switch (name) {
|
|
80
|
+
case 'font-face':
|
|
81
|
+
return `${prelude} {${body}}`;
|
|
82
|
+
case 'keyframes': {
|
|
83
|
+
if (!/^[\w-]+$/.test(rest))
|
|
84
|
+
throw new UnsafeCss(`keyframes name "${rest}"`);
|
|
85
|
+
const keyword = prelude.slice(0, prelude.length - rest.length);
|
|
86
|
+
return `${keyword}${scope.prefix}${rest} {${rewriteUrls(body, scope.prefix)}}`;
|
|
87
|
+
}
|
|
88
|
+
case 'media':
|
|
89
|
+
case 'supports':
|
|
90
|
+
return `${prelude} { ${scopeStylesheet(body, scope)} }`;
|
|
91
|
+
default:
|
|
92
|
+
throw new UnsafeCss(`@${name ?? ''}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (body.includes('{'))
|
|
96
|
+
throw new UnsafeCss('a nested rule');
|
|
97
|
+
const selectors = splitList(prelude).map((selector) => scopeSelector(selector, scope));
|
|
98
|
+
return `${selectors.join(', ')} {${scopeDeclarations(body, scope)}}`;
|
|
99
|
+
}
|
|
100
|
+
const UNSAFE_PSEUDO = /:(?:is|where|not|has|matches|any|root|host|host-context|scope|global|local)\b|:-(?:webkit|moz)-any\b/i;
|
|
101
|
+
const NTH_ARGUMENT = /^(?:[\s\dn+-]*|\s*(?:odd|even)\s*)$/i;
|
|
102
|
+
function scopeSelector(selector, scope) {
|
|
103
|
+
const text = selector.trim();
|
|
104
|
+
if (text === '' || /^[>+~]/.test(text))
|
|
105
|
+
throw new UnsafeCss(`selector "${selector}"`);
|
|
106
|
+
if (/[\\&]/.test(text) || UNSAFE_PSEUDO.test(text))
|
|
107
|
+
throw new UnsafeCss(`selector "${text}"`);
|
|
108
|
+
let out = '';
|
|
109
|
+
for (let at = 0; at < text.length;) {
|
|
110
|
+
const char = text[at];
|
|
111
|
+
if (char === '[') {
|
|
112
|
+
const end = find(text, at + 1, ']');
|
|
113
|
+
if (end < 0)
|
|
114
|
+
throw new UnsafeCss(`selector "${text}"`);
|
|
115
|
+
const attribute = text.slice(at, end + 1);
|
|
116
|
+
// `[class~=a]` and `[id=a]` name what is being renamed.
|
|
117
|
+
if (/^\[\s*(?:[\w-]*\|)?(?:class|id)\b/i.test(attribute)) {
|
|
118
|
+
throw new UnsafeCss(`attribute selector ${attribute}`);
|
|
119
|
+
}
|
|
120
|
+
out += attribute;
|
|
121
|
+
at = end + 1;
|
|
122
|
+
}
|
|
123
|
+
else if (char === '.' || char === '#') {
|
|
124
|
+
const name = /^[.#](-?[_a-zA-Z][\w-]*)/.exec(text.slice(at))?.[1];
|
|
125
|
+
if (name === undefined)
|
|
126
|
+
throw new UnsafeCss(`selector "${text}"`);
|
|
127
|
+
out += `${char}${scope.prefix}${name}`;
|
|
128
|
+
at += 1 + name.length;
|
|
129
|
+
}
|
|
130
|
+
else if (char === '(') {
|
|
131
|
+
// Only the position functions: `:nth-child(2n+1 of .a)` has a selector in it.
|
|
132
|
+
const end = text.indexOf(')', at);
|
|
133
|
+
if (end < 0 || !/:nth-(?:last-)?(?:child|of-type)$/.test(out)) {
|
|
134
|
+
throw new UnsafeCss(`selector "${text}"`);
|
|
135
|
+
}
|
|
136
|
+
if (!NTH_ARGUMENT.test(text.slice(at + 1, end)))
|
|
137
|
+
throw new UnsafeCss(`selector "${text}"`);
|
|
138
|
+
out += text.slice(at, end + 1);
|
|
139
|
+
at = end + 1;
|
|
140
|
+
}
|
|
141
|
+
else if (char === '|') {
|
|
142
|
+
throw new UnsafeCss(`namespace in "${text}"`);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
out += char;
|
|
146
|
+
at++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const rootId = `#${scope.scopeId}`;
|
|
150
|
+
if (/^svg(?![\w-])/i.test(out))
|
|
151
|
+
return rootId + out.slice(3);
|
|
152
|
+
if (out.startsWith(rootId) && !/[\w-]/.test(out[rootId.length] ?? ''))
|
|
153
|
+
return out;
|
|
154
|
+
return `${rootId} ${out}`;
|
|
155
|
+
}
|
|
156
|
+
// -- the little scanner ----------------------------------------------------------------------
|
|
157
|
+
/**
|
|
158
|
+
* The index of the first of `stops` outside quotes, brackets and parentheses, or -1. A `]` stop
|
|
159
|
+
* is looked for at depth 0 too: the caller starts after the `[`.
|
|
160
|
+
*/
|
|
161
|
+
function find(text, from, stops) {
|
|
162
|
+
let quote = '';
|
|
163
|
+
let depth = 0;
|
|
164
|
+
for (let at = from; at < text.length; at++) {
|
|
165
|
+
const char = text[at];
|
|
166
|
+
if (quote) {
|
|
167
|
+
if (char === '\\')
|
|
168
|
+
at++;
|
|
169
|
+
else if (char === quote)
|
|
170
|
+
quote = '';
|
|
171
|
+
}
|
|
172
|
+
else if (char === '"' || char === "'")
|
|
173
|
+
quote = char;
|
|
174
|
+
else if (depth <= 0 && stops.includes(char))
|
|
175
|
+
return at;
|
|
176
|
+
else if (char === '[' || char === '(')
|
|
177
|
+
depth++;
|
|
178
|
+
else if (char === ']' || char === ')')
|
|
179
|
+
depth--;
|
|
180
|
+
}
|
|
181
|
+
return -1;
|
|
182
|
+
}
|
|
183
|
+
/** The `}` that closes the `{` at `open`, or -1. */
|
|
184
|
+
function blockEnd(text, open) {
|
|
185
|
+
let quote = '';
|
|
186
|
+
let depth = 0;
|
|
187
|
+
for (let at = open; at < text.length; at++) {
|
|
188
|
+
const char = text[at];
|
|
189
|
+
if (quote) {
|
|
190
|
+
if (char === '\\')
|
|
191
|
+
at++;
|
|
192
|
+
else if (char === quote)
|
|
193
|
+
quote = '';
|
|
194
|
+
}
|
|
195
|
+
else if (char === '"' || char === "'")
|
|
196
|
+
quote = char;
|
|
197
|
+
else if (char === '{')
|
|
198
|
+
depth++;
|
|
199
|
+
else if (char === '}' && --depth === 0)
|
|
200
|
+
return at;
|
|
201
|
+
}
|
|
202
|
+
return -1;
|
|
203
|
+
}
|
|
204
|
+
/** A selector list split at its top-level commas. */
|
|
205
|
+
function splitList(list) {
|
|
206
|
+
const parts = [];
|
|
207
|
+
let start = 0;
|
|
208
|
+
for (let comma = find(list, 0, ','); comma >= 0; comma = find(list, start, ',')) {
|
|
209
|
+
parts.push(list.slice(start, comma));
|
|
210
|
+
start = comma + 1;
|
|
211
|
+
}
|
|
212
|
+
parts.push(list.slice(start));
|
|
213
|
+
return parts;
|
|
214
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The colours an SVG can say: what the recorder reads for a canvas `fillStyle` (hex, `rgb()`,
|
|
3
|
+
* `transparent`) plus the CSS colour names, which the recorder leaves to the canvas. There is no
|
|
4
|
+
* canvas here: the converter has to give the same answer in a worker and in a test.
|
|
5
|
+
*/
|
|
6
|
+
import { type Rgba } from '../scene/recordingContext.js';
|
|
7
|
+
/** `null` for what is not a colour this converter knows (`hsl()`, `color-mix()`, a typo). */
|
|
8
|
+
export declare function parseSvgColor(value: string): Rgba | null;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* The colours an SVG can say: what the recorder reads for a canvas `fillStyle` (hex, `rgb()`,
|
|
4
|
+
* `transparent`) plus the CSS colour names, which the recorder leaves to the canvas. There is no
|
|
5
|
+
* canvas here: the converter has to give the same answer in a worker and in a test.
|
|
6
|
+
*/
|
|
7
|
+
import { parseCssColor } from '../scene/recordingContext.js';
|
|
8
|
+
/** The CSS named colours, `name:rrggbb`. */
|
|
9
|
+
const NAMED = new Map(('aliceblue:f0f8ff,antiquewhite:faebd7,aqua:00ffff,aquamarine:7fffd4,azure:f0ffff,' +
|
|
10
|
+
'beige:f5f5dc,bisque:ffe4c4,black:000000,blanchedalmond:ffebcd,blue:0000ff,blueviolet:8a2be2,' +
|
|
11
|
+
'brown:a52a2a,burlywood:deb887,cadetblue:5f9ea0,chartreuse:7fff00,chocolate:d2691e,' +
|
|
12
|
+
'coral:ff7f50,cornflowerblue:6495ed,cornsilk:fff8dc,crimson:dc143c,cyan:00ffff,' +
|
|
13
|
+
'darkblue:00008b,darkcyan:008b8b,darkgoldenrod:b8860b,darkgray:a9a9a9,darkgreen:006400,' +
|
|
14
|
+
'darkgrey:a9a9a9,darkkhaki:bdb76b,darkmagenta:8b008b,darkolivegreen:556b2f,darkorange:ff8c00,' +
|
|
15
|
+
'darkorchid:9932cc,darkred:8b0000,darksalmon:e9967a,darkseagreen:8fbc8f,darkslateblue:483d8b,' +
|
|
16
|
+
'darkslategray:2f4f4f,darkslategrey:2f4f4f,darkturquoise:00ced1,darkviolet:9400d3,' +
|
|
17
|
+
'deeppink:ff1493,deepskyblue:00bfff,dimgray:696969,dimgrey:696969,dodgerblue:1e90ff,' +
|
|
18
|
+
'firebrick:b22222,floralwhite:fffaf0,forestgreen:228b22,fuchsia:ff00ff,gainsboro:dcdcdc,' +
|
|
19
|
+
'ghostwhite:f8f8ff,gold:ffd700,goldenrod:daa520,gray:808080,green:008000,greenyellow:adff2f,' +
|
|
20
|
+
'grey:808080,honeydew:f0fff0,hotpink:ff69b4,indianred:cd5c5c,indigo:4b0082,ivory:fffff0,' +
|
|
21
|
+
'khaki:f0e68c,lavender:e6e6fa,lavenderblush:fff0f5,lawngreen:7cfc00,lemonchiffon:fffacd,' +
|
|
22
|
+
'lightblue:add8e6,lightcoral:f08080,lightcyan:e0ffff,lightgoldenrodyellow:fafad2,' +
|
|
23
|
+
'lightgray:d3d3d3,lightgreen:90ee90,lightgrey:d3d3d3,lightpink:ffb6c1,lightsalmon:ffa07a,' +
|
|
24
|
+
'lightseagreen:20b2aa,lightskyblue:87cefa,lightslategray:778899,lightslategrey:778899,' +
|
|
25
|
+
'lightsteelblue:b0c4de,lightyellow:ffffe0,lime:00ff00,limegreen:32cd32,linen:faf0e6,' +
|
|
26
|
+
'magenta:ff00ff,maroon:800000,mediumaquamarine:66cdaa,mediumblue:0000cd,mediumorchid:ba55d3,' +
|
|
27
|
+
'mediumpurple:9370db,mediumseagreen:3cb371,mediumslateblue:7b68ee,mediumspringgreen:00fa9a,' +
|
|
28
|
+
'mediumturquoise:48d1cc,mediumvioletred:c71585,midnightblue:191970,mintcream:f5fffa,' +
|
|
29
|
+
'mistyrose:ffe4e1,moccasin:ffe4b5,navajowhite:ffdead,navy:000080,oldlace:fdf5e6,olive:808000,' +
|
|
30
|
+
'olivedrab:6b8e23,orange:ffa500,orangered:ff4500,orchid:da70d6,palegoldenrod:eee8aa,' +
|
|
31
|
+
'palegreen:98fb98,paleturquoise:afeeee,palevioletred:db7093,papayawhip:ffefd5,' +
|
|
32
|
+
'peachpuff:ffdab9,peru:cd853f,pink:ffc0cb,plum:dda0dd,powderblue:b0e0e6,purple:800080,' +
|
|
33
|
+
'rebeccapurple:663399,red:ff0000,rosybrown:bc8f8f,royalblue:4169e1,' +
|
|
34
|
+
'saddlebrown:8b4513,salmon:fa8072,sandybrown:f4a460,seagreen:2e8b57,seashell:fff5ee,' +
|
|
35
|
+
'sienna:a0522d,silver:c0c0c0,skyblue:87ceeb,slateblue:6a5acd,slategray:708090,' +
|
|
36
|
+
'slategrey:708090,snow:fffafa,springgreen:00ff7f,steelblue:4682b4,tan:d2b48c,teal:008080,' +
|
|
37
|
+
'thistle:d8bfd8,tomato:ff6347,turquoise:40e0d0,violet:ee82ee,wheat:f5deb3,white:ffffff,' +
|
|
38
|
+
'whitesmoke:f5f5f5,yellow:ffff00,yellowgreen:9acd32')
|
|
39
|
+
.split(',')
|
|
40
|
+
.map((entry) => entry.split(':')));
|
|
41
|
+
/** `null` for what is not a colour this converter knows (`hsl()`, `color-mix()`, a typo). */
|
|
42
|
+
export function parseSvgColor(value) {
|
|
43
|
+
const v = value.trim().toLowerCase();
|
|
44
|
+
const named = NAMED.get(v);
|
|
45
|
+
return named ? { hex: `#${named}`, alpha: 1 } : parseCssColor(v);
|
|
46
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type SvgToSceneDefaults } from './svgToScene.js';
|
|
2
|
+
import type { FontFileSet } from '../fonts/fontFiles.js';
|
|
3
|
+
import type { ClipPath, ExportScene, FontSpec, SceneImageSource, SceneOp } from '../scene/types.js';
|
|
4
|
+
type ImageOp = Extract<SceneOp, {
|
|
5
|
+
kind: 'image';
|
|
6
|
+
}>;
|
|
7
|
+
type GroupOp = Extract<SceneOp, {
|
|
8
|
+
kind: 'group';
|
|
9
|
+
}>;
|
|
10
|
+
export type SvgSource = Extract<SceneImageSource, {
|
|
11
|
+
kind: 'svg';
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the markup has an element named `text` (with or without a namespace prefix): the only
|
|
15
|
+
* thing an SVG can set text with. Text in a picture is the one thing a writer outlines or embeds
|
|
16
|
+
* a font for, so a picture without any needs none of what follows.
|
|
17
|
+
*/
|
|
18
|
+
export declare function svgHasText(markup: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The picture of `op` as the ops `svgToScene` makes of it, in one group. `outerClips` are the
|
|
21
|
+
* clips the group stands in: the image op's own for a writer that opens the group like any other
|
|
22
|
+
* op (the PDF writer), none for one that has put them around the op already (the SVG writer). The
|
|
23
|
+
* crop is added to them when the source rect is not the whole picture. The group carries the op's
|
|
24
|
+
* alpha and no shadow, which is the caller's to make for the image op.
|
|
25
|
+
*
|
|
26
|
+
* `unsupported` is not empty when the picture uses what the converter cannot reproduce; the ops
|
|
27
|
+
* are then best effort and the caller draws the picture another way.
|
|
28
|
+
*/
|
|
29
|
+
export declare function svgImageGroup(op: ImageOp, source: SvgSource, defaults: SvgToSceneDefaults, outerClips: readonly ClipPath[]): {
|
|
30
|
+
group: GroupOp;
|
|
31
|
+
unsupported: string[];
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* What an SVG's text inherits where it names nothing: the family (and its fallbacks) the scenes
|
|
35
|
+
* draw most text in, at the default weight and size. Charts say `sans-serif`, which means "the
|
|
36
|
+
* document's font" here.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mostUsedFont(pages: readonly ExportScene[]): FontSpec;
|
|
39
|
+
/** `svgToScene`'s defaults for a picture drawn with `fonts`, inheriting `font`. */
|
|
40
|
+
export declare function svgDefaultsFor(fonts: ReadonlyMap<string, FontFileSet>, font: FontSpec): SvgToSceneDefaults;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* An SVG image op as scene ops, for the writers that draw an SVG picture's shapes and text
|
|
4
|
+
* themselves rather than embedding the picture: the PDF writer always, the SVG writer for a
|
|
5
|
+
* picture with text of its own in outline mode (a chart), so that text is outlined like the
|
|
6
|
+
* page's own and no font travels in the file.
|
|
7
|
+
*
|
|
8
|
+
* - `svgImageGroup` puts `svgToScene`'s ops where the canvas would have drawn the picture: at its
|
|
9
|
+
* natural size, stretched onto the destination rect by the matrix, cropped by a clip, with the
|
|
10
|
+
* image op's alpha and clips.
|
|
11
|
+
* - `svgDefaultsFor` and `mostUsedFont` say what the picture's text inherits where it names no
|
|
12
|
+
* family, and how wide its glyphs are, from the font files the export resolved.
|
|
13
|
+
*/
|
|
14
|
+
import { advanceOf } from '../fonts/glyphs.js';
|
|
15
|
+
import { IDENTITY, multiply } from '../scene/matrix.js';
|
|
16
|
+
import { svgToScene } from './svgToScene.js';
|
|
17
|
+
import { formatNumber } from './xml.js';
|
|
18
|
+
/**
|
|
19
|
+
* Whether the markup has an element named `text` (with or without a namespace prefix): the only
|
|
20
|
+
* thing an SVG can set text with. Text in a picture is the one thing a writer outlines or embeds
|
|
21
|
+
* a font for, so a picture without any needs none of what follows.
|
|
22
|
+
*/
|
|
23
|
+
export function svgHasText(markup) {
|
|
24
|
+
return /<(?:[\w-]+:)?text[\s/>]/i.test(markup);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The picture of `op` as the ops `svgToScene` makes of it, in one group. `outerClips` are the
|
|
28
|
+
* clips the group stands in: the image op's own for a writer that opens the group like any other
|
|
29
|
+
* op (the PDF writer), none for one that has put them around the op already (the SVG writer). The
|
|
30
|
+
* crop is added to them when the source rect is not the whole picture. The group carries the op's
|
|
31
|
+
* alpha and no shadow, which is the caller's to make for the image op.
|
|
32
|
+
*
|
|
33
|
+
* `unsupported` is not empty when the picture uses what the converter cannot reproduce; the ops
|
|
34
|
+
* are then best effort and the caller draws the picture another way.
|
|
35
|
+
*/
|
|
36
|
+
export function svgImageGroup(op, source, defaults, outerClips) {
|
|
37
|
+
const kx = op.dw / op.sw;
|
|
38
|
+
const ky = op.dh / op.sh;
|
|
39
|
+
// The picture at its natural size, stretched onto the destination rect; a crop is a clip.
|
|
40
|
+
const matrix = multiply(op.matrix, multiply([1, 0, 0, 1, op.dx, op.dy], multiply([kx, 0, 0, ky, 0, 0], [1, 0, 0, 1, -op.sx, -op.sy])));
|
|
41
|
+
const { ops, unsupported } = svgToScene(source.markup, { matrix, width: source.width, height: source.height }, defaults);
|
|
42
|
+
const whole = formatNumber(op.sx) === '0' &&
|
|
43
|
+
formatNumber(op.sy) === '0' &&
|
|
44
|
+
formatNumber(op.sw) === formatNumber(source.width) &&
|
|
45
|
+
formatNumber(op.sh) === formatNumber(source.height);
|
|
46
|
+
const crop = {
|
|
47
|
+
matrix: op.matrix,
|
|
48
|
+
commands: [
|
|
49
|
+
{ op: 'M', x: op.dx, y: op.dy },
|
|
50
|
+
{ op: 'L', x: op.dx + op.dw, y: op.dy },
|
|
51
|
+
{ op: 'L', x: op.dx + op.dw, y: op.dy + op.dh },
|
|
52
|
+
{ op: 'L', x: op.dx, y: op.dy + op.dh },
|
|
53
|
+
{ op: 'Z' },
|
|
54
|
+
],
|
|
55
|
+
rule: 'nonzero',
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
group: {
|
|
59
|
+
kind: 'group',
|
|
60
|
+
matrix: IDENTITY,
|
|
61
|
+
alpha: op.alpha,
|
|
62
|
+
clips: whole ? outerClips : [...outerClips, crop],
|
|
63
|
+
shadow: null,
|
|
64
|
+
ops,
|
|
65
|
+
},
|
|
66
|
+
unsupported,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// ---------------------------------------------------------------------------------------------
|
|
70
|
+
// What the picture's text inherits
|
|
71
|
+
const faceKey = (font) => `${font.family}|${font.weight}|${font.style}`;
|
|
72
|
+
function* textFonts(ops) {
|
|
73
|
+
for (const op of ops) {
|
|
74
|
+
if (op.kind === 'text')
|
|
75
|
+
yield op.font;
|
|
76
|
+
else if (op.kind === 'group')
|
|
77
|
+
yield* textFonts(op.ops);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* What an SVG's text inherits where it names nothing: the family (and its fallbacks) the scenes
|
|
82
|
+
* draw most text in, at the default weight and size. Charts say `sans-serif`, which means "the
|
|
83
|
+
* document's font" here.
|
|
84
|
+
*/
|
|
85
|
+
export function mostUsedFont(pages) {
|
|
86
|
+
const counts = new Map();
|
|
87
|
+
for (const scene of pages) {
|
|
88
|
+
for (const item of scene.items) {
|
|
89
|
+
for (const font of textFonts(item.ops)) {
|
|
90
|
+
const known = counts.get(font.family);
|
|
91
|
+
if (known)
|
|
92
|
+
known.count++;
|
|
93
|
+
else
|
|
94
|
+
counts.set(font.family, { font, count: 1 });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let best = null;
|
|
99
|
+
for (const entry of counts.values())
|
|
100
|
+
if (!best || entry.count > best.count)
|
|
101
|
+
best = entry;
|
|
102
|
+
return {
|
|
103
|
+
family: best?.font.family ?? 'sans-serif',
|
|
104
|
+
...(best?.font.fallback && { fallback: best.font.fallback }),
|
|
105
|
+
weight: 400,
|
|
106
|
+
style: 'normal',
|
|
107
|
+
size: 16,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The advance of every code point of `text`, from the font files, in px: svgToScene places SVG
|
|
112
|
+
* text by it, and the writer then sets the glyphs at those places. A code point no file has gets
|
|
113
|
+
* 0.6 em, the converter's own guess.
|
|
114
|
+
*/
|
|
115
|
+
function measure(fonts, text, font) {
|
|
116
|
+
const set = fonts.get(faceKey(font));
|
|
117
|
+
return Array.from(text, (char) => {
|
|
118
|
+
const codePoint = char.codePointAt(0) ?? 0;
|
|
119
|
+
const file = set?.fileFor(codePoint);
|
|
120
|
+
return file
|
|
121
|
+
? (advanceOf(file.font, codePoint) * font.size) / file.font.unitsPerEm
|
|
122
|
+
: font.size * 0.6;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/** `svgToScene`'s defaults for a picture drawn with `fonts`, inheriting `font`. */
|
|
126
|
+
export function svgDefaultsFor(fonts, font) {
|
|
127
|
+
return { font, measure: (text, face) => measure(fonts, text, face) };
|
|
128
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Matrix, PathCommand } from '../scene/types.js';
|
|
2
|
+
export interface Bounds {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
/** A whitespace/comma separated list of numbers; `null` if anything in it is not a number. */
|
|
9
|
+
export declare function parseNumberList(value: string): number[] | null;
|
|
10
|
+
/**
|
|
11
|
+
* An SVG elliptical arc (endpoint parameterisation, SVG 1.1 F.6.5) as cubics. The caller is
|
|
12
|
+
* already at `(x1, y1)`. A zero radius is a line, radii too small for the chord are scaled up.
|
|
13
|
+
*/
|
|
14
|
+
export declare function arcCommands(x1: number, y1: number, rxIn: number, ryIn: number, rotationDegrees: number, largeArc: boolean, sweep: boolean, x2: number, y2: number): PathCommand[];
|
|
15
|
+
/**
|
|
16
|
+
* The commands of an SVG `d` attribute. Data after the first error is dropped and what came
|
|
17
|
+
* before it is kept, as the SVG error handling says. Relative commands are made absolute;
|
|
18
|
+
* `H`/`V`/`S`/`T`/`A` become `L`/`C`/`Q`/`C`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parsePathData(data: string): PathCommand[];
|
|
21
|
+
/** A rectangle, with corner radii when `rx`/`ry` are positive (already clamped by the caller). */
|
|
22
|
+
export declare function rectCommands(x: number, y: number, width: number, height: number, rx?: number, ry?: number): PathCommand[];
|
|
23
|
+
/** A full ellipse, starting at its right-most point and running clockwise on screen. */
|
|
24
|
+
export declare function ellipseCommands(cx: number, cy: number, rx: number, ry: number): PathCommand[];
|
|
25
|
+
/** `points` of a polyline/polygon; an odd trailing number is dropped. */
|
|
26
|
+
export declare function pointsCommands(points: string, close: boolean): PathCommand[];
|
|
27
|
+
export declare function transformCommands(commands: readonly PathCommand[], m: Matrix): PathCommand[];
|
|
28
|
+
/** The exact bounding box of the geometry (curve extrema included), or `null` for no points. */
|
|
29
|
+
export declare function commandBounds(commands: readonly PathCommand[]): Bounds | null;
|
|
30
|
+
/** The matrix of an SVG `transform` list; `null` when it does not parse. */
|
|
31
|
+
export declare function parseTransform(value: string): Matrix | null;
|