From 6ac78e60d9734ce58a4fba760c091cd78f37dcc6 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 4 Oct 2021 18:47:19 +0200 Subject: [PATCH] wip #4747 @0.5 --- src/Compiler/Compiler.php | 4 +- src/Compiler/Styles.php | 214 ++++++++++++++++++++++++++++++-------- src/PDF/Document.php | 7 +- 3 files changed, 178 insertions(+), 47 deletions(-) diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index 6f75227..fd38318 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -110,7 +110,7 @@ class Compiler implements ShouldQueue, ShouldBeUnique return [ 'thumb' => new ProcessFile('jpg', 'thumb'), 'back150' => new ProcessFile('jpg', 150, true, false), - 'text' => new ProcessFile('svg', 300, false, true), + 'text' => new ProcessFile('svg', 150, false, true), ]; } @@ -214,7 +214,7 @@ class Compiler implements ShouldQueue, ShouldBeUnique $this->compileDimensions(); $this->compilePageNumbers(); $this->compileChapters(); - $this->compileLinks(); + $this->compileStyles($this->compileLinks()); $c = 'var SETTINGS=' . json_encode($this->config, JSON_THROW_ON_ERROR) . ';'; $this->vdir->file_put_contents('data/datas.js', $c); diff --git a/src/Compiler/Styles.php b/src/Compiler/Styles.php index dba98d6..36a06c9 100644 --- a/src/Compiler/Styles.php +++ b/src/Compiler/Styles.php @@ -3,24 +3,77 @@ namespace Fluidbook\Tools\Compiler; use Cubist\Util\CommandLine; +use Cubist\Util\Files\Files; use Cubist\Util\Graphics\Color; +use Cubist\Util\Graphics\Image; trait Styles { protected $lessVariables = []; + protected $stylesheets = []; + protected $lessSource; + protected $cssfont = []; + public $lessFiles = ['fluidbook']; - protected function writeCSS($links) + public function isMobileFirst() + { + return $this->config->mobileNavigationType === 'mobilefirst'; + } + + public function addFontKit($font) + { + if ($font === 'sans-serif') { + return; + } + if ($font === 'Open Sans') { + $font = 'OpenSans'; + } + + $path = 'fonts/' . $font; + $css = $path . '/font.css'; + if (in_array($css, $this->stylesheets)) { + return; + } + $this->stylesheets[] = $css; + $this->vdir->copyDirectory($this->lessSource . $path, $path); + return $path . '/font.css'; + } + + protected function _font($f) + { + $default = 'Arial, Helvetica, sans-serif'; + if ($f === 'OpenSans') { + $f = 'Open Sans'; + } + switch ($f) { + case 'Montserrat': + case 'Open Sans': + $this->addFontKit($f); + return "'" . $f . "', " . $default; + case 'sans-serif': + return $f; + case 'Arial': + return $default; + default: + return "'Open Sans', Arial, Helverica, sans-serif"; + } + } + + protected function compileStyles($links) { $res = array(); - $this->addFontKit('OpenSans'); + $this->lessSource = $this->stub . '/_less/'; $lessContents = ''; + $this->lessVariables['import-cart-styles']='none'; + $this->lessVariables['articles-title-color']='#000000'; + $this->lessVariables['font'] = $this->_font($this->config->interfaceFont); $this->lessVariables['text-transform'] = $this->_themeBoolean($this->config->interfaceFontUppercase) ? 'uppercase' : 'inherit'; - $this->lessVariables['css-scale'] = $this->cssScale; + $this->lessVariables['css-scale'] = $this->config->cssScale; $this->lessVariables['slider-background'] = Color::colorToCSS(!$this->config->sliderBackground ? 'rgba(0,0,0,0.1)' : $this->config->sliderBackground); $this->lessVariables['slider-handle'] = Color::colorToCSS(!$this->config->sliderHandle ? '#ffffff' : $this->config->sliderHandle); @@ -30,27 +83,27 @@ trait Styles // General theme - $cssWidth = $this->cssWidth; - $cssHeight = $this->cssHeight; - $cssScale = $this->cssScale; + $cssWidth = $this->config->width; + $cssHeight = $this->config->height; + $cssScale = $this->config->cssScale; $w2 = ($cssWidth * 2) . 'px'; $h = $cssHeight . 'px'; - $wm = ($this->getWidth() * $this->multiply) . 'px'; - $hm = ($this->getHeight() * $this->multiply) . 'px'; + $wm = ($this->getWidth() * $this->config->multiply) . 'px'; + $hm = ($this->getHeight() * $this->config->multiply) . 'px'; $w = $cssWidth . 'px'; - $offsetLeft = round(($this->optimalWidth - $cssWidth) / 2, 3); + $offsetLeft = round(($this->config->optimalWidth - $cssWidth) / 2, 3); $offsetLeft2 = $offsetLeft * 2; - $offsetTop = round(($this->optimalHeight - $cssHeight) / 2, 3); + $offsetTop = round(($this->config->optimalHeight - $cssHeight) / 2, 3); $navTop = ($cssHeight - 40 - 100) / 2; $leftOfRightPage = (floor($cssWidth) - 1) . 'px'; - $this->lessVariables['z'] = $this->z; + $this->lessVariables['z'] = $this->config->pageZoomFactor; $this->lessVariables['book-page-width'] = $w; if ($this->config->correctCenter && !$this->isMobileFirst()) { - $this->lessVariables['book-page-correct-width'] = ceil($w) + 1; - $this->lessVariables['book-page-correct-height'] = ceil($h) + 1; + $this->lessVariables['book-page-correct-width'] = ceil($cssWidth) + 1; + $this->lessVariables['book-page-correct-height'] = ceil($cssHeight) + 1; } else { $this->lessVariables['book-page-correct-width'] = $w; $this->lessVariables['book-page-correct-height'] = $h; @@ -75,18 +128,6 @@ trait Styles $corrText = $this->isMobileFirst() ? 0 : 4; - if ($this->cssSVGScale != 1) { - $texts = '.texts{' . wsHTML5::writeCSSUA('transform-origin', 'top left') . ';'; - $texts .= wsHTML5::writeCSSUA('transform', 'scale(' . round((1 / $this->multiply) * $cssScale * $this->cssSVGScale, 3) . ')') . ';'; - $texts .= 'width:' . ($wm / $this->cssSVGScale) . 'px; max-width:' . ($wm / $this->cssSVGScale) . 'px;'; - $texts .= 'height:' . ($hm / $this->cssSVGScale) . 'px; max-height:' . ($hm / $this->cssSVGScale) . 'px;'; - $texts .= '}'; - } else { - $texts = '.texts{width:' . floor(floatval($w) + $corrText) . 'px;height:' . floor(floatval($h) + $corrText) . 'px;}'; - } - - $res[] = $texts; - // Theme $shade = '.page .shade{'; $shade .= 'opacity:' . min(($this->config->shadeAlpha * 2) / 100, 1) . ';'; @@ -120,7 +161,7 @@ trait Styles $logo = '#logo{'; if ($l = $this->checkThemeImage($this->config->logo)) { $this->vdir->copy($l, 'data/images/' . $this->config->logo); - $dim = CubeIT_Image::getimagesize($l); + $dim = Image::getimagesize($l); $logo .= 'background-image:url(../images/' . $this->config->logo . ');width:' . $dim[0] . 'px;height:' . $dim[1] . 'px;'; } $logo .= '}'; @@ -156,7 +197,7 @@ trait Styles $this->lessVariables['bookmark-star-disabled-color'] = Color::colorToCSS($this->config->bookmarkStarDisabledColor); $this->lessVariables['bookmark-star-enabled-color'] = Color::colorToCSS($this->config->bookmarkStarEnabledColor); $this->lessVariables['bookmark-color'] = Color::colorToCSS($this->config->bookmarkBackgroundColor); - $this->lessVariables['bookmark-corner-size'] = round($this->getWidth() * $this->config->bookmarkCornerSize * 0.0075 * $this->z) . 'px'; + $this->lessVariables['bookmark-corner-size'] = round($this->getWidth() * $this->config->bookmarkCornerSize * 0.0075 * $this->config->pageZoomFactor) . 'px'; $this->lessVariables['bookmark-corner-offset'] = $this->config->bookmarkOffset . 'px'; // Menus @@ -189,7 +230,7 @@ trait Styles $this->lessVariables['menu-chapters-columns-width'] = $this->config->chaptersColMaxWidth; $this->lessVariables['menu-chapters-font-size'] = $this->config->chaptersFontSize; - foreach ($this->book->chapters as $chapter) { + foreach ($this->config->chapters as $chapter) { if (substr($chapter->page, 0, 1) != '#') { continue; } @@ -200,11 +241,11 @@ trait Styles $lessContents .= '.mview.c_' . $color . '{.menu-color(' . Color::colorToCSS($color) . ');}'; } - // Archives - if ($this->config->externalArchivesBack) { - $this->vdir->copy($this->wdir . '/' . $this->config->externalArchivesBack, 'data/images/' . $this->config->externalArchivesBack); - $res[] = '.mview.archives{background-image:url("../images/' . $this->config->externalArchivesBack . '");}'; - } +// // Archives +// if ($this->config->externalArchivesBack) { +// $this->vdir->copy($this->wdir . '/' . $this->config->externalArchivesBack, 'data/images/' . $this->config->externalArchivesBack); +// $res[] = '.mview.archives{background-image:url("../images/' . $this->config->externalArchivesBack . '");}'; +// } # Index $thumbw = $this->config->mobileNavigationType === 'portrait' ? 200 : 100; @@ -246,16 +287,16 @@ trait Styles protected function compileLess($variables, $lessContents = '') { - if ($this->widget) { - $this->lessFiles[] = 'widget'; - } - foreach ($this->specialCSS as $s) { - $this->lessFiles[] = 'special/' . $s; - } +// if ($this->widget) { +// $this->lessFiles[] = 'widget'; +// } +// foreach ($this->specialCSS as $s) { +// $this->lessFiles[] = 'special/' . $s; +// } - $tmp = CubeIT_Files::tmpdir(); + $tmp = Files::tmpdir(); - $from = $this->assets . '/style/*'; + $from = $this->lessSource . '/*'; `cp -r $from $tmp`; $bookVariables = array(); @@ -266,7 +307,7 @@ trait Styles file_put_contents($tmp . '/additional.less', $lessContents); foreach ($this->lessFiles as $f) { - $source_less = $this->assets . '/style/' . $f . '.less'; + $source_less = $this->lessSource . '/' . $f . '.less'; $destination_less = $tmp . '/' . $f . '.less'; $destination_css = $tmp . '/' . $f . '.css'; @@ -322,8 +363,97 @@ trait Styles return false; } + protected function _lessBoolean($val) + { + return $this->_themeBoolean($val) ? 'true' : 'false'; + } + protected function _themeBoolean($v): bool { return !(null === $v || $v === '0' || $v === 0 || $v === false || !$v); } + + + protected function _cssBackground() + { + $body = '#background, #splash {'; + + switch ($this->config->repeat) { + case 1: + $body .= 'background-repeat:repeat;'; + break; + case 3: + $body .= 'background-repeat:no-repeat;'; + break; + case 2: + $body .= 'background-repeat:no-repeat;'; + $body .= 'background-size:cover;'; + break; + case 0: + $body .= 'background-repeat:no-repeat;'; + $body .= 'background-size:100% 100%;'; + break; + } + + if ($bi = $this->checkThemeImage($this->config->backgroundImage)) { + + $dbi = Image::getimagesize($bi); + $this->config->backgroundImageDimensions = array('width' => $dbi[0], 'height' => $dbi[1]); + + + $this->vdir->copy($bi, 'data/images/' . $this->config->backgroundImage); + $body .= 'background-image:url(../images/' . $this->config->backgroundImage . ');'; + $body .= 'background-position:'; + + switch ($this->config->backgroundVAlign) { + case 8: + $body .= 'top'; + break; + case 7: + $body .= 'center'; + break; + case 9: + $body .= 'bottom'; + break; + } + + $body .= ' '; + + switch ($this->config->backgroundHAlign) { + case 5: + $body .= 'left'; + break; + case 4: + $body .= 'center'; + break; + case 6: + $body .= 'right'; + break; + } + $body .= ';'; + } + + $body .= '}'; + + + if ($this->_themeBoolean($this->config->displayBackgroundDuringLoading)) { + $body .= '#background, #splash { + background-color:' . Color::colorToCSS($this->config->backgroundColor) . ' !important; + }'; + } else { + $body .= '#background { + transition: 500ms opacity; + visibility: hidden; + opacity: 0; + background-color:' . Color::colorToCSS($this->config->backgroundColor) . ' !important; + }'; + + $body .= '#splash { + background-color:' . Color::colorToCSS($this->config->loadingBackColor) . ' !important; + background-image: none; + }'; + } + + return $body; + } } diff --git a/src/PDF/Document.php b/src/PDF/Document.php index b83d0f6..f9844b6 100644 --- a/src/PDF/Document.php +++ b/src/PDF/Document.php @@ -110,11 +110,12 @@ class Document if ($k === 'BookmarkTitle') { $bookmark_id++; - $this->chapters[$bookmark_id] = array('label' => html_entity_decode($v)); + $this->chapters[$bookmark_id] = new \stdClass(); + $this->chapters[$bookmark_id]->label = html_entity_decode($v); } elseif ($k === 'BookmarkLevel') { - $this->chapters[$bookmark_id]['level'] = (int)$v - 1; + $this->chapters[$bookmark_id]->level = (int)$v - 1; } elseif ($k === 'BookmarkPage') { - $this->chapters[$bookmark_id]['page'] = $v; + $this->chapters[$bookmark_id]->page = $v; } } } -- 2.39.5