AbstractRenderer.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. namespace Dompdf\Renderer;
  11. use Dompdf\Adapter\CPDF;
  12. use Dompdf\Css\Color;
  13. use Dompdf\Css\Style;
  14. use Dompdf\Dompdf;
  15. use Dompdf\Helpers;
  16. use Dompdf\Frame;
  17. use Dompdf\Image\Cache;
  18. /**
  19. * Base renderer class
  20. *
  21. * @package dompdf
  22. */
  23. abstract class AbstractRenderer
  24. {
  25. /**
  26. * Rendering backend
  27. *
  28. * @var \Dompdf\Canvas
  29. */
  30. protected $_canvas;
  31. /**
  32. * Current dompdf instance
  33. *
  34. * @var Dompdf
  35. */
  36. protected $_dompdf;
  37. /**
  38. * Class constructor
  39. *
  40. * @param Dompdf $dompdf The current dompdf instance
  41. */
  42. function __construct(Dompdf $dompdf)
  43. {
  44. $this->_dompdf = $dompdf;
  45. $this->_canvas = $dompdf->getCanvas();
  46. }
  47. /**
  48. * Render a frame.
  49. *
  50. * Specialized in child classes
  51. *
  52. * @param Frame $frame The frame to render
  53. */
  54. abstract function render(Frame $frame);
  55. /**
  56. * Render a background image over a rectangular area
  57. *
  58. * @param string $url The background image to load
  59. * @param float $x The left edge of the rectangular area
  60. * @param float $y The top edge of the rectangular area
  61. * @param float $width The width of the rectangular area
  62. * @param float $height The height of the rectangular area
  63. * @param Style $style The associated Style object
  64. *
  65. * @throws \Exception
  66. */
  67. protected function _background_image($url, $x, $y, $width, $height, $style)
  68. {
  69. if (!function_exists("imagecreatetruecolor")) {
  70. throw new \Exception("The PHP GD extension is required, but is not installed.");
  71. }
  72. $sheet = $style->get_stylesheet();
  73. // Skip degenerate cases
  74. if ($width == 0 || $height == 0) {
  75. return;
  76. }
  77. $box_width = $width;
  78. $box_height = $height;
  79. //debugpng
  80. if ($this->_dompdf->getOptions()->getDebugPng()) {
  81. print '[_background_image ' . $url . ']';
  82. }
  83. list($img, $type, /*$msg*/) = Cache::resolve_url(
  84. $url,
  85. $sheet->get_protocol(),
  86. $sheet->get_host(),
  87. $sheet->get_base_path(),
  88. $this->_dompdf
  89. );
  90. // Bail if the image is no good
  91. if (Cache::is_broken($img)) {
  92. return;
  93. }
  94. //Try to optimize away reading and composing of same background multiple times
  95. //Postponing read with imagecreatefrom ...()
  96. //final composition parameters and name not known yet
  97. //Therefore read dimension directly from file, instead of creating gd object first.
  98. //$img_w = imagesx($src); $img_h = imagesy($src);
  99. list($img_w, $img_h) = Helpers::dompdf_getimagesize($img, $this->_dompdf->getHttpContext());
  100. if (!isset($img_w) || $img_w == 0 || !isset($img_h) || $img_h == 0) {
  101. return;
  102. }
  103. // save for later check if file needs to be resized.
  104. $org_img_w = $img_w;
  105. $org_img_h = $img_h;
  106. $repeat = $style->background_repeat;
  107. $dpi = $this->_dompdf->getOptions()->getDpi();
  108. //Increase background resolution and dependent box size according to image resolution to be placed in
  109. //Then image can be copied in without resize
  110. $bg_width = round((float)($width * $dpi) / 72);
  111. $bg_height = round((float)($height * $dpi) / 72);
  112. list($img_w, $img_h) = $this->_resize_background_image(
  113. $img_w,
  114. $img_h,
  115. $bg_width,
  116. $bg_height,
  117. $style->background_size,
  118. $dpi
  119. );
  120. //Need %bg_x, $bg_y as background pos, where img starts, converted to pixel
  121. list($bg_x, $bg_y) = $style->background_position;
  122. if (Helpers::is_percent($bg_x)) {
  123. // The point $bg_x % from the left edge of the image is placed
  124. // $bg_x % from the left edge of the background rectangle
  125. $p = ((float)$bg_x) / 100.0;
  126. $x1 = $p * $img_w;
  127. $x2 = $p * $bg_width;
  128. $bg_x = $x2 - $x1;
  129. } else {
  130. $bg_x = (float)($style->length_in_pt($bg_x) * $dpi) / 72;
  131. }
  132. $bg_x = round($bg_x + (float)$style->length_in_pt($style->border_left_width) * $dpi / 72);
  133. if (Helpers::is_percent($bg_y)) {
  134. // The point $bg_y % from the left edge of the image is placed
  135. // $bg_y % from the left edge of the background rectangle
  136. $p = ((float)$bg_y) / 100.0;
  137. $y1 = $p * $img_h;
  138. $y2 = $p * $bg_height;
  139. $bg_y = $y2 - $y1;
  140. } else {
  141. $bg_y = (float)($style->length_in_pt($bg_y) * $dpi) / 72;
  142. }
  143. $bg_y = round($bg_y + (float)$style->length_in_pt($style->border_top_width) * $dpi / 72);
  144. //clip background to the image area on partial repeat. Nothing to do if img off area
  145. //On repeat, normalize start position to the tile at immediate left/top or 0/0 of area
  146. //On no repeat with positive offset: move size/start to have offset==0
  147. //Handle x/y Dimensions separately
  148. if ($repeat !== "repeat" && $repeat !== "repeat-x") {
  149. //No repeat x
  150. if ($bg_x < 0) {
  151. $bg_width = $img_w + $bg_x;
  152. } else {
  153. $x += ($bg_x * 72) / $dpi;
  154. $bg_width = $bg_width - $bg_x;
  155. if ($bg_width > $img_w) {
  156. $bg_width = $img_w;
  157. }
  158. $bg_x = 0;
  159. }
  160. if ($bg_width <= 0) {
  161. return;
  162. }
  163. $width = (float)($bg_width * 72) / $dpi;
  164. } else {
  165. //repeat x
  166. if ($bg_x < 0) {
  167. $bg_x = -((-$bg_x) % $img_w);
  168. } else {
  169. $bg_x = $bg_x % $img_w;
  170. if ($bg_x > 0) {
  171. $bg_x -= $img_w;
  172. }
  173. }
  174. }
  175. if ($repeat !== "repeat" && $repeat !== "repeat-y") {
  176. //no repeat y
  177. if ($bg_y < 0) {
  178. $bg_height = $img_h + $bg_y;
  179. } else {
  180. $y += ($bg_y * 72) / $dpi;
  181. $bg_height = $bg_height - $bg_y;
  182. if ($bg_height > $img_h) {
  183. $bg_height = $img_h;
  184. }
  185. $bg_y = 0;
  186. }
  187. if ($bg_height <= 0) {
  188. return;
  189. }
  190. $height = (float)($bg_height * 72) / $dpi;
  191. } else {
  192. //repeat y
  193. if ($bg_y < 0) {
  194. $bg_y = -((-$bg_y) % $img_h);
  195. } else {
  196. $bg_y = $bg_y % $img_h;
  197. if ($bg_y > 0) {
  198. $bg_y -= $img_h;
  199. }
  200. }
  201. }
  202. //Optimization, if repeat has no effect
  203. if ($repeat === "repeat" && $bg_y <= 0 && $img_h + $bg_y >= $bg_height) {
  204. $repeat = "repeat-x";
  205. }
  206. if ($repeat === "repeat" && $bg_x <= 0 && $img_w + $bg_x >= $bg_width) {
  207. $repeat = "repeat-y";
  208. }
  209. if (($repeat === "repeat-x" && $bg_x <= 0 && $img_w + $bg_x >= $bg_width) ||
  210. ($repeat === "repeat-y" && $bg_y <= 0 && $img_h + $bg_y >= $bg_height)
  211. ) {
  212. $repeat = "no-repeat";
  213. }
  214. //Use filename as indicator only
  215. //different names for different variants to have different copies in the pdf
  216. //This is not dependent of background color of box! .'_'.(is_array($bg_color) ? $bg_color["hex"] : $bg_color)
  217. //Note: Here, bg_* are the start values, not end values after going through the tile loops!
  218. $filedummy = $img;
  219. $is_png = false;
  220. $filedummy .= '_' . $bg_width . '_' . $bg_height . '_' . $bg_x . '_' . $bg_y . '_' . $repeat;
  221. //Optimization to avoid multiple times rendering the same image.
  222. //If check functions are existing and identical image already cached,
  223. //then skip creation of duplicate, because it is not needed by addImagePng
  224. if ($this->_canvas instanceof CPDF && $this->_canvas->get_cpdf()->image_iscached($filedummy)) {
  225. $bg = null;
  226. } else {
  227. // Create a new image to fit over the background rectangle
  228. $bg = imagecreatetruecolor($bg_width, $bg_height);
  229. switch (strtolower($type)) {
  230. case "png":
  231. $is_png = true;
  232. imagesavealpha($bg, true);
  233. imagealphablending($bg, false);
  234. $src = imagecreatefrompng($img);
  235. break;
  236. case "jpeg":
  237. $src = imagecreatefromjpeg($img);
  238. break;
  239. case "gif":
  240. $src = imagecreatefromgif($img);
  241. break;
  242. case "bmp":
  243. $src = Helpers::imagecreatefrombmp($img);
  244. break;
  245. default:
  246. return; // Unsupported image type
  247. }
  248. if ($src == null) {
  249. return;
  250. }
  251. if ($img_w != $org_img_w || $img_h != $org_img_h) {
  252. $newSrc = imagescale($src, $img_w, $img_h);
  253. imagedestroy($src);
  254. $src = $newSrc;
  255. }
  256. if ($src == null) {
  257. return;
  258. }
  259. //Background color if box is not relevant here
  260. //Non transparent image: box clipped to real size. Background non relevant.
  261. //Transparent image: The image controls the transparency and lets shine through whatever background.
  262. //However on transparent image preset the composed image with the transparency color,
  263. //to keep the transparency when copying over the non transparent parts of the tiles.
  264. $ti = imagecolortransparent($src);
  265. $palletsize = imagecolorstotal($src);
  266. if ($ti >= 0 && $ti < $palletsize) {
  267. $tc = imagecolorsforindex($src, $ti);
  268. $ti = imagecolorallocate($bg, $tc['red'], $tc['green'], $tc['blue']);
  269. imagefill($bg, 0, 0, $ti);
  270. imagecolortransparent($bg, $ti);
  271. }
  272. //This has only an effect for the non repeatable dimension.
  273. //compute start of src and dest coordinates of the single copy
  274. if ($bg_x < 0) {
  275. $dst_x = 0;
  276. $src_x = -$bg_x;
  277. } else {
  278. $src_x = 0;
  279. $dst_x = $bg_x;
  280. }
  281. if ($bg_y < 0) {
  282. $dst_y = 0;
  283. $src_y = -$bg_y;
  284. } else {
  285. $src_y = 0;
  286. $dst_y = $bg_y;
  287. }
  288. //For historical reasons exchange meanings of variables:
  289. //start_* will be the start values, while bg_* will be the temporary start values in the loops
  290. $start_x = $bg_x;
  291. $start_y = $bg_y;
  292. // Copy regions from the source image to the background
  293. if ($repeat === "no-repeat") {
  294. // Simply place the image on the background
  295. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $img_h);
  296. } else if ($repeat === "repeat-x") {
  297. for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
  298. if ($bg_x < 0) {
  299. $dst_x = 0;
  300. $src_x = -$bg_x;
  301. $w = $img_w + $bg_x;
  302. } else {
  303. $dst_x = $bg_x;
  304. $src_x = 0;
  305. $w = $img_w;
  306. }
  307. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $img_h);
  308. }
  309. } else if ($repeat === "repeat-y") {
  310. for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
  311. if ($bg_y < 0) {
  312. $dst_y = 0;
  313. $src_y = -$bg_y;
  314. $h = $img_h + $bg_y;
  315. } else {
  316. $dst_y = $bg_y;
  317. $src_y = 0;
  318. $h = $img_h;
  319. }
  320. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $img_w, $h);
  321. }
  322. } else if ($repeat === "repeat") {
  323. for ($bg_y = $start_y; $bg_y < $bg_height; $bg_y += $img_h) {
  324. for ($bg_x = $start_x; $bg_x < $bg_width; $bg_x += $img_w) {
  325. if ($bg_x < 0) {
  326. $dst_x = 0;
  327. $src_x = -$bg_x;
  328. $w = $img_w + $bg_x;
  329. } else {
  330. $dst_x = $bg_x;
  331. $src_x = 0;
  332. $w = $img_w;
  333. }
  334. if ($bg_y < 0) {
  335. $dst_y = 0;
  336. $src_y = -$bg_y;
  337. $h = $img_h + $bg_y;
  338. } else {
  339. $dst_y = $bg_y;
  340. $src_y = 0;
  341. $h = $img_h;
  342. }
  343. imagecopy($bg, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h);
  344. }
  345. }
  346. } else {
  347. print 'Unknown repeat!';
  348. }
  349. imagedestroy($src);
  350. } /* End optimize away creation of duplicates */
  351. $this->_canvas->clipping_rectangle($x, $y, $box_width, $box_height);
  352. //img: image url string
  353. //img_w, img_h: original image size in px
  354. //width, height: box size in pt
  355. //bg_width, bg_height: box size in px
  356. //x, y: left/top edge of box on page in pt
  357. //start_x, start_y: placement of image relative to pattern
  358. //$repeat: repeat mode
  359. //$bg: GD object of result image
  360. //$src: GD object of original image
  361. //When using cpdf and optimization to direct png creation from gd object is available,
  362. //don't create temp file, but place gd object directly into the pdf
  363. if (!$is_png && $this->_canvas instanceof CPDF) {
  364. // Note: CPDF_Adapter image converts y position
  365. $this->_canvas->get_cpdf()->addImagePng($bg, $filedummy, $x, $this->_canvas->get_height() - $y - $height, $width, $height);
  366. } else {
  367. $tmp_dir = $this->_dompdf->getOptions()->getTempDir();
  368. $tmp_name = @tempnam($tmp_dir, "bg_dompdf_img_");
  369. @unlink($tmp_name);
  370. $tmp_file = "$tmp_name.png";
  371. //debugpng
  372. if ($this->_dompdf->getOptions()->getDebugPng()) {
  373. print '[_background_image ' . $tmp_file . ']';
  374. }
  375. imagepng($bg, $tmp_file);
  376. $this->_canvas->image($tmp_file, $x, $y, $width, $height);
  377. imagedestroy($bg);
  378. //debugpng
  379. if ($this->_dompdf->getOptions()->getDebugPng()) {
  380. print '[_background_image unlink ' . $tmp_file . ']';
  381. }
  382. if (!$this->_dompdf->getOptions()->getDebugKeepTemp()) {
  383. unlink($tmp_file);
  384. }
  385. }
  386. $this->_canvas->clipping_end();
  387. }
  388. /**
  389. * @param $style
  390. * @param $width
  391. * @return array
  392. */
  393. protected function _get_dash_pattern($style, $width)
  394. {
  395. $pattern = [];
  396. switch ($style) {
  397. default:
  398. /*case "solid":
  399. case "double":
  400. case "groove":
  401. case "inset":
  402. case "outset":
  403. case "ridge":*/
  404. case "none":
  405. break;
  406. case "dotted":
  407. if ($width <= 1) {
  408. $pattern = [$width, $width * 2];
  409. } else {
  410. $pattern = [$width];
  411. }
  412. break;
  413. case "dashed":
  414. $pattern = [3 * $width];
  415. break;
  416. }
  417. return $pattern;
  418. }
  419. /**
  420. * @param $x
  421. * @param $y
  422. * @param $length
  423. * @param $color
  424. * @param $widths
  425. * @param $side
  426. * @param string $corner_style
  427. * @param int $r1
  428. * @param int $r2
  429. */
  430. protected function _border_none($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  431. {
  432. return;
  433. }
  434. /**
  435. * @param $x
  436. * @param $y
  437. * @param $length
  438. * @param $color
  439. * @param $widths
  440. * @param $side
  441. * @param string $corner_style
  442. * @param int $r1
  443. * @param int $r2
  444. */
  445. protected function _border_hidden($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  446. {
  447. return;
  448. }
  449. // Border rendering functions
  450. /**
  451. * @param $x
  452. * @param $y
  453. * @param $length
  454. * @param $color
  455. * @param $widths
  456. * @param $side
  457. * @param string $corner_style
  458. * @param int $r1
  459. * @param int $r2
  460. */
  461. protected function _border_dotted($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  462. {
  463. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dotted", $r1, $r2);
  464. }
  465. /**
  466. * @param $x
  467. * @param $y
  468. * @param $length
  469. * @param $color
  470. * @param $widths
  471. * @param $side
  472. * @param string $corner_style
  473. * @param int $r1
  474. * @param int $r2
  475. */
  476. protected function _border_dashed($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  477. {
  478. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "dashed", $r1, $r2);
  479. }
  480. /**
  481. * @param $x
  482. * @param $y
  483. * @param $length
  484. * @param $color
  485. * @param $widths
  486. * @param $side
  487. * @param string $corner_style
  488. * @param int $r1
  489. * @param int $r2
  490. */
  491. protected function _border_solid($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  492. {
  493. // TODO: Solve rendering where one corner is beveled (radius == 0), one corner isn't.
  494. if ($corner_style !== "bevel" || $r1 > 0 || $r2 > 0) {
  495. // do it the simple way
  496. $this->_border_line($x, $y, $length, $color, $widths, $side, $corner_style, "solid", $r1, $r2);
  497. return;
  498. }
  499. list($top, $right, $bottom, $left) = $widths;
  500. // All this polygon business is for beveled corners...
  501. switch ($side) {
  502. case "top":
  503. $points = [$x, $y,
  504. $x + $length, $y,
  505. $x + $length - $right, $y + $top,
  506. $x + $left, $y + $top];
  507. $this->_canvas->polygon($points, $color, null, null, true);
  508. break;
  509. case "bottom":
  510. $points = [$x, $y,
  511. $x + $length, $y,
  512. $x + $length - $right, $y - $bottom,
  513. $x + $left, $y - $bottom];
  514. $this->_canvas->polygon($points, $color, null, null, true);
  515. break;
  516. case "left":
  517. $points = [$x, $y,
  518. $x, $y + $length,
  519. $x + $left, $y + $length - $bottom,
  520. $x + $left, $y + $top];
  521. $this->_canvas->polygon($points, $color, null, null, true);
  522. break;
  523. case "right":
  524. $points = [$x, $y,
  525. $x, $y + $length,
  526. $x - $right, $y + $length - $bottom,
  527. $x - $right, $y + $top];
  528. $this->_canvas->polygon($points, $color, null, null, true);
  529. break;
  530. default:
  531. return;
  532. }
  533. }
  534. /**
  535. * @param $side
  536. * @param $ratio
  537. * @param $top
  538. * @param $right
  539. * @param $bottom
  540. * @param $left
  541. * @param $x
  542. * @param $y
  543. * @param $length
  544. * @param $r1
  545. * @param $r2
  546. */
  547. protected function _apply_ratio($side, $ratio, $top, $right, $bottom, $left, &$x, &$y, &$length, &$r1, &$r2)
  548. {
  549. switch ($side) {
  550. case "top":
  551. $r1 -= $left * $ratio;
  552. $r2 -= $right * $ratio;
  553. $x += $left * $ratio;
  554. $y += $top * $ratio;
  555. $length -= $left * $ratio + $right * $ratio;
  556. break;
  557. case "bottom":
  558. $r1 -= $right * $ratio;
  559. $r2 -= $left * $ratio;
  560. $x += $left * $ratio;
  561. $y -= $bottom * $ratio;
  562. $length -= $left * $ratio + $right * $ratio;
  563. break;
  564. case "left":
  565. $r1 -= $top * $ratio;
  566. $r2 -= $bottom * $ratio;
  567. $x += $left * $ratio;
  568. $y += $top * $ratio;
  569. $length -= $top * $ratio + $bottom * $ratio;
  570. break;
  571. case "right":
  572. $r1 -= $bottom * $ratio;
  573. $r2 -= $top * $ratio;
  574. $x -= $right * $ratio;
  575. $y += $top * $ratio;
  576. $length -= $top * $ratio + $bottom * $ratio;
  577. break;
  578. default:
  579. return;
  580. }
  581. }
  582. /**
  583. * @param $x
  584. * @param $y
  585. * @param $length
  586. * @param $color
  587. * @param $widths
  588. * @param $side
  589. * @param string $corner_style
  590. * @param int $r1
  591. * @param int $r2
  592. */
  593. protected function _border_double($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  594. {
  595. list($top, $right, $bottom, $left) = $widths;
  596. $third_widths = [$top / 3, $right / 3, $bottom / 3, $left / 3];
  597. // draw the outer border
  598. $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
  599. $this->_apply_ratio($side, 2 / 3, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  600. $this->_border_solid($x, $y, $length, $color, $third_widths, $side, $corner_style, $r1, $r2);
  601. }
  602. /**
  603. * @param $x
  604. * @param $y
  605. * @param $length
  606. * @param $color
  607. * @param $widths
  608. * @param $side
  609. * @param string $corner_style
  610. * @param int $r1
  611. * @param int $r2
  612. */
  613. protected function _border_groove($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  614. {
  615. list($top, $right, $bottom, $left) = $widths;
  616. $half_widths = [$top / 2, $right / 2, $bottom / 2, $left / 2];
  617. $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  618. $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  619. $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  620. }
  621. /**
  622. * @param $x
  623. * @param $y
  624. * @param $length
  625. * @param $color
  626. * @param $widths
  627. * @param $side
  628. * @param string $corner_style
  629. * @param int $r1
  630. * @param int $r2
  631. */
  632. protected function _border_ridge($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  633. {
  634. list($top, $right, $bottom, $left) = $widths;
  635. $half_widths = [$top / 2, $right / 2, $bottom / 2, $left / 2];
  636. $this->_border_outset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  637. $this->_apply_ratio($side, 0.5, $top, $right, $bottom, $left, $x, $y, $length, $r1, $r2);
  638. $this->_border_inset($x, $y, $length, $color, $half_widths, $side, $corner_style, $r1, $r2);
  639. }
  640. /**
  641. * @param $c
  642. * @return mixed
  643. */
  644. protected function _tint($c)
  645. {
  646. if (!is_numeric($c)) {
  647. return $c;
  648. }
  649. return min(1, $c + 0.16);
  650. }
  651. /**
  652. * @param $c
  653. * @return mixed
  654. */
  655. protected function _shade($c)
  656. {
  657. if (!is_numeric($c)) {
  658. return $c;
  659. }
  660. return max(0, $c - 0.33);
  661. }
  662. /**
  663. * @param $x
  664. * @param $y
  665. * @param $length
  666. * @param $color
  667. * @param $widths
  668. * @param $side
  669. * @param string $corner_style
  670. * @param int $r1
  671. * @param int $r2
  672. */
  673. protected function _border_inset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  674. {
  675. switch ($side) {
  676. case "top":
  677. case "left":
  678. $shade = array_map([$this, "_shade"], $color);
  679. $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
  680. break;
  681. case "bottom":
  682. case "right":
  683. $tint = array_map([$this, "_tint"], $color);
  684. $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
  685. break;
  686. default:
  687. return;
  688. }
  689. }
  690. /**
  691. * @param $x
  692. * @param $y
  693. * @param $length
  694. * @param $color
  695. * @param $widths
  696. * @param $side
  697. * @param string $corner_style
  698. * @param int $r1
  699. * @param int $r2
  700. */
  701. protected function _border_outset($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $r1 = 0, $r2 = 0)
  702. {
  703. switch ($side) {
  704. case "top":
  705. case "left":
  706. $tint = array_map([$this, "_tint"], $color);
  707. $this->_border_solid($x, $y, $length, $tint, $widths, $side, $corner_style, $r1, $r2);
  708. break;
  709. case "bottom":
  710. case "right":
  711. $shade = array_map([$this, "_shade"], $color);
  712. $this->_border_solid($x, $y, $length, $shade, $widths, $side, $corner_style, $r1, $r2);
  713. break;
  714. default:
  715. return;
  716. }
  717. }
  718. /**
  719. * Draws a solid, dotted, or dashed line, observing the border radius
  720. *
  721. * @param $x
  722. * @param $y
  723. * @param $length
  724. * @param $color
  725. * @param $widths
  726. * @param $side
  727. * @param string $corner_style
  728. * @param $pattern_name
  729. * @param int $r1
  730. * @param int $r2
  731. *
  732. * @var $top
  733. */
  734. protected function _border_line($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $pattern_name = "none", $r1 = 0, $r2 = 0)
  735. {
  736. /** used by $$side */
  737. list($top, $right, $bottom, $left) = $widths;
  738. $width = $$side;
  739. $pattern = $this->_get_dash_pattern($pattern_name, $width);
  740. $half_width = $width / 2;
  741. $r1 -= $half_width;
  742. $r2 -= $half_width;
  743. $adjust = $r1 / 80;
  744. $length -= $width;
  745. switch ($side) {
  746. case "top":
  747. $x += $half_width;
  748. $y += $half_width;
  749. if ($r1 > 0) {
  750. $this->_canvas->arc($x + $r1, $y + $r1, $r1, $r1, 90 - $adjust, 135 + $adjust, $color, $width, $pattern);
  751. }
  752. $this->_canvas->line($x + $r1, $y, $x + $length - $r2, $y, $color, $width, $pattern);
  753. if ($r2 > 0) {
  754. $this->_canvas->arc($x + $length - $r2, $y + $r2, $r2, $r2, 45 - $adjust, 90 + $adjust, $color, $width, $pattern);
  755. }
  756. break;
  757. case "bottom":
  758. $x += $half_width;
  759. $y -= $half_width;
  760. if ($r1 > 0) {
  761. $this->_canvas->arc($x + $r1, $y - $r1, $r1, $r1, 225 - $adjust, 270 + $adjust, $color, $width, $pattern);
  762. }
  763. $this->_canvas->line($x + $r1, $y, $x + $length - $r2, $y, $color, $width, $pattern);
  764. if ($r2 > 0) {
  765. $this->_canvas->arc($x + $length - $r2, $y - $r2, $r2, $r2, 270 - $adjust, 315 + $adjust, $color, $width, $pattern);
  766. }
  767. break;
  768. case "left":
  769. $y += $half_width;
  770. $x += $half_width;
  771. if ($r1 > 0) {
  772. $this->_canvas->arc($x + $r1, $y + $r1, $r1, $r1, 135 - $adjust, 180 + $adjust, $color, $width, $pattern);
  773. }
  774. $this->_canvas->line($x, $y + $r1, $x, $y + $length - $r2, $color, $width, $pattern);
  775. if ($r2 > 0) {
  776. $this->_canvas->arc($x + $r2, $y + $length - $r2, $r2, $r2, 180 - $adjust, 225 + $adjust, $color, $width, $pattern);
  777. }
  778. break;
  779. case "right":
  780. $y += $half_width;
  781. $x -= $half_width;
  782. if ($r1 > 0) {
  783. $this->_canvas->arc($x - $r1, $y + $r1, $r1, $r1, 0 - $adjust, 45 + $adjust, $color, $width, $pattern);
  784. }
  785. $this->_canvas->line($x, $y + $r1, $x, $y + $length - $r2, $color, $width, $pattern);
  786. if ($r2 > 0) {
  787. $this->_canvas->arc($x - $r2, $y + $length - $r2, $r2, $r2, 315 - $adjust, 360 + $adjust, $color, $width, $pattern);
  788. }
  789. break;
  790. }
  791. }
  792. /**
  793. * @param $opacity
  794. */
  795. protected function _set_opacity($opacity)
  796. {
  797. if (is_numeric($opacity) && $opacity <= 1.0 && $opacity >= 0.0) {
  798. $this->_canvas->set_opacity($opacity);
  799. }
  800. }
  801. /**
  802. * @param $box
  803. * @param string $color
  804. * @param array $style
  805. */
  806. protected function _debug_layout($box, $color = "red", $style = [])
  807. {
  808. $this->_canvas->rectangle($box[0], $box[1], $box[2], $box[3], Color::parse($color), 0.1, $style);
  809. }
  810. /**
  811. * @param float $img_width
  812. * @param float $img_height
  813. * @param float $container_width
  814. * @param float $container_height
  815. * @param array|string $bg_resize
  816. * @param int $dpi
  817. * @return array
  818. */
  819. protected function _resize_background_image(
  820. $img_width,
  821. $img_height,
  822. $container_width,
  823. $container_height,
  824. $bg_resize,
  825. $dpi
  826. ) {
  827. // We got two some specific numbers and/or auto definitions
  828. if (is_array($bg_resize)) {
  829. $is_auto_width = $bg_resize[0] === 'auto';
  830. if ($is_auto_width) {
  831. $new_img_width = $img_width;
  832. } else {
  833. $new_img_width = $bg_resize[0];
  834. if (Helpers::is_percent($new_img_width)) {
  835. $new_img_width = round(($container_width / 100) * (float)$new_img_width);
  836. } else {
  837. $new_img_width = round($new_img_width * $dpi / 72);
  838. }
  839. }
  840. $is_auto_height = $bg_resize[1] === 'auto';
  841. if ($is_auto_height) {
  842. $new_img_height = $img_height;
  843. } else {
  844. $new_img_height = $bg_resize[1];
  845. if (Helpers::is_percent($new_img_height)) {
  846. $new_img_height = round(($container_height / 100) * (float)$new_img_height);
  847. } else {
  848. $new_img_height = round($new_img_height * $dpi / 72);
  849. }
  850. }
  851. // if one of both was set to auto the other one needs to scale proportionally
  852. if ($is_auto_width !== $is_auto_height) {
  853. if ($is_auto_height) {
  854. $new_img_height = round($new_img_width * ($img_height / $img_width));
  855. } else {
  856. $new_img_width = round($new_img_height * ($img_width / $img_height));
  857. }
  858. }
  859. } else {
  860. $container_ratio = $container_height / $container_width;
  861. if ($bg_resize === 'cover' || $bg_resize === 'contain') {
  862. $img_ratio = $img_height / $img_width;
  863. if (
  864. ($bg_resize === 'cover' && $container_ratio > $img_ratio) ||
  865. ($bg_resize === 'contain' && $container_ratio < $img_ratio)
  866. ) {
  867. $new_img_height = $container_height;
  868. $new_img_width = round($container_height / $img_ratio);
  869. } else {
  870. $new_img_width = $container_width;
  871. $new_img_height = round($container_width * $img_ratio);
  872. }
  873. } else {
  874. $new_img_width = $img_width;
  875. $new_img_height = $img_height;
  876. }
  877. }
  878. return [$new_img_width, $new_img_height];
  879. }
  880. }