Fix #8052 - fixing pdf
[pear] / XML / SvgToPdfAlt / Base.php
diff --git a/XML/SvgToPdfAlt/Base.php b/XML/SvgToPdfAlt/Base.php
new file mode 100644 (file)
index 0000000..56bd61f
--- /dev/null
@@ -0,0 +1,266 @@
+<?php
+
+/*
+The base node includes:
+    fromNode - convert the XML_Tree node into object with vars
+        (and parse styles)
+    
+    // iternate tree with writePDF
+    writePDF($pdf,$data) 
+    childrenWritePDF($pdf,$data)  
+
+    // shift coordinates of group dynamic elements 
+    // so the x is relative to the block corner.
+    shiftChildren($x,$y) {
+    shift($x,$y) 
+    
+    // find a dynamic block and calculate how much data it can hold.
+    // so you know how many pages are needed.
+    calcPerPage()
+*/
+
+#[AllowDynamicProperties] 
+class XML_SvgToPDFAlt_Base { 
+
+    var $content;
+    
+    function fromNode($node) {
+        
+        if ($node->attributes) {
+            foreach($node->attributes as $k=>$v) {
+                if (strpos($k,':')) {
+                    $kk = explode(':',$k);
+                    $k = $kk[1];
+                }
+                $this->$k = $v;
+                $this->{'_'.$k} = $v;
+                if (preg_match('/[0-9]+mm$/',$v)) {
+                    $v = str_replace('mm','',$v);
+                    $v = $v * 3.543307;
+                
+                    $this->$k = $v;
+                    $this->{'_'.$k} = $v;// save the original..
+                    continue;
+                }
+                
+            }
+        }
+        
+        if (isset($this->style)) {
+            $s = explode(';',$this->style);
+            foreach($s as $ss) {
+                if (!strlen(trim($ss))) {
+                    continue;
+                }
+                if (strpos($ss,':') === false) {
+                    $style[$ss] = true;
+                    continue;
+                }
+                $sss = explode(':',$ss);
+                if (preg_match('/[0-9]+pt$/',$sss[1])) {
+                    $sss[1] =  str_replace('pt','',$sss[1]);
+                }
+                $style[$sss[0]] = $sss[1];
+            }
+            $this->style = $style;
+        }
+                
+        
+        if ($node->content) {
+            $this->content = trim($node->content);
+        }
+        if ($node->children) {
+            $this->children = $node->children;
+        }
+        $this->transform();
+    }
+
+
+    function transform() {
+        if (!@$this->transform) {
+            return;
+        }
+        // deal with transformation..
+        $tr = $this->transform;
+        if (preg_match('/scale\(([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
+            $xscale = (float)$args[1];
+            $yscale = (float)$args[2];
+            $method = 'scale';
+            } else if (preg_match('/matrix\(([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
+            array_shift($args);
+            require_once 'Math/Matrix.php';
+            $matrix =  new Math_Matrix( array(
+                    array((float)$args[0],(float)$args[2],(float)$args[4]),
+                    array((float)$args[1],(float)$args[3],(float)$args[5]),
+                    array(       0,       0,   1))
+            );
+            $method = 'matrix';
+        } else if (preg_match('/translate\(([0-9e.-]+),([0-9e.-]+)\)/',$tr,$args)) {
+            $x = (float)$args[1];
+            $y =(float) $args[2];
+            $method = 'translate';
+        } else {
+          echo "<PRE>no match?";print_r($this); exit;
+            return;
+        }
+        //
+        switch ($method) {
+            case 'scale':
+                $this->x *=  $xscale;
+                $this->y *=  $yscale;
+                if (!@$this->children) {
+                    return;
+                }
+                foreach(array_keys($this->children) as $i) {
+                    if (!isset($this->children[$i]->x)) {
+                        continue;
+                        // echo "<PRE>";print_r($this);exit;
+                    }
+                    $this->children[$i]->x *=  $xscale;
+                    $this->children[$i]->y *=  $yscale;
+                }
+                break;
+                    case 'matrix':
+                $v = new Math_Vector(array($this->x,$this->y,0));
+                
+                $r = $matrix->vectorMultiply($v);
+                $r = $r->getData();
+                $this->x = $r[0];
+                $this->y = $r[1];
+                //echo "<PRE>";var_dump(       $r);exit;
+                if (!@$this->children) {
+                    return;
+                }
+                foreach(array_keys($this->children) as $i) {
+                    if (!isset($this->children[$i]->x)) {
+                        continue;
+                        // echo "<PRE>";print_r($this);exit;
+                    }
+                    $v = new Math_Vector(array($this->children[$i]->x,$this->children[$i]->y,0));
+                    $r =  $matrix->vectorMultiply($v);
+                    $r = $r->getData();
+                    $this->children[$i]->x = $r[0];
+                    $this->children[$i]->y = $r[1];
+     
+                }
+                break; 
+            case 'translate':
+                //echo 'translate' . $x .',' . $y . "\n";
+                if (isset($this->x) &&  isset($this->y)) {
+                         
+                   $this->x +=  $x;
+                   $this->y +=  $y;
+                }
+                if (!@$this->children) {
+                    return;
+                }
+                foreach(array_keys($this->children) as $i) {
+                    if (!isset($this->children[$i]->x) || !isset($this->children[$i]->y)) {
+                        continue;
+                        // echo "<PRE>";print_r($this);exit;
+                    }
+                    $this->children[$i]->x +=  $x;
+                    $this->children[$i]->y +=  $y;
+                }
+                break; 
+                 
+          }
+     }
+
+
+
+
+    
+    function writePDF($pdf,$data) {
+        $this->childrenWritePDF($pdf,$data);
+    }
+    
+    function childrenWritePDF(&$pdf,&$data) {
+        if (!@$this->children) {
+            return;
+        }
+        foreach(array_keys($this->children) as $k) {
+            if (!$this->children[$k]) {
+                continue;
+            }
+            if (!method_exists($this->children[$k],'writePDF')) {
+                echo "OOPS unknown object? <PRE>" ; print_r($this->children[$k]); exit;
+            }
+            $this->children[$k]->writePDF($pdf,$data);
+        }
+    }
+    
+    function shiftChildren($x,$y) {
+        if (!@$this->children) {
+            return;
+        }
+        foreach(array_keys($this->children) as $k) {
+            if (!$this->children[$k]) {
+                continue;
+            }
+            $this->children[$k]->shift($x,$y);
+        }
+    }
+    
+    function shift($x,$y) {
+        //XML_SvgToPDF::debug('shift');
+        //XML_SvgToPDF::debug(array($x,$y));
+        //XML_SvgToPDF::debug($this);
+        if (isset($this->x)) {
+            
+            $this->x -= $x;
+        }
+        if (isset($this->y)) {
+            $this->y -= $y;
+        }
+        //XML_SvgToPDF::debug($this);
+        $this->shiftChildren($x,$y);
+    }
+    function calcPerPage() {
+        $ret = array();
+        foreach($this->children as $n) {
+            if (!$n) {
+                continue;
+            }
+            if (!is_a($n, 'XML_SvgToPDF_G')) {
+                continue;
+            }
+            if (!isset($n->settings)) {
+                continue;
+            }
+            
+            $rows  = isset($n->settings['rows']) ? $n->settings['rows'] : 1;
+            $cols  = isset($n->settings['cols']) ? $n->settings['cols'] : 1;
+            
+            
+            $ret[$n->settings['dynamic']] =  $rows * $cols;
+            
+            
+            
+        }
+        return $ret;
+         
+    
+    
+    
+    
+    }
+    
+    function toColor($color) {
+        if (!$color || ($color == 'none')) {
+            return false;
+        }
+        
+        if ($color == 'black') {
+            $color = '#000000';
+        }
+        
+        return array(
+            hexdec(substr($color,1,2)),
+            hexdec(substr($color,3,2)),
+            hexdec(substr($color,5,2)));
+        
+    }
+    
+    
+}