-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
204 lines (166 loc) · 5.01 KB
/
main.go
File metadata and controls
204 lines (166 loc) · 5.01 KB
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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"strings"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/math/fixed"
)
type Config struct {
quote string
author string
width int
height int
outputPath string
fontPath string
scale float64
}
func parseFlags() Config {
quote := flag.String("quote", "[ Hello World ]", "The quote text")
author := flag.String("author", "", "The author of the quote")
width := flag.Int("width", 3840, "Width of the wallpaper")
height := flag.Int("height", 2160, "Height of the wallpaper")
outputPath := flag.String("output", "wallpaper.png", "Output file path")
fontPath := flag.String("font", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "Path to font file")
scale := flag.Float64("scale", 1.0, "Scale factor for font size")
flag.Parse()
if *quote == "" {
fmt.Println("No quote provided...")
}
return Config{
quote: *quote,
author: *author,
width: *width,
height: *height,
outputPath: *outputPath,
fontPath: *fontPath,
scale: *scale,
}
}
func createGradientBackground(width, height int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, width, height))
// Fill with black background
draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.Point{}, draw.Src)
return img
}
func addText(img *image.RGBA, config Config) error {
c := freetype.NewContext()
c.SetDPI(72)
fontData, err := os.ReadFile(config.fontPath)
if err != nil {
return fmt.Errorf("failed to read font: %v", err)
}
font, err := truetype.Parse(fontData)
if err != nil {
return fmt.Errorf("failed to parse font: %v", err)
}
c.SetFont(font)
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(image.White)
quoteSize := (float64(config.height) / 30) * config.scale
authorSize := quoteSize * 0.6 * config.scale
// Create font face for measurements
opts := truetype.Options{
Size: quoteSize,
DPI: 72,
}
face := truetype.NewFace(font, &opts)
defer face.Close()
lines := wrapText(config.quote, config.width/int(quoteSize)*2)
// Calculate total height of all lines plus spacing
totalHeight := float64(len(lines)) * quoteSize * 1.5
if config.author != "" {
totalHeight += authorSize * 1.5 // Add space for author
}
// Calculate starting Y position to center all text vertically
y := float64(config.height)/2 - totalHeight/2
// Draw quote lines
c.SetFontSize(quoteSize)
for _, line := range lines {
// Calculate width using the advance width metric
var width fixed.Int26_6
for _, r := range line {
awidth, _ := face.GlyphAdvance(r)
width += awidth
}
widthPx := float64(width) / 64 // Convert from 26.6 fixed point to pixels
// Center horizontally
x := (float64(config.width) - widthPx) / 2
pt := freetype.Pt(int(x), int(y))
_, err = c.DrawString(line, pt)
if err != nil {
return fmt.Errorf("failed to draw quote: %v", err)
}
y += quoteSize * 1.5
}
// Draw author if present
if config.author != "" {
c.SetFontSize(authorSize)
authorText := fmt.Sprintf("- %s", config.author)
opts.Size = authorSize
authorFace := truetype.NewFace(font, &opts)
defer authorFace.Close()
// Calculate width using the advance width metric
var width fixed.Int26_6
for _, r := range authorText {
awidth, _ := authorFace.GlyphAdvance(r)
width += awidth
}
widthPx := float64(width) / 64 // Convert from 26.6 fixed point to pixels
// Center horizontally
x := (float64(config.width) - widthPx) / 2
pt := freetype.Pt(int(x), int(y))
_, err = c.DrawString(authorText, pt)
if err != nil {
return fmt.Errorf("failed to draw author: %v", err)
}
}
return nil
}
func wrapText(text string, maxChars int) []string {
words := strings.Fields(text)
if len(words) == 0 {
return nil
}
var lines []string
currentLine := words[0]
for _, word := range words[1:] {
if len(currentLine)+1+len(word) <= maxChars {
currentLine += " " + word
} else {
lines = append(lines, currentLine)
currentLine = word
}
}
lines = append(lines, currentLine)
return lines
}
func main() {
config := parseFlags()
// Create background
img := createGradientBackground(config.width, config.height)
// Add text
err := addText(img, config)
if err != nil {
log.Fatalf("Failed to add text: %v", err)
}
// Save the image
f, err := os.Create(config.outputPath)
if err != nil {
log.Fatalf("Failed to create output file: %v", err)
}
defer f.Close()
err = png.Encode(f, img)
if err != nil {
log.Fatalf("Failed to encode image: %v", err)
}
fmt.Printf("Wallpaper generated to: %s\n", config.outputPath)
}