From 8861c842a64012d6aa9b3253dcfae4aa996ef705 Mon Sep 17 00:00:00 2001 From: "dongzhou.xie" Date: Wed, 27 Oct 2021 01:06:38 +0800 Subject: [PATCH 1/2] prefer full/UCS4 sub-tables over BMP/UCS2 ones --- truetype/truetype.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/truetype/truetype.go b/truetype/truetype.go index 7270bbf..6b180d8 100644 --- a/truetype/truetype.go +++ b/truetype/truetype.go @@ -133,7 +133,7 @@ func parseSubtables(table []byte, name string, offset, size int, pred func([]byt if len(table) < size*nSubtables+offset { return 0, 0, FormatError(name + " too short") } - ok := false + bestScore := -1 for i := 0; i < nSubtables; i, offset = i+1, offset+size { if pred != nil && !pred(table[offset:]) { continue @@ -143,19 +143,22 @@ func parseSubtables(table []byte, name string, offset, size int, pred func([]byt pidPsid := u32(table, offset) // We prefer the Unicode cmap encoding. Failing to find that, we fall // back onto the Microsoft cmap encoding. + // And we prefer full/UCS4 encoding over BMP/UCS2. So the priority goes: + // unicodeEncodingFull > microsoftUCS4Encoding > unicodeEncodingBMPOnly > microsoftUCS2Encoding > microsoftSymbolEncoding + // It is in accord with the Psid part. + score := int(pidPsid & 0xFFFF) + if score <= bestScore { + continue + } if pidPsid == unicodeEncodingBMPOnly || pidPsid == unicodeEncodingFull { - bestOffset, bestPID, ok = offset, pidPsid>>16, true - break - + bestOffset, bestPID, bestScore = offset, pidPsid>>16, score } else if pidPsid == microsoftSymbolEncoding || pidPsid == microsoftUCS2Encoding || pidPsid == microsoftUCS4Encoding { - - bestOffset, bestPID, ok = offset, pidPsid>>16, true - // We don't break out of the for loop, so that Unicode can override Microsoft. + bestOffset, bestPID, bestScore = offset, pidPsid>>16, score } } - if !ok { + if bestScore < 0 { return 0, 0, UnsupportedError(name + " encoding") } return bestOffset, bestPID, nil From 9bd971f15a1d616c038c6c3715becc3cf8327134 Mon Sep 17 00:00:00 2001 From: "dongzhou.xie" Date: Wed, 24 Nov 2021 14:32:36 +0800 Subject: [PATCH 2/2] remove import directive for truetype package --- truetype/truetype.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/truetype/truetype.go b/truetype/truetype.go index 6b180d8..22c48ce 100644 --- a/truetype/truetype.go +++ b/truetype/truetype.go @@ -15,7 +15,7 @@ // // To measure a TrueType font in ideal FUnit space, use scale equal to // font.FUnitsPerEm(). -package truetype // import "github.com/golang/freetype/truetype" +package truetype import ( "fmt"