| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package php-font-lib |
| 5 |
* @link https://github.com/dompdf/php-font-lib |
| 6 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 7 |
*/ |
| 8 |
namespace WCPOS\Vendor\FontLib; |
| 9 |
|
| 10 |
use WCPOS\Vendor\FontLib\Table\Type\name; |
| 11 |
use WCPOS\Vendor\FontLib\TrueType\File; |
| 12 |
/** |
| 13 |
* Adobe Font Metrics file creation utility class. |
| 14 |
* |
| 15 |
* @package php-font-lib |
| 16 |
*/ |
| 17 |
class AdobeFontMetrics |
| 18 |
{ |
| 19 |
private $f; |
| 20 |
/** |
| 21 |
* @var File |
| 22 |
*/ |
| 23 |
private $font; |
| 24 |
function __construct(File $font) |
| 25 |
{ |
| 26 |
$this->font = $font; |
| 27 |
} |
| 28 |
function write($file, $encoding = null) |
| 29 |
{ |
| 30 |
$map_data = array(); |
| 31 |
if ($encoding) { |
| 32 |
$encoding = \preg_replace("/[^a-z0-9-_]/", "", $encoding); |
| 33 |
$map_file = \dirname(__FILE__) . "/../../maps/{$encoding}.map"; |
| 34 |
if (!\file_exists($map_file)) { |
| 35 |
throw new \Exception("Unknown encoding ({$encoding})"); |
| 36 |
} |
| 37 |
$map = new EncodingMap($map_file); |
| 38 |
$map_data = $map->parse(); |
| 39 |
} |
| 40 |
$this->f = \fopen($file, "w+"); |
| 41 |
$font = $this->font; |
| 42 |
$this->startSection("FontMetrics", 4.1); |
| 43 |
$this->addPair("Notice", "Converted by PHP-font-lib"); |
| 44 |
$this->addPair("Comment", "https://github.com/dompdf/php-font-lib"); |
| 45 |
$encoding_scheme = $encoding ? $encoding : "FontSpecific"; |
| 46 |
$this->addPair("EncodingScheme", $encoding_scheme); |
| 47 |
$records = $font->getData("name", "records"); |
| 48 |
foreach ($records as $id => $record) { |
| 49 |
if (!isset(name::$nameIdCodes[$id]) || \preg_match("/[\r\n]/", $record->string)) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
$this->addPair(name::$nameIdCodes[$id], $record->string); |
| 53 |
} |
| 54 |
$os2 = $font->getData("OS/2"); |
| 55 |
$this->addPair("Weight", $os2["usWeightClass"] > 400 ? "Bold" : "Medium"); |
| 56 |
$post = $font->getData("post"); |
| 57 |
$this->addPair("ItalicAngle", $post["italicAngle"]); |
| 58 |
$this->addPair("IsFixedPitch", $post["isFixedPitch"] ? "true" : "false"); |
| 59 |
$this->addPair("UnderlineThickness", $font->normalizeFUnit($post["underlineThickness"])); |
| 60 |
$this->addPair("UnderlinePosition", $font->normalizeFUnit($post["underlinePosition"])); |
| 61 |
$hhea = $font->getData("hhea"); |
| 62 |
if (isset($hhea["ascent"])) { |
| 63 |
$this->addPair("FontHeightOffset", $font->normalizeFUnit($hhea["lineGap"])); |
| 64 |
} else { |
| 65 |
$this->addPair("FontHeightOffset", $font->normalizeFUnit($os2["typoLineGap"])); |
| 66 |
} |
| 67 |
$glyf = $font->getData("glyf"); |
| 68 |
$glyphIndexArray = $font->getUnicodeCharMap(); |
| 69 |
$hasGlyphs = $glyf instanceof glyf && \is_array($glyphIndexArray); |
| 70 |
// capHeight is based on capital H |
| 71 |
if ($hasGlyphs && \array_key_exists(72, $glyphIndexArray)) { |
| 72 |
$upperH = $glyf[$glyphIndexArray[72]]; |
| 73 |
$upperH->parseData(); |
| 74 |
$this->addPair("CapHeight", $font->normalizeFUnit($upperH->yMax)); |
| 75 |
} |
| 76 |
// xHeight is based on lowercase x |
| 77 |
if ($hasGlyphs && \array_key_exists(120, $glyphIndexArray)) { |
| 78 |
$lowerX = $glyf[$glyphIndexArray[120]]; |
| 79 |
$lowerX->parseData(); |
| 80 |
$this->addPair("XHeight", $font->normalizeFUnit($lowerX->yMax)); |
| 81 |
} |
| 82 |
// ascender is based on lowercase d |
| 83 |
if ($hasGlyphs && \array_key_exists(100, $glyphIndexArray)) { |
| 84 |
$lowerD = $glyf[$glyphIndexArray[100]]; |
| 85 |
$lowerD->parseData(); |
| 86 |
$this->addPair("Ascender", $font->normalizeFUnit($lowerD->yMax)); |
| 87 |
} elseif (isset($hhea["ascent"])) { |
| 88 |
$this->addPair("Ascender", $font->normalizeFUnit($hhea["ascent"])); |
| 89 |
} else { |
| 90 |
$this->addPair("Ascender", $font->normalizeFUnit($os2["typoAscender"])); |
| 91 |
} |
| 92 |
// descender is based on lowercase p |
| 93 |
if ($hasGlyphs && \array_key_exists(112, $glyphIndexArray)) { |
| 94 |
$lowerP = $glyf[$glyphIndexArray[112]]; |
| 95 |
$lowerP->parseData(); |
| 96 |
$this->addPair("Descender", $font->normalizeFUnit($lowerP->yMin)); |
| 97 |
} elseif (isset($hhea["descent"])) { |
| 98 |
$this->addPair("Descender", $font->normalizeFUnit($hhea["descent"])); |
| 99 |
} else { |
| 100 |
$this->addPair("Descender", -\abs($font->normalizeFUnit($os2["typoDescender"]))); |
| 101 |
} |
| 102 |
$head = $font->getData("head"); |
| 103 |
$this->addArray("FontBBox", array($font->normalizeFUnit($head["xMin"]), $font->normalizeFUnit($head["yMin"]), $font->normalizeFUnit($head["xMax"]), $font->normalizeFUnit($head["yMax"]))); |
| 104 |
if ($glyphIndexArray) { |
| 105 |
$hmtx = $font->getData("hmtx"); |
| 106 |
$names = $font->getData("post", "names"); |
| 107 |
$this->startSection("CharMetrics", \count($hmtx)); |
| 108 |
if ($encoding) { |
| 109 |
foreach ($map_data as $code => $value) { |
| 110 |
list($c, $name) = $value; |
| 111 |
if (!isset($glyphIndexArray[$c])) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
$g = $glyphIndexArray[$c]; |
| 115 |
if (!isset($hmtx[$g])) { |
| 116 |
$hmtx[$g] = $hmtx[0]; |
| 117 |
} |
| 118 |
$this->addMetric(array("C" => $code > 255 ? -1 : $code, "WX" => $font->normalizeFUnit($hmtx[$g][0]), "N" => $name)); |
| 119 |
} |
| 120 |
} else { |
| 121 |
foreach ($glyphIndexArray as $c => $g) { |
| 122 |
if (!isset($hmtx[$g])) { |
| 123 |
$hmtx[$g] = $hmtx[0]; |
| 124 |
} |
| 125 |
$this->addMetric(array("U" => $c, "WX" => $font->normalizeFUnit($hmtx[$g][0]), "N" => isset($names[$g]) ? $names[$g] : \sprintf("uni%04x", $c), "G" => $g)); |
| 126 |
} |
| 127 |
} |
| 128 |
$this->endSection("CharMetrics"); |
| 129 |
$kern = $font->getData("kern", "subtable"); |
| 130 |
$tree = \is_array($kern) ? $kern["tree"] : null; |
| 131 |
if (!$encoding && \is_array($tree)) { |
| 132 |
$this->startSection("KernData"); |
| 133 |
$this->startSection("KernPairs", \count($tree, \COUNT_RECURSIVE) - \count($tree)); |
| 134 |
foreach ($tree as $left => $values) { |
| 135 |
if (!\is_array($values)) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
if (!isset($glyphIndexArray[$left])) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
$left_gid = $glyphIndexArray[$left]; |
| 142 |
if (!isset($names[$left_gid])) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
$left_name = $names[$left_gid]; |
| 146 |
$this->addLine(""); |
| 147 |
foreach ($values as $right => $value) { |
| 148 |
if (!isset($glyphIndexArray[$right])) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
$right_gid = $glyphIndexArray[$right]; |
| 152 |
if (!isset($names[$right_gid])) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
$right_name = $names[$right_gid]; |
| 156 |
$this->addPair("KPX", "{$left_name} {$right_name} {$value}"); |
| 157 |
} |
| 158 |
} |
| 159 |
$this->endSection("KernPairs"); |
| 160 |
$this->endSection("KernData"); |
| 161 |
} |
| 162 |
} |
| 163 |
$this->endSection("FontMetrics"); |
| 164 |
} |
| 165 |
function addLine($line) |
| 166 |
{ |
| 167 |
\fwrite($this->f, "{$line}\n"); |
| 168 |
} |
| 169 |
function addPair($key, $value) |
| 170 |
{ |
| 171 |
$this->addLine("{$key} {$value}"); |
| 172 |
} |
| 173 |
function addArray($key, $array) |
| 174 |
{ |
| 175 |
$this->addLine("{$key} " . \implode(" ", $array)); |
| 176 |
} |
| 177 |
function addMetric($data) |
| 178 |
{ |
| 179 |
$array = array(); |
| 180 |
foreach ($data as $key => $value) { |
| 181 |
$array[] = "{$key} {$value}"; |
| 182 |
} |
| 183 |
$this->addLine(\implode(" ; ", $array)); |
| 184 |
} |
| 185 |
function startSection($name, $value = "") |
| 186 |
{ |
| 187 |
$this->addLine("Start{$name} {$value}"); |
| 188 |
} |
| 189 |
function endSection($name) |
| 190 |
{ |
| 191 |
$this->addLine("End{$name}"); |
| 192 |
} |
| 193 |
} |
| 194 |
|