Skip to content

Commit

Permalink
font/sfnt: support early version 0 OS/2 tables
Browse files Browse the repository at this point in the history
Version 0 OS/2 tables can be as small as 68 bytes.

See https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html.

This works is taken over from https://go.dev/cl/510055 by Erik Agsjö.

Fixes golang/go#41658

Change-Id: If6fb961943b5563ed21fe5148252005743c17168
Reviewed-on: https://go-review.googlesource.com/c/image/+/533495
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) <nigeltao@google.com>
  • Loading branch information
erkkah authored and hajimehoshi committed Oct 7, 2023
1 parent c20bbc3 commit 240a51a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions font/sfnt/sfnt.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func (f *Font) initialize(offset int, isDfont bool) error {
f.cached.xHeight = xHeight

if !hasXHeightCapHeight {
xh, ch, err := f.initOS2Version1()
xh, ch, err := f.initOS2VersionBelow2()
if err != nil {
return err
}
Expand Down Expand Up @@ -1201,7 +1201,7 @@ func (f *Font) glyphTopOS2(b *Buffer, ppem fixed.Int26_6, r rune) (int32, error)
return int32(min), nil
}

func (f *Font) initOS2Version1() (xHeight, capHeight int32, err error) {
func (f *Font) initOS2VersionBelow2() (xHeight, capHeight int32, err error) {
ppem := fixed.Int26_6(f.UnitsPerEm())
var b Buffer

Expand Down Expand Up @@ -1235,12 +1235,14 @@ func (f *Font) parseOS2(buf []byte) (buf1 []byte, hasXHeightCapHeight bool, xHei
if err != nil {
return nil, false, 0, 0, err
}
if vers <= 1 {
const headerSize = 86
if vers < 2 {
// "The original TrueType specification had this table at 68 bytes long."
// https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html
const headerSize = 68
if f.os2.length < headerSize {
return nil, false, 0, 0, errInvalidOS2Table
}
// Will resolve xHeight and capHeight later, see initOS2Version1.
// Will resolve xHeight and capHeight later, see initOS2VersionBelow2.
return buf, false, 0, 0, nil
}
const headerSize = 96
Expand Down

0 comments on commit 240a51a

Please sign in to comment.