-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
228 lines (178 loc) · 5.74 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"flag"
"fmt"
"image"
"image/color"
"math"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/fogleman/gg"
"github.com/pravj/geopattern/pattern"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
)
// Config stores values passed in as command-line flags.
type config struct {
text string
note string
pattern string
width int
height int
showVersion bool
}
var (
cfg config
cmd *flag.FlagSet
version = "devel"
)
func init() {
cfg = config{}
cmd = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
cmd.StringVar(&cfg.text, "text", "Hello, world.", "Text line")
cmd.StringVar(&cfg.note, "note", "https://www.example.com", "Footnote text")
cmd.StringVar(&cfg.pattern, "pattern", "nested-squares", "Geopattern for the background")
cmd.IntVar(&cfg.width, "width", 1200, "output image width")
cmd.IntVar(&cfg.height, "height", 628, "output image height")
cmd.BoolVar(&cfg.showVersion, "version", false, "print version number")
cmd.Usage = usage
}
func usage() {
fmt.Fprintf(cmd.Output(), "ogi is a tool for generation social images with a geopattern background.\n\n")
fmt.Fprintf(cmd.Output(), "USAGE\n\n")
fmt.Fprintf(cmd.Output(), " $ %s [OPTIONS]\n\n", os.Args[0])
fmt.Fprintf(cmd.Output(), "OPTIONS\n\n")
cmd.PrintDefaults()
fmt.Fprintf(cmd.Output(), "\n")
fmt.Fprintf(cmd.Output(), "EXAMPLES\n\n")
fmt.Fprintf(cmd.Output(), " # with default options\n")
fmt.Fprintf(cmd.Output(), " $ %s\n\n", os.Args[0])
fmt.Fprintf(cmd.Output(), " # with custom title\n")
fmt.Fprintf(cmd.Output(), " $ %s\n\n --title=\"Example\"", os.Args[0])
}
func main() {
cmd.Parse(os.Args[1:])
if cfg.showVersion {
fmt.Fprintf(cmd.Output(), "%s %s (runtime: %s)\n", os.Args[0], version, runtime.Version())
os.Exit(0)
}
if err := run(); err != nil {
fmt.Fprintf(cmd.Output(), "%s\n", err)
os.Exit(1)
}
}
func run() error {
ctx := gg.NewContext(cfg.width, cfg.height)
if err := renderBackground(ctx); err != nil {
return fmt.Errorf("Error rendering background: %w", err)
}
if err := renderOverlay(ctx); err != nil {
return fmt.Errorf("Error rendering overlay: %w", err)
}
if err := renderText(ctx); err != nil {
return fmt.Errorf("Error rendering text: %w", err)
}
if err := renderNote(ctx); err != nil {
return fmt.Errorf("Error rendering note: %w", err)
}
if err := ctx.EncodePNG(os.Stdout); err != nil {
return fmt.Errorf("Error encoding PNG: %w", err)
}
return nil
}
func renderBackground(ctx *gg.Context) error {
tile, err := generateBackgroundTile()
if err != nil {
return fmt.Errorf("Error generating background tile: %w", err)
}
tileWidth := tile.Bounds().Size().X
tileHeight := tile.Bounds().Size().Y
// Calculate number of vertical (ny) and horizontal (nx) tiles
nx := int(math.Ceil(float64(cfg.width) / float64(tileWidth)))
ny := int(math.Ceil(float64(cfg.height) / float64(tileHeight)))
// Put background tiles
for y := 0; y < ny; y++ {
for x := 0; x < nx; x++ {
ctx.DrawImage(tile, x*tileWidth, y*tileHeight)
}
}
return nil
}
func generateBackgroundTile() (image.Image, error) {
p := pattern.New(map[string]string{
"phrase": cfg.text,
"generator": cfg.pattern,
})
// Generate SVG code for given pattern.
svg := p.SvgStr()
// NOTE: These two lines MUST BE after a call to p.SvgStr() otherwise width
// and height of SVG is unknown.
svgWidth := p.Svg.GetWidth()
svgHeight := p.Svg.GetHeight()
// HACK: Strings "width='100%'" and "height='100%'" are hardcoded in
// geopattern/pattern package. Some refactoring is required to make
// them flexible, but it'll take some time to implement.
svg = strings.ReplaceAll(svg, "width='100%'", fmt.Sprintf("width='%d'", svgWidth))
svg = strings.ReplaceAll(svg, "height='100%'", fmt.Sprintf("height='%d'", svgHeight))
// Rasterize SVG.
icon, err := oksvg.ReadIconStream(strings.NewReader(svg))
if err != nil {
return nil, fmt.Errorf("Error reading SVG: %w", err)
}
icon.SetTarget(0, 0, float64(svgWidth), float64(svgHeight))
rgba := image.NewRGBA(image.Rect(0, 0, svgWidth, svgHeight))
icon.Draw(rasterx.NewDasher(svgWidth, svgHeight, rasterx.NewScannerGV(svgWidth, svgHeight, rgba, rgba.Bounds())), 1)
return image.Image(rgba), nil
}
// Renders semi-transparend overlay rectangle.
func renderOverlay(ctx *gg.Context) error {
borderRadius := 16.0
margin := 80.0
x := margin
y := margin
width := float64(ctx.Width()) - (2.0 * margin)
height := float64(ctx.Height()) - (2.0 * margin)
ctx.SetColor(color.RGBA{0, 0, 0, 204})
ctx.DrawRoundedRectangle(x, y, width, height, borderRadius)
ctx.Fill()
return nil
}
// Renders the given text line (with shadow).
func renderText(ctx *gg.Context) error {
marginLeft := 160.0
marginTop := 160.0
x := marginLeft
y := marginTop
width := float64(ctx.Width()) - 2.0*marginLeft
textColor := color.White
shadowColor := color.Black
fontSize := 70.0
fontPath := filepath.Join("fonts", "JetBrainsMono-Bold.ttf")
if err := ctx.LoadFontFace(fontPath, fontSize); err != nil {
return fmt.Errorf("Error loading font: %w", err)
}
ctx.SetColor(shadowColor)
ctx.DrawStringWrapped(cfg.text, x+1, y+1, 0, 0, width, 1.5, gg.AlignLeft)
ctx.SetColor(textColor)
ctx.DrawStringWrapped(cfg.text, x, y, 0, 0, width, 1.5, gg.AlignLeft)
return nil
}
// Renders the given note.
func renderNote(ctx *gg.Context) error {
fontSize := 30.0
fontPath := filepath.Join("fonts", "JetBrainsMono-Regular.ttf")
if err := ctx.LoadFontFace(fontPath, fontSize); err != nil {
return fmt.Errorf("Error loading font: %w", err)
}
marginLeft := 160.0
marginBottom := 100.0
_, height := ctx.MeasureString(cfg.note)
x := marginLeft
y := float64(ctx.Height()) - height - marginBottom
textColor := color.White
ctx.SetColor(textColor)
ctx.DrawString(cfg.note, x, y)
return nil
}