diff --git a/truetype/truetype.go b/truetype/truetype.go index 7270bbf..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" @@ -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