LineBox.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Fabien Ménager <fabien.menager@gmail.com>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. namespace Dompdf;
  9. use Dompdf\FrameDecorator\Block;
  10. use Dompdf\FrameDecorator\Page;
  11. /**
  12. * The line box class
  13. *
  14. * This class represents a line box
  15. * http://www.w3.org/TR/CSS2/visuren.html#line-box
  16. *
  17. * @package dompdf
  18. */
  19. class LineBox
  20. {
  21. /**
  22. * @var Block
  23. */
  24. protected $_block_frame;
  25. /**
  26. * @var Frame[]
  27. */
  28. protected $_frames = [];
  29. /**
  30. * @var integer
  31. */
  32. public $wc = 0;
  33. /**
  34. * @var float
  35. */
  36. public $y = null;
  37. /**
  38. * @var float
  39. */
  40. public $w = 0.0;
  41. /**
  42. * @var float
  43. */
  44. public $h = 0.0;
  45. /**
  46. * @var float
  47. */
  48. public $left = 0.0;
  49. /**
  50. * @var float
  51. */
  52. public $right = 0.0;
  53. /**
  54. * @var Frame
  55. */
  56. public $tallest_frame = null;
  57. /**
  58. * @var bool[]
  59. */
  60. public $floating_blocks = [];
  61. /**
  62. * @var bool
  63. */
  64. public $br = false;
  65. /**
  66. * Class constructor
  67. *
  68. * @param Block $frame the Block containing this line
  69. * @param int $y
  70. */
  71. public function __construct(Block $frame, $y = 0)
  72. {
  73. $this->_block_frame = $frame;
  74. $this->_frames = [];
  75. $this->y = $y;
  76. $this->get_float_offsets();
  77. }
  78. /**
  79. * Returns the floating elements inside the first floating parent
  80. *
  81. * @param Page $root
  82. *
  83. * @return Frame[]
  84. */
  85. public function get_floats_inside(Page $root)
  86. {
  87. $floating_frames = $root->get_floating_frames();
  88. if (count($floating_frames) == 0) {
  89. return $floating_frames;
  90. }
  91. // Find nearest floating element
  92. $p = $this->_block_frame;
  93. while ($p->get_style()->float === "none") {
  94. $parent = $p->get_parent();
  95. if (!$parent) {
  96. break;
  97. }
  98. $p = $parent;
  99. }
  100. if ($p == $root) {
  101. return $floating_frames;
  102. }
  103. $parent = $p;
  104. $childs = [];
  105. foreach ($floating_frames as $_floating) {
  106. $p = $_floating->get_parent();
  107. while (($p = $p->get_parent()) && $p !== $parent);
  108. if ($p) {
  109. $childs[] = $p;
  110. }
  111. }
  112. return $childs;
  113. }
  114. /**
  115. *
  116. */
  117. public function get_float_offsets()
  118. {
  119. static $anti_infinite_loop = 10000; // FIXME smelly hack
  120. $reflower = $this->_block_frame->get_reflower();
  121. if (!$reflower) {
  122. return;
  123. }
  124. $cb_w = null;
  125. $block = $this->_block_frame;
  126. $root = $block->get_root();
  127. if (!$root) {
  128. return;
  129. }
  130. $style = $this->_block_frame->get_style();
  131. $floating_frames = $this->get_floats_inside($root);
  132. $inside_left_floating_width = 0;
  133. $inside_right_floating_width = 0;
  134. $outside_left_floating_width = 0;
  135. $outside_right_floating_width = 0;
  136. foreach ($floating_frames as $child_key => $floating_frame) {
  137. $floating_frame_parent = $floating_frame->get_parent();
  138. $id = $floating_frame->get_id();
  139. if (isset($this->floating_blocks[$id])) {
  140. continue;
  141. }
  142. $float = $floating_frame->get_style()->float;
  143. $floating_width = $floating_frame->get_margin_width();
  144. if (!$cb_w) {
  145. $cb_w = $floating_frame->get_containing_block("w");
  146. }
  147. $line_w = $this->get_width();
  148. if (!$floating_frame->_float_next_line && ($cb_w <= $line_w + $floating_width) && ($cb_w > $line_w)) {
  149. $floating_frame->_float_next_line = true;
  150. continue;
  151. }
  152. // If the child is still shifted by the floating element
  153. if ($anti_infinite_loop-- > 0 &&
  154. $floating_frame->get_position("y") + $floating_frame->get_margin_height() >= $this->y &&
  155. $block->get_position("x") + $block->get_margin_width() >= $floating_frame->get_position("x")
  156. ) {
  157. if ($float === "left") {
  158. if ($floating_frame_parent === $this->_block_frame) {
  159. $inside_left_floating_width += $floating_width;
  160. } else {
  161. $outside_left_floating_width += $floating_width;
  162. }
  163. } elseif ($float === "right") {
  164. if ($floating_frame_parent === $this->_block_frame) {
  165. $inside_right_floating_width += $floating_width;
  166. } else {
  167. $outside_right_floating_width += $floating_width;
  168. }
  169. }
  170. $this->floating_blocks[$id] = true;
  171. } // else, the floating element won't shift anymore
  172. else {
  173. $root->remove_floating_frame($child_key);
  174. }
  175. }
  176. $this->left += $inside_left_floating_width;
  177. if ($outside_left_floating_width > 0 && $outside_left_floating_width > ((float)$style->length_in_pt($style->margin_left) + (float)$style->length_in_pt($style->padding_left))) {
  178. $this->left += $outside_left_floating_width - (float)$style->length_in_pt($style->margin_left) - (float)$style->length_in_pt($style->padding_left);
  179. }
  180. $this->right += $inside_right_floating_width;
  181. if ($outside_right_floating_width > 0 && $outside_right_floating_width > ((float)$style->length_in_pt($style->margin_left) + (float)$style->length_in_pt($style->padding_right))) {
  182. $this->right += $outside_right_floating_width - (float)$style->length_in_pt($style->margin_right) - (float)$style->length_in_pt($style->padding_right);
  183. }
  184. }
  185. /**
  186. * @return float
  187. */
  188. public function get_width()
  189. {
  190. return $this->left + $this->w + $this->right;
  191. }
  192. /**
  193. * @return Block
  194. */
  195. public function get_block_frame()
  196. {
  197. return $this->_block_frame;
  198. }
  199. /**
  200. * @return Frame[]
  201. */
  202. function &get_frames()
  203. {
  204. return $this->_frames;
  205. }
  206. /**
  207. * @param Frame $frame
  208. */
  209. public function add_frame(Frame $frame)
  210. {
  211. $this->_frames[] = $frame;
  212. }
  213. /**
  214. * Recalculate LineBox width based on the contained frames total width.
  215. *
  216. * @return float
  217. */
  218. public function recalculate_width()
  219. {
  220. $width = 0;
  221. foreach ($this->get_frames() as $frame) {
  222. $width += $frame->calculate_auto_width();
  223. }
  224. return $this->w = $width;
  225. }
  226. /**
  227. * @return string
  228. */
  229. public function __toString()
  230. {
  231. $props = ["wc", "y", "w", "h", "left", "right", "br"];
  232. $s = "";
  233. foreach ($props as $prop) {
  234. $s .= "$prop: " . $this->$prop . "\n";
  235. }
  236. $s .= count($this->_frames) . " frames\n";
  237. return $s;
  238. }
  239. /*function __get($prop) {
  240. if (!isset($this->{"_$prop"})) return;
  241. return $this->{"_$prop"};
  242. }*/
  243. }
  244. /*
  245. class LineBoxList implements Iterator {
  246. private $_p = 0;
  247. private $_lines = array();
  248. }
  249. */