PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / dompdf / php-font-lib / src / FontLib / Glyph / OutlineSimple.php

OutlineSimple.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/dompdf/php-font-lib/src/FontLib/Glyph/OutlineSimple.php

267 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package php-font-lib
5 * @link https://github.com/dompdf/php-font-lib
6 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7 * @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
8 */
9 namespace WCPOS\Vendor\FontLib\Glyph;
10
11 /**
12 * `glyf` font table.
13 *
14 * @package php-font-lib
15 */
16 class OutlineSimple extends Outline
17 {
18 const ON_CURVE = 0x1;
19 const X_SHORT_VECTOR = 0x2;
20 const Y_SHORT_VECTOR = 0x4;
21 const REPEAT = 0x8;
22 const THIS_X_IS_SAME = 0x10;
23 const THIS_Y_IS_SAME = 0x20;
24 public $instructions;
25 public $points;
26 function parseData()
27 {
28 parent::parseData();
29 if (!$this->size) {
30 return;
31 }
32 $font = $this->getFont();
33 $noc = $this->numberOfContours;
34 if ($noc == 0) {
35 return;
36 }
37 $endPtsOfContours = $font->r(array(self::uint16, $noc));
38 $instructionLength = $font->readUInt16();
39 $this->instructions = $font->r(array(self::uint8, $instructionLength));
40 $count = $endPtsOfContours[$noc - 1] + 1;
41 // Flags
42 $flags = array();
43 for ($index = 0; $index < $count; $index++) {
44 $flags[$index] = $font->readUInt8();
45 if ($flags[$index] & self::REPEAT) {
46 $repeats = $font->readUInt8();
47 for ($i = 1; $i <= $repeats; $i++) {
48 $flags[$index + $i] = $flags[$index];
49 }
50 $index += $repeats;
51 }
52 }
53 $points = array();
54 foreach ($flags as $i => $flag) {
55 $points[$i]["onCurve"] = $flag & self::ON_CURVE;
56 $points[$i]["endOfContour"] = \in_array($i, $endPtsOfContours);
57 }
58 // X Coords
59 $x = 0;
60 for ($i = 0; $i < $count; $i++) {
61 $flag = $flags[$i];
62 if ($flag & self::THIS_X_IS_SAME) {
63 if ($flag & self::X_SHORT_VECTOR) {
64 $x += $font->readUInt8();
65 }
66 } else {
67 if ($flag & self::X_SHORT_VECTOR) {
68 $x -= $font->readUInt8();
69 } else {
70 $x += $font->readInt16();
71 }
72 }
73 $points[$i]["x"] = $x;
74 }
75 // Y Coords
76 $y = 0;
77 for ($i = 0; $i < $count; $i++) {
78 $flag = $flags[$i];
79 if ($flag & self::THIS_Y_IS_SAME) {
80 if ($flag & self::Y_SHORT_VECTOR) {
81 $y += $font->readUInt8();
82 }
83 } else {
84 if ($flag & self::Y_SHORT_VECTOR) {
85 $y -= $font->readUInt8();
86 } else {
87 $y += $font->readInt16();
88 }
89 }
90 $points[$i]["y"] = $y;
91 }
92 $this->points = $points;
93 }
94 public function splitSVGPath($path)
95 {
96 \preg_match_all('/([a-z])|(-?\\d+(?:\\.\\d+)?)/i', $path, $matches, \PREG_PATTERN_ORDER);
97 return $matches[0];
98 }
99 public function makePoints($path)
100 {
101 $path = $this->splitSVGPath($path);
102 $l = \count($path);
103 $i = 0;
104 $points = array();
105 while ($i < $l) {
106 switch ($path[$i]) {
107 // moveTo
108 case "M":
109 $points[] = array("onCurve" => \true, "x" => $path[++$i], "y" => $path[++$i], "endOfContour" => \false);
110 break;
111 // lineTo
112 case "L":
113 $points[] = array("onCurve" => \true, "x" => $path[++$i], "y" => $path[++$i], "endOfContour" => \false);
114 break;
115 // quadraticCurveTo
116 case "Q":
117 $points[] = array("onCurve" => \false, "x" => $path[++$i], "y" => $path[++$i], "endOfContour" => \false);
118 $points[] = array("onCurve" => \true, "x" => $path[++$i], "y" => $path[++$i], "endOfContour" => \false);
119 break;
120 // closePath
121 /** @noinspection PhpMissingBreakStatementInspection */
122 case "z":
123 $points[\count($points) - 1]["endOfContour"] = \true;
124 default:
125 $i++;
126 break;
127 }
128 }
129 return $points;
130 }
131 function encode()
132 {
133 if (empty($this->points)) {
134 return parent::encode();
135 }
136 return $this->size = $this->encodePoints($this->points);
137 }
138 public function encodePoints($points)
139 {
140 $endPtsOfContours = array();
141 $flags = array();
142 $coords_x = array();
143 $coords_y = array();
144 $last_x = 0;
145 $last_y = 0;
146 $xMin = $yMin = 0xffff;
147 $xMax = $yMax = -0xffff;
148 foreach ($points as $i => $point) {
149 $flag = 0;
150 if ($point["onCurve"]) {
151 $flag |= self::ON_CURVE;
152 }
153 if ($point["endOfContour"]) {
154 $endPtsOfContours[] = $i;
155 }
156 // Simplified, we could do some optimizations
157 if ($point["x"] == $last_x) {
158 $flag |= self::THIS_X_IS_SAME;
159 } else {
160 $x = \intval($point["x"]);
161 $xMin = \min($x, $xMin);
162 $xMax = \max($x, $xMax);
163 $coords_x[] = $x - $last_x;
164 // int16
165 }
166 // Simplified, we could do some optimizations
167 if ($point["y"] == $last_y) {
168 $flag |= self::THIS_Y_IS_SAME;
169 } else {
170 $y = \intval($point["y"]);
171 $yMin = \min($y, $yMin);
172 $yMax = \max($y, $yMax);
173 $coords_y[] = $y - $last_y;
174 // int16
175 }
176 $flags[] = $flag;
177 $last_x = $point["x"];
178 $last_y = $point["y"];
179 }
180 $font = $this->getFont();
181 $l = 0;
182 $l += $font->writeInt16(\count($endPtsOfContours));
183 // endPtsOfContours
184 $l += $font->writeFWord(isset($this->xMin) ? $this->xMin : $xMin);
185 // xMin
186 $l += $font->writeFWord(isset($this->yMin) ? $this->yMin : $yMin);
187 // yMin
188 $l += $font->writeFWord(isset($this->xMax) ? $this->xMax : $xMax);
189 // xMax
190 $l += $font->writeFWord(isset($this->yMax) ? $this->yMax : $yMax);
191 // yMax
192 // Simple glyf
193 $l += $font->w(array(self::uint16, \count($endPtsOfContours)), $endPtsOfContours);
194 // endPtsOfContours
195 $l += $font->writeUInt16(0);
196 // instructionLength
197 $l += $font->w(array(self::uint8, \count($flags)), $flags);
198 // flags
199 $l += $font->w(array(self::int16, \count($coords_x)), $coords_x);
200 // xCoordinates
201 $l += $font->w(array(self::int16, \count($coords_y)), $coords_y);
202 // yCoordinates
203 return $l;
204 }
205 public function getSVGContours($points = null)
206 {
207 $path = "";
208 if (!$points) {
209 if (empty($this->points)) {
210 $this->parseData();
211 }
212 $points = $this->points;
213 }
214 $length = empty($points) ? 0 : \count($points);
215 $firstIndex = 0;
216 $count = 0;
217 for ($i = 0; $i < $length; $i++) {
218 $count++;
219 if ($points[$i]["endOfContour"]) {
220 $path .= $this->getSVGPath($points, $firstIndex, $count);
221 $firstIndex = $i + 1;
222 $count = 0;
223 }
224 }
225 return $path;
226 }
227 protected function getSVGPath($points, $startIndex, $count)
228 {
229 $offset = 0;
230 $path = "";
231 while ($offset < $count) {
232 $point = $points[$startIndex + $offset % $count];
233 $point_p1 = $points[$startIndex + ($offset + 1) % $count];
234 if ($offset == 0) {
235 $path .= "M{$point['x']},{$point['y']} ";
236 }
237 if ($point["onCurve"]) {
238 if ($point_p1["onCurve"]) {
239 $path .= "L{$point_p1['x']},{$point_p1['y']} ";
240 $offset++;
241 } else {
242 $point_p2 = $points[$startIndex + ($offset + 2) % $count];
243 if ($point_p2["onCurve"]) {
244 $path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
245 } else {
246 $path .= "Q{$point_p1['x']},{$point_p1['y']}," . $this->midValue($point_p1['x'], $point_p2['x']) . "," . $this->midValue($point_p1['y'], $point_p2['y']) . " ";
247 }
248 $offset += 2;
249 }
250 } else {
251 if ($point_p1["onCurve"]) {
252 $path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
253 } else {
254 $path .= "Q{$point['x']},{$point['y']}," . $this->midValue($point['x'], $point_p1['x']) . "," . $this->midValue($point['y'], $point_p1['y']) . " ";
255 }
256 $offset++;
257 }
258 }
259 $path .= "z ";
260 return $path;
261 }
262 function midValue($a, $b)
263 {
264 return $a + ($b - $a) / 2;
265 }
266 }
267