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