Center-align text that takes only one line

This commit is contained in:
Matt Baer 2021-01-19 11:45:11 -05:00
parent 27bb32c4c5
commit 2d916df166

View File

@ -100,17 +100,24 @@ func GenerateImage(opt *ContentOptions, outputFilename string) error {
} }
s := opt.Content s := opt.Content
lines := dc.WordWrap(s, contentWidth) lines := dc.WordWrap(s, contentWidth)
// Center-align text by default for short content
alignment := gg.AlignCenter
linesStr := "" linesStr := ""
for i, str := range lines { for i, str := range lines {
linesStr += str linesStr += str
if i != len(lines)-1 { if i != len(lines)-1 {
linesStr += "\n" linesStr += "\n"
if alignment == gg.AlignCenter {
// Since content uses multiple lines, left-align it.
alignment = gg.AlignLeft
}
} }
} }
_, contentTextHeight := dc.MeasureMultilineString(linesStr, lineSpacing) _, contentTextHeight := dc.MeasureMultilineString(linesStr, lineSpacing)
x = contentRightMargin x = contentRightMargin
y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2 y = contentTopMargin - contentBottomMargin + hf/2 - contentTextHeight/2
dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, gg.AlignLeft) dc.DrawStringWrapped(s, x, y, 0, 0, contentWidth, lineSpacing, alignment)
err = dc.SavePNG(outputFilename) err = dc.SavePNG(outputFilename)
if err != nil { if err != nil {