Add inline member on the DrawContext structure

This commit is contained in:
Adrian-George Bostan 2018-09-17 22:11:25 +03:00
parent ba5b493b70
commit 7004fb0d0e
3 changed files with 18 additions and 7 deletions

View File

@ -107,13 +107,16 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
ctx.Height -= div.margins.top ctx.Height -= div.margins.top
} }
// Set the inline mode of the division to the context
ctx.Inline = div.inline
// Draw.
divCtx := ctx divCtx := ctx
tmpCtx := ctx tmpCtx := ctx
var lineHeight float64 var lineHeight float64
// Draw.
for _, component := range div.components { for _, component := range div.components {
if div.inline { if ctx.Inline {
if (ctx.X-divCtx.X)+component.Width() <= ctx.Width { if (ctx.X-divCtx.X)+component.Width() <= ctx.Width {
ctx.Y = tmpCtx.Y ctx.Y = tmpCtx.Y
ctx.Height = tmpCtx.Height ctx.Height = tmpCtx.Height
@ -147,9 +150,7 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
} }
// Apply padding/margins. // Apply padding/margins.
if !div.inline { if ctx.Inline {
updCtx.X = ctx.X
} else {
if dl := ctx.Height - updCtx.Height; dl > lineHeight { if dl := ctx.Height - updCtx.Height; dl > lineHeight {
lineHeight = dl lineHeight = dl
} }
@ -159,11 +160,16 @@ func (div *Division) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
tmpCtx.Height = divCtx.Height tmpCtx.Height = divCtx.Height
lineHeight = 0 lineHeight = 0
} }
} else {
updCtx.X = ctx.X
} }
ctx = updCtx ctx = updCtx
} }
// Restore the original inline mode of the context
ctx.Inline = origCtx.Inline
if div.positioning.isRelative() { if div.positioning.isRelative() {
// Move back X to same start of line. // Move back X to same start of line.
ctx.X = origCtx.X ctx.X = origCtx.X

View File

@ -43,4 +43,7 @@ type DrawContext struct {
// Absolute Page size, widths and height. // Absolute Page size, widths and height.
PageWidth float64 PageWidth float64
PageHeight float64 PageHeight float64
// Controls whether the components are stacked horizontally
Inline bool
} }

View File

@ -516,10 +516,12 @@ func drawParagraphOnBlock(blk *Block, p *Paragraph, ctx DrawContext) (DrawContex
if p.positioning.isRelative() { if p.positioning.isRelative() {
pHeight := p.Height() + p.margins.bottom pHeight := p.Height() + p.margins.bottom
ctx.X += p.Width() + p.margins.right
ctx.Y += pHeight ctx.Y += pHeight
ctx.Height -= pHeight ctx.Height -= pHeight
if ctx.Inline {
ctx.X += p.Width() + p.margins.right
}
} }
return ctx, nil return ctx, nil