]> _ Git - fluidbook_tools.git/commitdiff
wip #4666 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Sep 2021 08:12:31 +0000 (10:12 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Sep 2021 08:12:31 +0000 (10:12 +0200)
src/CommandLine/FWSTK.php [new file with mode: 0644]
src/PDF/Document.php
src/PDF/PDFTools.php
src/SVG/SVGTools.php

diff --git a/src/CommandLine/FWSTK.php b/src/CommandLine/FWSTK.php
new file mode 100644 (file)
index 0000000..8ab8189
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+namespace Fluidbook\Tools\CommandLine;
+
+use Cubist\Util\CommandLine;
+use Fluidbook\Tools\FluidbookTools;
+
+class FWSTK extends CommandLine
+{
+    public function __construct($output = null, $error = true)
+    {
+        parent::__construct(FluidbookTools::tools_path('fwstk.sh', true), $output, $error);
+    }
+}
index 31c5deb6e4ed441a5004e290a91bc1dc05d7beac..bd2cc9529eb2b59c97d6701f605b4ba602ef5bc9 100644 (file)
@@ -3,6 +3,7 @@
 namespace Fluidbook\Tools\PDF;
 
 use Cubist\Util\CommandLine;
+use Fluidbook\Tools\CommandLine\FWSTK;
 use Fluidbook\Tools\FluidbookTools;
 use Fluidbook\Tools\Jobs\ProcessFile;
 use Fluidbook\Tools\Jobs\ProcessPage;
@@ -52,7 +53,7 @@ class Document
             return;
         }
 
-        $fwstk = new CommandLine(FluidbookTools::tools_path('fwstk.sh',true));
+        $fwstk = new FWSTK();
         $fwstk->setArg('--input ' . $this->getPDFInput());
         $fwstk->setArg('--infos');
         $fwstk->execute();
@@ -116,10 +117,30 @@ class Document
      */
     public function processPage($page, $dest, $files, $sync = false)
     {
-        start_measure('Process page ' . $page . ' (doc)');
         $dispatchFunction = $sync ? 'dispatchSync' : 'dispatch';
         ProcessPage::$dispatchFunction($this, $page, $dest, $files);
-        stop_measure('Process page ' . $page . ' (doc)');
+    }
+
+    public function processLinks($dest)
+    {
+        PDFTools::extractLinks($this->getPDFInput(), $dest);
+    }
+
+    public function processTexts($dest)
+    {
+        start_measure('Extract texts');
+        PDFTools::extractTexts($this->getPDFInput(), $dest);
+        stop_measure('Extract texts');
+        start_measure('Get highlights data');
+        PDFTools::extractHighlightsData($this->getPDFInput(), $dest);
+        stop_measure('Get highlights data');
+        start_measure('Compile search index');
+        $this->compileSearchIndex($dest);
+        stop_measure('Compile search index');
+    }
+
+    public function compileSearchIndex($dest){
+
     }
 
     public function getResolutionRatio()
index 03187046d7d9923f476720695194d6060905ebcf..f8013398cf0561e5fdb4d27aef1b164a4f38ac09 100644 (file)
@@ -7,6 +7,7 @@ use Cubist\Util\Files\Files;
 use DOMDocument;
 use DOMNode;
 use DOMXPath;
