summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPranshu Gaba <pranshugaba@gmail.com>2025-02-18 05:21:42 +0530
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2025-04-24 14:09:13 +0200
commit2fce0bac033d8b7e98046f85f669ba813d862788 (patch)
tree5463da788d59b654d58397684e9461d307400765
parent179aea11ac2ce80a38b211e11fd513cead63b17e (diff)
downloadhugo-2fce0bac033d8b7e98046f85f669ba813d862788.tar.gz
hugo-2fce0bac033d8b7e98046f85f669ba813d862788.zip
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
-rw-r--r--docs/content/en/functions/images/Text.md3
-rw-r--r--resources/images/filters.go6
-rw-r--r--resources/images/text.go16
3 files changed, 21 insertions, 4 deletions
diff --git a/docs/content/en/functions/images/Text.md b/docs/content/en/functions/images/Text.md
index 94cdb4e9d..8f7e730ba 100644
--- a/docs/content/en/functions/images/Text.md
+++ b/docs/content/en/functions/images/Text.md
@@ -18,6 +18,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).
diff --git a/resources/images/filters.go b/resources/images/filters.go
index 9c2b9b46f..1e44f1184 100644
--- a/resources/images/filters.go
+++ b/resources/images/filters.go
@@ -79,6 +79,7 @@ func (*Filters) Text(text string, options ...any) gift.Filter {
x: 10,
y: 10,
alignx: "left",
+ aligny: "top",
linespacing: 2,
}
@@ -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)
diff --git a/resources/images/text.go b/resources/images/text.go
index 324878839..f3943a475 100644
--- a/resources/images/text.go
+++ b/resources/images/text.go
@@ -36,6 +36,7 @@ type textFilter struct {
color color.Color
x, y int
alignx string
+ aligny string
size float64
linespacing int
fontSource hugio.ReadSeekCloserProvider
@@ -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 {