Exemple #1
0
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.")
}
Exemple #2
0
func (fc *TtfFonts) Select(family, weight, style string, ranges []string) (fontMetrics font.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
}
Exemple #3
0
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.")
}
Exemple #4
0
// 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')
	}
}