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