prose/common/common.go
Naman Sood 1981a291aa
All checks were successful
/ Deploy blog to deuterium (push) Successful in 1m9s
rewrite code a bit
Signed-off-by: Naman Sood <mail@nsood.in>
2024-11-23 02:53:25 -05:00

77 lines
2 KiB
Go

package common
import (
"io"
"time"
"github.com/fogleman/gg"
)
const (
BlogTitle = "Prose"
BlogURL = "https://prose.nsood.in"
BlogSummary = "Where I infodump in Markdown and nobody can stop me."
)
func RSSDatetime(timestamp int64) string {
return time.Unix(timestamp, 0).Format("Mon, 02 Jan 2006 15:04:05 MST")
}
func CreateImage(title, summary, url string, out io.Writer) error {
imgWidth, imgHeight, imgPaddingX, imgPaddingY := 1200, 600, 50, 100
accentHeight, spacerHeight := 12.5, 20.0
titleSize, summarySize, urlSize := 63.0, 42.0, 27.0
lineHeight := 1.05
textWidth := float64(imgWidth - 2*imgPaddingX)
draw := gg.NewContext(imgWidth, imgHeight)
titleFont, err := gg.LoadFontFace("static/fonts/Nunito-Bold.ttf", titleSize)
if err != nil {
return err
}
summaryFont, err := gg.LoadFontFace("static/fonts/Nunito-LightItalic.ttf", summarySize)
if err != nil {
return err
}
urlFont, err := gg.LoadFontFace("static/fonts/JetBrainsMono-ExtraLight.ttf", urlSize)
if err != nil {
return err
}
draw.SetFontFace(titleFont)
wrappedTitle := draw.WordWrap(title, textWidth)
draw.SetFontFace(summaryFont)
wrappedSummary := draw.WordWrap(summary, textWidth)
draw.SetHexColor("#fff")
draw.DrawRectangle(0, 0, float64(imgWidth), float64(imgHeight))
draw.Fill()
draw.SetHexColor("#3498db")
draw.DrawRectangle(0, float64(imgHeight)-accentHeight, float64(imgWidth), accentHeight)
draw.Fill()
offset := float64(imgPaddingY)
draw.SetFontFace(titleFont)
draw.SetHexColor("#333")
for _, line := range wrappedTitle {
draw.DrawString(line, float64(imgPaddingX), offset)
offset += lineHeight * titleSize
}
offset += spacerHeight
draw.SetFontFace(summaryFont)
draw.SetHexColor("#999")
for _, line := range wrappedSummary {
draw.DrawString(line, float64(imgPaddingX), offset)
offset += lineHeight * summarySize
}
draw.SetHexColor("#333")
draw.SetFontFace(urlFont)
urlY := float64(imgHeight - imgPaddingY)
draw.DrawStringWrapped(url, float64(imgPaddingX), urlY, 0, 0, textWidth, lineHeight, gg.AlignRight)
return draw.EncodePNG(out)
}