]> _ Git - fluidbook_tools.git/commitdiff
wip #5799 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Mar 2023 17:25:31 +0000 (18:25 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 10 Mar 2023 17:25:31 +0000 (18:25 +0100)
src/Compiler/Compiler.php
src/Compiler/CompilerInterface.php
src/Compiler/DummyCompiler.php
src/Compiler/FluidbookCompiler.php
src/Compiler/Links.php
src/Links/LayerLink.php
src/Links/Link.php
src/Links/ZoomLink.php

index 6073022a6a1a26bf1f7d2fdb03faa683f49bbaf1..eaeb7461e87afe5f2c82e7ade4ee2024905f46b5 100644 (file)
@@ -372,4 +372,12 @@ class Compiler implements ShouldQueue, ShouldBeUnique, CompilerInterface
     public function addJsLib($name, $files) {
         // TODO: Implement addJsLib() method.
     }
+
+    public function getCacheDir(string $path) {
+        // TODO: Implement getCacheDir() method.
+    }
+
+    public function isOnePage(): bool {
+        // TODO: Implement isOnePage() method.
+    }
 }
index b875663bb7b42a03a4e71b819bdb0414c63f8b32..192910efe98abb4604d86773d1a78e8e06291d4c 100644 (file)
@@ -4,11 +4,10 @@ namespace Fluidbook\Tools\Compiler;
 
 use Cubist\Util\Files\VirtualDirectory;
 
-interface CompilerInterface
-{
+interface CompilerInterface {
     public function addContentLock($page, $unlockConditions = '');
 
-    public function addTriggersLink($page, $link, $delay=0);
+    public function addTriggersLink($page, $link, $delay = 0);
 
     public function addAudiodescription($link);
 
@@ -29,6 +28,8 @@ interface CompilerInterface
 
     public function setWorkingDir(string $wdir);
 
+    public function getCacheDir(string $path);
+
     public function working_path($path = ''): string;
 
     public function source_path($path = ''): string;
@@ -44,4 +45,8 @@ interface CompilerInterface
     public function getPagePDFSource($page): string;
 
     public function addJsLib($name, $files);
+
+    public function isOnePage(): bool;
+
+    public function simpleCopyLinkFile($source, $dest);
 }
index f67265651f7058fe7109fea1d267e621f197a133..ae368572bdee3cf0e096994929e2ca1d47c3d346 100644 (file)
@@ -91,4 +91,12 @@ class DummyCompiler implements CompilerInterface {
     public function setData(Data $data): void {
         $this->_data = $data;
     }
+
+    public function getCacheDir(string $path) {
+        // TODO: Implement getCacheDir() method.
+    }
+
+    public function isOnePage(): bool {
+        // TODO: Implement isOnePage() method.
+    }
 }
index 0f9f61cf4c02d97fbe9da81c05d0ea304d3794bb..7b6d28ce23b37e2019f6d43f52121a435e034f99 100644 (file)
@@ -62,4 +62,6 @@ trait FluidbookCompiler
         $this->config->set($key, $value);
     }
 
+
+
 }
index c858354d03724b7928b708949c450633fc28c09c..59bfc03af26b6f5429b9592004b1dc55d1e7b818 100644 (file)
@@ -180,7 +180,7 @@ trait Links
                     $$v[$lta->page][$lta->blendmode] = [];
                 }
 
-                $$v[$lta->page][$lta->blendmode][] = $lta;
+                array_push($$v[$lta->page][$lta->blendmode],$lta);
                 $i++;
             }
             // Make old "aftersearch" link compatible with new "extra" menu option by extracting link URL
index 73294b1db896126a461ae1f77780f24961254e6a..0e1891b7224a012a594a5f8057d3a6de3f66f105 100644 (file)
@@ -12,6 +12,9 @@ class LayerLink extends ImageLink
         return parent::ignore() || $this->isOutsidePage();
     }
 
