]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/images: Retain newlines with text overlays
authorJoe Mooring <joe.mooring@veriphor.com>
Wed, 6 Mar 2024 16:15:39 +0000 (08:15 -0800)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 7 Mar 2024 08:52:30 +0000 (09:52 +0100)
Closes #12206

resources/images/text.go

index cc67a5d1d46d5fcd412b754da48494847bfa3057..2d3370c6195ced44c675c37578a870ecbe40d500 100644 (file)
@@ -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)
        }
 }