提交 f9531a36 编写于 作者: N Nigel Tao

Add a face.Metrics method, and parse hhea ascent and descent.

A recent change added a Metrics method to the font.Face interface.
上级 9ce4eec9
...@@ -77,4 +77,12 @@ func main() { ...@@ -77,4 +77,12 @@ func main() {
printGlyph(g) printGlyph(g)
i1 := f.Index(c1) i1 := f.Index(c1)
fmt.Printf("\n'%c', '%c' Kern:%d\n", c0, c1, f.Kern(fupe, i0, i1)) fmt.Printf("\n'%c', '%c' Kern:%d\n", c0, c1, f.Kern(fupe, i0, i1))
fmt.Printf("\nThe numbers above are in FUnits.\n" +
"The numbers below are in 26.6 fixed point pixels, at 12pt and 72dpi.\n\n")
a := truetype.NewFace(f, &truetype.Options{
Size: 12,
DPI: 72,
})
fmt.Printf("%#v\n", a.Metrics())
} }
...@@ -7,6 +7,7 @@ package truetype ...@@ -7,6 +7,7 @@ package truetype
import ( import (
"image" "image"
"math"
"github.com/golang/freetype/raster" "github.com/golang/freetype/raster"
"golang.org/x/image/font" "golang.org/x/image/font"
...@@ -249,6 +250,17 @@ func (a *face) index(r rune) Index { ...@@ -249,6 +250,17 @@ func (a *face) index(r rune) Index {
// Close satisfies the font.Face interface. // Close satisfies the font.Face interface.
func (a *face) Close() error { return nil } func (a *face) Close() error { return nil }
// Metrics satisfies the font.Face interface.
func (a *face) Metrics() font.Metrics {
scale := float64(a.scale)
fupe := float64(a.f.FUnitsPerEm())
return font.Metrics{
Height: a.scale,
Ascent: fixed.Int26_6(math.Ceil(scale * float64(+a.f.ascent) / fupe)),
Descent: fixed.Int26_6(math.Ceil(scale * float64(-a.f.descent) / fupe)),
}
}
// Kern satisfies the font.Face interface. // Kern satisfies the font.Face interface.
func (a *face) Kern(r0, r1 rune) fixed.Int26_6 { func (a *face) Kern(r0, r1 rune) fixed.Int26_6 {
i0 := a.index(r0) i0 := a.index(r0)
......
...@@ -184,7 +184,9 @@ type Font struct { ...@@ -184,7 +184,9 @@ type Font struct {
locaOffsetFormat int locaOffsetFormat int
nGlyph, nHMetric, nKern int nGlyph, nHMetric, nKern int
fUnitsPerEm int32 fUnitsPerEm int32
bounds fixed.Rectangle26_6 ascent int32 // In FUnits.
descent int32 // In FUnits; typically negative.
bounds fixed.Rectangle26_6 // In FUnits.
// Values from the maxp section. // Values from the maxp section.
maxTwilightPoints, maxStorage, maxFunctionDefs, maxStackElements uint16 maxTwilightPoints, maxStorage, maxFunctionDefs, maxStackElements uint16
} }
...@@ -289,6 +291,8 @@ func (f *Font) parseHhea() error { ...@@ -289,6 +291,8 @@ func (f *Font) parseHhea() error {
if len(f.hhea) != 36 { if len(f.hhea) != 36 {
return FormatError(fmt.Sprintf("bad hhea length: %d", len(f.hhea))) return FormatError(fmt.Sprintf("bad hhea length: %d", len(f.hhea)))
} }
f.ascent = int32(int16(u16(f.hhea, 4)))
f.descent = int32(int16(u16(f.hhea, 6)))
f.nHMetric = int(u16(f.hhea, 34)) f.nHMetric = int(u16(f.hhea, 34))
if 4*f.nHMetric+2*(f.nGlyph-f.nHMetric) != len(f.hmtx) { if 4*f.nHMetric+2*(f.nGlyph-f.nHMetric) != len(f.hmtx) {
return FormatError(fmt.Sprintf("bad hmtx length: %d", len(f.hmtx))) return FormatError(fmt.Sprintf("bad hmtx length: %d", len(f.hmtx)))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册