s4s-editor / local-scratch-vm /src /util /get-costume-url.js
soiz1's picture
Upload 811 files
30c32c8 verified
raw
history blame
672 Bytes
const {inlineSvgFonts} = require('scratch-svg-renderer');
const HAS_FONT_REGEXP = 'font-family(?!="none")';
const getCostumeUrl = asset => {
// If the SVG refers to fonts, they must be inlined in order to display correctly in the img tag.
// Avoid parsing the SVG when possible, since it's expensive.
if (asset.assetType.name === 'ImageVector') {
const svgString = asset.decodeText();
if (svgString.match(HAS_FONT_REGEXP)) {
const svgText = inlineSvgFonts(svgString);
return `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
}
}
return asset.encodeDataURI();
};
module.exports = getCostumeUrl;