Açıklama Yok

glyf.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @package php-font-lib
  4. * @link https://github.com/PhenX/php-font-lib
  5. * @author Fabien Ménager <fabien.menager@gmail.com>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. namespace FontLib\Table\Type;
  9. use FontLib\Table\Table;
  10. use FontLib\Glyph\Outline;
  11. use FontLib\Glyph\OutlineSimple;
  12. /**
  13. * `glyf` font table.
  14. *
  15. * @package php-font-lib
  16. * @property Outline[] $data
  17. */
  18. class glyf extends Table {
  19. protected function _parse() {
  20. $font = $this->getFont();
  21. $offset = $font->pos();
  22. $loca = $font->getData("loca");
  23. $real_loca = array_slice($loca, 0, -1); // Not the last dummy loca entry
  24. $data = array();
  25. foreach ($real_loca as $gid => $location) {
  26. $_offset = $offset + $loca[$gid];
  27. $_size = $loca[$gid + 1] - $loca[$gid];
  28. $data[$gid] = Outline::init($this, $_offset, $_size, $font);
  29. }
  30. $this->data = $data;
  31. }
  32. public function getGlyphIDs($gids = array()) {
  33. $glyphIDs = array();
  34. foreach ($gids as $_gid) {
  35. $_glyph = $this->data[$_gid];
  36. $glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
  37. }
  38. return array_unique(array_merge($gids, $glyphIDs));
  39. }
  40. public function toHTML() {
  41. $max = 160;
  42. $font = $this->getFont();
  43. $head = $font->getData("head");
  44. $head_json = json_encode($head);
  45. $os2 = $font->getData("OS/2");
  46. $os2_json = json_encode($os2);
  47. $hmtx = $font->getData("hmtx");
  48. $hmtx_json = json_encode($hmtx);
  49. $names = $font->getData("post", "names");
  50. $glyphIndexArray = array_flip($font->getUnicodeCharMap());
  51. $width = (abs($head["xMin"]) + $head["xMax"]);
  52. $height = (abs($head["yMin"]) + $head["yMax"]);
  53. $ratio = 1;
  54. if ($width > $max || $height > $max) {
  55. $ratio = max($width, $height) / $max;
  56. $width = round($width / $ratio);
  57. $height = round($height / $ratio);
  58. }
  59. $n = 500;
  60. $s = "<h3>" . "Only the first $n simple glyphs are shown (" . count($this->data) . " total)
  61. <div class='glyph-view simple'>Simple glyph</div>
  62. <div class='glyph-view composite'>Composite glyph</div>
  63. Zoom: <input type='range' value='100' max='400' onchange='Glyph.resize(this.value)' />
  64. </h3>
  65. <script>
  66. Glyph.ratio = $ratio;
  67. Glyph.head = $head_json;
  68. Glyph.os2 = $os2_json;
  69. Glyph.hmtx = $hmtx_json;
  70. Glyph.width = $width;
  71. Glyph.height = $height;
  72. </script>";
  73. foreach ($this->data as $g => $glyph) {
  74. if ($n-- <= 0) {
  75. break;
  76. }
  77. $glyph->parseData();
  78. $shape = array(
  79. "SVGContours" => $glyph->getSVGContours(),
  80. "xMin" => $glyph->xMin,
  81. "yMin" => $glyph->yMin,
  82. "xMax" => $glyph->xMax,
  83. "yMax" => $glyph->yMax,
  84. );
  85. $shape_json = json_encode($shape);
  86. $type = ($glyph instanceof OutlineSimple ? "simple" : "composite");
  87. $char = isset($glyphIndexArray[$g]) ? $glyphIndexArray[$g] : 0;
  88. $name = isset($names[$g]) ? $names[$g] : sprintf("uni%04x", $char);
  89. $char = $char ? "&#{$glyphIndexArray[$g]};" : "";
  90. $s .= "<div class='glyph-view $type' id='glyph-$g'>
  91. <span class='glyph-id'>$g</span>
  92. <span class='char'>$char</span>
  93. <span class='char-name'>$name</span>
  94. ";
  95. if ($type == "composite") {
  96. foreach ($glyph->getGlyphIDs() as $_id) {
  97. $s .= "<a href='#glyph-$_id' class='glyph-component-id'>$_id</a> ";
  98. }
  99. }
  100. $s .= "<br />
  101. <canvas width='$width' height='$height' id='glyph-canvas-$g'></canvas>
  102. </div>
  103. <script>Glyph.glyphs.push([$g,$shape_json]);</script>";
  104. }
  105. return $s;
  106. }
  107. protected function _encode() {
  108. $font = $this->getFont();
  109. $subset = $font->getSubset();
  110. $data = $this->data;
  111. $loca = array();
  112. $length = 0;
  113. foreach ($subset as $gid) {
  114. $loca[] = $length;
  115. $length += $data[$gid]->encode();
  116. }
  117. $loca[] = $length; // dummy loca
  118. $font->getTableObject("loca")->data = $loca;
  119. return $length;
  120. }
  121. }