aboutsummaryrefslogtreecommitdiff
path: root/cmd/bdf-preview/main.go
blob: b1f20453c5559716a037290363a7f77cd3d8c232 (plain)
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
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))
}