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