From 63380ed539f2e87449de62c32a77877757696515 Mon Sep 17 00:00:00 2001 From: Viktor Kojouharov Date: Wed, 17 Dec 2014 14:53:30 +0200 Subject: [PATCH] Preserve the meaning of BR elements Since BR elements are line breaks, treat them as such and leave a unicode line separator token in the array of text. When splitting the fragments into lines, treat the line separator as an explicit break. --- jspdf.plugin.from_html.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/jspdf.plugin.from_html.js b/jspdf.plugin.from_html.js index 79395fcd9..89488c2c8 100644 --- a/jspdf.plugin.from_html.js +++ b/jspdf.plugin.from_html.js @@ -87,14 +87,17 @@ trailingSpace = true; i = 0; while (i !== l) { - fragment = array[i].replace(/\s+/g, " "); - if (trailingSpace) { - fragment = fragment.trimLeft(); - } - if (fragment) { - trailingSpace = r.test(fragment); + // Leave the line breaks intact + if (array[i] != "\u2028") { + fragment = array[i].replace(/\s+/g, " "); + if (trailingSpace) { + fragment = fragment.trimLeft(); + } + if (fragment) { + trailingSpace = r.test(fragment); + } + array[i] = fragment; } - array[i] = fragment; i++; } return array; @@ -463,6 +466,7 @@ renderer.x = temp; } else if (cn.nodeName === "BR") { renderer.y += fragmentCSS["font-size"] * renderer.pdf.internal.scaleFactor; + renderer.addText("\u2028", clone(fragmentCSS)); } else { if (!elementHandledElsewhere(cn, renderer, elementHandlers)) { DrillForContent(cn, renderer, elementHandlers); @@ -723,7 +727,10 @@ textIndent : currentLineLength }; fragmentLength = this.pdf.getStringUnitWidth(fragment, fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k; - if (currentLineLength + fragmentLength > maxLineLength) { + if (fragment == "\u2028") { + line = []; + lines.push(line); + } else if (currentLineLength + fragmentLength > maxLineLength) { fragmentChopped = this.pdf.splitTextToSize(fragment, maxLineLength, fragmentSpecificMetrics); line.push([fragmentChopped.shift(), style]); while (fragmentChopped.length) {