Skip to content

Commit

Permalink
Workaround wrong font height returned by freetype
Browse files Browse the repository at this point in the history
The height seems to be only slightly bigger than the Ascent.
It's small enough such that characters that take up space below the
baseline (e.g. 'g', 'y', 'j', 'p') gets cut off at the bottom.
(For reference, see
https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png)

This is a freetype bug: golang/freetype#34
There are multiple PRs with a fix that haven't been merged:
golang/freetype#32
golang/freetype#62

Plan9port's fontsrv also similarly sums up ascent and descent to compute
the height:
https://github.com/9fans/plan9port/blob/047fd921744f39a82a86d9370e03f7af511e6e84/src/cmd/fontsrv/x11.c#L80
  • Loading branch information
fhs committed Mar 8, 2019
1 parent 3c3ad83 commit b7ab47b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ func openFont(id FaceID) (*Font, error) {
faceCache.Lock()
defer faceCache.Unlock()
if f, ok := faceCache.m[id]; ok {
m := f.Metrics()
// TODO(fhs): Remove workaround for wrong m.Height.
return &Font{
FaceID: id,
Height: int(f.Metrics().Height / 64),
Height: (m.Ascent + m.Descent).Round(),
face: f,
}, nil
}
Expand All @@ -93,9 +95,11 @@ func openFont(id FaceID) (*Font, error) {
face := pixFace{Face: truetype.NewFace(f, &opt)}
faceCache.m[id] = face

m := face.Metrics()
// TODO(fhs): Remove workaround for wrong m.Height.
return &Font{
FaceID: id,
Height: int((face.Metrics().Height + 32) / 64),
Height: (m.Ascent + m.Descent).Round(),
face: face,
}, nil
}
Expand Down

0 comments on commit b7ab47b

Please sign in to comment.