1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: abstract_renderer.cls.php,v $ |
---|
6 | * Created on: 2004-06-01 |
---|
7 | * |
---|
8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
9 | * |
---|
10 | * This library is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU Lesser General Public |
---|
12 | * License as published by the Free Software Foundation; either |
---|
13 | * version 2.1 of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * This library is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public License |
---|
21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
23 | * 02111-1307 USA |
---|
24 | * |
---|
25 | * Alternatively, you may distribute this software under the terms of the |
---|
26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
29 | * |
---|
30 | * The latest version of DOMPDF might be available at: |
---|
31 | * http://www.digitaljunkies.ca/dompdf |
---|
32 | * |
---|
33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
34 | * @copyright 2004 Benj Carson |
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
36 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
37 | * @package dompdf |
---|
38 | * @version 0.5.1 |
---|
39 | * |
---|
40 | * Changes |
---|
41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
42 | * @version 0.5.1.htischer.20090507 |
---|
43 | * - On background image |
---|
44 | * - Clip invisible areas from background images, then merge identical |
---|
45 | * image/size/offset to a single image. |
---|
46 | * - Fix rounding of background image size. |
---|
47 | * - Fix background image position given as percent |
---|
48 | * - Check if identical image is already cached by cpdf. Then do not create |
---|
49 | * duplicates to save memory and CPU time |
---|
50 | * - Fix skipping of image repetition if area is too small |
---|
51 | * - Do not create temporary files, but pass gd object directly |
---|
52 | */ |
---|
53 | |
---|
54 | /* $Id: abstract_renderer.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
55 | |
---|
56 | /** |
---|
57 | * Base renderer class |
---|
58 | * |
---|
59 | * @access private |
---|
60 | * @package dompdf |
---|
61 | */ |
---|
62 | abstract class Abstract_Renderer { |
---|
63 | |
---|
64 | /** |
---|
65 | * Rendering backend |
---|
66 | * |
---|
67 | * @var Canvas |
---|
68 | */ |
---|
69 | protected $_canvas; |
---|
70 | |
---|
71 | /** |
---|
72 | * Current dompdf instance |
---|
73 | * |
---|
74 | * @var DOMPDF |
---|
75 | */ |
---|
76 | protected $_dompdf; |
---|
77 | |
---|
78 | /** |
---|
79 | * Class constructor |
---|
80 | * |
---|
81 | * @param DOMPDF $dompdf The current dompdf instance |
---|
82 | */ |
---|
83 | function __construct(DOMPDF $dompdf) { |
---|
84 | $this->_dompdf = $dompdf; |
---|
85 | $this->_canvas = $dompdf->get_canvas(); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Render a frame. |
---|
90 | * |
---|
91 | * Specialized in child classes |
---|
92 | * |
---|
93 | * @param Frame $frame The frame to render |
---|
94 | */ |
---|
95 | abstract function render(Frame $frame); |
---|
96 | |
---|
97 | //........................................................................ |
---|
98 | |
---|
99 | /** |
---|
100 | * Render a background image over a rectangular area |
---|
101 | * |
---|
102 | * @param string $img The background image to load |
---|
103 | * @param float $x The left edge of the rectangular area |
---|
104 | * @param float $y The top edge of the rectangular area |
---|
105 | * @param float $width The width of the rectangular area |
---|
106 | * @param float $height The height of the rectangular area |
---|
107 | * @param Style $style The associated Style object |
---|
108 | */ |
---|
109 | protected function _background_image($url, $x, $y, $width, $height, $style) { |
---|
110 | $sheet = $style->get_stylesheet(); |
---|
111 | |
---|
112 | // Skip degenerate cases |
---|
113 | if ( $width == 0 || $height == 0 ) |
---|
114 | return; |
---|
115 | |
---|
116 | //debugpng |
---|
117 | if (DEBUGPNG) print '[_background_image '.$url.']'; |
---|
118 | |
---|
119 | list($img, $ext) = Image_Cache::resolve_url($url, |
---|
120 | $sheet->get_protocol(), |
---|
121 | $sheet->get_host(), |
---|
122 | $sheet->get_base_path()); |
---|
123 | |
---|
124 | // Bail if the image is no good |
---|
125 | if ( $img == DOMPDF_LIB_DIR . "/res/broken_image.png" ) |
---|
126 | return; |
---|
127 | |
---|
128 | //Try to optimize away reading and composing of same background multiple times |
---|
129 | //Postponing read with imagecreatefrom ...() |
---|
130 | //final composition paramters and name not known yet |
---|
131 | //Therefore read dimension directly from file, instead of creating gd object first. |
---|
132 | //$img_w = imagesx($src); $img_h = imagesy($src); |
---|
133 | |
---|
134 | list($img_w, $img_h) = getimagesize($img); |
---|
135 | if (!isset($img_w) || $img_w == 0 || !isset($img_h) || $img_h == 0) { |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | $repeat = $style->background_repeat; |
---|
140 | $bg_color = $style->background_color; |
---|
141 | |
---|
142 | //Increase background resolution and dependent box size according to image resolution to be placed in |
---|
143 | //Then image can be copied in without resize |
---|
144 | $bg_width = round((float)($width * DOMPDF_DPI) / 72); |
---|
145 | $bg_height = round((float)($height * DOMPDF_DPI) / 72); |
---|
146 | |
---|
147 | //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel |
---|
148 | |
---|
149 | list($bg_x, $bg_y) = $style->background_position; |
---|
150 | |
---|
151 | if ( is_percent($bg_x) ) { |
---|
152 | // The point $bg_x % from the left edge of the image is placed |
---|
153 | // $bg_x % from the left edge of the background rectangle |
---|
154 | $p = ((float)$bg_x)/100.0; |
---|
155 | $x1 = $p * $img_w; |
---|
156 | $x2 = $p * $bg_width; |
---|
157 | |
---|
158 | $bg_x = round($x2 - $x1); |
---|
159 | } else { |
---|
160 | $bg_x = round((float)($style->length_in_pt($bg_x)*DOMPDF_DPI) / 72); |
---|
161 | } |
---|
162 | |
---|
163 | if ( is_percent($bg_y) ) { |
---|
164 | // The point $bg_y % from the left edge of the image is placed |
---|
165 | // $bg_y % from the left edge of the background rectangle |
---|
166 | $p = ((float)$bg_y)/100.0; |
---|
167 | $y1 = $p * $img_h; |
---|
168 | $y2 = $p * $bg_height; |
---|
169 | |
---|
170 | $bg_y = round($y2 - $y1); |
---|
171 | } else { |
---|
172 | $bg_y = round((float)($style->length_in_pt($bg_y)*DOMPDF_DPI) / 72); |
---|
173 | } |
---|
174 | |
---|
175 | //clip background to the image area on partial repeat. Nothing to do if img off area |
---|
176 | //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area |
---|
177 | //On no repeat with positive offset: move size/start to have offset==0 |
---|
178 | //Handle x/y Dimensions separately |
---|
179 | |
---|
180 | if ( $repeat != "repeat" && $repeat != "repeat-x" ) { |
---|
181 | //No repeat x |
---|
182 | if ($bg_x < 0) { |
---|
183 | $bg_width = $img_w + $bg_x; |
---|
184 | } else { |
---|
185 | $x += ($bg_x * 72)/DOMPDF_DPI; |
---|
186 | $bg_width = $bg_width - $bg_x; |
---|
187 | if ($bg_width > $img_w) { |
---|
188 | $bg_width = $img_w; |
---|
189 | } |
---|
190 | $bg_x = 0; |
---|
191 | } |
---|
192 | if ($bg_width <= 0) { |
---|
193 | return; |
---|
194 | } |
---|
195 | $width = (float)($bg_width * 72)/DOMPDF_DPI; |
---|
196 | } else { |
---|
197 | //repeat x |
---|
198 | if ($bg_x < 0) { |
---|
199 | $bg_x = - ((-$bg_x) % $img_w); |
---|
200 | } else { |
---|
201 | $bg_x = $bg_x % $img_w; |
---|
202 | if ($bg_x > 0) { |
---|
203 | $bg_x -= $img_w; |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | if ( $repeat != "repeat" && $repeat != "repeat-y" ) { |
---|
209 | //no repeat y |
---|
210 | if ($bg_y < 0) { |
---|
211 | $bg_height = $img_h + $bg_y; |
---|
212 | } else { |
---|
213 | $y += ($bg_y * 72)/DOMPDF_DPI; |
---|
214 | $bg_height = $bg_height - $bg_y; |
---|
215 | if ($bg_height > $img_h) { |
---|
216 | $bg_height = $img_h; |
---|
217 | } |
---|
218 | $bg_y = 0; |
---|
219 | } |
---|
220 | if ($bg_height <= 0) { |
---|
221 | return; |
---|
222 | } |
---|
223 | $height = (float)($bg_height * 72)/DOMPDF_DPI; |
---|
224 | } else { |
---|
225 | //repeat y |
---|
226 | if ($bg_y < 0) { |
---|
227 | $bg_y = - ((-$bg_y) % $img_h); |
---|
228 | } else { |
---|
229 | $bg_y = $bg_y % $img_h; |
---|
230 | if ($bg_y > 0) { |
---|
231 | $bg_y -= $img_h; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | //Optimization, if repeat has no effect |
---|
237 | if ( $repeat == "repeat" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height ) { |
---|
238 | $repeat = "repeat-x"; |
---|
239 | } |
---|
240 | if ( $repeat == "repeat" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width ) { |
---|
241 | $repeat = "repeat-y"; |
---|
242 | } |
---|
243 | if ( ($repeat == "repeat-x" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width) || |
---|
244 | ($repeat == "repeat-y" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height) ) { |
---|
245 | $repeat = "no-repeat"; |
---|
246 | } |
---|
247 | |
---|
248 | //Use filename as indicator only |
---|
249 | //different names for different variants to have different copies in the pdf |
---|
250 | //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color) |
---|
251 | //Note: Here, bg_* are the start values, not end values after going through the tile loops! |
---|
252 | |
---|
253 | $filedummy = $img; |
---|
254 | |
---|
255 | /* |
---|
256 | //Make shorter strings with limited characters for cache associative array index - needed? |
---|
257 | //Strip common base path - server root, explicite temp, default temp; remove unwanted characters; |
---|
258 | $filedummy = strtr($filedummy,"\\:","//"); |
---|
259 | $p = strtr($_SERVER["DOCUMENT_ROOT"],"\\:","//"); |
---|
260 | $l = strlen($p); |
---|
261 | if ( substr($filedummy,0,$l) == $p) { |
---|
262 | $filedummy = substr($filedummy,$l); |
---|
263 | } else { |
---|
264 | $p = strtr(DOMPDF_TEMP_DIR,"\\:","//"); |
---|
265 | $l = strlen($p); |
---|
266 | if ( substr($filedummy,0,$l) == $p) { |
---|
267 | $filedummy = substr($filedummy,$l); |
---|
268 | } else { |
---|
269 | $p = strtr(sys_get_temp_dir(),"\\:","//"); |
---|
270 | $l = strlen($p); |
---|
271 | if ( substr($filedummy,0,$l) == $p) { |
---|
272 | $filedummy = substr($filedummy,$l); |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | */ |
---|
277 | |
---|
278 | $filedummy .= '_'.$bg_width.'_'.$bg_height.'_'.$bg_x.'_'.$bg_y.'_'.$repeat; |
---|
279 | //debugpng |
---|
280 | //if (DEBUGPNG) print '<pre>[_background_image name '.$filedummy.']</pre>'; |
---|
281 | |
---|
282 | //Optimization to avoid multiple times rendering the same image. |
---|
283 | //If check functions are existing and identical image already cached, |
---|
284 | //then skip creation of duplicate, because it is not needed by addImagePng |
---|
285 | if ( method_exists( $this->_canvas, "get_cpdf" ) && |
---|
286 | method_exists( $this->_canvas->get_cpdf(), "addImagePng" ) && |
---|
287 | method_exists( $this->_canvas->get_cpdf(), "image_iscached" ) && |
---|
288 | $this->_canvas->get_cpdf()->image_iscached($filedummy) ) { |
---|
289 | $bg = null; |
---|
290 | |
---|
291 | //debugpng |
---|
292 | //if (DEBUGPNG) print '[_background_image skip]'; |
---|
293 | |
---|
294 | } else { |
---|
295 | |
---|
296 | // Create a new image to fit over the background rectangle |
---|
297 | $bg = imagecreatetruecolor($bg_width, $bg_height); |
---|
298 | //anyway default |
---|
299 | //imagealphablending($img, true); |
---|
300 | |
---|
301 | switch (strtolower($ext)) { |
---|
302 | |
---|
303 | case "png": |
---|
304 | $src = imagecreatefrompng($img); |
---|
305 | break; |
---|
306 | |
---|
307 | case "jpg": |
---|
308 | case "jpeg": |
---|
309 | $src = imagecreatefromjpeg($img); |
---|
310 | break; |
---|
311 | |
---|
312 | case "gif": |
---|
313 | $src = imagecreatefromgif($img); |
---|
314 | break; |
---|
315 | |
---|
316 | default: |
---|
317 | return; // Unsupported image type |
---|
318 | } |
---|
319 | |
---|
320 | if ($src == null) { |
---|
321 | return; |
---|
322 | } |
---|
323 | |
---|
324 | //Background color if box is not relevant here |
---|
325 | //Non transparent image: box clipped to real size. Background non relevant. |
---|
326 | //Transparent image: The image controls the transparency and lets shine through whatever background. |
---|
327 | //However on transparent imaage preset the composed image with the transparency color, |
---|
328 | //to keep the transparency when copying over the non transparent parts of the tiles. |
---|
329 | $ti = imagecolortransparent($src); |
---|
330 | if ($ti >= 0) { |
---|
331 | $tc = imagecolorsforindex($src,$ti); |
---|
332 | $ti = imagecolorallocate($bg,$tc['red'],$tc['green'],$tc['blue']); |
---|
333 | imagefill($bg,0,0,$ti); |
---|
334 | imagecolortransparent($bg, $ti); |
---|
335 | } |
---|
336 | |
---|
337 | //This has only an effect for the non repeatable dimension. |
---|
338 | //compute start of src and dest coordinates of the single copy |
---|
339 | if ( $bg_x < 0 ) { |
---|
340 | $dst_x = 0; |
---|
341 | $src_x = -$bg_x; |
---|
342 | } else { |
---|
343 | $src_x = 0; |
---|
344 | $dst_x = $bg_x; |
---|
345 | } |
---|
346 | |
---|
347 | if ( $bg_y < 0 ) { |
---|
348 | $dst_y = 0; |
---|
349 | $src_y = -$bg_y; |
---|
350 | } else { |
---|
351 | $src_y = 0; |
---|
352 | $dst_y = $bg_y; |
---|
353 | } |
---|
354 | |
---|
355 | //For historical reasons exchange meanings of variables: |
---|
356 | //start_* will be the start values, while bg_* will be the temporary start values in the loops |
---|
357 | $start_x = $bg_x; |
---|
358 | $start_y = $bg_y; |
---|
359 | |
---|
360 | // Copy regions from the source image to the background |
---|
361 | |
---|
362 | if ( $repeat == "no-repeat" ) { |
---|
363 | |
---|
364 | // Simply place the image on the background |
---|
365 | imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h); |
---|
366 | |
---|
367 | } else if ( $repeat == "repeat-x" ) { |
---|
368 | |
---|
369 | for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) { |
---|
370 | if ( $bg_x < 0 ) { |
---|
371 | $dst_x = 0; |
---|
372 | $src_x = -$bg_x; |
---|
373 | $w = $img_w + $bg_x; |
---|
374 | } else { |
---|
375 | $dst_x = $bg_x; |
---|
376 | $src_x = 0; |
---|
377 | $w = $img_w; |
---|
378 | } |
---|
379 | imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h); |
---|
380 | } |
---|
381 | |
---|
382 | } else if ( $repeat == "repeat-y" ) { |
---|
383 | |
---|
384 | for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) { |
---|
385 | if ( $bg_y < 0 ) { |
---|
386 | $dst_y = 0; |
---|
387 | $src_y = -$bg_y; |
---|
388 | $h = $img_h + $bg_y; |
---|
389 | } else { |
---|
390 | $dst_y = $bg_y; |
---|
391 | $src_y = 0; |
---|
392 | $h = $img_h; |
---|
393 | } |
---|
394 | imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h); |
---|
395 | |
---|
396 | } |
---|
397 | |
---|
398 | } else if ( $repeat == "repeat" ) { |
---|
399 | |
---|
400 | for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) { |
---|
401 | for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) { |
---|
402 | |
---|
403 | if ( $bg_x < 0 ) { |
---|
404 | $dst_x = 0; |
---|
405 | $src_x = -$bg_x; |
---|
406 | $w = $img_w + $bg_x; |
---|
407 | } else { |
---|
408 | $dst_x = $bg_x; |
---|
409 | $src_x = 0; |
---|
410 | $w = $img_w; |
---|
411 | } |
---|
412 | |
---|
413 | if ( $bg_y < 0 ) { |
---|
414 | $dst_y = 0; |
---|
415 | $src_y = -$bg_y; |
---|
416 | $h = $img_h + $bg_y; |
---|
417 | } else { |
---|
418 | $dst_y = $bg_y; |
---|
419 | $src_y = 0; |
---|
420 | $h = $img_h; |
---|
421 | } |
---|
422 | imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h); |
---|
423 | } |
---|
424 | } |
---|
425 | } else { |
---|
426 | print 'Unknown repeat!'; |
---|
427 | } |
---|
428 | |
---|
429 | } /* End optimize away creation of duplicates */ |
---|
430 | |
---|
431 | //img: image url string |
---|
432 | //img_w, img_h: original image size in px |
---|
433 | //width, height: box size in pt |
---|
434 | //bg_width, bg_height: box size in px |
---|
435 | //x, y: left/top edge of box on page in pt |
---|
436 | //start_x, start_y: placement of image relativ to pattern |
---|
437 | //$repeat: repeat mode |
---|
438 | //$bg: GD object of result image |
---|
439 | //$src: GD object of original image |
---|
440 | //When using cpdf and optimization to direct png creation from gd object is available, |
---|
441 | //don't create temp file, but place gd object directly into the pdf |
---|
442 | if ( method_exists( $this->_canvas, "get_cpdf" ) && method_exists( $this->_canvas->get_cpdf(), "addImagePng" ) ) { |
---|
443 | //Note: CPDF_Adapter image converts y position |
---|
444 | $this->_canvas->get_cpdf()->addImagePng( |
---|
445 | $filedummy, |
---|
446 | $x, $this->_canvas->get_height() - $y - $height, $width, $height, $bg); |
---|
447 | } else { |
---|
448 | $tmp_file = tempnam(DOMPDF_TEMP_DIR, "bg_dompdf_img_").'.png'; |
---|
449 | //debugpng |
---|
450 | if (DEBUGPNG) print '[_background_image '.$tmp_file.']'; |
---|
451 | |
---|
452 | imagepng($bg, $tmp_file); |
---|
453 | $this->_canvas->image($tmp_file, "png", $x, $y, $width, $height); |
---|
454 | |
---|
455 | //debugpng |
---|
456 | if (DEBUGPNG) print '[_background_image unlink '.$tmp_file.']'; |
---|
457 | |
---|
458 | if (!DEBUGKEEPTEMP) |
---|
459 | unlink($tmp_file); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
464 | return; |
---|
465 | } |
---|
466 | |
---|
467 | // Border rendering functions |
---|
468 | protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
469 | list($top, $right, $bottom, $left) = $widths; |
---|
470 | |
---|
471 | if ( $$side < 2 ) |
---|
472 | $dash = array($$side, 2); |
---|
473 | else |
---|
474 | $dash = array($$side); |
---|
475 | |
---|
476 | |
---|
477 | switch ($side) { |
---|
478 | |
---|
479 | case "top": |
---|
480 | $delta = $top / 2; |
---|
481 | case "bottom": |
---|
482 | $delta = isset($delta) ? $delta : -$bottom / 2; |
---|
483 | $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $dash); |
---|
484 | break; |
---|
485 | |
---|
486 | case "left": |
---|
487 | $delta = $left / 2; |
---|
488 | case "right": |
---|
489 | $delta = isset($delta) ? $delta : - $right / 2; |
---|
490 | $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $dash); |
---|
491 | break; |
---|
492 | |
---|
493 | default: |
---|
494 | return; |
---|
495 | |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | |
---|
500 | protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
501 | list($top, $right, $bottom, $left) = $widths; |
---|
502 | |
---|
503 | switch ($side) { |
---|
504 | |
---|
505 | case "top": |
---|
506 | $delta = $top / 2; |
---|
507 | case "bottom": |
---|
508 | $delta = isset($delta) ? $delta : -$bottom / 2; |
---|
509 | $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, array(3 * $$side)); |
---|
510 | break; |
---|
511 | |
---|
512 | case "left": |
---|
513 | $delta = $left / 2; |
---|
514 | case "right": |
---|
515 | $delta = isset($delta) ? $delta : - $right / 2; |
---|
516 | $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, array(3 * $$side)); |
---|
517 | break; |
---|
518 | |
---|
519 | default: |
---|
520 | return; |
---|
521 | } |
---|
522 | |
---|
523 | } |
---|
524 | |
---|
525 | |
---|
526 | protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
527 | list($top, $right, $bottom, $left) = $widths; |
---|
528 | |
---|
529 | // All this polygon business is for beveled corners... |
---|
530 | switch ($side) { |
---|
531 | |
---|
532 | case "top": |
---|
533 | if ( $corner_style == "bevel" ) { |
---|
534 | |
---|
535 | $points = array($x, $y, |
---|
536 | $x + $length, $y, |
---|
537 | $x + $length - $right, $y + $top, |
---|
538 | $x + $left, $y + $top); |
---|
539 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
540 | } else |
---|
541 | $this->_canvas->filled_rectangle($x, $y, $length, $top, $color); |
---|
542 | |
---|
543 | break; |
---|
544 | |
---|
545 | case "bottom": |
---|
546 | if ( $corner_style == "bevel" ) { |
---|
547 | $points = array($x, $y, |
---|
548 | $x + $length, $y, |
---|
549 | $x + $length - $right, $y - $bottom, |
---|
550 | $x + $left, $y - $bottom); |
---|
551 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
552 | } else |
---|
553 | $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $bottom, $color); |
---|
554 | |
---|
555 | break; |
---|
556 | |
---|
557 | case "left": |
---|
558 | if ( $corner_style == "bevel" ) { |
---|
559 | $points = array($x, $y, |
---|
560 | $x, $y + $length, |
---|
561 | $x + $left, $y + $length - $bottom, |
---|
562 | $x + $left, $y + $top); |
---|
563 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
564 | } else |
---|
565 | $this->_canvas->filled_rectangle($x, $y, $left, $length, $color); |
---|
566 | |
---|
567 | break; |
---|
568 | |
---|
569 | case "right": |
---|
570 | if ( $corner_style == "bevel" ) { |
---|
571 | $points = array($x, $y, |
---|
572 | $x, $y + $length, |
---|
573 | $x - $right, $y + $length - $bottom, |
---|
574 | $x - $right, $y + $top); |
---|
575 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
576 | } else |
---|
577 | $this->_canvas->filled_rectangle($x - $right, $y, $right, $length, $color); |
---|
578 | |
---|
579 | break; |
---|
580 | |
---|
581 | default: |
---|
582 | return; |
---|
583 | |
---|
584 | } |
---|
585 | |
---|
586 | } |
---|
587 | |
---|
588 | |
---|
589 | protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
590 | list($top, $right, $bottom, $left) = $widths; |
---|
591 | |
---|
592 | $line_width = $$side / 4; |
---|
593 | |
---|
594 | // We draw the outermost edge first. Points are ordered: outer left, |
---|
595 | // outer right, inner right, inner left, or outer top, outer bottom, |
---|
596 | // inner bottom, inner top. |
---|
597 | switch ($side) { |
---|
598 | |
---|
599 | case "top": |
---|
600 | if ( $corner_style == "bevel" ) { |
---|
601 | $left_line_width = $left / 4; |
---|
602 | $right_line_width = $right / 4; |
---|
603 | |
---|
604 | $points = array($x, $y, |
---|
605 | $x + $length, $y, |
---|
606 | $x + $length - $right_line_width, $y + $line_width, |
---|
607 | $x + $left_line_width, $y + $line_width,); |
---|
608 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
609 | |
---|
610 | $points = array($x + $left - $left_line_width, $y + $top - $line_width, |
---|
611 | $x + $length - $right + $right_line_width, $y + $top - $line_width, |
---|
612 | $x + $length - $right, $y + $top, |
---|
613 | $x + $left, $y + $top); |
---|
614 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
615 | |
---|
616 | } else { |
---|
617 | $this->_canvas->filled_rectangle($x, $y, $length, $line_width, $color); |
---|
618 | $this->_canvas->filled_rectangle($x, $y + $top - $line_width, $length, $line_width, $color); |
---|
619 | |
---|
620 | } |
---|
621 | break; |
---|
622 | |
---|
623 | case "bottom": |
---|
624 | if ( $corner_style == "bevel" ) { |
---|
625 | $left_line_width = $left / 4; |
---|
626 | $right_line_width = $right / 4; |
---|
627 | |
---|
628 | $points = array($x, $y, |
---|
629 | $x + $length, $y, |
---|
630 | $x + $length - $right_line_width, $y - $line_width, |
---|
631 | $x + $left_line_width, $y - $line_width); |
---|
632 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
633 | |
---|
634 | $points = array($x + $left - $left_line_width, $y - $bottom + $line_width, |
---|
635 | $x + $length - $right + $right_line_width, $y - $bottom + $line_width, |
---|
636 | $x + $length - $right, $y - $bottom, |
---|
637 | $x + $left, $y - $bottom); |
---|
638 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
639 | |
---|
640 | } else { |
---|
641 | $this->_canvas->filled_rectangle($x, $y - $line_width, $length, $line_width, $color); |
---|
642 | $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $line_width, $color); |
---|
643 | } |
---|
644 | |
---|
645 | break; |
---|
646 | |
---|
647 | case "left": |
---|
648 | if ( $corner_style == "bevel" ) { |
---|
649 | $top_line_width = $top / 4; |
---|
650 | $bottom_line_width = $bottom / 4; |
---|
651 | |
---|
652 | $points = array($x, $y, |
---|
653 | $x, $y + $length, |
---|
654 | $x + $line_width, $y + $length - $bottom_line_width, |
---|
655 | $x + $line_width, $y + $top_line_width); |
---|
656 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
657 | |
---|
658 | $points = array($x + $left - $line_width, $y + $top - $top_line_width, |
---|
659 | $x + $left - $line_width, $y + $length - $bottom + $bottom_line_width, |
---|
660 | $x + $left, $y + $length - $bottom, |
---|
661 | $x + $left, $y + $top); |
---|
662 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
663 | |
---|
664 | } else { |
---|
665 | $this->_canvas->filled_rectangle($x, $y, $line_width, $length, $color); |
---|
666 | $this->_canvas->filled_rectangle($x + $left - $line_width, $y, $line_width, $length, $color); |
---|
667 | } |
---|
668 | |
---|
669 | break; |
---|
670 | |
---|
671 | case "right": |
---|
672 | if ( $corner_style == "bevel" ) { |
---|
673 | $top_line_width = $top / 4; |
---|
674 | $bottom_line_width = $bottom / 4; |
---|
675 | |
---|
676 | |
---|
677 | $points = array($x, $y, |
---|
678 | $x, $y + $length, |
---|
679 | $x - $line_width, $y + $length - $bottom_line_width, |
---|
680 | $x - $line_width, $y + $top_line_width); |
---|
681 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
682 | |
---|
683 | $points = array($x - $right + $line_width, $y + $top - $top_line_width, |
---|
684 | $x - $right + $line_width, $y + $length - $bottom + $bottom_line_width, |
---|
685 | $x - $right, $y + $length - $bottom, |
---|
686 | $x - $right, $y + $top); |
---|
687 | $this->_canvas->polygon($points, $color, null, null, true); |
---|
688 | |
---|
689 | } else { |
---|
690 | $this->_canvas->filled_rectangle($x - $line_width, $y, $line_width, $length, $color); |
---|
691 | $this->_canvas->filled_rectangle($x - $right, $y, $line_width, $length, $color); |
---|
692 | } |
---|
693 | |
---|
694 | break; |
---|
695 | |
---|
696 | default: |
---|
697 | return; |
---|
698 | |
---|
699 | } |
---|
700 | |
---|
701 | } |
---|
702 | |
---|
703 | protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
704 | list($top, $right, $bottom, $left) = $widths; |
---|
705 | |
---|
706 | $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2); |
---|
707 | |
---|
708 | $this->_border_inset($x, $y, $length, $color, $half_widths, $side); |
---|
709 | |
---|
710 | switch ($side) { |
---|
711 | |
---|
712 | case "top": |
---|
713 | $x += $left / 2; |
---|
714 | $y += $top / 2; |
---|
715 | $length -= $left / 2 + $right / 2; |
---|
716 | break; |
---|
717 | |
---|
718 | case "bottom": |
---|
719 | $x += $left / 2; |
---|
720 | $y -= $bottom / 2; |
---|
721 | $length -= $left / 2 + $right / 2; |
---|
722 | break; |
---|
723 | |
---|
724 | case "left": |
---|
725 | $x += $left / 2; |
---|
726 | $y += $top / 2; |
---|
727 | $length -= $top / 2 + $bottom / 2; |
---|
728 | break; |
---|
729 | |
---|
730 | case "right": |
---|
731 | $x -= $right / 2; |
---|
732 | $y += $top / 2; |
---|
733 | $length -= $top / 2 + $bottom / 2; |
---|
734 | break; |
---|
735 | |
---|
736 | default: |
---|
737 | return; |
---|
738 | |
---|
739 | } |
---|
740 | |
---|
741 | $this->_border_outset($x, $y, $length, $color, $half_widths, $side); |
---|
742 | |
---|
743 | } |
---|
744 | |
---|
745 | protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
746 | list($top, $right, $bottom, $left) = $widths; |
---|
747 | |
---|
748 | $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2); |
---|
749 | |
---|
750 | $this->_border_outset($x, $y, $length, $color, $half_widths, $side); |
---|
751 | |
---|
752 | switch ($side) { |
---|
753 | |
---|
754 | case "top": |
---|
755 | $x += $left / 2; |
---|
756 | $y += $top / 2; |
---|
757 | $length -= $left / 2 + $right / 2; |
---|
758 | break; |
---|
759 | |
---|
760 | case "bottom": |
---|
761 | $x += $left / 2; |
---|
762 | $y -= $bottom / 2; |
---|
763 | $length -= $left / 2 + $right / 2; |
---|
764 | break; |
---|
765 | |
---|
766 | case "left": |
---|
767 | $x += $left / 2; |
---|
768 | $y += $top / 2; |
---|
769 | $length -= $top / 2 + $bottom / 2; |
---|
770 | break; |
---|
771 | |
---|
772 | case "right": |
---|
773 | $x -= $right / 2; |
---|
774 | $y += $top / 2; |
---|
775 | $length -= $top / 2 + $bottom / 2; |
---|
776 | break; |
---|
777 | |
---|
778 | default: |
---|
779 | return; |
---|
780 | |
---|
781 | } |
---|
782 | |
---|
783 | $this->_border_inset($x, $y, $length, $color, $half_widths, $side); |
---|
784 | |
---|
785 | } |
---|
786 | |
---|
787 | protected function _tint($c) { |
---|
788 | if ( !is_numeric($c) ) |
---|
789 | return $c; |
---|
790 | |
---|
791 | return min(1, $c + 0.66); |
---|
792 | } |
---|
793 | |
---|
794 | protected function _shade($c) { |
---|
795 | if ( !is_numeric($c) ) |
---|
796 | return $c; |
---|
797 | |
---|
798 | return max(0, $c - 0.66); |
---|
799 | } |
---|
800 | |
---|
801 | protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
802 | list($top, $right, $bottom, $left) = $widths; |
---|
803 | |
---|
804 | switch ($side) { |
---|
805 | |
---|
806 | case "top": |
---|
807 | case "left": |
---|
808 | $shade = array_map(array($this, "_shade"), $color); |
---|
809 | $this->_border_solid($x, $y, $length, $shade, $widths, $side); |
---|
810 | break; |
---|
811 | |
---|
812 | case "bottom": |
---|
813 | case "right": |
---|
814 | $tint = array_map(array($this, "_tint"), $color); |
---|
815 | $this->_border_solid($x, $y, $length, $tint, $widths, $side); |
---|
816 | break; |
---|
817 | |
---|
818 | default: |
---|
819 | return; |
---|
820 | } |
---|
821 | } |
---|
822 | |
---|
823 | protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") { |
---|
824 | list($top, $right, $bottom, $left) = $widths; |
---|
825 | |
---|
826 | switch ($side) { |
---|
827 | case "top": |
---|
828 | case "left": |
---|
829 | $tint = array_map(array($this, "_tint"), $color); |
---|
830 | $this->_border_solid($x, $y, $length, $tint, $widths, $side); |
---|
831 | break; |
---|
832 | |
---|
833 | case "bottom": |
---|
834 | case "right": |
---|
835 | $shade = array_map(array($this, "_shade"), $color); |
---|
836 | $this->_border_solid($x, $y, $length, $shade, $widths, $side); |
---|
837 | break; |
---|
838 | |
---|
839 | default: |
---|
840 | return; |
---|
841 | |
---|
842 | } |
---|
843 | } |
---|
844 | |
---|
845 | //........................................................................ |
---|
846 | |
---|
847 | |
---|
848 | } |
---|
849 | |
---|
850 | ?> |
---|