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

Account for linegap in Metrics.Height. #32

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
9 changes: 6 additions & 3 deletions truetype/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,13 @@ func (a *face) Close() error { return nil }
func (a *face) Metrics() font.Metrics {
scale := float64(a.scale)
fupe := float64(a.f.FUnitsPerEm())
ascent := fixed.Int26_6(math.Ceil(scale * float64(+a.f.ascent) / fupe))
descent := fixed.Int26_6(math.Ceil(scale * float64(-a.f.descent) / fupe))
lineGap := fixed.Int26_6(math.Ceil(scale * float64(+a.f.lineGap) / fupe))
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)),
Height: ascent + lineGap + descent,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a difference in practice, but I'd order it: ascent, descent, lineGap.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Ascent: ascent,
Descent: descent,
}
}

Expand Down
2 changes: 2 additions & 0 deletions truetype/truetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ type Font struct {
fUnitsPerEm int32
ascent int32 // In FUnits.
descent int32 // In FUnits; typically negative.
lineGap int32 // In FUnits.
bounds fixed.Rectangle26_6 // In FUnits.
// Values from the maxp section.
maxTwilightPoints, maxStorage, maxFunctionDefs, maxStackElements uint16
Expand Down Expand Up @@ -293,6 +294,7 @@ func (f *Font) parseHhea() error {
}
f.ascent = int32(int16(u16(f.hhea, 4)))
f.descent = int32(int16(u16(f.hhea, 6)))
f.lineGap = int32(int16(u16(f.hhea, 8)))
f.nHMetric = int(u16(f.hhea, 34))
if 4*f.nHMetric+2*(f.nGlyph-f.nHMetric) != len(f.hmtx) {
return FormatError(fmt.Sprintf("bad hmtx length: %d", len(f.hmtx)))
Expand Down