Skip to content

Commit 83cb6f4

Browse files
committed
Add dynamic font resizing for <140 chars notes
1 parent f748b99 commit 83cb6f4

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

render_image.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d
102102
img.SetColor(FOREGROUND)
103103

104104
// main content text
105-
textImg := drawText(paragraphs, width-paddingLeft*2, height-20)
105+
textImg := drawText(paragraphs, width-paddingLeft*2, height-20, true)
106106
img.DrawImage(textImg, paddingLeft, 20)
107107

108108
// font for writing the bottom bar stuff
@@ -144,7 +144,7 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d
144144
authorTextY := height - barHeight + 15
145145
authorMaxWidth := width/2.0 - paddingLeft*2 - barExtraPadding
146146
img.SetColor(color.White)
147-
textImg = drawText([]string{metadata.ShortName()}, width, barHeight)
147+
textImg = drawText([]string{metadata.ShortName()}, width, barHeight, false)
148148
img.DrawImage(textImg, authorTextX, authorTextY)
149149

150150
// a gradient to cover too long names
@@ -177,11 +177,36 @@ func drawImage(paragraphs []string, style Style, metadata sdk.ProfileMetadata, d
177177
return img.Image(), nil
178178
}
179179

180-
func drawText(paragraphs []string, width, height int) image.Image {
181-
const FONT_SIZE = 25
180+
func drawText(paragraphs []string, width, height int, dynamicResize bool) image.Image {
181+
FONT_SIZE := 25
182182
color := color.RGBA{R: 255, G: 230, B: 238, A: 255}
183183
img := image.NewNRGBA(image.Rect(0, 0, width, height))
184184

185+
joinedContent := strings.Join(paragraphs, "\n")
186+
if dynamicResize && len(joinedContent) < 141 {
187+
FONT_SIZE = 7
188+
img := gg.NewContext(width, height)
189+
fontData, _ := fonts.ReadFile("fonts/NotoSans.ttf")
190+
ttf, _ := truetype.Parse(fontData)
191+
i := 1
192+
lineSpacing := 1.2
193+
for i < 20 {
194+
FONT_SIZE += i
195+
img.SetFontFace(truetype.NewFace(ttf, &truetype.Options{
196+
Size: float64(FONT_SIZE),
197+
DPI: 260,
198+
}))
199+
wrappedContent := strings.Join(img.WordWrap(joinedContent, float64(width-120)), "\n")
200+
_, checkHeight := img.MeasureMultilineString(wrappedContent, lineSpacing)
201+
if checkHeight > float64(height-70-60*2) {
202+
FONT_SIZE -= 1
203+
break
204+
}
205+
i += 1
206+
}
207+
FONT_SIZE = FONT_SIZE*4 - 2
208+
}
209+
185210
lineNumber := 1
186211
for _, paragraph := range paragraphs {
187212
rawText := []rune(paragraph)

0 commit comments

Comments
 (0)