@@ -35,6 +35,7 @@ type textFilter struct {
3535 text string
3636 color color.Color
3737 x , y int
38+ alignx string
3839 size float64
3940 linespacing int
4041 fontSource hugio.ReadSeekCloserProvider
@@ -77,30 +78,62 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
7778
7879 gift .New ().Draw (dst , src )
7980
80- // Draw text, consider and include linebreaks
8181 maxWidth := dst .Bounds ().Dx () - 20
82+
83+ var availableWidth int
84+ switch f .alignx {
85+ case "right" :
86+ availableWidth = f .x
87+ case "center" :
88+ availableWidth = min ((maxWidth - f .x ), f .x ) * 2
89+ case "left" :
90+ availableWidth = maxWidth - f .x
91+ }
92+
8293 fontHeight := face .Metrics ().Ascent .Ceil ()
8394
95+ // Calculate lines, consider and include linebreaks
96+ finalLines := []string {}
97+ f .text = strings .ReplaceAll (f .text , "\r " , "" )
98+ for _ , line := range strings .Split (f .text , "\n " ) {
99+ currentLine := ""
100+ // Break each line at the maximum width.
101+ for _ , str := range strings .Fields (line ) {
102+ fieldStrWidth := font .MeasureString (face , str )
103+ currentLineStrWidth := font .MeasureString (face , currentLine )
104+
105+ if (currentLineStrWidth .Ceil () + fieldStrWidth .Ceil ()) >= availableWidth {
106+ finalLines = append (finalLines , currentLine )
107+ currentLine = ""
108+ }
109+ currentLine += str + " "
110+ }
111+ finalLines = append (finalLines , currentLine )
112+ }
113+
84114 // Correct y position based on font and size
85115 f .y = f .y + fontHeight
86116
87117 // Start position
88118 y := f .y
89- d .Dot = fixed .P (f .x , f .y )
90119
91- // Draw text line by line, breaking each line at the maximum width.
92- f .text = strings .ReplaceAll (f .text , "\r " , "" )
93- for _ , line := range strings .Split (f .text , "\n " ) {
94- for _ , str := range strings .Fields (line ) {
95- strWidth := font .MeasureString (face , str )
96- if (d .Dot .X .Ceil () + strWidth .Ceil ()) >= maxWidth {
97- y = y + fontHeight + f .linespacing
98- d .Dot = fixed .P (f .x , y )
99- }
100- d .DrawString (str + " " )
120+ // Draw text line by line
121+ for _ , line := range finalLines {
122+ line = strings .TrimSpace (line )
123+ strWidth := font .MeasureString (face , line )
124+ var x int
125+ switch f .alignx {
126+ case "right" :
127+ x = f .x - strWidth .Ceil ()
128+ case "center" :
129+ x = f .x - (strWidth .Ceil () / 2 )
130+
131+ case "left" :
132+ x = f .x
101133 }
134+ d .Dot = fixed .P (x , y )
135+ d .DrawString (line )
102136 y = y + fontHeight + f .linespacing
103- d .Dot = fixed .P (f .x , y )
104137 }
105138}
106139
0 commit comments