Source of file SurfacePDFLib.php
Size: 11,511 Bytes - Last Modified: 2021-12-23T10:11:59+00:00
/var/www/docs.ssmods.com/process/src/ext/phenx/php-svg-lib/src/Svg/Surface/SurfacePDFLib.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 | <?php /** * @package php-svg-lib * @link http://github.com/PhenX/php-svg-lib * @author Fabien M�nager <fabien.menager@gmail.com> * @license GNU LGPLv3+ http://www.gnu.org/copyleft/lesser.html */ namespace Svg\Surface; use Svg\Style; use Svg\Document; class SurfacePDFLib implements SurfaceInterface { const DEBUG = false; private $canvas; private $width; private $height; /** @var Style */ private $style; public function __construct(Document $doc, $canvas = null) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $dimensions = $doc->getDimensions(); $w = $dimensions["width"]; $h = $dimensions["height"]; if (!$canvas) { $canvas = new \PDFlib(); /* all strings are expected as utf8 */ $canvas->set_option("stringformat=utf8"); $canvas->set_option("errorpolicy=return"); /* open new PDF file; insert a file name to create the PDF on disk */ if ($canvas->begin_document("", "") == 0) { die("Error: " . $canvas->get_errmsg()); } $canvas->set_info("Creator", "PDFlib starter sample"); $canvas->set_info("Title", "starter_graphics"); $canvas->begin_page_ext($w, $h, ""); } // Flip PDF coordinate system so that the origin is in // the top left rather than the bottom left $canvas->setmatrix( 1, 0, 0, -1, 0, $h ); $this->width = $w; $this->height = $h; $this->canvas = $canvas; } function out() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->end_page_ext(""); $this->canvas->end_document(""); return $this->canvas->get_buffer(); } public function save() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->save(); } public function restore() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->restore(); } public function scale($x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->scale($x, $y); } public function rotate($angle) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->rotate($angle); } public function translate($x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->translate($x, $y); } public function transform($a, $b, $c, $d, $e, $f) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->concat($a, $b, $c, $d, $e, $f); } public function beginPath() { if (self::DEBUG) echo __FUNCTION__ . "\n"; // TODO: Implement beginPath() method. } public function closePath() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->closepath(); } public function fillStroke() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->fill_stroke(); } public function clip() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->clip(); } public function fillText($text, $x, $y, $maxWidth = null) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->set_text_pos($x, $y); $this->canvas->show($text); } public function strokeText($text, $x, $y, $maxWidth = null) { if (self::DEBUG) echo __FUNCTION__ . "\n"; // TODO: Implement drawImage() method. } public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null) { if (self::DEBUG) echo __FUNCTION__ . "\n"; if (strpos($image, "data:") === 0) { $data = substr($image, strpos($image, ";") + 1); if (strpos($data, "base64") === 0) { $data = base64_decode(substr($data, 7)); } } else { $data = file_get_contents($image); } $image = tempnam("", "svg"); file_put_contents($image, $data); $img = $this->canvas->load_image("auto", $image, ""); $sy = $sy - $sh; $this->canvas->fit_image($img, $sx, $sy, 'boxsize={' . "$sw $sh" . '} fitmethod=entire'); unlink($image); } public function lineTo($x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->lineto($x, $y); } public function moveTo($x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->moveto($x, $y); } public function quadraticCurveTo($cpx, $cpy, $x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; // FIXME not accurate $this->canvas->curveTo($cpx, $cpy, $cpx, $cpy, $x, $y); } public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->curveto($cp1x, $cp1y, $cp2x, $cp2y, $x, $y); } public function arcTo($x1, $y1, $x2, $y2, $radius) { if (self::DEBUG) echo __FUNCTION__ . "\n"; } public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = false) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->arc($x, $y, $radius, $startAngle, $endAngle); } public function circle($x, $y, $radius) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->circle($x, $y, $radius); } public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->ellipse($x, $y, $radiusX, $radiusY); } public function fillRect($x, $y, $w, $h) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->rect($x, $y, $w, $h); $this->fill(); } public function rect($x, $y, $w, $h, $rx = 0, $ry = 0) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $canvas = $this->canvas; if ($rx <= 0.000001/* && $ry <= 0.000001*/) { $canvas->rect($x, $y, $w, $h); return; } /* Define a path for a rectangle with corners rounded by a given radius. * Start from the lower left corner and proceed counterclockwise. */ $canvas->moveto($x + $rx, $y); /* Start of the arc segment in the lower right corner */ $canvas->lineto($x + $w - $rx, $y); /* Arc segment in the lower right corner */ $canvas->arc($x + $w - $rx, $y + $rx, $rx, 270, 360); /* Start of the arc segment in the upper right corner */ $canvas->lineto($x + $w, $y + $h - $rx ); /* Arc segment in the upper right corner */ $canvas->arc($x + $w - $rx, $y + $h - $rx, $rx, 0, 90); /* Start of the arc segment in the upper left corner */ $canvas->lineto($x + $rx, $y + $h); /* Arc segment in the upper left corner */ $canvas->arc($x + $rx, $y + $h - $rx, $rx, 90, 180); /* Start of the arc segment in the lower left corner */ $canvas->lineto($x , $y + $rx); /* Arc segment in the lower left corner */ $canvas->arc($x + $rx, $y + $rx, $rx, 180, 270); } public function fill() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->fill(); } public function strokeRect($x, $y, $w, $h) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->rect($x, $y, $w, $h); $this->stroke(); } public function stroke() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->stroke(); } public function endPath() { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->canvas->endPath(); } public function measureText($text) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $style = $this->getStyle(); $font = $this->getFont($style->fontFamily, $style->fontStyle); return $this->canvas->stringwidth($text, $font, $this->getStyle()->fontSize); } public function getStyle() { if (self::DEBUG) echo __FUNCTION__ . "\n"; return $this->style; } public function setStyle(Style $style) { if (self::DEBUG) echo __FUNCTION__ . "\n"; $this->style = $style; $canvas = $this->canvas; if ($stroke = $style->stroke && is_array($style->stroke)) { $canvas->setcolor( "stroke", "rgb", $stroke[0] / 255, $stroke[1] / 255, $stroke[2] / 255, null ); } if ($fill = $style->fill && is_array($style->fill)) { $canvas->setcolor( "fill", "rgb", $fill[0] / 255, $fill[1] / 255, $fill[2] / 255, null ); } if ($fillRule = strtolower($style->fillRule)) { $map = array( "nonzero" => "winding", "evenodd" => "evenodd", ); if (isset($map[$fillRule])) { $fillRule = $map[$fillRule]; $canvas->set_parameter("fillrule", $fillRule); } } $opts = array(); if ($style->strokeWidth > 0.000001) { $opts[] = "linewidth=$style->strokeWidth"; } if (in_array($style->strokeLinecap, array("butt", "round", "projecting"))) { $opts[] = "linecap=$style->strokeLinecap"; } if (in_array($style->strokeLinejoin, array("miter", "round", "bevel"))) { $opts[] = "linejoin=$style->strokeLinejoin"; } $canvas->set_graphics_option(implode(" ", $opts)); $opts = array(); $opacity = $style->opacity; if ($opacity !== null && $opacity < 1.0) { $opts[] = "opacityfill=$opacity"; $opts[] = "opacitystroke=$opacity"; } else { $fillOpacity = $style->fillOpacity; if ($fillOpacity !== null && $fillOpacity < 1.0) { $opts[] = "opacityfill=$fillOpacity"; } $strokeOpacity = $style->strokeOpacity; if ($strokeOpacity !== null && $strokeOpacity < 1.0) { $opts[] = "opacitystroke=$strokeOpacity"; } } if (count($opts)) { $gs = $canvas->create_gstate(implode(" ", $opts)); $canvas->set_gstate($gs); } $font = $this->getFont($style->fontFamily, $style->fontStyle); if ($font) { $canvas->setfont($font, $style->fontSize); } } private function getFont($family, $style) { $map = array( "serif" => "Times", "sans-serif" => "Helvetica", "fantasy" => "Symbol", "cursive" => "Times", "monospace" => "Courier", "arial" => "Helvetica", "verdana" => "Helvetica", ); $family = strtolower($family); if (isset($map[$family])) { $family = $map[$family]; } return $this->canvas->load_font($family, "unicode", "fontstyle=$style"); } public function setFont($family, $style, $weight) { // TODO: Implement setFont() method. } } |