@@ -137,7 +137,7 @@ func parseSubtables(table []byte, name string, offset, size int, pred func([]byt
137137 if len (table ) < size * nSubtables + offset {
138138 return 0 , 0 , FormatError (name + " too short" )
139139 }
140- ok := false
140+ bestScore := - 1
141141 for i := 0 ; i < nSubtables ; i , offset = i + 1 , offset + size {
142142 if pred != nil && ! pred (table [offset :]) {
143143 continue
@@ -147,19 +147,24 @@ func parseSubtables(table []byte, name string, offset, size int, pred func([]byt
147147 pidPsid := u32 (table , offset )
148148 // We prefer the Unicode cmap encoding. Failing to find that, we fall
149149 // back onto the Microsoft cmap encoding.
150+ // And we prefer full/UCS4 encoding over BMP/UCS2. So the priority goes:
151+ // unicodeEncodingFull > microsoftUCS4Encoding > unicodeEncodingBMPOnly > microsoftUCS2Encoding > microsoftSymbolEncoding
152+ // It is in accord with the Psid part.
153+ score := int (pidPsid & 0xFFFF )
154+ if score <= bestScore {
155+ continue
156+ }
150157 if pidPsid == unicodeEncodingBMPOnly || pidPsid == unicodeEncodingFull {
151- bestOffset , bestPID , ok = offset , pidPsid >> 16 , true
158+ bestOffset , bestPID , bestScore = offset , pidPsid >> 16 , score
152159 break
153160
154161 } else if pidPsid == microsoftSymbolEncoding ||
155162 pidPsid == microsoftUCS2Encoding ||
156163 pidPsid == microsoftUCS4Encoding {
157-
158- bestOffset , bestPID , ok = offset , pidPsid >> 16 , true
159- // We don't break out of the for loop, so that Unicode can override Microsoft.
164+ bestOffset , bestPID , bestScore = offset , pidPsid >> 16 , score
160165 }
161166 }
162- if ! ok {
167+ if bestScore < 0 {
163168 return 0 , 0 , UnsupportedError (name + " encoding" )
164169 }
165170 return bestOffset , bestPID , nil
0 commit comments