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

Retain newlines when overlaying text on images #12206

Closed
jmooring opened this issue Mar 6, 2024 · 1 comment · Fixed by #12209
Closed

Retain newlines when overlaying text on images #12206

jmooring opened this issue Mar 6, 2024 · 1 comment · Fixed by #12209

Comments

@jmooring
Copy link
Member

jmooring commented Mar 6, 2024

Reference: https://discourse.gohugo.io/t/render-markdown-with-newlines-in-shortcode/48684

This simple approach to word wrapping eats newlines:

// Draw text and break line at max width
parts := strings.Fields(f.text)
for _, str := range parts {
strWith := font.MeasureString(face, str)
if (d.Dot.X.Ceil() + strWith.Ceil()) >= maxWidth {
y = y + fontHeight + f.linespacing
d.Dot = fixed.P(f.x, y)
}
d.DrawString(str + " ")
}

Suggest something like this instead:

// Draw text line by line, breaking each line at the maximum width.
f.text = strings.ReplaceAll(f.text, "\r", "")
for _, line := range strings.Split(f.text, "\n") {
	for _, str := range strings.Fields(line) {
		strWidth := font.MeasureString(face, str)
		if (d.Dot.X.Ceil() + strWidth.Ceil()) >= maxWidth {
			y = y + fontHeight + f.linespacing
			d.Dot = fixed.P(f.x, y)
		}
		d.DrawString(str + " ")
	}
	y = y + fontHeight + f.linespacing
	d.Dot = fixed.P(f.x, y)
}

This would allow you to overlay this:

{{ $text := "This is a list:\n\n1. Item one\n2. Item two" }}

With this result:

good

Instead of this:

bad

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants