@@ -242,7 +242,9 @@ Renderer.prototype.renderParagraph = function(){
242242
243243 var fragments = PurgeWhiteSpace ( this . paragraph . text )
244244 , styles = this . paragraph . style
245- this . paragraph = { 'text' :[ ] , 'style' :[ ] }
245+ , blockstyle = this . paragraph . blockstyle
246+ , priorblockstype = this . paragraph . blockstyle || { }
247+ this . paragraph = { 'text' :[ ] , 'style' :[ ] , 'blockstyle' :{ } , 'priorblockstyle' :blockstyle }
246248
247249 if ( ! fragments . join ( '' ) . trim ( ) ) {
248250 /* if it's empty string */
@@ -254,10 +256,25 @@ Renderer.prototype.renderParagraph = function(){
254256
255257 , maxLineHeight
256258 , defaultFontSize = 12
257- , i , l
259+ , fontToUnitRatio = defaultFontSize / this . pdf . internal . scaleFactor
260+
261+ // these will be in pdf instance units
262+ , paragraphspacing_before = (
263+ // we only use margin-top potion that is larger than margin-bottom of previous elem
264+ // because CSS margins don't stack, they overlap.
265+ Math . max ( ( blockstyle [ 'margin-top' ] || 0 ) - ( priorblockstype [ 'margin-bottom' ] || 0 ) , 0 ) +
266+ ( blockstyle [ 'padding-top' ] || 0 )
267+ ) * fontToUnitRatio
268+ , paragraphspacing_after = (
269+ ( blockstyle [ 'margin-bottom' ] || 0 ) + ( blockstyle [ 'padding-bottom' ] || 0 )
270+ ) * fontToUnitRatio
258271
259272 , out = this . pdf . internal . write
260273
274+ , i , l
275+
276+ this . y += paragraphspacing_before
277+
261278 out (
262279 'q' // canning the scope
263280 , 'BT' // Begin Text
@@ -298,21 +315,29 @@ Renderer.prototype.renderParagraph = function(){
298315 // y is in user units (cm, inch etc)
299316 // maxLineHeight is ratio of defaultFontSize
300317 // defaultFontSize is in points always.
301- // this.internal.scaleFactor is ratio of user unit to points. Dividing by it converts points to user units.
302- // verticalOffset will be in user units.
303- this . y += maxLineHeight * defaultFontSize / this . pdf . internal . scaleFactor
318+ // this.internal.scaleFactor is ratio of user unit to points.
319+ // Dividing by it converts points to user units.
320+ // vertical offset will be in user units.
321+ // this.y is in user units.
322+ this . y += maxLineHeight * fontToUnitRatio
304323 }
305324
306325 out (
307326 'ET' // End Text
308327 , 'Q' // restore scope
309328 )
329+
330+ this . y += paragraphspacing_after
310331}
311332
312333Renderer . prototype . setBlockBoundary = function ( ) {
313334 this . renderParagraph ( )
314335}
315336
337+ Renderer . prototype . setBlockStyle = function ( css ) {
338+ this . paragraph . blockstyle = css
339+ }
340+
316341Renderer . prototype . addText = function ( text , css ) {
317342 this . paragraph . text . push ( text )
318343 this . paragraph . style . push ( css )
@@ -395,6 +420,13 @@ function GetCSS(element){
395420
396421 css [ 'display' ] = $e . css ( 'display' ) === 'inline' ? 'inline' : 'block'
397422
423+ if ( css [ 'display' ] === 'block' ) {
424+ css [ 'margin-top' ] = ResolveUnitedNumber ( $e . css ( 'margin-top' ) ) || 0
425+ css [ 'margin-bottom' ] = ResolveUnitedNumber ( $e . css ( 'margin-bottom' ) ) || 0
426+ css [ 'padding-top' ] = ResolveUnitedNumber ( $e . css ( 'padding-top' ) ) || 0
427+ css [ 'padding-bottom' ] = ResolveUnitedNumber ( $e . css ( 'padding-bottom' ) ) || 0
428+ }
429+
398430 return css
399431}
400432
@@ -436,11 +468,12 @@ function elementHandledElsewhere(element, renderer, elementHandlers){
436468function DrillForContent ( element , renderer , elementHandlers ) {
437469 var cns = element . childNodes
438470 , cn
439- , css = GetCSS ( element )
440- , isBlock = css . display === 'block'
471+ , fragmentCSS = GetCSS ( element )
472+ , isBlock = fragmentCSS . display === 'block'
441473
442474 if ( isBlock ) {
443- renderer . setBlockBoundary ( )
475+ renderer . setBlockBoundary ( )
476+ renderer . setBlockStyle ( fragmentCSS )
444477 }
445478
446479 for ( var i = 0 , l = cns . length ; i < l ; i ++ ) {
@@ -451,10 +484,10 @@ function DrillForContent(element, renderer, elementHandlers){
451484 DrillForContent ( cn , renderer , elementHandlers )
452485 }
453486 } else if ( cn . nodeType === 3 ) {
454- renderer . addText ( cn . nodeValue , css )
487+ renderer . addText ( cn . nodeValue , fragmentCSS )
455488 }
456489 } else if ( typeof cn === 'string' ) {
457- renderer . addText ( cn , css )
490+ renderer . addText ( cn , fragmentCSS )
458491 }
459492 }
460493
0 commit comments