From a37ea675122ca7da96a84090aa86f7e7eaa038d4 Mon Sep 17 00:00:00 2001 From: Paul Kaplan <pkaplan@media.mit.edu> Date: Thu, 24 Jan 2019 08:49:17 -0500 Subject: [PATCH] Use fontInliner instead of entire SVGRenderer for showing costume tiles. The fontInliner from the scratch-svg-renderer was changed recently to work entirely on strings, so we can use that instead of parsing/loading/possibly drawing/stringifying the entire SVG. This does not address the size-1 cache issue where the cache is invalidated any time there is more than 1 sprite. But that can be handled later. Using the 4x CPU slowdown in chrome dev tools, on a simple costume that just has a single text element the getCostumeUrl time goes from 10ms to 1ms, very important for updating in a timely way. --- src/lib/get-costume-url.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib/get-costume-url.js b/src/lib/get-costume-url.js index 2ecb196c2..7c4b66572 100644 --- a/src/lib/get-costume-url.js +++ b/src/lib/get-costume-url.js @@ -1,5 +1,5 @@ import storage from './storage'; -import {SVGRenderer} from 'scratch-svg-renderer'; +import {inlineSvgFonts} from 'scratch-svg-renderer'; // Contains 'font-family', but doesn't only contain 'font-family="none"' const HAS_FONT_REGEXP = 'font-family(?!="none")'; @@ -21,9 +21,7 @@ const getCostumeUrl = (function () { if (asset.assetType === storage.AssetType.ImageVector) { const svgString = asset.decodeText(); if (svgString.match(HAS_FONT_REGEXP)) { - const svgRenderer = new SVGRenderer(); - svgRenderer.loadString(svgString); - const svgText = svgRenderer.toString(true /* shouldInjectFonts */); + const svgText = inlineSvgFonts(svgString); cachedUrl = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`; } else { cachedUrl = asset.encodeDataURI(); -- GitLab