meme: font size selection and top text

This commit is contained in:
Chris Sexton 2020-04-29 11:58:14 -04:00 committed by Chris Sexton
parent bcb5d43ea6
commit 49991e530a
1 changed files with 27 additions and 9 deletions

View File

@ -86,11 +86,18 @@ func (p *MemePlugin) registerWeb(c bot.Connector) {
parts := strings.SplitN(r.PostForm.Get("text"), " ", 2) parts := strings.SplitN(r.PostForm.Get("text"), " ", 2)
isCmd, message := bot.IsCmd(p.c, parts[1]) isCmd, message := bot.IsCmd(p.c, parts[1])
format := parts[0]
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text")) log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text"))
w.WriteHeader(200) w.WriteHeader(200)
id := p.genMeme(parts[0], message) top, bottom := "", message
parts = strings.Split(message, "\n")
if len(parts) > 1 {
top, bottom = parts[0], parts[1]
}
id := p.genMeme(format, top, bottom)
baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`) baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`)
u, _ := url.Parse(baseURL) u, _ := url.Parse(baseURL)
u.Path = path.Join(u.Path, "meme", "img", id) u.Path = path.Join(u.Path, "meme", "img", id)
@ -153,8 +160,9 @@ var defaultFormats = map[string]string{
"raptor": "Philosoraptor.jpg", "raptor": "Philosoraptor.jpg",
} }
func (p *MemePlugin) genMeme(meme, text string) string { func (p *MemePlugin) genMeme(meme, top, bottom string) string {
const fontSize = 36 fontSizes := []float64{96, 48, 24, 12}
fontSize := fontSizes[0]
formats := p.c.GetMap("meme.memes", defaultFormats) formats := p.c.GetMap("meme.memes", defaultFormats)
@ -181,10 +189,18 @@ func (p *MemePlugin) genMeme(meme, text string) string {
m := gg.NewContext(w, h) m := gg.NewContext(w, h)
m.DrawImage(img, 0, 0) m.DrawImage(img, 0, 0)
fontLocation := p.c.Get("meme.font", "impact.ttf") fontLocation := p.c.Get("meme.font", "impact.ttf")
err = m.LoadFontFace(fontLocation, fontSize) // problem for _, sz := range fontSizes {
err = m.LoadFontFace(fontLocation, sz) // problem
if err != nil { if err != nil {
log.Error().Err(err).Msg("could not load font") log.Error().Err(err).Msg("could not load font")
} }
topW, _ := m.MeasureString(top)
botW, _ := m.MeasureString(bottom)
if topW < float64(w) && botW < float64(w) {
fontSize = sz
break
}
}
// Apply black stroke // Apply black stroke
m.SetHexColor("#000") m.SetHexColor("#000")
@ -196,14 +212,16 @@ func (p *MemePlugin) genMeme(meme, text string) string {
continue continue
} }
x := float64(w/2 + dx) x := float64(w/2 + dx)
y := float64(h - fontSize + dy) y := float64(h) - fontSize + float64(dy)
m.DrawStringAnchored(text, x, y, 0.5, 0.5) m.DrawStringAnchored(top, x, fontSize, 0.5, 0.5)
m.DrawStringAnchored(bottom, x, y, 0.5, 0.5)
} }
} }
// Apply white fill // Apply white fill
m.SetHexColor("#FFF") m.SetHexColor("#FFF")
m.DrawStringAnchored(text, float64(w)/2, float64(h)-fontSize, 0.5, 0.5) m.DrawStringAnchored(top, float64(w)/2, fontSize, 0.5, 0.5)
m.DrawStringAnchored(bottom, float64(w)/2, float64(h)-fontSize, 0.5, 0.5)
i := bytes.Buffer{} i := bytes.Buffer{}
png.Encode(&i, m.Image()) png.Encode(&i, m.Image())