PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.16
Yatra – Travel Booking & Tour Operator Software v3.0.16
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / vendor / phenx / php-font-lib / src / FontLib / AdobeFontMetrics.php

AdobeFontMetrics.php in Yatra – Travel Booking & Tour Operator Software 3.0.16, at vendor/phenx/php-font-lib/src/FontLib/AdobeFontMetrics.php

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