PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dompdf/include/abstract_renderer.cls.php

http://findresources.googlecode.com/
PHP | 890 lines | 534 code | 169 blank | 187 comment | 110 complexity | 67cdec1b98902b5f6c1fb54dc41db428 MD5 | raw file
Possible License(s): LGPL-2.1
  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 361 2011-02-16 21:03:05Z fabien.menager $ */
  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) = dompdf_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. }
  261. else {
  262. // Create a new image to fit over the background rectangle
  263. $bg = imagecreatetruecolor($bg_width, $bg_height);
  264. //anyway default
  265. //imagealphablending($img, true);
  266. switch (strtolower($ext)) {
  267. case "png":
  268. $src = imagecreatefrompng($img);
  269. break;
  270. case "jpg":
  271. case "jpeg":
  272. $src = imagecreatefromjpeg($img);
  273. break;
  274. case "gif":
  275. $src = imagecreatefromgif($img);
  276. break;
  277. case "bmp":
  278. $src = imagecreatefrombmp($img);
  279. break;
  280. default: return; // Unsupported image type
  281. }
  282. if ($src == null) {
  283. return;
  284. }
  285. //Background color if box is not relevant here
  286. //Non transparent image: box clipped to real size. Background non relevant.
  287. //Transparent image: The image controls the transparency and lets shine through whatever background.
  288. //However on transparent imaage preset the composed image with the transparency color,
  289. //to keep the transparency when copying over the non transparent parts of the tiles.
  290. $ti = imagecolortransparent($src);
  291. if ($ti >= 0) {
  292. $tc = imagecolorsforindex($src,$ti);
  293. $ti = imagecolorallocate($bg,$tc['red'],$tc['green'],$tc['blue']);
  294. imagefill($bg,0,0,$ti);
  295. imagecolortransparent($bg, $ti);
  296. }
  297. //This has only an effect for the non repeatable dimension.
  298. //compute start of src and dest coordinates of the single copy
  299. if ( $bg_x < 0 ) {
  300. $dst_x = 0;
  301. $src_x = -$bg_x;
  302. } else {
  303. $src_x = 0;
  304. $dst_x = $bg_x;
  305. }
  306. if ( $bg_y < 0 ) {
  307. $dst_y = 0;
  308. $src_y = -$bg_y;
  309. } else {
  310. $src_y = 0;
  311. $dst_y = $bg_y;
  312. }
  313. //For historical reasons exchange meanings of variables:
  314. //start_* will be the start values, while bg_* will be the temporary start values in the loops
  315. $start_x = $bg_x;
  316. $start_y = $bg_y;
  317. // Copy regions from the source image to the background
  318. if ( $repeat === "no-repeat" ) {
  319. // Simply place the image on the background
  320. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
  321. } else if ( $repeat === "repeat-x" ) {
  322. for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
  323. if ( $bg_x < 0 ) {
  324. $dst_x = 0;
  325. $src_x = -$bg_x;
  326. $w = $img_w + $bg_x;
  327. } else {
  328. $dst_x = $bg_x;
  329. $src_x = 0;
  330. $w = $img_w;
  331. }
  332. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
  333. }
  334. } else if ( $repeat === "repeat-y" ) {
  335. for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
  336. if ( $bg_y < 0 ) {
  337. $dst_y = 0;
  338. $src_y = -$bg_y;
  339. $h = $img_h + $bg_y;
  340. } else {
  341. $dst_y = $bg_y;
  342. $src_y = 0;
  343. $h = $img_h;
  344. }
  345. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
  346. }
  347. } else if ( $repeat === "repeat" ) {
  348. for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
  349. for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
  350. if ( $bg_x < 0 ) {
  351. $dst_x = 0;
  352. $src_x = -$bg_x;
  353. $w = $img_w + $bg_x;
  354. } else {
  355. $dst_x = $bg_x;
  356. $src_x = 0;
  357. $w = $img_w;
  358. }
  359. if ( $bg_y < 0 ) {
  360. $dst_y = 0;
  361. $src_y = -$bg_y;
  362. $h = $img_h + $bg_y;
  363. } else {
  364. $dst_y = $bg_y;
  365. $src_y = 0;
  366. $h = $img_h;
  367. }
  368. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
  369. }
  370. }
  371. }
  372. else {
  373. print 'Unknown repeat!';
  374. }
  375. imagedestroy($src);
  376. } /* End optimize away creation of duplicates */
  377. //img: image url string
  378. //img_w, img_h: original image size in px
  379. //width, height: box size in pt
  380. //bg_width, bg_height: box size in px
  381. //x, y: left/top edge of box on page in pt
  382. //start_x, start_y: placement of image relativ to pattern
  383. //$repeat: repeat mode
  384. //$bg: GD object of result image
  385. //$src: GD object of original image
  386. //When using cpdf and optimization to direct png creation from gd object is available,
  387. //don't create temp file, but place gd object directly into the pdf
  388. if ( method_exists( $this->_canvas, "get_cpdf" ) &&
  389. method_exists( $this->_canvas->get_cpdf(), "addImagePng" ) ) {
  390. // Note: CPDF_Adapter image converts y position
  391. $this->_canvas->get_cpdf()->addImagePng($filedummy, $x, $this->_canvas->get_height() - $y - $height, $width, $height, $bg);
  392. }
  393. else {
  394. $tmp_file = tempnam(DOMPDF_TEMP_DIR, "bg_dompdf_img_").'.png';
  395. //debugpng
  396. if (DEBUGPNG) print '[_background_image '.$tmp_file.']';
  397. imagepng($bg, $tmp_file);
  398. $this->_canvas->image($tmp_file, "png", $x, $y, $width, $height);
  399. imagedestroy($bg);
  400. //debugpng
  401. if (DEBUGPNG) print '[_background_image unlink '.$tmp_file.']';
  402. if (!DEBUGKEEPTEMP)
  403. unlink($tmp_file);
  404. }
  405. }
  406. protected function _get_dash_pattern($style, $width) {
  407. $pattern = array();
  408. switch ($style) {
  409. default:
  410. /*case "solid":
  411. case "double":
  412. case "groove":
  413. case "inset":
  414. case "outset":
  415. case "ridge":*/
  416. case "none": break;
  417. case "dotted":
  418. if ( $width < 2 )
  419. $pattern = array($width, 2);
  420. else
  421. $pattern = array($width);
  422. break;
  423. case "dashed":
  424. $pattern = array(3 * $width);
  425. break;
  426. }
  427. return $pattern;
  428. }
  429. protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  430. return;
  431. }
  432. // Border rendering functions
  433. protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  434. list($top, $right, $bottom, $left) = $widths;
  435. $pattern = $this->_get_dash_pattern("dotted", $$side);
  436. switch ($side) {
  437. case "top":
  438. $delta = $top / 2;
  439. case "bottom":
  440. $delta = isset($delta) ? $delta : -$bottom / 2;
  441. $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $pattern);
  442. break;
  443. case "left":
  444. $delta = $left / 2;
  445. case "right":
  446. $delta = isset($delta) ? $delta : - $right / 2;
  447. $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $pattern);
  448. break;
  449. default:
  450. return;
  451. }
  452. }
  453. protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  454. list($top, $right, $bottom, $left) = $widths;
  455. $pattern = $this->_get_dash_pattern("dashed", $$side);
  456. switch ($side) {
  457. case "top":
  458. $delta = $top / 2;
  459. case "bottom":
  460. $delta = isset($delta) ? $delta : -$bottom / 2;
  461. $this->_canvas->line($x, $y + $delta, $x + $length, $y + $delta, $color, $$side, $pattern);
  462. break;
  463. case "left":
  464. $delta = $left / 2;
  465. case "right":
  466. $delta = isset($delta) ? $delta : - $right / 2;
  467. $this->_canvas->line($x + $delta, $y, $x + $delta, $y + $length, $color, $$side, $pattern);
  468. break;
  469. default:
  470. return;
  471. }
  472. }
  473. protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  474. list($top, $right, $bottom, $left) = $widths;
  475. // All this polygon business is for beveled corners...
  476. switch ($side) {
  477. case "top":
  478. if ( $corner_style === "bevel" ) {
  479. $points = array($x, $y,
  480. $x + $length, $y,
  481. $x + $length - $right, $y + $top,
  482. $x + $left, $y + $top);
  483. $this->_canvas->polygon($points, $color, null, null, true);
  484. } else
  485. $this->_canvas->filled_rectangle($x, $y, $length, $top, $color);
  486. break;
  487. case "bottom":
  488. if ( $corner_style === "bevel" ) {
  489. $points = array($x, $y,
  490. $x + $length, $y,
  491. $x + $length - $right, $y - $bottom,
  492. $x + $left, $y - $bottom);
  493. $this->_canvas->polygon($points, $color, null, null, true);
  494. } else
  495. $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $bottom, $color);
  496. break;
  497. case "left":
  498. if ( $corner_style === "bevel" ) {
  499. $points = array($x, $y,
  500. $x, $y + $length,
  501. $x + $left, $y + $length - $bottom,
  502. $x + $left, $y + $top);
  503. $this->_canvas->polygon($points, $color, null, null, true);
  504. } else
  505. $this->_canvas->filled_rectangle($x, $y, $left, $length, $color);
  506. break;
  507. case "right":
  508. if ( $corner_style === "bevel" ) {
  509. $points = array($x, $y,
  510. $x, $y + $length,
  511. $x - $right, $y + $length - $bottom,
  512. $x - $right, $y + $top);
  513. $this->_canvas->polygon($points, $color, null, null, true);
  514. } else
  515. $this->_canvas->filled_rectangle($x - $right, $y, $right, $length, $color);
  516. break;
  517. default:
  518. return;
  519. }
  520. }
  521. protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  522. list($top, $right, $bottom, $left) = $widths;
  523. $line_width = $$side / 4;
  524. // We draw the outermost edge first. Points are ordered: outer left,
  525. // outer right, inner right, inner left, or outer top, outer bottom,
  526. // inner bottom, inner top.
  527. switch ($side) {
  528. case "top":
  529. if ( $corner_style === "bevel" ) {
  530. $left_line_width = $left / 4;
  531. $right_line_width = $right / 4;
  532. $points = array($x, $y,
  533. $x + $length, $y,
  534. $x + $length - $right_line_width, $y + $line_width,
  535. $x + $left_line_width, $y + $line_width,);
  536. $this->_canvas->polygon($points, $color, null, null, true);
  537. $points = array($x + $left - $left_line_width, $y + $top - $line_width,
  538. $x + $length - $right + $right_line_width, $y + $top - $line_width,
  539. $x + $length - $right, $y + $top,
  540. $x + $left, $y + $top);
  541. $this->_canvas->polygon($points, $color, null, null, true);
  542. } else {
  543. $this->_canvas->filled_rectangle($x, $y, $length, $line_width, $color);
  544. $this->_canvas->filled_rectangle($x, $y + $top - $line_width, $length, $line_width, $color);
  545. }
  546. break;
  547. case "bottom":
  548. if ( $corner_style === "bevel" ) {
  549. $left_line_width = $left / 4;
  550. $right_line_width = $right / 4;
  551. $points = array($x, $y,
  552. $x + $length, $y,
  553. $x + $length - $right_line_width, $y - $line_width,
  554. $x + $left_line_width, $y - $line_width);
  555. $this->_canvas->polygon($points, $color, null, null, true);
  556. $points = array($x + $left - $left_line_width, $y - $bottom + $line_width,
  557. $x + $length - $right + $right_line_width, $y - $bottom + $line_width,
  558. $x + $length - $right, $y - $bottom,
  559. $x + $left, $y - $bottom);
  560. $this->_canvas->polygon($points, $color, null, null, true);
  561. } else {
  562. $this->_canvas->filled_rectangle($x, $y - $line_width, $length, $line_width, $color);
  563. $this->_canvas->filled_rectangle($x, $y - $bottom, $length, $line_width, $color);
  564. }
  565. break;
  566. case "left":
  567. if ( $corner_style === "bevel" ) {
  568. $top_line_width = $top / 4;
  569. $bottom_line_width = $bottom / 4;
  570. $points = array($x, $y,
  571. $x, $y + $length,
  572. $x + $line_width, $y + $length - $bottom_line_width,
  573. $x + $line_width, $y + $top_line_width);
  574. $this->_canvas->polygon($points, $color, null, null, true);
  575. $points = array($x + $left - $line_width, $y + $top - $top_line_width,
  576. $x + $left - $line_width, $y + $length - $bottom + $bottom_line_width,
  577. $x + $left, $y + $length - $bottom,
  578. $x + $left, $y + $top);
  579. $this->_canvas->polygon($points, $color, null, null, true);
  580. } else {
  581. $this->_canvas->filled_rectangle($x, $y, $line_width, $length, $color);
  582. $this->_canvas->filled_rectangle($x + $left - $line_width, $y, $line_width, $length, $color);
  583. }
  584. break;
  585. case "right":
  586. if ( $corner_style === "bevel" ) {
  587. $top_line_width = $top / 4;
  588. $bottom_line_width = $bottom / 4;
  589. $points = array($x, $y,
  590. $x, $y + $length,
  591. $x - $line_width, $y + $length - $bottom_line_width,
  592. $x - $line_width, $y + $top_line_width);
  593. $this->_canvas->polygon($points, $color, null, null, true);
  594. $points = array($x - $right + $line_width, $y + $top - $top_line_width,
  595. $x - $right + $line_width, $y + $length - $bottom + $bottom_line_width,
  596. $x - $right, $y + $length - $bottom,
  597. $x - $right, $y + $top);
  598. $this->_canvas->polygon($points, $color, null, null, true);
  599. } else {
  600. $this->_canvas->filled_rectangle($x - $line_width, $y, $line_width, $length, $color);
  601. $this->_canvas->filled_rectangle($x - $right, $y, $line_width, $length, $color);
  602. }
  603. break;
  604. default:
  605. return;
  606. }
  607. }
  608. protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  609. list($top, $right, $bottom, $left) = $widths;
  610. $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
  611. $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
  612. switch ($side) {
  613. case "top":
  614. $x += $left / 2;
  615. $y += $top / 2;
  616. $length -= $left / 2 + $right / 2;
  617. break;
  618. case "bottom":
  619. $x += $left / 2;
  620. $y -= $bottom / 2;
  621. $length -= $left / 2 + $right / 2;
  622. break;
  623. case "left":
  624. $x += $left / 2;
  625. $y += $top / 2;
  626. $length -= $top / 2 + $bottom / 2;
  627. break;
  628. case "right":
  629. $x -= $right / 2;
  630. $y += $top / 2;
  631. $length -= $top / 2 + $bottom / 2;
  632. break;
  633. default:
  634. return;
  635. }
  636. $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
  637. }
  638. protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  639. list($top, $right, $bottom, $left) = $widths;
  640. $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
  641. $this->_border_outset($x, $y, $length, $color, $half_widths, $side);
  642. switch ($side) {
  643. case "top":
  644. $x += $left / 2;
  645. $y += $top / 2;
  646. $length -= $left / 2 + $right / 2;
  647. break;
  648. case "bottom":
  649. $x += $left / 2;
  650. $y -= $bottom / 2;
  651. $length -= $left / 2 + $right / 2;
  652. break;
  653. case "left":
  654. $x += $left / 2;
  655. $y += $top / 2;
  656. $length -= $top / 2 + $bottom / 2;
  657. break;
  658. case "right":
  659. $x -= $right / 2;
  660. $y += $top / 2;
  661. $length -= $top / 2 + $bottom / 2;
  662. break;
  663. default:
  664. return;
  665. }
  666. $this->_border_inset($x, $y, $length, $color, $half_widths, $side);
  667. }
  668. protected function _tint($c) {
  669. if ( !is_numeric($c) )
  670. return $c;
  671. return min(1, $c + 0.16);
  672. }
  673. protected function _shade($c) {
  674. if ( !is_numeric($c) )
  675. return $c;
  676. return max(0, $c - 0.33);
  677. }
  678. protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  679. list($top, $right, $bottom, $left) = $widths;
  680. switch ($side) {
  681. case "top":
  682. case "left":
  683. $shade = array_map(array($this, "_shade"), $color);
  684. $this->_border_solid($x, $y, $length, $shade, $widths, $side);
  685. break;
  686. case "bottom":
  687. case "right":
  688. $tint = array_map(array($this, "_tint"), $color);
  689. $this->_border_solid($x, $y, $length, $tint, $widths, $side);
  690. break;
  691. default:
  692. return;
  693. }
  694. }
  695. protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel") {
  696. list($top, $right, $bottom, $left) = $widths;
  697. switch ($side) {
  698. case "top":
  699. case "left":
  700. $tint = array_map(array($this, "_tint"), $color);
  701. $this->_border_solid($x, $y, $length, $tint, $widths, $side);
  702. break;
  703. case "bottom":
  704. case "right":
  705. $shade = array_map(array($this, "_shade"), $color);
  706. $this->_border_solid($x, $y, $length, $shade, $widths, $side);
  707. break;
  708. default:
  709. return;
  710. }
  711. }
  712. protected function _set_opacity($opacity) {
  713. if ( is_numeric($opacity) && $opacity <= 1.0 && $opacity >= 0.0 ) {
  714. $this->_canvas->set_opacity( $opacity );
  715. }
  716. }
  717. protected function _debug_layout($box, $color = "red", $style = array()) {
  718. $this->_canvas->rectangle($box[0], $box[1], $box[2], $box[3], CSS_Color::parse($color), 0.1, $style);
  719. }
  720. //........................................................................
  721. }