1<?php 2// $Header: /cvsroot/html2ps/tree.navigation.inc.php,v 1.13 2007/05/06 18:49:29 Konstantin Exp $ 3 4class TreeWalkerDepthFirst { 5 var $_callback; 6 7 function TreeWalkerDepthFirst($callback) { 8 $this->_callback = $callback; 9 } 10 11 function run(&$node) { 12 call_user_func($this->_callback, array('node' => &$node)); 13 $this->walk_element($node); 14 } 15 16 function walk_element(&$node) { 17 if (!isset($node->content)) { 18 return; 19 }; 20 21 for ($i = 0, $size = count($node->content); $i < $size; $i++) { 22 $child =& $node->content[$i]; 23 $this->run($child); 24 }; 25 } 26} 27 28function &traverse_dom_tree_pdf(&$root) { 29 switch ($root->node_type()) { 30 case XML_DOCUMENT_NODE: 31 $child =& $root->first_child(); 32 while($child) { 33 $body =& traverse_dom_tree_pdf($child); 34 if ($body) { 35 return $body; 36 } 37 $child =& $child->next_sibling(); 38 }; 39 40 $null = null; 41 return $null; 42 case XML_ELEMENT_NODE: 43 if (strtolower($root->tagname()) == "body") { 44 return $root; 45 } 46 47 $child =& $root->first_child(); 48 while ($child) { 49 $body =& traverse_dom_tree_pdf($child); 50 if ($body) { 51 return $body; 52 } 53 $child =& $child->next_sibling(); 54 }; 55 56 $null = null; 57 return $null; 58 default: 59 $null = null; 60 return $null; 61 } 62}; 63 64function dump_tree(&$box, $level) { 65 print(str_repeat(" ", $level)); 66 if (is_a($box, 'TextBox')) { 67 print(get_class($box).":".$box->uid.":".join('/', $box->words)."\n"); 68 } else { 69 print(get_class($box).":".$box->uid."\n"); 70 }; 71 72 if (isset($box->content)) { 73 for ($i=0; $i<count($box->content); $i++) { 74 dump_tree($box->content[$i], $level+1); 75 }; 76 }; 77}; 78 79?>