Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer full/UCS4 sub-tables over BMP/UCS2 ones #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions truetype/truetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down