@vvfx/svg2ir 0.0.1-beta.0 → 0.0.1-beta.11
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/README.md +62 -33
- package/dist/common/types.d.ts +6 -2
- package/dist/convert.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1226 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1226 -38
- package/dist/index.mjs.map +1 -1
- package/dist/node.js +1228 -39
- package/dist/node.js.map +1 -1
- package/dist/node.mjs +1228 -40
- package/dist/node.mjs.map +1 -1
- package/dist/parse-svg/build-element-tree-path.d.ts +2 -0
- package/dist/svg-rasterizer/prepare-snapshot.d.ts +9 -0
- package/dist/svg-rasterizer/rasterize-svg.d.ts +11 -0
- package/dist/svg2ir/assemble.d.ts +3 -1
- package/dist/svg2ir/convert.d.ts +3 -1
- package/dist/svg2ir/converter/layers/conversion-session.d.ts +2 -0
- package/dist/svg2ir/converter/text/font-metrics.d.ts +2 -0
- package/dist/svg2layers-adapter/assemble.d.ts +15 -2
- package/dist/svg2layers-adapter/baking/build-baked-layers.d.ts +13 -0
- package/dist/svg2layers-adapter/baking/normalize-layer-blending.d.ts +10 -0
- package/dist/svg2layers-adapter/baking/plan-graphic-baking.d.ts +18 -0
- package/dist/svg2layers-adapter/baking/resolve-baking-warnings.d.ts +4 -0
- package/dist/svg2layers-adapter/convert.d.ts +2 -2
- package/dist/svg2layers-adapter/converter/image/inspect-mask.d.ts +3 -0
- package/dist/svg2layers-adapter/converter/layers/build-layers.d.ts +1 -1
- package/dist/svg2layers-adapter/strip-layer-animation.d.ts +5 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Description: SVG to Animation IR and Galacean Effects conversion engine
|
|
4
4
|
* Author: Ant Group CO., Ltd.
|
|
5
5
|
* Contributors: 鳞息
|
|
6
|
-
* Version: v0.0.1-beta.
|
|
6
|
+
* Version: v0.0.1-beta.11
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { IRPaintType, IRLineJoin, IRLineCap, IRShapePrimitiveType, IRLayerType, ANIMATION_IR_SCHEMA_VERSION, validateIRScene, IRBlendMode } from '@vvfx/animation-ir';
|
|
@@ -1505,7 +1505,7 @@ function parseHexColor(raw) {
|
|
|
1505
1505
|
return null;
|
|
1506
1506
|
}
|
|
1507
1507
|
var hex = match[1];
|
|
1508
|
-
var expanded = hex.length <= 4 ?
|
|
1508
|
+
var expanded = hex.length <= 4 ? Array.from(hex).map(function(char) {
|
|
1509
1509
|
return "" + char + char;
|
|
1510
1510
|
}).join('') : hex;
|
|
1511
1511
|
var r = parseInt(expanded.slice(0, 2), 16) / 255;
|
|
@@ -2606,7 +2606,24 @@ function parseSMILTag(tag, root, beginCache, comparisonDuration, valueParser, un
|
|
|
2606
2606
|
return maxEnd;
|
|
2607
2607
|
}
|
|
2608
2608
|
|
|
2609
|
-
/**
|
|
2609
|
+
/** Structural source identity, independent of optional or duplicate SVG IDs. */ function buildElementTreePath(element) {
|
|
2610
|
+
var indices = [];
|
|
2611
|
+
var current = element;
|
|
2612
|
+
while(isElementNode(current.parentNode)){
|
|
2613
|
+
var parent = current.parentNode;
|
|
2614
|
+
indices.unshift(Array.from(parent.children).indexOf(current));
|
|
2615
|
+
current = parent;
|
|
2616
|
+
}
|
|
2617
|
+
return indices.join('/');
|
|
2618
|
+
}
|
|
2619
|
+
function isElementNode(node) {
|
|
2620
|
+
return (node == null ? void 0 : node.nodeType) === 1;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
/** SVG 转换诊断码;用于报告烘焙、降级和不支持的特性。 */ var SVG2IR_WARNING_CODES = {
|
|
2624
|
+
/** 图形或图形组已栅格化,矢量编辑能力变化通过 warning 报告。 */ LAYERS_GRAPHIC_BAKED: 'svg2ge_layers_graphic_baked',
|
|
2625
|
+
LAYERS_BLEND_MODE_DOWNGRADED: 'svg2ge_layers_blend_mode_downgraded',
|
|
2626
|
+
LAYERS_ANIMATION_STRIPPED: 'svg2ge_layers_animation_stripped',
|
|
2610
2627
|
LAYERS_UNSUPPORTED_TYPE: 'svg2ge_layers_unsupported_type',
|
|
2611
2628
|
LAYERS_UNSUPPORTED_FEATURE: 'svg2ge_layers_unsupported_feature',
|
|
2612
2629
|
LAYERS_INVALID_DATA: 'svg2ge_layers_invalid_data',
|
|
@@ -6126,8 +6143,9 @@ function readVec3(channel) {
|
|
|
6126
6143
|
node
|
|
6127
6144
|
], Array.from(node.querySelectorAll('*')));
|
|
6128
6145
|
var hasUnsupportedRule = ruleNodes.some(function(candidate) {
|
|
6146
|
+
var _ref, _ref1;
|
|
6129
6147
|
var _candidate_getAttribute, _candidate_getAttribute1;
|
|
6130
|
-
return ((_candidate_getAttribute = candidate.getAttribute('clip-rule')) == null ? void 0 : _candidate_getAttribute.trim()) !== '' || ((_candidate_getAttribute1 = candidate.getAttribute('fill-rule')) == null ? void 0 : _candidate_getAttribute1.trim()) !== '';
|
|
6148
|
+
return ((_ref = (_candidate_getAttribute = candidate.getAttribute('clip-rule')) == null ? void 0 : _candidate_getAttribute.trim()) != null ? _ref : '') !== '' || ((_ref1 = (_candidate_getAttribute1 = candidate.getAttribute('fill-rule')) == null ? void 0 : _candidate_getAttribute1.trim()) != null ? _ref1 : '') !== '';
|
|
6131
6149
|
});
|
|
6132
6150
|
if (hasUnsupportedRule) {
|
|
6133
6151
|
warn(SVG2IR_WARNING_CODES.CLIP_RULE_DROPPED, "" + tag + " 内显式 clip-rule/fill-rule 未映射,IR 只保留 mask 几何,不保留该规则。");
|
|
@@ -6968,8 +6986,10 @@ function createSVGConversionSession(es, options) {
|
|
|
6968
6986
|
};
|
|
6969
6987
|
var appendLayer = function appendLayer(layer, element, applyElementClip) {
|
|
6970
6988
|
if (applyElementClip === void 0) applyElementClip = true;
|
|
6989
|
+
var _options_layerSourceCollector;
|
|
6971
6990
|
var clippedLayer = applyElementClip ? applyClipMask(es, element, layer, clipMasks) : layer;
|
|
6972
6991
|
layers.push(withGroupParent(clippedLayer, element));
|
|
6992
|
+
(_options_layerSourceCollector = options.layerSourceCollector) == null ? void 0 : _options_layerSourceCollector.set(layer.id, buildElementTreePath(element.node));
|
|
6973
6993
|
};
|
|
6974
6994
|
var appendAsset = function appendAsset(asset) {
|
|
6975
6995
|
if (registeredAssetIds.has(asset.id)) {
|
|
@@ -10377,7 +10397,7 @@ function mapFontWeight(raw, fontFamily) {
|
|
|
10377
10397
|
seen.set(Math.round(w * 100), ch);
|
|
10378
10398
|
}
|
|
10379
10399
|
}
|
|
10380
|
-
return
|
|
10400
|
+
return Array.from(seen.entries()).map(function(param) {
|
|
10381
10401
|
var k = param[0], ch = param[1];
|
|
10382
10402
|
return {
|
|
10383
10403
|
ch: ch,
|
|
@@ -10427,6 +10447,9 @@ function mapFontWeight(raw, fontFamily) {
|
|
|
10427
10447
|
// latin / digit / 标点:典型比例字体 sans-serif 大写字母约 0.65em,数字稍窄。
|
|
10428
10448
|
return /[0-9]/.test(ch) ? 0.55 : 0.62;
|
|
10429
10449
|
}
|
|
10450
|
+
/** 去除单个字体族名的 CSS 字符串引号。 */ function normalizeFontFamilyName(fontFamily) {
|
|
10451
|
+
return fontFamily.trim().replace(/^(['"])(.*)\1$/, '$2');
|
|
10452
|
+
}
|
|
10430
10453
|
/** 构造与 Effects Runtime getFontDesc 一致的 CSS font 简写字符串,用于 Canvas measureText。 */ function buildCanvasFontDesc(fontSize, fontFamily, fontWeight, fontStyle) {
|
|
10431
10454
|
var weightStr = String(fontWeight);
|
|
10432
10455
|
var genericFamilies = [
|
|
@@ -10435,7 +10458,8 @@ function mapFontWeight(raw, fontFamily) {
|
|
|
10435
10458
|
'monospace',
|
|
10436
10459
|
'courier'
|
|
10437
10460
|
];
|
|
10438
|
-
var
|
|
10461
|
+
var family = normalizeFontFamilyName(fontFamily);
|
|
10462
|
+
var familyName = genericFamilies.includes(family) ? family : '"' + family + '"';
|
|
10439
10463
|
var desc = fontSize + "px " + familyName;
|
|
10440
10464
|
if (weightStr !== 'normal' && weightStr !== '') {
|
|
10441
10465
|
desc = weightStr + " " + desc;
|
|
@@ -10446,13 +10470,13 @@ function mapFontWeight(raw, fontFamily) {
|
|
|
10446
10470
|
return desc;
|
|
10447
10471
|
}
|
|
10448
10472
|
/** 文本宽度估算。优先使用调用方提供的 Canvas measureText,否则使用字符级启发式系数。 */ function estimateTextWidth(content, fontSize, letterSpacing, measureText, fontFamily, fontWeight, fontStyle) {
|
|
10449
|
-
var
|
|
10473
|
+
var chars = Array.from(content);
|
|
10474
|
+
var len = chars.length;
|
|
10450
10475
|
if (measureText != null && fontFamily !== '') {
|
|
10451
10476
|
var fontDesc = buildCanvasFontDesc(fontSize, fontFamily, fontWeight, fontStyle);
|
|
10452
10477
|
var width = measureText(fontDesc, content).width;
|
|
10453
10478
|
return width + letterSpacing * Math.max(len - 1, 0);
|
|
10454
10479
|
}
|
|
10455
|
-
var chars = [].concat(content);
|
|
10456
10480
|
var width1 = chars.reduce(function(s, ch) {
|
|
10457
10481
|
return s + charWidthCoef(ch) * fontSize;
|
|
10458
10482
|
}, 0);
|
|
@@ -12049,7 +12073,16 @@ function createSVGElementConverterRegistry(session) {
|
|
|
12049
12073
|
}
|
|
12050
12074
|
|
|
12051
12075
|
function buildSVGLayers(es, options) {
|
|
12052
|
-
var
|
|
12076
|
+
var currentPath;
|
|
12077
|
+
var warningSourceCollector = options.warningSourceCollector;
|
|
12078
|
+
var session = createSVGConversionSession(es, warningSourceCollector == null ? options : _extends({}, options, {
|
|
12079
|
+
emit: function emit(warning) {
|
|
12080
|
+
if (currentPath != null) {
|
|
12081
|
+
warningSourceCollector.set(warning, currentPath);
|
|
12082
|
+
}
|
|
12083
|
+
options.emit(warning);
|
|
12084
|
+
}
|
|
12085
|
+
}));
|
|
12053
12086
|
var converters = createSVGElementConverterRegistry(session);
|
|
12054
12087
|
for(var _iterator = _create_for_of_iterator_helper_loose(session.all), _step; !(_step = _iterator()).done;){
|
|
12055
12088
|
var element = _step.value;
|
|
@@ -12057,6 +12090,9 @@ function buildSVGLayers(es, options) {
|
|
|
12057
12090
|
var _element_node_tagName, _element_node;
|
|
12058
12091
|
var tag = (_ref = (_element_node = element.node) == null ? void 0 : (_element_node_tagName = _element_node.tagName) == null ? void 0 : _element_node_tagName.toLowerCase()) != null ? _ref : '';
|
|
12059
12092
|
var selector = describeElement(element);
|
|
12093
|
+
if (warningSourceCollector != null) {
|
|
12094
|
+
currentPath = buildElementTreePath(element.node);
|
|
12095
|
+
}
|
|
12060
12096
|
if (isInInactiveSwitchBranch(element)) {
|
|
12061
12097
|
continue;
|
|
12062
12098
|
}
|
|
@@ -12090,11 +12126,10 @@ function buildSVGLayers(es, options) {
|
|
|
12090
12126
|
|
|
12091
12127
|
var ParseableSVG = /*#__PURE__*/ function() {
|
|
12092
12128
|
function ParseableSVG(svgStr, _options) {
|
|
12093
|
-
var
|
|
12094
|
-
|
|
12095
|
-
|
|
12129
|
+
var _SVG;
|
|
12130
|
+
// svg.js parses a fragment; remove only the prolog and let the DOM handle nested markup.
|
|
12131
|
+
var content = svgStr.replace(/^\uFEFF/, '').replace(/^(?:\s|<\?xml\b[\s\S]*?\?>|<!DOCTYPE\b[^>]*>|<!--[\s\S]*?-->)+/i, '');
|
|
12096
12132
|
// svg.js SVG(string) 的 d.ts 标为 Element,但对完整 <svg> 文档运行时返回根 SVG 文档实例。
|
|
12097
|
-
var content = typeof svgStr === 'string' ? (_ref = (_svgStr_match = svgStr.match(RegExp("<svg\\s+.*?>.*?<\\/svg>", "s"))) == null ? void 0 : _svgStr_match[0]) != null ? _ref : svgStr : svgStr;
|
|
12098
12133
|
// svg.js 对非 SVG 字符串(如 'not svg')解析返 null。兜底空 svg:让 svg2ir 上层
|
|
12099
12134
|
// hasSVGRootElement 检查(非 svg 抛 INVALID_SVG)生效,而非在构造期抛错。
|
|
12100
12135
|
this.svg = (_SVG = SVG(content)) != null ? _SVG : SVG('<svg xmlns="http://www.w3.org/2000/svg"/>');
|
|
@@ -12297,6 +12332,8 @@ function parseSVGToIRScene(svgString, options) {
|
|
|
12297
12332
|
value: scannedSmilDuration
|
|
12298
12333
|
};
|
|
12299
12334
|
var layerBuildOptions = {
|
|
12335
|
+
layerSourceCollector: options == null ? void 0 : options.layerSourceCollector,
|
|
12336
|
+
warningSourceCollector: options == null ? void 0 : options.warningSourceCollector,
|
|
12300
12337
|
frameRate: frameRate,
|
|
12301
12338
|
durationSeconds: effectiveDuration,
|
|
12302
12339
|
emit: emit,
|
|
@@ -12476,6 +12513,8 @@ function parseSVGToIRWithPreservedData(source, options, collectors) {
|
|
|
12476
12513
|
var svgString = readSVGString(source);
|
|
12477
12514
|
var irScene = parseSVGToIRScene(svgString, _extends({}, options, {
|
|
12478
12515
|
onWarning: emitWarning,
|
|
12516
|
+
layerSourceCollector: collectors == null ? void 0 : collectors.layerSourceCollector,
|
|
12517
|
+
warningSourceCollector: collectors == null ? void 0 : collectors.warningSourceCollector,
|
|
12479
12518
|
textDataCollector: collectors == null ? void 0 : collectors.textDataByLayerId,
|
|
12480
12519
|
paintAnimationsCollector: collectors == null ? void 0 : collectors.paintAnimationsByLayerId,
|
|
12481
12520
|
shapeAnimationCollector: collectors == null ? void 0 : collectors.shapeAnimationsByLayerId
|
|
@@ -12651,7 +12690,8 @@ function collectLayerWarning(warnings, options, code, message, selector) {
|
|
|
12651
12690
|
});
|
|
12652
12691
|
warnings.push(warning);
|
|
12653
12692
|
options.onWarning == null ? void 0 : options.onWarning.call(options, warning);
|
|
12654
|
-
|
|
12693
|
+
var advisory = code === SVG2IR_WARNING_CODES.LAYERS_TEXT_BOUNDS_ESTIMATED || code === SVG2IR_WARNING_CODES.LAYERS_GRAPHIC_BAKED;
|
|
12694
|
+
if (options.strict === true && !advisory) {
|
|
12655
12695
|
throw new SVGConverterError(SVG_CONVERTER_ERROR_CODES.LAYERS_CONVERSION_FAILED, warning.message, {
|
|
12656
12696
|
stage: 'layers',
|
|
12657
12697
|
sourceId: selector,
|
|
@@ -12976,9 +13016,12 @@ function buildTextRunLayout(runs, family, spacing, content, size, conversionOpti
|
|
|
12976
13016
|
// 分段文字使用统一参考字号度量字宽和字体边界,再按片段字号缩放。
|
|
12977
13017
|
var measurementSize = segmented ? 10 : run.fontSize;
|
|
12978
13018
|
var scale = run.fontSize / measurementSize;
|
|
12979
|
-
var
|
|
13019
|
+
var weight = run.bold ? 'bold' : 'normal';
|
|
13020
|
+
var style = run.italic ? 'italic' : 'normal';
|
|
13021
|
+
var font = buildCanvasFontDesc(measurementSize, family, weight, style);
|
|
12980
13022
|
var metrics = conversionOptions.measureText == null ? void 0 : conversionOptions.measureText.call(conversionOptions, font, segmented ? 'x' : run.text);
|
|
12981
|
-
var
|
|
13023
|
+
var characters = Array.from(run.text);
|
|
13024
|
+
var chunks = segmented ? characters : [
|
|
12982
13025
|
run.text
|
|
12983
13026
|
];
|
|
12984
13027
|
var advance = 0;
|
|
@@ -12987,12 +13030,13 @@ function buildTextRunLayout(runs, family, spacing, content, size, conversionOpti
|
|
|
12987
13030
|
var width = segmented ? conversionOptions.measureText == null ? void 0 : conversionOptions.measureText.call(conversionOptions, font, chunk).width : metrics == null ? void 0 : metrics.width;
|
|
12988
13031
|
var valid = width != null && Number.isFinite(width) && width >= 0;
|
|
12989
13032
|
estimated || (estimated = !valid);
|
|
12990
|
-
advance += valid ? width :
|
|
13033
|
+
advance += valid ? width : estimateTextWidth(chunk, measurementSize, 0, undefined, family, weight, style);
|
|
12991
13034
|
}
|
|
12992
|
-
var characterCount =
|
|
13035
|
+
var characterCount = characters.length;
|
|
12993
13036
|
var width1 = advance * scale + Math.max(0, characterCount - 1) * spacing;
|
|
12994
|
-
var
|
|
12995
|
-
var
|
|
13037
|
+
var fallback = resolveFontMetrics(undefined, family, weight, style, run.fontSize);
|
|
13038
|
+
var ascent = (metrics == null ? void 0 : metrics.fontBoundingBoxAscent) == null ? fallback.asc : metrics.fontBoundingBoxAscent * scale;
|
|
13039
|
+
var descent = (metrics == null ? void 0 : metrics.fontBoundingBoxDescent) == null ? fallback.desc : metrics.fontBoundingBoxDescent * scale;
|
|
12996
13040
|
estimated || (estimated = (metrics == null ? void 0 : metrics.fontBoundingBoxAscent) == null || metrics.fontBoundingBoxDescent == null);
|
|
12997
13041
|
if (!Number.isFinite(width1) || width1 < 0 || !Number.isFinite(ascent) || !Number.isFinite(descent)) {
|
|
12998
13042
|
throwInvalidLayerData('Text measurement returned invalid metrics.');
|
|
@@ -13139,6 +13183,7 @@ function convertIRTextLayer(layer, state, session, id) {
|
|
|
13139
13183
|
content: part.run.text,
|
|
13140
13184
|
bbox: mapPointsToBBox(polygon),
|
|
13141
13185
|
polygon: polygon,
|
|
13186
|
+
// Keep the authored font-family string, quotes included; consumers resolve families themselves.
|
|
13142
13187
|
font_family: font.family,
|
|
13143
13188
|
font_size: roundPixel(part.run.fontSize * scaleY),
|
|
13144
13189
|
color: part.run.color,
|
|
@@ -13183,6 +13228,41 @@ function createLayerConverterRegistry(session) {
|
|
|
13183
13228
|
]);
|
|
13184
13229
|
}
|
|
13185
13230
|
|
|
13231
|
+
/** 完全覆盖图片的直边矩形 mask 不改变可见内容。 */ function maskCoversImage(mask, bounds) {
|
|
13232
|
+
var _Math, _Math1, _Math2, _Math3;
|
|
13233
|
+
var path = mask.path;
|
|
13234
|
+
var epsilon = 1e-6;
|
|
13235
|
+
if (mask.mode === 'subtract' || mask.inverted === true || !path.closed || path.points.length !== 4) {
|
|
13236
|
+
return false;
|
|
13237
|
+
}
|
|
13238
|
+
if (path.inTangents.length !== 4 || path.outTangents.length !== 4 || [].concat(path.inTangents, path.outTangents).some(function(tangent) {
|
|
13239
|
+
return tangent.x !== 0 || tangent.y !== 0;
|
|
13240
|
+
})) {
|
|
13241
|
+
return false;
|
|
13242
|
+
}
|
|
13243
|
+
var straightEdges = path.points.every(function(point, index) {
|
|
13244
|
+
var next = path.points[(index + 1) % 4];
|
|
13245
|
+
var opposite = path.points[(index + 2) % 4];
|
|
13246
|
+
var dx = Math.abs(next.x - point.x);
|
|
13247
|
+
var dy = Math.abs(next.y - point.y);
|
|
13248
|
+
return Number.isFinite(point.x) && Number.isFinite(point.y) && Math.abs(opposite.x - point.x) > epsilon && Math.abs(opposite.y - point.y) > epsilon && (dx <= epsilon && dy > epsilon || dy <= epsilon && dx > epsilon);
|
|
13249
|
+
});
|
|
13250
|
+
if (!straightEdges) {
|
|
13251
|
+
return false;
|
|
13252
|
+
}
|
|
13253
|
+
var xs = path.points.map(function(point) {
|
|
13254
|
+
return point.x;
|
|
13255
|
+
});
|
|
13256
|
+
var ys = path.points.map(function(point) {
|
|
13257
|
+
return point.y;
|
|
13258
|
+
});
|
|
13259
|
+
var left = (_Math = Math).min.apply(_Math, [].concat(xs));
|
|
13260
|
+
var right = (_Math1 = Math).max.apply(_Math1, [].concat(xs));
|
|
13261
|
+
var top = (_Math2 = Math).min.apply(_Math2, [].concat(ys));
|
|
13262
|
+
var bottom = (_Math3 = Math).max.apply(_Math3, [].concat(ys));
|
|
13263
|
+
return bounds.width > 0 && bounds.height > 0 && left <= bounds.x + epsilon && right >= bounds.x + bounds.width - epsilon && top <= bounds.y + epsilon && bottom >= bounds.y + bounds.height - epsilon;
|
|
13264
|
+
}
|
|
13265
|
+
|
|
13186
13266
|
function resolveIRVisualState(layer, session) {
|
|
13187
13267
|
if (!layer.visible) {
|
|
13188
13268
|
return undefined;
|
|
@@ -13191,19 +13271,20 @@ function resolveIRVisualState(layer, session) {
|
|
|
13191
13271
|
if (!state.visible || state.opacity <= 0) {
|
|
13192
13272
|
return undefined;
|
|
13193
13273
|
}
|
|
13194
|
-
|
|
13274
|
+
var unsupportedMask = layer.mask != null && (layer.type !== 'image' || !maskCoversImage(layer.mask, layer.localBounds));
|
|
13275
|
+
if (unsupportedMask || layer.matte != null) {
|
|
13195
13276
|
throwUnsupportedLayerFeature('Mask or clipping is not supported by the flat layer protocol.');
|
|
13196
13277
|
}
|
|
13197
13278
|
if (Math.abs(state.opacity - 1) > 1e-6) {
|
|
13198
13279
|
throwUnsupportedLayerFeature('Layer opacity cannot be expressed by the target protocol.');
|
|
13199
13280
|
}
|
|
13200
13281
|
if (layer.blendMode != null && layer.blendMode !== IRBlendMode.Normal) {
|
|
13201
|
-
|
|
13282
|
+
collectLayerWarning(session.warnings, session.options, SVG2IR_WARNING_CODES.LAYERS_BLEND_MODE_DOWNGRADED, 'Blend mode "' + String(layer.blendMode) + '" was downgraded to "normal"; the layer is retained.', layer.id);
|
|
13202
13283
|
}
|
|
13203
13284
|
return state;
|
|
13204
13285
|
}
|
|
13205
13286
|
|
|
13206
|
-
function buildIRStaticLayers(session) {
|
|
13287
|
+
function buildIRStaticLayers(session, sourceIds) {
|
|
13207
13288
|
var composition = session.composition, maskSourceIds = session.maskSourceIds, options = session.options, warnings = session.warnings;
|
|
13208
13289
|
var registry = createLayerConverterRegistry(session);
|
|
13209
13290
|
var result = [];
|
|
@@ -13228,6 +13309,7 @@ function buildIRStaticLayers(session) {
|
|
|
13228
13309
|
var converted = converter.convert(layer, state, converter.layerType + "_" + String(index).padStart(4, '0'));
|
|
13229
13310
|
if (converted != null) {
|
|
13230
13311
|
result.push(converted);
|
|
13312
|
+
sourceIds == null ? void 0 : sourceIds.set(converted, layer.id);
|
|
13231
13313
|
layerCounts.set(converter.layerType, index);
|
|
13232
13314
|
}
|
|
13233
13315
|
} catch (error) {
|
|
@@ -13240,9 +13322,9 @@ function buildIRStaticLayers(session) {
|
|
|
13240
13322
|
return result;
|
|
13241
13323
|
}
|
|
13242
13324
|
|
|
13243
|
-
function assembleSVGStaticLayers(scene, options, warnings, preservedData) {
|
|
13325
|
+
function assembleSVGStaticLayers(scene, options, warnings, preservedData, sourceIds) {
|
|
13244
13326
|
var session = buildLayerConversionSession(scene, options, warnings, preservedData);
|
|
13245
|
-
var data = buildIRStaticLayers(session);
|
|
13327
|
+
var data = buildIRStaticLayers(session, sourceIds);
|
|
13246
13328
|
var composition = session.composition;
|
|
13247
13329
|
if (composition.backgroundColor != null) {
|
|
13248
13330
|
collectLayerWarning(warnings, options, SVG2IR_WARNING_CODES.LAYERS_UNSUPPORTED_TYPE, 'Composition background color is not an image or text layer.');
|
|
@@ -13255,6 +13337,1034 @@ function assembleSVGStaticLayers(scene, options, warnings, preservedData) {
|
|
|
13255
13337
|
data: data
|
|
13256
13338
|
};
|
|
13257
13339
|
}
|
|
13340
|
+
/** Replace baked sources, combine native and raster layers in painter order, then report final diagnostics. */ function assembleSVGLayers(input) {
|
|
13341
|
+
var _warnings, _ordered;
|
|
13342
|
+
var scene = input.scene, options = input.options, preservedData = input.preservedData, layerSources = input.layerSources, warningSources = input.warningSources, baking = input.baking;
|
|
13343
|
+
var warnings = input.warnings.filter(function(warning) {
|
|
13344
|
+
return !baking.resolvesWarning(warning, warningSources.get(warning));
|
|
13345
|
+
});
|
|
13346
|
+
var composition = getIRSceneEntryComposition(scene);
|
|
13347
|
+
var nativeScene = _extends({}, scene, {
|
|
13348
|
+
compositions: scene.compositions.map(function(candidate) {
|
|
13349
|
+
return candidate === composition ? _extends({}, candidate, {
|
|
13350
|
+
layers: candidate.layers.filter(function(layer) {
|
|
13351
|
+
return !baking.replacesSource(layerSources.get(layer.id));
|
|
13352
|
+
})
|
|
13353
|
+
}) : candidate;
|
|
13354
|
+
})
|
|
13355
|
+
});
|
|
13356
|
+
var sourceIds = new Map();
|
|
13357
|
+
(_warnings = warnings).push.apply(_warnings, [].concat(baking.warnings));
|
|
13358
|
+
var native = assembleSVGStaticLayers(nativeScene, _extends({}, options, {
|
|
13359
|
+
strict: false,
|
|
13360
|
+
onWarning: undefined
|
|
13361
|
+
}), warnings, preservedData, sourceIds);
|
|
13362
|
+
var ordered = native.data.map(function(layer) {
|
|
13363
|
+
var _layerSources_get, _sourceIds_get;
|
|
13364
|
+
return {
|
|
13365
|
+
path: (_layerSources_get = layerSources.get((_sourceIds_get = sourceIds.get(layer)) != null ? _sourceIds_get : '')) != null ? _layerSources_get : '',
|
|
13366
|
+
layer: layer
|
|
13367
|
+
};
|
|
13368
|
+
});
|
|
13369
|
+
(_ordered = ordered).push.apply(_ordered, [].concat(baking.layers));
|
|
13370
|
+
ordered.sort(function(left, right) {
|
|
13371
|
+
return comparePaths(left.path, right.path);
|
|
13372
|
+
});
|
|
13373
|
+
var counts = {
|
|
13374
|
+
image: 0,
|
|
13375
|
+
text: 0
|
|
13376
|
+
};
|
|
13377
|
+
var data = ordered.map(function(param) {
|
|
13378
|
+
var layer = param.layer;
|
|
13379
|
+
return _extends({}, layer, {
|
|
13380
|
+
id: layer.type + "_" + String(++counts[layer.type]).padStart(4, '0')
|
|
13381
|
+
});
|
|
13382
|
+
});
|
|
13383
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(warnings), _step; !(_step = _iterator()).done;){
|
|
13384
|
+
var warning = _step.value;
|
|
13385
|
+
options.onWarning == null ? void 0 : options.onWarning.call(options, warning);
|
|
13386
|
+
if (options.strict === true) {
|
|
13387
|
+
var message = warning.message.replace(/^\[svg2ge:layers\] /, '');
|
|
13388
|
+
collectLayerWarning([], _extends({}, options, {
|
|
13389
|
+
onWarning: undefined
|
|
13390
|
+
}), warning.code, message, warning.elementSelector);
|
|
13391
|
+
}
|
|
13392
|
+
}
|
|
13393
|
+
return {
|
|
13394
|
+
size: native.size,
|
|
13395
|
+
data: data,
|
|
13396
|
+
warnings: warnings
|
|
13397
|
+
};
|
|
13398
|
+
}
|
|
13399
|
+
function comparePaths(left, right) {
|
|
13400
|
+
var a = left.split('/').map(Number);
|
|
13401
|
+
var b = right.split('/').map(Number);
|
|
13402
|
+
for(var index = 0; index < Math.min(a.length, b.length); index++){
|
|
13403
|
+
if (a[index] !== b[index]) {
|
|
13404
|
+
return a[index] - b[index];
|
|
13405
|
+
}
|
|
13406
|
+
}
|
|
13407
|
+
return a.length - b.length;
|
|
13408
|
+
}
|
|
13409
|
+
|
|
13410
|
+
var animatedElements = new Set([
|
|
13411
|
+
'animate',
|
|
13412
|
+
'animatetransform',
|
|
13413
|
+
'animatemotion',
|
|
13414
|
+
'set'
|
|
13415
|
+
]);
|
|
13416
|
+
var declarations$1 = new Set([
|
|
13417
|
+
'defs',
|
|
13418
|
+
'style',
|
|
13419
|
+
'title',
|
|
13420
|
+
'desc',
|
|
13421
|
+
'metadata',
|
|
13422
|
+
'symbol',
|
|
13423
|
+
'clippath',
|
|
13424
|
+
'mask',
|
|
13425
|
+
'filter',
|
|
13426
|
+
'lineargradient',
|
|
13427
|
+
'radialgradient',
|
|
13428
|
+
'pattern',
|
|
13429
|
+
'marker'
|
|
13430
|
+
]);
|
|
13431
|
+
/** Preserve selector context until styles are frozen, then isolate the requested paint subtree. */ function prepareSVGSnapshot(source, path, size, inspect) {
|
|
13432
|
+
var parsed = new DOMParser().parseFromString(source, 'image/svg+xml');
|
|
13433
|
+
if (parsed.querySelector('parsererror') != null) {
|
|
13434
|
+
throw new Error('Invalid SVG document.');
|
|
13435
|
+
}
|
|
13436
|
+
var root = parsed.documentElement;
|
|
13437
|
+
var elements = [].concat([
|
|
13438
|
+
root
|
|
13439
|
+
], Array.from(root.querySelectorAll('*')));
|
|
13440
|
+
var selected = elements.find(function(element) {
|
|
13441
|
+
return buildElementTreePath(element) === path;
|
|
13442
|
+
});
|
|
13443
|
+
if (selected == null) {
|
|
13444
|
+
throw new Error('The graphic source element could not be located.');
|
|
13445
|
+
}
|
|
13446
|
+
if (root.querySelector('script, foreignObject') != null) {
|
|
13447
|
+
throw new Error('Scripts and foreignObject are outside static graphic baking support.');
|
|
13448
|
+
}
|
|
13449
|
+
if (Array.from(root.querySelectorAll('style')).some(function(element) {
|
|
13450
|
+
var _element_textContent;
|
|
13451
|
+
return /@import|@font-face/i.test((_element_textContent = element.textContent) != null ? _element_textContent : '');
|
|
13452
|
+
})) {
|
|
13453
|
+
throw new Error('External stylesheets and fonts require explicit resource preparation.');
|
|
13454
|
+
}
|
|
13455
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(elements), _step; !(_step = _iterator()).done;){
|
|
13456
|
+
var element = _step.value;
|
|
13457
|
+
for(var _iterator1 = _create_for_of_iterator_helper_loose(Array.from(element.attributes)), _step1; !(_step1 = _iterator1()).done;){
|
|
13458
|
+
var attribute = _step1.value;
|
|
13459
|
+
if (/^on/i.test(attribute.name)) {
|
|
13460
|
+
element.removeAttributeNode(attribute);
|
|
13461
|
+
}
|
|
13462
|
+
}
|
|
13463
|
+
}
|
|
13464
|
+
var frame = document.createElement('iframe');
|
|
13465
|
+
frame.setAttribute('sandbox', 'allow-same-origin');
|
|
13466
|
+
frame.style.cssText = "position:fixed;left:-100000px;top:0;width:" + size[0] + "px;height:" + size[1] + "px;border:0;";
|
|
13467
|
+
document.body.appendChild(frame);
|
|
13468
|
+
try {
|
|
13469
|
+
var _loop = function() {
|
|
13470
|
+
var element = _step3.value;
|
|
13471
|
+
var _element_getAttribute;
|
|
13472
|
+
var computed = frameWindow.getComputedStyle(element);
|
|
13473
|
+
var inside = target.contains(element);
|
|
13474
|
+
var ancestor = element !== target && element.contains(target);
|
|
13475
|
+
if ((inside || ancestor) && (computed.animationName !== 'none' || hasAnimationChildren(element))) {
|
|
13476
|
+
throw new Error('Animated graphics require an explicit sampling policy.');
|
|
13477
|
+
}
|
|
13478
|
+
// Freezing inherited defaults on a definition changes <use> instance inheritance.
|
|
13479
|
+
// Freeze only authored declarations, after matching selectors in the intact source tree.
|
|
13480
|
+
var authored = frameDocument.createElement('div').style;
|
|
13481
|
+
authored.cssText = (_element_getAttribute = element.getAttribute('style')) != null ? _element_getAttribute : '';
|
|
13482
|
+
var names = new Set(Array.from(authored));
|
|
13483
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(styleRules), _step; !(_step = _iterator()).done;){
|
|
13484
|
+
var rule = _step.value;
|
|
13485
|
+
if (element.matches(rule.selectorText)) {
|
|
13486
|
+
for(var _iterator1 = _create_for_of_iterator_helper_loose(Array.from(rule.style)), _step1; !(_step1 = _iterator1()).done;){
|
|
13487
|
+
var name = _step1.value;
|
|
13488
|
+
names.add(name);
|
|
13489
|
+
}
|
|
13490
|
+
}
|
|
13491
|
+
}
|
|
13492
|
+
var properties = Array.from(names, function(name) {
|
|
13493
|
+
var value = computed.getPropertyValue(name);
|
|
13494
|
+
value = value.replace(/url\(["']?([^)'"\s]+)["']?\)/g, function(_match, url) {
|
|
13495
|
+
return 'url("' + normalizeReference(url) + '")';
|
|
13496
|
+
});
|
|
13497
|
+
return name + ":" + value + " !important;";
|
|
13498
|
+
});
|
|
13499
|
+
var isTemplate = templateRoots.some(function(template) {
|
|
13500
|
+
return template.contains(element);
|
|
13501
|
+
});
|
|
13502
|
+
frozenStyles.push(isTemplate ? null : properties.join(''));
|
|
13503
|
+
};
|
|
13504
|
+
var frameDocument = frame.contentDocument;
|
|
13505
|
+
var frameWindow = frame.contentWindow;
|
|
13506
|
+
if (frameDocument == null || frameWindow == null) {
|
|
13507
|
+
throw new Error('Unable to create isolated SVG context.');
|
|
13508
|
+
}
|
|
13509
|
+
frameDocument.body.style.cssText = 'margin:0;';
|
|
13510
|
+
var mounted = frameDocument.importNode(root, true);
|
|
13511
|
+
mounted.setAttribute('width', String(size[0]));
|
|
13512
|
+
mounted.setAttribute('height', String(size[1]));
|
|
13513
|
+
frameDocument.body.appendChild(mounted);
|
|
13514
|
+
var mountedElements = [].concat([
|
|
13515
|
+
mounted
|
|
13516
|
+
], Array.from(mounted.querySelectorAll('*')));
|
|
13517
|
+
var selectedIndex = elements.indexOf(selected);
|
|
13518
|
+
var target = mountedElements[selectedIndex];
|
|
13519
|
+
var normalizeReference = function normalizeReference(url) {
|
|
13520
|
+
if (url.startsWith('#')) {
|
|
13521
|
+
return url;
|
|
13522
|
+
}
|
|
13523
|
+
var hash = url.indexOf('#');
|
|
13524
|
+
if (hash >= 0 && url.slice(0, hash) === frameDocument.baseURI.split('#')[0]) {
|
|
13525
|
+
return url.slice(hash);
|
|
13526
|
+
}
|
|
13527
|
+
throw new Error('External paint resources require explicit resource preparation.');
|
|
13528
|
+
};
|
|
13529
|
+
var frozenStyles = [];
|
|
13530
|
+
var templateRoots = Array.from(mounted.querySelectorAll('use')).flatMap(function(element) {
|
|
13531
|
+
var _ref, _element_getAttribute;
|
|
13532
|
+
var href = (_ref = (_element_getAttribute = element.getAttribute('href')) != null ? _element_getAttribute : element.getAttribute('xlink:href')) != null ? _ref : '';
|
|
13533
|
+
var template = href.startsWith('#') ? mountedElements.find(function(candidate) {
|
|
13534
|
+
return candidate.id === href.slice(1);
|
|
13535
|
+
}) : null;
|
|
13536
|
+
return template == null ? [] : [
|
|
13537
|
+
template
|
|
13538
|
+
];
|
|
13539
|
+
});
|
|
13540
|
+
var styleRules = [];
|
|
13541
|
+
var collectRules = function collectRules1(rules) {
|
|
13542
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(rules)), _step; !(_step = _iterator()).done;){
|
|
13543
|
+
var rule = _step.value;
|
|
13544
|
+
if (isStyleRule$2(rule)) {
|
|
13545
|
+
styleRules.push(rule);
|
|
13546
|
+
} else if (hasNestedRules$1(rule)) {
|
|
13547
|
+
if (isMediaRule(rule) && !frameWindow.matchMedia(rule.conditionText).matches) {
|
|
13548
|
+
continue;
|
|
13549
|
+
}
|
|
13550
|
+
collectRules(rule.cssRules);
|
|
13551
|
+
}
|
|
13552
|
+
}
|
|
13553
|
+
};
|
|
13554
|
+
for(var _iterator2 = _create_for_of_iterator_helper_loose(Array.from(frameDocument.styleSheets)), _step2; !(_step2 = _iterator2()).done;){
|
|
13555
|
+
var sheet = _step2.value;
|
|
13556
|
+
collectRules(sheet.cssRules);
|
|
13557
|
+
}
|
|
13558
|
+
for(var _iterator3 = _create_for_of_iterator_helper_loose(mountedElements), _step3; !(_step3 = _iterator3()).done;)_loop();
|
|
13559
|
+
for(var index = 0; index < mountedElements.length; index++){
|
|
13560
|
+
var style = frozenStyles[index];
|
|
13561
|
+
if (style != null) {
|
|
13562
|
+
mountedElements[index].setAttribute('style', style);
|
|
13563
|
+
}
|
|
13564
|
+
}
|
|
13565
|
+
var byId = new Map(mountedElements.filter(function(element) {
|
|
13566
|
+
return element.id !== '';
|
|
13567
|
+
}).map(function(element) {
|
|
13568
|
+
return [
|
|
13569
|
+
element.id,
|
|
13570
|
+
element
|
|
13571
|
+
];
|
|
13572
|
+
}));
|
|
13573
|
+
var seen = new Set();
|
|
13574
|
+
var checkDependencies = function checkDependencies1(element) {
|
|
13575
|
+
if (seen.has(element)) {
|
|
13576
|
+
return;
|
|
13577
|
+
}
|
|
13578
|
+
seen.add(element);
|
|
13579
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(element.attributes)), _step; !(_step = _iterator()).done;){
|
|
13580
|
+
var attribute = _step.value;
|
|
13581
|
+
if (attribute.localName === 'href' && element.tagName.toLowerCase() === 'a') {
|
|
13582
|
+
continue;
|
|
13583
|
+
}
|
|
13584
|
+
if (attribute.localName === 'href' && !attribute.value.startsWith('#') && !([
|
|
13585
|
+
'image',
|
|
13586
|
+
'feimage'
|
|
13587
|
+
].includes(element.tagName.toLowerCase()) && /^data:image\//i.test(attribute.value))) {
|
|
13588
|
+
throw new Error('External SVG references require explicit resource preparation.');
|
|
13589
|
+
}
|
|
13590
|
+
var normalized = attribute.value.replace(/url\(\s*["']?([^)'"\s]+)["']?\s*\)/g, function(_match, url) {
|
|
13591
|
+
return 'url("' + normalizeReference(url) + '")';
|
|
13592
|
+
});
|
|
13593
|
+
var ids = Array.from(normalized.matchAll(/url\(["']?#([^)'"\s]+)["']?\)/g), function(match) {
|
|
13594
|
+
return match[1];
|
|
13595
|
+
});
|
|
13596
|
+
if (normalized !== attribute.value) {
|
|
13597
|
+
element.setAttribute(attribute.name, normalized);
|
|
13598
|
+
}
|
|
13599
|
+
if (attribute.localName === 'href' && attribute.value.startsWith('#')) {
|
|
13600
|
+
ids.push(attribute.value.slice(1));
|
|
13601
|
+
}
|
|
13602
|
+
for(var _iterator1 = _create_for_of_iterator_helper_loose(ids), _step1; !(_step1 = _iterator1()).done;){
|
|
13603
|
+
var id = _step1.value;
|
|
13604
|
+
var dependency = byId.get(id);
|
|
13605
|
+
if (dependency == null) {
|
|
13606
|
+
throw new Error("Missing SVG reference: #" + id + ".");
|
|
13607
|
+
}
|
|
13608
|
+
checkDependencies(dependency);
|
|
13609
|
+
}
|
|
13610
|
+
}
|
|
13611
|
+
for(var _iterator2 = _create_for_of_iterator_helper_loose(Array.from(element.children)), _step2; !(_step2 = _iterator2()).done;){
|
|
13612
|
+
var child = _step2.value;
|
|
13613
|
+
if (!declarations$1.has(child.tagName.toLowerCase())) {
|
|
13614
|
+
checkDependencies(child);
|
|
13615
|
+
}
|
|
13616
|
+
}
|
|
13617
|
+
};
|
|
13618
|
+
checkDependencies(target);
|
|
13619
|
+
for(var _iterator4 = _create_for_of_iterator_helper_loose(seen), _step4; !(_step4 = _iterator4()).done;){
|
|
13620
|
+
var dependency = _step4.value;
|
|
13621
|
+
if (frameWindow.getComputedStyle(dependency).animationName !== 'none' || hasAnimationChildren(dependency)) {
|
|
13622
|
+
throw new Error('Animated graphics require an explicit sampling policy.');
|
|
13623
|
+
}
|
|
13624
|
+
}
|
|
13625
|
+
inspect == null ? void 0 : inspect({
|
|
13626
|
+
root: mounted,
|
|
13627
|
+
target: target,
|
|
13628
|
+
elements: mountedElements,
|
|
13629
|
+
dependencies: seen,
|
|
13630
|
+
readStyle: function readStyle(element) {
|
|
13631
|
+
return frameWindow.getComputedStyle(element);
|
|
13632
|
+
}
|
|
13633
|
+
});
|
|
13634
|
+
var isolate = function isolate1(element) {
|
|
13635
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(element.children)), _step; !(_step = _iterator()).done;){
|
|
13636
|
+
var child = _step.value;
|
|
13637
|
+
var tag = child.tagName.toLowerCase();
|
|
13638
|
+
if (declarations$1.has(tag) || child === target || target.contains(child)) {
|
|
13639
|
+
continue;
|
|
13640
|
+
}
|
|
13641
|
+
if (child.contains(target)) {
|
|
13642
|
+
isolate(child);
|
|
13643
|
+
} else {
|
|
13644
|
+
var definitions = frameDocument.createElementNS('http://www.w3.org/2000/svg', 'defs');
|
|
13645
|
+
child.replaceWith(definitions);
|
|
13646
|
+
definitions.appendChild(child);
|
|
13647
|
+
}
|
|
13648
|
+
}
|
|
13649
|
+
};
|
|
13650
|
+
if (target !== mounted) {
|
|
13651
|
+
isolate(mounted);
|
|
13652
|
+
}
|
|
13653
|
+
return new XMLSerializer().serializeToString(mounted);
|
|
13654
|
+
} finally{
|
|
13655
|
+
frame.remove();
|
|
13656
|
+
}
|
|
13657
|
+
}
|
|
13658
|
+
/** Only animations attached to this element reject its own bake scope, not unrelated subtrees. */ function hasAnimationChildren(element) {
|
|
13659
|
+
return Array.from(element.children).some(function(child) {
|
|
13660
|
+
return animatedElements.has(child.tagName.toLowerCase());
|
|
13661
|
+
});
|
|
13662
|
+
}
|
|
13663
|
+
function isStyleRule$2(rule) {
|
|
13664
|
+
return rule.type === 1;
|
|
13665
|
+
}
|
|
13666
|
+
function hasNestedRules$1(rule) {
|
|
13667
|
+
return 'cssRules' in rule;
|
|
13668
|
+
}
|
|
13669
|
+
function isMediaRule(rule) {
|
|
13670
|
+
return rule.type === 4;
|
|
13671
|
+
}
|
|
13672
|
+
|
|
13673
|
+
/** Render an SVG subtree as a cropped PNG, or return null if it has no visible pixels. */ function rasterizeSVG(source, path, size, scale, inspect) {
|
|
13674
|
+
if (scale === void 0) scale = 2;
|
|
13675
|
+
return _async_to_generator(function() {
|
|
13676
|
+
var snapshot, canvas;
|
|
13677
|
+
return _ts_generator(this, function(_state) {
|
|
13678
|
+
switch(_state.label){
|
|
13679
|
+
case 0:
|
|
13680
|
+
if (!Number.isFinite(scale) || scale <= 0) {
|
|
13681
|
+
throw new RangeError('rasterScale must be finite and positive.');
|
|
13682
|
+
}
|
|
13683
|
+
if (Math.ceil(size[0] * scale) * Math.ceil(size[1] * scale) > 16777216) {
|
|
13684
|
+
throw new RangeError('SVG rasterization exceeds the 16 megapixel working limit; reduce rasterScale.');
|
|
13685
|
+
}
|
|
13686
|
+
snapshot = prepareSVGSnapshot(source, path, size, inspect);
|
|
13687
|
+
return [
|
|
13688
|
+
4,
|
|
13689
|
+
renderSVG(snapshot, size, scale)
|
|
13690
|
+
];
|
|
13691
|
+
case 1:
|
|
13692
|
+
canvas = _state.sent();
|
|
13693
|
+
return [
|
|
13694
|
+
2,
|
|
13695
|
+
cropRasterImage(canvas, scale)
|
|
13696
|
+
];
|
|
13697
|
+
}
|
|
13698
|
+
});
|
|
13699
|
+
})();
|
|
13700
|
+
}
|
|
13701
|
+
/** No DOM access at module initialization: the Node entry also imports this module. */ function canRasterizeSVG() {
|
|
13702
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined' && typeof window.HTMLCanvasElement === 'function' && typeof window.Image === 'function' && typeof window.getComputedStyle === 'function';
|
|
13703
|
+
}
|
|
13704
|
+
function renderSVG(svg, size, scale) {
|
|
13705
|
+
return _async_to_generator(function() {
|
|
13706
|
+
var canvas, context, image, url;
|
|
13707
|
+
return _ts_generator(this, function(_state) {
|
|
13708
|
+
switch(_state.label){
|
|
13709
|
+
case 0:
|
|
13710
|
+
canvas = document.createElement('canvas');
|
|
13711
|
+
canvas.width = Math.ceil(size[0] * scale);
|
|
13712
|
+
canvas.height = Math.ceil(size[1] * scale);
|
|
13713
|
+
context = canvas.getContext('2d');
|
|
13714
|
+
if (context == null) {
|
|
13715
|
+
throw new Error('A 2D canvas context is required for SVG rasterization.');
|
|
13716
|
+
}
|
|
13717
|
+
image = new Image();
|
|
13718
|
+
url = URL.createObjectURL(new Blob([
|
|
13719
|
+
svg
|
|
13720
|
+
], {
|
|
13721
|
+
type: 'image/svg+xml'
|
|
13722
|
+
}));
|
|
13723
|
+
_state.label = 1;
|
|
13724
|
+
case 1:
|
|
13725
|
+
_state.trys.push([
|
|
13726
|
+
1,
|
|
13727
|
+
,
|
|
13728
|
+
3,
|
|
13729
|
+
4
|
|
13730
|
+
]);
|
|
13731
|
+
return [
|
|
13732
|
+
4,
|
|
13733
|
+
new Promise(function(resolve, reject) {
|
|
13734
|
+
image.onload = function() {
|
|
13735
|
+
return resolve();
|
|
13736
|
+
};
|
|
13737
|
+
image.onerror = function() {
|
|
13738
|
+
return reject(new Error('The prepared SVG could not be decoded.'));
|
|
13739
|
+
};
|
|
13740
|
+
image.src = url;
|
|
13741
|
+
})
|
|
13742
|
+
];
|
|
13743
|
+
case 2:
|
|
13744
|
+
_state.sent();
|
|
13745
|
+
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
13746
|
+
return [
|
|
13747
|
+
2,
|
|
13748
|
+
canvas
|
|
13749
|
+
];
|
|
13750
|
+
case 3:
|
|
13751
|
+
URL.revokeObjectURL(url);
|
|
13752
|
+
return [
|
|
13753
|
+
7
|
|
13754
|
+
];
|
|
13755
|
+
case 4:
|
|
13756
|
+
return [
|
|
13757
|
+
2
|
|
13758
|
+
];
|
|
13759
|
+
}
|
|
13760
|
+
});
|
|
13761
|
+
})();
|
|
13762
|
+
}
|
|
13763
|
+
/** Trim transparent pixels after rendering, retaining strokes, blur and filter extents. */ function cropRasterImage(canvas, scale) {
|
|
13764
|
+
var context = canvas.getContext('2d');
|
|
13765
|
+
if (context == null) {
|
|
13766
|
+
throw new Error('A 2D canvas context is required for SVG rasterization.');
|
|
13767
|
+
}
|
|
13768
|
+
var data = context.getImageData(0, 0, canvas.width, canvas.height).data;
|
|
13769
|
+
var left = canvas.width;
|
|
13770
|
+
var top = canvas.height;
|
|
13771
|
+
var right = 0;
|
|
13772
|
+
var bottom = 0;
|
|
13773
|
+
for(var y = 0; y < canvas.height; y++){
|
|
13774
|
+
for(var x = 0; x < canvas.width; x++){
|
|
13775
|
+
if (data[(y * canvas.width + x) * 4 + 3] === 0) {
|
|
13776
|
+
continue;
|
|
13777
|
+
}
|
|
13778
|
+
left = Math.min(left, x);
|
|
13779
|
+
top = Math.min(top, y);
|
|
13780
|
+
right = Math.max(right, x + 1);
|
|
13781
|
+
bottom = Math.max(bottom, y + 1);
|
|
13782
|
+
}
|
|
13783
|
+
}
|
|
13784
|
+
if (right <= left || bottom <= top) {
|
|
13785
|
+
return null;
|
|
13786
|
+
}
|
|
13787
|
+
var cropped = document.createElement('canvas');
|
|
13788
|
+
cropped.width = right - left;
|
|
13789
|
+
cropped.height = bottom - top;
|
|
13790
|
+
var croppedContext = cropped.getContext('2d');
|
|
13791
|
+
if (croppedContext == null) {
|
|
13792
|
+
throw new Error('A 2D canvas context is required for SVG rasterization.');
|
|
13793
|
+
}
|
|
13794
|
+
croppedContext.drawImage(canvas, left, top, cropped.width, cropped.height, 0, 0, cropped.width, cropped.height);
|
|
13795
|
+
return {
|
|
13796
|
+
url: cropped.toDataURL('image/png'),
|
|
13797
|
+
bbox: [
|
|
13798
|
+
left / scale,
|
|
13799
|
+
top / scale,
|
|
13800
|
+
right / scale,
|
|
13801
|
+
bottom / scale
|
|
13802
|
+
]
|
|
13803
|
+
};
|
|
13804
|
+
}
|
|
13805
|
+
|
|
13806
|
+
var normalValues = new Set([
|
|
13807
|
+
'normal',
|
|
13808
|
+
'initial',
|
|
13809
|
+
'inherit',
|
|
13810
|
+
'unset',
|
|
13811
|
+
'revert',
|
|
13812
|
+
'revert-layer'
|
|
13813
|
+
]);
|
|
13814
|
+
/** Replace non-normal mix-blend-mode declarations with normal and report each downgrade. */ function normalizeLayerBlending(source) {
|
|
13815
|
+
var _loop = function() {
|
|
13816
|
+
var element = _step.value;
|
|
13817
|
+
var _element_getAttribute;
|
|
13818
|
+
var _readPresentationAttribute;
|
|
13819
|
+
var tag = element.tagName.toLowerCase();
|
|
13820
|
+
var id = (_element_getAttribute = element.getAttribute('id')) != null ? _element_getAttribute : '';
|
|
13821
|
+
var selector = id === '' ? tag : tag + "#" + id;
|
|
13822
|
+
var mode = (_readPresentationAttribute = readPresentationAttribute(element, 'mix-blend-mode')) == null ? void 0 : _readPresentationAttribute.replace(/\/\*[\s\S]*?\*\//g, '').trim();
|
|
13823
|
+
if (mode != null && !normalValues.has(mode.toLowerCase())) {
|
|
13824
|
+
downgrades.push({
|
|
13825
|
+
mode: mode,
|
|
13826
|
+
selector: selector
|
|
13827
|
+
});
|
|
13828
|
+
}
|
|
13829
|
+
var style = element.getAttribute('style');
|
|
13830
|
+
if (style != null) {
|
|
13831
|
+
var normalized = normalizeBlendDeclarations(style);
|
|
13832
|
+
if (normalized !== style) {
|
|
13833
|
+
element.setAttribute('style', normalized);
|
|
13834
|
+
changed = true;
|
|
13835
|
+
}
|
|
13836
|
+
}
|
|
13837
|
+
var attribute = element.getAttribute('mix-blend-mode');
|
|
13838
|
+
if (attribute != null && !normalValues.has(attribute.trim().toLowerCase())) {
|
|
13839
|
+
element.setAttribute('mix-blend-mode', 'normal');
|
|
13840
|
+
changed = true;
|
|
13841
|
+
}
|
|
13842
|
+
if (tag === 'style') {
|
|
13843
|
+
var _element_textContent;
|
|
13844
|
+
var text = (_element_textContent = element.textContent) != null ? _element_textContent : '';
|
|
13845
|
+
var normalized1 = normalizeBlendDeclarations(text, function(value) {
|
|
13846
|
+
return downgrades.push({
|
|
13847
|
+
mode: value,
|
|
13848
|
+
selector: selector
|
|
13849
|
+
});
|
|
13850
|
+
});
|
|
13851
|
+
if (normalized1 !== text) {
|
|
13852
|
+
element.textContent = normalized1;
|
|
13853
|
+
changed = true;
|
|
13854
|
+
}
|
|
13855
|
+
}
|
|
13856
|
+
};
|
|
13857
|
+
var svg = SVG(source);
|
|
13858
|
+
var downgrades = [];
|
|
13859
|
+
var changed = false;
|
|
13860
|
+
for(var _iterator = _create_for_of_iterator_helper_loose([].concat([
|
|
13861
|
+
svg.node
|
|
13862
|
+
], Array.from(svg.node.querySelectorAll('*')))), _step; !(_step = _iterator()).done;)_loop();
|
|
13863
|
+
return {
|
|
13864
|
+
source: changed ? svg.svg() : source,
|
|
13865
|
+
downgrades: downgrades
|
|
13866
|
+
};
|
|
13867
|
+
}
|
|
13868
|
+
/** Match declaration boundaries without changing strings, comments or function arguments. */ function normalizeBlendDeclarations(source, onDowngrade) {
|
|
13869
|
+
var replacements = [];
|
|
13870
|
+
var start = 0;
|
|
13871
|
+
var colon = -1;
|
|
13872
|
+
var parentheses = 0;
|
|
13873
|
+
var quote;
|
|
13874
|
+
var comment = false;
|
|
13875
|
+
var finish = function finish(end) {
|
|
13876
|
+
if (colon < start) {
|
|
13877
|
+
return;
|
|
13878
|
+
}
|
|
13879
|
+
var name = source.slice(start, colon).replace(/\/\*[\s\S]*?\*\//g, '').trim().toLowerCase();
|
|
13880
|
+
if (name !== 'mix-blend-mode') {
|
|
13881
|
+
return;
|
|
13882
|
+
}
|
|
13883
|
+
var value = source.slice(colon + 1, end).replace(/\/\*[\s\S]*?\*\//g, '').trim();
|
|
13884
|
+
var mode = value.replace(/\s*!important\s*$/i, '').trim();
|
|
13885
|
+
if (mode === '' || normalValues.has(mode.toLowerCase())) {
|
|
13886
|
+
return;
|
|
13887
|
+
}
|
|
13888
|
+
onDowngrade == null ? void 0 : onDowngrade(mode);
|
|
13889
|
+
replacements.push({
|
|
13890
|
+
start: colon + 1,
|
|
13891
|
+
end: end,
|
|
13892
|
+
value: /!important\s*$/i.test(value) ? ' normal !important' : ' normal'
|
|
13893
|
+
});
|
|
13894
|
+
};
|
|
13895
|
+
for(var index = 0; index < source.length; index++){
|
|
13896
|
+
var character = source[index];
|
|
13897
|
+
var next = source[index + 1];
|
|
13898
|
+
if (comment) {
|
|
13899
|
+
if (character === '*' && next === '/') {
|
|
13900
|
+
comment = false;
|
|
13901
|
+
index++;
|
|
13902
|
+
}
|
|
13903
|
+
continue;
|
|
13904
|
+
}
|
|
13905
|
+
if (quote != null) {
|
|
13906
|
+
if (character === '\\') {
|
|
13907
|
+
index++;
|
|
13908
|
+
} else if (character === quote) {
|
|
13909
|
+
quote = undefined;
|
|
13910
|
+
}
|
|
13911
|
+
continue;
|
|
13912
|
+
}
|
|
13913
|
+
if (character === '/' && next === '*') {
|
|
13914
|
+
comment = true;
|
|
13915
|
+
index++;
|
|
13916
|
+
continue;
|
|
13917
|
+
}
|
|
13918
|
+
if (character === '"' || character === "'") {
|
|
13919
|
+
quote = character;
|
|
13920
|
+
continue;
|
|
13921
|
+
}
|
|
13922
|
+
if (character === '(') {
|
|
13923
|
+
parentheses++;
|
|
13924
|
+
continue;
|
|
13925
|
+
}
|
|
13926
|
+
if (character === ')') {
|
|
13927
|
+
parentheses = Math.max(0, parentheses - 1);
|
|
13928
|
+
continue;
|
|
13929
|
+
}
|
|
13930
|
+
if (parentheses > 0) {
|
|
13931
|
+
continue;
|
|
13932
|
+
}
|
|
13933
|
+
if (character === ':' && colon < start) {
|
|
13934
|
+
colon = index;
|
|
13935
|
+
}
|
|
13936
|
+
if (character === ';' || character === '}' || character === '{') {
|
|
13937
|
+
if (character !== '{') {
|
|
13938
|
+
finish(index);
|
|
13939
|
+
}
|
|
13940
|
+
start = index + 1;
|
|
13941
|
+
colon = -1;
|
|
13942
|
+
}
|
|
13943
|
+
}
|
|
13944
|
+
finish(source.length);
|
|
13945
|
+
var result = '';
|
|
13946
|
+
var cursor = 0;
|
|
13947
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(replacements), _step; !(_step = _iterator()).done;){
|
|
13948
|
+
var replacement = _step.value;
|
|
13949
|
+
result += source.slice(cursor, replacement.start) + replacement.value;
|
|
13950
|
+
cursor = replacement.end;
|
|
13951
|
+
}
|
|
13952
|
+
return result + source.slice(cursor);
|
|
13953
|
+
}
|
|
13954
|
+
|
|
13955
|
+
var declarations = new Set([
|
|
13956
|
+
'defs',
|
|
13957
|
+
'style',
|
|
13958
|
+
'title',
|
|
13959
|
+
'desc',
|
|
13960
|
+
'metadata',
|
|
13961
|
+
'symbol',
|
|
13962
|
+
'clippath',
|
|
13963
|
+
'mask',
|
|
13964
|
+
'filter',
|
|
13965
|
+
'lineargradient',
|
|
13966
|
+
'radialgradient',
|
|
13967
|
+
'pattern',
|
|
13968
|
+
'marker'
|
|
13969
|
+
]);
|
|
13970
|
+
var graphics = new Set([
|
|
13971
|
+
'path',
|
|
13972
|
+
'rect',
|
|
13973
|
+
'circle',
|
|
13974
|
+
'ellipse',
|
|
13975
|
+
'line',
|
|
13976
|
+
'polyline',
|
|
13977
|
+
'polygon',
|
|
13978
|
+
'use'
|
|
13979
|
+
]);
|
|
13980
|
+
var containers = new Set([
|
|
13981
|
+
'svg',
|
|
13982
|
+
'g',
|
|
13983
|
+
'a',
|
|
13984
|
+
'switch'
|
|
13985
|
+
]);
|
|
13986
|
+
var groupEffects = [
|
|
13987
|
+
'opacity',
|
|
13988
|
+
'filter',
|
|
13989
|
+
'mask',
|
|
13990
|
+
'clip-path'
|
|
13991
|
+
];
|
|
13992
|
+
var native = new Set([
|
|
13993
|
+
'text',
|
|
13994
|
+
'image',
|
|
13995
|
+
'foreignobject'
|
|
13996
|
+
]);
|
|
13997
|
+
/** Select graphic-only subtrees; keep native text and images as separate layers. */ function planGraphicBaking(source) {
|
|
13998
|
+
var root = SVG(source).node;
|
|
13999
|
+
var scopes = [];
|
|
14000
|
+
var warnings = [];
|
|
14001
|
+
var effectRules = collectGroupEffectRules(root);
|
|
14002
|
+
var byId = new Map(Array.from(root.querySelectorAll('[id]')).map(function(element) {
|
|
14003
|
+
var _element_getAttribute;
|
|
14004
|
+
return [
|
|
14005
|
+
(_element_getAttribute = element.getAttribute('id')) != null ? _element_getAttribute : '',
|
|
14006
|
+
element
|
|
14007
|
+
];
|
|
14008
|
+
}));
|
|
14009
|
+
var tagOf = function tagOf(element) {
|
|
14010
|
+
return element.tagName.toLowerCase();
|
|
14011
|
+
};
|
|
14012
|
+
var selectorOf = function selectorOf(element) {
|
|
14013
|
+
var _element_getAttribute;
|
|
14014
|
+
var id = (_element_getAttribute = element.getAttribute('id')) != null ? _element_getAttribute : '';
|
|
14015
|
+
return "" + tagOf(element) + (id === '' ? '' : "#" + id);
|
|
14016
|
+
};
|
|
14017
|
+
var references = function references(element) {
|
|
14018
|
+
var ids = Array.from(element.attributes).flatMap(function(attribute) {
|
|
14019
|
+
var values = Array.from(attribute.value.matchAll(/url\(\s*['"]?#([^)'"\s]+)['"]?\s*\)/g), function(match) {
|
|
14020
|
+
return match[1];
|
|
14021
|
+
});
|
|
14022
|
+
if (attribute.localName === 'href' && attribute.value.startsWith('#')) {
|
|
14023
|
+
values.push(attribute.value.slice(1));
|
|
14024
|
+
}
|
|
14025
|
+
return values;
|
|
14026
|
+
});
|
|
14027
|
+
return ids.flatMap(function(id) {
|
|
14028
|
+
var target = byId.get(id);
|
|
14029
|
+
return target == null ? [] : [
|
|
14030
|
+
target
|
|
14031
|
+
];
|
|
14032
|
+
});
|
|
14033
|
+
};
|
|
14034
|
+
var containsNative = function containsNative1(element, seen) {
|
|
14035
|
+
if (seen === void 0) seen = new Set();
|
|
14036
|
+
if (seen.has(element)) {
|
|
14037
|
+
return false;
|
|
14038
|
+
}
|
|
14039
|
+
seen.add(element);
|
|
14040
|
+
if (native.has(tagOf(element))) {
|
|
14041
|
+
return true;
|
|
14042
|
+
}
|
|
14043
|
+
return Array.from(element.children).some(function(child) {
|
|
14044
|
+
return !declarations.has(tagOf(child)) && containsNative(child, seen);
|
|
14045
|
+
}) || references(element).some(function(target) {
|
|
14046
|
+
return containsNative(target, seen);
|
|
14047
|
+
});
|
|
14048
|
+
};
|
|
14049
|
+
var hasGraphics = function hasGraphics1(element) {
|
|
14050
|
+
return graphics.has(tagOf(element)) || Array.from(element.children).some(function(child) {
|
|
14051
|
+
return !declarations.has(tagOf(child)) && hasGraphics(child);
|
|
14052
|
+
});
|
|
14053
|
+
};
|
|
14054
|
+
var hasGroupEffect = function hasGroupEffect(element) {
|
|
14055
|
+
var _element_getAttribute;
|
|
14056
|
+
var style = (_element_getAttribute = element.getAttribute('style')) != null ? _element_getAttribute : '';
|
|
14057
|
+
return effectRules.some(function(rule) {
|
|
14058
|
+
return element.matches(rule.selectorText);
|
|
14059
|
+
}) || hasGraphicGroupEffect(function(name) {
|
|
14060
|
+
var _element_getAttribute;
|
|
14061
|
+
var _exec_, _exec;
|
|
14062
|
+
return (_element_getAttribute = element.getAttribute(name)) != null ? _element_getAttribute : (_exec = new RegExp("(?:^|;)\\s*" + name + "\\s*:\\s*([^;]+)").exec(style)) == null ? void 0 : (_exec_ = _exec[1]) == null ? void 0 : _exec_.trim();
|
|
14063
|
+
});
|
|
14064
|
+
};
|
|
14065
|
+
var visit = function visit1(element) {
|
|
14066
|
+
var _element_getAttribute, _ref;
|
|
14067
|
+
var _exec_, _exec;
|
|
14068
|
+
var tag = tagOf(element);
|
|
14069
|
+
var style = (_element_getAttribute = element.getAttribute('style')) != null ? _element_getAttribute : '';
|
|
14070
|
+
var display = (_ref = (_exec = /(?:^|;)\s*display\s*:\s*([^;]+)/.exec(style)) == null ? void 0 : (_exec_ = _exec[1]) == null ? void 0 : _exec_.trim()) != null ? _ref : element.getAttribute('display');
|
|
14071
|
+
if (display === 'none') {
|
|
14072
|
+
return;
|
|
14073
|
+
}
|
|
14074
|
+
if (declarations.has(tag) || native.has(tag)) {
|
|
14075
|
+
return;
|
|
14076
|
+
}
|
|
14077
|
+
if (!containers.has(tag) && !graphics.has(tag)) {
|
|
14078
|
+
return;
|
|
14079
|
+
}
|
|
14080
|
+
if (!hasGraphics(element)) {
|
|
14081
|
+
return;
|
|
14082
|
+
}
|
|
14083
|
+
if ((graphics.has(tag) || tag === 'switch') && !containsNative(element)) {
|
|
14084
|
+
scopes.push({
|
|
14085
|
+
path: buildElementTreePath(element),
|
|
14086
|
+
selector: selectorOf(element),
|
|
14087
|
+
isGroup: containers.has(tag)
|
|
14088
|
+
});
|
|
14089
|
+
return;
|
|
14090
|
+
}
|
|
14091
|
+
if (hasGroupEffect(element) && !containsNative(element)) {
|
|
14092
|
+
scopes.push({
|
|
14093
|
+
path: buildElementTreePath(element),
|
|
14094
|
+
selector: selectorOf(element),
|
|
14095
|
+
isGroup: containers.has(tag)
|
|
14096
|
+
});
|
|
14097
|
+
return;
|
|
14098
|
+
}
|
|
14099
|
+
if (hasGroupEffect(element)) {
|
|
14100
|
+
warnings.push({
|
|
14101
|
+
message: 'A compositing group contains native text or images; its graphics cannot be baked separately.',
|
|
14102
|
+
selector: selectorOf(element)
|
|
14103
|
+
});
|
|
14104
|
+
return;
|
|
14105
|
+
}
|
|
14106
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(element.children)), _step; !(_step = _iterator()).done;){
|
|
14107
|
+
var child = _step.value;
|
|
14108
|
+
visit(child);
|
|
14109
|
+
}
|
|
14110
|
+
};
|
|
14111
|
+
visit(root);
|
|
14112
|
+
return {
|
|
14113
|
+
scopes: scopes,
|
|
14114
|
+
warnings: warnings
|
|
14115
|
+
};
|
|
14116
|
+
}
|
|
14117
|
+
function collectGroupEffectRules(root) {
|
|
14118
|
+
var result = [];
|
|
14119
|
+
if (typeof CSSStyleSheet !== 'function') {
|
|
14120
|
+
return result;
|
|
14121
|
+
}
|
|
14122
|
+
var visit = function visit1(rules) {
|
|
14123
|
+
var _loop = function() {
|
|
14124
|
+
var rule = _step.value;
|
|
14125
|
+
if (isStyleRule$1(rule)) {
|
|
14126
|
+
var affectsGroup = hasGraphicGroupEffect(function(name) {
|
|
14127
|
+
var value = rule.style.getPropertyValue(name).trim();
|
|
14128
|
+
return value === '' || value === 'initial' || value === 'unset' ? undefined : value;
|
|
14129
|
+
});
|
|
14130
|
+
if (affectsGroup) {
|
|
14131
|
+
result.push(rule);
|
|
14132
|
+
}
|
|
14133
|
+
} else if (hasNestedRules(rule)) {
|
|
14134
|
+
visit(rule.cssRules);
|
|
14135
|
+
}
|
|
14136
|
+
};
|
|
14137
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(rules)), _step; !(_step = _iterator()).done;)_loop();
|
|
14138
|
+
};
|
|
14139
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(root.querySelectorAll('style'))), _step; !(_step = _iterator()).done;){
|
|
14140
|
+
var style = _step.value;
|
|
14141
|
+
var _style_textContent;
|
|
14142
|
+
var sheet = new CSSStyleSheet();
|
|
14143
|
+
sheet.replaceSync((_style_textContent = style.textContent) != null ? _style_textContent : '');
|
|
14144
|
+
visit(sheet.cssRules);
|
|
14145
|
+
}
|
|
14146
|
+
return result;
|
|
14147
|
+
}
|
|
14148
|
+
/** Reject text/image dependencies and effects that cannot be rendered independently. */ function validateGraphicSnapshot(context) {
|
|
14149
|
+
var _loop = function() {
|
|
14150
|
+
var element = _step1.value;
|
|
14151
|
+
var computed = readStyle(element);
|
|
14152
|
+
var ancestor = element !== target && element.contains(target);
|
|
14153
|
+
var groupEffect = ancestor && hasGraphicGroupEffect(function(name) {
|
|
14154
|
+
return name === 'mask' ? computed.maskImage : computed.getPropertyValue(name);
|
|
14155
|
+
});
|
|
14156
|
+
if (ancestor && (groupEffect || computed.mixBlendMode !== 'normal')) {
|
|
14157
|
+
throw new Error('A mixed group effect cannot be applied independently to its graphics.');
|
|
14158
|
+
}
|
|
14159
|
+
if (target.contains(element) && target !== root && computed.mixBlendMode !== 'normal') {
|
|
14160
|
+
throw new Error('Blend modes that depend on an external backdrop cannot be baked independently.');
|
|
14161
|
+
}
|
|
14162
|
+
};
|
|
14163
|
+
var root = context.root, target = context.target, elements = context.elements, dependencies = context.dependencies, readStyle = context.readStyle;
|
|
14164
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(dependencies), _step; !(_step = _iterator()).done;){
|
|
14165
|
+
var element = _step.value;
|
|
14166
|
+
if ([
|
|
14167
|
+
'text',
|
|
14168
|
+
'image',
|
|
14169
|
+
'foreignobject'
|
|
14170
|
+
].includes(element.tagName.toLowerCase())) {
|
|
14171
|
+
throw new Error('Graphic dependencies contain native text or images; they must remain editable.');
|
|
14172
|
+
}
|
|
14173
|
+
}
|
|
14174
|
+
for(var _iterator1 = _create_for_of_iterator_helper_loose(elements), _step1; !(_step1 = _iterator1()).done;)_loop();
|
|
14175
|
+
}
|
|
14176
|
+
function isStyleRule$1(rule) {
|
|
14177
|
+
return rule.type === 1;
|
|
14178
|
+
}
|
|
14179
|
+
function hasNestedRules(rule) {
|
|
14180
|
+
return 'cssRules' in rule;
|
|
14181
|
+
}
|
|
14182
|
+
function hasGraphicGroupEffect(readValue) {
|
|
14183
|
+
return groupEffects.some(function(name) {
|
|
14184
|
+
var value = readValue(name);
|
|
14185
|
+
return value != null && value !== 'none' && value !== 'normal' && value !== '1';
|
|
14186
|
+
});
|
|
14187
|
+
}
|
|
14188
|
+
|
|
14189
|
+
/** Mask and clipPath definitions do not produce visible layers. */ function collectNonRenderingDeclarationPaths(source) {
|
|
14190
|
+
var root = SVG(source).node;
|
|
14191
|
+
var elements = Array.from(root.querySelectorAll('*')).filter(function(element) {
|
|
14192
|
+
var tag = element.tagName.toLowerCase();
|
|
14193
|
+
return tag === 'mask' || tag === 'clippath';
|
|
14194
|
+
});
|
|
14195
|
+
return new Set(elements.map(buildElementTreePath));
|
|
14196
|
+
}
|
|
14197
|
+
/** A stylesheet diagnostic is resolved only when every element it can affect was baked. */ function collectBakedStylesheetPaths(source, isBaked) {
|
|
14198
|
+
var resolved = new Set();
|
|
14199
|
+
if (typeof DOMParser === 'undefined' || typeof CSSStyleSheet !== 'function') {
|
|
14200
|
+
return resolved;
|
|
14201
|
+
}
|
|
14202
|
+
var root = new DOMParser().parseFromString(source, 'image/svg+xml').documentElement;
|
|
14203
|
+
var allUsesBaked = Array.from(root.querySelectorAll('use')).every(function(element) {
|
|
14204
|
+
return isBaked(buildElementTreePath(element));
|
|
14205
|
+
});
|
|
14206
|
+
var isHandled = function isHandled(element) {
|
|
14207
|
+
if (isBaked(buildElementTreePath(element))) {
|
|
14208
|
+
return true;
|
|
14209
|
+
}
|
|
14210
|
+
if (element.closest('defs, symbol, marker, pattern, clipPath, mask, filter') != null) {
|
|
14211
|
+
return allUsesBaked;
|
|
14212
|
+
}
|
|
14213
|
+
return false;
|
|
14214
|
+
};
|
|
14215
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(Array.from(root.querySelectorAll('style'))), _step; !(_step = _iterator()).done;){
|
|
14216
|
+
var style = _step.value;
|
|
14217
|
+
var _style_textContent;
|
|
14218
|
+
var sheet = new CSSStyleSheet();
|
|
14219
|
+
sheet.replaceSync((_style_textContent = style.textContent) != null ? _style_textContent : '');
|
|
14220
|
+
var rules = Array.from(sheet.cssRules);
|
|
14221
|
+
var handled = rules.every(function(rule) {
|
|
14222
|
+
if (!isStyleRule(rule)) {
|
|
14223
|
+
return false;
|
|
14224
|
+
}
|
|
14225
|
+
if (Array.from(rule.style).every(function(name) {
|
|
14226
|
+
return name === 'mix-blend-mode';
|
|
14227
|
+
})) {
|
|
14228
|
+
return true;
|
|
14229
|
+
}
|
|
14230
|
+
var matched = [].concat([
|
|
14231
|
+
root
|
|
14232
|
+
], Array.from(root.querySelectorAll('*'))).filter(function(element) {
|
|
14233
|
+
return element.matches(rule.selectorText);
|
|
14234
|
+
});
|
|
14235
|
+
return matched.every(isHandled);
|
|
14236
|
+
});
|
|
14237
|
+
if (handled) {
|
|
14238
|
+
resolved.add(buildElementTreePath(style));
|
|
14239
|
+
}
|
|
14240
|
+
}
|
|
14241
|
+
return resolved;
|
|
14242
|
+
}
|
|
14243
|
+
function isStyleRule(rule) {
|
|
14244
|
+
return rule.type === 1;
|
|
14245
|
+
}
|
|
14246
|
+
|
|
14247
|
+
/** Rasterize graphics and track the source layers and warnings they replace. */ function buildBakedLayers(source, size, options, rasterScale) {
|
|
14248
|
+
return _async_to_generator(function() {
|
|
14249
|
+
var deferredOptions, bakedPaths, baked, bakingWarnings, blending, plan, _iterator, _step, downgrade, _iterator1, _step1, warning, _iterator2, _step2, scope, image, message, _image_bbox, left, top, right, bottom, width, height, error, reason, replacesSource, handledStyles, declarations;
|
|
14250
|
+
return _ts_generator(this, function(_state) {
|
|
14251
|
+
switch(_state.label){
|
|
14252
|
+
case 0:
|
|
14253
|
+
deferredOptions = _extends({}, options, {
|
|
14254
|
+
strict: false,
|
|
14255
|
+
onWarning: undefined
|
|
14256
|
+
});
|
|
14257
|
+
bakedPaths = [];
|
|
14258
|
+
baked = [];
|
|
14259
|
+
bakingWarnings = [];
|
|
14260
|
+
blending = normalizeLayerBlending(source);
|
|
14261
|
+
plan = planGraphicBaking(blending.source);
|
|
14262
|
+
for(_iterator = _create_for_of_iterator_helper_loose(blending.downgrades); !(_step = _iterator()).done;){
|
|
14263
|
+
downgrade = _step.value;
|
|
14264
|
+
collectLayerWarning(bakingWarnings, deferredOptions, SVG2IR_WARNING_CODES.LAYERS_BLEND_MODE_DOWNGRADED, 'mix-blend-mode "' + downgrade.mode + '" was downgraded to "normal"; the layer is retained.', downgrade.selector);
|
|
14265
|
+
}
|
|
14266
|
+
for(_iterator1 = _create_for_of_iterator_helper_loose(plan.warnings); !(_step1 = _iterator1()).done;){
|
|
14267
|
+
warning = _step1.value;
|
|
14268
|
+
collectLayerWarning(bakingWarnings, deferredOptions, SVG2IR_WARNING_CODES.LAYERS_UNSUPPORTED_FEATURE, warning.message, warning.selector);
|
|
14269
|
+
}
|
|
14270
|
+
_iterator2 = _create_for_of_iterator_helper_loose(plan.scopes);
|
|
14271
|
+
_state.label = 1;
|
|
14272
|
+
case 1:
|
|
14273
|
+
if (!!(_step2 = _iterator2()).done) return [
|
|
14274
|
+
3,
|
|
14275
|
+
6
|
|
14276
|
+
];
|
|
14277
|
+
scope = _step2.value;
|
|
14278
|
+
_state.label = 2;
|
|
14279
|
+
case 2:
|
|
14280
|
+
_state.trys.push([
|
|
14281
|
+
2,
|
|
14282
|
+
4,
|
|
14283
|
+
,
|
|
14284
|
+
5
|
|
14285
|
+
]);
|
|
14286
|
+
if (!canRasterizeSVG()) {
|
|
14287
|
+
throw new Error('Graphic baking requires a browser with SVG and Canvas support.');
|
|
14288
|
+
}
|
|
14289
|
+
return [
|
|
14290
|
+
4,
|
|
14291
|
+
rasterizeSVG(blending.source, scope.path, size, rasterScale, validateGraphicSnapshot)
|
|
14292
|
+
];
|
|
14293
|
+
case 3:
|
|
14294
|
+
image = _state.sent();
|
|
14295
|
+
bakedPaths.push(scope.path);
|
|
14296
|
+
message = image == null ? 'Graphic rasterization produced no visible pixels; no image layer was emitted.' : scope.isGroup ? 'Graphic group baked into a single PNG; child graphics and paint/effect parameters are no longer editable.' : 'Graphic baked into a PNG; vector geometry and paint/effect parameters are no longer editable.';
|
|
14297
|
+
collectLayerWarning(bakingWarnings, deferredOptions, SVG2IR_WARNING_CODES.LAYERS_GRAPHIC_BAKED, message, scope.selector);
|
|
14298
|
+
if (image == null) {
|
|
14299
|
+
return [
|
|
14300
|
+
3,
|
|
14301
|
+
5
|
|
14302
|
+
];
|
|
14303
|
+
}
|
|
14304
|
+
_image_bbox = image.bbox, left = _image_bbox[0], top = _image_bbox[1], right = _image_bbox[2], bottom = _image_bbox[3];
|
|
14305
|
+
width = right - left;
|
|
14306
|
+
height = bottom - top;
|
|
14307
|
+
baked.push({
|
|
14308
|
+
path: scope.path,
|
|
14309
|
+
layer: {
|
|
14310
|
+
id: '',
|
|
14311
|
+
type: 'image',
|
|
14312
|
+
role: 'content',
|
|
14313
|
+
bbox: image.bbox,
|
|
14314
|
+
size: [
|
|
14315
|
+
width,
|
|
14316
|
+
height
|
|
14317
|
+
],
|
|
14318
|
+
rotation: [
|
|
14319
|
+
0,
|
|
14320
|
+
0,
|
|
14321
|
+
0
|
|
14322
|
+
],
|
|
14323
|
+
url: image.url,
|
|
14324
|
+
width: width,
|
|
14325
|
+
height: height
|
|
14326
|
+
}
|
|
14327
|
+
});
|
|
14328
|
+
return [
|
|
14329
|
+
3,
|
|
14330
|
+
5
|
|
14331
|
+
];
|
|
14332
|
+
case 4:
|
|
14333
|
+
error = _state.sent();
|
|
14334
|
+
reason = _instanceof(error, Error) ? error.message : String(error);
|
|
14335
|
+
collectLayerWarning(bakingWarnings, deferredOptions, SVG2IR_WARNING_CODES.LAYERS_UNSUPPORTED_FEATURE, "Graphic baking failed: " + reason, scope.selector);
|
|
14336
|
+
return [
|
|
14337
|
+
3,
|
|
14338
|
+
5
|
|
14339
|
+
];
|
|
14340
|
+
case 5:
|
|
14341
|
+
return [
|
|
14342
|
+
3,
|
|
14343
|
+
1
|
|
14344
|
+
];
|
|
14345
|
+
case 6:
|
|
14346
|
+
replacesSource = function replacesSource(path) {
|
|
14347
|
+
return path != null && bakedPaths.some(function(scope) {
|
|
14348
|
+
return scope === '' || path === scope || path.startsWith("" + scope + "/");
|
|
14349
|
+
});
|
|
14350
|
+
};
|
|
14351
|
+
handledStyles = collectBakedStylesheetPaths(blending.source, replacesSource);
|
|
14352
|
+
declarations = collectNonRenderingDeclarationPaths(source);
|
|
14353
|
+
return [
|
|
14354
|
+
2,
|
|
14355
|
+
{
|
|
14356
|
+
layers: baked,
|
|
14357
|
+
warnings: bakingWarnings,
|
|
14358
|
+
replacesSource: replacesSource,
|
|
14359
|
+
resolvesWarning: function resolvesWarning(warning, path) {
|
|
14360
|
+
return replacesSource(path) || warning.code === SVG2IR_WARNING_CODES.ELEMENT_DROPPED && path != null && (handledStyles.has(path) || declarations.has(path));
|
|
14361
|
+
}
|
|
14362
|
+
}
|
|
14363
|
+
];
|
|
14364
|
+
}
|
|
14365
|
+
});
|
|
14366
|
+
})();
|
|
14367
|
+
}
|
|
13258
14368
|
|
|
13259
14369
|
function encodeBase64(data) {
|
|
13260
14370
|
var chunks = [];
|
|
@@ -13443,26 +14553,103 @@ function fetchImageResource(url) {
|
|
|
13443
14553
|
}).apply(this, arguments);
|
|
13444
14554
|
}
|
|
13445
14555
|
|
|
13446
|
-
|
|
14556
|
+
var animatedTags = new Set([
|
|
14557
|
+
'animate',
|
|
14558
|
+
'animatetransform',
|
|
14559
|
+
'animatemotion',
|
|
14560
|
+
'set'
|
|
14561
|
+
]);
|
|
14562
|
+
/** Static layers cannot sample animation; drop SMIL nodes so elements keep their authored state. */ function stripLayerAnimation(source) {
|
|
14563
|
+
var svg = SVG(source);
|
|
14564
|
+
if ((svg == null ? void 0 : svg.node) == null) {
|
|
14565
|
+
return {
|
|
14566
|
+
source: source,
|
|
14567
|
+
removed: 0
|
|
14568
|
+
};
|
|
14569
|
+
}
|
|
14570
|
+
var elements = Array.from(svg.node.querySelectorAll('*')).filter(function(element) {
|
|
14571
|
+
return animatedTags.has(element.tagName.toLowerCase());
|
|
14572
|
+
});
|
|
14573
|
+
if (elements.length === 0) {
|
|
14574
|
+
return {
|
|
14575
|
+
source: source,
|
|
14576
|
+
removed: 0
|
|
14577
|
+
};
|
|
14578
|
+
}
|
|
14579
|
+
for(var _iterator = _create_for_of_iterator_helper_loose(elements), _step; !(_step = _iterator()).done;){
|
|
14580
|
+
var element = _step.value;
|
|
14581
|
+
element.remove();
|
|
14582
|
+
}
|
|
14583
|
+
return {
|
|
14584
|
+
source: svg.svg(),
|
|
14585
|
+
removed: elements.length
|
|
14586
|
+
};
|
|
14587
|
+
}
|
|
14588
|
+
|
|
14589
|
+
function convertSVGToLayers(source, options, loadImage, preservedData, rasterScale) {
|
|
14590
|
+
if (rasterScale === void 0) rasterScale = 2;
|
|
13447
14591
|
return _async_to_generator(function() {
|
|
13448
|
-
var
|
|
14592
|
+
var layerSources, warningSources, collected, stripped, staticSource, parsed, composition, baking, assembled, _tmp, _tmp1;
|
|
13449
14593
|
return _ts_generator(this, function(_state) {
|
|
13450
14594
|
switch(_state.label){
|
|
13451
14595
|
case 0:
|
|
13452
|
-
|
|
13453
|
-
|
|
13454
|
-
|
|
13455
|
-
|
|
13456
|
-
|
|
14596
|
+
layerSources = new Map();
|
|
14597
|
+
warningSources = new Map();
|
|
14598
|
+
collected = {
|
|
14599
|
+
textDataByLayerId: new Map(),
|
|
14600
|
+
paintAnimationsByLayerId: new Map(),
|
|
14601
|
+
shapeAnimationsByLayerId: new Map()
|
|
13457
14602
|
};
|
|
14603
|
+
stripped = stripLayerAnimation(source);
|
|
14604
|
+
staticSource = stripped.source;
|
|
14605
|
+
// Preserve original layer IDs while gathering the source identities required by baking and assembly.
|
|
14606
|
+
parsed = parseSVGToIRWithPreservedData(staticSource, _extends({}, options, {
|
|
14607
|
+
strict: false,
|
|
14608
|
+
onWarning: undefined
|
|
14609
|
+
}), _extends({}, collected, {
|
|
14610
|
+
layerSourceCollector: layerSources,
|
|
14611
|
+
warningSourceCollector: warningSources
|
|
14612
|
+
}));
|
|
14613
|
+
if (stripped.removed > 0) {
|
|
14614
|
+
collectLayerWarning(parsed.warnings, _extends({}, options, {
|
|
14615
|
+
strict: false,
|
|
14616
|
+
onWarning: undefined
|
|
14617
|
+
}), SVG2IR_WARNING_CODES.LAYERS_ANIMATION_STRIPPED, "" + stripped.removed + " animated element(s) were removed; static layers output does not sample animation.");
|
|
14618
|
+
}
|
|
14619
|
+
composition = parsed.ir.compositions[0];
|
|
13458
14620
|
return [
|
|
13459
14621
|
4,
|
|
13460
|
-
|
|
14622
|
+
buildBakedLayers(staticSource, [
|
|
14623
|
+
composition.width,
|
|
14624
|
+
composition.height
|
|
14625
|
+
], options, rasterScale)
|
|
13461
14626
|
];
|
|
13462
14627
|
case 1:
|
|
14628
|
+
baking = _state.sent();
|
|
14629
|
+
assembled = assembleSVGLayers({
|
|
14630
|
+
scene: parsed.ir,
|
|
14631
|
+
options: options,
|
|
14632
|
+
warnings: parsed.warnings,
|
|
14633
|
+
preservedData: preservedData != null ? preservedData : collected,
|
|
14634
|
+
layerSources: layerSources,
|
|
14635
|
+
warningSources: warningSources,
|
|
14636
|
+
baking: baking
|
|
14637
|
+
});
|
|
14638
|
+
_tmp = [
|
|
14639
|
+
{},
|
|
14640
|
+
assembled
|
|
14641
|
+
];
|
|
14642
|
+
_tmp1 = {};
|
|
14643
|
+
return [
|
|
14644
|
+
4,
|
|
14645
|
+
inlineImageResources(assembled.data, loadImage)
|
|
14646
|
+
];
|
|
14647
|
+
case 2:
|
|
13463
14648
|
return [
|
|
13464
14649
|
2,
|
|
13465
|
-
(
|
|
14650
|
+
_extends.apply(void 0, _tmp.concat([
|
|
14651
|
+
(_tmp1.data = _state.sent(), _tmp1)
|
|
14652
|
+
]))
|
|
13466
14653
|
];
|
|
13467
14654
|
}
|
|
13468
14655
|
});
|
|
@@ -15992,11 +17179,12 @@ function convertSVG(source, options, preservedData) {
|
|
|
15992
17179
|
throw new TypeError('[svg2ir] outputFormat must be ge or layers.');
|
|
15993
17180
|
}
|
|
15994
17181
|
if ((options == null ? void 0 : options.outputFormat) === 'layers') {
|
|
15995
|
-
options.outputFormat; var loadImage = options.loadImage, parserOptions = _object_without_properties_loose(options, [
|
|
17182
|
+
options.outputFormat; var loadImage = options.loadImage, rasterScale = options.rasterScale, parserOptions = _object_without_properties_loose(options, [
|
|
15996
17183
|
"outputFormat",
|
|
15997
|
-
"loadImage"
|
|
17184
|
+
"loadImage",
|
|
17185
|
+
"rasterScale"
|
|
15998
17186
|
]);
|
|
15999
|
-
return convertSVGToLayers(source, parserOptions, loadImage, preservedData);
|
|
17187
|
+
return convertSVGToLayers(source, parserOptions, loadImage, preservedData, rasterScale);
|
|
16000
17188
|
}
|
|
16001
17189
|
var _ref1 = options != null ? options : {}; _ref1.outputFormat; var geOptions = _object_without_properties_loose(_ref1, [
|
|
16002
17190
|
"outputFormat"
|
|
@@ -16004,5 +17192,5 @@ function convertSVG(source, options, preservedData) {
|
|
|
16004
17192
|
return convertSVGToGE(source, geOptions, preservedData);
|
|
16005
17193
|
}
|
|
16006
17194
|
|
|
16007
|
-
export { convertSVG, convertSVGToGE, convertSVGToIR, splitSVG };
|
|
17195
|
+
export { convertSVG, convertSVGToGE, convertSVGToIR, inferNumericWeightFromFamily, splitSVG };
|
|
16008
17196
|
//# sourceMappingURL=index.mjs.map
|