+    /**
+     * @throws \Exception
+     */
     public function getCSS()
     {
         $attributes = $this->getZoomAttributes();
@@ -22,7 +25,7 @@ class LayerLink extends ImageLink
 
     public function getImageUrl()
     {
-        return '../links/layer_' . $this->uid . '.jpg';
+        return 'data/links/layer_' . $this->uid . '.jpg';
     }
 
     public function getZoomAttributes()
index d4d53feafab3b1c79e510199974bac62b5f45d9f..5fd2d0370e8729cb587c22c20dc10538c4f2f24d 100644 (file)
@@ -200,6 +200,7 @@ class Link {
                 }
                 break;
             case 39:
+
                 return new LayerLink($id, $init, $compiler);
             default:
                 return null;
@@ -591,14 +592,7 @@ class Link {
     }
 
     public function isOutsidePage() {
-        $height = isset($this->compiler->getSetting('pagesDimensions')[$this->page][1]) ?: $this->compiler->getSetting('height');
-
-        if ($this->top > $height) {
-            return true;
-        }
-        if ($this->left > $this->compiler->getSetting('width')) {
-            return true;
-        }
+        return $this->top > $this->compiler->getHeight() || $this->left > $this->compiler->getWidth();
     }
 
     public function ignore() {
index 6822f5cc01288a29a8983114ca99349ae6181691..c1b6fc02070b89ec57ea95de5a7992824c076170 100644 (file)
@@ -7,19 +7,17 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\Graphics\PDF;
 use Cubist\Util\Text;
 use Fluidbook\Tools\Compiler\Compiler;
+use Fluidbook\Tools\Compiler\CompilerInterface;
 
-class ZoomLink extends NormalLink
-{
+class ZoomLink extends NormalLink {
     protected $maxzoom_default = 2;
     protected $_groups = null;
 
-    public function ignore()
-    {
+    public function ignore() {
         return parent::ignore() || $this->isOutsidePage();
     }
 
-    public function getGroups()
-    {
+    public function getGroups() {
         if (null === $this->_groups) {
             $this->_groups = [];
             $groups = explode(',', $this->group);
@@ -30,15 +28,13 @@ class ZoomLink extends NormalLink
         return $this->_groups;
     }
 
-    public function init()
-    {
+    public function init() {
         $this->compiler->addJsLib('fluidbook-zoom', 'js/libs/fluidbook/links/fluidbook.links.zoom.js');
         $this->maxzoom_default = $this->compiler->getSetting('zoomAreaDefaultZoomLevel', 2);
         parent::init();
     }
 
-    public function getHTMLContainerClass()
-    {
+    public function getHTMLContainerClass() {
         $class = ' zoomarea';
 
         $groups = $this->getGroups();
@@ -52,13 +48,11 @@ class ZoomLink extends NormalLink
         return parent::getHTMLContainerClass() . $class;
     }
 
-    public function getDefaultTooltip()
-    {
+    public function getDefaultTooltip() {
         return 'zoom in';
     }
 
-    public function getAdditionnalContent()
-    {
+    public function getAdditionnalContent() {
         $res = parent::getAdditionnalContent();
 
         $res .= ' id="' . $this->uid . '"';
@@ -78,8 +72,7 @@ class ZoomLink extends NormalLink
         return $res;
     }
 
-    public function getZoomAttributes()
-    {
+    public function getZoomAttributes() {
         return [
             'id' => $this->uid,
             'page' => $this->page,
@@ -97,13 +90,12 @@ class ZoomLink extends NormalLink
 
     /**
      * @param $attributes
-     * @param $compiler Compiler
+     * @param $compiler CompilerInterface
      * @param $cachedir
      * @param $save
      * @return void
      */
-    public static function generateImage($attributes, $compiler, $cachedir, $save)
-    {
+    public static function generateImage($attributes, $compiler, $cachedir, $save) {
 
         $maxzoom = $attributes['maxzoom']; // Max zoom level might not always be set in the link editor
         $maxzoom = max(2, min($maxzoom, 20));
@@ -111,13 +103,11 @@ class ZoomLink extends NormalLink
             $maxzoom = 2;
         }
 
-        // TODO: Consider generating higher-res images (eg. 2x) for HiDPI screens. Maybe some extra optimisations can be done on the larger images...
-
         $extractOptions = [
             // The Poppler::extractArea function accepts a resolution setting and uses that to determine the
             // scale factor on the extracted images. It does so by dividing by 72, so we can pass our own scale
             // factor by setting the resolution to 72 * $maxzoom
-            'resolution' => 150 * $maxzoom
+            'resolution' => 72 * 2 * $maxzoom
         ];
 
         // Round all link co-ordinates because there seems to be a problem with the the Workshop link editor
@@ -129,8 +119,6 @@ class ZoomLink extends NormalLink
         $h = $attributes['height'];
         $bookwidth = round($compiler->getSetting('width'));
 
-        //error_log("--- Book Width: $bookwidth ---");
-
         if (!isset($attributes['pdf']) || !$attributes['pdf']) {
             $pdfpath = $compiler->getPagePDFSource($attributes['page']);
             $extractPage = 1;
@@ -139,17 +127,15 @@ class ZoomLink extends NormalLink
             $extractPage = $attributes['page'];
         }
 
-        $cache = storage_path('fluidbook/cache/zoomarea');
+        $cache = $compiler->getCacheDir("zoomarea/" . $cachedir);
 
-        $left = Files::tempnam();
         $leftfile = PDF::extractArea($pdfpath,
             $extractPage,
             array('x' => $x, 'y' => $y, 'width' => $w, 'height' => $h),
-            $left, $extractOptions, $cache);
+            null, $extractOptions, $cache);
 
-        if (($x + $w) > $bookwidth) {
+        if (($x + $w) > $bookwidth && !$compiler->isOnePage()) {
             if (!isset($attributes['pdf']) || !$attributes['pdf']) {
-
                 $pdfpath = $compiler->getPagePDFSource($attributes['page'] + 1);
                 $extractPage = 1;
             } else {
@@ -158,18 +144,19 @@ class ZoomLink extends NormalLink
             }
 
             $diff = ($w + $x) - $bookwidth;
-            $right = Files::tempnam();
             $rightfile = PDF::extractArea($pdfpath,
                 $extractPage,
                 array('x' => 0, 'y' => $y, 'width' => $diff, 'height' => $h),
-                $right, $extractOptions, $cache);
+                null, $extractOptions, $cache);
 
             if (!file_exists($rightfile)) {
                 die('Error generating right part ' . $rightfile);
             }
 
-            $both = Files::tempnam() . '.jpg';
-            Imagemagick::append(array($leftfile, $rightfile), $both, 'horizontal');
+            $both = $cache . hash('sha256', $leftfile . $rightfile) . '.jpg';
+            if (!file_exists($both)) {
+                Imagemagick::append(array($leftfile, $rightfile), $both, 'horizontal');
+            }
             if (!file_exists($both)) {
                 throw new \Exception('Error glueing ' . $leftfile . ' and ' . $rightfile);
             }
@@ -177,33 +164,28 @@ class ZoomLink extends NormalLink
             $both = $leftfile;
         }
 
-        if (isset($attributes['border']) && $attributes['border'] > 0) {
-            $tmp = Files::tempnam() . '.jpg';
-            Imagemagick::addBorder($both, $tmp, $attributes['border'], $attributes['borderColor']);
-            $compiler->getVirtualDirectory()->addTemp($both);
-            $both = $tmp;
-        }
+        //                if (isset($attributes['border']) && $attributes['border'] > 0) {
+        //                    $tmp = Files::tempnam() . '.jpg';
+        //                    Imagemagick::addBorder($both, $tmp, $attributes['border'], $attributes['borderColor']);
+        //                    $compiler->getVirtualDirectory()->addTemp($both);
+        //                    $both = $tmp;
+        //                }
+        //                dd($both);
 
-        $compiler->simpleCopyLinkFile($both, 'data/links/' . $save . '_' . $attributes['id'] . '.jpg');
 
-        // Perform tidy up and delete temporary files if they exist
-        $files_to_delete = ['left', 'leftfile', 'right', 'rightfile', 'both'];
-        foreach ($files_to_delete as $file) {
-            if (isset($$file)) {
-                $compiler->getVirtualDirectory()->addTemp($$file);
-            }
-        }
+        $dest = 'data/links/' . $save . '_' . $attributes['id'] . '.jpg';
+        $compiler->simpleCopyLinkFile($both, $dest);
     }
 
 
-    public function getClasses()
-    {
+    public function getClasses() {
         // Assign CSS classes for all groups so we can match and group them via JS
         $groups = $this->getGroups();
         $group_classes = [];
 
         foreach ($groups as $group) {
-            if (empty($group)) continue;
+            if (empty($group))
+                continue;
 
             $group_classes[] = 'zoom-group-' . trim(Text::str2URL($group));
         }