Skip to content

Commit 4997eb9

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 2a84418 commit 4997eb9

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/FontLib/Table/Type/name.php

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

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+
153159
$names = array();
154160
foreach ($records as $record) {
155161
$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);
158196
$names[$record->nameID] = $record;
159197
}
160198

@@ -184,7 +222,7 @@ protected function _encode() {
184222
}
185223

186224
foreach ($records as $record) {
187-
$str = $record->getUTF16();
225+
$str = $record->stringRaw;
188226
$length += $font->write($str, mb_strlen($str, "8bit"));
189227
}
190228

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)