Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add platformID/platformSpecificID/languageID support to getFont* #139

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/FontLib/AdobeFontMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function write($file, $encoding = null) {
$this->addPair("EncodingScheme", $encoding_scheme);

$records = $font->getData("name", "records");
foreach ($records as $id => $record) {
foreach ($records as $uniqueIdentifier => $record) {
$id = end(explode(',', $uniqueIdentifier));
if (!isset(name::$nameIdCodes[$id]) || preg_match("/[\r\n]/", $record->string)) {
continue;
}
Expand Down
18 changes: 18 additions & 0 deletions src/FontLib/BinaryStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BinaryStream {
* @var resource The file pointer
*/
protected $f;
protected $f2;

const uint8 = 1;
const int8 = 2;
Expand Down Expand Up @@ -66,6 +67,12 @@ public function open($filename, $mode = self::modeRead) {
throw new \Exception("Unknown file open mode");
}

if ($this->f !== null && $this->f !== false) {
if ($this->f2 !== null && $this->f2 !== false) {
fclose($this->f2);
}
$this->f2 = $this->f;
}
$this->f = fopen($filename, $mode);

return $this->f != false;
Expand All @@ -75,9 +82,20 @@ public function open($filename, $mode = self::modeRead) {
* Close the internal file pointer
*/
public function close() {
if ($this->f2 !== null && $this->f2 !== false) {
fclose($this->f2);
}
return fclose($this->f) != false;
}

public function revert() {
if ($this->f2 !== null) {
fclose($this->f);
$this->f = $this->f2;
$this->f2 = null;
}
}

/**
* Change the internal file pointer
*
Expand Down
9 changes: 9 additions & 0 deletions src/FontLib/EOT/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public function readUInt32() {
return $uint32 >> 16 & 0x0000FFFF | $uint32 << 16 & 0xFFFF0000;
}


function setData($name, $key, $value) {
if (!$key) {
$this->header->data = $value;
} else {
$this->header->data[$key] = $value;
}
}

/**
* Get font copyright
*
Expand Down
17 changes: 10 additions & 7 deletions src/FontLib/Table/Type/name.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,44 +160,47 @@ protected function _parse() {

$encoding = null;
switch ($record->platformID) {
case 1:
$encoding = mb_detect_encoding($record->stringRaw, array("UTF-8", "ASCII", "GB2312", "GB18030", "GBK", "SJIS", "BIG-5"));
break;
case 3:
switch ($record->platformSpecificID) {
case 2:
if (\array_key_exists("SJIS", $system_encodings)) {
$encoding = "SJIS";
$encoding = mb_detect_encoding($record->stringRaw, array("SJIS", "UTF-16", "UTF-8", "ASCII"));
}
break;
case 3:
if (\array_key_exists("GB18030", $system_encodings)) {
$encoding = "GB18030";
$encoding = mb_detect_encoding($record->stringRaw, array("GB18030", "UTF-16", "UTF-8", "ASCII"));
}
break;
case 4:
if (\array_key_exists("BIG-5", $system_encodings)) {
$encoding = "BIG-5";
$encoding = mb_detect_encoding($record->stringRaw, array("BIG-5", "UTF-16", "UTF-8", "ASCII"));
}
break;
case 5:
if (\array_key_exists("UHC", $system_encodings)) {
$encoding = "UHC";
$encoding = mb_detect_encoding($record->stringRaw, array("UHC", "UTF-16", "UTF-8", "ASCII"));
}
break;
}
break;
}
if ($encoding === null) {
if ($encoding === null || $encoding === false) {
$encoding = "UTF-16";
}

$record->string = mb_convert_encoding($record->stringRaw, "UTF-8", $encoding);

if (strpos($record->string, "\0") !== false) {
$record->string = str_replace("\0", "", $record->string);
}
$names[$record->nameID] = $record;
$names["{$record->platformID},{$record->platformSpecificID},{$record->languageID},{$record->nameID}"] = $record;
}

$data["records"] = $names;

$this->data = $data;
}

Expand Down
38 changes: 22 additions & 16 deletions src/FontLib/TrueType/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ private function uniord (string $c, string $encoding = null) {

function getTable() {
$this->parseTableEntries();

return $this->directory;
}

Expand Down Expand Up @@ -392,7 +391,6 @@ function parseTableEntries() {
if (!empty($this->directory)) {
return;
}

if (empty($this->header->data["numTables"])) {
return;
}
Expand Down Expand Up @@ -472,6 +470,14 @@ public function getData($name, $key = null) {
}
}

public function setData($name, $key, $value) {
if (!$key) {
$this->data[$name]->data = $value;
} else {
$this->data[$name]->data[$key] = $value;
}
}

function addDirectoryEntry(DirectoryEntry $entry) {
$this->directory[$entry->tag] = $entry;
}
Expand Down Expand Up @@ -504,53 +510,53 @@ function getNameTableString($nameID) {
*
* @return string|null
*/
function getFontCopyright() {
return $this->getNameTableString(name::NAME_COPYRIGHT);
function getFontCopyright($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_COPYRIGHT);
}

/**
* Get font name
*
* @return string|null
*/
function getFontName() {
return $this->getNameTableString(name::NAME_NAME);
function getFontName($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_NAME);
}

/**
* Get font subfamily
*
* @return string|null
*/
function getFontSubfamily() {
return $this->getNameTableString(name::NAME_SUBFAMILY);
function getFontSubfamily($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_SUBFAMILY);
}

/**
* Get font subfamily ID
*
* @return string|null
*/
function getFontSubfamilyID() {
return $this->getNameTableString(name::NAME_SUBFAMILY_ID);
function getFontSubfamilyID($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_SUBFAMILY_ID);
}

/**
* Get font full name
*
* @return string|null
*/
function getFontFullName() {
return $this->getNameTableString(name::NAME_FULL_NAME);
function getFontFullName($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_FULL_NAME);
}

/**
* Get font version
*
* @return string|null
*/
function getFontVersion() {
return $this->getNameTableString(name::NAME_VERSION);
function getFontVersion($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_VERSION);
}

/**
Expand All @@ -567,8 +573,8 @@ function getFontWeight() {
*
* @return string|null
*/
function getFontPostscriptName() {
return $this->getNameTableString(name::NAME_POSTSCRIPT_NAME);
function getFontPostscriptName($platformID, $platformSpecificID, $languageID) {
return $this->getNameTableString("{$platformID},{$platformSpecificID},{$languageID}," . name::NAME_POSTSCRIPT_NAME);
}

function reduce() {
Expand Down