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