Skip to content

Commit 89d295f

Browse files
committed
Improve name encoding support for platform 3
Most name strings should be encoded with UTF-16BE per the spec, but there are situations where other encodings are required or acceptable. This change only addresses a subset of potential encodings. fixes #70
1 parent c5f7810 commit 89d295f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/FontLib/Table/Type/name.php

+38-3
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,46 @@ protected function _parse() {
150150
$records[] = $record;
151151
}
152152

153+
$system_encodings = mb_list_encodings();
154+
$system_encodings = array_change_key_case(array_fill_keys($system_encodings, true), CASE_UPPER);
155+
153156
$names = array();
154157
foreach ($records as $record) {
155158
$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);
158193
$names[$record->nameID] = $record;
159194
}
160195

@@ -184,7 +219,7 @@ protected function _encode() {
184219
}
185220

186221
foreach ($records as $record) {
187-
$str = $record->getUTF16();
222+
$str = $record->stringRaw;
188223
$length += $font->write($str, mb_strlen($str, "8bit"));
189224
}
190225

src/FontLib/Table/Type/nameRecord.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class nameRecord extends BinaryStream {
2323
public $length;
2424
public $offset;
2525
public $string;
26+
public $stringRaw;
2627

2728
public static $format = array(
2829
"platformID" => self::uint16,

0 commit comments

Comments
 (0)