Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Add font dumping command #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cmd/dumpfont/dumpfont.go
Original file line number Diff line number Diff line change
@@ -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()
}
23 changes: 23 additions & 0 deletions truetype/truetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}