-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (141 loc) · 4.21 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
package main
import (
"bytes"
_ "embed"
"flag"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
//go:embed Montserrat-ExtraBold.ttf
var titleFontFile []byte
//go:embed Montserrat-SemiBold.ttf
var contentFontFile []byte
//go:embed blue.png
var background []byte
var (
width = 1200
height = 630
title = flag.String("title", "Sample Post Title", "Title text")
description = flag.String("desc", "Description of the post in about 25 words. This string could be your og description also!", "Description text")
date = flag.String("date", "30 April 2021", "Date text")
readingtime = flag.String("readtime", "4 min read", "Reading time text")
out = flag.String("out", "out.png", "Name of the output file")
dpi = flag.Float64("dpi", 72, "screen resolution in dots per inch")
// TODO: use the fonts provided
tfont = flag.String("tfont", "Montserrat-ExtraBold.ttf", "Font to use for the title")
cfont = flag.String("cfont", "Montserrat-SemiBold.ttf", "Font to use for the content (description and date)")
)
var (
marginTop = 130
marginLeft = 114
titleSize float64 = 42
descriptionSize = 22
titleColor = color.NRGBA{96, 234, 206, 255}
descriptionColor = color.NRGBA{232, 232, 232, 255}
)
func main() {
flag.Parse()
// create logger
logger := log.New(os.Stdout, "og-image-generator: ", log.LstdFlags)
tFont, err := truetype.Parse(titleFontFile)
if err != nil {
logger.Printf("Error parsing font: %s\n", *tfont)
logger.Println(err)
return
}
cFont, err := truetype.Parse(contentFontFile)
if err != nil {
logger.Printf("Error parsing font: %s", *cfont)
logger.Println(err)
return
}
bg, _, _ := image.Decode(bytes.NewReader(background))
output := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(output, bg.Bounds(), bg, image.Point{0, 0}, draw.Over)
outFile, err := os.Create(*out)
defer outFile.Close()
if err != nil {
logger.Printf("Error creating output file : %s\n", *out)
logger.Println(err)
}
tDrawer := &font.Drawer{
Dst: output,
Face: truetype.NewFace(tFont, &truetype.Options{Size: titleSize, Hinting: font.HintingNone, DPI: *dpi}),
Src: image.NewUniform(titleColor),
}
tDrawer.Dot = fixed.Point26_6{
X: fixed.I(marginLeft),
Y: fixed.I(marginTop),
}
headline := wrapLines(*title, true)
for _, r := range headline {
if r != "" {
tDrawer.DrawString(r)
yCursor := int(titleSize) + 4 + marginTop
// move the point to next line
tDrawer.Dot = fixed.Point26_6{
X: fixed.I(marginLeft),
Y: fixed.I(yCursor), // 4 is space between lines
}
marginTop = yCursor // update the global cursor
}
}
cDrawer := &font.Drawer{
Dst: output,
Face: truetype.NewFace(cFont, &truetype.Options{Size: 20, Hinting: font.HintingNone, DPI: *dpi}),
Src: image.NewUniform(descriptionColor),
}
cDrawer.Dot = fixed.Point26_6{
X: fixed.I(marginLeft),
Y: fixed.I(marginTop), // 12 (8 + 4), 4 is from the last iteration of loop
}
descriptionText := wrapLines(*description, false)
for _, r := range descriptionText {
if r != "" {
cDrawer.DrawString(r)
yCursor := marginTop + int(descriptionSize) + 4
// move the point to next line
cDrawer.Dot = fixed.Point26_6{
X: fixed.I(marginLeft),
Y: fixed.I(yCursor), // 4 is space between lines
}
marginTop = yCursor // update the global cursor
}
}
marginTop = 100
cDrawer.Dot = fixed.Point26_6{
X: fixed.I(marginLeft),
Y: fixed.I(bg.Bounds().Size().Y - marginTop), // 12 (8 + 4), 4 is from the last iteration of loop
}
cDrawer.DrawString(*date)
if *readingtime != "" {
cDrawer.DrawString(" | ")
cDrawer.DrawString(*readingtime)
}
png.Encode(outFile, output)
logger.Printf("File created successfully: %s!", *out)
}
// wrapLines returns slice of string of max length 4
// There can be empty string is the length of arg text is
// small
func wrapLines(text string, isTitle bool) [4]string {
limit := 14
if isTitle {
limit = 5
}
var out [4]string
words := strings.Split(text, " ")
for i, r := range words {
out[i/limit] += r
out[i/limit] += " "
}
return out
}