Skip to content
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

images.Text: Add option for horizontal alignment #13223

Merged
merged 1 commit into from
Jan 8, 2025

Conversation

jlskuz
Copy link
Contributor

@jlskuz jlskuz commented Jan 6, 2025

Please note that this is my first contribution to Hugo and also the first time I wrote something in Go. Please let me know if I missed anything and I will try my best to correct it.

Add an "alignx" option to the images.Text to control whether the value of the "x" option is the left border of the text (current behaviour), the center of each line or the right border.

Fixes #10849

Example with "alignx" "left":

grafik

Example with "alignx" "center":

grafik

Example with "alignx" "right":

grafik

@CLAassistant
Copy link

CLAassistant commented Jan 6, 2025

CLA assistant check
All committers have signed the CLA.

@bep bep requested a review from jmooring January 6, 2025 20:54
@jlskuz jlskuz force-pushed the feat/images-text-x-alignment branch from cc35544 to 0f351a8 Compare January 6, 2025 22:21
resources/images/filters.go Show resolved Hide resolved
resources/images/text.go Outdated Show resolved Hide resolved
resources/images/text.go Outdated Show resolved Hide resolved
resources/images/text.go Outdated Show resolved Hide resolved
@jlskuz jlskuz force-pushed the feat/images-text-x-alignment branch from 0f351a8 to 01c17b9 Compare January 7, 2025 21:20
@jlskuz
Copy link
Contributor Author

jlskuz commented Jan 7, 2025

@bep Thanks for the review all your comments should be addressed now.

@jlskuz jlskuz requested a review from bep January 7, 2025 21:21
Copy link
Member

@jmooring jmooring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This work fine, you just have to get the image before setting the options in order to calculate x offset...

{{ $fontPath := "https://github.com/google/fonts/raw/main/ofl/lato/Lato-Regular.ttf" }}
{{ $imagePath := "images/original.jpg" }}
{{ $text := "A kitten!" }}

{{ with try (resources.GetRemote $fontPath) }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else with .Value }}
    {{ $font := . }}
    {{ with resources.Get $imagePath }}
      {{ $opts := dict
        "color" "#fbfaf5"
        "font" $font
        "linespacing" 8
        "size" 120
        "x" (div .Width 2 | int)
        "y" 50
        "alignx" "center"
      }}
      {{ $filter := images.Text $text $opts }}
      {{ with . | images.Filter $filter }}
        <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
      {{ end }}
    {{ else }}
      {{ errorf "%s" "Unable to get resource %q" $imagePath }}
    {{ end }}
  {{ else }}
    {{ errorf "Unable to get resource %q" $fontPath }}
  {{ end }}
{{ end }}

@bep bep added this to the v0.141.0 milestone Jan 8, 2025
Add an "alignx" option to the images.Text to control whether the value of the "x" option is the left border of the text (current behaviour), the center of each line or the right border.

Fixes gohugoio#10849
@jlskuz
Copy link
Contributor Author

jlskuz commented Jan 8, 2025

Is there an easy way to re-trigger the tests myself? I fixed them.

Also I opened #13232 to add the ./check_gofmt.sh step to the development guide

@jlskuz jlskuz force-pushed the feat/images-text-x-alignment branch from 01c17b9 to 47ac7bd Compare January 8, 2025 10:43
@bep
Copy link
Member

bep commented Jan 8, 2025

Is there an easy way to re-trigger the tests myself? I fixed them.

No.

There is a restriction in GitHub for first time contributors of a repository, it requires someone to Approve each PR build (which I guess makes sense, else spam bots would go bananas creating new CI builds).

Once this is merged, any new PR or push to a PR branch from you will trigger a build without any ceremony.

@bep bep merged commit 4a5cc2d into gohugoio:master Jan 8, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for aligning text when adding to images using images.Text
4 participants