1<?php 2 3namespace Mpdf\Fonts; 4 5use Mpdf\TTFontFile; 6 7class MetricsGenerator 8{ 9 10 private $fontCache; 11 12 private $fontDescriptor; 13 14 public function __construct(FontCache $fontCache, $fontDescriptor) 15 { 16 $this->fontCache = $fontCache; 17 $this->fontDescriptor = $fontDescriptor; 18 } 19 20 public function generateMetrics($ttffile, $ttfstat, $fontkey, $TTCfontID, $debugfonts, $BMPonly, $useOTL, $fontUseOTL) 21 { 22 $ttf = new TTFontFile($this->fontCache, $this->fontDescriptor); 23 $ttf->getMetrics($ttffile, $fontkey, $TTCfontID, $debugfonts, $BMPonly, $useOTL); // mPDF 5.7.1 24 25 $font = [ 26 'name' => $this->getFontName($ttf->fullName), 27 'type' => 'TTF', 28 'desc' => [ 29 'CapHeight' => round($ttf->capHeight), 30 'XHeight' => round($ttf->xHeight), 31 'FontBBox' => '[' . round($ttf->bbox[0]) . " " . round($ttf->bbox[1]) . " " . round($ttf->bbox[2]) . " " . round($ttf->bbox[3]) . ']', 32 /* FontBBox from head table */ 33 /* 'MaxWidth' => round($ttf->advanceWidthMax), // AdvanceWidthMax from hhea table NB ArialUnicode MS = 31990 ! */ 34 'Flags' => $ttf->flags, 35 'Ascent' => round($ttf->ascent), 36 'Descent' => round($ttf->descent), 37 'Leading' => round($ttf->lineGap), 38 'ItalicAngle' => $ttf->italicAngle, 39 'StemV' => round($ttf->stemV), 40 'MissingWidth' => round($ttf->defaultWidth) 41 ], 42 'unitsPerEm' => round($ttf->unitsPerEm), 43 'up' => round($ttf->underlinePosition), 44 'ut' => round($ttf->underlineThickness), 45 'strp' => round($ttf->strikeoutPosition), 46 'strs' => round($ttf->strikeoutSize), 47 'ttffile' => $ttffile, 48 'TTCfontID' => $TTCfontID, 49 'originalsize' => $ttfstat['size'] + 0, /* cast ? */ 50 'sip' => ($ttf->sipset) ? true : false, 51 'smp' => ($ttf->smpset) ? true : false, 52 'BMPselected' => ($BMPonly) ? true : false, 53 'fontkey' => $fontkey, 54 'panose' => $this->getPanose($ttf), 55 'haskerninfo' => ($ttf->kerninfo) ? true : false, 56 'haskernGPOS' => ($ttf->haskernGPOS) ? true : false, 57 'hassmallcapsGSUB' => ($ttf->hassmallcapsGSUB) ? true : false, 58 'fontmetrics' => $this->fontDescriptor, 59 'useOTL' => ($fontUseOTL) ? $fontUseOTL : 0, 60 'rtlPUAstr' => $ttf->rtlPUAstr, 61 'GSUBScriptLang' => $ttf->GSUBScriptLang, 62 'GSUBFeatures' => $ttf->GSUBFeatures, 63 'GSUBLookups' => $ttf->GSUBLookups, 64 'GPOSScriptLang' => $ttf->GPOSScriptLang, 65 'GPOSFeatures' => $ttf->GPOSFeatures, 66 'GPOSLookups' => $ttf->GPOSLookups, 67 'kerninfo' => $ttf->kerninfo, 68 ]; 69 70 $this->fontCache->jsonWrite($fontkey . '.mtx.json', $font); 71 $this->fontCache->binaryWrite($fontkey . '.cw.dat', $ttf->charWidths); 72 $this->fontCache->binaryWrite($fontkey . '.gid.dat', $ttf->glyphIDtoUni); 73 74 if ($this->fontCache->has($fontkey . '.cgm')) { 75 $this->fontCache->remove($fontkey . '.cgm'); 76 } 77 78 if ($this->fontCache->has($fontkey . '.z')) { 79 $this->fontCache->remove($fontkey . '.z'); 80 } 81 82 if ($this->fontCache->jsonHas($fontkey . '.cw127.json')) { 83 $this->fontCache->jsonRemove($fontkey . '.cw127.json'); 84 } 85 86 if ($this->fontCache->has($fontkey . '.cw')) { 87 $this->fontCache->remove($fontkey . '.cw'); 88 } 89 90 unset($ttf); 91 } 92 93 protected function getFontName($fullName) 94 { 95 return preg_replace('/[ ()]/', '', $fullName); 96 } 97 98 protected function getPanose($ttf) 99 { 100 $panose = ''; 101 if (count($ttf->panose)) { 102 $panoseArray = array_merge([$ttf->sFamilyClass, $ttf->sFamilySubClass], $ttf->panose); 103 foreach ($panoseArray as $value) { 104 $panose .= ' ' . dechex($value); 105 } 106 } 107 108 return $panose; 109 } 110} 111