diff options
author | Joe Mooring <[email protected]> | 2024-03-06 08:15:39 -0800 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-03-07 09:52:30 +0100 |
commit | 05e23bd55c67f636c628a105ca45e19408740ec6 (patch) | |
tree | c37c9ec15fbc5f701fbb0f255efe5e1f383fbb5a /resources | |
parent | 7afac3f1adcdbaa7e93a7062dbd687ec5e58df9c (diff) | |
download | hugo-05e23bd55c67f636c628a105ca45e19408740ec6.tar.gz hugo-05e23bd55c67f636c628a105ca45e19408740ec6.zip |
resources/images: Retain newlines with text overlays
Closes #12206
Diffstat (limited to 'resources')
-rw-r--r-- | resources/images/text.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/resources/images/text.go b/resources/images/text.go index cc67a5d1d..2d3370c61 100644 --- a/resources/images/text.go +++ b/resources/images/text.go @@ -91,15 +91,19 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) y := f.y d.Dot = fixed.P(f.x, f.y) - // Draw text and break line at max width - parts := strings.Fields(f.text) - for _, str := range parts { - strWith := font.MeasureString(face, str) - if (d.Dot.X.Ceil() + strWith.Ceil()) >= maxWidth { - y = y + fontHeight + f.linespacing - d.Dot = fixed.P(f.x, y) + // Draw text line by line, breaking each line at the maximum width. + f.text = strings.ReplaceAll(f.text, "\r", "") + for _, line := range strings.Split(f.text, "\n") { + for _, str := range strings.Fields(line) { + strWidth := font.MeasureString(face, str) + if (d.Dot.X.Ceil() + strWidth.Ceil()) >= maxWidth { + y = y + fontHeight + f.linespacing + d.Dot = fixed.P(f.x, y) + } + d.DrawString(str + " ") } - d.DrawString(str + " ") + y = y + fontHeight + f.linespacing + d.Dot = fixed.P(f.x, y) } } |