1<?php
2
3namespace Mpdf;
4
5class TTFontFileAnalysis extends TTFontFile
6{
7
8	// Used to get font information from files in directory
9	function extractCoreInfo($file, $TTCfontID = 0)
10	{
11		$this->filename = $file;
12		$this->fh = fopen($file, 'rb');
13		if (!$this->fh) {
14			throw new \Mpdf\MpdfException('ERROR - Can\'t open file ' . $file);
15		}
16		$this->_pos = 0;
17		$this->charWidths = '';
18		$this->glyphPos = [];
19		$this->charToGlyph = [];
20		$this->tables = [];
21		$this->otables = [];
22		$this->ascent = 0;
23		$this->descent = 0;
24		$this->numTTCFonts = 0;
25		$this->TTCFonts = [];
26		$this->version = $version = $this->read_ulong();
27		$this->panose = []; // mPDF 5.0
28
29		if ($version == 0x4F54544F) {
30			throw new \Mpdf\Exception\FontException(sprintf('Fonts with postscript outlines are not supported (%s)', $file));
31		}
32
33		if ($version == 0x74746366) {
34			if ($TTCfontID > 0) {
35				$this->version = $version = $this->read_ulong(); // TTC Header version now
36				if (!in_array($version, [0x00010000, 0x00020000])) {
37					throw new \Mpdf\MpdfException("ERROR - NOT ADDED as Error parsing TrueType Collection: version=" . $version . " - " . $file);
38				}
39			} else {
40				throw new \Mpdf\MpdfException("ERROR - Error parsing TrueType Collection - " . $file);
41			}
42			$this->numTTCFonts = $this->read_ulong();
43			for ($i = 1; $i <= $this->numTTCFonts; $i++) {
44				$this->TTCFonts[$i]['offset'] = $this->read_ulong();
45			}
46			$this->seek($this->TTCFonts[$TTCfontID]['offset']);
47			$this->version = $version = $this->read_ulong(); // TTFont version again now
48			$this->readTableDirectory(false);
49		} else {
50			if (!in_array($version, [0x00010000, 0x74727565])) {
51				throw new \Mpdf\MpdfException("ERROR - NOT ADDED as Not a TrueType font: version=" . $version . " - " . $file);
52			}
53			$this->readTableDirectory(false);
54		}
55
56		/* Included for testing...
57		  $cmap_offset = $this->seek_table("cmap");
58		  $this->skip(2);
59		  $cmapTableCount = $this->read_ushort();
60		  $unicode_cmap_offset = 0;
61		  for ($i=0;$i<$cmapTableCount;$i++) {
62		  $x[$i]['platformId'] = $this->read_ushort();
63		  $x[$i]['encodingId'] = $this->read_ushort();
64		  $x[$i]['offset'] = $this->read_ulong();
65		  $save_pos = $this->_pos;
66		  $x[$i]['format'] = $this->get_ushort($cmap_offset + $x[$i]['offset'] );
67		  $this->seek($save_pos );
68		  }
69		  print_r($x); exit;
70		 */
71		///////////////////////////////////
72		// name - Naming table
73		///////////////////////////////////
74
75		/* Test purposes - displays table of names
76		  $name_offset = $this->seek_table("name");
77		  $format = $this->read_ushort();
78		  if ($format != 0 && $format != 1)	// mPDF 5.3.73
79		  die("Unknown name table format ".$format);
80		  $numRecords = $this->read_ushort();
81		  $string_data_offset = $name_offset + $this->read_ushort();
82		  for ($i=0;$i<$numRecords; $i++) {
83		  $x[$i]['platformId'] = $this->read_ushort();
84		  $x[$i]['encodingId'] = $this->read_ushort();
85		  $x[$i]['languageId'] = $this->read_ushort();
86		  $x[$i]['nameId'] = $this->read_ushort();
87		  $x[$i]['length'] = $this->read_ushort();
88		  $x[$i]['offset'] = $this->read_ushort();
89
90		  $N = '';
91		  if ($x[$i]['platformId'] == 1 && $x[$i]['encodingId'] == 0 && $x[$i]['languageId'] == 0) { // Roman
92		  $opos = $this->_pos;
93		  $N = $this->get_chunk($string_data_offset + $x[$i]['offset'] , $x[$i]['length'] );
94		  $this->_pos = $opos;
95		  $this->seek($opos);
96		  }
97		  else { 	// Unicode
98		  $opos = $this->_pos;
99		  $this->seek($string_data_offset + $x[$i]['offset'] );
100		  $length = $x[$i]['length'] ;
101		  if ($length % 2 != 0)
102		  $length -= 1;
103		  //		die("PostScript name is UTF-16BE string of odd length");
104		  $length /= 2;
105		  $N = '';
106		  while ($length > 0) {
107		  $char = $this->read_ushort();
108		  $N .= (chr($char));
109		  $length -= 1;
110		  }
111		  $this->_pos = $opos;
112		  $this->seek($opos);
113		  }
114		  $x[$i]['names'][$nameId] = $N;
115		  }
116		  print_r($x); exit;
117		 */
118
119		$name_offset = $this->seek_table("name");
120		$format = $this->read_ushort();
121		if ($format != 0 && $format != 1) { // mPDF 5.3.73
122			throw new \Mpdf\MpdfException("ERROR - NOT ADDED as Unknown name table format " . $format . " - " . $file);
123		}
124		$numRecords = $this->read_ushort();
125		$string_data_offset = $name_offset + $this->read_ushort();
126		$names = [1 => '', 2 => '', 3 => '', 4 => '', 6 => ''];
127		$K = array_keys($names);
128		$nameCount = count($names);
129		for ($i = 0; $i < $numRecords; $i++) {
130			$platformId = $this->read_ushort();
131			$encodingId = $this->read_ushort();
132			$languageId = $this->read_ushort();
133			$nameId = $this->read_ushort();
134			$length = $this->read_ushort();
135			$offset = $this->read_ushort();
136			if (!in_array($nameId, $K)) {
137				continue;
138			}
139			$N = '';
140			if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name
141				$opos = $this->_pos;
142				$this->seek($string_data_offset + $offset);
143				if ($length % 2 != 0) {
144					$length += 1;
145				}
146				$length /= 2;
147				$N = '';
148				while ($length > 0) {
149					$char = $this->read_ushort();
150					$N .= (chr($char));
151					$length -= 1;
152				}
153				$this->_pos = $opos;
154				$this->seek($opos);
155			} elseif ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name
156				$opos = $this->_pos;
157				$N = $this->get_chunk($string_data_offset + $offset, $length);
158				$this->_pos = $opos;
159				$this->seek($opos);
160			}
161			if ($N && $names[$nameId] == '') {
162				$names[$nameId] = $N;
163				$nameCount -= 1;
164				if ($nameCount == 0) {
165					break;
166				}
167			}
168		}
169		if ($names[6]) {
170			$psName = preg_replace('/ /', '-', $names[6]);
171		} elseif ($names[4]) {
172			$psName = preg_replace('/ /', '-', $names[4]);
173		} elseif ($names[1]) {
174			$psName = preg_replace('/ /', '-', $names[1]);
175		} else {
176			$psName = '';
177		}
178		if (!$names[1] && !$psName) {
179			throw new \Mpdf\MpdfException("ERROR - NOT ADDED as Could not find valid font name - " . $file);
180		}
181		$this->name = $psName;
182		if ($names[1]) {
183			$this->familyName = $names[1];
184		} else {
185			$this->familyName = $psName;
186		}
187		if ($names[2]) {
188			$this->styleName = $names[2];
189		} else {
190			$this->styleName = 'Regular';
191		}
192
193		///////////////////////////////////
194		// head - Font header table
195		///////////////////////////////////
196		$this->seek_table("head");
197		$ver_maj = $this->read_ushort();
198		$ver_min = $this->read_ushort();
199		if ($ver_maj != 1) {
200			throw new \Mpdf\MpdfException('ERROR - NOT ADDED as Unknown head table version ' . $ver_maj . '.' . $ver_min . " - " . $file);
201		}
202		$this->fontRevision = $this->read_ushort() . $this->read_ushort();
203		$this->skip(4);
204		$magic = $this->read_ulong();
205		if ($magic != 0x5F0F3CF5) {
206			throw new \Mpdf\MpdfException('ERROR - NOT ADDED as Invalid head table magic ' . $magic . " - " . $file);
207		}
208		$this->skip(2);
209		$this->unitsPerEm = $unitsPerEm = $this->read_ushort();
210		$scale = 1000 / $unitsPerEm;
211		$this->skip(24);
212		$macStyle = $this->read_short();
213		$this->skip(4);
214		$indexLocFormat = $this->read_short();
215
216		///////////////////////////////////
217		// OS/2 - OS/2 and Windows metrics table
218		///////////////////////////////////
219		$sFamily = '';
220		$panose = '';
221		$fsSelection = '';
222		if (isset($this->tables["OS/2"])) {
223			$this->seek_table("OS/2");
224			$this->skip(30);
225			$sF = $this->read_short();
226			$sFamily = ($sF >> 8);
227			$this->_pos += 10;  //PANOSE = 10 byte length
228			$panose = fread($this->fh, 10);
229			$this->panose = [];
230			for ($p = 0; $p < strlen($panose); $p++) {
231				$this->panose[] = ord($panose[$p]);
232			}
233			$this->skip(20);
234			$fsSelection = $this->read_short();
235		}
236
237		///////////////////////////////////
238		// post - PostScript table
239		///////////////////////////////////
240		$this->seek_table("post");
241		$this->skip(4);
242		$this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0;
243		$this->skip(4);
244		$isFixedPitch = $this->read_ulong();
245
246
247
248		///////////////////////////////////
249		// cmap - Character to glyph index mapping table
250		///////////////////////////////////
251		$cmap_offset = $this->seek_table("cmap");
252		$this->skip(2);
253		$cmapTableCount = $this->read_ushort();
254		$unicode_cmap_offset = 0;
255		for ($i = 0; $i < $cmapTableCount; $i++) {
256			$platformID = $this->read_ushort();
257			$encodingID = $this->read_ushort();
258			$offset = $this->read_ulong();
259			$save_pos = $this->_pos;
260			if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode
261				$format = $this->get_ushort($cmap_offset + $offset);
262				if ($format == 4) {
263					if (!$unicode_cmap_offset) {
264						$unicode_cmap_offset = $cmap_offset + $offset;
265					}
266				}
267			} elseif ((($platformID == 3 && $encodingID == 10) || $platformID == 0)) { // Microsoft, Unicode Format 12 table HKCS
268				$format = $this->get_ushort($cmap_offset + $offset);
269				if ($format == 12) {
270					$unicode_cmap_offset = $cmap_offset + $offset;
271					break;
272				}
273			}
274			$this->seek($save_pos);
275		}
276
277		if (!$unicode_cmap_offset) {
278			throw new \Mpdf\MpdfException('ERROR - Font (' . $this->filename . ') NOT ADDED as it is not Unicode encoded, and cannot be used by mPDF');
279		}
280
281		$rtl = false;
282		$indic = false;
283		$cjk = false;
284		$sip = false;
285		$smp = false;
286		$pua = false;
287		$puaag = false;
288		$glyphToChar = [];
289		$unAGlyphs = '';
290		// Format 12 CMAP does characters above Unicode BMP i.e. some HKCS characters U+20000 and above
291		if ($format == 12) {
292			$this->seek($unicode_cmap_offset + 4);
293			$length = $this->read_ulong();
294			$limit = $unicode_cmap_offset + $length;
295			$this->skip(4);
296			$nGroups = $this->read_ulong();
297			for ($i = 0; $i < $nGroups; $i++) {
298				$startCharCode = $this->read_ulong();
299				$endCharCode = $this->read_ulong();
300				$startGlyphCode = $this->read_ulong();
301				if (($endCharCode > 0x20000 && $endCharCode < 0x2A6DF) || ($endCharCode > 0x2F800 && $endCharCode < 0x2FA1F)) {
302					$sip = true;
303				}
304				if ($endCharCode > 0x10000 && $endCharCode < 0x1FFFF) {
305					$smp = true;
306				}
307				if (($endCharCode > 0x0590 && $endCharCode < 0x077F) || ($endCharCode > 0xFE70 && $endCharCode < 0xFEFF) || ($endCharCode > 0xFB50 && $endCharCode < 0xFDFF)) {
308					$rtl = true;
309				}
310				if ($endCharCode > 0x0900 && $endCharCode < 0x0DFF) {
311					$indic = true;
312				}
313				if ($endCharCode > 0xE000 && $endCharCode < 0xF8FF) {
314					$pua = true;
315					if ($endCharCode > 0xF500 && $endCharCode < 0xF7FF) {
316						$puaag = true;
317					}
318				}
319				if (($endCharCode > 0x2E80 && $endCharCode < 0x4DC0) || ($endCharCode > 0x4E00 && $endCharCode < 0xA4CF) || ($endCharCode > 0xAC00 && $endCharCode < 0xD7AF) || ($endCharCode > 0xF900 && $endCharCode < 0xFAFF) || ($endCharCode > 0xFE30 && $endCharCode < 0xFE4F)) {
320					$cjk = true;
321				}
322
323				$offset = 0;
324				// Get each glyphToChar - only point if going to analyse un-mapped Arabic Glyphs
325				if (isset($this->tables['post'])) {
326					for ($unichar = $startCharCode; $unichar <= $endCharCode; $unichar++) {
327						$glyph = $startGlyphCode + $offset;
328						$offset++;
329						$glyphToChar[$glyph][] = $unichar;
330					}
331				}
332			}
333		} else { // Format 4 CMap
334			$this->seek($unicode_cmap_offset + 2);
335			$length = $this->read_ushort();
336			$limit = $unicode_cmap_offset + $length;
337			$this->skip(2);
338
339			$segCount = $this->read_ushort() / 2;
340			$this->skip(6);
341			$endCount = [];
342			for ($i = 0; $i < $segCount; $i++) {
343				$endCount[] = $this->read_ushort();
344			}
345			$this->skip(2);
346			$startCount = [];
347			for ($i = 0; $i < $segCount; $i++) {
348				$startCount[] = $this->read_ushort();
349			}
350			$idDelta = [];
351			for ($i = 0; $i < $segCount; $i++) {
352				$idDelta[] = $this->read_short();
353			}
354			$idRangeOffset_start = $this->_pos;
355			$idRangeOffset = [];
356			for ($i = 0; $i < $segCount; $i++) {
357				$idRangeOffset[] = $this->read_ushort();
358			}
359
360			for ($n = 0; $n < $segCount; $n++) {
361				if (($endCount[$n] > 0x0590 && $endCount[$n] < 0x077F) || ($endCount[$n] > 0xFE70 && $endCount[$n] < 0xFEFF) || ($endCount[$n] > 0xFB50 && $endCount[$n] < 0xFDFF)) {
362					$rtl = true;
363				}
364				if ($endCount[$n] > 0x0900 && $endCount[$n] < 0x0DFF) {
365					$indic = true;
366				}
367				if (($endCount[$n] > 0x2E80 && $endCount[$n] < 0x4DC0) || ($endCount[$n] > 0x4E00 && $endCount[$n] < 0xA4CF) || ($endCount[$n] > 0xAC00 && $endCount[$n] < 0xD7AF) || ($endCount[$n] > 0xF900 && $endCount[$n] < 0xFAFF) || ($endCount[$n] > 0xFE30 && $endCount[$n] < 0xFE4F)) {
368					$cjk = true;
369				}
370				if ($endCount[$n] > 0xE000 && $endCount[$n] < 0xF8FF) {
371					$pua = true;
372					if ($endCount[$n] > 0xF500 && $endCount[$n] < 0xF7FF) {
373						$puaag = true;
374					}
375				}
376				// Get each glyphToChar - only point if going to analyse un-mapped Arabic Glyphs
377				if (isset($this->tables['post'])) {
378					$endpoint = ($endCount[$n] + 1);
379					for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) {
380						if ($idRangeOffset[$n] == 0) {
381							$glyph = ($unichar + $idDelta[$n]) & 0xFFFF;
382						} else {
383							$offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n];
384							$offset = $idRangeOffset_start + 2 * $n + $offset;
385							if ($offset >= $limit) {
386								$glyph = 0;
387							} else {
388								$glyph = $this->get_ushort($offset);
389								if ($glyph != 0) {
390									$glyph = ($glyph + $idDelta[$n]) & 0xFFFF;
391								}
392							}
393						}
394						$glyphToChar[$glyph][] = $unichar;
395					}
396				}
397			}
398		}
399
400
401		$bold = false;
402		$italic = false;
403		$ftype = '';
404		if ($macStyle & (1 << 0)) {
405			$bold = true;
406		} // bit 0 bold
407		elseif ($fsSelection & (1 << 5)) {
408			$bold = true;
409		} // 5 	BOLD 	Characters are emboldened
410
411		if ($macStyle & (1 << 1)) {
412			$italic = true;
413		} // bit 1 italic
414		elseif ($fsSelection & (1 << 0)) {
415			$italic = true;
416		} // 0 	ITALIC 	Font contains Italic characters, otherwise they are upright
417		elseif ($this->italicAngle <> 0) {
418			$italic = true;
419		}
420
421		if ($isFixedPitch) {
422			$ftype = 'mono';
423		} elseif ($sFamily > 0 && $sFamily < 8) {
424			$ftype = 'serif';
425		} elseif ($sFamily == 8) {
426			$ftype = 'sans';
427		} elseif ($sFamily == 10) {
428			$ftype = 'cursive';
429		}
430		// Use PANOSE
431		if ($panose) {
432			$bFamilyType = ord($panose[0]);
433			if ($bFamilyType == 2) {
434				$bSerifStyle = ord($panose[1]);
435				if (!$ftype) {
436					if ($bSerifStyle > 1 && $bSerifStyle < 11) {
437						$ftype = 'serif';
438					} elseif ($bSerifStyle > 10) {
439						$ftype = 'sans';
440					}
441				}
442				$bProportion = ord($panose[3]);
443				if ($bProportion == 9 || $bProportion == 1) {
444					$ftype = 'mono';
445				} // ==1 i.e. No Fit needed for OCR-a and -b
446			} elseif ($bFamilyType == 3) {
447				$ftype = 'cursive';
448			}
449		}
450
451		fclose($this->fh);
452		return [$this->familyName, $bold, $italic, $ftype, $TTCfontID, $rtl, $indic, $cjk, $sip, $smp, $puaag, $pua, $unAGlyphs];
453	}
454}
455