func TestFont_Matches(t *testing.T) { arial1, err := ttf.LoadFont("/Library/Fonts/Arial.ttf") if err != nil { t.Fatal(err) } f1 := &Font{metrics: arial1} arial2, err := ttf.LoadFont("/Library/Fonts/Arial.ttf") if err != nil { t.Fatal(err) } f2 := &Font{metrics: arial2} check(t, f1.Matches(f2), "Fonts should match.") }
func (fc *TtfFontCollection) Select(family, weight, style string, ranges []string) (fontMetrics FontMetrics, err error) { var ws string if weight != "" && style != "" { ws = weight + " " + style } else if weight == "" && style == "" { ws = "Regular" } else if style == "" { ws = weight } else if weight == "" { ws = style } if fc.fonts == nil { fc.fonts = make(map[string]*ttf.Font) } search: for _, f := range fc.FontInfos { if strings.EqualFold(f.Family(), family) && strings.EqualFold(f.Style(), ws) { for _, r := range ranges { cpr, ok := ttf.CodepointRangesByName[r] if !ok || !f.CharRanges().IsSet(int(cpr.Bit)) { continue search } } font := fc.fonts[f.Filename] if font == nil { font, err = ttf.LoadFont(f.Filename) fc.fonts[f.Filename] = font } fontMetrics = font return } } err = fmt.Errorf("Font %s %s not found", family, ws) return }
func TestFont_HasRune(t *testing.T) { arial, err := ttf.LoadFont("/Library/Fonts/Arial.ttf") if err != nil { t.Fatal(err) } f := &Font{metrics: arial} check(t, f.HasRune('A'), "Arial should have 'A'.") check(t, !f.HasRune(0x9999), "Arial should not have 0x9999.") }
// 55.2 ns // 46.1 ns go1.1.1 // 46.0 ns go1.1.2 // 46.1 ns go1.2.1 // 49.2 ns go1.4.2 func BenchmarkFont_HasRune(b *testing.B) { b.StopTimer() arial, err := ttf.LoadFont("/Library/Fonts/Arial.ttf") if err != nil { b.Fatal(err) } f := &Font{metrics: arial} b.StartTimer() for i := 0; i < b.N; i++ { f.HasRune('A') } }
func main() { if len(os.Args) < 2 { fmt.Println("Usage: ttdump fontname [table1 [table2...]]") os.Exit(-1) } fontname := os.Args[1] fmt.Println("ttdump", fontname) font, err := ttf.LoadFont(fontname) if err != nil { fmt.Println(err) os.Exit(-1) } if len(os.Args) < 3 { font.Dump(os.Stdout, "all") } else { for i := 2; i < len(os.Args); i++ { font.Dump(os.Stdout, os.Args[i]) } } }