Debian Codegraph @

vendor/dompdf/php-font-lib/src/FontLib/AdobeFontMetrics.php

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