This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (130 loc) · 3.12 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
package main
import (
"bufio"
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io/ioutil"
"log"
"os"
"github.com/golang/freetype"
"github.com/icodealot/noaa"
"golang.org/x/image/font"
)
type DailyForecast struct {
Name string
HighTemp float64
LowTemp float64
}
func main() {
forecast, err := sevenDayForecast("42.6526", "-73.7562")
if err != nil {
panic("Failed to retrieve forecast.")
}
if err = printForecastToImage(forecast); err != nil {
panic("Failed to print forecast to file.")
}
}
func sevenDayForecast(lat string, lng string) ([]DailyForecast, error) {
forecast, err := noaa.Forecast(lat, lng)
if err != nil {
return nil, err
}
var sevenDayForecast = []DailyForecast{}
var currentDay DailyForecast
for i, period := range forecast.Periods {
if i%2 == 0 {
currentDay = DailyForecast{
Name: period.Name,
HighTemp: period.Temperature,
}
} else {
currentDay.LowTemp = period.Temperature
sevenDayForecast = append(sevenDayForecast, currentDay)
}
}
return sevenDayForecast, nil
}
var (
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
fontfile = flag.String("fontfile", "luxisr.ttf", "filename of the ttf font")
hinting = flag.String("hinting", "none", "none | full")
size = flag.Float64("size", 12, "font size in points")
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
wonb = flag.Bool("whiteonblack", true, "white text on a black background")
)
func printForecastToImage(forecast []DailyForecast) error {
for _, day := range forecast {
fmt.Printf("%-20s %.0f / %.0f\n", day.Name, day.HighTemp, day.LowTemp)
}
flag.Parse()
// Read the font data.
fontBytes, err := ioutil.ReadFile(*fontfile)
if err != nil {
return err
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
return err
}
// Initialize the context.
fg, bg := image.Black, image.White
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
if *wonb {
fg, bg = image.White, image.Black
ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
}
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(*dpi)
c.SetFont(f)
c.SetFontSize(*size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
switch *hinting {
default:
c.SetHinting(font.HintingNone)
case "full":
c.SetHinting(font.HintingFull)
}
// Draw the guidelines.
for i := 0; i < 200; i++ {
rgba.Set(10, 10+i, ruler)
rgba.Set(10+i, 10, ruler)
}
text := []string{"this", "is", "a", "test"}
// Draw the text.
pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6))
for _, s := range text {
_, err = c.DrawString(s, pt)
if err != nil {
return err
}
pt.Y += c.PointToFixed(*size * *spacing)
}
// Save that RGBA image to disk.
outFile, err := os.Create("out/out.png")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer outFile.Close()
b := bufio.NewWriter(outFile)
err = png.Encode(b, rgba)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = b.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Println("Wrote out.png OK.")
return nil
}