forked from joiningdata/lollipops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.go
58 lines (49 loc) · 1.33 KB
/
fonts.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"code.google.com/p/jamslam-freetype-go/freetype"
)
var (
arialPath *string
fontContext *freetype.Context
)
func init() {
// try to find Arial so we can measure it
// I don't try very hard...
popularpaths := []string{
// OS X path
"/Library/Fonts/Arial.ttf",
// Windows path
"C:/Windows/Fonts/arial.ttf",
// Ubuntu with multiverse msttcorefonts package
"/usr/share/fonts/truetype/msttcorefonts/arial.ttf",
}
for _, path := range popularpaths {
fontBytes, err := ioutil.ReadFile(path)
if err == nil {
arialFont, err := freetype.ParseFont(fontBytes)
if err == nil {
fontContext = freetype.NewContext()
fontContext.SetFont(arialFont)
return
}
}
}
fmt.Fprintln(os.Stderr, "can't find arial.ttf - for more accurate font sizing use -f=/path/to/arial.ttf")
arialPath = flag.String("f", "", "path to arial.ttf")
}
// MeasureFont returns the pixel width of the string s at font size sz.
// It tries to use system Arial font if possible, but falls back to a
// conservative ballpark estimate otherwise.
func MeasureFont(s string, sz int) int {
// use actual TTF font metrics if available
if fontContext != nil {
fontContext.SetFontSize(float64(sz))
w, _, _ := fontContext.MeasureString(s)
return freetype.Pixel(w)
}
return len(s) * (sz - 2)
}