From 240a51ac9f088c1c81cad2cf80a37b99c52abcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Agsj=C3=B6?= Date: Tue, 18 Jul 2023 12:38:19 +0000 Subject: [PATCH] font/sfnt: support early version 0 OS/2 tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Reviewed-by: Nigel Tao Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) --- font/sfnt/sfnt.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/font/sfnt/sfnt.go b/font/sfnt/sfnt.go index 8d0dba7..8ed19e2 100644 --- a/font/sfnt/sfnt.go +++ b/font/sfnt/sfnt.go @@ -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 } @@ -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 @@ -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