@open-artifact/export-kit 0.2.0 → 0.2.2
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/package.json +1 -1
- package/src/capture.d.ts +17 -0
- package/src/capture.js +90 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/export-kit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "What the editors' exports share in the browser: DOM capture to SVG and pictures, the PDF text layer measured from rendered pages, picture shrinking and font stylesheet helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
package/src/capture.d.ts
CHANGED
|
@@ -80,6 +80,23 @@ export interface CaptureArea {
|
|
|
80
80
|
width: number;
|
|
81
81
|
height: number;
|
|
82
82
|
}
|
|
83
|
+
export declare const XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
84
|
+
/**
|
|
85
|
+
* Puts every `<image>` link under `root` in the XLink namespace.
|
|
86
|
+
*
|
|
87
|
+
* dom-to-svg writes an image's address with `setAttribute('xlink:href', …)`,
|
|
88
|
+
* which in its XML document makes an attribute in no namespace. Its
|
|
89
|
+
* `inlineResources` reads `href.baseVal`, which only sees a namespaced
|
|
90
|
+
* `xlink:href` (or a plain `href`), so every picture failed with "No URL
|
|
91
|
+
* passed", kept its web address, and came out blank once the address was
|
|
92
|
+
* dropped. Once namespaced, inlining's own `setAttribute('xlink:href', data)`
|
|
93
|
+
* updates that same attribute.
|
|
94
|
+
*/
|
|
95
|
+
export declare function linkImages(root: Element): void;
|
|
96
|
+
/** `object-position` as an SVG alignment (`xMidYMid`); positions other than 0%, 50% and 100% are taken as the middle. */
|
|
97
|
+
export declare function objectAlign(position: string): string;
|
|
98
|
+
/** `object-fit` (and `object-position`) as an SVG `preserveAspectRatio`; null where SVG has no match (`none`). */
|
|
99
|
+
export declare function objectFitAspect(fit: string, position: string): string | null;
|
|
83
100
|
/**
|
|
84
101
|
* The element rebuilt as plain SVG by dom-to-svg (links dropped), its
|
|
85
102
|
* resources inlined. `area` defaults to the element's own box.
|
package/src/capture.js
CHANGED
|
@@ -170,6 +170,93 @@ export async function loadSvgImage(svg, options) {
|
|
|
170
170
|
await image.decode().catch(() => undefined);
|
|
171
171
|
return image;
|
|
172
172
|
}
|
|
173
|
+
export const XLINK_NS = 'http://www.w3.org/1999/xlink';
|
|
174
|
+
/**
|
|
175
|
+
* Puts every `<image>` link under `root` in the XLink namespace.
|
|
176
|
+
*
|
|
177
|
+
* dom-to-svg writes an image's address with `setAttribute('xlink:href', …)`,
|
|
178
|
+
* which in its XML document makes an attribute in no namespace. Its
|
|
179
|
+
* `inlineResources` reads `href.baseVal`, which only sees a namespaced
|
|
180
|
+
* `xlink:href` (or a plain `href`), so every picture failed with "No URL
|
|
181
|
+
* passed", kept its web address, and came out blank once the address was
|
|
182
|
+
* dropped. Once namespaced, inlining's own `setAttribute('xlink:href', data)`
|
|
183
|
+
* updates that same attribute.
|
|
184
|
+
*/
|
|
185
|
+
export function linkImages(root) {
|
|
186
|
+
for (const image of Array.from(root.getElementsByTagNameNS(SVG_NS, 'image'))) {
|
|
187
|
+
const plain = image.getAttributeNodeNS(null, 'xlink:href');
|
|
188
|
+
if (!plain)
|
|
189
|
+
continue;
|
|
190
|
+
image.removeAttributeNode(plain);
|
|
191
|
+
image.setAttributeNS(XLINK_NS, 'xlink:href', plain.value);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** `object-position` as an SVG alignment (`xMidYMid`); positions other than 0%, 50% and 100% are taken as the middle. */
|
|
195
|
+
export function objectAlign(position) {
|
|
196
|
+
const [x = '50%', y = '50%'] = position.trim().split(/\s+/);
|
|
197
|
+
const KEYWORDS = { left: 0, top: 0, center: 50, right: 100, bottom: 100 };
|
|
198
|
+
const axis = (value) => {
|
|
199
|
+
const at = KEYWORDS[value] ?? (value.endsWith('%') || Number.parseFloat(value) === 0 ? Number.parseFloat(value) : Number.NaN);
|
|
200
|
+
return at === 0 ? 'Min' : at === 100 ? 'Max' : 'Mid';
|
|
201
|
+
};
|
|
202
|
+
return `x${axis(x)}Y${axis(y)}`;
|
|
203
|
+
}
|
|
204
|
+
/** `object-fit` (and `object-position`) as an SVG `preserveAspectRatio`; null where SVG has no match (`none`). */
|
|
205
|
+
export function objectFitAspect(fit, position) {
|
|
206
|
+
switch (fit) {
|
|
207
|
+
case 'fill':
|
|
208
|
+
return 'none';
|
|
209
|
+
case 'cover':
|
|
210
|
+
return `${objectAlign(position)} slice`;
|
|
211
|
+
case 'contain':
|
|
212
|
+
case 'scale-down':
|
|
213
|
+
return `${objectAlign(position)} meet`;
|
|
214
|
+
default:
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** How each `<img>` under `element` fills its box, measured where dom-to-svg measures it. */
|
|
219
|
+
function imageFits(element) {
|
|
220
|
+
const view = element.ownerDocument.defaultView;
|
|
221
|
+
const fits = [];
|
|
222
|
+
const images = element.localName === 'img' ? [element] : Array.from(element.querySelectorAll('img'));
|
|
223
|
+
for (const image of images) {
|
|
224
|
+
const style = view.getComputedStyle(image);
|
|
225
|
+
const preserveAspectRatio = objectFitAspect(style.objectFit, style.objectPosition);
|
|
226
|
+
if (!preserveAspectRatio)
|
|
227
|
+
continue;
|
|
228
|
+
const rect = image.getBoundingClientRect();
|
|
229
|
+
const left = Number.parseFloat(style.paddingLeft) || 0;
|
|
230
|
+
const top = Number.parseFloat(style.paddingTop) || 0;
|
|
231
|
+
fits.push({
|
|
232
|
+
x: rect.x + left,
|
|
233
|
+
y: rect.y + top,
|
|
234
|
+
width: rect.width - left - (Number.parseFloat(style.paddingRight) || 0),
|
|
235
|
+
height: rect.height - top - (Number.parseFloat(style.paddingBottom) || 0),
|
|
236
|
+
preserveAspectRatio,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
return fits;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Gives each `<image>` dom-to-svg made for an `<img>` the picture's
|
|
243
|
+
* `object-fit`: it leaves SVG's default (`xMidYMid meet`), which letterboxes
|
|
244
|
+
* a cropped (`cover`) or stretched (`fill`, HTML's default) picture. Images
|
|
245
|
+
* are matched by position and size; background images already carry their
|
|
246
|
+
* own `preserveAspectRatio` and are left alone.
|
|
247
|
+
*/
|
|
248
|
+
function fitImages(root, fits) {
|
|
249
|
+
if (!fits.length)
|
|
250
|
+
return;
|
|
251
|
+
const near = (a, b) => a !== null && Math.abs(Number(a) - b) < 0.5;
|
|
252
|
+
for (const image of Array.from(root.getElementsByTagNameNS(SVG_NS, 'image'))) {
|
|
253
|
+
if (image.hasAttribute('preserveAspectRatio'))
|
|
254
|
+
continue;
|
|
255
|
+
const fit = fits.find((f) => near(image.getAttribute('x'), f.x) && near(image.getAttribute('y'), f.y) && near(image.getAttribute('width'), f.width) && near(image.getAttribute('height'), f.height));
|
|
256
|
+
if (fit)
|
|
257
|
+
image.setAttribute('preserveAspectRatio', fit.preserveAspectRatio);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
173
260
|
/**
|
|
174
261
|
* The element rebuilt as plain SVG by dom-to-svg (links dropped), its
|
|
175
262
|
* resources inlined. `area` defaults to the element's own box.
|
|
@@ -177,7 +264,10 @@ export async function loadSvgImage(svg, options) {
|
|
|
177
264
|
export async function elementSvg(element, area) {
|
|
178
265
|
const box = area ?? element.getBoundingClientRect();
|
|
179
266
|
const view = element.ownerDocument.defaultView;
|
|
267
|
+
const fits = imageFits(element);
|
|
180
268
|
const doc = withCaptureSafeDom(element, () => elementToSVG(element, { captureArea: new view.DOMRect(box.x, box.y, box.width, box.height), keepLinks: false }));
|
|
269
|
+
linkImages(doc.documentElement);
|
|
270
|
+
fitImages(doc.documentElement, fits);
|
|
181
271
|
await inlineResources(doc.documentElement);
|
|
182
272
|
return doc;
|
|
183
273
|
}
|