Skip to content

Commit 2fce0ba

Browse files
pranshugababep
authored andcommitted
images: Add option for vertical alignment to images.Text
Add option ``aligny`` to specify the vertical alignment of the text with respect to the ``y`` offset from the top of the image. Possible values of ``aligny`` are ``top`` (default), ``center``, and ``bottom``. The height of the block of text is measured from the top of the first line to the baseline of the last line. - ``top``: (Current behaviour) The top of the first line of the block of text is at an offset of ``y`` from the top of the image. - ``center``: The vertical center of the block of text is at an offset of ``y`` from the top of the image. - ``bottom``: The baseline of the last line of the text is at an offset of ``y`` from the top of the image. Resolves #13414
1 parent 179aea1 commit 2fce0ba

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

‎docs/content/en/functions/images/Text.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ alignx
1818
: {{< new-in 0.141.0 />}}
1919
: (`string`) The horizontal alignment of the text relative to the horizontal offset, one of `left`, `center`, or `right`. Default is `left`.
2020

21+
aligny
22+
: (`string`) The vertical alignment of the text relative to the vertical offset, one of `top`, `center`, or `bottom`. Default is `top`.
23+
2124
color
2225
: (`string`) The font color, either a 3-digit or 6-digit hexadecimal color code. Default is `#ffffff` (white).
2326

‎resources/images/filters.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (*Filters) Text(text string, options ...any) gift.Filter {
7979
x: 10,
8080
y: 10,
8181
alignx: "left",
82+
aligny: "top",
8283
linespacing: 2,
8384
}
8485

@@ -102,6 +103,11 @@ func (*Filters) Text(text string, options ...any) gift.Filter {
102103
if tf.alignx != "left" && tf.alignx != "center" && tf.alignx != "right" {
103104
panic("alignx must be one of left, center, right")
104105
}
106+
case "aligny":
107+
tf.aligny = cast.ToString(v)
108+
if tf.aligny != "top" && tf.aligny != "center" && tf.aligny != "bottom" {
109+
panic("aligny must be one of top, center, bottom")
110+
}
105111

106112
case "linespacing":
107113
tf.linespacing = cast.ToInt(v)

‎resources/images/text.go‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type textFilter struct {
3636
color color.Color
3737
x, y int
3838
alignx string
39+
aligny string
3940
size float64
4041
linespacing int
4142
fontSource hugio.ReadSeekCloserProvider
@@ -110,12 +111,19 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
110111
}
111112
finalLines = append(finalLines, currentLine)
112113
}
114+
// Total height of the text from the top of the first line to the baseline of the last line
115+
totalHeight := len(finalLines)*fontHeight + (len(finalLines)-1)*f.linespacing
113116

114117
// Correct y position based on font and size
115-
f.y = f.y + fontHeight
116-
117-
// Start position
118-
y := f.y
118+
y := f.y + fontHeight
119+
switch f.aligny {
120+
case "top":
121+
// Do nothing
122+
case "center":
123+
y = y - totalHeight/2
124+
case "bottom":
125+
y = y - totalHeight
126+
}
119127

120128
// Draw text line by line
121129
for _, line := range finalLines {

0 commit comments

Comments
 (0)