commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / vendor / phenx / php-svg-lib / src / Svg / Surface / SurfaceGmagick.php
1 <?php
2 /**
3 * @package php-svg-lib
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
7 */
8
9 namespace Svg\Surface;
10
11 use Svg\Style;
12
13 class SurfaceGmagick implements SurfaceInterface
14 {
15 const DEBUG = false;
16
17 /** @var \GmagickDraw */
18 private $canvas;
19
20 private $width;
21 private $height;
22
23 /** @var Style */
24 private $style;
25
26 public function __construct($w, $h)
27 {
28 if (self::DEBUG) {
29 echo __FUNCTION__ . "\n";
30 }
31 $this->width = $w;
32 $this->height = $h;
33
34 $canvas = new \GmagickDraw();
35
36 $this->canvas = $canvas;
37 }
38
39 function out()
40 {
41 if (self::DEBUG) {
42 echo __FUNCTION__ . "\n";
43 }
44
45 $image = new \Gmagick();
46 $image->newimage($this->width, $this->height);
47 $image->drawimage($this->canvas);
48
49 $tmp = tempnam("", "gm");
50
51 $image->write($tmp);
52
53 return file_get_contents($tmp);
54 }
55
56 public function save()
57 {
58 if (self::DEBUG) {
59 echo __FUNCTION__ . "\n";
60 }
61 $this->canvas->save();
62 }
63
64 public function restore()
65 {
66 if (self::DEBUG) echo __FUNCTION__ . "\n";
67 $this->canvas->restore();
68 }
69
70 public function scale($x, $y)
71 {
72 if (self::DEBUG) echo __FUNCTION__ . "\n";
73 $this->canvas->scale($x, $y);
74 }
75
76 public function rotate($angle)
77 {
78 if (self::DEBUG) echo __FUNCTION__ . "\n";
79 $this->canvas->rotate($angle);
80 }
81
82 public function translate($x, $y)
83 {
84 if (self::DEBUG) echo __FUNCTION__ . "\n";
85 $this->canvas->translate($x, $y);
86 }
87
88 public function transform($a, $b, $c, $d, $e, $f)
89 {
90 if (self::DEBUG) echo __FUNCTION__ . "\n";
91 $this->canvas->concat($a, $b, $c, $d, $e, $f);
92 }
93
94 public function beginPath()
95 {
96 if (self::DEBUG) echo __FUNCTION__ . "\n";
97 // TODO: Implement beginPath() method.
98 }
99
100 public function closePath()
101 {
102 if (self::DEBUG) echo __FUNCTION__ . "\n";
103 $this->canvas->closepath();
104 }
105
106 public function fillStroke()
107 {
108 if (self::DEBUG) echo __FUNCTION__ . "\n";
109 $this->canvas->fill_stroke();
110 }
111
112 public function clip()
113 {
114 if (self::DEBUG) echo __FUNCTION__ . "\n";
115 $this->canvas->clip();
116 }
117
118 public function fillText($text, $x, $y, $maxWidth = null)
119 {
120 if (self::DEBUG) echo __FUNCTION__ . "\n";
121 $this->canvas->set_text_pos($x, $y);
122 $this->canvas->show($text);
123 }
124
125 public function strokeText($text, $x, $y, $maxWidth = null)
126 {
127 if (self::DEBUG) echo __FUNCTION__ . "\n";
128 // TODO: Implement drawImage() method.
129 }
130
131 public function drawImage($image, $sx, $sy, $sw = null, $sh = null, $dx = null, $dy = null, $dw = null, $dh = null)
132 {
133 if (self::DEBUG) echo __FUNCTION__ . "\n";
134
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));
139 }
140
141 $image = tempnam("", "svg");
142 file_put_contents($image, $data);
143 }
144
145 $img = $this->canvas->load_image("auto", $image, "");
146
147 $sy = $sy - $sh;
148 $this->canvas->fit_image($img, $sx, $sy, 'boxsize={' . "$sw $sh" . '} fitmethod=entire');
149 }
150
151 public function lineTo($x, $y)
152 {
153 if (self::DEBUG) echo __FUNCTION__ . "\n";
154 $this->canvas->lineto($x, $y);
155 }
156
157 public function moveTo($x, $y)
158 {
159 if (self::DEBUG) echo __FUNCTION__ . "\n";
160 $this->canvas->moveto($x, $y);
161 }
162
163 public function quadraticCurveTo($cpx, $cpy, $x, $y)
164 {
165 if (self::DEBUG) echo __FUNCTION__ . "\n";
166 // TODO: Implement quadraticCurveTo() method.
167 }
168
169 public function bezierCurveTo($cp1x, $cp1y, $cp2x, $cp2y, $x, $y)
170 {
171 if (self::DEBUG) echo __FUNCTION__ . "\n";
172 $this->canvas->curveto($cp1x, $cp1y, $cp2x, $cp2y, $x, $y);
173 }
174
175 public function arcTo($x1, $y1, $x2, $y2, $radius)
176 {
177 if (self::DEBUG) echo __FUNCTION__ . "\n";
178 }
179
180 public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise = false)
181 {
182 if (self::DEBUG) echo __FUNCTION__ . "\n";
183 $this->canvas->arc($x, $y, $radius, $startAngle, $endAngle);
184 }
185
186 public function circle($x, $y, $radius)
187 {
188 if (self::DEBUG) echo __FUNCTION__ . "\n";
189 $this->canvas->circle($x, $y, $radius);
190 }
191
192 public function ellipse($x, $y, $radiusX, $radiusY, $rotation, $startAngle, $endAngle, $anticlockwise)
193 {
194 if (self::DEBUG) echo __FUNCTION__ . "\n";
195 $this->canvas->ellipse($x, $y, $radiusX, $radiusY);
196 }
197
198 public function fillRect($x, $y, $w, $h)
199 {
200 if (self::DEBUG) echo __FUNCTION__ . "\n";
201 $this->rect($x, $y, $w, $h);
202 $this->fill();
203 }
204
205 public function rect($x, $y, $w, $h)
206 {
207 if (self::DEBUG) echo __FUNCTION__ . "\n";
208 $this->canvas->rect($x, $y, $w, $h);
209 }
210
211 public function fill()
212 {
213 if (self::DEBUG) echo __FUNCTION__ . "\n";
214 $this->canvas->fill();
215 }
216
217 public function strokeRect($x, $y, $w, $h)
218 {
219 if (self::DEBUG) echo __FUNCTION__ . "\n";
220 $this->rect($x, $y, $w, $h);
221 $this->stroke();
222 }
223
224 public function stroke()
225 {
226 if (self::DEBUG) echo __FUNCTION__ . "\n";
227 $this->canvas->stroke();
228 }
229
230 public function endPath()
231 {
232 if (self::DEBUG) echo __FUNCTION__ . "\n";
233 //$this->canvas->endPath();
234 }
235
236 public function measureText($text)
237 {
238 if (self::DEBUG) echo __FUNCTION__ . "\n";
239 $style = $this->getStyle();
240 $font = $this->getFont($style->fontFamily, $style->fontStyle);
241
242 return $this->canvas->stringwidth($text, $font, $this->getStyle()->fontSize);
243 }
244
245 public function getStyle()
246 {
247 if (self::DEBUG) echo __FUNCTION__ . "\n";
248
249 return $this->style;
250 }
251
252 public function setStyle(Style $style)
253 {
254 if (self::DEBUG) echo __FUNCTION__ . "\n";
255
256 $this->style = $style;
257 $canvas = $this->canvas;
258
259 if ($stroke = $style->stroke) {
260 $canvas->setcolor("stroke", "rgb", $stroke[0] / 255, $stroke[1] / 255, $stroke[2] / 255, null);
261 }
262
263 if ($fill = $style->fill) {
264 // $canvas->setcolor("fill", "rgb", $fill[0] / 255, $fill[1] / 255, $fill[2] / 255, null);
265 }
266
267 $opts = array();
268 if ($style->strokeWidth > 0.000001) {
269 $opts[] = "linewidth=$style->strokeWidth";
270 }
271
272 if (in_array($style->strokeLinecap, array("butt", "round", "projecting"))) {
273 $opts[] = "linecap=$style->strokeLinecap";
274 }
275
276 if (in_array($style->strokeLinejoin, array("miter", "round", "bevel"))) {
277 $opts[] = "linejoin=$style->strokeLinejoin";
278 }
279
280 $canvas->set_graphics_option(implode(" ", $opts));
281
282 $font = $this->getFont($style->fontFamily, $style->fontStyle);
283 $canvas->setfont($font, $style->fontSize);
284 }
285
286 private function getFont($family, $style)
287 {
288 $map = array(
289 "serif" => "Times",
290 "sans-serif" => "Helvetica",
291 "fantasy" => "Symbol",
292 "cursive" => "serif",
293 "monospance" => "Courier",
294 );
295
296 $family = strtolower($family);
297 if (isset($map[$family])) {
298 $family = $map[$family];
299 }
300
301 return $this->canvas->load_font($family, "unicode", "fontstyle=$style");
302 }
303 }