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