From 589dd7d5e04f62a57257c96530ba6b3a3af6ee6b Mon Sep 17 00:00:00 2001
From: DD <liudi08@gmail.com>
Date: Thu, 10 May 2018 11:00:51 -0400
Subject: [PATCH] Attach the svg renderer instead of providing it in order to
 fix VM tests

---
 package.json               | 2 +-
 src/engine/runtime.js      | 8 ++++++++
 src/import/load-costume.js | 8 +++-----
 src/virtual-machine.js     | 8 ++++++++
 4 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/package.json b/package.json
index 0352094a7..cdebb7cfd 100644
--- a/package.json
+++ b/package.json
@@ -61,11 +61,11 @@
     "scratch-parser": "^4.1.0",
     "scratch-render": "latest",
     "scratch-storage": "^0.4.0",
-    "scratch-svg-renderer": "0.1.0-prerelease.20180423193917",
     "script-loader": "0.7.2",
     "socket.io-client": "2.0.4",
     "stats.js": "^0.17.0",
     "tap": "^11.0.1",
+    "text-encoding": "0.6.4",
     "tiny-worker": "^2.1.1",
     "webpack": "^4.8.0",
     "webpack-cli": "^2.0.15",
diff --git a/src/engine/runtime.js b/src/engine/runtime.js
index c3d68657d..004b12ead 100644
--- a/src/engine/runtime.js
+++ b/src/engine/runtime.js
@@ -917,6 +917,14 @@ class Runtime extends EventEmitter {
         this.renderer = renderer;
     }
 
+    /**
+     * Set the svg adapter, which converts scratch 2 svgs to scratch 3 svgs
+     * @param {!SvgRenderer} svgAdapter The adapter to attach
+     */
+    attachV2SVGAdapter (svgAdapter) {
+        this.v2SvgAdapter = svgAdapter;
+    }
+
     /**
      * Attach the storage module
      * @param {!ScratchStorage} storage The storage module to attach
diff --git a/src/import/load-costume.js b/src/import/load-costume.js
index 4f4a09d17..f76fb699a 100644
--- a/src/import/load-costume.js
+++ b/src/import/load-costume.js
@@ -1,6 +1,5 @@
 const StringUtil = require('../util/string-util');
 const log = require('../util/log');
-const SvgRenderer = require('scratch-svg-renderer').SVGRenderer;
 
 /**
  * Initialize a costume from an asset asynchronously.
@@ -34,10 +33,9 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime, optVersio
     if (costumeAsset.assetType === AssetType.ImageVector) {
         let svgString = costumeAsset.decodeText();
         // SVG Renderer load fixes "quirks" associated with Scratch 2 projects
-        if (optVersion && optVersion === 2) {
-            const svgRenderer = new SvgRenderer();
-            svgRenderer.loadString(svgString);
-            svgString = svgRenderer.toString();
+        if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) {
+            runtime.v2SvgAdapter.loadString(svgString);
+            svgString = runtime.v2SvgAdapter.toString();
             // Put back into storage
             const storage = runtime.storage;
             costumeAsset.encodeTextData(svgString, storage.DataFormat.SVG);
diff --git a/src/virtual-machine.js b/src/virtual-machine.js
index 2758c35ef..05ef9ad1b 100644
--- a/src/virtual-machine.js
+++ b/src/virtual-machine.js
@@ -828,6 +828,14 @@ class VirtualMachine extends EventEmitter {
         this.runtime.attachRenderer(renderer);
     }
 
+    /**
+     * Set the svg adapter for the VM/runtime, which converts scratch 2 svgs to scratch 3 svgs
+     * @param {!SvgRenderer} svgAdapter The adapter to attach
+     */
+    attachV2SVGAdapter (svgAdapter) {
+        this.runtime.attachV2SVGAdapter(svgAdapter);
+    }
+
     /**
      * Set the storage module for the VM/runtime
      * @param {!ScratchStorage} storage The storage module to attach