From db93b6e873218ca93f7fde07ccb6969e97609a27 Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Mon, 8 Oct 2018 17:34:57 +0300 Subject: [PATCH] Add getMaxLineWidth method on StyledParagraph --- pdf/creator/styled_paragraph.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pdf/creator/styled_paragraph.go b/pdf/creator/styled_paragraph.go index c305019b..d22bfbae 100644 --- a/pdf/creator/styled_paragraph.go +++ b/pdf/creator/styled_paragraph.go @@ -264,10 +264,10 @@ func (p *StyledParagraph) getTextLineWidth(line []TextChunk) float64 { for _, chunk := range line { style := &chunk.Style - for _, rune := range chunk.Text { - glyph, found := p.encoder.RuneToGlyph(rune) + for _, r := range chunk.Text { + glyph, found := p.encoder.RuneToGlyph(r) if !found { - common.Log.Debug("Error! Glyph not found for rune: %s\n", rune) + common.Log.Debug("Error! Glyph not found for rune: %s\n", r) // XXX/FIXME: return error. return -1 @@ -293,6 +293,23 @@ func (p *StyledParagraph) getTextLineWidth(line []TextChunk) float64 { return width } +// getMaxLineWidth returns the width of the longest line of text in the paragraph. +func (p *StyledParagraph) getMaxLineWidth() float64 { + if p.lines == nil || len(p.lines) == 0 { + p.wrapText() + } + + var width float64 + for _, line := range p.lines { + w := p.getTextLineWidth(line) + if w > width { + width = w + } + } + + 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