feat: expose vertical/horizontal alignment and max font size to metadata yamls
parent
8b012a2bbc
commit
f5902126a3
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue