-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathsvg.ts
More file actions
640 lines (546 loc) · 17.6 KB
/
Copy pathsvg.ts
File metadata and controls
640 lines (546 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import { ElementTypeEnum } from '../constants';
import {
createElement,
getElementByRole,
getViewBox,
setAttributes,
setElementRole,
traverse,
} from '../utils';
import { embedFonts } from './font';
import type { SVGExportOptions } from './types';
const VIEWBOX_CHANGE_TOLERANCE = 0.5;
export async function exportToSVGString(
svg: SVGSVGElement,
options: Omit<SVGExportOptions, 'type'> = {},
) {
const node = await exportToSVG(svg, options);
const str = new XMLSerializer().serializeToString(node);
return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(str);
}
function getExportViewBox(svg: SVGSVGElement) {
if (svg.hasAttribute('viewBox')) return getViewBox(svg);
const width = parseAbsoluteLength(svg.getAttribute('width'));
const height = parseAbsoluteLength(svg.getAttribute('height'));
if (
!Number.isNaN(width) &&
width > 0 &&
!Number.isNaN(height) &&
height > 0
) {
return { x: 0, y: 0, width, height };
}
const rect = svg.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) {
return { x: 0, y: 0, width: rect.width, height: rect.height };
}
return null;
}
function parseAbsoluteLength(value: string | null): number {
if (!value) return Number.NaN;
const trimmed = value.trim();
if (!trimmed) return Number.NaN;
if (!/^[-+]?(?:\d+\.?\d*|\.\d+)(?:px)?$/.test(trimmed)) return Number.NaN;
return Number.parseFloat(trimmed);
}
function measureSpanContentHeight(span: HTMLElement): number {
const prevHeight = span.style.height;
const prevOverflow = span.style.overflow;
try {
span.style.height = 'max-content';
span.style.overflow = 'hidden';
void span.offsetHeight; // force reflow
return span.scrollHeight;
} finally {
span.style.height = prevHeight;
span.style.overflow = prevOverflow;
}
}
function measureSpanContentWidth(span: HTMLElement): number {
const prevWidth = span.style.width;
const prevOverflow = span.style.overflow;
try {
span.style.width = 'max-content';
span.style.overflow = 'hidden';
void span.offsetWidth; // force reflow
return span.scrollWidth;
} finally {
span.style.width = prevWidth;
span.style.overflow = prevOverflow;
}
}
// Returns [left, top, right, bottom] in SVG coordinates for a foreignObject,
// accounting for flex alignment: bottom/center-aligned content can overflow,
// and horizontally aligned content can overflow as well.
function getFOContentBoundsInSVG(
fo: SVGForeignObjectElement,
content: HTMLElement,
toSVGCoord: (x: number, y: number) => SVGPoint,
): [number, number, number, number] {
const foRect = fo.getBoundingClientRect();
const foTopLeft = toSVGCoord(foRect.left, foRect.top);
const foBottomRight = toSVGCoord(foRect.right, foRect.bottom);
const foLeftSVG = foTopLeft.x;
const foTopSVG = foTopLeft.y;
const foRightSVG = foBottomRight.x;
const foBottomSVG = foBottomRight.y;
const foWidthSVG = foRightSVG - foLeftSVG;
const foHeightSVG = foBottomSVG - foTopSVG;
const svgUnitsPerClientPxY =
foRect.height > 0 ? foHeightSVG / foRect.height : 1;
const svgUnitsPerClientPxX = foRect.width > 0 ? foWidthSVG / foRect.width : 1;
// Measure actual content dimensions
const realScrollHeight = measureSpanContentHeight(content);
const contentHeightSVG =
realScrollHeight > 0
? realScrollHeight * svgUnitsPerClientPxY
: foHeightSVG;
const realScrollWidth = measureSpanContentWidth(content);
const contentWidthSVG =
realScrollWidth > 0 ? realScrollWidth * svgUnitsPerClientPxX : foWidthSVG;
const computedStyle = window.getComputedStyle(content);
const alignItems = computedStyle.alignItems;
const justifyContent = computedStyle.justifyContent;
// Calculate vertical bounds
let top: number, bottom: number;
if (alignItems === 'flex-end' || alignItems === 'end') {
top = foBottomSVG - contentHeightSVG;
bottom = foBottomSVG;
} else if (alignItems === 'center') {
const overflowY = contentHeightSVG - foHeightSVG;
top = foTopSVG - overflowY / 2;
bottom = foBottomSVG + overflowY / 2;
} else {
top = foTopSVG;
bottom = foTopSVG + contentHeightSVG;
}
// Calculate horizontal bounds
let left: number, right: number;
if (
justifyContent === 'flex-end' ||
justifyContent === 'end' ||
justifyContent === 'right'
) {
left = foRightSVG - contentWidthSVG;
right = foRightSVG;
} else if (justifyContent === 'center') {
const overflowX = contentWidthSVG - foWidthSVG;
left = foLeftSVG - overflowX / 2;
right = foRightSVG + overflowX / 2;
} else {
left = foLeftSVG;
right = foLeftSVG + contentWidthSVG;
}
return [left, top, right, bottom];
}
/**
* Computes a viewBox that fully covers all foreignObject text content,
* accounting for overflow caused by flex alignment (bottom/center align
* can push content outside the foreignObject bounds).
*/
function computeFullViewBox(svg: SVGSVGElement): string | null {
const viewBox = getExportViewBox(svg);
if (!viewBox) return null;
if (typeof svg.getScreenCTM !== 'function') return null;
const screenCTM = svg.getScreenCTM();
if (!screenCTM) return null;
const inverseCTM = screenCTM.inverse();
const toSVGCoord = (clientX: number, clientY: number) => {
const pt = svg.createSVGPoint();
pt.x = clientX;
pt.y = clientY;
return pt.matrixTransform(inverseCTM);
};
let minX = viewBox.x;
let minY = viewBox.y;
let maxX = viewBox.x + viewBox.width;
let maxY = viewBox.y + viewBox.height;
svg
.querySelectorAll<SVGForeignObjectElement>('foreignObject')
.forEach((fo) => {
const content = fo.firstElementChild as HTMLElement;
if (!content) return;
const [left, top, right, bottom] = getFOContentBoundsInSVG(
fo,
content,
toSVGCoord,
);
minX = Math.min(minX, left);
minY = Math.min(minY, top);
maxX = Math.max(maxX, right);
maxY = Math.max(maxY, bottom);
});
const newX = minX;
const newY = minY;
const newWidth = maxX - newX;
const newHeight = maxY - newY;
if (
newWidth <= viewBox.width + VIEWBOX_CHANGE_TOLERANCE &&
newHeight <= viewBox.height + VIEWBOX_CHANGE_TOLERANCE &&
newX >= viewBox.x - VIEWBOX_CHANGE_TOLERANCE &&
newY >= viewBox.y - VIEWBOX_CHANGE_TOLERANCE
)
return null;
return `${newX} ${newY} ${newWidth} ${newHeight}`;
}
export async function exportToSVG(
svg: SVGSVGElement,
options: Omit<SVGExportOptions, 'type'> = {},
) {
const {
removeBackground = false,
embedResources = true,
removeIds = false,
} = options;
const clonedSVG = svg.cloneNode(true) as SVGSVGElement;
if (typeof document !== 'undefined') {
const fullViewBox = computeFullViewBox(svg);
if (fullViewBox) {
clonedSVG.setAttribute('viewBox', fullViewBox);
}
}
const { width, height } = getViewBox(clonedSVG);
setAttributes(clonedSVG, { width, height });
if (removeIds) {
inlineUseElements(clonedSVG);
inlineDefsReferences(clonedSVG);
} else {
await embedIcons(clonedSVG);
}
await embedFonts(clonedSVG, embedResources);
if (removeBackground) {
removeSVGBackground(clonedSVG);
}
cleanSVG(clonedSVG);
return clonedSVG;
}
async function embedIcons(svg: SVGSVGElement) {
const icons = svg.querySelectorAll('use');
const defs = getDefs(svg);
icons.forEach((icon) => {
const href = icon.getAttribute('href');
if (!href) return;
const existsSymbol = svg.querySelector(href);
if (!existsSymbol) {
const symbolElement = document.querySelector(href);
if (symbolElement) {
defs.appendChild(symbolElement.cloneNode(true));
}
}
});
}
const iconRole = 'icon-defs';
function getDefs(svg: SVGSVGElement) {
const defs = getElementByRole(svg, iconRole);
if (defs) return defs;
const _defs = createElement('defs');
setElementRole(_defs, iconRole);
svg.prepend(_defs);
return _defs;
}
function inlineUseElements(svg: SVGSVGElement) {
const uses = Array.from(svg.querySelectorAll<SVGUseElement>('use'));
if (!uses.length) return;
uses.forEach((use) => {
const href = getUseHref(use);
if (!href || !href.startsWith('#')) return;
const target = resolveUseTarget(svg, href);
if (!target || target === use) return;
const replacement = createInlineElement(use, target);
if (!replacement) return;
use.replaceWith(replacement);
});
}
function getUseHref(use: SVGUseElement) {
return use.getAttribute('href') ?? use.getAttribute('xlink:href');
}
function resolveUseTarget(svg: SVGSVGElement, href: string) {
const localTarget = svg.querySelector(href);
if (localTarget) return localTarget as SVGElement;
const docTarget = document.querySelector(href);
return docTarget as SVGElement | null;
}
function createInlineElement(use: SVGUseElement, target: SVGElement) {
const tag = target.tagName.toLowerCase();
if (tag === 'symbol') {
return materializeSymbol(use, target as SVGSymbolElement);
}
if (tag === 'svg') {
return materializeSVG(use, target as SVGSVGElement);
}
return materializeElement(use, target);
}
function materializeSymbol(use: SVGUseElement, symbol: SVGSymbolElement) {
const symbolClone = symbol.cloneNode(true) as SVGSymbolElement;
const svg = createElement<SVGSVGElement>('svg');
applyAttributes(svg, symbolClone, new Set(['id']));
applyAttributes(svg, use, new Set(['href', 'xlink:href']));
while (symbolClone.firstChild) {
svg.appendChild(symbolClone.firstChild);
}
return svg;
}
function materializeSVG(use: SVGUseElement, source: SVGSVGElement) {
const clone = source.cloneNode(true) as SVGSVGElement;
clone.removeAttribute('id');
applyAttributes(clone, use, new Set(['href', 'xlink:href']));
return clone;
}
function materializeElement(use: SVGUseElement, source: SVGElement) {
const clone = source.cloneNode(true) as SVGElement;
clone.removeAttribute('id');
const wrapper = createElement<SVGGElement>('g');
applyAttributes(
wrapper,
use,
new Set(['href', 'xlink:href', 'x', 'y', 'width', 'height', 'transform']),
);
const transform = buildUseTransform(use);
if (transform) {
wrapper.setAttribute('transform', transform);
}
wrapper.appendChild(clone);
return wrapper;
}
function buildUseTransform(use: SVGUseElement) {
const x = use.getAttribute('x');
const y = use.getAttribute('y');
const translate = x || y ? `translate(${x ?? 0} ${y ?? 0})` : '';
const transform = use.getAttribute('transform') ?? '';
if (translate && transform) return `${translate} ${transform}`;
return translate || transform || null;
}
function applyAttributes(
target: SVGElement,
source: SVGElement,
exclude: Set<string> = new Set(),
) {
Array.from(source.attributes).forEach((attr) => {
if (exclude.has(attr.name)) return;
if (attr.name === 'style') {
mergeStyleAttribute(target, attr.value);
return;
}
if (attr.name === 'class') {
mergeClassAttribute(target, attr.value);
return;
}
target.setAttribute(attr.name, attr.value);
});
}
function mergeStyleAttribute(target: SVGElement, value: string) {
const current = target.getAttribute('style');
if (!current) {
target.setAttribute('style', value);
return;
}
const separator = current.trim().endsWith(';') ? '' : ';';
target.setAttribute('style', `${current}${separator}${value}`);
}
function mergeClassAttribute(target: SVGElement, value: string) {
const current = target.getAttribute('class');
if (!current) {
target.setAttribute('class', value);
return;
}
target.setAttribute('class', `${current} ${value}`.trim());
}
const urlRefRegex = /url\(\s*['"]?#([^'")\s]+)['"]?\s*\)/g;
function inlineDefsReferences(svg: SVGSVGElement) {
const referencedIds = collectReferencedIds(svg);
if (referencedIds.size === 0) {
removeDefs(svg);
return;
}
const defsDataUrl = createDefsDataUrl(svg, referencedIds);
if (!defsDataUrl) return;
traverse(svg, (node) => {
if (node.tagName.toLowerCase() === 'defs') return false;
const attrs = Array.from(node.attributes);
attrs.forEach((attr) => {
const value = attr.value;
if (!value.includes('url(')) return;
const updated = value.replace(urlRefRegex, (_match, id) => {
const encodedId = encodeURIComponent(id);
return `url("${defsDataUrl}#${encodedId}")`;
});
if (updated !== value) node.setAttribute(attr.name, updated);
});
});
removeDefs(svg);
}
function collectReferencedIds(svg: SVGSVGElement) {
const ids = new Set<string>();
traverse(svg, (node) => {
if (node.tagName.toLowerCase() === 'defs') return false;
collectIdsFromAttributes(node, (id) => ids.add(id));
});
return ids;
}
function collectIdsFromAttributes(
node: SVGElement,
addId: (id: string) => void,
) {
for (const attr of Array.from(node.attributes)) {
const value = attr.value;
if (value.includes('url(')) {
for (const match of value.matchAll(urlRefRegex)) {
if (match[1]) addId(match[1]);
}
}
if (
(attr.name === 'href' || attr.name === 'xlink:href') &&
value[0] === '#'
) {
addId(value.slice(1));
}
}
}
function createDefsDataUrl(svg: SVGSVGElement, ids: Set<string>) {
if (ids.size === 0) return null;
const collected = collectDefElements(svg, ids);
if (collected.size === 0) return null;
const defsSvg = createElement<SVGSVGElement>('svg', {
xmlns: 'http://www.w3.org/2000/svg',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
});
const defs = createElement<SVGDefsElement>('defs');
collected.forEach((node) => {
defs.appendChild(node.cloneNode(true));
});
if (!defs.children.length) return null;
defsSvg.appendChild(defs);
const serialized = new XMLSerializer().serializeToString(defsSvg);
return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(serialized);
}
function collectDefElements(svg: SVGSVGElement, ids: Set<string>) {
const collected = new Map<string, SVGElement>();
const queue = Array.from(ids);
const queued = new Set(queue);
const visited = new Set<string>();
const enqueue = (id: string) => {
if (visited.has(id) || queued.has(id)) return;
queue.push(id);
queued.add(id);
};
while (queue.length) {
const id = queue.shift()!;
if (!id) continue;
if (visited.has(id)) continue;
visited.add(id);
const selector = `#${escapeCssId(id)}`;
const target = svg.querySelector(selector);
if (!target) continue;
collected.set(id, target as SVGElement);
traverse(target as SVGElement, (node) => {
collectIdsFromAttributes(node, enqueue);
});
}
return collected;
}
// Fallback implementation based on the CSS.escape algorithm
function cssEscape(value: string): string {
const string = String(value);
const length = string.length;
let result = '';
if (length === 0) {
return '';
}
for (let i = 0; i < length; i++) {
const codeUnit = string.charCodeAt(i);
// Null character
if (codeUnit === 0x0000) {
result += '\uFFFD';
continue;
}
// Control characters or DEL
if ((codeUnit >= 0x0001 && codeUnit <= 0x001f) || codeUnit === 0x007f) {
result += '\\' + codeUnit.toString(16) + ' ';
continue;
}
// Escape if first character is a digit
if (i === 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) {
result += '\\' + codeUnit.toString(16) + ' ';
continue;
}
// Escape if second character is a digit and first is a hyphen
if (
i === 1 &&
codeUnit >= 0x0030 &&
codeUnit <= 0x0039 &&
string.charCodeAt(0) === 0x002d
) {
result += '\\' + codeUnit.toString(16) + ' ';
continue;
}
// If the character is the first and is a hyphen followed by end of string, escape it
if (i === 0 && length === 1 && codeUnit === 0x002d) {
result += '\\' + string.charAt(i);
continue;
}
// Characters that are safe to use unescaped
if (
codeUnit >= 0x0080 ||
(codeUnit >= 0x0030 && codeUnit <= 0x0039) || // 0-9
(codeUnit >= 0x0041 && codeUnit <= 0x005a) || // A-Z
(codeUnit >= 0x0061 && codeUnit <= 0x007a) || // a-z
codeUnit === 0x002d || // -
codeUnit === 0x005f // _
) {
result += string.charAt(i);
continue;
}
// All other characters
result += '\\' + string.charAt(i);
}
return result;
}
function escapeCssId(id: string) {
if (globalThis.CSS && typeof globalThis.CSS.escape === 'function') {
return globalThis.CSS.escape(id);
}
return cssEscape(id);
}
function removeDefs(svg: SVGSVGElement) {
const defsList = Array.from(svg.querySelectorAll('defs'));
defsList.forEach((defs) => defs.remove());
}
function cleanSVG(svg: SVGSVGElement) {
removeBtnGroup(svg);
removeTransientContainer(svg);
removeUselessAttrs(svg);
clearDataset(svg);
}
function removeSVGBackground(svg: SVGSVGElement) {
svg.style.removeProperty('background-color');
const background = getElementByRole(svg, ElementTypeEnum.Background);
background?.remove();
}
function removeBtnGroup(svg: SVGSVGElement) {
const btnGroup = getElementByRole(svg, ElementTypeEnum.BtnsGroup);
btnGroup?.remove();
const btnIconDefs = getElementByRole(svg, 'btn-icon-defs');
btnIconDefs?.remove();
}
function removeTransientContainer(svg: SVGSVGElement) {
const transientContainer = svg.querySelector(
'[data-element-type=transient-container]',
);
transientContainer?.remove();
}
function removeUselessAttrs(svg: SVGSVGElement) {
const groups = svg.querySelectorAll('g');
groups.forEach((group) => {
group.removeAttribute('x');
group.removeAttribute('y');
group.removeAttribute('width');
group.removeAttribute('height');
});
}
function clearDataset(svg: SVGSVGElement) {
traverse(svg, (node) => {
for (const key in node.dataset) {
delete node.dataset[key];
}
});
}