-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
138 lines (114 loc) · 2.91 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package gomeme
import (
"image"
"image/color"
"io/ioutil"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/golang/freetype/truetype"
"github.com/jpoz/dilation"
)
// DefaultFontSize is the default size of the font
const DefaultFontSize = 42
// DefaultMargin is the default distance between the text and the top
// and bottem
const DefaultMargin = 10
// DefaultDPI is the fonts DPI
const DefaultDPI = 42
// DefaultStrokeSize default width of the stroke
const DefaultStrokeSize = 4
// Config hold all the configuration needed to make a new meme.
// It Also produces the text image to be overlayed
type Config struct {
FontPath string
FontSize float64
FontDPI float64
FontColor image.Image
FontStrokeSize int
FontStrokeColor color.Color
Margin int
TopText string
BottomText string
}
// NewConfig builds a default configuration.
func NewConfig() *Config {
meme := &Config{
FontColor: image.White,
FontDPI: DefaultDPI,
FontSize: DefaultFontSize,
FontStrokeColor: color.Black,
FontStrokeSize: DefaultStrokeSize,
Margin: DefaultMargin,
}
return meme
}
func (c *Config) loadFont() (*truetype.Font, error) {
var err error
var fontData []byte
if c.FontPath == "" {
fontData, err = Asset("inpact.ttf")
if err != nil {
return nil, err
}
} else {
fontData, err = ioutil.ReadFile(c.FontPath)
if err != nil {
return nil, err
}
}
return truetype.Parse(fontData)
}
// TextImage produces a image of the bottom and top text
func (c *Config) TextImage(bounds image.Rectangle) (*image.RGBA, error) {
fnt, err := c.loadFont()
if err != nil {
return nil, err
}
textImage := image.NewRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
f := truetype.NewFace(fnt, &truetype.Options{
Size: c.FontSize,
DPI: c.FontDPI,
Hinting: font.HintingNone,
})
d := &font.Drawer{
Dst: textImage,
Src: c.FontColor,
Face: f,
}
// Not sure if these are the best metrics for the margin calculations
metrics := f.Metrics()
ascent := metrics.Ascent.Ceil()
descent := metrics.Descent.Ceil()
// Maybe height should be used?
//height := metrics.Height.Ceil()
if c.TopText != "" {
// Compute the top text position
y := c.Margin + ascent
x := (fixed.I(bounds.Dx()) - d.MeasureString(c.TopText)) / 2
topDot := fixed.Point26_6{
X: x,
Y: fixed.I(y),
}
// Draw the top text
d.Dot = topDot
d.DrawString(c.TopText)
}
if c.BottomText != "" {
// Compute the bottom text position
y := bounds.Dy() - c.Margin - descent
x := (fixed.I(bounds.Dx()) - d.MeasureString(c.BottomText)) / 2
bottomDot := fixed.Point26_6{
X: x,
Y: fixed.I(y),
}
// Draw the bottom text
d.Dot = bottomDot
d.DrawString(c.BottomText)
}
// Dialate aka give text a stroke
dilation.Dialate(textImage, dilation.DialateConfig{
Stroke: c.FontStrokeSize,
StrokeColor: c.FontStrokeColor,
})
return textImage, nil
}