Skip to content

Instantly share code, notes, and snippets.

@siriux
Last active August 29, 2015 14:15
Show Gist options
  • Save siriux/f84d817b526a476686b5 to your computer and use it in GitHub Desktop.
Save siriux/f84d817b526a476686b5 to your computer and use it in GitHub Desktop.
Transform flowRegions from Inkscape into foreignObjects
// window.onload = init; // This can be overwritten
window.addEventListener("load",init);
function init() {
var texts = document.querySelectorAll("flowRoot");
for (var i = 0; i < texts.length; i++) {
var t = texts[i];
var rect = t.querySelector("flowRegion > rect");
var fO = document.createElementNS("http://www.w3.org/2000/svg", "foreignObject");
fO.setAttribute("x",rect.getAttribute("x"));
fO.setAttribute("y",rect.getAttribute("y"));
fO.setAttribute("width", rect.getAttribute("width"));
fO.setAttribute("height", rect.getAttribute("height"));
fO.setAttribute("transform", t.getAttribute("transform"));
var div = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
div.setAttribute("xmlns","http://www.w3.org/1999/xhtml");
div.setAttribute("style", t.getAttribute("style"));
div.innerHTML = t.innerHTML
.replace(/<flowRegion.*flowRegion>/g,"")
.replace(/http:\/\/www.w3.org\/2000\/svg/g,"http://www.w3.org/1999/xhtml")
.replace(/flowPara/g,"div")
.replace(/flowSpan/g,"span")
;
var innerDivs = div.querySelectorAll("div");
for (var j = 0; j < innerDivs.length; j++) {
var d = innerDivs[j];
if (d.innerHTML == "") {
d.innerHTML = "\u200B";
}
}
fO.appendChild(div);
t.parentNode.replaceChild(fO, t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment