Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / phenx / php-font-lib / src / FontLib / Table / Type / glyf.php
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
9 namespace FontLib\Table\Type;
10
11 use FontLib\Table\Table;
12 use FontLib\Glyph\Outline;
13 use FontLib\Glyph\OutlineSimple;
14
15 /**
16  * `glyf` font table.
17  *
18  * @package php-font-lib
19  * @property Outline[] $data
20  */
21 class glyf extends Table {
22   protected function _parse() {
23     $font   = $this->getFont();
24     $offset = $font->pos();
25
26     $loca      = $font->getData("loca");
27     $real_loca = array_slice($loca, 0, -1); // Not the last dummy loca entry
28
29     $data = array();
30
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);
35     }
36
37     $this->data = $data;
38   }
39
40   public function getGlyphIDs($gids = array()) {
41     $glyphIDs = array();
42
43     foreach ($gids as $_gid) {
44       $_glyph   = $this->data[$_gid];
45       $glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
46     }
47
48     return array_unique(array_merge($gids, $glyphIDs));
49   }
50
51   public function toHTML() {
52     $max  = 160;
53     $font = $this->getFont();
54
55     $head      = $font->getData("head");
56     $head_json = json_encode($head);
57
58     $os2      = $font->getData("OS/2");
59     $os2_json = json_encode($os2);
60
61     $hmtx      = $font->getData("hmtx");
62     $hmtx_json = json_encode($hmtx);
63
64     $names           = $font->getData("post", "names");
65     $glyphIndexArray = array_flip($font->getUnicodeCharMap());
66
67     $width  = (abs($head["xMin"]) + $head["xMax"]);
68     $height = (abs($head["yMin"]) + $head["yMax"]);
69
70     $ratio = 1;
71     if ($width > $max || $height > $max) {
72       $ratio  = max($width, $height) / $max;
73       $width  = round($width / $ratio);
74       $height = round($height / $ratio);
75     }
76
77     $n = 500;
78
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)' />
83     </h3>
84     <script>
85       Glyph.ratio  = $ratio;
86       Glyph.head   = $head_json;
87       Glyph.os2    = $os2_json;
88       Glyph.hmtx   = $hmtx_json;
89       Glyph.width  = $width;
90       Glyph.height = $height;
91     </script>";
92
93     foreach ($this->data as $g => $glyph) {
94       if ($n-- <= 0) {
95         break;
96       }
97
98       $glyph->parseData();
99
100       $shape      = array(
101         "SVGContours" => $glyph->getSVGContours(),
102         "xMin"        => $glyph->xMin,
103         "yMin"        => $glyph->yMin,
104         "xMax"        => $glyph->xMax,
105         "yMax"        => $glyph->yMax,
106       );
107       $shape_json = json_encode($shape);
108
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]};" : "";
113
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>
118               ";
119
120       if ($type == "composite") {
121         foreach ($glyph->getGlyphIDs() as $_id) {
122           $s .= "<a href='#glyph-$_id' class='glyph-component-id'>$_id</a> ";
123         }
124       }
125
126       $s .= "<br />
127             <canvas width='$width' height='$height' id='glyph-canvas-$g'></canvas>
128             </div>
129             <script>Glyph.glyphs.push([$g,$shape_json]);</script>";
130     }
131
132     return $s;
133   }
134
135
136   protected function _encode() {
137     $font   = $this->getFont();
138     $subset = $font->getSubset();
139     $data   = $this->data;
140
141     $loca = array();
142
143     $length = 0;
144     foreach ($subset as $gid) {
145       $loca[] = $length;
146       $length += $data[$gid]->encode();
147     }
148
149     $loca[]                             = $length; // dummy loca
150     $font->getTableObject("loca")->data = $loca;
151
152     return $length;
153   }
154 }