Fix #8070 - merging in svgtopdf stuff
[pear] / XML / SvgToPdf / 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_SvgToPDF_G     extends XML_SvgToPDF_Base
10
11     var $boundingbox = false; // for repeats...
12     var $settings = array();  // cols/rows..
13     var $id;
14     var $dynamic;
15     var $cols;
16     var $rows;
17     
18     function fromXmlNode($node)
19     {
20        // print_r("G:fromXmlNode");
21         parent::fromXmlNode($node);
22  
23         
24         if (empty($this->dynamic)) {
25             return;
26         }
27         
28         $setting_cols = !empty($this->cols) ? $this->cols : '';
29         $setting_rows = !empty($this->rows) ? $this->rows : '';
30         $setting_dynamic = !empty($this->dynamic) ? $this->dynamic : '';
31         
32         $settings = array(
33             'rows' => $setting_rows,
34             'cols' => $setting_cols,
35             'dynamic' => $setting_dynamic
36         );
37         
38         
39          //look for the bounding box..
40         $boundingbox = false;
41 //echo "<PRE>";print_r($this->children);exit;
42         foreach(array_keys($this->children) as $k) {
43             if (!is_a($this->children[$k], 'XML_SvgToPDF_Rect')) {
44                continue;
45             }
46             if (empty($this->children[$k]->nonprintable) || ($this->children[$k]->nonprintable != 'true')) {
47                 continue;
48             }
49  //           echo "SETTING BOUNDING BOX"; exit; 
50             $boundingbox = clone($this->children[$k]);
51             // box will be rendered..
52             $this->children[$k]->style['fill'] = 'none';
53                // unset($this->children[$k]);
54           
55         }
56         if (!$boundingbox) {
57             return;
58         }
59         //echo "<PRE>";print_r($boundingbox ); exit;
60
61         if(is_string($boundingbox)) {
62             $bits = explode(',', $boundingbox);
63             $boundingbox = (object) array('x'=> $boundingbox[0], 'y'=> $boundingbox[1]);
64         }
65         $this->boundingbox =  $boundingbox ;
66         $this->settings = $settings;
67         
68         // change the X/Y values of all the child elements..
69          
70         //var_dump($boundingbox);
71         $this->shiftChildren(-1* $this->boundingbox->x,-1 * $this->boundingbox->y);
72         //$this->shiftChildren($this->boundingbox->x,$this->boundingbox->y);
73       
74     }
75
76     // not sure why this is done twice?
77
78     function fromNode($node) {
79         parent::fromNode($node);
80         
81     
82 //----------- applyDynamic...        
83         
84           // look for 
85         if (empty($this->children)) {
86             return;
87         }
88         
89         if (empty($this->dynamic)) {
90             return;
91         }
92         $settings = array(
93             'rows' => $this->rows,
94             'cols' => $this->cols,
95             'dynamic' => $this->dynamic
96         );
97          
98         
99         
100          //look for the bounding box..
101         $boundingbox = false;
102         foreach(array_keys($this->children) as $k) {
103             if (!is_a($this->children[$k], 'XML_SvgToPDF_Rect')) {
104                continue;
105             }
106             if (@$this->children[$k]->nonprintable == 'true') {
107                 $boundingbox = clone($this->children[$k]);
108                 $this->children[$k]->style['fill'] = 'none';
109                // unset($this->children[$k]);
110             }
111         }
112         if (!$boundingbox) {
113             return;
114         }
115         //echo "<PRE>";print_r($boundingbox );
116         
117         $this->boundingbox =  $boundingbox ;
118         $this->settings = $settings;
119         $this->shiftChildren($this->boundingbox->x,$this->boundingbox->y);
120     }
121     
122     function shift($x,$y) {
123         
124         if ($this->boundingbox) {
125             return;
126         }
127         
128         $this->shiftChildren($x,$y);
129     
130     }
131
132
133
134     function writePDF($pdf,$data) {
135         // g group = does it have a 
136         // look for 
137         if (empty($this->children)) {
138             return;
139         }
140          
141         // not dynamic.. -> just dump..
142         if (empty($this->settings)) {
143             return $this->childrenWritePDF($pdf,$data);
144         }
145         
146         $use = false;
147         if (substr($this->settings['dynamic'],-2,2) == '()') {
148         
149             $use = $data->{substr($this->settings['dynamic'],0,-2)}();
150             
151         } else {
152             $use = empty($data[$this->settings['dynamic']]) ? ''  : $data[$this->settings['dynamic']];
153         }
154         
155             
156         if (empty($use)) {
157             return;
158         }
159         // if use is a value  - make it an array with a single element, so that the bounding box
160         // additions apply..
161         if (!is_array($use)) {
162             $use = array($use);
163         }
164         
165         // echo "<PRE>";print_r($boundingbox );
166         
167         
168         $this->x = $x = $this->boundingbox->x;
169         $this->y =$y = $this->boundingbox->y;
170         $w = $this->boundingbox->width;
171         $h = $this->boundingbox->height; 
172         
173         
174         //echo '<PRE>';print_r($this);exit;
175         // shift... ** this does not handle groups!!!
176       
177         //print_R($use);
178         $keys = array_keys($use);
179         $kpos = 0;
180         $kmax = count($keys);
181         //XML_SvgToPDF::debug(array($x,$y,$w,$h));
182         //XML_SvgToPDF::debug($keys);
183         XML_SvgToPDF::debug($this->settings);
184         
185         $yy = $y;
186         
187         for($r=0;$r<$this->settings['rows'];$r++) {
188             $record = $use[$keys[$kpos]];
189             
190             
191             for($c=0;$c<$this->settings['cols'];$c++) {
192                 
193                 $record = $use[$keys[$kpos]];
194                 
195                 $xx = $x + ($c*$w);
196                 XML_SvgToPDF::debug(array($xx,$yy));
197                 foreach(array_keys($this->children) as $k) {
198                     if (!$this->children[$k]) {
199                         continue;
200                     }
201                     
202                    //  if (is_object($use[$keys[$kpos]]) && method_exists($use[$keys[$kpos]], 'loadSvg')) {
203                    //     // set the defaults, as we cant do it in the thing now..
204                    //     $use[$keys[$kpos]]->loadSvg();
205                    // }
206                     
207                     
208                     $this->children[$k]->xx = $xx;
209                     $this->children[$k]->yy = $yy;
210                     $this->children[$k]->maxWidth = $w - 20; 
211                     $this->children[$k]->writePDF($pdf,$use[$keys[$kpos]]);
212                 }
213                 $kpos++;
214                 if ($kpos >= $kmax) {
215                     break 2;
216                 }
217             }
218             $yy += !empty($record->userows) ? ($record->userows) * $h : $h;
219
220             
221         }
222         
223         
224         
225     }
226
227
228
229
230 }