From a6ae6fc399509ad4860318d24ea182d977128c9d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 17 Feb 2022 10:43:51 +0100 Subject: [PATCH] wip #5084 @0.5 --- .idea/deployment.xml | 57 ++++++++++++++++++++++++++--- src/Compiler/Compiler.php | 75 +++++++++++++++++++++++++++------------ src/Jobs/ProcessFile.php | 2 +- src/Links/ZoomLink.php | 27 +++++++++----- 4 files changed, 124 insertions(+), 37 deletions(-) diff --git a/.idea/deployment.xml b/.idea/deployment.xml index 5d9a0bb..2b6ae22 100644 --- a/.idea/deployment.xml +++ b/.idea/deployment.xml @@ -2,7 +2,14 @@ - + + + + + + + + @@ -44,6 +51,13 @@ + + + + + + + @@ -51,6 +65,13 @@ + + + + + + + @@ -114,10 +135,17 @@ + + + + + + + - + @@ -131,6 +159,13 @@ + + + + + + + @@ -145,14 +180,14 @@ - + - + @@ -236,6 +271,13 @@ + + + + + + + @@ -285,6 +327,13 @@ + + + + + + + diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index 9808852..57fd9df 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -20,7 +20,8 @@ use Illuminate\Queue\SerializesModels; use JsonException; use stdClass; -class Compiler implements ShouldQueue, ShouldBeUnique { +class Compiler implements ShouldQueue, ShouldBeUnique +{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Links; @@ -64,7 +65,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { */ protected $resolutions = [150, 300]; - public function addSource($document, $start = 1, $pages = null) { + public function addSource($document, $start = 1, $pages = null) + { $source = new Source(new Document($document), $start, $pages); $this->sources[] = $source; foreach ($source->getRange() as $sourcePage) { @@ -72,7 +74,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { } } - public function getVirtualDirectory() { + public function getVirtualDirectory() + { return $this->vdir; } @@ -82,7 +85,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { * * @return void */ - public function handle() { + public function handle() + { $sync = ($this->job instanceof SyncJob); if ($sync) { PHP::neverStop(false); @@ -95,7 +99,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { stop_measure('Process Fluidbook'); } - public function processPages($sync = false) { + public function processPages($sync = false) + { start_measure('Process Pages'); foreach ($this->sources as $source) { $source->getDocument()->processPages($this->getProcessFiles(), $source->getRange(), $sync); @@ -103,7 +108,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { stop_measure('Process Pages'); } - public function getTexts() { + public function getTexts() + { start_measure('Process texts'); foreach ($this->sources as $source) { $source->getDocument()->processTexts(); @@ -111,10 +117,12 @@ class Compiler implements ShouldQueue, ShouldBeUnique { start_measure('Process texts'); } + /** * @return ProcessFile[] */ - public function getProcessFiles() { + public function getProcessFiles() + { $res = [ 'thumb' => new ProcessFile('jpg', 'thumb'), 'text' => new ProcessFile('svg', 150, false, true), @@ -131,7 +139,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { * @return ProcessFile * @throws Exception */ - public function getProcessFile($name, $page = null) { + public function getProcessFile($name, $page = null) + { $files = $this->getProcessFiles(); if (isset($files[$name])) { $res = $files[$name]; @@ -149,7 +158,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { * @return SourcePage * @throws Exception */ - public function getSourceOfPage($page) { + public function getSourceOfPage($page) + { if (isset($this->pages[$page - 1])) { return $this->pages[$page - 1]; } @@ -159,7 +169,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { /** * @throws JsonException */ - protected function compileFluidbook() { + protected function compileFluidbook() + { start_measure('Compile fluidbook'); if ($this->wdir === '') { @@ -179,13 +190,15 @@ class Compiler implements ShouldQueue, ShouldBeUnique { stop_measure('Compile fluidbook'); } - public function prepareStub() { + public function prepareStub() + { if ($this->stub !== '') { $this->vdir->copyDirectory($this->stub, '/'); } } - public function compileContents() { + public function compileContents() + { for ($i = 1; $i <= $this->getPageCount(); $i++) { foreach ($this->resolutions as $resolution) { @@ -196,7 +209,13 @@ class Compiler implements ShouldQueue, ShouldBeUnique { } } - public function getPageCount() { + public function getPagePDFSource($page) + { + return $this->getProcessFile('back300', $page)->getSplittedPDFPage(); + } + + public function getPageCount() + { $res = 0; foreach ($this->sources as $source) { $res += $source->getPages(); @@ -208,7 +227,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { /** * @throws \JsonException */ - public function compileSearch() { + public function compileSearch() + { $index = new SearchIndex(); foreach ($this->pages as $i => $page) { @@ -222,7 +242,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { /** * @throws JsonException */ - public function compileConfig() { + public function compileConfig() + { $settings = mb_substr(file_get_contents($this->stub . '/data/datas.js'), 13, -2); $this->config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR); $this->config->maxResolution = 300; @@ -236,7 +257,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { $this->vdir->file_put_contents('data/datas.js', $c); } - protected function compileDimensions() { + protected function compileDimensions() + { $firstPageDoc = $this->getSourceOfPage(1)->getSource()->getDocument(); $width = round($firstPageDoc->getWidth(), 8); @@ -269,7 +291,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { } } - protected function compilePageNumbers() { + protected function compilePageNumbers() + { $this->config->numerotation = []; foreach ($this->sources as $source) { @@ -277,7 +300,8 @@ class Compiler implements ShouldQueue, ShouldBeUnique { } } - protected function compileChapters() { + protected function compileChapters() + { $this->config->chaptersPagesNumber = 'physical'; $this->config->chapters = []; foreach ($this->sources as $source) { @@ -285,23 +309,28 @@ class Compiler implements ShouldQueue, ShouldBeUnique { } } - public function getBookSurface() { + public function getBookSurface() + { return $this->getWidth() * $this->getHeight(); } - public function getWidth() { + public function getWidth() + { return $this->getSourceOfPage(1)->getSource()->getDocument()->getWidth(); } - public function getHeight() { + public function getHeight() + { return $this->getSourceOfPage(1)->getSource()->getDocument()->getHeight(); } - public function addVideoJs() { + public function addVideoJs() + { $this->addLess('videojs/videojs'); } - public function addSlideshowLibrary($inline = true) { + public function addSlideshowLibrary($inline = true) + { $l = ($inline ? $this->config->inlineSlideshowLibrary : $this->config->popupSlideshowLibrary); $this->addLess('slideshow/' . $l); } diff --git a/src/Jobs/ProcessFile.php b/src/Jobs/ProcessFile.php index 6ae1049..f421e94 100644 --- a/src/Jobs/ProcessFile.php +++ b/src/Jobs/ProcessFile.php @@ -257,7 +257,7 @@ class ProcessFile SVGTools::optimizeSVG($out, $out); } - protected function getSplittedPDFPage() + public function getSplittedPDFPage() { $res = $this->getOut() . 'pdf/p' . $this->getPage() . '.pdf'; if (!file_exists($res)) { diff --git a/src/Links/ZoomLink.php b/src/Links/ZoomLink.php index 1c49c84..0447606 100644 --- a/src/Links/ZoomLink.php +++ b/src/Links/ZoomLink.php @@ -6,6 +6,7 @@ use Cubist\Util\CommandLine\Imagemagick; use Cubist\Util\Files\Files; use Cubist\Util\Graphics\PDF; use Cubist\Util\Text; +use Fluidbook\Tools\Compiler\Compiler; class ZoomLink extends NormalLink { @@ -88,6 +89,13 @@ class ZoomLink extends NormalLink ]; } + /** + * @param $attributes + * @param $compiler Compiler + * @param $cachedir + * @param $save + * @return void + */ public static function generateImage($attributes, $compiler, $cachedir, $save) { @@ -113,29 +121,30 @@ class ZoomLink extends NormalLink $y = $attributes['y']; $w = $attributes['width']; $h = $attributes['height']; - $bookwidth = round($compiler->width); + $bookwidth = round($compiler->config->width); //error_log("--- Book Width: $bookwidth ---"); if (!isset($attributes['pdf']) || !$attributes['pdf']) { - $p = wsDAOBook::getDocumentPage($compiler->book_id, $attributes['page']); - $pdfpath = wsDocument::getDir($p['document_id']) . '/pdf/p' . $p['document_page'] . '.pdf'; + $pdfpath = $compiler->getPagePDFSource($attributes['page']); $extractPage = 1; } else { $pdfpath = $attributes['pdf']; $extractPage = $attributes['page']; } + $cache=storage_path('fluidbook/cache/zoomarea'); + $left = Files::tempnam(); $leftfile = PDF::extractArea($pdfpath, $extractPage, array('x' => $x, 'y' => $y, 'width' => $w, 'height' => $h), - $left, $extractOptions, storage_path('cache/' . $cachedir . '/' . $compiler->book_id . '/')); + $left, $extractOptions,$cache); if (($x + $w) > $bookwidth) { if (!isset($attributes['pdf']) || !$attributes['pdf']) { - $p = wsDAOBook::getDocumentPage($compiler->book_id, $attributes['page'] + 1); - $pdfpath = wsDocument::getDir($p['document_id']) . '/pdf/p' . $p['document_page'] . '.pdf'; + + $pdfpath = $compiler->getPagePDFSource($attributes['page'] + 1); $extractPage = 1; } else { $pdfpath = $attributes['pdf']; @@ -147,7 +156,7 @@ class ZoomLink extends NormalLink $rightfile = PDF::extractArea($pdfpath, $extractPage, array('x' => 0, 'y' => $y, 'width' => $diff, 'height' => $h), - $right, $extractOptions, storage_path('cache/' . $cachedir . '/' . $compiler->book_id . '/')); + $right, $extractOptions,$cache); if (!file_exists($rightfile)) { die('Error generating right part ' . $rightfile); @@ -165,7 +174,7 @@ class ZoomLink extends NormalLink if (isset($attributes['border']) && $attributes['border'] > 0) { $tmp = Files::tempnam() . '.jpg'; Imagemagick::addBorder($both, $tmp, $attributes['border'], $attributes['borderColor']); - $compiler->vdir->addTemp($both); + $compiler->getVirtualDirectory()->addTemp($both); $both = $tmp; } @@ -175,7 +184,7 @@ class ZoomLink extends NormalLink $files_to_delete = ['left', 'leftfile', 'right', 'rightfile', 'both']; foreach ($files_to_delete as $file) { if (isset($$file)) { - $compiler->vdir->addTemp($$file); + $compiler->getVirtualDirectory()->addTemp($$file); } } } -- 2.39.5