Repository: go
Issue: x/image/font/opentype: font metrics discrepancies wrt golang/freetype/truetype
Labels: NeedsInvestigation

hi,

it seems parsing the same TTF files (Liberation fonts, for example) leads to different font metrics values.
I'd like to understand whether that's coming from a bug/oversight from `x/image/font/sfnt` or from `freetype/truetype`.

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version devel +732ea4c2dc Thu Mar 18 08:25:24 2021 +0000 linux/amd64

</pre>

### Does this issue reproduce with the latest release?

yes.

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/binet/.cache/go-build"
GOENV="/home/binet/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/binet/dev/go/gocode/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/binet/dev/go/gocode"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/binet/sdk/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/binet/sdk/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel +732ea4c2dc Thu Mar 18 08:25:24 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1145417185=/tmp/go-build -gno-record-gcc-switches"
</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->

https://play.golang.org/p/BM_FTuuxrxF


### What did you expect to see?

reasonnable agreement between font metrics extracted from a TTF file via `x/image` and via `freetype/truetype`.
- `freetype/truetype` doesn't extract XHeight, CapHeight nor CaretSlope
- Descent is defined negative in one case, positive in the other
but apart from those known differences, (and the glyph bounds), nothing really matches.

### What did you see instead?

```
$> go run main.go
fnter: truetype: units-per-em=2048
fnter: opentype: units-per-em=2048
fnter: truetype: ext=main.Extents{Ascent:9.7998046875, Descent:-3.0322265625, Height:12.83203125}
fnter: opentype: ext=main.Extents{Ascent:9.052734375, Descent:2.119140625, Height:11.4990234375}
fnter: truetype: bnds=fixed.Rectangle26_6{Min:fixed.Point26_6{X:-1114, Y:-621}, Max:fixed.Point26_6{X:2666, Y:2007}}
fnter: opentype: bnds=fixed.Rectangle26_6{Min:fixed.Point26_6{X:-1114, Y:-2007}, Max:fixed.Point26_6{X:2666, Y:621}}
fnter: truetype: met=font.Metrics{Height:640, Ascent:580, Descent:136, XHeight:0, CapHeight:0, CaretSlope:image.Point{X:0, Y:0}}
fnter: opentype: met=font.Metrics{Height:736, Ascent:579, Descent:136, XHeight:338, CapHeight:440, CaretSlope:image.Point{X:0, Y:1}}
fnter: delta(t-o):   font.Metrics{Height:-96, Ascent:1, Descent:0, XHeight:-338, CapHeight:-440, CaretSlope:image.Point{X:0, Y:-1}}
fnter: opentype: met=font.Metrics{Height:2355, Ascent:1854, Descent:434, XHeight:1082, CapHeight:1409, CaretSlope:image.Point{X:0, Y:1}}
fnter: truetype: line-gap=196
fnter: opentype: line-gap=21
```