block_frame_reflower.cls.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: block_frame_reflower.cls.php,v $
  6. * Created on: 2004-06-17
  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. * @package dompdf
  37. */
  38. /* $Id: block_frame_reflower.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Reflows block frames
  41. *
  42. * @access private
  43. * @package dompdf
  44. */
  45. class Block_Frame_Reflower extends Frame_Reflower {
  46. const MIN_JUSTIFY_WIDTH = 0.80; // (Minimum line width to justify, as
  47. // fraction of available width)
  48. function __construct(Block_Frame_Decorator $frame) { parent::__construct($frame); }
  49. //........................................................................
  50. // Calculate the ideal used value for the width property as per:
  51. // http://www.w3.org/TR/CSS21/visudet.html#Computing_widths_and_margins
  52. protected function _calculate_width($width) {
  53. $style = $this->_frame->get_style();
  54. $w = $this->_frame->get_containing_block("w");
  55. $rm = $style->length_in_pt($style->margin_right, $w);
  56. $lm = $style->length_in_pt($style->margin_left, $w);
  57. $left = $style->length_in_pt($style->left, $w);
  58. $right = $style->length_in_pt($style->right, $w);
  59. // Handle 'auto' values
  60. $dims = array($style->border_left_width,
  61. $style->border_right_width,
  62. $style->padding_left,
  63. $style->padding_right,
  64. $width !== "auto" ? $width : 0,
  65. $rm !== "auto" ? $rm : 0,
  66. $lm !== "auto" ? $lm : 0);
  67. // absolutely positioned boxes take the 'left' and 'right' properties into account
  68. if ( $style->position === "absolute" || $style->position === "fixed" ) {
  69. $absolute = true;
  70. $dims[] = $left !== "auto" ? $left : 0;
  71. $dims[] = $right !== "auto" ? $right : 0;
  72. } else {
  73. $absolute = false;
  74. }
  75. $sum = $style->length_in_pt($dims, $w);
  76. // Compare to the containing block
  77. $diff = $w - $sum;
  78. if ( $diff > 0 ) {
  79. if ( $absolute ) {
  80. // resolve auto properties: see
  81. // http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width
  82. if ( $width === "auto" && $left === "auto" && $right === "auto" ) {
  83. if ( $lm === "auto" )
  84. $lm = 0;
  85. if ( $rm === "auto" )
  86. $rm = 0;
  87. // Technically, the width should be "shrink-to-fit" i.e. based on the
  88. // preferred width of the content... a little too costly here as a
  89. // special case. Just get the width to take up the slack:
  90. $left = 0;
  91. $right = 0;
  92. $width = $diff;
  93. } else if ( $width === "auto" ) {
  94. if ( $lm === "auto" )
  95. $lm = 0;
  96. if ( $rm === "auto" )
  97. $rm = 0;
  98. if ( $left === "auto" )
  99. $left = 0;
  100. if ( $right === "auto" )
  101. $right = 0;
  102. $width = $diff;
  103. } else if ( $left === "auto" ) {
  104. if ( $lm === "auto" )
  105. $lm = 0;
  106. if ( $rm === "auto" )
  107. $rm = 0;
  108. if ( $right === "auto" )
  109. $right = 0;
  110. $left = $diff;
  111. } else if ( $right === "auto" ) {
  112. if ( $lm === "auto" )
  113. $lm = 0;
  114. if ( $rm === "auto" )
  115. $rm = 0;
  116. $right = $diff;
  117. }
  118. } else {
  119. // Find auto properties and get them to take up the slack
  120. if ( $width === "auto" )
  121. $width = $diff;
  122. else if ( $lm === "auto" && $rm === "auto" )
  123. $lm = $rm = round($diff / 2);
  124. else if ( $lm === "auto" )
  125. $lm = $diff;
  126. else if ( $rm === "auto" )
  127. $rm = $diff;
  128. }
  129. } else if ($diff < 0) {
  130. // We are over constrained--set margin-right to the difference
  131. $rm = $diff;
  132. }
  133. $ret = array("width"=> $width, "margin_left" => $lm, "margin_right" => $rm, "left" => $left, "right" => $right);
  134. return $ret;
  135. }
  136. // Call the above function, but resolve max/min widths
  137. protected function _calculate_restricted_width() {
  138. $style = $this->_frame->get_style();
  139. $cb = $this->_frame->get_containing_block();
  140. if ( !isset($cb["w"]) )
  141. throw new DOMPDF_Exception("Box property calculation requires containing block width");
  142. // Treat width 100% as auto
  143. if ( $style->width === "100%" )
  144. $width = "auto";
  145. else
  146. $width = $style->length_in_pt($style->width, $cb["w"]);
  147. extract($this->_calculate_width($width));
  148. // Handle min/max width
  149. $min_width = $style->length_in_pt($style->min_width, $cb["w"]);
  150. $max_width = $style->length_in_pt($style->max_width, $cb["w"]);
  151. if ( $max_width !== "none" && $min_width > $max_width)
  152. // Swap 'em
  153. list($max_width, $min_width) = array($min_width, $max_width);
  154. if ( $max_width !== "none" && $width > $max_width )
  155. extract($this->_calculate_width($max_width));
  156. if ( $width < $min_width )
  157. extract($this->_calculate_width($min_width));
  158. return array($width, $margin_left, $margin_right, $left, $right);
  159. }
  160. //........................................................................
  161. // Determine the unrestricted height of content within the block
  162. protected function _calculate_content_height() {
  163. // Calculate the actual height
  164. $height = 0;
  165. // Add the height of all lines
  166. foreach ($this->_frame->get_lines() as $line)
  167. $height += $line["h"];
  168. return $height;
  169. }
  170. // Determine the frame's restricted height
  171. protected function _calculate_restricted_height() {
  172. $style = $this->_frame->get_style();
  173. $content_height = $this->_calculate_content_height();
  174. $cb = $this->_frame->get_containing_block();
  175. $height = $style->length_in_pt($style->height, $cb["h"]);
  176. $top = $style->length_in_pt($style->top, $cb["h"]);
  177. $bottom = $style->length_in_pt($style->bottom, $cb["h"]);
  178. $margin_top = $style->length_in_pt($style->margin_top, $cb["h"]);
  179. $margin_bottom = $style->length_in_pt($style->margin_bottom, $cb["h"]);
  180. if ( $style->position === "absolute" || $style->position === "fixed" ) {
  181. // see http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-height
  182. $dims = array($top !== "auto" ? $top : 0,
  183. $style->margin_top !== "auto" ? $style->margin_top : 0,
  184. $style->padding_top,
  185. $style->border_top_width,
  186. $height !== "height" ? $height : 0,
  187. $style->border_bottom_width,
  188. $style->padding_bottom,
  189. $style->margin_bottom !== "auto" ? $style->margin_bottom : 0,
  190. $bottom !== "auto" ? $bottom : 0);
  191. $sum = $style->length_in_pt($dims, $cb["h"]);
  192. $diff = $cb["h"] - $sum;
  193. if ( $diff > 0 ) {
  194. if ( $height === "auto" && $top === "auto" && $bottom === "auto" ) {
  195. if ( $margin_top === "auto" )
  196. $margin_top = 0;
  197. if ( $margin_bottom === "auto" )
  198. $margin_bottom = 0;
  199. $height = $diff;
  200. } else if ( $height === "auto" && $top === "auto" ) {
  201. if ( $margin_top === "auto" )
  202. $margin_top = 0;
  203. if ( $margin_bottom === "auto" )
  204. $margin_bottom = 0;
  205. $height = $content_height;
  206. $top = $diff - $content_height;
  207. } else if ( $height === "auto" && $bottom === "auto" ) {
  208. if ( $margin_top === "auto" )
  209. $margin_top = 0;
  210. if ( $margin_bottom === "auto" )
  211. $margin_bottom = 0;
  212. $height = $content_height;
  213. $bottom = $diff - $content_height;
  214. } else if ( $top === "auto" && $bottom === "auto" ) {
  215. if ( $margin_top === "auto" )
  216. $margin_top = 0;
  217. if ( $margin_bottom === "auto" )
  218. $margin_bottom = 0;
  219. $bottom = $diff;
  220. } else if ( $top === "auto" ) {
  221. if ( $margin_top === "auto" )
  222. $margin_top = 0;
  223. if ( $margin_bottom === "auto" )
  224. $margin_bottom = 0;
  225. $top = $diff;
  226. } else if ( $height === "auto" ) {
  227. if ( $margin_top === "auto" )
  228. $margin_top = 0;
  229. if ( $margin_bottom === "auto" )
  230. $margin_bottom = 0;
  231. $height = $diff;
  232. } else if ( $bottom === "auto" ) {
  233. if ( $margin_top === "auto" )
  234. $margin_top = 0;
  235. if ( $margin_bottom === "auto" )
  236. $margin_bottom = 0;
  237. $bottom = $diff;
  238. } else {
  239. if ( $style->overflow === "visible" ) {
  240. // set all autos to zero
  241. if ( $margin_top === "auto" )
  242. $margin_top = 0;
  243. if ( $margin_bottom === "auto" )
  244. $margin_bottom = 0;
  245. if ( $top === "auto" )
  246. $top = 0;
  247. if ( $bottom === "auto" )
  248. $bottom = 0;
  249. if ( $height === "auto" )
  250. $height = $content_height;
  251. }
  252. // FIXME: overflow hidden
  253. }
  254. }
  255. } else {
  256. // Expand the height if overflow is visible
  257. if ( $height == "auto" && $content_height > $height && $style->overflow === "visible" )
  258. $height = $content_height;
  259. // FIXME: this should probably be moved to a seperate function as per
  260. // _calculate_restricted_width
  261. // Only handle min/max height if the height is independent of the frame's content
  262. if ( !($style->overflow === "visible" ||
  263. ($style->overflow === "hidden" && $height === "auto")) ) {
  264. $min_height = $style->min_height;
  265. $max_height = $style->max_height;
  266. if ( isset($cb["h"]) ) {
  267. $min_height = $style->length_in_pt($min_height, $cb["h"]);
  268. $max_height = $style->length_in_pt($max_height, $cb["h"]);
  269. } else if ( isset($cb["w"]) ) {
  270. if ( mb_strpos($min_height, "%") !== false )
  271. $min_height = 0;
  272. else
  273. $min_height = $style->length_in_pt($min_height, $cb["w"]);
  274. if ( mb_strpos($max_height, "%") !== false )
  275. $max_height = "none";
  276. else
  277. $max_height = $style->length_in_pt($max_height, $cb["w"]);
  278. }
  279. if ( $max_height !== "none" && $min_height > $max_height )
  280. // Swap 'em
  281. list($max_height, $min_height) = array($min_height, $max_height);
  282. if ( $max_height !== "none" && $height > $max_height )
  283. $height = $max_height;
  284. if ( $height < $min_height )
  285. $height = $min_height;
  286. }
  287. }
  288. return array($height, $margin_top, $margin_bottom, $top, $bottom);
  289. }
  290. //........................................................................
  291. protected function _text_align() {
  292. $style = $this->_frame->get_style();
  293. $w = $this->_frame->get_containing_block("w");
  294. $width = $style->length_in_pt($style->width, $w);
  295. // Adjust the justification of each of our lines.
  296. // http://www.w3.org/TR/CSS21/text.html#propdef-text-align
  297. switch ($style->text_align) {
  298. default:
  299. case "left":
  300. return;
  301. case "right":
  302. foreach ($this->_frame->get_lines() as $line) {
  303. // Move each child over by $dx
  304. $dx = $width - $line["w"];
  305. foreach($line["frames"] as $frame)
  306. $frame->set_position( $frame->get_position("x") + $dx );
  307. }
  308. break;
  309. case "justify":
  310. foreach ($this->_frame->get_lines() as $i => $line) {
  311. // Only set the spacing if the line is long enough. This is really
  312. // just an aesthetic choice ;)
  313. if ( $line["w"] > self::MIN_JUSTIFY_WIDTH * $width ) {
  314. // Set the spacing for each child
  315. if ( $line["wc"] > 1 )
  316. $spacing = ($width - $line["w"]) / ($line["wc"] - 1);
  317. else
  318. $spacing = 0;
  319. $dx = 0;
  320. foreach($line["frames"] as $frame) {
  321. if ( !$frame instanceof Text_Frame_Decorator )
  322. continue;
  323. $frame->set_position( $frame->get_position("x") + $dx );
  324. $frame->set_text_spacing($spacing);
  325. $dx += mb_substr_count($frame->get_text(), " ") * $spacing;
  326. }
  327. // The line (should) now occupy the entire width
  328. $this->_frame->set_line($i, null, $width);
  329. }
  330. }
  331. break;
  332. case "center":
  333. case "centre":
  334. foreach ($this->_frame->get_lines() as $i => $line) {
  335. // Centre each line by moving each frame in the line by:
  336. $dx = ($width - $line["w"]) / 2;
  337. foreach ($line["frames"] as $frame)
  338. $frame->set_position( $frame->get_position("x") + $dx );
  339. }
  340. break;
  341. }
  342. }
  343. /**
  344. * Align inline children vertically
  345. */
  346. function vertical_align() {
  347. // Align each child vertically after each line is reflowed
  348. foreach ( $this->_frame->get_lines() as $i => $line ) {
  349. foreach ( $line["frames"] as $frame ) {
  350. $style = $frame->get_style();
  351. if ( $style->display !== "inline" && $style->display !== "text" )
  352. continue;
  353. $align = $style->vertical_align;
  354. $frame_h = $frame->get_margin_height();
  355. switch ($align) {
  356. case "baseline":
  357. $y = $line["y"] + $line["h"] - $frame_h;
  358. break;
  359. case "middle":
  360. $y = $line["y"] + ($line["h"] + $frame_h) / 2;
  361. break;
  362. case "sub":
  363. $y = $line["y"] + 0.9 * $line["h"];
  364. break;
  365. case "super":
  366. $y = $line["y"] + 0.1 * $line["h"];
  367. break;
  368. case "text-top":
  369. case "top": // Not strictly accurate, but good enough for now
  370. $y = $line["y"];
  371. break;
  372. case "text-bottom":
  373. case "bottom":
  374. $y = $line["y"] + $line["h"] - $frame_h;
  375. break;
  376. }
  377. $x = $frame->get_position("x");
  378. $frame->set_position($x, $y);
  379. }
  380. }
  381. }
  382. //........................................................................
  383. function reflow() {
  384. // Check if a page break is forced
  385. $page = $this->_frame->get_root();
  386. $page->check_forced_page_break($this->_frame);
  387. // Bail if the page is full
  388. if ( $page->is_full() )
  389. return;
  390. // Collapse margins if required
  391. $this->_collapse_margins();
  392. $style = $this->_frame->get_style();
  393. $cb = $this->_frame->get_containing_block();
  394. // Determine the constraints imposed by this frame: calculate the width
  395. // of the content area:
  396. list($w, $left_margin, $right_margin, $left, $right) = $this->_calculate_restricted_width();
  397. // Store the calculated properties
  398. $style->width = $w;
  399. $style->margin_left = $left_margin."pt";
  400. $style->margin_right = $right_margin."pt";
  401. $style->left = $left ."pt";
  402. $style->right = $right . "pt";
  403. // Update the position
  404. $this->_frame->position();
  405. list($x, $y) = $this->_frame->get_position();
  406. // Adjust the first line based on the text-indent property
  407. $indent = $style->length_in_pt($style->text_indent, $cb["w"]);
  408. $this->_frame->increase_line_width($indent);
  409. // Determine the content edge
  410. $top = $style->length_in_pt(array($style->margin_top,
  411. $style->padding_top,
  412. $style->border_top_width), $cb["h"]);
  413. $bottom = $style->length_in_pt(array($style->border_bottom_width,
  414. $style->margin_bottom,
  415. $style->padding_bottom), $cb["h"]);
  416. $cb_x = $x + $left_margin +
  417. $style->length_in_pt($style->border_left_width, $cb["w"]) +
  418. $style->length_in_pt($style->padding_left, $cb["w"]);
  419. $cb_y = $line_y = $y + $top;
  420. $cb_h = ($cb["h"] + $cb["y"]) - $bottom - $cb_y;
  421. // Set the y position of the first line in this block
  422. $this->_frame->set_current_line($line_y);
  423. // Set the containing blocks and reflow each child
  424. foreach ( $this->_frame->get_children() as $child ) {
  425. // Bail out if the page is full
  426. if ( $page->is_full() )
  427. break;
  428. $child->set_containing_block($cb_x, $cb_y, $w, $cb_h);
  429. $child->reflow();
  430. // Don't add the child to the line if a page break has occurred
  431. if ( $page->check_page_break($child) )
  432. break;
  433. // If the frame is not absolutely positioned, It's okay to add the frame
  434. // to the line
  435. if ( $child->get_style()->position !== "absolute" &&
  436. $child->get_style()->position !== "fixed" ) {
  437. $this->_frame->add_frame_to_line( $child );
  438. }
  439. }
  440. // Determine our height
  441. list($height, $margin_top, $margin_bottom, $top, $bottom) = $this->_calculate_restricted_height();
  442. $style->height = $height;
  443. $style->margin_top = $margin_top;
  444. $style->margin_bottom = $margin_bottom;
  445. $style->top = $top;
  446. $style->bottom = $bottom;
  447. $this->_text_align();
  448. $this->vertical_align();
  449. }
  450. //........................................................................
  451. }