From e74951ef312480b1f77d669d387b8b3b5c10c5ff Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 30 Jan 2017 17:56:52 -0700 Subject: [PATCH] Add font dumping command --- cmd/dumpfont/dumpfont.go | 38 ++++++++++++++++++++++++++++++++++++++ truetype/truetype.go | 23 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 cmd/dumpfont/dumpfont.go diff --git a/cmd/dumpfont/dumpfont.go b/cmd/dumpfont/dumpfont.go new file mode 100644 index 0000000..333b177 --- /dev/null +++ b/cmd/dumpfont/dumpfont.go @@ -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() +} diff --git a/truetype/truetype.go b/truetype/truetype.go index 613fc17..feecf05 100644 --- a/truetype/truetype.go +++ b/truetype/truetype.go @@ -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) + } +}