Skip to content
Browse files

plugins: fromHTML - now detecting padding+margin before+after for par…

…agraphs
  • Loading branch information...
1 parent d3afdc5 commit 916d27852af158bd828ba2d0d09bd3cc21e85dee Daniel Dotsenko committed Aug 30, 2012
Showing with 43 additions and 10 deletions.
  1. +43 −10 jspdf.plugin.from_html.js
View
53 jspdf.plugin.from_html.js
@@ -242,7 +242,9 @@ Renderer.prototype.renderParagraph = function(){
var fragments = PurgeWhiteSpace( this.paragraph.text )
, styles = this.paragraph.style
- this.paragraph = {'text':[], 'style':[]}
+ , blockstyle = this.paragraph.blockstyle
+ , priorblockstype = this.paragraph.blockstyle || {}
+ this.paragraph = {'text':[], 'style':[], 'blockstyle':{}, 'priorblockstyle':blockstyle}
if (!fragments.join('').trim()) {
/* if it's empty string */
@@ -254,10 +256,25 @@ Renderer.prototype.renderParagraph = function(){
, maxLineHeight
, defaultFontSize = 12
- , i, l
+ , fontToUnitRatio = defaultFontSize / this.pdf.internal.scaleFactor
+
+ // these will be in pdf instance units
+ , paragraphspacing_before = (
+ // we only use margin-top potion that is larger than margin-bottom of previous elem
+ // because CSS margins don't stack, they overlap.
+ Math.max( ( blockstyle['margin-top'] || 0 ) - ( priorblockstype['margin-bottom'] || 0 ), 0 ) +
+ ( blockstyle['padding-top'] || 0 )
+ ) * fontToUnitRatio
+ , paragraphspacing_after = (
+ ( blockstyle['margin-bottom'] || 0 ) + ( blockstyle['padding-bottom'] || 0 )
+ ) * fontToUnitRatio
, out = this.pdf.internal.write
+ , i, l
+
+ this.y += paragraphspacing_before
+
out(
'q' // canning the scope
, 'BT' // Begin Text
@@ -298,21 +315,29 @@ Renderer.prototype.renderParagraph = function(){
// y is in user units (cm, inch etc)
// maxLineHeight is ratio of defaultFontSize
// defaultFontSize is in points always.
- // this.internal.scaleFactor is ratio of user unit to points. Dividing by it converts points to user units.
- // verticalOffset will be in user units.
- this.y += maxLineHeight * defaultFontSize / this.pdf.internal.scaleFactor
+ // this.internal.scaleFactor is ratio of user unit to points.
+ // Dividing by it converts points to user units.
+ // vertical offset will be in user units.
+ // this.y is in user units.
+ this.y += maxLineHeight * fontToUnitRatio
}
out(
'ET' // End Text
, 'Q' // restore scope
)
+
+ this.y += paragraphspacing_after
}
Renderer.prototype.setBlockBoundary = function(){
this.renderParagraph()
}
+Renderer.prototype.setBlockStyle = function(css){
+ this.paragraph.blockstyle = css
+}
+
Renderer.prototype.addText = function(text, css){
this.paragraph.text.push(text)
this.paragraph.style.push(css)
@@ -395,6 +420,13 @@ function GetCSS(element){
css['display'] = $e.css('display') === 'inline' ? 'inline' : 'block'
+ if (css['display'] === 'block'){
+ css['margin-top'] = ResolveUnitedNumber( $e.css('margin-top') ) || 0
+ css['margin-bottom'] = ResolveUnitedNumber( $e.css('margin-bottom') ) || 0
+ css['padding-top'] = ResolveUnitedNumber( $e.css('padding-top') ) || 0
+ css['padding-bottom'] = ResolveUnitedNumber( $e.css('padding-bottom') ) || 0
+ }
+
return css
}
@@ -436,11 +468,12 @@ function elementHandledElsewhere(element, renderer, elementHandlers){
function DrillForContent(element, renderer, elementHandlers){
var cns = element.childNodes
, cn
- , css = GetCSS(element)
- , isBlock = css.display === 'block'
+ , fragmentCSS = GetCSS(element)
+ , isBlock = fragmentCSS.display === 'block'
if (isBlock) {
- renderer.setBlockBoundary()
+ renderer.setBlockBoundary()
+ renderer.setBlockStyle(fragmentCSS)
}
for (var i = 0, l = cns.length; i < l ; i++){
@@ -451,10 +484,10 @@ function DrillForContent(element, renderer, elementHandlers){
DrillForContent(cn, renderer, elementHandlers)
}
} else if (cn.nodeType === 3){
- renderer.addText( cn.nodeValue, css )
+ renderer.addText( cn.nodeValue, fragmentCSS )
}
} else if (typeof cn === 'string') {
- renderer.addText( cn, css )
+ renderer.addText( cn, fragmentCSS )
}
}

0 comments on commit 916d278

Please sign in to comment.
Something went wrong with that request. Please try again.