fix svgtopdf
[pear] / XML / SvgToPdfAlt / G.php
1 <?php
2
3 /* code that deals with svg groups
4 it does alot of smart stuff to handle 'dynamic' blocks
5
6 */
7
8
9 class XML_SvgToPDFAlt_G     extends XML_SvgToPDFAlt_Base { 
10
11     function fromNode($node) {
12         parent::fromNode($node);
13           // look for 
14         if (!@$this->children) {
15             return;
16         }
17         $settings = array(
18             'rows' => 1,
19             'cols' => 1
20         );
21         
22         $isDynamic = false;
23         foreach(array_keys($this->children) as $k) {
24             if (!is_a($this->children[$k], 'XML_SvgToPDFAlt_Text')) {
25                 continue;
26             }
27             
28             if (!isset($this->children[$k]->children[0]->content) || 
29                     (strpos($this->children[$k]->children[0]->content,'=') === false)) {
30                 
31                 continue;
32             }
33               
34             foreach($this->children[$k]->children as $o) {
35                 list($l,$r) = explode('=',$o->content);
36                 $settings[$l] = $r;
37             }
38              
39             
40             unset($this->children[$k]);
41             $isDynamic = true;
42             break;
43         }
44       
45         if (!$isDynamic) {
46             return;
47         }
48         
49         
50         
51          //look for the bounding box..
52         $boundingbox = false;
53         foreach(array_keys($this->children) as $k) {
54             if (!is_a($this->children[$k], 'XML_SvgToPDFAlt_Rect')) {
55                continue;
56             }
57             if (@$this->children[$k]->nonprintable == 'true') {
58                 $boundingbox = clone($this->children[$k]);
59                 $this->children[$k]->style['fill'] = 'none';
60                // unset($this->children[$k]);
61             }
62         }
63         if (!$boundingbox) {
64             return;
65         }
66         
67         $this->boundingbox = $boundingbox;
68         $this->settings = $settings;
69         $this->shiftChildren($this->boundingbox->x,$this->boundingbox->y);
70     }
71     
72     function shift($x,$y) {
73         
74         if ($this->boundingbox) {
75             return;
76         }
77         
78          $this->shiftChildren($x,$y);
79     
80     }
81
82
83
84     function writePDF($pdf,$data) {
85         // g group = does it have a 
86         // look for 
87         if (!@$this->children) {
88             return;
89         }
90          
91         
92         if (empty($this->settings)) {
93             return $this->childrenWritePDF($pdf,$data);
94         }
95         $use = false;
96         if (substr($this->settings['dynamic'],-2,2) == '()') {
97         
98             $use = $data->{substr($this->settings['dynamic'],0,-2)}();
99             
100         } else {
101             $use = @$data[$this->settings['dynamic']];
102         }
103         
104             
105         if ($use === false) {
106             return;
107         }
108         
109         if (!is_array($use) || !$use) {
110             return $this->childrenWritePDF($pdf,$data);
111         }
112         
113         
114         
115         
116         $this->x = $x = $this->boundingbox->x;
117         $this->y =$y = $this->boundingbox->y;
118         $w = $this->boundingbox->width;
119         $h = $this->boundingbox->height; 
120         
121         
122         //echo '<PRE>';print_r($this);exit;
123         // shift... ** this does not handle groups!!!
124       
125         //print_R($use);
126         $keys = array_keys($use);
127         $kpos = 0;
128         $kmax = count($keys);
129         //XML_SvgToPDF::debug(array($x,$y,$w,$h));
130         //XML_SvgToPDF::debug($keys);
131         XML_SvgToPDFAlt::debug($this->settings);
132         for($r=0;$r<$this->settings['rows'];$r++) {
133             $yy = $y + ($r*$h);
134             for($c=0;$c<$this->settings['cols'];$c++) {
135                 $xx = $x + ($c*$w);
136                 XML_SvgToPDFAlt::debug(array($xx,$yy));
137                 foreach(array_keys($this->children) as $k) {
138                     if (!$this->children[$k]) {
139                         continue;
140                     }
141                     $this->children[$k]->xx = $xx;
142                     $this->children[$k]->yy = $yy;
143                     $this->children[$k]->maxWidth = $w - 20; 
144                     $this->children[$k]->writePDF($pdf,$use[$keys[$kpos]]);
145                 }
146                 $kpos++;
147                 if ($kpos >= $kmax) {
148                     break 2;
149                 }
150             }
151         }
152         
153         
154         
155     }
156
157
158
159
160 }