Frame_Factory

Contains frame decorating logic.

Defined (1)

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

/lib/dompdf/include/frame_factory.cls.php  
  1. class Frame_Factory { 
  2.  
  3. /** 
  4. * Decorate the root Frame 
  5. *  
  6. * @param $root Frame The frame to decorate 
  7. * @param $dompdf DOMPDF The dompdf instance 
  8. * @return Page_Frame_Decorator 
  9. */ 
  10. static function decorate_root(Frame $root, DOMPDF $dompdf) { 
  11. $frame = new Page_Frame_Decorator($root, $dompdf); 
  12. $frame->set_reflower( new Page_Frame_Reflower($frame) ); 
  13. $root->set_decorator($frame); 
  14. return $frame; 
  15.  
  16. /** 
  17. * Decorate a Frame 
  18. * @param Frame $frame The frame to decorate 
  19. * @param DOMPDF $dompdf The dompdf instance 
  20. * @param Frame $root The frame to decorate 
  21. * @throws DOMPDF_Exception 
  22. * @return Frame_Decorator 
  23. * FIXME: this is admittedly a little smelly... 
  24. */ 
  25. static function decorate_frame(Frame $frame, DOMPDF $dompdf, Frame $root = null) { 
  26. if ( is_null($dompdf) ) { 
  27. throw new DOMPDF_Exception("The DOMPDF argument is required"); 
  28.  
  29. $style = $frame->get_style(); 
  30.  
  31. // Floating (and more generally out-of-flow) elements are blocks  
  32. // http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/ 
  33. if ( !$frame->is_in_flow() && in_array($style->display, Style::$INLINE_TYPES)) { 
  34. $style->display = "block"; 
  35.  
  36. $display = $style->display; 
  37.  
  38. switch ($display) { 
  39.  
  40. case "block": 
  41. $positioner = "Block";  
  42. $decorator = "Block"; 
  43. $reflower = "Block"; 
  44. break; 
  45.  
  46. case "inline-block": 
  47. $positioner = "Inline"; 
  48. $decorator = "Block"; 
  49. $reflower = "Block"; 
  50. break; 
  51.  
  52. case "inline": 
  53. $positioner = "Inline"; 
  54. if ( $frame->is_text_node() ) { 
  55. $decorator = "Text"; 
  56. $reflower = "Text"; 
  57. }  
  58. else { 
  59. $enable_css_float = $dompdf->get_option("enable_css_float"); 
  60. if ( $enable_css_float && $style->float !== "none" ) { 
  61. $decorator = "Block"; 
  62. $reflower = "Block"; 
  63. else { 
  64. $decorator = "Inline"; 
  65. $reflower = "Inline"; 
  66. break;  
  67.  
  68. case "table": 
  69. $positioner = "Block"; 
  70. $decorator = "Table"; 
  71. $reflower = "Table"; 
  72. break; 
  73.  
  74. case "inline-table": 
  75. $positioner = "Inline"; 
  76. $decorator = "Table"; 
  77. $reflower = "Table"; 
  78. break; 
  79.  
  80. case "table-row-group": 
  81. case "table-header-group": 
  82. case "table-footer-group": 
  83. $positioner = "Null"; 
  84. $decorator = "Table_Row_Group"; 
  85. $reflower = "Table_Row_Group"; 
  86. break; 
  87.  
  88. case "table-row": 
  89. $positioner = "Null"; 
  90. $decorator = "Table_Row"; 
  91. $reflower = "Table_Row"; 
  92. break; 
  93.  
  94. case "table-cell": 
  95. $positioner = "Table_Cell"; 
  96. $decorator = "Table_Cell"; 
  97. $reflower = "Table_Cell"; 
  98. break; 
  99.  
  100. case "list-item": 
  101. $positioner = "Block"; 
  102. $decorator = "Block"; 
  103. $reflower = "Block"; 
  104. break; 
  105.  
  106. case "-dompdf-list-bullet": 
  107. if ( $style->list_style_position === "inside" ) { 
  108. $positioner = "Inline"; 
  109. else {  
  110. $positioner = "List_Bullet"; 
  111.  
  112. if ( $style->list_style_image !== "none" ) { 
  113. $decorator = "List_Bullet_Image"; 
  114. else { 
  115. $decorator = "List_Bullet"; 
  116.  
  117. $reflower = "List_Bullet"; 
  118. break; 
  119.  
  120. case "-dompdf-image": 
  121. $positioner = "Inline"; 
  122. $decorator = "Image"; 
  123. $reflower = "Image"; 
  124. break; 
  125.  
  126. case "-dompdf-br": 
  127. $positioner = "Inline"; 
  128. $decorator = "Inline"; 
  129. $reflower = "Inline"; 
  130. break; 
  131.  
  132. default: 
  133. // FIXME: should throw some sort of warning or something? 
  134. case "none": 
  135. if ( $style->_dompdf_keep !== "yes" ) { 
  136. // Remove the node and the frame 
  137. $frame->get_parent()->remove_child($frame); 
  138. return; 
  139.  
  140. $positioner = "Null"; 
  141. $decorator = "Null"; 
  142. $reflower = "Null"; 
  143. break; 
  144.  
  145. // Handle CSS position 
  146. $position = $style->position; 
  147.  
  148. if ( $position === "absolute" ) { 
  149. $positioner = "Absolute"; 
  150. else if ( $position === "fixed" ) { 
  151. $positioner = "Fixed"; 
  152.  
  153. $node = $frame->get_node(); 
  154.  
  155. // Handle nodeName 
  156. if ( $node->nodeName === "img" ) { 
  157. $style->display = "-dompdf-image"; 
  158. $decorator = "Image"; 
  159. $reflower = "Image"; 
  160.  
  161. $positioner .= "_Positioner"; 
  162. $decorator .= "_Frame_Decorator"; 
  163. $reflower .= "_Frame_Reflower"; 
  164.  
  165. $deco = new $decorator($frame, $dompdf); 
  166.  
  167. $deco->set_positioner( new $positioner($deco) ); 
  168. $deco->set_reflower( new $reflower($deco) ); 
  169.  
  170. if ( $root ) { 
  171. $deco->set_root($root); 
  172.  
  173. if ( $display === "list-item" ) { 
  174. // Insert a list-bullet frame 
  175. $xml = $dompdf->get_dom(); 
  176. $bullet_node = $xml->createElement("bullet"); // arbitrary choice 
  177. $b_f = new Frame($bullet_node); 
  178.  
  179. $node = $frame->get_node(); 
  180. $parent_node = $node->parentNode; 
  181.  
  182. if ( $parent_node ) { 
  183. if ( !$parent_node->hasAttribute("dompdf-children-count") ) { 
  184. $xpath = new DOMXPath($xml); 
  185. $count = $xpath->query("li", $parent_node)->length; 
  186. $parent_node->setAttribute("dompdf-children-count", $count); 
  187.  
  188. if ( is_numeric($node->getAttribute("value")) ) { 
  189. $index = intval($node->getAttribute("value")); 
  190. else { 
  191. if ( !$parent_node->hasAttribute("dompdf-counter") ) { 
  192. $index = ($parent_node->hasAttribute("start") ? $parent_node->getAttribute("start") : 1); 
  193. else { 
  194. $index = $parent_node->getAttribute("dompdf-counter")+1; 
  195.  
  196. $parent_node->setAttribute("dompdf-counter", $index); 
  197. $bullet_node->setAttribute("dompdf-counter", $index); 
  198.  
  199. $new_style = $dompdf->get_css()->create_style(); 
  200. $new_style->display = "-dompdf-list-bullet"; 
  201. $new_style->inherit($style); 
  202. $b_f->set_style($new_style); 
  203.  
  204. $deco->prepend_child( Frame_Factory::decorate_frame($b_f, $dompdf, $root) ); 
  205.  
  206. return $deco;