...

Source file src/github.com/golang/freetype/example/truetype/main.go

Documentation: github.com/golang/freetype/example/truetype

     1  // Copyright 2010 The Freetype-Go Authors. All rights reserved.
     2  // Use of this source code is governed by your choice of either the
     3  // FreeType License or the GNU General Public License version 2 (or
     4  // any later version), both of which can be found in the LICENSE file.
     5  
     6  // +build example
     7  //
     8  // This build tag means that "go install github.com/golang/freetype/..."
     9  // doesn't install this example program. Use "go run main.go" to run it or "go
    10  // install -tags=example" to install it.
    11  
    12  package main
    13  
    14  import (
    15  	"flag"
    16  	"fmt"
    17  	"io/ioutil"
    18  	"log"
    19  
    20  	"github.com/golang/freetype/truetype"
    21  	"golang.org/x/image/font"
    22  	"golang.org/x/image/math/fixed"
    23  )
    24  
    25  var fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
    26  
    27  func printBounds(b fixed.Rectangle26_6) {
    28  	fmt.Printf("Min.X:%d Min.Y:%d Max.X:%d Max.Y:%d\n", b.Min.X, b.Min.Y, b.Max.X, b.Max.Y)
    29  }
    30  
    31  func printGlyph(g *truetype.GlyphBuf) {
    32  	printBounds(g.Bounds)
    33  	fmt.Print("Points:\n---\n")
    34  	e := 0
    35  	for i, p := range g.Points {
    36  		fmt.Printf("%4d, %4d", p.X, p.Y)
    37  		if p.Flags&0x01 != 0 {
    38  			fmt.Print("  on\n")
    39  		} else {
    40  			fmt.Print("  off\n")
    41  		}
    42  		if i+1 == int(g.Ends[e]) {
    43  			fmt.Print("---\n")
    44  			e++
    45  		}
    46  	}
    47  }
    48  
    49  func main() {
    50  	flag.Parse()
    51  	fmt.Printf("Loading fontfile %q\n", *fontfile)
    52  	b, err := ioutil.ReadFile(*fontfile)
    53  	if err != nil {
    54  		log.Println(err)
    55  		return
    56  	}
    57  	f, err := truetype.Parse(b)
    58  	if err != nil {
    59  		log.Println(err)
    60  		return
    61  	}
    62  	fupe := fixed.Int26_6(f.FUnitsPerEm())
    63  	printBounds(f.Bounds(fupe))
    64  	fmt.Printf("FUnitsPerEm:%d\n\n", fupe)
    65  
    66  	c0, c1 := 'A', 'V'
    67  
    68  	i0 := f.Index(c0)
    69  	hm := f.HMetric(fupe, i0)
    70  	g := &truetype.GlyphBuf{}
    71  	err = g.Load(f, fupe, i0, font.HintingNone)
    72  	if err != nil {
    73  		log.Println(err)
    74  		return
    75  	}
    76  	fmt.Printf("'%c' glyph\n", c0)
    77  	fmt.Printf("AdvanceWidth:%d LeftSideBearing:%d\n", hm.AdvanceWidth, hm.LeftSideBearing)
    78  	printGlyph(g)
    79  	i1 := f.Index(c1)
    80  	fmt.Printf("\n'%c', '%c' Kern:%d\n", c0, c1, f.Kern(fupe, i0, i1))
    81  
    82  	fmt.Printf("\nThe numbers above are in FUnits.\n" +
    83  		"The numbers below are in 26.6 fixed point pixels, at 12pt and 72dpi.\n\n")
    84  	a := truetype.NewFace(f, &truetype.Options{
    85  		Size: 12,
    86  		DPI:  72,
    87  	})
    88  	fmt.Printf("%#v\n", a.Metrics())
    89  }
    90  

View as plain text