From 7004fb0d0e08cb123dd2de60c382f830aa62c1e3 Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Mon, 17 Sep 2018 22:11:25 +0300 Subject: [PATCH] Add inline member on the DrawContext structure --- pdf/creator/division.go | 16 +++++++++++----- pdf/creator/drawable.go | 3 +++ pdf/creator/paragraph.go | 6 ++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pdf/creator/division.go b/pdf/creator/division.go index e32f5ad3..4f21f4dc 100644 --- a/pdf/creator/division.go +++ b/pdf/creator/division.go @@ -107,13 +107,16 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, ctx.Height -= div.margins.top } + // Set the inline mode of the division to the context + ctx.Inline = div.inline + + // Draw. divCtx := ctx tmpCtx := ctx var lineHeight float64 - // Draw. for _, component := range div.components { - if div.inline { + if ctx.Inline { if (ctx.X-divCtx.X)+component.Width() <= ctx.Width { ctx.Y = tmpCtx.Y ctx.Height = tmpCtx.Height @@ -147,9 +150,7 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, } // Apply padding/margins. - if !div.inline { - updCtx.X = ctx.X - } else { + if ctx.Inline { if dl := ctx.Height - updCtx.Height; dl > lineHeight { lineHeight = dl } @@ -159,11 +160,16 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, tmpCtx.Height = divCtx.Height lineHeight = 0 } + } else { + updCtx.X = ctx.X } ctx = updCtx } + // Restore the original inline mode of the context + ctx.Inline = origCtx.Inline + if div.positioning.isRelative() { // Move back X to same start of line. ctx.X = origCtx.X diff --git a/pdf/creator/drawable.go b/pdf/creator/drawable.go index c556088e..c85031bf 100644 --- a/pdf/creator/drawable.go +++ b/pdf/creator/drawable.go @@ -43,4 +43,7 @@ type DrawContext struct { // Absolute Page size, widths and height. PageWidth float64 PageHeight float64 + + // Controls whether the components are stacked horizontally + Inline bool } diff --git a/pdf/creator/paragraph.go b/pdf/creator/paragraph.go index c3770bd0..63b59a5d 100644 --- a/pdf/creator/paragraph.go +++ b/pdf/creator/paragraph.go @@ -516,10 +516,12 @@ func drawParagraphOnBlock(blk *Block, p *Paragraph, ctx DrawContext) (DrawContex if p.positioning.isRelative() { pHeight := p.Height() + p.margins.bottom - - ctx.X += p.Width() + p.margins.right ctx.Y += pHeight ctx.Height -= pHeight + + if ctx.Inline { + ctx.X += p.Width() + p.margins.right + } } return ctx, nil