Fix StyledParagraph wrapping issue when used inside table cell

This commit is contained in:
Adrian-George Bostan 2018-09-25 21:15:20 +03:00
parent 9ffcdf7698
commit 3885bb9e20
2 changed files with 26 additions and 0 deletions

View File

@ -238,6 +238,19 @@ func (p *StyledParagraph) getTextWidth() float64 {
return width
}
// getTextHeight calculates the text height as if all in one line (not taking wrapping into account).
func (p *StyledParagraph) getTextHeight() float64 {
var height float64
for _, chunk := range p.chunks {
h := chunk.Style.FontSize * p.lineHeight
if h > height {
height = h
}
}
return height
}
// wrapText splits text into lines. It uses a simple greedy algorithm to wrap
// fill the lines.
// XXX/TODO: Consider the Knuth/Plass algorithm or an alternative.

View File

@ -210,6 +210,19 @@ func (table *Table) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
// Add diff to last row.
table.rowHeights[cell.row+cell.rowspan-2] += diffh
}
case *StyledParagraph:
sp := t
if sp.enableWrap {
sp.SetWidth(w - cell.indent)
}
newh := sp.Height() + sp.margins.bottom + sp.margins.bottom
newh += 0.5 * sp.getTextHeight() // TODO: Make the top margin configurable?
if newh > h {
diffh := newh - h
// Add diff to last row.
table.rowHeights[cell.row+cell.rowspan-2] += diffh
}
case *Image:
img := t
newh := img.Height() + img.margins.top + img.margins.bottom