Fix #8052 - fixing pdf
[pear] / XML / SvgToPdfAlt / Base.php
1 <?php
2
3 /*
4 The base node includes:
5     fromNode - convert the XML_Tree node into object with vars
6         (and parse styles)
7     
8     // iternate tree with writePDF
9     writePDF($pdf,$data) 
10     childrenWritePDF($pdf,$data)  
11
12     // shift coordinates of group dynamic elements 
13     // so the x is relative to the block corner.
14     shiftChildren($x,$y) {
15     shift($x,$y) 
16     
17     // find a dynamic block and calculate how much data it can hold.
18     // so you know how many pages are needed.
19     calcPerPage()
20 */
21
22 #[AllowDynamicProperties] 
23 class XML_SvgToPDFAlt_Base { 
24
25     var $content;
26     
27     function fromNode($node) {
28         
29         if ($node->attributes) {
30             foreach($node->attributes as $k=>$v) {
31                 if (strpos($k,':')) {
32                     $kk = explode(':',$k);
33                     $k = $kk[1];
34                 }
35                 $this->$k = $v;
36                 $this->{'_'.$k} = $v;
37                 if (preg_match('/[0-9]+mm$/',$v)) {
38                     $v = str_replace('mm','',$v);
39                     $v = $v * 3.543307;
40                 
41                     $this->$k = $v;
42                     $this->{'_'.$k} = $v;// save the original..
43                     continue;
44                 }
45                 
46             }
47         }
48         
49         if (isset($this->style)) {
50             $s = explode(';',$this->style);
51             foreach($s as $ss) {
52                 if (!strlen(trim($ss))) {
53                     continue;
54                 }
55                 if (strpos($ss,':') === false) {
56                     $style[$ss] = true;
57                     continue;
58                 }
59                 $sss = explode(':',$ss);
60                 if (preg_match('/[0-9]+pt$/',$sss[1])) {
61                     $sss[1] =  str_replace('pt','',$sss[1]);
62                 }
63                 $style[$sss[0]] = $sss[1];
64             }
65             $this->style = $style;
66         }
67                 
68         
69         if ($node->content) {
70             $this->content = trim($node->content);
71         }
72         if ($node->children) {
73             $this->children = $node->children;
74         }
75         $this->transform();
76     }
77
78
79     function transform() {
80         if (!@$this->transform) {
81             return;
82         }
83         // deal with transformation..
84         $tr = $this->transform;
85         if (preg_match('/scale\(([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
86             $xscale = (float)$args[1];
87             $yscale = (float)$args[2];
88             $method = 'scale';
89             } else if (preg_match('/matrix\(([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
90             array_shift($args);
91             require_once 'Math/Matrix.php';
92             $matrix =  new Math_Matrix( array(
93                     array((float)$args[0],(float)$args[2],(float)$args[4]),
94                     array((float)$args[1],(float)$args[3],(float)$args[5]),
95                     array(       0,       0,    1))
96             );
97             $method = 'matrix';
98         } else if (preg_match('/translate\(([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
99             $x = (float)$args[1];
100             $y =(float) $args[2];
101             $method = 'translate';
102         } else {
103           echo "<PRE>no match?";print_r($this); exit;
104             return;
105         }
106         //
107         switch ($method) {
108             case 'scale':
109                 $this->x *=  $xscale;
110                 $this->y *=  $yscale;
111                 if (!@$this->children) {
112                     return;
113                 }
114                 foreach(array_keys($this->children) as $i) {
115                     if (!isset($this->children[$i]->x)) {
116                         continue;
117                         // echo "<PRE>";print_r($this);exit;
118                     }
119                     $this->children[$i]->x *=  $xscale;
120                     $this->children[$i]->y *=  $yscale;
121                 }
122                 break;
123                     case 'matrix':
124                 $v = new Math_Vector(array($this->x,$this->y,0));
125                 
126                 $r = $matrix->vectorMultiply($v);
127                 $r = $r->getData();
128                 $this->x = $r[0];
129                 $this->y = $r[1];
130                 //echo "<PRE>";var_dump(        $r);exit;
131                 if (!@$this->children) {
132                     return;
133                 }
134                 foreach(array_keys($this->children) as $i) {
135                     if (!isset($this->children[$i]->x)) {
136                         continue;
137                         // echo "<PRE>";print_r($this);exit;
138                     }
139                     $v = new Math_Vector(array($this->children[$i]->x,$this->children[$i]->y,0));
140                     $r =  $matrix->vectorMultiply($v);
141                     $r = $r->getData();
142                     $this->children[$i]->x = $r[0];
143                     $this->children[$i]->y = $r[1];
144      
145                 }
146                 break; 
147             case 'translate':
148                 //echo 'translate' . $x .',' . $y . "\n";
149                 if (isset($this->x) &&  isset($this->y)) {
150                          
151                    $this->x +=  $x;
152                    $this->y +=  $y;
153                 }
154                 if (!@$this->children) {
155                     return;
156                 }
157                 foreach(array_keys($this->children) as $i) {
158                     if (!isset($this->children[$i]->x) || !isset($this->children[$i]->y)) {
159                         continue;
160                         // echo "<PRE>";print_r($this);exit;
161                     }
162                     $this->children[$i]->x +=  $x;
163                     $this->children[$i]->y +=  $y;
164                 }
165                 break; 
166                  
167           }
168      }
169
170
171
172
173     
174     function writePDF($pdf,$data) {
175         $this->childrenWritePDF($pdf,$data);
176     }
177     
178     function childrenWritePDF(&$pdf,&$data) {
179         if (!@$this->children) {
180             return;
181         }
182         foreach(array_keys($this->children) as $k) {
183             if (!$this->children[$k]) {
184                 continue;
185             }
186             if (!method_exists($this->children[$k],'writePDF')) {
187                 echo "OOPS unknown object? <PRE>" ; print_r($this->children[$k]); exit;
188             }
189             $this->children[$k]->writePDF($pdf,$data);
190         }
191     }
192     
193     function shiftChildren($x,$y) {
194         if (!@$this->children) {
195             return;
196         }
197         foreach(array_keys($this->children) as $k) {
198             if (!$this->children[$k]) {
199                 continue;
200             }
201             $this->children[$k]->shift($x,$y);
202         }
203     }
204     
205     function shift($x,$y) {
206         //XML_SvgToPDF::debug('shift');
207         //XML_SvgToPDF::debug(array($x,$y));
208         //XML_SvgToPDF::debug($this);
209         if (isset($this->x)) {
210             
211             $this->x -= $x;
212         }
213         if (isset($this->y)) {
214             $this->y -= $y;
215         }
216         //XML_SvgToPDF::debug($this);
217         $this->shiftChildren($x,$y);
218     }
219     function calcPerPage() {
220         $ret = array();
221         foreach($this->children as $n) {
222             if (!$n) {
223                 continue;
224             }
225             if (!is_a($n, 'XML_SvgToPDF_G')) {
226                 continue;
227             }
228             if (!isset($n->settings)) {
229                 continue;
230             }
231             
232             $rows  = isset($n->settings['rows']) ? $n->settings['rows'] : 1;
233             $cols  = isset($n->settings['cols']) ? $n->settings['cols'] : 1;
234             
235             
236             $ret[$n->settings['dynamic']] =  $rows * $cols;
237             
238             
239             
240         }
241         return $ret;
242          
243     
244     
245     
246     
247     }
248     
249     function toColor($color) {
250         if (!$color || ($color == 'none')) {
251             return false;
252         }
253         
254         if ($color == 'black') {
255             $color = '#000000';
256         }
257         
258         return array(
259             hexdec(substr($color,1,2)),
260             hexdec(substr($color,3,2)),
261             hexdec(substr($color,5,2)));
262         
263     }
264     
265     
266 }