暫無描述

abstract_renderer.cls.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @author Helmut Tischer <htischer@weihenstephan.org>
  7. * @author Fabien Ménager <fabien.menager@gmail.com>
  8. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  9. */
  10. /**
  11. * Base renderer class
  12. *
  13. * @access private
  14. * @package dompdf
  15. */
  16. abstract class Abstract_Renderer {
  17. /**
  18. * Rendering backend
  19. *
  20. * @var Canvas
  21. */
  22. protected $_canvas;
  23. /**
  24. * Current dompdf instance
  25. *
  26. * @var DOMPDF
  27. */
  28. protected $_dompdf;
  29. /**
  30. * Class constructor
  31. *
  32. * @param DOMPDF $dompdf The current dompdf instance
  33. */
  34. function __construct(DOMPDF $dompdf) {
  35. $this->_dompdf = $dompdf;
  36. $this->_canvas = $dompdf->get_canvas();
  37. }
  38. /**
  39. * Render a frame.
  40. *
  41. * Specialized in child classes
  42. *
  43. * @param Frame $frame The frame to render
  44. */
  45. abstract function render(Frame $frame);
  46. //........................................................................
  47. /**
  48. * Render a background image over a rectangular area
  49. *
  50. * @param string $url The background image to load
  51. * @param float $x The left edge of the rectangular area
  52. * @param float $y The top edge of the rectangular area
  53. * @param float $width The width of the rectangular area
  54. * @param float $height The height of the rectangular area
  55. * @param Style $style The associated Style object
  56. *
  57. * @throws Exception
  58. */
  59. protected function _background_image($url, $x, $y, $width, $height, $style) {
  60. if ( !function_exists("imagecreatetruecolor") ) {
  61. throw new Exception("The PHP GD extension is required, but is not installed.");
  62. }
  63. $sheet = $style->get_stylesheet();
  64. // Skip degenerate cases
  65. if ( $width == 0 || $height == 0 ) {
  66. return;
  67. }
  68. $box_width = $width;
  69. $box_height = $height;
  70. //debugpng
  71. if (DEBUGPNG) print '[_background_image '.$url.']';
  72. list($img, $type, /*$msg*/) = Image_Cache::resolve_url(
  73. $url,
  74. $sheet->get_protocol(),
  75. $sheet->get_host(),
  76. $sheet->get_base_path(),
  77. $this->_dompdf
  78. );
  79. // Bail if the image is no good
  80. if ( Image_Cache::is_broken($img) ) {
  81. return;
  82. }
  83. //Try to optimize away reading and composing of same background multiple times
  84. //Postponing read with imagecreatefrom ...()
  85. //final composition parameters and name not known yet
  86. //Therefore read dimension directly from file, instead of creating gd object first.
  87. //$img_w = imagesx($src); $img_h = imagesy($src);
  88. list($img_w, $img_h) = dompdf_getimagesize($img, $this->_dompdf->get_http_context());
  89. if (!isset($img_w) || $img_w == 0 || !isset($img_h) || $img_h == 0) {
  90. return;
  91. }
  92. $repeat = $style->background_repeat;
  93. $dpi = $this->_dompdf->get_option("dpi");
  94. //Increase background resolution and dependent box size according to image resolution to be placed in
  95. //Then image can be copied in without resize
  96. $bg_width = round((float)($width * $dpi) / 72);
  97. $bg_height = round((float)($height * $dpi) / 72);
  98. //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel
  99. list($bg_x, $bg_y) = $style->background_position;
  100. if ( is_percent($bg_x) ) {
  101. // The point $bg_x % from the left edge of the image is placed
  102. // $bg_x % from the left edge of the background rectangle
  103. $p = ((float)$bg_x)/100.0;
  104. $x1 = $p * $img_w;
  105. $x2 = $p * $bg_width;
  106. $bg_x = $x2 - $x1;
  107. }
  108. else {
  109. $bg_x = (float)($style->length_in_pt($bg_x)*$dpi) / 72;
  110. }
  111. $bg_x = round($bg_x + $style->length_in_pt($style->border_left_width)*$dpi / 72);
  112. if ( is_percent($bg_y) ) {
  113. // The point $bg_y % from the left edge of the image is placed
  114. // $bg_y % from the left edge of the background rectangle
  115. $p = ((float)$bg_y)/100.0;
  116. $y1 = $p * $img_h;
  117. $y2 = $p * $bg_height;
  118. $bg_y = $y2 - $y1;
  119. }
  120. else {
  121. $bg_y = (float)($style->length_in_pt($bg_y)*$dpi) / 72;
  122. }
  123. $bg_y = round($bg_y + $style->length_in_pt($style->border_top_width)*$dpi / 72);
  124. //clip background to the image area on partial repeat. Nothing to do if img off area
  125. //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area
  126. //On no repeat with positive offset: move size/start to have offset==0
  127. //Handle x/y Dimensions separately
  128. if ( $repeat !== "repeat" && $repeat !== "repeat-x" ) {
  129. //No repeat x
  130. if ($bg_x < 0) {
  131. $bg_width = $img_w + $bg_x;
  132. }
  133. else {
  134. $x += ($bg_x * 72)/$dpi;
  135. $bg_width = $bg_width - $bg_x;
  136. if ($bg_width > $img_w) {
  137. $bg_width = $img_w;
  138. }
  139. $bg_x = 0;
  140. }
  141. if ($bg_width <= 0) {
  142. return;
  143. }
  144. $width = (float)($bg_width * 72)/$dpi;
  145. }
  146. else {
  147. //repeat x
  148. if ($bg_x < 0) {
  149. $bg_x = - ((-$bg_x) % $img_w);
  150. }
  151. else {
  152. $bg_x = $bg_x % $img_w;
  153. if ($bg_x > 0) {
  154. $bg_x -= $img_w;
  155. }
  156. }
  157. }
  158. if ( $repeat !== "repeat" && $repeat !== "repeat-y" ) {
  159. //no repeat y
  160. if ($bg_y < 0) {
  161. $bg_height = $img_h + $bg_y;
  162. }
  163. else {
  164. $y += ($bg_y * 72)/$dpi;
  165. $bg_height = $bg_height - $bg_y;
  166. if ($bg_height > $img_h) {
  167. $bg_height = $img_h;
  168. }
  169. $bg_y = 0;
  170. }
  171. if ($bg_height <= 0) {
  172. return;
  173. }
  174. $height = (float)($bg_height * 72)/$dpi;
  175. }
  176. else {
  177. //repeat y
  178. if ($bg_y < 0) {
  179. $bg_y = - ((-$bg_y) % $img_h);
  180. }
  181. else {
  182. $bg_y = $bg_y % $img_h;
  183. if ($bg_y > 0) {
  184. $bg_y -= $img_h;
  185. }
  186. }
  187. }
  188. //Optimization, if repeat has no effect
  189. if ( $repeat === "repeat" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height ) {
  190. $repeat = "repeat-x";
  191. }
  192. if ( $repeat === "repeat" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width ) {
  193. $repeat = "repeat-y";
  194. }
  195. if ( ($repeat === "repeat-x" && $bg_x <= 0 && $img_w+$bg_x >= $bg_width) ||
  196. ($repeat === "repeat-y" && $bg_y <= 0 && $img_h+$bg_y >= $bg_height) ) {
  197. $repeat = "no-repeat";
  198. }
  199. //Use filename as indicator only
  200. //different names for different variants to have different copies in the pdf
  201. //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color)
  202. //Note: Here, bg_* are the start values, not end values after going through the tile loops!
  203. $filedummy = $img;
  204. $is_png = false;
  205. $filedummy .= '_'.$bg_width.'_'.$bg_height.'_'.$bg_x.'_'.$bg_y.'_'.$repeat;
  206. //Optimization to avoid multiple times rendering the same image.
  207. //If check functions are existing and identical image already cached,
  208. //then skip creation of duplicate, because it is not needed by addImagePng
  209. if ( $this->_canvas instanceof CPDF_Adapter &&
  210. $this->_canvas->get_cpdf()->image_iscached($filedummy) ) {
  211. $bg = null;
  212. }
  213. else {
  214. // Create a new image to fit over the background rectangle
  215. $bg = imagecreatetruecolor($bg_width, $bg_height);
  216. switch (strtolower($type)) {
  217. case IMAGETYPE_PNG:
  218. $is_png = true;
  219. imagesavealpha($bg, true);
  220. imagealphablending($bg, false);
  221. $src = imagecreatefrompng($img);
  222. break;
  223. case IMAGETYPE_JPEG:
  224. $src = imagecreatefromjpeg($img);
  225. break;
  226. case IMAGETYPE_GIF:
  227. $src = imagecreatefromgif($img);
  228. break;
  229. case IMAGETYPE_BMP:
  230. $src = imagecreatefrombmp($img);
  231. break;
  232. default:
  233. return; // Unsupported image type
  234. }
  235. if ( $src == null ) {
  236. return;
  237. }
  238. //Background color if box is not relevant here
  239. //Non transparent image: box clipped to real size. Background non relevant.
  240. //Transparent image: The image controls the transparency and lets shine through whatever background.
  241. //However on transparent image preset the composed image with the transparency color,
  242. //to keep the transparency when copying over the non transparent parts of the tiles.
  243. $ti = imagecolortransparent($src);
  244. if ( $ti >= 0 ) {
  245. $tc = imagecolorsforindex($src, $ti);
  246. $ti = imagecolorallocate($bg, $tc['red'], $tc['green'], $tc['blue']);
  247. imagefill($bg, 0, 0, $ti);
  248. imagecolortransparent($bg, $ti);
  249. }
  250. //This has only an effect for the non repeatable dimension.
  251. //compute start of src and dest coordinates of the single copy
  252. if ( $bg_x < 0 ) {
  253. $dst_x = 0;
  254. $src_x = -$bg_x;
  255. }
  256. else {
  257. $src_x = 0;
  258. $dst_x = $bg_x;
  259. }
  260. if ( $bg_y < 0 ) {
  261. $dst_y = 0;
  262. $src_y = -$bg_y;
  263. }
  264. else {
  265. $src_y = 0;
  266. $dst_y = $bg_y;
  267. }
  268. //For historical reasons exchange meanings of variables:
  269. //start_* will be the start values, while bg_* will be the temporary start values in the loops
  270. $start_x = $bg_x;
  271. $start_y = $bg_y;
  272. // Copy regions from the source image to the background
  273. if ( $repeat === "no-repeat" ) {
  274. // Simply place the image on the background
  275. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
  276. }
  277. else if ( $repeat === "repeat-x" ) {
  278. for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
  279. if ( $bg_x < 0 ) {
  280. $dst_x = 0;
  281. $src_x = -$bg_x;
  282. $w = $img_w + $bg_x;
  283. }
  284. else {
  285. $dst_x = $bg_x;
  286. $src_x = 0;
  287. $w = $img_w;
  288. }
  289. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
  290. }
  291. }
  292. else if ( $repeat === "repeat-y" ) {
  293. for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
  294. if ( $bg_y < 0 ) {
  295. $dst_y = 0;
  296. $src_y = -$bg_y;
  297. $h = $img_h + $bg_y;
  298. }
  299. else {
  300. $dst_y = $bg_y;
  301. $src_y = 0;
  302. $h = $img_h;
  303. }
  304. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
  305. }
  306. }
  307. else if ( $repeat === "repeat" ) {
  308. for ( $bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h ) {
  309. for ( $bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w ) {
  310. if ( $bg_x < 0 ) {
  311. $dst_x = 0;
  312. $src_x = -$bg_x;
  313. $w = $img_w + $bg_x;
  314. }
  315. else {
  316. $dst_x = $bg_x;
  317. $src_x = 0;
  318. $w = $img_w;
  319. }
  320. if ( $bg_y < 0 ) {
  321. $dst_y = 0;
  322. $src_y = -$bg_y;
  323. $h = $img_h + $bg_y;
  324. }
  325. else {
  326. $dst_y = $bg_y;
  327. $src_y = 0;
  328. $h = $img_h;
  329. }
  330. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
  331. }
  332. }
  333. }
  334. else {
  335. print 'Unknown repeat!';
  336. }
  337. imagedestroy($src);
  338. } /* End optimize away creation of duplicates */
  339. $this->_canvas->clipping_rectangle($x, $y, $box_width, $box_height);
  340. //img: image url string
  341. //img_w, img_h: original image size in px
  342. //width, height: box size in pt
  343. //bg_width, bg_height: box size in px
  344. //x, y: left/top edge of box on page in pt
  345. //start_x, start_y: placement of image relative to pattern
  346. //$repeat: repeat mode
  347. //$bg: GD object of result image
  348. //$src: GD object of original image
  349. //When using cpdf and optimization to direct png creation from gd object is available,
  350. //don't create temp file, but place gd object directly into the pdf
  351. if ( !$is_png && $this->_canvas instanceof CPDF_Adapter ) {
  352. // Note: CPDF_Adapter image converts y position
  353. $this->_canvas->get_cpdf()->addImagePng($filedummy, $x, $this->_canvas->get_height() - $y - $height, $width, $height, $bg);
  354. }
  355. else {
  356. $tmp_dir = $this->_dompdf->get_option("temp_dir");
  357. $tmp_name = tempnam($tmp_dir, "bg_dompdf_img_");
  358. @unlink($tmp_name);
  359. $tmp_file = "$tmp_name.png";
  360. //debugpng
  361. if (DEBUGPNG) print '[_background_image '.$tmp_file.']';
  362. imagepng($bg, $tmp_file);
  363. $this->_canvas->image($tmp_file, $x, $y, $width, $height);
  364. imagedestroy($bg);
  365. //debugpng
  366. if (DEBUGPNG) print '[_background_image unlink '.$tmp_file.']';
  367. if (!DEBUGKEEPTEMP) {
  368. unlink($tmp_file);
  369. }
  370. }
  371. $this->_canvas->clipping_end();
  372. }
  373. protected function _get_dash_pattern($style, $width) {
  374. $pattern = array();
  375. switch ($style) {
  376. default:
  377. /*case "solid":
  378. case "double":
  379. case "groove":
  380. case "inset":
  381. case "outset":
  382. case "ridge":*/
  383. case "none": break;
  384. case "dotted":
  385. if ( $width <= 1 )
  386. $pattern = array($width, $width*2);
  387. else
  388. $pattern = array($width);
  389. break;
  390. case "dashed":
  391. $pattern = array(3 * $width);
  392. break;
  393. }
  394. return $pattern;
  395. }
  396. protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  397. return;
  398. }
  399. protected function _border_hidden($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  400. return;
  401. }
  402. // Border rendering functions
  403. protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  404. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dotted", $r1, $r2);
  405. }
  406. protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  407. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dashed", $r1, $r2);
  408. }
  409. protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  410. // TODO: Solve rendering where one corner is beveled (radius == 0), one corner isn't.
  411. if ( $corner_style !== "bevel" || $r1 > 0 || $r2 > 0 ) {
  412. // do it the simple way
  413. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "solid", $r1, $r2);
  414. return;
  415. }
  416. list($top, $right, $bottom, $left) = $widths;
  417. // All this polygon business is for beveled corners...
  418. switch ($side) {
  419. case "top":
  420. $points = array($x, $y,
  421. $x + $length, $y,
  422. $x + $length - $right, $y + $top,
  423. $x + $left, $y + $top);
  424. $this->_canvas->polygon($points, $color, null, null, true);
  425. break;
  426. case "bottom":
  427. $points = array($x, $y,
  428. $x + $length, $y,
  429. $x + $length - $right, $y - $bottom,
  430. $x + $left, $y - $bottom);
  431. $this->_canvas->polygon($points, $color, null, null, true);
  432. break;
  433. case "left":
  434. $points = array($x, $y,
  435. $x, $y + $length,
  436. $x + $left, $y + $length - $bottom,
  437. $x + $left, $y + $top);
  438. $this->_canvas->polygon($points, $color, null, null, true);
  439. break;
  440. case "right":
  441. $points = array($x, $y,
  442. $x, $y + $length,
  443. $x - $right, $y + $length - $bottom,
  444. $x - $right, $y + $top);
  445. $this->_canvas->polygon($points, $color, null, null, true);
  446. break;
  447. default:
  448. return;
  449. }
  450. }
  451. protected function _apply_ratio($side, $ratio, $top, $right, $bottom, $left, &$x, &$y, &$length, &$r1, &$r2) {
  452. switch ($side) {
  453. case "top":
  454. $r1 -= $left * $ratio;
  455. $r2 -= $right * $ratio;
  456. $x += $left * $ratio;
  457. $y += $top * $ratio;
  458. $length -= $left * $ratio + $right * $ratio;
  459. break;
  460. case "bottom":
  461. $r1 -= $right * $ratio;
  462. $r2 -= $left * $ratio;
  463. $x += $left * $ratio;
  464. $y -= $bottom * $ratio;
  465. $length -= $left * $ratio + $right * $ratio;
  466. break;
  467. case "left":
  468. $r1 -= $top * $ratio;
  469. $r2 -= $bottom * $ratio;
  470. $x += $left * $ratio;
  471. $y += $top * $ratio;
  472. $length -= $top * $ratio + $bottom * $ratio;
  473. break;
  474. case "right":
  475. $r1 -= $bottom * $ratio;
  476. $r2 -= $top * $ratio;
  477. $x -= $right * $ratio;
  478. $y += $top * $ratio;
  479. $length -= $top * $ratio + $bottom * $ratio;
  480. break;
  481. default:
  482. return;
  483. }
  484. }
  485. protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  486. list($top, $right, $bottom, $left) = $widths;
  487. $third_widths = array($top / 3, $right / 3, $bottom / 3, $left / 3);
  488. // draw the outer border
  489. $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
  490. $this->_apply_ratio($side, 2/3, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  491. $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
  492. }
  493. protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  494. list($top, $right, $bottom, $left) = $widths;
  495. $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
  496. $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  497. $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  498. $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  499. }
  500. protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  501. list($top, $right, $bottom, $left) = $widths;
  502. $half_widths = array($top / 2, $right / 2, $bottom / 2, $left / 2);
  503. $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  504. $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  505. $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  506. }
  507. protected function _tint($c) {
  508. if ( !is_numeric($c) )
  509. return $c;
  510. return min(1, $c + 0.16);
  511. }
  512. protected function _shade($c) {
  513. if ( !is_numeric($c) )
  514. return $c;
  515. return max(0, $c - 0.33);
  516. }
  517. protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  518. switch ($side) {
  519. case "top":
  520. case "left":
  521. $shade = array_map(array($this, "_shade"), $color);
  522. $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
  523. break;
  524. case "bottom":
  525. case "right":
  526. $tint = array_map(array($this, "_tint"), $color);
  527. $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
  528. break;
  529. default:
  530. return;
  531. }
  532. }
  533. protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0) {
  534. switch ($side) {
  535. case "top":
  536. case "left":
  537. $tint = array_map(array($this, "_tint"), $color);
  538. $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
  539. break;
  540. case "bottom":
  541. case "right":
  542. $shade = array_map(array($this, "_shade"), $color);
  543. $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
  544. break;
  545. default:
  546. return;
  547. }
  548. }
  549. // Draws a solid, dotted, or dashed line, observing the border radius
  550. protected function _border_line($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $pattern_name, $r1 = 0, $r2 = 0) {
  551. list($top, $right, $bottom, $left) = $widths;
  552. $width = $$side;
  553. $pattern = $this->_get_dash_pattern($pattern_name, $width);
  554. $half_width = $width/2;
  555. $r1 -= $half_width;
  556. $r2 -= $half_width;
  557. $adjust = $r1/80;
  558. $length -= $width;
  559. switch ($side) {
  560. case "top":
  561. $x += $half_width;
  562. $y += $half_width;
  563. if ( $r1 > 0 ) {
  564. $this->_canvas->arc($x + $r1, $y + $r1, $r1, $r1, 90-$adjust, 135+$adjust, $color, $width, $pattern);
  565. }
  566. $this->_canvas->line($x + $r1, $y, $x + $length - $r2, $y, $color, $width, $pattern);
  567. if ( $r2 > 0 ) {
  568. $this->_canvas->arc($x + $length - $r2, $y + $r2, $r2, $r2, 45-$adjust, 90+$adjust, $color, $width, $pattern);
  569. }
  570. break;
  571. case "bottom":
  572. $x += $half_width;
  573. $y -= $half_width;
  574. if ( $r1 > 0 ) {
  575. $this->_canvas->arc($x + $r1, $y - $r1, $r1, $r1, 225-$adjust, 270+$adjust, $color, $width, $pattern);
  576. }
  577. $this->_canvas->line($x + $r1, $y, $x + $length - $r2, $y, $color, $width, $pattern);
  578. if ( $r2 > 0 ) {
  579. $this->_canvas->arc($x + $length - $r2, $y - $r2, $r2, $r2, 270-$adjust, 315+$adjust, $color, $width, $pattern);
  580. }
  581. break;
  582. case "left":
  583. $y += $half_width;
  584. $x += $half_width;
  585. if ( $r1 > 0 ) {
  586. $this->_canvas->arc($x + $r1, $y + $r1, $r1, $r1, 135-$adjust, 180+$adjust, $color, $width, $pattern);
  587. }
  588. $this->_canvas->line($x, $y + $r1, $x, $y + $length - $r2, $color, $width, $pattern);
  589. if ( $r2 > 0 ) {
  590. $this->_canvas->arc($x + $r2, $y + $length - $r2, $r2, $r2, 180-$adjust, 225+$adjust, $color, $width, $pattern);
  591. }
  592. break;
  593. case "right":
  594. $y += $half_width;
  595. $x -= $half_width;
  596. if ( $r1 > 0 ) {
  597. $this->_canvas->arc($x - $r1, $y + $r1, $r1, $r1, 0-$adjust, 45+$adjust, $color, $width, $pattern);
  598. }
  599. $this->_canvas->line($x, $y + $r1, $x, $y + $length - $r2, $color, $width, $pattern);
  600. if ( $r2 > 0 ) {
  601. $this->_canvas->arc($x - $r2, $y + $length - $r2, $r2, $r2, 315-$adjust, 360+$adjust, $color, $width, $pattern);
  602. }
  603. break;
  604. }
  605. }
  606. protected function _set_opacity($opacity) {
  607. if ( is_numeric($opacity) && $opacity <= 1.0 && $opacity >= 0.0 ) {
  608. $this->_canvas->set_opacity( $opacity );
  609. }
  610. }
  611. protected function _debug_layout($box, $color = "red", $style = array()) {
  612. $this->_canvas->rectangle($box[0], $box[1], $box[2], $box[3], CSS_Color::parse($color), 0.1, $style);
  613. }
  614. }