Block_Frame_Decorator

Decorates frames for block layout.

Defined (1)

The class is defined in the following location(s).

/lib/dompdf/include/block_frame_decorator.cls.php  
  1. class Block_Frame_Decorator extends Frame_Decorator { 
  2. /** 
  3. * Current line index 
  4. * @var int 
  5. */ 
  6. protected $_cl; 
  7.  
  8. /** 
  9. * The block's line boxes 
  10. *  
  11. * @var Line_Box[] 
  12. */ 
  13. protected $_line_boxes; 
  14.  
  15. function __construct(Frame $frame, DOMPDF $dompdf) { 
  16. parent::__construct($frame, $dompdf); 
  17.  
  18. $this->_line_boxes = array(new Line_Box($this)); 
  19. $this->_cl = 0; 
  20.  
  21. function reset() { 
  22. parent::reset(); 
  23.  
  24. $this->_line_boxes = array(new Line_Box($this)); 
  25. $this->_cl = 0; 
  26.  
  27. /** 
  28. * @return Line_Box 
  29. */ 
  30. function get_current_line_box() { 
  31. return $this->_line_boxes[$this->_cl]; 
  32.  
  33. /** 
  34. * @return integer 
  35. */ 
  36. function get_current_line_number() { 
  37. return $this->_cl; 
  38.  
  39. /** 
  40. * @return Line_Box[] 
  41. */ 
  42. function get_line_boxes() {  
  43. return $this->_line_boxes;  
  44.  
  45. /** 
  46. * @param integer $i 
  47. */ 
  48. function clear_line($i) { 
  49. if ( isset($this->_line_boxes[$i]) ) { 
  50. unset($this->_line_boxes[$i]); 
  51.  
  52. /** 
  53. * @param Frame $frame 
  54. */ 
  55. function add_frame_to_line(Frame $frame) { 
  56. if ( !$frame->is_in_flow() ) { 
  57. return; 
  58.  
  59. $style = $frame->get_style(); 
  60.  
  61. $frame->set_containing_line($this->_line_boxes[$this->_cl]); 
  62.  
  63. /** 
  64. // Adds a new line after a block, only if certain conditions are met 
  65. if ((($frame instanceof Inline_Frame_Decorator && $frame->get_node()->nodeName !== "br") ||  
  66. $frame instanceof Text_Frame_Decorator && trim($frame->get_text())) &&  
  67. ($frame->get_prev_sibling() && $frame->get_prev_sibling()->get_style()->display === "block" &&  
  68. $this->_line_boxes[$this->_cl]->w > 0 )) { 
  69.  
  70. $this->maximize_line_height( $style->length_in_pt($style->line_height), $frame ); 
  71. $this->add_line(); 
  72.  
  73. // Add each child of the inline frame to the line individually 
  74. foreach ($frame->get_children() as $child) 
  75. $this->add_frame_to_line( $child );  
  76. else*/ 
  77.  
  78. // Handle inline frames (which are effectively wrappers) 
  79. if ( $frame instanceof Inline_Frame_Decorator ) { 
  80.  
  81. // Handle line breaks 
  82. if ( $frame->get_node()->nodeName === "br" ) { 
  83. $this->maximize_line_height( $style->length_in_pt($style->line_height), $frame ); 
  84. $this->add_line(true); 
  85.  
  86. return; 
  87.  
  88. // Trim leading text if this is an empty line. Kinda a hack to put it here,  
  89. // but what can you do... 
  90. if ( $this->get_current_line_box()->w == 0 && 
  91. $frame->is_text_node() && 
  92. !$frame->is_pre() ) { 
  93.  
  94. $frame->set_text( ltrim($frame->get_text()) ); 
  95. $frame->recalculate_width(); 
  96.  
  97. $w = $frame->get_margin_width(); 
  98.  
  99. if ( $w == 0 ) { 
  100. return; 
  101.  
  102. // Debugging code: 
  103. /** 
  104. pre_r("\n<h3>Adding frame to line:</h3>"); 
  105.   
  106. // pre_r("Me: " . $this->get_node()->nodeName . " (" . spl_object_hash($this->get_node()) . ")"); 
  107. // pre_r("Node: " . $frame->get_node()->nodeName . " (" . spl_object_hash($frame->get_node()) . ")"); 
  108. if ( $frame->is_text_node() ) 
  109. pre_r('"'.$frame->get_node()->nodeValue.'"'); 
  110.   
  111. pre_r("Line width: " . $this->_line_boxes[$this->_cl]->w); 
  112. pre_r("Frame: " . get_class($frame)); 
  113. pre_r("Frame width: " . $w); 
  114. pre_r("Frame height: " . $frame->get_margin_height()); 
  115. pre_r("Containing block width: " . $this->get_containing_block("w")); 
  116. */ 
  117. // End debugging 
  118.  
  119. $line = $this->_line_boxes[$this->_cl]; 
  120. if ( $line->left + $line->w + $line->right + $w > $this->get_containing_block("w")) { 
  121. $this->add_line(); 
  122.  
  123. $frame->position(); 
  124.  
  125. $current_line = $this->_line_boxes[$this->_cl]; 
  126. $current_line->add_frame($frame); 
  127.  
  128. if ( $frame->is_text_node() ) { 
  129. $current_line->wc += count(preg_split("/\s+/", trim($frame->get_text()))); 
  130.  
  131. $this->increase_line_width($w); 
  132.  
  133. $this->maximize_line_height($frame->get_margin_height(), $frame); 
  134.  
  135. function remove_frames_from_line(Frame $frame) { 
  136. // Search backwards through the lines for $frame 
  137. $i = $this->_cl; 
  138. $j = null; 
  139.  
  140. while ($i >= 0) { 
  141. if ( ($j = in_array($frame, $this->_line_boxes[$i]->get_frames(), true)) !== false ) { 
  142. break; 
  143.  
  144. $i--; 
  145.  
  146. if ( $j === false ) { 
  147. return; 
  148.  
  149. // Remove $frame and all frames that follow 
  150. while ($j < count($this->_line_boxes[$i]->get_frames())) { 
  151. $frames = $this->_line_boxes[$i]->get_frames(); 
  152. $f = $frames[$j]; 
  153. $frames[$j] = null; 
  154. unset($frames[$j]); 
  155. $j++; 
  156. $this->_line_boxes[$i]->w -= $f->get_margin_width(); 
  157.  
  158. // Recalculate the height of the line 
  159. $h = 0; 
  160. foreach ($this->_line_boxes[$i]->get_frames() as $f) { 
  161. $h = max( $h, $f->get_margin_height() ); 
  162.  
  163. $this->_line_boxes[$i]->h = $h; 
  164.  
  165. // Remove all lines that follow 
  166. while ($this->_cl > $i) { 
  167. $this->_line_boxes[ $this->_cl ] = null; 
  168. unset($this->_line_boxes[ $this->_cl ]); 
  169. $this->_cl--; 
  170.  
  171. function increase_line_width($w) { 
  172. $this->_line_boxes[ $this->_cl ]->w += $w; 
  173.  
  174. function maximize_line_height($val, Frame $frame) { 
  175. if ( $val > $this->_line_boxes[ $this->_cl ]->h ) { 
  176. $this->_line_boxes[ $this->_cl ]->tallest_frame = $frame; 
  177. $this->_line_boxes[ $this->_cl ]->h = $val; 
  178.  
  179. function add_line($br = false) { 
  180.  
  181. // if ( $this->_line_boxes[$this->_cl]["h"] == 0 ) //count($this->_line_boxes[$i]["frames"]) == 0 || 
  182. // return; 
  183.  
  184. $this->_line_boxes[$this->_cl]->br = $br; 
  185. $y = $this->_line_boxes[$this->_cl]->y + $this->_line_boxes[$this->_cl]->h; 
  186.  
  187. $new_line = new Line_Box($this, $y); 
  188.  
  189. $this->_line_boxes[ ++$this->_cl ] = $new_line; 
  190.  
  191. //........................................................................