Skip to content

Commit

Permalink
Fixes #1027 - SVG namespaces from Illustrator. Also switched to Snap'…
Browse files Browse the repository at this point in the history
…s toDataUrl()
  • Loading branch information
Andy Werner committed Oct 6, 2020
1 parent 9bed315 commit da66d1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 10 additions & 10 deletions octoprint_mrbeam/static/js/render_fills.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,8 @@ Snap.plugin(function (Snap, Element, Paper, global) {
bbox.y
);

// get svg as dataUrl TODO: check snap's .toDataURL() function instead of a homebrew one.
var svgStr = elem.outerSVG();
// on iOS (Safari and Chrome) embedded images are linked with NS1:href which doesn't work later on...
svgStr = svgStr.replace(
/NS1:href=/gi,
'xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href='
);
var svgDataUri =
"data:image/svg+xml;base64," +
window.btoa(unescape(encodeURIComponent(svgStr))); //deprecated unescape needed!
// get svg as dataUrl
var svgDataUri = elem.toDataURL();

// init render canvas and attach to page
var renderCanvas = document.createElement("canvas");
Expand Down Expand Up @@ -289,6 +281,14 @@ Snap.plugin(function (Snap, Element, Paper, global) {
len +
")";
console.error(msg, e);
console.log(
"renderPNG ERR: original svgStr that failed to load: ",
svgStr
);
console.log(
"renderPNG ERR: svgDataUri that failed to load: ",
svgDataUri
);
new PNotify({
title: gettext("Conversion failed"),
text: msg,
Expand Down
18 changes: 15 additions & 3 deletions octoprint_mrbeam/static/js/working_area.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,12 +1139,24 @@ $(function () {

// copy namespaces into group
if (attr.name.indexOf("xmlns") === 0) {
namespaces[attr.name] = attr.value;
// Illustrator uses namespaces that reference a entity defined as ENTITY outside of the xml of the svg.
// like this: xmlns:x="&ns_extend;"
// We replace it to xmlns:x="ENTITYREF_ns_extend"
namespaces[attr.name] = attr.value
.replace(/&/g, "ENTITYREF_")
.replace(/;/g, "");
if (attr.value.match(/^&.+;$/)) {
if (attr.name == "xmlns") {
namespaces[attr.name] =
"http://www.w3.org/2000/svg";
} else if (attr.name == "xmlns:xlink") {
// not sure if this is important
namespaces[attr.name] =
"http://www.w3.org/1999/xlink";
} else {
namespaces[attr.name] = attr.value
.replace(/&/g, "ENTITYREF_")
.replace(/;/g, "");
}
}
}
}
return namespaces;
Expand Down

0 comments on commit da66d1b

Please sign in to comment.