diff --git a/src/svg2pdf.js b/src/svg2pdf.js
index 3da1d55e..b1de4b5e 100644
--- a/src/svg2pdf.js
+++ b/src/svg2pdf.js
@@ -1315,6 +1315,29 @@ SOFTWARE.
}
};
+ var textMeasuringTextElement = null;
+ function getMeasurementTextNode() {
+ if (!textMeasuringTextElement) {
+ textMeasuringTextElement = document.createElementNS(svgNamespaceURI, "text");
+
+ var svg = document.createElementNS(svgNamespaceURI, "svg");
+ svg.appendChild(textMeasuringTextElement);
+
+ svg.style.setProperty("position", "absolute");
+ svg.style.setProperty("visibility", "hidden");
+ document.body.appendChild(svg);
+ }
+
+ return textMeasuringTextElement
+ }
+
+ function cleanupTextMeasuring() {
+ if (textMeasuringTextElement) {
+ document.body.removeChild(textMeasuringTextElement.parentNode);
+ textMeasuringTextElement = null
+ }
+ }
+
/**
* Canvas text measuring is a lot faster than svg measuring. However, it is inaccurate for some fonts. So test each
* font once and decide if canvas is accurate enough.
@@ -1346,24 +1369,15 @@ SOFTWARE.
* @param {string} fontWeight
*/
function svgTextMeasure(text, fontFamily, fontSize, fontStyle, fontWeight) {
- var textNode = document.createElementNS(svgNamespaceURI, "text");
+ var textNode = getMeasurementTextNode();
textNode.setAttribute("font-family", fontFamily);
textNode.setAttribute("font-size", fontSize);
textNode.setAttribute("font-style", fontStyle);
textNode.setAttribute("font-weight", fontWeight);
textNode.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
- textNode.appendChild(document.createTextNode(text));
-
- var svg = document.createElementNS(svgNamespaceURI, "svg");
- svg.appendChild(textNode);
- svg.setAttribute("visibility", "hidden");
- document.body.appendChild(svg);
-
- var width = textNode.getBBox().width;
+ textNode.textContent = text;
- document.body.removeChild(svg);
-
- return width;
+ return textNode.getBBox().width;
}
var testString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789!\"$%&/()=?'\\+*-_.:,;^}][{#~|<>";
@@ -2339,6 +2353,8 @@ SOFTWARE.
});
+ cleanupTextMeasuring();
+
return _pdf;
};
diff --git a/tests/big-graph1.html b/tests/big-graph1.html
index d744f806..e9212418 100644
--- a/tests/big-graph1.html
+++ b/tests/big-graph1.html
@@ -1,11 +1,12 @@
-
-
+
+
+
SVG:
@@ -31,6 +32,5 @@
PDF:
console.timeEnd("t");
}
- setTimeout(save, 10);
-
\ No newline at end of file
+
@@ -33,4 +32,4 @@
PDF:
setTimeout(save, 10);
-
diff --git a/tests/big-graph2.html b/tests/big-graph2.html
index b930558e..405ec58a 100644
--- a/tests/big-graph2.html
+++ b/tests/big-graph2.html
@@ -1,8 +1,7 @@