Blame view

public/vendor/phenx/php-font-lib/src/FontLib/Glyph/OutlineSimple.php 8.21 KB
86143e36f   Андрей Ларионов   Коммит вторник
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  <?php
  /**
   * @package php-font-lib
   * @link    https://github.com/dompdf/php-font-lib
   * @author  Fabien Ménager <fabien.menager@gmail.com>
   * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
   * @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
   */
  
  namespace FontLib\Glyph;
  
  /**
   * `glyf` font table.
   *
   * @package php-font-lib
   */
  class OutlineSimple extends Outline {
    const ON_CURVE       = 0x01;
    const X_SHORT_VECTOR = 0x02;
    const Y_SHORT_VECTOR = 0x04;
    const REPEAT         = 0x08;
    const THIS_X_IS_SAME = 0x10;
    const THIS_Y_IS_SAME = 0x20;
  
    public $instructions;
    public $points;
  
    function parseData() {
      parent::parseData();
  
      if (!$this->size) {
        return;
      }
  
      $font = $this->getFont();
  
      $noc = $this->numberOfContours;
  
      if ($noc == 0) {
        return;
      }
  
      $endPtsOfContours = $font->r(array(self::uint16, $noc));
  
      $instructionLength  = $font->readUInt16();
      $this->instructions = $font->r(array(self::uint8, $instructionLength));
  
      $count = $endPtsOfContours[$noc - 1] + 1;
  
      // Flags
      $flags = array();
      for ($index = 0; $index < $count; $index++) {
        $flags[$index] = $font->readUInt8();
  
        if ($flags[$index] & self::REPEAT) {
          $repeats = $font->readUInt8();
  
          for ($i = 1; $i <= $repeats; $i++) {
            $flags[$index + $i] = $flags[$index];
          }
  
          $index += $repeats;
        }
      }
  
      $points = array();
      foreach ($flags as $i => $flag) {
        $points[$i]["onCurve"]      = $flag & self::ON_CURVE;
        $points[$i]["endOfContour"] = in_array($i, $endPtsOfContours);
      }
  
      // X Coords
      $x = 0;
      for ($i = 0; $i < $count; $i++) {
        $flag = $flags[$i];
  
        if ($flag & self::THIS_X_IS_SAME) {
          if ($flag & self::X_SHORT_VECTOR) {
            $x += $font->readUInt8();
          }
        }
        else {
          if ($flag & self::X_SHORT_VECTOR) {
            $x -= $font->readUInt8();
          }
          else {
            $x += $font->readInt16();
          }
        }
  
        $points[$i]["x"] = $x;
      }
  
      // Y Coords
      $y = 0;
      for ($i = 0; $i < $count; $i++) {
        $flag = $flags[$i];
  
        if ($flag & self::THIS_Y_IS_SAME) {
          if ($flag & self::Y_SHORT_VECTOR) {
            $y += $font->readUInt8();
          }
        }
        else {
          if ($flag & self::Y_SHORT_VECTOR) {
            $y -= $font->readUInt8();
          }
          else {
            $y += $font->readInt16();
          }
        }
  
        $points[$i]["y"] = $y;
      }
  
      $this->points = $points;
    }
  
    public function splitSVGPath($path) {
      preg_match_all('/([a-z])|(-?\d+(?:\.\d+)?)/i', $path, $matches, PREG_PATTERN_ORDER);
  
      return $matches[0];
    }
  
    public function makePoints($path) {
      $path = $this->splitSVGPath($path);
      $l    = count($path);
      $i    = 0;
  
      $points = array();
  
      while ($i < $l) {
        switch ($path[$i]) {
          // moveTo
          case "M":
            $points[] = array(
              "onCurve"      => true,
              "x"            => $path[++$i],
              "y"            => $path[++$i],
              "endOfContour" => false,
            );
            break;
  
          // lineTo
          case "L":
            $points[] = array(
              "onCurve"      => true,
              "x"            => $path[++$i],
              "y"            => $path[++$i],
              "endOfContour" => false,
            );
            break;
  
          // quadraticCurveTo
          case "Q":
            $points[] = array(
              "onCurve"      => false,
              "x"            => $path[++$i],
              "y"            => $path[++$i],
              "endOfContour" => false,
            );
            $points[] = array(
              "onCurve"      => true,
              "x"            => $path[++$i],
              "y"            => $path[++$i],
              "endOfContour" => false,
            );
            break;
  
          // closePath
          /** @noinspection PhpMissingBreakStatementInspection */
          case "z":
            $points[count($points) - 1]["endOfContour"] = true;
  
          default:
            $i++;
            break;
        }
      }
  
      return $points;
    }
  
    function encode() {
      if (empty($this->points)) {
        return parent::encode();
      }
  
      return $this->size = $this->encodePoints($this->points);
    }
  
    public function encodePoints($points) {
      $endPtsOfContours = array();
      $flags            = array();
      $coords_x         = array();
      $coords_y         = array();
  
      $last_x = 0;
      $last_y = 0;
      $xMin   = $yMin = 0xFFFF;
      $xMax   = $yMax = -0xFFFF;
      foreach ($points as $i => $point) {
        $flag = 0;
        if ($point["onCurve"]) {
          $flag |= self::ON_CURVE;
        }
  
        if ($point["endOfContour"]) {
          $endPtsOfContours[] = $i;
        }
  
        // Simplified, we could do some optimizations
        if ($point["x"] == $last_x) {
          $flag |= self::THIS_X_IS_SAME;
        }
        else {
          $x          = intval($point["x"]);
          $xMin       = min($x, $xMin);
          $xMax       = max($x, $xMax);
          $coords_x[] = $x - $last_x; // int16
        }
  
        // Simplified, we could do some optimizations
        if ($point["y"] == $last_y) {
          $flag |= self::THIS_Y_IS_SAME;
        }
        else {
          $y          = intval($point["y"]);
          $yMin       = min($y, $yMin);
          $yMax       = max($y, $yMax);
          $coords_y[] = $y - $last_y; // int16
        }
  
        $flags[] = $flag;
        $last_x  = $point["x"];
        $last_y  = $point["y"];
      }
  
      $font = $this->getFont();
  
      $l = 0;
      $l += $font->writeInt16(count($endPtsOfContours)); // endPtsOfContours
      $l += $font->writeFWord(isset($this->xMin) ? $this->xMin : $xMin); // xMin
      $l += $font->writeFWord(isset($this->yMin) ? $this->yMin : $yMin); // yMin
      $l += $font->writeFWord(isset($this->xMax) ? $this->xMax : $xMax); // xMax
      $l += $font->writeFWord(isset($this->yMax) ? $this->yMax : $yMax); // yMax
  
      // Simple glyf
      $l += $font->w(array(self::uint16, count($endPtsOfContours)), $endPtsOfContours); // endPtsOfContours
      $l += $font->writeUInt16(0); // instructionLength
      $l += $font->w(array(self::uint8, count($flags)), $flags); // flags
      $l += $font->w(array(self::int16, count($coords_x)), $coords_x); // xCoordinates
      $l += $font->w(array(self::int16, count($coords_y)), $coords_y); // yCoordinates
      return $l;
    }
  
    public function getSVGContours($points = null) {
      $path = "";
  
      if (!$points) {
        if (empty($this->points)) {
          $this->parseData();
        }
  
        $points = $this->points;
      }
  
      $length     = (empty($points) ? 0 : count($points));
      $firstIndex = 0;
      $count      = 0;
  
      for ($i = 0; $i < $length; $i++) {
        $count++;
  
        if ($points[$i]["endOfContour"]) {
          $path .= $this->getSVGPath($points, $firstIndex, $count);
          $firstIndex = $i + 1;
          $count      = 0;
        }
      }
  
      return $path;
    }
  
    protected function getSVGPath($points, $startIndex, $count) {
      $offset = 0;
      $path   = "";
  
      while ($offset < $count) {
        $point    = $points[$startIndex + $offset % $count];
        $point_p1 = $points[$startIndex + ($offset + 1) % $count];
  
        if ($offset == 0) {
          $path .= "M{$point['x']},{$point['y']} ";
        }
  
        if ($point["onCurve"]) {
          if ($point_p1["onCurve"]) {
            $path .= "L{$point_p1['x']},{$point_p1['y']} ";
            $offset++;
          }
          else {
            $point_p2 = $points[$startIndex + ($offset + 2) % $count];
  
            if ($point_p2["onCurve"]) {
              $path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
            }
            else {
              $path .= "Q{$point_p1['x']},{$point_p1['y']}," . $this->midValue($point_p1['x'], $point_p2['x']) . "," . $this->midValue($point_p1['y'], $point_p2['y']) . " ";
            }
  
            $offset += 2;
          }
        }
        else {
          if ($point_p1["onCurve"]) {
            $path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
          }
          else {
            $path .= "Q{$point['x']},{$point['y']}," . $this->midValue($point['x'], $point_p1['x']) . "," . $this->midValue($point['y'], $point_p1['y']) . " ";
          }
  
          $offset++;
        }
      }
  
      $path .= "z ";
  
      return $path;
    }
  
    function midValue($a, $b) {
      return $a + ($b - $a) / 2;
    }
  }