4 * @link http://github.com/PhenX/php-svg-lib
5 * @author Fabien Ménager <fabien.menager@gmail.com>
6 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
13 class SurfaceGmagick
implements SurfaceInterface
17 /** @var \GmagickDraw */
26 public function __construct($w, $h)
29 echo __FUNCTION__
. "\n";
34 $canvas = new \
GmagickDraw();
36 $this->canvas
= $canvas;
42 echo __FUNCTION__
. "\n";
45 $image = new \
Gmagick();
46 $image->newimage($this->width
, $this->height
);
47 $image->drawimage($this->canvas
);
49 $tmp = tempnam("", "gm");
53 return file_get_contents($tmp);
56 public function save()
59 echo __FUNCTION__
. "\n";
61 $this->canvas
->save();
64 public function restore()
66 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
67 $this->canvas
->restore();
70 public function scale($x, $y)
72 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
73 $this->canvas
->scale($x, $y);
76 public function rotate($angle)
78 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
79 $this->canvas
->rotate($angle);
82 public function translate($x, $y)
84 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
85 $this->canvas
->translate($x, $y);
88 public function transform($a, $b, $c, $d, $e, $f)
90 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
91 $this->canvas
->concat($a, $b, $c, $d, $e, $f);
94 public function beginPath()
96 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
97 // TODO: Implement beginPath() method.
100 public function closePath()
102 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
103 $this->canvas
->closepath();
106 public function fillStroke()
108 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
109 $this->canvas
->fill_stroke();
112 public function clip()
114 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
115 $this->canvas
->clip();
118 public function fillText($text, $x, $y, $maxWidth = null)
120 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
121 $this->canvas
->set_text_pos($x, $y);
122 $this->canvas
->show($text);
125 public function strokeText($text, $x, $y, $maxWidth = null)
127 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
128 // TODO: Implement drawImage() method.
131 public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null)
133 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
135 if (strpos($image, "data:") === 0) {
136 $data = substr($image, strpos($image, ";") +
1);
137 if (strpos($data, "base64") === 0) {
138 $data = base64_decode(substr($data, 7));
141 $image = tempnam("", "svg");
142 file_put_contents($image, $data);
145 $img = $this->canvas
->load_image("auto", $image, "");
148 $this->canvas
->fit_image($img, $sx, $sy, 'boxsize={' . "$sw $sh" . '} fitmethod=entire');
151 public function lineTo($x, $y)
153 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
154 $this->canvas
->lineto($x, $y);
157 public function moveTo($x, $y)
159 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
160 $this->canvas
->moveto($x, $y);
163 public function quadraticCurveTo($cpx, $cpy, $x, $y)
165 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
166 // TODO: Implement quadraticCurveTo() method.
169 public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y)
171 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
172 $this->canvas
->curveto($cp1x, $cp1y, $cp2x, $cp2y, $x, $y);
175 public function arcTo($x1, $y1, $x2, $y2, $radius)
177 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
180 public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = false)
182 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
183 $this->canvas
->arc($x, $y, $radius, $startAngle, $endAngle);
186 public function circle($x, $y, $radius)
188 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
189 $this->canvas
->circle($x, $y, $radius);
192 public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise)
194 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
195 $this->canvas
->ellipse($x, $y, $radiusX, $radiusY);
198 public function fillRect($x, $y, $w, $h)
200 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
201 $this->rect($x, $y, $w, $h);
205 public function rect($x, $y, $w, $h)
207 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
208 $this->canvas
->rect($x, $y, $w, $h);
211 public function fill()
213 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
214 $this->canvas
->fill();
217 public function strokeRect($x, $y, $w, $h)
219 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
220 $this->rect($x, $y, $w, $h);
224 public function stroke()
226 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
227 $this->canvas
->stroke();
230 public function endPath()
232 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
233 //$this->canvas->endPath();
236 public function measureText($text)
238 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
239 $style = $this->getStyle();
240 $font = $this->getFont($style->fontFamily
, $style->fontStyle
);
242 return $this->canvas
->stringwidth($text, $font, $this->getStyle()->fontSize
);
245 public function getStyle()
247 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
252 public function setStyle(Style
$style)
254 if (self
::DEBUG
) echo __FUNCTION__
. "\n";
256 $this->style
= $style;
257 $canvas = $this->canvas
;
259 if ($stroke = $style->stroke
) {
260 $canvas->setcolor("stroke", "rgb", $stroke[0] / 255, $stroke[1] / 255, $stroke[2] / 255, null);
263 if ($fill = $style->fill
) {
264 // $canvas->setcolor("fill", "rgb", $fill[0] / 255, $fill[1] / 255, $fill[2] / 255, null);
268 if ($style->strokeWidth
> 0.000001) {
269 $opts[] = "linewidth=$style->strokeWidth";
272 if (in_array($style->strokeLinecap
, array("butt", "round", "projecting"))) {
273 $opts[] = "linecap=$style->strokeLinecap";
276 if (in_array($style->strokeLinejoin
, array("miter", "round", "bevel"))) {
277 $opts[] = "linejoin=$style->strokeLinejoin";
280 $canvas->set_graphics_option(implode(" ", $opts));
282 $font = $this->getFont($style->fontFamily
, $style->fontStyle
);
283 $canvas->setfont($font, $style->fontSize
);
286 private function getFont($family, $style)
290 "sans-serif" => "Helvetica",
291 "fantasy" => "Symbol",
292 "cursive" => "serif",
293 "monospance" => "Courier",
296 $family = strtolower($family);
297 if (isset($map[$family])) {
298 $family = $map[$family];
301 return $this->canvas
->load_font($family, "unicode", "fontstyle=$style");