main.go 1.9 KB
Newer Older
1 2
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
3 4
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
5

6 7 8 9 10
// +build ignore
//
// This build tag means that "go install github.com/golang/freetype/..."
// doesn't install this example program. Use "go run main.go" to run it.

11 12 13 14 15 16 17 18
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"

N
Nigel Tao 已提交
19
	"github.com/golang/freetype/truetype"
20
	"golang.org/x/exp/shiny/font"
21
	"golang.org/x/image/math/fixed"
22 23
)

24
var fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
25 26 27 28 29

func printBounds(b truetype.Bounds) {
	fmt.Printf("XMin:%d YMin:%d XMax:%d YMax:%d\n", b.XMin, b.YMin, b.XMax, b.YMax)
}

30
func printGlyph(g *truetype.GlyphBuf) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	printBounds(g.B)
	fmt.Print("Points:\n---\n")
	e := 0
	for i, p := range g.Point {
		fmt.Printf("%4d, %4d", p.X, p.Y)
		if p.Flags&0x01 != 0 {
			fmt.Print("  on\n")
		} else {
			fmt.Print("  off\n")
		}
		if i+1 == int(g.End[e]) {
			fmt.Print("---\n")
			e++
		}
	}
}

func main() {
	flag.Parse()
	fmt.Printf("Loading fontfile %q\n", *fontfile)
	b, err := ioutil.ReadFile(*fontfile)
	if err != nil {
53
		log.Println(err)
54 55
		return
	}
56
	f, err := truetype.Parse(b)
57
	if err != nil {
58
		log.Println(err)
59 60
		return
	}
61 62
	fupe := fixed.Int26_6(f.FUnitsPerEm())
	printBounds(f.Bounds(fupe))
63
	fmt.Printf("FUnitsPerEm:%d\n\n", fupe)
64 65 66

	c0, c1 := 'A', 'V'

67 68
	i0 := f.Index(c0)
	hm := f.HMetric(fupe, i0)
69
	g := truetype.NewGlyphBuf()
70
	err = g.Load(f, fupe, i0, font.HintingNone)
71
	if err != nil {
72
		log.Println(err)
73 74 75 76 77
		return
	}
	fmt.Printf("'%c' glyph\n", c0)
	fmt.Printf("AdvanceWidth:%d LeftSideBearing:%d\n", hm.AdvanceWidth, hm.LeftSideBearing)
	printGlyph(g)
78 79
	i1 := f.Index(c1)
	fmt.Printf("\n'%c', '%c' Kerning:%d\n", c0, c1, f.Kerning(fupe, i0, i1))
80
}