+use Fluidbook\Tools\CommandLine\FWSTK;
 
 class PDFTools
 {
@@ -55,10 +56,10 @@ class PDFTools
         $pdf2swf->setArg('p', $page);
         $pdf2swf->setArg('T', 10);
         $pdf2swf->setArg('Q', 120);
-        $pdf2swf->setArg('set reordertags', '0');
-        $pdf2swf->setArg('fonts');
         $pdf2swf->setArg('set poly2bitmap');
         $pdf2swf->setArg('set storeallcharacters');
+        $pdf2swf->setArg('set reordertags', '0');
+        $pdf2swf->setArg('fonts');
         $pdf2swf->setArg('set subpixels', $resolution / 72);
         $pdf2swf->setArg('set jpegquality', $quality);
         $pdf2swf->setArg('set disablelinks');
@@ -205,7 +206,7 @@ class PDFTools
 
     public static function makeShotPNM($in, $out, $page, $prefix = '', $resolution = 72, $quality = 90, $antialiasing = 4, $texts = true, $width = null, $height = null, $format = 'jpg')
     {
-        $tmp =Files::tempnam();
+        $tmp = Files::tempnam();
 
         $antialiasing = $antialiasing ? 'yes' : 'no';
         $freetype = $texts ? 'yes' : 'no';
@@ -271,4 +272,48 @@ class PDFTools
         return $image;
     }
 
+    public static function extractLinks($pdf, $out)
+    {
+        $out .= 'links/';
+        Files::mkdir($out);
+
+        if (file_exists($out . '/p1.csv')) {
+            return;
+        }
+        $fwstk = new FWSTK();
+        $fwstk->setArg('--input ' . $pdf);
+        $fwstk->setArg('--extractLinks ' . $out . 'p%d.csv');
+        $fwstk->setArg('--threads 1');
+        $fwstk->execute();
+    }
+
+    public static function extractTexts($pdf, $out, $ignoreSeparators = '')
+    {
+        $out .= 'texts/';
+        Files::mkdir($out);
+
+        $fwstk = new FWSTK();
+        $fwstk->setArg('--input ' . $pdf);
+        $fwstk->setArg('--extractTexts ' . $out . '%s%d.txt');
+        $fwstk->setArg('--extractTextsMethod fluidbook');
+        $fwstk->setArg('--threads 1');
+        if ($ignoreSeparators !== '') {
+            $fwstk->setArg('--ignoreSeparators ' . $ignoreSeparators);
+        }
+        $fwstk->execute();
+    }
+
+    public static function extractHighlightsData($pdf, $out)
+    {
+        $out .= 'texts/';
+        Files::mkdir($out);
+
+        $fwstk = new FWSTK();
+        $fwstk->setArg('--input ' . $pdf);
+        $fwstk->setArg('--layout ' . $out . 'p%d.fby');
+        $fwstk->setArg('--cmaps ' . $out);
+        $fwstk->setArg('--fonts' . $out . 'fonts/web/');
+        $fwstk->execute();
+    }
+
 }
index de7ff934f94de2f2e123aa1ee0990595e0cc8165..4c08a17c7e7a9256f8b2df56036cfbe712435a3a 100644 (file)
@@ -3,6 +3,7 @@
 namespace Fluidbook\Tools\SVG;
 
 use Cubist\Util\Files\Files;
+use Fluidbook\Tools\FluidbookTools;
 
 class SVGTools
 {
@@ -16,8 +17,8 @@ class SVGTools
         if (null === $out) {
             $e = explode('.', $in);
             $ext = array_pop($e);
-            array_push($e, 'o');
-            array_push($e, $ext);
+            $e[] = 'o';
+            $e[] = $ext;
             $out = implode('.', $e);
         }
 
@@ -26,9 +27,9 @@ class SVGTools
 
     public static function _optimizeSVG($in, $out)
     {
-        $cmd = "timeout -s 1 120 /usr/local/bin/svgcleaner --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout $in";
-        $svg = `$cmd`;
-        if ($svg == '') {
+
+        $svg = shell_exec('timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in);
+        if (!$svg) {
             $svg = file_get_contents($in);
         }
         $svg = self::_disablePreserveRatio($svg);
@@ -39,7 +40,7 @@ class SVGTools
     protected static function _disablePreserveRatio($in)
     {
         $str = 'preserveAspectRatio="none"';
-        if (strpos($in, $str) !== false) {
+        if (str_contains($in, $str)) {
             return $in;
         }
         return str_replace("<svg ", '<svg ' . $str . ' ', $in);
@@ -64,9 +65,7 @@ class SVGTools
         self::$_e = 0;
         self::$_u = 0;
 
-        $c = str_replace('id="', 'id="p' . $p . '-', $c);
-        $c = str_replace('url(#', 'url(#p' . $p . '-', $c);
-        $c = str_replace('xlink:href="#', 'xlink:href="#p' . $p . '-', $c);
+        $c = str_replace(array('id="', 'url(#', 'xlink:href="#'), array('id="p' . $p . '-', 'url(#p' . $p . '-', 'xlink:href="#p' . $p . '-'), $c);
         $c = preg_replace_callback('/\<image([^\>]*)\>/m', function ($matches) use ($p) {
             self::$_i++;
             return '<g id="p' . $p . '-imageholder-' . self::$_i . '"><image id="p' . $p . '-image-' . self::$_i . '" ' . $matches[1] . '></g>';