From 3885bb9e20858cb15af1eea63e9fbb69f1c55c7f Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Tue, 25 Sep 2018 21:15:20 +0300 Subject: [PATCH] Fix StyledParagraph wrapping issue when used inside table cell --- pdf/creator/styled_paragraph.go | 13 +++++++++++++ pdf/creator/table.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pdf/creator/styled_paragraph.go b/pdf/creator/styled_paragraph.go index 4008911a..e9cebbab 100644 --- a/pdf/creator/styled_paragraph.go +++ b/pdf/creator/styled_paragraph.go @@ -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. diff --git a/pdf/creator/table.go b/pdf/creator/table.go index 0de7028b..38e6ccec 100644 --- a/pdf/creator/table.go +++ b/pdf/creator/table.go @@ -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