PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/dompdf/include/abstract_renderer.cls.php

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