distark-render 1.2.0 → 1.2.1
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/cli/distark-check.d.ts +1 -0
- package/dist/cli/distark-check.d.ts.map +1 -1
- package/dist/cli/distark-check.js +11 -0
- package/dist/cli/distark-check.js.map +1 -1
- package/dist/cli/fix.d.ts +8 -0
- package/dist/cli/fix.d.ts.map +1 -0
- package/dist/cli/fix.js +129 -0
- package/dist/cli/fix.js.map +1 -0
- package/dist/cli/validate.d.ts +25 -0
- package/dist/cli/validate.d.ts.map +1 -0
- package/dist/cli/validate.js +205 -0
- package/dist/cli/validate.js.map +1 -0
- package/dist/modules/adapters/skiaRenderer.d.ts.map +1 -1
- package/dist/modules/adapters/skiaRenderer.js +2 -1
- package/dist/modules/adapters/skiaRenderer.js.map +1 -1
- package/dist/modules/characterValidator.d.ts +75 -1
- package/dist/modules/characterValidator.d.ts.map +1 -1
- package/dist/modules/characterValidator.js +282 -0
- package/dist/modules/characterValidator.js.map +1 -1
- package/dist/modules/imageLoad.d.ts +7 -9
- package/dist/modules/imageLoad.d.ts.map +1 -1
- package/dist/modules/imageLoad.js +16 -64
- package/dist/modules/imageLoad.js.map +1 -1
- package/dist/modules/rigAssetFix.d.ts +40 -0
- package/dist/modules/rigAssetFix.d.ts.map +1 -0
- package/dist/modules/rigAssetFix.js +208 -0
- package/dist/modules/rigAssetFix.js.map +1 -0
- package/dist/modules/svgAssetAnalysis.d.ts +168 -0
- package/dist/modules/svgAssetAnalysis.d.ts.map +1 -0
- package/dist/modules/svgAssetAnalysis.js +760 -0
- package/dist/modules/svgAssetAnalysis.js.map +1 -0
- package/dist/modules/svgAssetFix.d.ts +19 -0
- package/dist/modules/svgAssetFix.d.ts.map +1 -0
- package/dist/modules/svgAssetFix.js +470 -0
- package/dist/modules/svgAssetFix.js.map +1 -0
- package/dist/modules/svgRaster.d.ts +3 -0
- package/dist/modules/svgRaster.d.ts.map +1 -1
- package/dist/modules/svgRaster.js +7 -3
- package/dist/modules/svgRaster.js.map +1 -1
- package/dist/modules/svgViewBox.d.ts +35 -0
- package/dist/modules/svgViewBox.d.ts.map +1 -0
- package/dist/modules/svgViewBox.js +84 -0
- package/dist/modules/svgViewBox.js.map +1 -0
- package/dist/tests/test-cli.js +34 -1
- package/dist/tests/test-cli.js.map +1 -1
- package/dist/tests/test-newtank-viewport-zoom-body-path.js +14 -10
- package/dist/tests/test-newtank-viewport-zoom-body-path.js.map +1 -1
- package/dist/tests/test-newtank-viewport-zoom-diagnostic.d.ts +1 -1
- package/dist/tests/test-newtank-viewport-zoom-diagnostic.js +11 -22
- package/dist/tests/test-newtank-viewport-zoom-diagnostic.js.map +1 -1
- package/dist/tests/test-newtank-zoom-sizes.js +9 -9
- package/dist/tests/test-newtank-zoom-sizes.js.map +1 -1
- package/dist/tests/test-rig-standardization.d.ts +5 -0
- package/dist/tests/test-rig-standardization.d.ts.map +1 -0
- package/dist/tests/test-rig-standardization.js +171 -0
- package/dist/tests/test-rig-standardization.js.map +1 -0
- package/dist/tests/test-scene-object-svg-rerasterization.js +5 -5
- package/dist/tests/test-svg-asset-fix.d.ts +5 -0
- package/dist/tests/test-svg-asset-fix.d.ts.map +1 -0
- package/dist/tests/test-svg-asset-fix.js +102 -0
- package/dist/tests/test-svg-asset-fix.js.map +1 -0
- package/dist/tests/test-svg-rerasterization.js +30 -45
- package/dist/tests/test-svg-rerasterization.js.map +1 -1
- package/dist/tests/test-svg-viewbox.d.ts +2 -0
- package/dist/tests/test-svg-viewbox.d.ts.map +1 -0
- package/dist/tests/test-svg-viewbox.js +33 -0
- package/dist/tests/test-svg-viewbox.js.map +1 -0
- package/dist/tests/test-tanknew-svg-edit-data.d.ts +6 -0
- package/dist/tests/test-tanknew-svg-edit-data.d.ts.map +1 -0
- package/dist/tests/test-tanknew-svg-edit-data.js +63 -0
- package/dist/tests/test-tanknew-svg-edit-data.js.map +1 -0
- package/package.json +10 -5
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SVG asset analysis for rig standardization checks.
|
|
3
|
+
* Pure functions — no network, no DOM required.
|
|
4
|
+
*/
|
|
5
|
+
export const MAX_TOTAL_VERTICES = 400;
|
|
6
|
+
export const VIEWBOX_PADDING_TOLERANCE_RATIO = 0.02;
|
|
7
|
+
export const VIEWBOX_PADDING_TOLERANCE_MIN_PX = 1;
|
|
8
|
+
export const VIEWBOX_OVERFLOW_EPSILON = 0.5;
|
|
9
|
+
/** Content occupying less than this fraction of viewBox width/height is "shared canvas" padding (breaks SVG edit drag preview). */
|
|
10
|
+
export const VIEWBOX_MIN_CONTENT_COVERAGE = 0.5;
|
|
11
|
+
const FORBIDDEN_NON_VECTOR_RE = /<(text|use|foreignObject)\b/gi;
|
|
12
|
+
const PATH_TAG_RE = /<path\b([^>]*?)(?:\/>|>[\s\S]*?<\/path>)/gi;
|
|
13
|
+
const IMAGE_TAG_RE = /<image\b([^>]*?)(?:\/>|>[\s\S]*?<\/image>)/gi;
|
|
14
|
+
function sideMetric(value, tolerance) {
|
|
15
|
+
return { value, tolerance, excess: Math.max(0, value - tolerance) };
|
|
16
|
+
}
|
|
17
|
+
function readAttr(tag, name) {
|
|
18
|
+
return tag.match(new RegExp(`\\b${name}=["']([^"']*)["']`, 'i'))?.[1] ?? null;
|
|
19
|
+
}
|
|
20
|
+
function parseNumber(value, fallback = 0) {
|
|
21
|
+
if (!value)
|
|
22
|
+
return fallback;
|
|
23
|
+
const n = Number(value.trim());
|
|
24
|
+
return Number.isFinite(n) ? n : fallback;
|
|
25
|
+
}
|
|
26
|
+
function getRootTag(svgSource) {
|
|
27
|
+
return svgSource.match(/<svg\b[^>]*>/i)?.[0] ?? null;
|
|
28
|
+
}
|
|
29
|
+
function readStyleProperty(style, prop) {
|
|
30
|
+
if (!style)
|
|
31
|
+
return null;
|
|
32
|
+
const match = style.match(new RegExp(`(?:^|;)\\s*${prop}\\s*:\\s*([^;]+)`, 'i'));
|
|
33
|
+
return match?.[1]?.trim() ?? null;
|
|
34
|
+
}
|
|
35
|
+
function detectHiddenReason(tag) {
|
|
36
|
+
const display = (readAttr(tag, 'display') || readStyleProperty(readAttr(tag, 'style'), 'display') || '').toLowerCase();
|
|
37
|
+
if (display === 'none')
|
|
38
|
+
return 'display_none';
|
|
39
|
+
const visibility = (readAttr(tag, 'visibility') || readStyleProperty(readAttr(tag, 'style'), 'visibility') || '').toLowerCase();
|
|
40
|
+
if (visibility === 'hidden' || visibility === 'collapse')
|
|
41
|
+
return 'visibility_hidden';
|
|
42
|
+
const opacityRaw = readAttr(tag, 'opacity') || readStyleProperty(readAttr(tag, 'style'), 'opacity');
|
|
43
|
+
if (opacityRaw !== null && parseNumber(opacityRaw, 1) <= 0.001)
|
|
44
|
+
return 'opacity_zero';
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
function normalizePathD(d) {
|
|
48
|
+
return d.replace(/\s+/g, ' ').trim().toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
function isTransparentColor(value) {
|
|
51
|
+
if (!value)
|
|
52
|
+
return true;
|
|
53
|
+
const v = value.trim().toLowerCase();
|
|
54
|
+
return v === 'none' || v === 'transparent';
|
|
55
|
+
}
|
|
56
|
+
function hasVisibleFill(path) {
|
|
57
|
+
if (isTransparentColor(path.fill))
|
|
58
|
+
return false;
|
|
59
|
+
return path.fillOpacity > 0.001;
|
|
60
|
+
}
|
|
61
|
+
function hasVisibleStroke(path) {
|
|
62
|
+
if (isTransparentColor(path.stroke))
|
|
63
|
+
return false;
|
|
64
|
+
if (path.strokeWidth <= 0.001)
|
|
65
|
+
return false;
|
|
66
|
+
return path.strokeOpacity > 0.001;
|
|
67
|
+
}
|
|
68
|
+
export function parseLengthUnit(value) {
|
|
69
|
+
if (!value)
|
|
70
|
+
return 'missing';
|
|
71
|
+
const match = value.trim().match(/^([+-]?(?:\d*\.\d+|\d+))([a-z%]*)$/i);
|
|
72
|
+
if (!match)
|
|
73
|
+
return 'unknown';
|
|
74
|
+
const unit = (match[2] || 'px').toLowerCase();
|
|
75
|
+
if (unit === '' || unit === 'px')
|
|
76
|
+
return 'px';
|
|
77
|
+
if (unit === 'mm' || unit === 'cm' || unit === 'in' || unit === 'pt' || unit === 'pc')
|
|
78
|
+
return unit;
|
|
79
|
+
if (unit === '%')
|
|
80
|
+
return '%';
|
|
81
|
+
return 'unknown';
|
|
82
|
+
}
|
|
83
|
+
export function parseSvgRootUnits(svgSource) {
|
|
84
|
+
const rootTag = getRootTag(svgSource) || '';
|
|
85
|
+
const widthRaw = readAttr(rootTag, 'width');
|
|
86
|
+
const heightRaw = readAttr(rootTag, 'height');
|
|
87
|
+
const widthUnit = parseLengthUnit(widthRaw);
|
|
88
|
+
const heightUnit = parseLengthUnit(heightRaw);
|
|
89
|
+
const pixelUnitsOnly = (widthUnit === 'px' || widthUnit === 'missing')
|
|
90
|
+
&& (heightUnit === 'px' || heightUnit === 'missing');
|
|
91
|
+
return { widthRaw, heightRaw, widthUnit, heightUnit, pixelUnitsOnly };
|
|
92
|
+
}
|
|
93
|
+
export function checkPreserveAspectRatioNone(svgSource) {
|
|
94
|
+
const rootTag = getRootTag(svgSource);
|
|
95
|
+
if (!rootTag)
|
|
96
|
+
return false;
|
|
97
|
+
return /preserveAspectRatio\s*=\s*["']none["']/i.test(rootTag);
|
|
98
|
+
}
|
|
99
|
+
/** Parse translation from an SVG transform attribute.
|
|
100
|
+
* Handles both `translate(tx[, ty])` and `matrix(a, b, c, d, tx, ty)` formats.
|
|
101
|
+
* For matrix transforms, extracts the translation components (e and f).
|
|
102
|
+
*/
|
|
103
|
+
export function parsePathTranslateTransform(transform) {
|
|
104
|
+
if (!transform)
|
|
105
|
+
return null;
|
|
106
|
+
const translateMatch = transform.match(/translate\s*\(\s*(-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?)(?:[\s,]+(-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?))?\s*\)/i);
|
|
107
|
+
if (translateMatch) {
|
|
108
|
+
const tx = Number(translateMatch[1]);
|
|
109
|
+
const ty = translateMatch[2] !== undefined ? Number(translateMatch[2]) : 0;
|
|
110
|
+
if (!Number.isFinite(tx) || !Number.isFinite(ty))
|
|
111
|
+
return null;
|
|
112
|
+
return { tx, ty };
|
|
113
|
+
}
|
|
114
|
+
const NUM = '(-?(?:\\d*\\.\\d+|\\d+)(?:e[-+]?\\d+)?)';
|
|
115
|
+
const SEP = '[\\s,]+';
|
|
116
|
+
const matrixRe = new RegExp(`matrix\\s*\\(\\s*${NUM}${SEP}${NUM}${SEP}${NUM}${SEP}${NUM}${SEP}${NUM}${SEP}${NUM}\\s*\\)`, 'i');
|
|
117
|
+
const matrixMatch = transform.match(matrixRe);
|
|
118
|
+
if (matrixMatch) {
|
|
119
|
+
const [, aStr, bStr, cStr, dStr, eStr, fStr] = matrixMatch;
|
|
120
|
+
const a = Number(aStr);
|
|
121
|
+
const b = Number(bStr);
|
|
122
|
+
const c = Number(cStr);
|
|
123
|
+
const d = Number(dStr);
|
|
124
|
+
const e = Number(eStr);
|
|
125
|
+
const f = Number(fStr);
|
|
126
|
+
if ([a, b, c, d, e, f].some(v => !Number.isFinite(v)))
|
|
127
|
+
return null;
|
|
128
|
+
const isPureTranslation = Math.abs(a - 1) < 1e-6
|
|
129
|
+
&& Math.abs(b) < 1e-6
|
|
130
|
+
&& Math.abs(c) < 1e-6
|
|
131
|
+
&& Math.abs(d - 1) < 1e-6;
|
|
132
|
+
if (!isPureTranslation)
|
|
133
|
+
return null;
|
|
134
|
+
return { tx: e, ty: f };
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
function offsetBounds(bounds, tx, ty) {
|
|
139
|
+
return {
|
|
140
|
+
minX: bounds.minX + tx,
|
|
141
|
+
minY: bounds.minY + ty,
|
|
142
|
+
maxX: bounds.maxX + tx,
|
|
143
|
+
maxY: bounds.maxY + ty,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
export function parseViewBox(svgSource) {
|
|
147
|
+
const rootTag = getRootTag(svgSource);
|
|
148
|
+
if (!rootTag)
|
|
149
|
+
return null;
|
|
150
|
+
const viewBoxRaw = readAttr(rootTag, 'viewBox');
|
|
151
|
+
if (viewBoxRaw) {
|
|
152
|
+
const parts = viewBoxRaw.trim().split(/[\s,]+/).map(Number);
|
|
153
|
+
if (parts.length === 4 && parts.every(Number.isFinite) && parts[2] > 0 && parts[3] > 0) {
|
|
154
|
+
return { minX: parts[0], minY: parts[1], width: parts[2], height: parts[3] };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const width = parseNumber(readAttr(rootTag, 'width'), 0);
|
|
158
|
+
const height = parseNumber(readAttr(rootTag, 'height'), 0);
|
|
159
|
+
if (width > 0 && height > 0) {
|
|
160
|
+
return { minX: 0, minY: 0, width, height };
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
export function extractSvgPaths(svgSource) {
|
|
165
|
+
// Pre-scan: collect <g> open/close events to track ancestor translate transforms.
|
|
166
|
+
const G_OPEN_RE = /<g\b([^>]*)>/gi;
|
|
167
|
+
const G_CLOSE_RE = /<\/g\s*>/gi;
|
|
168
|
+
const gEvents = [];
|
|
169
|
+
for (const m of svgSource.matchAll(G_OPEN_RE)) {
|
|
170
|
+
const transform = readAttr(m[0], 'transform');
|
|
171
|
+
const t = parsePathTranslateTransform(transform);
|
|
172
|
+
gEvents.push({ pos: m.index, type: 'open', tx: t?.tx ?? 0, ty: t?.ty ?? 0 });
|
|
173
|
+
}
|
|
174
|
+
for (const m of svgSource.matchAll(G_CLOSE_RE)) {
|
|
175
|
+
gEvents.push({ pos: m.index, type: 'close', tx: 0, ty: 0 });
|
|
176
|
+
}
|
|
177
|
+
gEvents.sort((a, b) => a.pos - b.pos);
|
|
178
|
+
const paths = [];
|
|
179
|
+
let index = 0;
|
|
180
|
+
let gEventIdx = 0;
|
|
181
|
+
const gStack = [];
|
|
182
|
+
let accTx = 0;
|
|
183
|
+
let accTy = 0;
|
|
184
|
+
for (const match of svgSource.matchAll(PATH_TAG_RE)) {
|
|
185
|
+
// Advance <g> event stack up to this path's position.
|
|
186
|
+
while (gEventIdx < gEvents.length && gEvents[gEventIdx].pos < match.index) {
|
|
187
|
+
const ev = gEvents[gEventIdx];
|
|
188
|
+
if (ev.type === 'open') {
|
|
189
|
+
gStack.push({ tx: ev.tx, ty: ev.ty });
|
|
190
|
+
accTx += ev.tx;
|
|
191
|
+
accTy += ev.ty;
|
|
192
|
+
}
|
|
193
|
+
else if (gStack.length > 0) {
|
|
194
|
+
const popped = gStack.pop();
|
|
195
|
+
accTx -= popped.tx;
|
|
196
|
+
accTy -= popped.ty;
|
|
197
|
+
}
|
|
198
|
+
gEventIdx++;
|
|
199
|
+
}
|
|
200
|
+
index += 1;
|
|
201
|
+
const tag = match[0];
|
|
202
|
+
const attrs = match[1] || tag;
|
|
203
|
+
const d = readAttr(attrs, 'd') ?? readAttr(tag, 'd');
|
|
204
|
+
if (!d || !d.trim())
|
|
205
|
+
continue;
|
|
206
|
+
const hiddenReason = detectHiddenReason(tag) ?? undefined;
|
|
207
|
+
const transformRaw = readAttr(attrs, 'transform') ?? readAttr(tag, 'transform');
|
|
208
|
+
const pathTranslate = parsePathTranslateTransform(transformRaw);
|
|
209
|
+
const totalTx = (pathTranslate?.tx ?? 0) + accTx;
|
|
210
|
+
const totalTy = (pathTranslate?.ty ?? 0) + accTy;
|
|
211
|
+
paths.push({
|
|
212
|
+
index,
|
|
213
|
+
d,
|
|
214
|
+
fill: readAttr(attrs, 'fill') ?? readAttr(tag, 'fill'),
|
|
215
|
+
stroke: readAttr(attrs, 'stroke') ?? readAttr(tag, 'stroke'),
|
|
216
|
+
fillOpacity: parseNumber(readAttr(attrs, 'fill-opacity') ?? readAttr(tag, 'fill-opacity'), 1),
|
|
217
|
+
strokeOpacity: parseNumber(readAttr(attrs, 'stroke-opacity') ?? readAttr(tag, 'stroke-opacity'), 1),
|
|
218
|
+
strokeWidth: parseNumber(readAttr(attrs, 'stroke-width') ?? readAttr(tag, 'stroke-width'), 1),
|
|
219
|
+
visible: hiddenReason === undefined,
|
|
220
|
+
hiddenReason,
|
|
221
|
+
translateX: totalTx || undefined,
|
|
222
|
+
translateY: totalTy || undefined,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return paths;
|
|
226
|
+
}
|
|
227
|
+
export function extractEmbeddedImages(svgSource) {
|
|
228
|
+
const images = [];
|
|
229
|
+
let index = 0;
|
|
230
|
+
for (const match of svgSource.matchAll(IMAGE_TAG_RE)) {
|
|
231
|
+
index += 1;
|
|
232
|
+
const tag = match[0];
|
|
233
|
+
const attrs = match[1] || tag;
|
|
234
|
+
const href = readAttr(attrs, 'href') ?? readAttr(attrs, 'xlink:href') ?? readAttr(tag, 'href') ?? readAttr(tag, 'xlink:href');
|
|
235
|
+
const hiddenReason = detectHiddenReason(tag) ?? undefined;
|
|
236
|
+
let hrefKind = 'missing';
|
|
237
|
+
if (href) {
|
|
238
|
+
if (/^data:image\//i.test(href) || /^data:.*;base64,/i.test(href)) {
|
|
239
|
+
hrefKind = 'embedded_raster';
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
hrefKind = 'external';
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
images.push({
|
|
246
|
+
index,
|
|
247
|
+
hidden: hiddenReason !== undefined,
|
|
248
|
+
hiddenReason,
|
|
249
|
+
hrefKind,
|
|
250
|
+
width: readAttr(attrs, 'width') ?? readAttr(tag, 'width'),
|
|
251
|
+
height: readAttr(attrs, 'height') ?? readAttr(tag, 'height'),
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return images;
|
|
255
|
+
}
|
|
256
|
+
export function countForbiddenNonVectorElements(svgSource) {
|
|
257
|
+
return [...svgSource.matchAll(FORBIDDEN_NON_VECTOR_RE)].length;
|
|
258
|
+
}
|
|
259
|
+
export function getVisiblePaths(paths) {
|
|
260
|
+
return paths.filter(p => p.visible);
|
|
261
|
+
}
|
|
262
|
+
/** Count authoring vertices in an SVG path `d` attribute. */
|
|
263
|
+
export function countPathVertices(d) {
|
|
264
|
+
let vertices = 0;
|
|
265
|
+
const re = /([MmLlHhVvCcSsQqTtAaZz])([^MmLlHhVvCcSsQqTtAaZz]*)/g;
|
|
266
|
+
let match;
|
|
267
|
+
while ((match = re.exec(d)) !== null) {
|
|
268
|
+
const cmd = match[1];
|
|
269
|
+
const args = (match[2].match(/-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?/gi) || []).map(Number);
|
|
270
|
+
const upper = cmd.toUpperCase();
|
|
271
|
+
switch (upper) {
|
|
272
|
+
case 'M':
|
|
273
|
+
vertices += Math.max(1, Math.floor(args.length / 2));
|
|
274
|
+
break;
|
|
275
|
+
case 'L':
|
|
276
|
+
vertices += Math.floor(args.length / 2);
|
|
277
|
+
break;
|
|
278
|
+
case 'H':
|
|
279
|
+
case 'V':
|
|
280
|
+
vertices += args.length;
|
|
281
|
+
break;
|
|
282
|
+
case 'C':
|
|
283
|
+
vertices += Math.floor(args.length / 6) * 3;
|
|
284
|
+
break;
|
|
285
|
+
case 'S':
|
|
286
|
+
case 'Q':
|
|
287
|
+
vertices += Math.floor(args.length / 4) * 2;
|
|
288
|
+
break;
|
|
289
|
+
case 'T':
|
|
290
|
+
vertices += Math.floor(args.length / 2);
|
|
291
|
+
break;
|
|
292
|
+
case 'A':
|
|
293
|
+
vertices += Math.floor(args.length / 7);
|
|
294
|
+
break;
|
|
295
|
+
default:
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return vertices;
|
|
300
|
+
}
|
|
301
|
+
export function getPathStartPoint(d) {
|
|
302
|
+
const match = d.match(/^[Mm]\s*(-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?)[,\s]+(-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?)/);
|
|
303
|
+
if (!match)
|
|
304
|
+
return null;
|
|
305
|
+
return { x: Number(match[1]), y: Number(match[2]) };
|
|
306
|
+
}
|
|
307
|
+
function mergeBounds(a, b) {
|
|
308
|
+
if (!a)
|
|
309
|
+
return b;
|
|
310
|
+
return {
|
|
311
|
+
minX: Math.min(a.minX, b.minX),
|
|
312
|
+
minY: Math.min(a.minY, b.minY),
|
|
313
|
+
maxX: Math.max(a.maxX, b.maxX),
|
|
314
|
+
maxY: Math.max(a.maxY, b.maxY),
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function updateBoundsFromPoint(bounds, x, y) {
|
|
318
|
+
return bounds
|
|
319
|
+
? { ...bounds, minX: Math.min(bounds.minX, x), minY: Math.min(bounds.minY, y), maxX: Math.max(bounds.maxX, x), maxY: Math.max(bounds.maxY, y) }
|
|
320
|
+
: { minX: x, minY: y, maxX: x, maxY: y };
|
|
321
|
+
}
|
|
322
|
+
/** Conservative axis-aligned bounds for a single path (includes curve control points). */
|
|
323
|
+
export function getPathBounds(d) {
|
|
324
|
+
let bounds = null;
|
|
325
|
+
let cx = 0;
|
|
326
|
+
let cy = 0;
|
|
327
|
+
let sx = 0;
|
|
328
|
+
let sy = 0;
|
|
329
|
+
const re = /([MmLlHhVvCcSsQqTtAaZz])([^MmLlHhVvCcSsQqTtAaZz]*)/g;
|
|
330
|
+
let match;
|
|
331
|
+
while ((match = re.exec(d)) !== null) {
|
|
332
|
+
const cmd = match[1];
|
|
333
|
+
const rel = cmd === cmd.toLowerCase();
|
|
334
|
+
const upper = cmd.toUpperCase();
|
|
335
|
+
const args = (match[2].match(/-?(?:\d*\.\d+|\d+)(?:e[-+]?\d+)?/gi) || []).map(Number);
|
|
336
|
+
const abs = (v, base) => rel ? base + v : v;
|
|
337
|
+
switch (upper) {
|
|
338
|
+
case 'M':
|
|
339
|
+
for (let i = 0; i + 1 < args.length; i += 2) {
|
|
340
|
+
cx = abs(args[i], cx);
|
|
341
|
+
cy = abs(args[i + 1], cy);
|
|
342
|
+
bounds = updateBoundsFromPoint(bounds, cx, cy);
|
|
343
|
+
if (i === 0) {
|
|
344
|
+
sx = cx;
|
|
345
|
+
sy = cy;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
case 'L':
|
|
350
|
+
for (let i = 0; i + 1 < args.length; i += 2) {
|
|
351
|
+
cx = abs(args[i], cx);
|
|
352
|
+
cy = abs(args[i + 1], cy);
|
|
353
|
+
bounds = updateBoundsFromPoint(bounds, cx, cy);
|
|
354
|
+
}
|
|
355
|
+
break;
|
|
356
|
+
case 'H':
|
|
357
|
+
for (const x of args) {
|
|
358
|
+
cx = abs(x, cx);
|
|
359
|
+
bounds = updateBoundsFromPoint(bounds, cx, cy);
|
|
360
|
+
}
|
|
361
|
+
break;
|
|
362
|
+
case 'V':
|
|
363
|
+
for (const y of args) {
|
|
364
|
+
cy = abs(y, cy);
|
|
365
|
+
bounds = updateBoundsFromPoint(bounds, cx, cy);
|
|
366
|
+
}
|
|
367
|
+
break;
|
|
368
|
+
case 'C':
|
|
369
|
+
for (let i = 0; i + 5 < args.length; i += 6) {
|
|
370
|
+
const cp1x = abs(args[i], cx), cp1y = abs(args[i + 1], cy);
|
|
371
|
+
const cp2x = abs(args[i + 2], cx), cp2y = abs(args[i + 3], cy);
|
|
372
|
+
const epx = abs(args[i + 4], cx), epy = abs(args[i + 5], cy);
|
|
373
|
+
bounds = updateBoundsFromPoint(bounds, cp1x, cp1y);
|
|
374
|
+
bounds = updateBoundsFromPoint(bounds, cp2x, cp2y);
|
|
375
|
+
bounds = updateBoundsFromPoint(bounds, epx, epy);
|
|
376
|
+
cx = epx;
|
|
377
|
+
cy = epy;
|
|
378
|
+
}
|
|
379
|
+
break;
|
|
380
|
+
case 'S':
|
|
381
|
+
case 'Q':
|
|
382
|
+
for (let i = 0; i + 3 < args.length; i += 4) {
|
|
383
|
+
const cpx = abs(args[i], cx), cpy = abs(args[i + 1], cy);
|
|
384
|
+
const epx = abs(args[i + 2], cx), epy = abs(args[i + 3], cy);
|
|
385
|
+
bounds = updateBoundsFromPoint(bounds, cpx, cpy);
|
|
386
|
+
bounds = updateBoundsFromPoint(bounds, epx, epy);
|
|
387
|
+
cx = epx;
|
|
388
|
+
cy = epy;
|
|
389
|
+
}
|
|
390
|
+
break;
|
|
391
|
+
case 'T':
|
|
392
|
+
for (let i = 0; i + 1 < args.length; i += 2) {
|
|
393
|
+
cx = abs(args[i], cx);
|
|
394
|
+
cy = abs(args[i + 1], cy);
|
|
395
|
+
bounds = updateBoundsFromPoint(bounds, cx, cy);
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
case 'A':
|
|
399
|
+
for (let i = 0; i + 6 < args.length; i += 7) {
|
|
400
|
+
const rx = args[i];
|
|
401
|
+
const ry = args[i + 1];
|
|
402
|
+
const epx = abs(args[i + 5], cx);
|
|
403
|
+
const epy = abs(args[i + 6], cy);
|
|
404
|
+
bounds = updateBoundsFromPoint(bounds, epx - rx, epy - ry);
|
|
405
|
+
bounds = updateBoundsFromPoint(bounds, epx + rx, epy + ry);
|
|
406
|
+
cx = epx;
|
|
407
|
+
cy = epy;
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
case 'Z':
|
|
411
|
+
cx = sx;
|
|
412
|
+
cy = sy;
|
|
413
|
+
break;
|
|
414
|
+
default:
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return bounds;
|
|
419
|
+
}
|
|
420
|
+
export function getPathBoundsWithTransform(path) {
|
|
421
|
+
const pb = getPathBounds(path.d);
|
|
422
|
+
if (!pb)
|
|
423
|
+
return null;
|
|
424
|
+
const tx = path.translateX ?? 0;
|
|
425
|
+
const ty = path.translateY ?? 0;
|
|
426
|
+
return tx === 0 && ty === 0 ? pb : offsetBounds(pb, tx, ty);
|
|
427
|
+
}
|
|
428
|
+
export function getContentBounds(paths) {
|
|
429
|
+
let bounds = null;
|
|
430
|
+
for (const path of paths) {
|
|
431
|
+
const pb = getPathBoundsWithTransform(path);
|
|
432
|
+
if (pb)
|
|
433
|
+
bounds = mergeBounds(bounds, pb);
|
|
434
|
+
}
|
|
435
|
+
return bounds;
|
|
436
|
+
}
|
|
437
|
+
/** True when axis-aligned bounds lie fully inside the viewBox (within overflow epsilon). */
|
|
438
|
+
export function contentFitsWithinViewBox(viewBox, bounds) {
|
|
439
|
+
const overflow = {
|
|
440
|
+
left: Math.max(0, viewBox.minX - bounds.minX),
|
|
441
|
+
right: Math.max(0, bounds.maxX - (viewBox.minX + viewBox.width)),
|
|
442
|
+
top: Math.max(0, viewBox.minY - bounds.minY),
|
|
443
|
+
bottom: Math.max(0, bounds.maxY - (viewBox.minY + viewBox.height)),
|
|
444
|
+
};
|
|
445
|
+
return overflow.left <= VIEWBOX_OVERFLOW_EPSILON
|
|
446
|
+
&& overflow.right <= VIEWBOX_OVERFLOW_EPSILON
|
|
447
|
+
&& overflow.top <= VIEWBOX_OVERFLOW_EPSILON
|
|
448
|
+
&& overflow.bottom <= VIEWBOX_OVERFLOW_EPSILON;
|
|
449
|
+
}
|
|
450
|
+
export function getViewBoxContentCoverage(viewBox, bounds) {
|
|
451
|
+
const contentW = bounds.maxX - bounds.minX;
|
|
452
|
+
const contentH = bounds.maxY - bounds.minY;
|
|
453
|
+
return {
|
|
454
|
+
widthRatio: viewBox.width > 0 ? contentW / viewBox.width : 0,
|
|
455
|
+
heightRatio: viewBox.height > 0 ? contentH / viewBox.height : 0,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
export function detectSplitFillStrokePaths(paths) {
|
|
459
|
+
const groups = new Map();
|
|
460
|
+
for (const path of paths) {
|
|
461
|
+
const key = normalizePathD(path.d);
|
|
462
|
+
const list = groups.get(key) || [];
|
|
463
|
+
list.push(path);
|
|
464
|
+
groups.set(key, list);
|
|
465
|
+
}
|
|
466
|
+
for (const group of groups.values()) {
|
|
467
|
+
if (group.length < 2)
|
|
468
|
+
continue;
|
|
469
|
+
const fillOnly = group.some(p => hasVisibleFill(p) && !hasVisibleStroke(p));
|
|
470
|
+
const strokeOnly = group.some(p => hasVisibleStroke(p) && !hasVisibleFill(p));
|
|
471
|
+
if (fillOnly && strokeOnly)
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
export function checkViewBoxFitsVisibleContent(svgSource, visiblePaths) {
|
|
477
|
+
const viewBox = parseViewBox(svgSource);
|
|
478
|
+
const visibleBounds = getContentBounds(visiblePaths);
|
|
479
|
+
if (!viewBox) {
|
|
480
|
+
return { pass: false, failureKind: 'missing_viewbox', reason: 'missing or invalid viewBox' };
|
|
481
|
+
}
|
|
482
|
+
if (!visibleBounds) {
|
|
483
|
+
return { pass: false, failureKind: 'no_visible_geometry', viewBox, reason: 'no measurable visible path geometry' };
|
|
484
|
+
}
|
|
485
|
+
const overflow = {
|
|
486
|
+
left: Math.max(0, viewBox.minX - visibleBounds.minX),
|
|
487
|
+
right: Math.max(0, visibleBounds.maxX - (viewBox.minX + viewBox.width)),
|
|
488
|
+
top: Math.max(0, viewBox.minY - visibleBounds.minY),
|
|
489
|
+
bottom: Math.max(0, visibleBounds.maxY - (viewBox.minY + viewBox.height)),
|
|
490
|
+
};
|
|
491
|
+
const visiblePathStart = visiblePaths.length > 0 ? getPathStartPoint(visiblePaths[0].d) ?? undefined : undefined;
|
|
492
|
+
const contentW = visibleBounds.maxX - visibleBounds.minX;
|
|
493
|
+
const contentH = visibleBounds.maxY - visibleBounds.minY;
|
|
494
|
+
const contentCoverage = {
|
|
495
|
+
widthRatio: viewBox.width > 0 ? contentW / viewBox.width : 0,
|
|
496
|
+
heightRatio: viewBox.height > 0 ? contentH / viewBox.height : 0,
|
|
497
|
+
};
|
|
498
|
+
const hasOverflow = overflow.left > VIEWBOX_OVERFLOW_EPSILON
|
|
499
|
+
|| overflow.right > VIEWBOX_OVERFLOW_EPSILON
|
|
500
|
+
|| overflow.top > VIEWBOX_OVERFLOW_EPSILON
|
|
501
|
+
|| overflow.bottom > VIEWBOX_OVERFLOW_EPSILON;
|
|
502
|
+
if (hasOverflow) {
|
|
503
|
+
const vb = viewBox;
|
|
504
|
+
return {
|
|
505
|
+
pass: false,
|
|
506
|
+
failureKind: 'content_outside_viewbox',
|
|
507
|
+
viewBox,
|
|
508
|
+
visibleBounds,
|
|
509
|
+
overflow,
|
|
510
|
+
visiblePathStart,
|
|
511
|
+
contentCoverage,
|
|
512
|
+
reason: `visible content outside viewBox (overflow L${overflow.left.toFixed(1)} R${overflow.right.toFixed(1)} T${overflow.top.toFixed(1)} B${overflow.bottom.toFixed(1)}; visible y ${visibleBounds.minY.toFixed(1)}–${visibleBounds.maxY.toFixed(1)} vs viewBox height ${vb.height.toFixed(1)})`,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
return { pass: true, viewBox, visibleBounds, overflow, visiblePathStart, contentCoverage };
|
|
516
|
+
}
|
|
517
|
+
export function checkCenteredTightViewBox(svgSource, paths) {
|
|
518
|
+
const viewBox = parseViewBox(svgSource);
|
|
519
|
+
const content = getContentBounds(paths);
|
|
520
|
+
if (!viewBox) {
|
|
521
|
+
return { pass: false, failureKind: 'missing_viewbox', reason: 'missing or invalid viewBox' };
|
|
522
|
+
}
|
|
523
|
+
if (!content) {
|
|
524
|
+
return { pass: false, failureKind: 'no_geometry', viewBox, reason: 'no measurable visible path geometry' };
|
|
525
|
+
}
|
|
526
|
+
const padTolX = Math.max(VIEWBOX_PADDING_TOLERANCE_MIN_PX, viewBox.width * VIEWBOX_PADDING_TOLERANCE_RATIO);
|
|
527
|
+
const padTolY = Math.max(VIEWBOX_PADDING_TOLERANCE_MIN_PX, viewBox.height * VIEWBOX_PADDING_TOLERANCE_RATIO);
|
|
528
|
+
const padLeft = content.minX - viewBox.minX;
|
|
529
|
+
const padRight = (viewBox.minX + viewBox.width) - content.maxX;
|
|
530
|
+
const padTop = content.minY - viewBox.minY;
|
|
531
|
+
const padBottom = (viewBox.minY + viewBox.height) - content.maxY;
|
|
532
|
+
const padding = {
|
|
533
|
+
left: sideMetric(padLeft, padTolX),
|
|
534
|
+
right: sideMetric(padRight, padTolX),
|
|
535
|
+
top: sideMetric(padTop, padTolY),
|
|
536
|
+
bottom: sideMetric(padBottom, padTolY),
|
|
537
|
+
};
|
|
538
|
+
const viewCx = viewBox.minX + viewBox.width / 2;
|
|
539
|
+
const viewCy = viewBox.minY + viewBox.height / 2;
|
|
540
|
+
const contentCx = (content.minX + content.maxX) / 2;
|
|
541
|
+
const contentCy = (content.minY + content.maxY) / 2;
|
|
542
|
+
const dx = Math.abs(contentCx - viewCx);
|
|
543
|
+
const dy = Math.abs(contentCy - viewCy);
|
|
544
|
+
const centerOffset = {
|
|
545
|
+
dx, dy,
|
|
546
|
+
toleranceX: padTolX,
|
|
547
|
+
toleranceY: padTolY,
|
|
548
|
+
excessDx: Math.max(0, dx - padTolX),
|
|
549
|
+
excessDy: Math.max(0, dy - padTolY),
|
|
550
|
+
};
|
|
551
|
+
if (padding.left.excess > 0 || padding.right.excess > 0 || padding.top.excess > 0 || padding.bottom.excess > 0) {
|
|
552
|
+
const coverage = getViewBoxContentCoverage(viewBox, content);
|
|
553
|
+
const lowCoverage = Math.max(coverage.widthRatio, coverage.heightRatio) < VIEWBOX_MIN_CONTENT_COVERAGE;
|
|
554
|
+
const svgEditNote = lowCoverage
|
|
555
|
+
? '; shared document canvas padding breaks SVG edit drag preview — trim viewBox to visible content'
|
|
556
|
+
: '';
|
|
557
|
+
return {
|
|
558
|
+
pass: false,
|
|
559
|
+
failureKind: 'excess_padding',
|
|
560
|
+
viewBox, contentBounds: content, padding, centerOffset,
|
|
561
|
+
reason: `excess viewBox padding (L${padLeft.toFixed(1)} R${padRight.toFixed(1)} T${padTop.toFixed(1)} B${padBottom.toFixed(1)}, tol ${padTolX.toFixed(1)}/${padTolY.toFixed(1)}; content spans ${(coverage.widthRatio * 100).toFixed(0)}%×${(coverage.heightRatio * 100).toFixed(0)}% of viewBox${svgEditNote})`,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
if (centerOffset.excessDx > 0 || centerOffset.excessDy > 0) {
|
|
565
|
+
return {
|
|
566
|
+
pass: false,
|
|
567
|
+
failureKind: 'off_center',
|
|
568
|
+
viewBox, contentBounds: content, padding, centerOffset,
|
|
569
|
+
reason: `content off-center (Δx=${dx.toFixed(1)}, Δy=${dy.toFixed(1)}, tol ${padTolX.toFixed(1)}/${padTolY.toFixed(1)})`,
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
return { pass: true, viewBox, contentBounds: content, padding, centerOffset };
|
|
573
|
+
}
|
|
574
|
+
export function formatViewBoxFitFailure(metrics) {
|
|
575
|
+
if (metrics.pass)
|
|
576
|
+
return 'pass';
|
|
577
|
+
if (metrics.failureKind === 'missing_viewbox')
|
|
578
|
+
return 'missing or invalid viewBox';
|
|
579
|
+
if (metrics.failureKind === 'no_visible_geometry')
|
|
580
|
+
return 'no measurable visible path geometry';
|
|
581
|
+
if (metrics.failureKind === 'content_outside_viewbox' && metrics.overflow && metrics.visibleBounds && metrics.viewBox) {
|
|
582
|
+
const o = metrics.overflow;
|
|
583
|
+
const vb = metrics.viewBox;
|
|
584
|
+
const b = metrics.visibleBounds;
|
|
585
|
+
const parts = [];
|
|
586
|
+
if (o.left > VIEWBOX_OVERFLOW_EPSILON)
|
|
587
|
+
parts.push(`overflow left +${o.left.toFixed(1)}`);
|
|
588
|
+
if (o.right > VIEWBOX_OVERFLOW_EPSILON)
|
|
589
|
+
parts.push(`overflow right +${o.right.toFixed(1)}`);
|
|
590
|
+
if (o.top > VIEWBOX_OVERFLOW_EPSILON)
|
|
591
|
+
parts.push(`overflow top +${o.top.toFixed(1)}`);
|
|
592
|
+
if (o.bottom > VIEWBOX_OVERFLOW_EPSILON)
|
|
593
|
+
parts.push(`overflow bottom +${o.bottom.toFixed(1)}`);
|
|
594
|
+
const start = metrics.visiblePathStart;
|
|
595
|
+
const startNote = start ? `; visible path starts at (${start.x.toFixed(1)}, ${start.y.toFixed(1)})` : '';
|
|
596
|
+
const coverage = metrics.contentCoverage;
|
|
597
|
+
const coverageNote = coverage
|
|
598
|
+
? `; content spans ${(coverage.heightRatio * 100).toFixed(0)}% of viewBox height (y ${b.minY.toFixed(1)}–${b.maxY.toFixed(1)} vs viewBox h ${vb.height.toFixed(1)})`
|
|
599
|
+
: '';
|
|
600
|
+
return `content outside viewBox — ${parts.join(', ')}${startNote}${coverageNote}`;
|
|
601
|
+
}
|
|
602
|
+
return metrics.reason || 'viewBox fit check failed';
|
|
603
|
+
}
|
|
604
|
+
export function formatViewBoxFailure(metrics) {
|
|
605
|
+
if (metrics.pass)
|
|
606
|
+
return 'pass';
|
|
607
|
+
if (metrics.failureKind === 'missing_viewbox')
|
|
608
|
+
return 'missing or invalid viewBox';
|
|
609
|
+
if (metrics.failureKind === 'no_geometry')
|
|
610
|
+
return 'no measurable path geometry';
|
|
611
|
+
if (metrics.failureKind === 'excess_padding' && metrics.padding) {
|
|
612
|
+
const sides = [
|
|
613
|
+
['L', metrics.padding.left], ['R', metrics.padding.right],
|
|
614
|
+
['T', metrics.padding.top], ['B', metrics.padding.bottom],
|
|
615
|
+
];
|
|
616
|
+
const bad = sides
|
|
617
|
+
.filter(([, m]) => m.excess > 0)
|
|
618
|
+
.map(([label, m]) => `${label} ${m.value.toFixed(1)}px (tol ${m.tolerance.toFixed(1)}, +${m.excess.toFixed(1)})`);
|
|
619
|
+
return bad.length > 0 ? `excess padding — ${bad.join(', ')}` : (metrics.reason || 'excess viewBox padding');
|
|
620
|
+
}
|
|
621
|
+
if (metrics.failureKind === 'off_center' && metrics.centerOffset) {
|
|
622
|
+
const c = metrics.centerOffset;
|
|
623
|
+
return `off-center — Δx=${c.dx.toFixed(1)} (tol ${c.toleranceX.toFixed(1)}, +${c.excessDx.toFixed(1)}), Δy=${c.dy.toFixed(1)} (tol ${c.toleranceY.toFixed(1)}, +${c.excessDy.toFixed(1)})`;
|
|
624
|
+
}
|
|
625
|
+
return metrics.reason || 'viewBox check failed';
|
|
626
|
+
}
|
|
627
|
+
export function viewBoxFitFailureSeverity(metrics) {
|
|
628
|
+
if (metrics.pass)
|
|
629
|
+
return 0;
|
|
630
|
+
if (!metrics.overflow)
|
|
631
|
+
return Number.POSITIVE_INFINITY;
|
|
632
|
+
return Math.max(metrics.overflow.left, metrics.overflow.right, metrics.overflow.top, metrics.overflow.bottom);
|
|
633
|
+
}
|
|
634
|
+
export function viewBoxFailureSeverity(metrics) {
|
|
635
|
+
if (metrics.pass)
|
|
636
|
+
return 0;
|
|
637
|
+
let worst = 0;
|
|
638
|
+
if (metrics.padding) {
|
|
639
|
+
for (const side of [metrics.padding.left, metrics.padding.right, metrics.padding.top, metrics.padding.bottom]) {
|
|
640
|
+
worst = Math.max(worst, side.excess);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
if (metrics.centerOffset) {
|
|
644
|
+
worst = Math.max(worst, metrics.centerOffset.excessDx, metrics.centerOffset.excessDy);
|
|
645
|
+
}
|
|
646
|
+
if (metrics.failureKind === 'missing_viewbox' || metrics.failureKind === 'no_geometry') {
|
|
647
|
+
return Number.POSITIVE_INFINITY;
|
|
648
|
+
}
|
|
649
|
+
return worst;
|
|
650
|
+
}
|
|
651
|
+
export function classifyAssetRef(ref) {
|
|
652
|
+
if (!ref || ref.includes('[MEDIA_REMOVED]'))
|
|
653
|
+
return 'missing';
|
|
654
|
+
if (ref.trim().startsWith('<svg'))
|
|
655
|
+
return 'svg';
|
|
656
|
+
if (ref.startsWith('data:image/svg+xml'))
|
|
657
|
+
return 'svg';
|
|
658
|
+
if (/^data:image\/(png|jpe?g|gif|webp)/i.test(ref))
|
|
659
|
+
return 'raster';
|
|
660
|
+
return 'missing';
|
|
661
|
+
}
|
|
662
|
+
function buildHiddenPathDetails(paths) {
|
|
663
|
+
return paths
|
|
664
|
+
.filter(p => !p.visible && p.hiddenReason)
|
|
665
|
+
.map(p => ({
|
|
666
|
+
index: p.index,
|
|
667
|
+
fill: p.fill,
|
|
668
|
+
reason: p.hiddenReason,
|
|
669
|
+
bounds: getPathBounds(p.d),
|
|
670
|
+
}));
|
|
671
|
+
}
|
|
672
|
+
export function analyzeSvgAsset(svgSource) {
|
|
673
|
+
const failures = [];
|
|
674
|
+
const warnings = [];
|
|
675
|
+
const allPaths = extractSvgPaths(svgSource);
|
|
676
|
+
const visiblePaths = getVisiblePaths(allPaths);
|
|
677
|
+
const hiddenPaths = buildHiddenPathDetails(allPaths);
|
|
678
|
+
const embeddedImages = extractEmbeddedImages(svgSource);
|
|
679
|
+
const forbiddenElementCount = countForbiddenNonVectorElements(svgSource);
|
|
680
|
+
const embeddedImageCount = embeddedImages.length;
|
|
681
|
+
const vertexCount = visiblePaths.reduce((sum, p) => sum + countPathVertices(p.d), 0);
|
|
682
|
+
const hasSplitFillStroke = detectSplitFillStrokePaths(visiblePaths);
|
|
683
|
+
const colorOutlineOnSameShape = !hasSplitFillStroke;
|
|
684
|
+
if (hasSplitFillStroke) {
|
|
685
|
+
failures.push('fill and stroke are on separate paths with identical geometry');
|
|
686
|
+
}
|
|
687
|
+
const rootUnits = parseSvgRootUnits(svgSource);
|
|
688
|
+
const pixelUnitsOnly = rootUnits.pixelUnitsOnly;
|
|
689
|
+
if (!pixelUnitsOnly) {
|
|
690
|
+
const unitParts = [];
|
|
691
|
+
if (rootUnits.widthUnit !== 'px' && rootUnits.widthUnit !== 'missing') {
|
|
692
|
+
unitParts.push(`width=${rootUnits.widthRaw} (${rootUnits.widthUnit})`);
|
|
693
|
+
}
|
|
694
|
+
if (rootUnits.heightUnit !== 'px' && rootUnits.heightUnit !== 'missing') {
|
|
695
|
+
unitParts.push(`height=${rootUnits.heightRaw} (${rootUnits.heightUnit})`);
|
|
696
|
+
}
|
|
697
|
+
failures.push(`root dimensions must use px units${unitParts.length ? ` (${unitParts.join(', ')})` : ''}`);
|
|
698
|
+
}
|
|
699
|
+
const preserveAspectRatioNone = checkPreserveAspectRatioNone(svgSource);
|
|
700
|
+
if (!preserveAspectRatioNone) {
|
|
701
|
+
failures.push('root preserveAspectRatio="none" required at export');
|
|
702
|
+
}
|
|
703
|
+
const vectorIssues = [];
|
|
704
|
+
if (forbiddenElementCount > 0) {
|
|
705
|
+
vectorIssues.push(`${forbiddenElementCount} non-vector element(s) (text/use/foreignObject)`);
|
|
706
|
+
}
|
|
707
|
+
if (embeddedImageCount > 0) {
|
|
708
|
+
const embeddedRaster = embeddedImages.filter(i => i.hrefKind === 'embedded_raster');
|
|
709
|
+
if (embeddedRaster.length > 0) {
|
|
710
|
+
vectorIssues.push(`${embeddedRaster.length} embedded raster <image> fallback(s)`);
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
vectorIssues.push(`${embeddedImageCount} <image> element(s)`);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (hiddenPaths.length > 0) {
|
|
717
|
+
vectorIssues.push(`${hiddenPaths.length} hidden path(s)`);
|
|
718
|
+
}
|
|
719
|
+
const pathsOnly = vectorIssues.length === 0;
|
|
720
|
+
if (!pathsOnly) {
|
|
721
|
+
failures.push(`vector clean — ${vectorIssues.join('; ')}`);
|
|
722
|
+
}
|
|
723
|
+
if (hiddenPaths.length > 0) {
|
|
724
|
+
for (const hp of hiddenPaths) {
|
|
725
|
+
const fillNote = hp.fill ? ` fill=${hp.fill}` : '';
|
|
726
|
+
warnings.push(`hidden path[${hp.index}]${fillNote} (${hp.reason.replace('_', ':')})`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
const viewBoxFit = checkViewBoxFitsVisibleContent(svgSource, visiblePaths);
|
|
730
|
+
const viewBoxMatchesVisibleContent = viewBoxFit.pass;
|
|
731
|
+
if (!viewBoxFit.pass && viewBoxFit.reason) {
|
|
732
|
+
failures.push(viewBoxFit.reason);
|
|
733
|
+
}
|
|
734
|
+
const viewBoxCheck = checkCenteredTightViewBox(svgSource, visiblePaths);
|
|
735
|
+
const centeredTightViewBox = viewBoxCheck.pass;
|
|
736
|
+
if (!viewBoxCheck.pass && viewBoxCheck.reason) {
|
|
737
|
+
failures.push(viewBoxCheck.reason);
|
|
738
|
+
}
|
|
739
|
+
return {
|
|
740
|
+
vertexCount,
|
|
741
|
+
visiblePathCount: visiblePaths.length,
|
|
742
|
+
hiddenPathCount: hiddenPaths.length,
|
|
743
|
+
forbiddenElementCount,
|
|
744
|
+
embeddedImageCount,
|
|
745
|
+
colorOutlineOnSameShape,
|
|
746
|
+
pathsOnly,
|
|
747
|
+
pixelUnitsOnly,
|
|
748
|
+
preserveAspectRatioNone,
|
|
749
|
+
viewBoxMatchesVisibleContent,
|
|
750
|
+
centeredTightViewBox,
|
|
751
|
+
viewBoxFit,
|
|
752
|
+
viewBoxMetrics: viewBoxCheck,
|
|
753
|
+
rootUnits,
|
|
754
|
+
hiddenPaths,
|
|
755
|
+
embeddedImages,
|
|
756
|
+
failures,
|
|
757
|
+
warnings,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
//# sourceMappingURL=svgAssetAnalysis.js.map
|