4 * @link http://github.com/PhenX/php-svg-lib
5 * @author Fabien M�nager <fabien.menager@gmail.com>
6 * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html
14 class SurfaceCpdf
implements SurfaceInterface
18 /** @var \CPdf\CPdf */
27 public function __construct(Document
$doc, $canvas = null)
29 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
31 $dimensions = $doc->getDimensions();
32 $w = $dimensions["width"];
33 $h = $dimensions["height"];
36 $canvas = new \CPdf\
CPdf(array(0, 0, $w, $h));
37 $refl = new \
ReflectionClass($canvas);
38 $canvas->fontcache
= realpath(dirname($refl->getFileName()) . "/../../fonts/")."/";
41 // Flip PDF coordinate system so that the origin is in
42 // the top left rather than the bottom left
43 $canvas->transform(array(
52 $this->canvas
= $canvas;
57 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
58 return $this->canvas
->output();
61 public function save()
63 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
64 $this->canvas
->save();
67 public function restore()
69 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
70 $this->canvas
->restore();
73 public function scale($x, $y)
75 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
77 $this->transform($x, 0, 0, $y, 0, 0);
80 public function rotate($angle)
82 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
95 public function translate($x, $y)
97 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
106 public function transform($a, $b, $c, $d, $e, $f)
108 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
110 $this->canvas
->transform(array($a, $b, $c, $d, $e, $f));
113 public function beginPath()
115 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
116 // TODO: Implement beginPath() method.
119 public function closePath()
121 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
122 $this->canvas
->closePath();
125 public function fillStroke()
127 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
128 $this->canvas
->fillStroke();
131 public function clip()
133 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
134 $this->canvas
->clip();
137 public function fillText($text, $x, $y, $maxWidth = null)
139 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
140 $this->canvas
->addText($x, $y, $this->style
->fontSize
, $text);
143 public function strokeText($text, $x, $y, $maxWidth = null)
145 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
146 $this->canvas
->addText($x, $y, $this->style
->fontSize
, $text);
149 public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null)
151 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
153 if (strpos($image, "data:") === 0) {
154 $parts = explode(',', $image, 2);
159 $token = strtok($parts[0], ';');
160 while ($token !== false) {
161 if ($token == 'base64') {
165 $token = strtok(';');
169 $data = base64_decode($data);
173 $data = file_get_contents($image);
176 $image = tempnam("", "svg");
177 file_put_contents($image, $data);
179 $img = $this->image($image, $sx, $sy, $sw, $sh, "normal");
185 public static function getimagesize($filename)
187 static $cache = array();
189 if (isset($cache[$filename])) {
190 return $cache[$filename];
193 list($width, $height, $type) = getimagesize($filename);
195 if ($width == null ||
$height == null) {
196 $data = file_get_contents($filename, null, null, 0, 26);
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
;
206 return $cache[$filename] = array($width, $height, $type);
209 function image($img, $x, $y, $w, $h, $resolution = "normal")
211 list($width, $height, $type) = $this->getimagesize($img);
215 $this->canvas
->addJpegFromFile($img, $x, $y - $h, $w, $h);
220 // @todo use cache for BMP and GIF
221 $img = $this->_convert_gif_bmp_to_png($img, $type);
224 $this->canvas
->addPngFromFile($img, $x, $y - $h, $w, $h);
231 public function lineTo($x, $y)
233 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
234 $this->canvas
->lineTo($x, $y);
237 public function moveTo($x, $y)
239 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
240 $this->canvas
->moveTo($x, $y);
243 public function quadraticCurveTo($cpx, $cpy, $x, $y)
245 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
247 // FIXME not accurate
248 $this->canvas
->quadTo($cpx, $cpy, $x, $y);
251 public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y)
253 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
254 $this->canvas
->curveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y);
257 public function arcTo($x1, $y1, $x2, $y2, $radius)
259 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
262 public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = false)
264 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
265 $this->canvas
->ellipse($x, $y, $radius, $radius, 0, 8, $startAngle, $endAngle, false, false, false, true);
268 public function circle($x, $y, $radius)
270 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
271 $this->canvas
->ellipse($x, $y, $radius, $radius, 0, 8, 0, 360, true, false, false, false);
274 public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise)
276 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
277 $this->canvas
->ellipse($x, $y, $radiusX, $radiusY, 0, 8, 0, 360, false, false, false, false);
280 public function fillRect($x, $y, $w, $h)
282 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
283 $this->rect($x, $y, $w, $h);
287 public function rect($x, $y, $w, $h, $rx = 0, $ry = 0)
289 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
291 $canvas = $this->canvas
;
293 if ($rx <= 0.000001/* && $ry <= 0.000001*/) {
294 $canvas->rect($x, $y, $w, $h);
299 $rx = min($rx, $w / 2);
300 $rx = min($rx, $h / 2);
302 /* Define a path for a rectangle with corners rounded by a given radius.
303 * Start from the lower left corner and proceed counterclockwise.
305 $this->moveTo($x +
$rx, $y);
307 /* Start of the arc segment in the lower right corner */
308 $this->lineTo($x +
$w - $rx, $y);
310 /* Arc segment in the lower right corner */
311 $this->arc($x +
$w - $rx, $y +
$rx, $rx, 270, 360);
313 /* Start of the arc segment in the upper right corner */
314 $this->lineTo($x +
$w, $y +
$h - $rx );
316 /* Arc segment in the upper right corner */
317 $this->arc($x +
$w - $rx, $y +
$h - $rx, $rx, 0, 90);
319 /* Start of the arc segment in the upper left corner */
320 $this->lineTo($x +
$rx, $y +
$h);
322 /* Arc segment in the upper left corner */
323 $this->arc($x +
$rx, $y +
$h - $rx, $rx, 90, 180);
325 /* Start of the arc segment in the lower left corner */
326 $this->lineTo($x , $y +
$rx);
328 /* Arc segment in the lower left corner */
329 $this->arc($x +
$rx, $y +
$rx, $rx, 180, 270);
332 public function fill()
334 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
335 $this->canvas
->fill();
338 public function strokeRect($x, $y, $w, $h)
340 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
341 $this->rect($x, $y, $w, $h);
345 public function stroke()
347 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
348 $this->canvas
->stroke();
351 public function endPath()
353 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
354 $this->canvas
->endPath();
357 public function measureText($text)
359 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
360 $style = $this->getStyle();
361 $this->setFont($style->fontFamily
, $style->fontStyle
, $style->fontWeight
);
363 return $this->canvas
->getTextWidth($this->getStyle()->fontSize
, $text);
366 public function getStyle()
368 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
372 public function setStyle(Style
$style)
374 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
376 $this->style
= $style;
377 $canvas = $this->canvas
;
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);
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);
387 if ($fillRule = strtolower($style->fillRule
)) {
388 $canvas->setFillRule($fillRule);
391 $opacity = $style->opacity
;
392 if ($opacity !== null && $opacity < 1.0) {
393 $canvas->setLineTransparency("Normal", $opacity);
394 $canvas->currentLineTransparency
= null;
396 $canvas->setFillTransparency("Normal", $opacity);
397 $canvas->currentFillTransparency
= null;
400 $fillOpacity = $style->fillOpacity
;
401 if ($fillOpacity !== null && $fillOpacity < 1.0) {
402 $canvas->setFillTransparency("Normal", $fillOpacity);
403 $canvas->currentFillTransparency
= null;
406 $strokeOpacity = $style->strokeOpacity
;
407 if ($strokeOpacity !== null && $strokeOpacity < 1.0) {
408 $canvas->setLineTransparency("Normal", $strokeOpacity);
409 $canvas->currentLineTransparency
= null;
414 if ($style->strokeDasharray
) {
415 $dashArray = preg_split('/\s*,\s*/', $style->strokeDasharray
);
418 $canvas->setLineStyle(
420 $style->strokeLinecap
,
421 $style->strokeLinejoin
,
425 $this->setFont($style->fontFamily
, $style->fontStyle
, $style->fontWeight
);
428 public function setFont($family, $style, $weight)
432 "sans-serif" => "Helvetica",
433 "fantasy" => "Symbol",
434 "cursive" => "Times",
435 "monospace" => "Courier",
437 "arial" => "Helvetica",
438 "verdana" => "Helvetica",
442 'Helvetica' => array(
443 'b' => 'Helvetica-Bold',
444 'i' => 'Helvetica-Oblique',
445 'bi' => 'Helvetica-BoldOblique',
448 'b' => 'Courier-Bold',
449 'i' => 'Courier-Oblique',
450 'bi' => 'Courier-BoldOblique',
455 'i' => 'Times-Italic',
456 'bi' => 'Times-BoldItalic',
460 $family = strtolower($family);
461 $style = strtolower($style);
462 $weight = strtolower($weight);
464 if (isset($map[$family])) {
465 $family = $map[$family];
468 if (isset($styleMap[$family])) {
471 if ($weight === "bold" ||
$weight === "bolder" ||
(is_numeric($weight) && $weight >= 600)) {
475 if ($style === "italic" ||
$style === "oblique") {
479 if (isset($styleMap[$family][$key])) {
480 $family = $styleMap[$family][$key];
484 $this->canvas
->selectFont("$family.afm");