Add font dumping command

This commit is contained in:
Dave Smith 2017-01-30 17:56:52 -07:00
parent d9be45aaf7
commit e74951ef31
2 changed files with 61 additions and 0 deletions

38
cmd/dumpfont/dumpfont.go Normal file
View file

@ -0,0 +1,38 @@
// Copyright 2010-2017 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package main
import (
"flag"
"io/ioutil"
"fmt"
"os"
"github.com/golang/freetype"
)
var fontfile = flag.String("font", "", "filename of font to dump")
func main() {
flag.Parse()
// Load the raw data from disk
fontData, err := ioutil.ReadFile(*fontfile)
if err != nil {
fmt.Printf("Failed to load font from %s: %+v\n", *fontfile, err)
os.Exit(1)
}
// Parse the font data
font, err := freetype.ParseFont(fontData)
if err != nil {
fmt.Printf("Failed to parse font from %s: %+v\n", *fontfile, err)
os.Exit(1)
}
// Dump summary info
font.Dump()
}

View file

@ -641,3 +641,26 @@ func parse(ttf []byte, offset int) (font *Font, err error) {
font = f
return
}
// Dump prints internal details about the font to stdout
func (f *Font) Dump() {
fmt.Printf("Name: %s\n", f.Name(NameIDFontFullName))
fmt.Printf(" Glyphs: %d\n HMetrics: %d\n Kerns: %d\n", f.nGlyph, f.nHMetric, f.nKern)
fmt.Printf(" FUnits-per-em: %d\n", f.fUnitsPerEm)
fmt.Printf(" Bounds: %+v\n", f.bounds)
fmt.Printf(" Ascent: %d\tDescent: %d\n", f.ascent, f.descent)
// Build a mapping of glyph indices to characters
glyphToChar := make([]uint32, f.nGlyph)
for _, cm := range f.cm {
for i := cm.start; i <= cm.end; i++ {
glyphToChar[f.Index(rune(i))] = i
}
}
// Dump the cmap in glyph index order
fmt.Printf("cmap:\n")
for i, val := range glyphToChar {
fmt.Printf(" Glyph %d -> %+q\n", i, val)
}
}