| 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\Document; |
| 11 |
use WCPOS\Vendor\Svg\Style; |
| 12 |
class SurfaceCpdf implements SurfaceInterface |
| 13 |
{ |
| 14 |
const DEBUG = \false; |
| 15 |
/** @var \Svg\Surface\CPdf */ |
| 16 |
private $canvas; |
| 17 |
private $width; |
| 18 |
private $height; |
| 19 |
/** @var Style */ |
| 20 |
private $style; |
| 21 |
public function __construct(Document $doc, $canvas = null) |
| 22 |
{ |
| 23 |
if (self::DEBUG) { |
| 24 |
echo __FUNCTION__ . "\n"; |
| 25 |
} |
| 26 |
$dimensions = $doc->getDimensions(); |
| 27 |
$w = $dimensions["width"]; |
| 28 |
$h = $dimensions["height"]; |
| 29 |
if (!$canvas) { |
| 30 |
$canvas = new \WCPOS\Vendor\Svg\Surface\CPdf(array(0, 0, $w, $h)); |
| 31 |
$refl = new \ReflectionClass($canvas); |
| 32 |
$canvas->fontcache = \realpath(\dirname($refl->getFileName()) . "/../../fonts/") . "/"; |
| 33 |
} |
| 34 |
// Flip PDF coordinate system so that the origin is in |
| 35 |
// the top left rather than the bottom left |
| 36 |
$canvas->transform(array(1, 0, 0, -1, 0, $h)); |
| 37 |
$this->width = $w; |
| 38 |
$this->height = $h; |
| 39 |
$this->canvas = $canvas; |
| 40 |
} |
| 41 |
function out() |
| 42 |
{ |
| 43 |
if (self::DEBUG) { |
| 44 |
echo __FUNCTION__ . "\n"; |
| 45 |
} |
| 46 |
return $this->canvas->output(); |
| 47 |
} |
| 48 |
public function save() |
| 49 |
{ |
| 50 |
if (self::DEBUG) { |
| 51 |
echo __FUNCTION__ . "\n"; |
| 52 |
} |
| 53 |
$this->canvas->save(); |
| 54 |
} |
| 55 |
public function restore() |
| 56 |
{ |
| 57 |
if (self::DEBUG) { |
| 58 |
echo __FUNCTION__ . "\n"; |
| 59 |
} |
| 60 |
$this->canvas->restore(); |
| 61 |
} |
| 62 |
public function scale($x, $y) |
| 63 |
{ |
| 64 |
if (self::DEBUG) { |
| 65 |
echo __FUNCTION__ . "\n"; |
| 66 |
} |
| 67 |
$this->transform($x, 0, 0, $y, 0, 0); |
| 68 |
} |
| 69 |
public function rotate($angle) |
| 70 |
{ |
| 71 |
if (self::DEBUG) { |
| 72 |
echo __FUNCTION__ . "\n"; |
| 73 |
} |
| 74 |
$a = \deg2rad($angle); |
| 75 |
$cos_a = \cos($a); |
| 76 |
$sin_a = \sin($a); |
| 77 |
$this->transform($cos_a, $sin_a, -$sin_a, $cos_a, 0, 0); |
| 78 |
} |
| 79 |
public function translate($x, $y) |
| 80 |
{ |
| 81 |
if (self::DEBUG) { |
| 82 |
echo __FUNCTION__ . "\n"; |
| 83 |
} |
| 84 |
$this->transform(1, 0, 0, 1, $x, $y); |
| 85 |
} |
| 86 |
public function transform($a, $b, $c, $d, $e, $f) |
| 87 |
{ |
| 88 |
if (self::DEBUG) { |
| 89 |
echo __FUNCTION__ . "\n"; |
| 90 |
} |
| 91 |
$this->canvas->transform(array($a, $b, $c, $d, $e, $f)); |
| 92 |
} |
| 93 |
public function beginPath() |
| 94 |
{ |
| 95 |
if (self::DEBUG) { |
| 96 |
echo __FUNCTION__ . "\n"; |
| 97 |
} |
| 98 |
// TODO: Implement beginPath() method. |
| 99 |
} |
| 100 |
public function closePath() |
| 101 |
{ |
| 102 |
if (self::DEBUG) { |
| 103 |
echo __FUNCTION__ . "\n"; |
| 104 |
} |
| 105 |
$this->canvas->closePath(); |
| 106 |
} |
| 107 |
public function fillStroke(bool $close = \false) |
| 108 |
{ |
| 109 |
if (self::DEBUG) { |
| 110 |
echo __FUNCTION__ . "\n"; |
| 111 |
} |
| 112 |
$this->canvas->fillStroke($close); |
| 113 |
} |
| 114 |
public function clip() |
| 115 |
{ |
| 116 |
if (self::DEBUG) { |
| 117 |
echo __FUNCTION__ . "\n"; |
| 118 |
} |
| 119 |
$this->canvas->clip(); |
| 120 |
} |
| 121 |
public function fillText($text, $x, $y, $maxWidth = null) |
| 122 |
{ |
| 123 |
if (self::DEBUG) { |
| 124 |
echo __FUNCTION__ . "\n"; |
| 125 |
} |
| 126 |
$this->canvas->addText($x, $y, $this->style->fontSize, $text); |
| 127 |
} |
| 128 |
public function strokeText($text, $x, $y, $maxWidth = null) |
| 129 |
{ |
| 130 |
if (self::DEBUG) { |
| 131 |
echo __FUNCTION__ . "\n"; |
| 132 |
} |
| 133 |
$this->canvas->addText($x, $y, $this->style->fontSize, $text); |
| 134 |
} |
| 135 |
public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null) |
| 136 |
{ |
| 137 |
if (self::DEBUG) { |
| 138 |
echo __FUNCTION__ . "\n"; |
| 139 |
} |
| 140 |
if (\strpos($image, "data:") === 0) { |
| 141 |
$parts = \explode(',', $image, 2); |
| 142 |
$data = $parts[1]; |
| 143 |
$base64 = \false; |
| 144 |
$token = \strtok($parts[0], ';'); |
| 145 |
while ($token !== \false) { |
| 146 |
if ($token == 'base64') { |
| 147 |
$base64 = \true; |
| 148 |
} |
| 149 |
$token = \strtok(';'); |
| 150 |
} |
| 151 |
if ($base64) { |
| 152 |
$data = \base64_decode($data); |
| 153 |
} |
| 154 |
} else { |
| 155 |
$data = \file_get_contents($image); |
| 156 |
} |
| 157 |
$image = \tempnam(\sys_get_temp_dir(), "svg"); |
| 158 |
\file_put_contents($image, $data); |
| 159 |
$img = $this->image($image, $sx, $sy, $sw, $sh, "normal"); |
| 160 |
\unlink($image); |
| 161 |
} |
| 162 |
public static function getimagesize($filename) |
| 163 |
{ |
| 164 |
static $cache = array(); |
| 165 |
if (isset($cache[$filename])) { |
| 166 |
return $cache[$filename]; |
| 167 |
} |
| 168 |
list($width, $height, $type) = \getimagesize($filename); |
| 169 |
if ($width == null || $height == null) { |
| 170 |
$data = \file_get_contents($filename, null, null, 0, 26); |
| 171 |
if (\substr($data, 0, 2) === "BM") { |
| 172 |
$meta = \unpack('vtype/Vfilesize/Vreserved/Voffset/Vheadersize/Vwidth/Vheight', $data); |
| 173 |
$width = (int) $meta['width']; |
| 174 |
$height = (int) $meta['height']; |
| 175 |
$type = \IMAGETYPE_BMP; |
| 176 |
} |
| 177 |
} |
| 178 |
return $cache[$filename] = array($width, $height, $type); |
| 179 |
} |
| 180 |
function image($img, $x, $y, $w, $h, $resolution = "normal") |
| 181 |
{ |
| 182 |
list($width, $height, $type) = $this->getimagesize($img); |
| 183 |
switch ($type) { |
| 184 |
case \IMAGETYPE_JPEG: |
| 185 |
$this->canvas->addJpegFromFile($img, $x, $y - $h, $w, $h); |
| 186 |
break; |
| 187 |
case \IMAGETYPE_GIF: |
| 188 |
case \IMAGETYPE_BMP: |
| 189 |
// @todo use cache for BMP and GIF |
| 190 |
$img = $this->_convert_gif_bmp_to_png($img, $type); |
| 191 |
case \IMAGETYPE_PNG: |
| 192 |
$this->canvas->addPngFromFile($img, $x, $y - $h, $w, $h); |
| 193 |
break; |
| 194 |
default: |
| 195 |
} |
| 196 |
} |
| 197 |
public function lineTo($x, $y) |
| 198 |
{ |
| 199 |
if (self::DEBUG) { |
| 200 |
echo __FUNCTION__ . "\n"; |
| 201 |
} |
| 202 |
$this->canvas->lineTo($x, $y); |
| 203 |
} |
| 204 |
public function moveTo($x, $y) |
| 205 |
{ |
| 206 |
if (self::DEBUG) { |
| 207 |
echo __FUNCTION__ . "\n"; |
| 208 |
} |
| 209 |
$this->canvas->moveTo($x, $y); |
| 210 |
} |
| 211 |
public function quadraticCurveTo($cpx, $cpy, $x, $y) |
| 212 |
{ |
| 213 |
if (self::DEBUG) { |
| 214 |
echo __FUNCTION__ . "\n"; |
| 215 |
} |
| 216 |
// FIXME not accurate |
| 217 |
$this->canvas->quadTo($cpx, $cpy, $x, $y); |
| 218 |
} |
| 219 |
public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y) |
| 220 |
{ |
| 221 |
if (self::DEBUG) { |
| 222 |
echo __FUNCTION__ . "\n"; |
| 223 |
} |
| 224 |
$this->canvas->curveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y); |
| 225 |
} |
| 226 |
public function arcTo($x1, $y1, $x2, $y2, $radius) |
| 227 |
{ |
| 228 |
if (self::DEBUG) { |
| 229 |
echo __FUNCTION__ . "\n"; |
| 230 |
} |
| 231 |
} |
| 232 |
public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = \false) |
| 233 |
{ |
| 234 |
if (self::DEBUG) { |
| 235 |
echo __FUNCTION__ . "\n"; |
| 236 |
} |
| 237 |
$this->canvas->ellipse($x, $y, $radius, $radius, 0, 8, $startAngle, $endAngle, \false, \false, \false, \true); |
| 238 |
} |
| 239 |
public function circle($x, $y, $radius) |
| 240 |
{ |
| 241 |
if (self::DEBUG) { |
| 242 |
echo __FUNCTION__ . "\n"; |
| 243 |
} |
| 244 |
$this->canvas->ellipse($x, $y, $radius, $radius, 0, 8, 0, 360, \true, \false, \false, \false); |
| 245 |
} |
| 246 |
public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise) |
| 247 |
{ |
| 248 |
if (self::DEBUG) { |
| 249 |
echo __FUNCTION__ . "\n"; |
| 250 |
} |
| 251 |
$this->canvas->ellipse($x, $y, $radiusX, $radiusY, 0, 8, 0, 360, \false, \false, \false, \false); |
| 252 |
} |
| 253 |
public function fillRect($x, $y, $w, $h) |
| 254 |
{ |
| 255 |
if (self::DEBUG) { |
| 256 |
echo __FUNCTION__ . "\n"; |
| 257 |
} |
| 258 |
$this->rect($x, $y, $w, $h); |
| 259 |
$this->fill(); |
| 260 |
} |
| 261 |
public function rect($x, $y, $w, $h, $rx = 0, $ry = 0) |
| 262 |
{ |
| 263 |
if (self::DEBUG) { |
| 264 |
echo __FUNCTION__ . "\n"; |
| 265 |
} |
| 266 |
$canvas = $this->canvas; |
| 267 |
if ($rx <= 1.0E-6) { |
| 268 |
$canvas->rect($x, $y, $w, $h); |
| 269 |
return; |
| 270 |
} |
| 271 |
$rx = \min($rx, $w / 2); |
| 272 |
$rx = \min($rx, $h / 2); |
| 273 |
/* Define a path for a rectangle with corners rounded by a given radius. |
| 274 |
* Start from the lower left corner and proceed counterclockwise. |
| 275 |
*/ |
| 276 |
$this->moveTo($x + $rx, $y); |
| 277 |
/* Start of the arc segment in the lower right corner */ |
| 278 |
$this->lineTo($x + $w - $rx, $y); |
| 279 |
/* Arc segment in the lower right corner */ |
| 280 |
$this->arc($x + $w - $rx, $y + $rx, $rx, 270, 360); |
| 281 |
/* Start of the arc segment in the upper right corner */ |
| 282 |
$this->lineTo($x + $w, $y + $h - $rx); |
| 283 |
/* Arc segment in the upper right corner */ |
| 284 |
$this->arc($x + $w - $rx, $y + $h - $rx, $rx, 0, 90); |
| 285 |
/* Start of the arc segment in the upper left corner */ |
| 286 |
$this->lineTo($x + $rx, $y + $h); |
| 287 |
/* Arc segment in the upper left corner */ |
| 288 |
$this->arc($x + $rx, $y + $h - $rx, $rx, 90, 180); |
| 289 |
/* Start of the arc segment in the lower left corner */ |
| 290 |
$this->lineTo($x, $y + $rx); |
| 291 |
/* Arc segment in the lower left corner */ |
| 292 |
$this->arc($x + $rx, $y + $rx, $rx, 180, 270); |
| 293 |
} |
| 294 |
public function fill() |
| 295 |
{ |
| 296 |
if (self::DEBUG) { |
| 297 |
echo __FUNCTION__ . "\n"; |
| 298 |
} |
| 299 |
$this->canvas->fill(); |
| 300 |
} |
| 301 |
public function strokeRect($x, $y, $w, $h) |
| 302 |
{ |
| 303 |
if (self::DEBUG) { |
| 304 |
echo __FUNCTION__ . "\n"; |
| 305 |
} |
| 306 |
$this->rect($x, $y, $w, $h); |
| 307 |
$this->stroke(); |
| 308 |
} |
| 309 |
public function stroke(bool $close = \false) |
| 310 |
{ |
| 311 |
if (self::DEBUG) { |
| 312 |
echo __FUNCTION__ . "\n"; |
| 313 |
} |
| 314 |
$this->canvas->stroke($close); |
| 315 |
} |
| 316 |
public function endPath() |
| 317 |
{ |
| 318 |
if (self::DEBUG) { |
| 319 |
echo __FUNCTION__ . "\n"; |
| 320 |
} |
| 321 |
$this->canvas->endPath(); |
| 322 |
} |
| 323 |
public function measureText($text) |
| 324 |
{ |
| 325 |
if (self::DEBUG) { |
| 326 |
echo __FUNCTION__ . "\n"; |
| 327 |
} |
| 328 |
$style = $this->getStyle(); |
| 329 |
$this->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight); |
| 330 |
return $this->canvas->getTextWidth($this->getStyle()->fontSize, $text); |
| 331 |
} |
| 332 |
public function getStyle() |
| 333 |
{ |
| 334 |
if (self::DEBUG) { |
| 335 |
echo __FUNCTION__ . "\n"; |
| 336 |
} |
| 337 |
return $this->style; |
| 338 |
} |
| 339 |
public function setStyle(Style $style) |
| 340 |
{ |
| 341 |
if (self::DEBUG) { |
| 342 |
echo __FUNCTION__ . "\n"; |
| 343 |
} |
| 344 |
$this->style = $style; |
| 345 |
$canvas = $this->canvas; |
| 346 |
if (\is_array($style->stroke) && ($stroke = $style->stroke)) { |
| 347 |
$canvas->setStrokeColor(array((float) $stroke[0] / 255, (float) $stroke[1] / 255, (float) $stroke[2] / 255), \true); |
| 348 |
} |
| 349 |
if (\is_array($style->fill) && ($fill = $style->fill)) { |
| 350 |
$canvas->setColor(array((float) $fill[0] / 255, (float) $fill[1] / 255, (float) $fill[2] / 255), \true); |
| 351 |
} |
| 352 |
if ($fillRule = \strtolower($style->fillRule)) { |
| 353 |
$canvas->setFillRule($fillRule); |
| 354 |
} |
| 355 |
$opacity = $style->opacity; |
| 356 |
if ($opacity !== null && $opacity < 1.0) { |
| 357 |
$canvas->setLineTransparency("Normal", $opacity); |
| 358 |
$canvas->currentLineTransparency = null; |
| 359 |
$canvas->setFillTransparency("Normal", $opacity); |
| 360 |
$canvas->currentFillTransparency = null; |
| 361 |
} else { |
| 362 |
$fillOpacity = $style->fillOpacity; |
| 363 |
if ($fillOpacity !== null && $fillOpacity < 1.0) { |
| 364 |
$canvas->setFillTransparency("Normal", $fillOpacity); |
| 365 |
$canvas->currentFillTransparency = null; |
| 366 |
} |
| 367 |
$strokeOpacity = $style->strokeOpacity; |
| 368 |
if ($strokeOpacity !== null && $strokeOpacity < 1.0) { |
| 369 |
$canvas->setLineTransparency("Normal", $strokeOpacity); |
| 370 |
$canvas->currentLineTransparency = null; |
| 371 |
} |
| 372 |
} |
| 373 |
$dashArray = null; |
| 374 |
if ($style->strokeDasharray) { |
| 375 |
$dashArray = \preg_split('/\\s*,\\s*/', $style->strokeDasharray); |
| 376 |
} |
| 377 |
$phase = 0; |
| 378 |
if ($style->strokeDashoffset) { |
| 379 |
$phase = $style->strokeDashoffset; |
| 380 |
} |
| 381 |
$canvas->setLineStyle($style->strokeWidth, $style->strokeLinecap, $style->strokeLinejoin, $dashArray, $phase); |
| 382 |
$this->setFont($style->fontFamily, $style->fontStyle, $style->fontWeight); |
| 383 |
} |
| 384 |
public function setFont($family, $style, $weight) |
| 385 |
{ |
| 386 |
$map = ["serif" => "times", "sans-serif" => "helvetica", "fantasy" => "symbol", "cursive" => "times", "monospace" => "courier"]; |
| 387 |
$styleMap = ["courier" => ["" => "Courier", "b" => "Courier-Bold", "i" => "Courier-Oblique", "bi" => "Courier-BoldOblique"], "helvetica" => ["" => "Helvetica", "b" => "Helvetica-Bold", "i" => "Helvetica-Oblique", "bi" => "Helvetica-BoldOblique"], "symbol" => ["" => "Symbol"], "times" => ["" => "Times-Roman", "b" => "Times-Bold", "i" => "Times-Italic", "bi" => "Times-BoldItalic"]]; |
| 388 |
$family_lc = \strtolower($family); |
| 389 |
if (isset($map[$family_lc])) { |
| 390 |
$family = $map[$family_lc]; |
| 391 |
} |
| 392 |
if (isset($styleMap[$family])) { |
| 393 |
$key = ""; |
| 394 |
$weight = \strtolower($weight); |
| 395 |
if ($weight === "bold" || $weight === "bolder" || \is_numeric($weight) && $weight >= 600) { |
| 396 |
$key .= "b"; |
| 397 |
} |
| 398 |
$style = \strtolower($style); |
| 399 |
if ($style === "italic" || $style === "oblique") { |
| 400 |
$key .= "i"; |
| 401 |
} |
| 402 |
if (isset($styleMap[$family][$key])) { |
| 403 |
$family = $styleMap[$family][$key]; |
| 404 |
} |
| 405 |
} |
| 406 |
$this->canvas->selectFont("{$family}.afm"); |
| 407 |
} |
| 408 |
} |
| 409 |
|