From: Vincent Vanwaelscappel Date: Fri, 10 Mar 2023 17:25:31 +0000 (+0100) Subject: wip #5799 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=277ded65cc92575ad32232748a7f80abe181bed5;p=fluidbook_tools.git wip #5799 @1 --- diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index 6073022..eaeb746 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -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. + } } diff --git a/src/Compiler/CompilerInterface.php b/src/Compiler/CompilerInterface.php index b875663..192910e 100644 --- a/src/Compiler/CompilerInterface.php +++ b/src/Compiler/CompilerInterface.php @@ -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); } diff --git a/src/Compiler/DummyCompiler.php b/src/Compiler/DummyCompiler.php index f672656..ae36857 100644 --- a/src/Compiler/DummyCompiler.php +++ b/src/Compiler/DummyCompiler.php @@ -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. + } } diff --git a/src/Compiler/FluidbookCompiler.php b/src/Compiler/FluidbookCompiler.php index 0f9f61c..7b6d28c 100644 --- a/src/Compiler/FluidbookCompiler.php +++ b/src/Compiler/FluidbookCompiler.php @@ -62,4 +62,6 @@ trait FluidbookCompiler $this->config->set($key, $value); } + + } diff --git a/src/Compiler/Links.php b/src/Compiler/Links.php index c858354..59bfc03 100644 --- a/src/Compiler/Links.php +++ b/src/Compiler/Links.php @@ -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 diff --git a/src/Links/LayerLink.php b/src/Links/LayerLink.php index 73294b1..0e1891b 100644 --- a/src/Links/LayerLink.php +++ b/src/Links/LayerLink.php @@ -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() diff --git a/src/Links/Link.php b/src/Links/Link.php index d4d53fe..5fd2d03 100644 --- a/src/Links/Link.php +++ b/src/Links/Link.php @@ -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() { diff --git a/src/Links/ZoomLink.php b/src/Links/ZoomLink.php index 6822f5c..c1b6fc0 100644 --- a/src/Links/ZoomLink.php +++ b/src/Links/ZoomLink.php @@ -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)); }