Skip to content

images.Text: Add option for vertical alignment #13415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content/en/functions/images/Text.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ alignx
{{< new-in 0.141.0 />}}
: (`string`) The horizontal alignment of the text relative to the horizontal offset, one of `left`, `center`, or `right`. Default is `left`.

aligny
: (`string`) The vertical alignment of the text relative to the vertical offset, one of `top`, `center`, or `bottom`. Default is `top`.

color
: (`string`) The font color, either a 3-digit or 6-digit hexadecimal color code. Default is `#ffffff` (white).

Expand Down
6 changes: 6 additions & 0 deletions resources/images/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (*Filters) Text(text string, options ...any) gift.Filter {
x: 10,
y: 10,
alignx: "left",
aligny: "top",
linespacing: 2,
}

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

case "linespacing":
tf.linespacing = cast.ToInt(v)
Expand Down
16 changes: 12 additions & 4 deletions resources/images/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type textFilter struct {
color color.Color
x, y int
alignx string
aligny string
size float64
linespacing int
fontSource hugio.ReadSeekCloserProvider
Expand Down Expand Up @@ -110,12 +111,19 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
}
finalLines = append(finalLines, currentLine)
}
// Total height of the text from the top of the first line to the baseline of the last line
totalHeight := len(finalLines)*fontHeight + (len(finalLines)-1)*f.linespacing

// Correct y position based on font and size
f.y = f.y + fontHeight

// Start position
y := f.y
y := f.y + fontHeight
switch f.aligny {
case "top":
// Do nothing
case "center":
y = y - totalHeight/2
case "bottom":
y = y - totalHeight
}

// Draw text line by line
for _, line := range finalLines {
Expand Down
Loading