@@ -150,11 +150,49 @@ protected function _parse() {
150
150
$ records [] = $ record ;
151
151
}
152
152
153
+ $ system_encodings = mb_list_encodings ();
154
+ foreach ($ system_encodings as $ enc ) {
155
+ $ system_encodings = array_merge ($ system_encodings , @mb_encoding_aliases ($ enc ));
156
+ }
157
+ $ system_encodings = array_change_key_case (array_fill_keys ($ system_encodings , true ), CASE_UPPER );
158
+
153
159
$ names = array ();
154
160
foreach ($ records as $ record ) {
155
161
$ font ->seek ($ tableOffset + $ data ["stringOffset " ] + $ record ->offset );
156
- $ s = $ font ->read ($ record ->length );
157
- $ record ->string = Font::UTF16ToUTF8 ($ s );
162
+ $ record ->stringRaw = $ font ->read ($ record ->length );
163
+
164
+ $ encoding = "UTF-16 " ;
165
+ switch ($ record ->platformID ) {
166
+ case 3 :
167
+ switch ($ record ->platformSpecificID ) {
168
+ case 2 :
169
+ if (\array_key_exists ("SJIS " , $ system_encodings )) {
170
+ $ encoding = mb_detect_encoding ($ record ->stringRaw , ["SJIS " , "UTF-16 " ], true );
171
+ }
172
+ break ;
173
+ case 3 :
174
+ if (\array_key_exists ("GB18030 " , $ system_encodings )) {
175
+ $ encoding = mb_detect_encoding ($ record ->stringRaw , ["GB18030 " , "UTF-16 " ], true );
176
+ }
177
+ break ;
178
+ case 4 :
179
+ if (\array_key_exists ("BIG-5 " , $ system_encodings )) {
180
+ $ encoding = mb_detect_encoding ($ record ->stringRaw , ["BIG-5 " , "UTF-16 " ], true );
181
+ }
182
+ break ;
183
+ case 5 :
184
+ if (\array_key_exists ("UHC " , $ system_encodings )) {
185
+ $ encoding = mb_detect_encoding ($ record ->stringRaw , ["UHC " , "UTF-16 " ], true );
186
+ }
187
+ break ;
188
+ }
189
+ break ;
190
+ }
191
+ if ($ encoding === false ) {
192
+ $ encoding = "UTF-16 " ;
193
+ }
194
+
195
+ $ record ->string = mb_convert_encoding ($ record ->stringRaw , "UTF-8 " , $ encoding );
158
196
$ names [$ record ->nameID ] = $ record ;
159
197
}
160
198
@@ -184,7 +222,7 @@ protected function _encode() {
184
222
}
185
223
186
224
foreach ($ records as $ record ) {
187
- $ str = $ record ->getUTF16 () ;
225
+ $ str = $ record ->stringRaw ;
188
226
$ length += $ font ->write ($ str , mb_strlen ($ str , "8bit " ));
189
227
}
190
228
0 commit comments