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
9 namespace FontLib\Table\Type;
11 use FontLib\Table\Table;
12 use FontLib\Glyph\Outline;
13 use FontLib\Glyph\OutlineSimple;
18 * @package php-font-lib
19 * @property Outline[] $data
21 class glyf extends Table {
22 protected function _parse() {
23 $font = $this->getFont();
24 $offset = $font->pos();
26 $loca = $font->getData("loca");
27 $real_loca = array_slice($loca, 0, -1); // Not the last dummy loca entry
31 foreach ($real_loca as $gid => $location) {
32 $_offset = $offset + $loca[$gid];
33 $_size = $loca[$gid + 1] - $loca[$gid];
34 $data[$gid] = Outline::init($this, $_offset, $_size, $font);
40 public function getGlyphIDs($gids = array()) {
43 foreach ($gids as $_gid) {
44 $_glyph = $this->data[$_gid];
45 $glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
48 return array_unique(array_merge($gids, $glyphIDs));
51 public function toHTML() {
53 $font = $this->getFont();
55 $head = $font->getData("head");
56 $head_json = json_encode($head);
58 $os2 = $font->getData("OS/2");
59 $os2_json = json_encode($os2);
61 $hmtx = $font->getData("hmtx");
62 $hmtx_json = json_encode($hmtx);
64 $names = $font->getData("post", "names");
65 $glyphIndexArray = array_flip($font->getUnicodeCharMap());
67 $width = (abs($head["xMin"]) + $head["xMax"]);
68 $height = (abs($head["yMin"]) + $head["yMax"]);
71 if ($width > $max || $height > $max) {
72 $ratio = max($width, $height) / $max;
73 $width = round($width / $ratio);
74 $height = round($height / $ratio);
79 $s = "<h3>" . "Only the first $n simple glyphs are shown (" . count($this->data) . " total)
80 <div class='glyph-view simple'>Simple glyph</div>
81 <div class='glyph-view composite'>Composite glyph</div>
82 Zoom: <input type='range' value='100' max='400' onchange='Glyph.resize(this.value)' />
86 Glyph.head = $head_json;
87 Glyph.os2 = $os2_json;
88 Glyph.hmtx = $hmtx_json;
90 Glyph.height = $height;
93 foreach ($this->data as $g => $glyph) {
101 "SVGContours" => $glyph->getSVGContours(),
102 "xMin" => $glyph->xMin,
103 "yMin" => $glyph->yMin,
104 "xMax" => $glyph->xMax,
105 "yMax" => $glyph->yMax,
107 $shape_json = json_encode($shape);
109 $type = ($glyph instanceof OutlineSimple ? "simple" : "composite");
110 $char = isset($glyphIndexArray[$g]) ? $glyphIndexArray[$g] : 0;
111 $name = isset($names[$g]) ? $names[$g] : sprintf("uni%04x", $char);
112 $char = $char ? "&#{$glyphIndexArray[$g]};" : "";
114 $s .= "<div class='glyph-view $type' id='glyph-$g'>
115 <span class='glyph-id'>$g</span>
116 <span class='char'>$char</span>
117 <span class='char-name'>$name</span>
120 if ($type == "composite") {
121 foreach ($glyph->getGlyphIDs() as $_id) {
122 $s .= "<a href='#glyph-$_id' class='glyph-component-id'>$_id</a> ";
127 <canvas width='$width' height='$height' id='glyph-canvas-$g'></canvas>
129 <script>Glyph.glyphs.push([$g,$shape_json]);</script>";
136 protected function _encode() {
137 $font = $this->getFont();
138 $subset = $font->getSubset();
144 foreach ($subset as $gid) {
146 $length += $data[$gid]->encode();
149 $loca[] = $length; // dummy loca
150 $font->getTableObject("loca")->data = $loca;