diff options
author | Přemysl Janouch <p@janouch.name> | 2019-04-14 22:30:40 +0200 |
---|---|---|
committer | Přemysl Janouch <p@janouch.name> | 2019-04-14 22:30:40 +0200 |
commit | 1331f3b5642f521236fcb1ec21ee43d5b76c0b91 (patch) | |
tree | 1213c2f2014cf0eff5a4a881b02cbebf91a604b8 /cmd/bdf-preview | |
parent | 7d9410c6b3a724e3670941f7ec2d00e7966d0b1a (diff) | |
download | sklad-1331f3b5642f521236fcb1ec21ee43d5b76c0b91.tar.gz sklad-1331f3b5642f521236fcb1ec21ee43d5b76c0b91.tar.xz sklad-1331f3b5642f521236fcb1ec21ee43d5b76c0b91.zip |
Move commands under cmd/
Diffstat (limited to 'cmd/bdf-preview')
-rw-r--r-- | cmd/bdf-preview/main.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/cmd/bdf-preview/main.go b/cmd/bdf-preview/main.go new file mode 100644 index 0000000..b1f2045 --- /dev/null +++ b/cmd/bdf-preview/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "html/template" + "image" + "image/draw" + "image/png" + "log" + "net/http" + "os" + + "janouch.name/sklad/bdf" +) + +type fontItem struct { + Font *bdf.Font + Preview image.Image +} + +var fonts = map[string]fontItem{} + +var tmpl = template.Must(template.New("list").Parse(` + <!DOCTYPE html> + <html><body> + <table border='1' cellpadding='3' style='border-collapse: collapse'> + <tr> + <th>Name</th> + <th>Preview</th> + <tr> +{{range $k, $v := . }} + <tr> + <td>{{$k}}</td> + <td><img src='?name={{$k}}'></td> + </tr> +{{end}} + </table> + </body></html> +`)) + +func handle(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), 500) + return + } + + name := r.FormValue("name") + if name == "" { + w.Header().Set("Content-Type", "text/html") + tmpl.Execute(w, fonts) + return + } + + item, ok := fonts[name] + if !ok { + http.Error(w, "No such font.", 400) + return + } + + w.Header().Set("Content-Type", "image/png") + if err := png.Encode(w, item.Preview); err != nil { + http.Error(w, err.Error(), 500) + return + } +} + +func main() { + for _, filename := range os.Args[1:] { + fi, err := os.Open(filename) + if err != nil { + log.Fatalln(err) + } + font, err := bdf.NewFromBDF(fi) + if err != nil { + log.Fatalf("%s: %s\n", filename, err) + } + if err := fi.Close(); err != nil { + log.Fatalln(err) + } + + r, _ := font.BoundString(font.Name) + super := r.Inset(-3) + + img := image.NewRGBA(super) + draw.Draw(img, super, image.White, image.ZP, draw.Src) + font.DrawString(img, image.ZP, font.Name) + + fonts[filename] = fontItem{Font: font, Preview: img} + } + + log.Println("Starting server") + http.HandleFunc("/", handle) + log.Fatal(http.ListenAndServe(":8080", nil)) +} |