PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.6
Yatra – Travel Booking & Tour Operator Software v3.0.6
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / vendor / phenx / php-svg-lib / src / Svg / Surface / SurfacePDFLib.php

SurfacePDFLib.php in Yatra – Travel Booking & Tour Operator Software 3.0.6, at vendor/phenx/php-svg-lib/src/Svg/Surface/SurfacePDFLib.php

431 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package php-svg-lib
4 * @link http://github.com/PhenX/php-svg-lib
5 * @author Fabien Ménager <[email protected]>
6 * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html
7 */
8
9 namespace Svg\Surface;
10
11 use Svg\Style;
12 use Svg\Document;
13
14 class SurfacePDFLib implements SurfaceInterface
15 {
16 const DEBUG = false;
17
18 private $canvas;
19
20 private $width;
21 private $height;
22
23 /** @var Style */
24 private $style;
25
26 public function __construct(Document $doc, $canvas = null)
27 {
28 if (self::DEBUG) echo __FUNCTION__ . "\n";
29
30 $dimensions = $doc->getDimensions();
31 $w = $dimensions["width"];
32 $h = $dimensions["height"];
33
34 if (!$canvas) {
35 $canvas = new \PDFlib();
36
37 /* all strings are expected as utf8 */
38 $canvas->set_option("stringformat=utf8");
39 $canvas->set_option("errorpolicy=return");
40
41 /* open new PDF file; insert a file name to create the PDF on disk */
42 if ($canvas->begin_document("", "") == 0) {
43 die("Error: " . $canvas->get_errmsg());
44 }
45 $canvas->set_info("Creator", "PDFlib starter sample");
46 $canvas->set_info("Title", "starter_graphics");
47
48 $canvas->begin_page_ext($w, $h, "");
49 }
50
51 // Flip PDF coordinate system so that the origin is in
52 // the top left rather than the bottom left
53 $canvas->setmatrix(
54 1, 0,
55 0, -1,
56 0, $h
57 );
58
59 $this->width = $w;
60 $this->height = $h;
61
62 $this->canvas = $canvas;
63 }
64
65 function out()
66 {
67 if (self::DEBUG) echo __FUNCTION__ . "\n";
68
69 $this->canvas->end_page_ext("");
70 $this->canvas->end_document("");
71
72 return $this->canvas->get_buffer();
73 }
74
75 public function save()
76 {
77 if (self::DEBUG) echo __FUNCTION__ . "\n";
78 $this->canvas->save();
79 }
80
81 public function restore()
82 {
83 if (self::DEBUG) echo __FUNCTION__ . "\n";
84 $this->canvas->restore();
85 }
86
87 public function scale($x, $y)
88 {
89 if (self::DEBUG) echo __FUNCTION__ . "\n";
90 $this->canvas->scale($x, $y);
91 }
92
93 public function rotate($angle)
94 {
95 if (self::DEBUG) echo __FUNCTION__ . "\n";
96 $this->canvas->rotate($angle);
97 }
98
99 public function translate($x, $y)
100 {
101 if (self::DEBUG) echo __FUNCTION__ . "\n";
102 $this->canvas->translate($x, $y);
103 }
104
105 public function transform($a, $b, $c, $d, $e, $f)
106 {
107 if (self::DEBUG) echo __FUNCTION__ . "\n";
108 $this->canvas->concat($a, $b, $c, $d, $e, $f);
109 }
110
111 public function beginPath()
112 {
113 if (self::DEBUG) echo __FUNCTION__ . "\n";
114 // TODO: Implement beginPath() method.
115 }
116
117 public function closePath()
118 {
119 if (self::DEBUG) echo __FUNCTION__ . "\n";
120 $this->canvas->closepath();
121 }
122
123 public function fillStroke(bool $close = false)
124 {
125 if (self::DEBUG) echo __FUNCTION__ . "\n";
126 if ($close) {
127 $this->canvas->closepath_fill_stroke();
128 } else {
129 $this->canvas->fill_stroke();
130 }
131 }
132
133 public function clip()
134 {
135 if (self::DEBUG) echo __FUNCTION__ . "\n";
136 $this->canvas->clip();
137 }
138
139 public function fillText($text, $x, $y, $maxWidth = null)
140 {
141 if (self::DEBUG) echo __FUNCTION__ . "\n";
142 $this->canvas->set_text_pos($x, $y);
143 $this->canvas->show($text);
144 }
145
146 public function strokeText($text, $x, $y, $maxWidth = null)
147 {
148 if (self::DEBUG) echo __FUNCTION__ . "\n";
149 // TODO: Implement drawImage() method.
150 }
151
152 public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null)
153 {
154 if (self::DEBUG) echo __FUNCTION__ . "\n";
155
156 if (strpos($image, "data:") === 0) {
157 $data = substr($image, strpos($image, ";") + 1);
158 if (strpos($data, "base64") === 0) {
159 $data = base64_decode(substr($data, 7));
160 }
161 }
162 else {
163 $data = file_get_contents($image);
164 }
165
166 $image = tempnam(sys_get_temp_dir(), "svg");
167 file_put_contents($image, $data);
168
169 $img = $this->canvas->load_image("auto", $image, "");
170
171 $sy = $sy - $sh;
172 $this->canvas->fit_image($img, $sx, $sy, 'boxsize={' . "$sw $sh" . '} fitmethod=entire');
173
174 unlink($image);
175 }
176
177 public function lineTo($x, $y)
178 {
179 if (self::DEBUG) echo __FUNCTION__ . "\n";
180 $this->canvas->lineto($x, $y);
181 }
182
183 public function moveTo($x, $y)
184 {
185 if (self::DEBUG) echo __FUNCTION__ . "\n";
186 $this->canvas->moveto($x, $y);
187 }
188
189 public function quadraticCurveTo($cpx, $cpy, $x, $y)
190 {
191 if (self::DEBUG) echo __FUNCTION__ . "\n";
192
193 // FIXME not accurate
194 $this->canvas->curveTo($cpx, $cpy, $cpx, $cpy, $x, $y);
195 }
196
197 public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y)
198 {
199 if (self::DEBUG) echo __FUNCTION__ . "\n";
200 $this->canvas->curveto($cp1x, $cp1y, $cp2x, $cp2y, $x, $y);
201 }
202
203 public function arcTo($x1, $y1, $x2, $y2, $radius)
204 {
205 if (self::DEBUG) echo __FUNCTION__ . "\n";
206 }
207
208 public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = false)
209 {
210 if (self::DEBUG) echo __FUNCTION__ . "\n";
211 $this->canvas->arc($x, $y, $radius, $startAngle, $endAngle);
212 }
213
214 public function circle($x, $y, $radius)
215 {
216 if (self::DEBUG) echo __FUNCTION__ . "\n";
217 $this->canvas->circle($x, $y, $radius);
218 }
219
220 public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise)
221 {
222 if (self::DEBUG) echo __FUNCTION__ . "\n";
223 $this->canvas->ellipse($x, $y, $radiusX, $radiusY);
224 }
225
226 public function fillRect($x, $y, $w, $h)
227 {
228 if (self::DEBUG) echo __FUNCTION__ . "\n";
229 $this->rect($x, $y, $w, $h);
230 $this->fill();
231 }
232
233 public function rect($x, $y, $w, $h, $rx = 0, $ry = 0)
234 {
235 if (self::DEBUG) echo __FUNCTION__ . "\n";
236
237 $canvas = $this->canvas;
238
239 if ($rx <= 0.000001/* && $ry <= 0.000001*/) {
240 $canvas->rect($x, $y, $w, $h);
241
242 return;
243 }
244
245 /* Define a path for a rectangle with corners rounded by a given radius.
246 * Start from the lower left corner and proceed counterclockwise.
247 */
248 $canvas->moveto($x + $rx, $y);
249
250 /* Start of the arc segment in the lower right corner */
251 $canvas->lineto($x + $w - $rx, $y);
252
253 /* Arc segment in the lower right corner */
254 $canvas->arc($x + $w - $rx, $y + $rx, $rx, 270, 360);
255
256 /* Start of the arc segment in the upper right corner */
257 $canvas->lineto($x + $w, $y + $h - $rx );
258
259 /* Arc segment in the upper right corner */
260 $canvas->arc($x + $w - $rx, $y + $h - $rx, $rx, 0, 90);
261
262 /* Start of the arc segment in the upper left corner */
263 $canvas->lineto($x + $rx, $y + $h);
264
265 /* Arc segment in the upper left corner */
266 $canvas->arc($x + $rx, $y + $h - $rx, $rx, 90, 180);
267
268 /* Start of the arc segment in the lower left corner */
269 $canvas->lineto($x , $y + $rx);
270
271 /* Arc segment in the lower left corner */
272 $canvas->arc($x + $rx, $y + $rx, $rx, 180, 270);
273 }
274
275 public function fill()
276 {
277 if (self::DEBUG) echo __FUNCTION__ . "\n";
278 $this->canvas->fill();
279 }
280
281 public function strokeRect($x, $y, $w, $h)
282 {
283 if (self::DEBUG) echo __FUNCTION__ . "\n";
284 $this->rect($x, $y, $w, $h);
285 $this->stroke();
286 }
287
288 public function stroke(bool $close = false)
289 {
290 if (self::DEBUG) echo __FUNCTION__ . "\n";
291 if ($close) {
292 $this->canvas->closepath_stroke();
293 } else {
294 $this->canvas->stroke();
295 }
296 }
297
298 public function endPath()
299 {
300 if (self::DEBUG) echo __FUNCTION__ . "\n";
301 $this->canvas->endPath();
302 }
303
304 public function measureText($text)
305 {
306 if (self::DEBUG) echo __FUNCTION__ . "\n";
307 $style = $this->getStyle();
308 $font = $this->getFont($style->fontFamily, $style->fontStyle);
309
310 return $this->canvas->stringwidth($text, $font, $this->getStyle()->fontSize);
311 }
312
313 public function getStyle()
314 {
315 if (self::DEBUG) echo __FUNCTION__ . "\n";
316 return $this->style;
317 }
318
319 public function setStyle(Style $style)
320 {
321 if (self::DEBUG) echo __FUNCTION__ . "\n";
322
323 $this->style = $style;
324 $canvas = $this->canvas;
325
326 if (is_array($style->stroke) && $stroke = $style->stroke) {
327 $canvas->setcolor(
328 "stroke",
329 "rgb",
330 $stroke[0] / 255,
331 $stroke[1] / 255,
332 $stroke[2] / 255,
333 null
334 );
335 }
336
337 if (is_array($style->fill) && $fill = $style->fill) {
338 $canvas->setcolor(
339 "fill",
340 "rgb",
341 $fill[0] / 255,
342 $fill[1] / 255,
343 $fill[2] / 255,
344 null
345 );
346 }
347
348 if ($fillRule = strtolower($style->fillRule)) {
349 $map = array(
350 "nonzero" => "winding",
351 "evenodd" => "evenodd",
352 );
353
354 if (isset($map[$fillRule])) {
355 $fillRule = $map[$fillRule];
356
357 $canvas->set_parameter("fillrule", $fillRule);
358 }
359 }
360
361 $opts = array();
362 if ($style->strokeWidth > 0.000001) {
363 $opts[] = "linewidth=$style->strokeWidth";
364 }
365
366 if (in_array($style->strokeLinecap, array("butt", "round", "projecting"))) {
367 $opts[] = "linecap=$style->strokeLinecap";
368 }
369
370 if (in_array($style->strokeLinejoin, array("miter", "round", "bevel"))) {
371 $opts[] = "linejoin=$style->strokeLinejoin";
372 }
373
374 $canvas->set_graphics_option(implode(" ", $opts));
375
376 $opts = array();
377 $opacity = $style->opacity;
378 if ($opacity !== null && $opacity < 1.0) {
379 $opts[] = "opacityfill=$opacity";
380 $opts[] = "opacitystroke=$opacity";
381 }
382 else {
383 $fillOpacity = $style->fillOpacity;
384 if ($fillOpacity !== null && $fillOpacity < 1.0) {
385 $opts[] = "opacityfill=$fillOpacity";
386 }
387
388 $strokeOpacity = $style->strokeOpacity;
389 if ($strokeOpacity !== null && $strokeOpacity < 1.0) {
390 $opts[] = "opacitystroke=$strokeOpacity";
391 }
392 }
393
394 if (count($opts)) {
395 $gs = $canvas->create_gstate(implode(" ", $opts));
396 $canvas->set_gstate($gs);
397 }
398
399 $font = $this->getFont($style->fontFamily, $style->fontStyle);
400 if ($font) {
401 $canvas->setfont($font, $style->fontSize);
402 }
403 }
404
405 private function getFont($family, $style)
406 {
407 $map = array(
408 "serif" => "Times",
409 "sans-serif" => "Helvetica",
410 "fantasy" => "Symbol",
411 "cursive" => "Times",
412 "monospace" => "Courier",
413
414 "arial" => "Helvetica",
415 "verdana" => "Helvetica",
416 );
417
418 $family = strtolower($family);
419 if (isset($map[$family])) {
420 $family = $map[$family];
421 }
422
423 return $this->canvas->load_font($family, "unicode", "fontstyle=$style");
424 }
425
426 public function setFont($family, $style, $weight)
427 {
428 // TODO: Implement setFont() method.
429 }
430 }
431