From 2d916df16659865598f075f750428d151138744f Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 19 Jan 2021 11:45:11 -0500 Subject: [PATCH] Center-align text that takes only one line --- textpic.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/textpic.go b/textpic.go index 128660c..88ecb75 100644 --- a/textpic.go +++ b/textpic.go @@ -100,17 +100,24 @@ func GenerateImage(opt *ContentOptions, outputFilename string) error { } s := opt.Content lines := dc.WordWrap(s, contentWidth) + + // Center-align text by default for short content + alignment := gg.AlignCenter linesStr := "" for i, str := range lines { linesStr += str if i != len(lines)-1 { linesStr += "\n" + if alignment == gg.AlignCenter { + // Since content uses multiple lines, left-align it. + alignment = gg.AlignLeft + } } } _, contentTextHeight := dc.MeasureMultilineString(linesStr, lineSpacing) x = contentRightMargin 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) if err != nil {