]> _ Git - cubist_pdf.git/commitdiff
wip #5532 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Oct 2022 14:39:40 +0000 (16:39 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 18 Oct 2022 14:39:40 +0000 (16:39 +0200)
src/PDFTools.php

index 8a8a911a5d4f5238096c9aa2f068066b1ae3096c..3d3158feaa7f6fe4652438be2f55b4f923b33571 100644 (file)
@@ -7,6 +7,7 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\Str;
 use Cubist\Util\Text;
 use DOMDocument;
+use DOMElement;
 use DOMNode;
 use DOMXPath;
 use Cubist\PDF\CommandLine\FWSTK;
@@ -226,11 +227,7 @@ class PDFTools
         $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
         $xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink');
         $xpath->registerNamespace("php", "http://php.net/xpath");
-        $toDelete = array(
-//            '//svg:defs/svg:clipPath',
-//            '//svg:defs/svg:image',
-//            '//svg:defs/svg:path',
-//            '//svg:defs/svg:pattern',
+        $toDelete = [
             '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:path',
             '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:rect',
             '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:use[starts-with(@xlink:href, "#image")]',
@@ -239,16 +236,13 @@ class PDFTools
             '/svg:svg/svg:g[@id="surface1"]//svg:filter',
             '/svg:svg/svg:g[@id="surface1"]//svg:use[starts-with(@xlink:href, "#image")]',
             '//svg:svg/svg:g[@id="surface1"]//svg:use[starts-with(@xlink:href, "#image")]',
-            //'//svg:image',
-        );
+        ];
+        $toDeleteIfOrphan = [
+            '//svg:image',
+        ];
 
-        //global $svglog;
-        //$svglog = array('XPATH : ' . print_r($xpath, true));
         foreach ($toDelete as $q) {
             $list = $xpath->query($q);
-            // $svglog[] = "Evaluate xpath query " . $q;
-            // $svglog[] = 'Give ' . $list->length . ' results';
-            // $svglog[] = 'Deleting Nodes in ' . print_r($list, true);
             if (count($list)) {
                 foreach ($list as $node) {
                     /* @var $node DOMNode */
@@ -257,6 +251,21 @@ class PDFTools
                 }
             }
         }
+
+        foreach ($toDeleteIfOrphan as $q) {
+            $list = $xpath->query($q);
+            if (count($list)) {
+                foreach ($list as $node) {
+                    /* @var $node DOMElement */
+                    $id = $node->getAttribute('id');
+                    if ($xpath->query('//*[@id="' . $id . '"]')->count() > 0) {
+                        $parent = $node->parentNode;
+                        $parent->removeChild($node);
+                    }
+                }
+            }
+        }
+
         $res = $svg->saveXML();
         $res = preg_replace('/<g clip-path="url\(#clip\d+\)" clip-rule="nonzero"\/>/', '', $res);
         while (true) {