Adobe_Font_Metrics

Adobe Font Metrics file creation utility class.

Defined (1)

The class is defined in the following location(s).

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