feat: expose vertical/horizontal alignment and max font size to metadata yamls

pull/4/head
Wojciech Kwolek 2024-03-15 01:17:20 +01:00
parent 8b012a2bbc
commit f5902126a3
Signed by: irth
SSH Key Fingerprint: SHA256:FAXb3OrtGoWgBfLFtqFOSVxGU8zuBPvG47dyUHA39Z4
2 changed files with 56 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package meme
import (
"fmt"
"image"
"image/color"
"image/draw"
@ -20,6 +21,19 @@ const (
Right HorizontalAlignment = 1
)
func ParseHorizontalAlignment(s string) (HorizontalAlignment, error) {
switch s {
case "left":
return Left, nil
case "center":
return Center, nil
case "right":
return Right, nil
default:
return 0, fmt.Errorf("invalid horizontal alignment: %s", s)
}
}
type VerticalAlignment int
const (
@ -28,6 +42,19 @@ const (
Bottom VerticalAlignment = 1
)
func ParseVerticalAlignment(s string) (VerticalAlignment, error) {
switch s {
case "top":
return Top, nil
case "middle":
return Middle, nil
case "bottom":
return Bottom, nil
default:
return 0, fmt.Errorf("invalid vertical alignment: %s", s)
}
}
type TextSlot struct {
Bounds image.Rectangle
Font *truetype.Font

View File

@ -20,12 +20,15 @@ type Pattern struct {
}
type Slot struct {
X int
Y int
Width int
Height int
Font string
TextColor []int `yaml:"text_color"`
X int
Y int
Width int
Height int
Font string
TextColor []int `yaml:"text_color"`
HorizontalAlignment string `yaml:"halign"`
VerticalAlignment string `yaml:"valign"`
MaxFontSize float64 `yaml:"max_font_size"`
}
type Metadata struct {
@ -76,6 +79,26 @@ func (m *Metadata) TextSlots(bounds image.Rectangle) (slots []*meme.TextSlot) {
if c := sliceToColor(slot.TextColor); c != nil {
textSlot.TextColor = c
}
var err error
if slot.HorizontalAlignment != "" {
textSlot.HorizontalAlignment, err = meme.ParseHorizontalAlignment(slot.HorizontalAlignment)
if err != nil {
panic(err)
}
}
if slot.VerticalAlignment != "" {
textSlot.VerticalAlignment, err = meme.ParseVerticalAlignment(slot.VerticalAlignment)
if err != nil {
panic(err)
}
}
if slot.MaxFontSize != 0 {
textSlot.MaxFontSize = slot.MaxFontSize
}
slots = append(slots, textSlot)
}
return