From: vincent@cubedesigners.com Date: Wed, 13 Jul 2011 18:56:34 +0000 (+0000) Subject: (no commit message) X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e6b6fc71e229c6900f40cc787350aa3e041c5e4f;p=cubeextranet.git --- diff --git a/inc/ws/Controlleur/class.ws.flash.php b/inc/ws/Controlleur/class.ws.flash.php index 1432e3492..6d07dc57c 100644 --- a/inc/ws/Controlleur/class.ws.flash.php +++ b/inc/ws/Controlleur/class.ws.flash.php @@ -181,6 +181,10 @@ class wsFlash extends cubeFlashGateway { } $dest = $dir . $fname; move_uploaded_file($infos['tmp_name'], $dest); + if (in_array(strtolower(files::getExtension($dest)), array('flv', 'f4v', 'mp4'))) { + // convert uploaded file as webvideo (ogv and mp4) + wsTools::encodeWebVideos($dest, null, true, false); + } $this->xml->addChild('file', $fname); return; } diff --git a/inc/ws/Util/_common.php b/inc/ws/Util/_common.php index ce25bae62..d13335f27 100644 --- a/inc/ws/Util/_common.php +++ b/inc/ws/Util/_common.php @@ -1,4 +1,5 @@ \ No newline at end of file diff --git a/inc/ws/Util/class.ws.tools.php b/inc/ws/Util/class.ws.tools.php new file mode 100644 index 000000000..faf132aa9 --- /dev/null +++ b/inc/ws/Util/class.ws.tools.php @@ -0,0 +1,70 @@ +setPath(CONVERTER_PATH); + if ($async) { + $webvideo->setNohup(true); + } + $webvideo->setArg(null, $dest); + $webvideo->setArg(null, $dir); + $webvideo->execute(); + } + + public static function colorizeAndRasterizeIcon($iconSet, $icon, $color, $dest, $scale, &$w, &$h) { + // Init directory + $color = ltrim($color, '#'); + $dirColorized = WS_ICONS . '/' . $iconSet . '/mobile/' . $color . '/'; + if (!file_exists($dirColorized)) { + mkdir($dirColorized, 0777, true); + } + + + // SVG + $svgRef = WS_ICONS . '/' . $iconSet . '/mobile/' . $icon . '.svg'; + $svgColorized = $dirColorized . '/' . $icon . '.svg'; + + if (!file_exists($svgColorized) || filemtime($svgColorized) <= filemtime($svgRef)) { + $svg = file_get_contents($svgRef); + // Colorize it + $svg = str_replace('$colorize', '#' . $color, $svg); + file_put_contents($svgColorized, $svg); + } + copy($svgColorized, $dest . '/' . $icon . '.svg'); + + // PNG + $png = $dirColorized . '/' . $icon . '.png'; + if (!file_exists($png) || filemtime($png) <= filemtime($svgColorized)) { + $svg = simplexml_load_file($svgColorized); + $w = (string) $svg['width']; + $h = (string) $svg['height']; + $w = rtrim($w, 'px'); + $h = rtrim($h, 'px'); + // Finally rasterize it + $batik = new cubeCommandLine('/usr/local/batik/batik-rasterizer'); + $batik->setArg('d', $dirColorized); + $batik->setArg('m', 'image/png'); + if ($scale != 1) { + $batik->setArg('w', $w * $scale); + $batik->setArg('h', $h * $scale); + } + $batik->setManualArg($svgColorized); + $batik->execute(); + } + copy($png, $dest . '/' . $icon . '.png'); + } + +} + +?> diff --git a/inc/ws/Util/packager/_common.php b/inc/ws/Util/packager/_common.php index 17aca9b37..caa932b13 100644 --- a/inc/ws/Util/packager/_common.php +++ b/inc/ws/Util/packager/_common.php @@ -1,7 +1,7 @@ version = 'html5'; - } - - protected function preparePackage() { - parent::preparePackage(); - - foreach ($this->pages as $page => $infos) { - $file = WS_DOCS . '/' . $infos['document_id'] . '/html/p' . $infos['document_page'] . '.layout'; - if (file_exists($file)) { - $this->layouts[$page] = simplexml_load_file($file, null, LIBXML_ERR_WARNING); - } - } - - $imagesize = getimagesize(WS_DOCS . '/' . $this->pages[1]['document_id'] . '/html/h72-' . $this->pages[1]['document_page'] . '.jpg'); - $this->pdf2htmlRatio = $imagesize[0] / $this->layouts[1]['width']; - $this->scale = 4; - $this->multiply = $this->pdf2htmlRatio * $this->scale; - - $this->createHTML(); - } - - public function makePackage() { - parent::makePackage(); - return $this->zip(); - } - - protected function createHTML() { - foreach ($this->layouts as $page => $layout) { - $this->div[$page] = array(); - $document_id = $this->pages[$page]['document_id']; - foreach ($layout->l as $line) { - $this->div[$page][] = $this->addLine($line, $document_id); - } - } - mkdir($this->vdir . '/data/images', 0777, true); - mkdir($this->vdir . '/data/contents', 0777, true); - mkdir($this->vdir . '/data/background', 0777, true); - mkdir($this->vdir . '/data/style', 0777, true); - - foreach ($this->div as $n => $page) { - file_put_contents($this->vdir . '/data/contents/p' . $n . '.html', $this->writePage($page)); - } - $this->writeFonts(); - $this->writeImages(); - $this->writeCSS($this->vdir . '/data/style/style_%d.css'); - - file_put_contents($this->vdir . '/data/datas.js', $this->writeConfig()); - } - - protected function writeConfig() { - $config = cubeObject::merge($this->book->parametres->toStandardObject(), $this->theme->parametres->toStandardObject()); - $config->id = $this->book->book_id; - return 'DATAS=' . json_encode($config) . ';'; - } - - protected function writeFonts() { - $fext = array('ttf', 'eot', 'svg', 'svgz', 'woff'); - foreach ($this->cssFont as $font => $index) { - list($font, $document_id) = explode('//', $font); - foreach ($fext as $ext) { - copy(WS_DOCS . '/' . $document_id . "/fonts/web/" . $font . '.' . $ext, $this->vdir . '/data/style/F' . $index . '.' . $ext); - } - } - } - - protected function writeIcons() { - $res = array(); - // Get the colors used to colorize graphics - if ($this->theme->parametres->colorizeIcons) { - $couleurI = '#' . $this->theme->parametres->couleurI; - } else { - $couleurI = '#FFFFFF'; - } - $arrowsColor = '#' . $this->theme->parametres->arrowsColor; - // Set the icon list with the color - $icons = array('nav-bookmark' => $couleurI, 'nav-friend' => $couleurI, 'nav-help' => $couleurI, 'nav-index' => $couleurI, 'nav-sommaire' => $couleurI, - 'next' => $arrowsColor, 'previous' => $arrowsColor, 'search' => $couleurI); - - foreach ($icons as $icon => $color) { - // Get content of the svg - $svg = file_get_contents(WS_ICONS . '/' . $this->theme->parametres->iconSet . '/mobile/' . $icon . '.svg'); - // Colorize it - $svg = str_replace('$colorize', $color, $svg); - // Write the file - file_put_contents($this->vdir . '/data/images/' . $icon . '.svg', $svg); - $svg = simplexml_load_string($svg); - $w = (string) $svg['width']; - $h = (string) $svg['height']; - $w = rtrim($w, 'px'); - $h = rtrim($h, 'px'); - // Finally rasterize it - $this->rasterizeSVG($this->vdir . '/data/images/' . $icon . '.svg', 'image/png', $w, $h, 4); - $res[] = '#icon-' . $icon . '{width:' . $w . 'px;height:' . $h . 'px;background-image:url(data/images/' . $icon . '.svg), url(data/images/' . $icon . '.png);background-repeat:no-repeat;background-size:' . $w . 'px ' . $h . 'px}'; - } - return $res; - } - - protected function rasterizeSVG($file, $type, $w, $h, $scale=1) { - $batik = new cubeCommandLine('/usr/local/batik/batik-rasterizer'); - $batik->setArg('d', $this->vdir . '/data/images'); - $batik->setArg('m', 'image/png'); - if ($scale != 1) { - $batik->setArg('w', $w * $scale); - $batik->setArg('h', $h * $scale); - } - $batik->setManualArg($file); - $batik->execute(); - } - - protected function writeImages() { - - foreach (self::$resolutions as $r) { - mkdir($this->vdir . '/data/background/' . $r, 0777); - } - foreach ($this->pages as $page => $infos) { - foreach (self::$resolutions as $r) { - copy(WS_DOCS . '/' . $infos['document_id'] . '/html/h' . $r . '-' . $infos['document_page'] . '.jpg', $this->vdir . '/data/background/' . $r . '/p' . $page . '.jpg'); - } - } - } - - protected function writePage($page) { - $res = ''; - foreach ($page as $line) { - $res .= $this->writeLine($line); - } - return $res; - } - - protected function writeLine($line) { - $line['x'] = $this->getCSSX($line['x'] * $this->multiply); - $line['y'] = $this->getCSSY($line['y'] * $this->multiply); - - $class = array('l'); - if (!is_null($line['rotation'])) { - $class[] = 'r' . $line['rotation']; - } - if (!is_null($line['x'])) { - $class[] = 'x' . $line['x']; - } - if (!is_null($line['y'])) { - $class[] = 'y' . $line['y']; - } - $class = implode(' ', $class); - - - $res = '
'; - foreach ($line['groups'] as $group) { - $res.=$this->writeGroup($group); - } - $res.='
'; - return $res; - } - - protected function writeGroup($group) { - if ($group === false) { - return ''; - } - - $group['y'] = $this->getCSSY($group['y'] * $this->multiply); - $group['x'] = $this->getCSSX($group['x'] * $this->multiply); - - $class = array('g'); - if (!is_null($group['color'])) { - $class[] = 'c' . $group['color']; - } - if (!is_null($group['size'])) { - $class[] = 's' . $group['size']; - } - if (!is_null($group['font'])) { - $class[] = 'f' . $group['font']; - } - if (!is_null($group['x'])) { - $class[] = 'x' . $group['x']; - } - if (!is_null($group['y'])) { - $class[] = 'y' . $group['y']; - } - if (!is_null($group['letterspacing'])) { - $class[] = 'l' . $group['letterspacing']; - } - if (!is_null($group['wordspacing'])) { - $class[] = 'w' . $group['wordspacing']; - } - $class = implode(' ', $class); - - $res = '
'; - foreach ($group['spans'] as $span) { - $res.=$this->writeSpan($span); - } - $res.='
'; - return $res; - } - - protected function writeSpan($span) { - if ($span === false) { - return ''; - } - - return self::escapeHTML($span['text']); - } - - protected function writeCSS($file) { - $res = array(); - - // General theme - $h = $this->book->parametres->height . 'px'; - $w2 = ($this->book->parametres->width * 2) . 'px'; - $w = $this->book->parametres->width . 'px'; - $wm = ($this->book->parametres->width * $this->multiply) . 'px'; - $hm = ($this->book->parametres->height * $this->multiply) . 'px'; - - - $navTop = ($this->book->parametres->height - 40 - 100) / 2; - $res[] = '.portrait #pages,.portrait .doublePage.page,.page,.portrait #shadow,#shadow.single{width:' . $w . ';max-width:' . $w . ';height:' . $h . ';max-height:' . $h . '}'; - $res[] = '.background{' . $this->writeCSSUA('transform-origin', 'top left') . ';}'; - foreach (self::$resolutions as $r) { - $ratio = 72 / $r; - $wr = $this->book->parametres->width / $ratio; - $hr = $this->book->parametres->height / $ratio; - - $br = '.background.r' . $r . '{'; - if ($ratio != 1) { - $br.=$this->writeCSSUA('transform', 'scale(' . $ratio . ')') . ';'; - } - $br.='width:' . $wr . 'px;height:' . $hr . 'px;}'; - $res[] = $br; - } - $res[] = '.doublePage,#pages,.landscape #shadow.double{width:' . $w2 . ';max-width:' . $w2 . ';height:' . $h . ';max-height:' . $h . '}'; - $res[] = '.landscape #shadow.single.right{left: ' . $w . ';}'; - $res[] = '.landscape .page.right{left:' . $w . '}'; - $texts = '.texts{' . $this->writeCSSUA('transform-origin', 'top left') . ';'; - $texts.=$this->writeCSSUA('transform', 'scale(' . (1 / $this->multiply) . ')') . ';'; - $texts.='width:' . $wm . '; max-width:' . $wm . ';'; - $texts.='height:' . $hm . '; max-height:' . $hm . ';'; - $texts.='}'; - $res[] = $texts; - - // Theme - // Background - $body = '#main{'; - $body.='background-color:#' . $this->theme->parametres->backgroundColor . ';'; - switch ($this->theme->parametres->repeat) { - case wsTheme::REPEAT: - $body.='background-repeat:repeat;'; - break; - case wsTheme::NONE: - $body.='background-repeat:no-repeat;'; - break; - case wsTheme::RATIO: - $body.='background-repeat:no-repeat;'; - break; - case wsTheme::STRETCH: - $body.='background-repeat:no-repeat;'; - $body.='background-size:100% 100%;'; - break; - } - if ($this->theme->parametres->backgroundImage != '') { - copy($this->themeRoot . '/' . $this->theme->parametres->backgroundImage, $this->vdir . '/data/images/' . $this->theme->parametres->backgroundImage); - $body.='background-image:url(../images/' . $this->theme->parametres->backgroundImage . ');'; - $body.='background-position:'; - - - switch ($this->theme->parametres->backgroundVAlign) { - case wsTheme::TOP: - $body.='top'; - break; - case wsTheme::MIDDLE: - $body.='center'; - break; - case wsTheme::BOTTOM: - $body.='bottom'; - break; - } - $body.=' '; - switch ($this->theme->parametres->backgroundHAlign) { - case wsTheme::LEFT: - $body.='left'; - break; - case wsTheme::CENTER: - $body.='center'; - break; - case wsTheme::RIGHT: - $body.='right'; - break; - } - $body.=';'; - } - - $body.='}'; - $res[] = $body; - - // Header - $header = '#header{'; - $header.='height:' . $this->theme->parametres->menuHeight . 'px;'; - $header.='background-color:' . self::colorToCSS($this->theme->parametres->menuColor) . ';'; - if ($this->theme->parametres->menuImage != '') { - copy($this->themeRoot . '/' . $this->theme->parametres->menuImage, $this->vdir . '/data/images/' . $this->theme->parametres->menuImage); - $header.='background-image:url(../images/' . $this->theme->parametres->menuImage . ');'; - $header.='background-repeat:no-repeat;'; - $header.='background-size:100% ' . $this->theme->parametres->menuHeight . 'px;'; - } - $header.='}'; - $res[] = $header; - - //Icons - $res = array_merge($res, $this->writeIcons()); - - // Logo - $logo = '#logo{'; - if ($this->theme->parametres->logo) { - copy($this->themeRoot . '/' . $this->theme->parametres->logo, $this->vdir . '/data/images/' . $this->theme->parametres->logo); - $dim = getimagesize($this->vdir . '/data/images/' . $this->theme->parametres->logo); - $logo.='background-image:url(../images/' . $this->theme->parametres->logo . ');width:' . $dim[0] . 'px;height:' . $dim[1] . 'px;'; - } - $logo.='}'; - $res[] = $logo; - - // Credits - $res[] = '#credits,#credits a{color:' . self::colorToCSS($this->theme->parametres->creditsColor) . ';}'; - - // Arrows - $res[] = '#next,#previous{background-color:' . self::colorToCSS($this->theme->parametres->couleurA) . ';}'; - // Book shadow - $shadowColor = self::colorToCSS($this->theme->parametres->bookShadeColor); - if ($shadowColor != 'transparent') { - $res[] = '#shadow{' . $this->writeCSSUA('box-shadow', '0 0 20px ' . $shadowColor) . '}'; - } - - // Pages styles - foreach ($this->cssColor as $color => $index) { - $res[] = '.c' . $index . '{color:#' . $color . '}'; - } - - foreach ($this->cssSize as $size => $index) { - $res[] = '.s' . $index . '{font-size:' . $size . 'px}'; - } - - foreach ($this->cssLetterSpacing as $letterspacing => $index) { - $res[] = '.l' . $index . '{letter-spacing:' . $letterspacing . 'em}'; - } - - foreach ($this->cssWordSpacing as $wordspacing => $index) { - $res[] = '.w' . $index . '{word-spacing:' . $wordspacing . 'em}'; - } - - foreach ($this->cssX as $x => $index) { - $res[] = '.x' . $index . '{left:' . $x . 'px}'; - } - - foreach ($this->cssY as $y => $index) { - $res[] = '.y' . $index . '{top:' . $y . 'px}'; - } - - foreach ($this->cssRotation as $rotation => $index) { - $rotation*= - 1; - - - $to = 'transform-origin:left top;'; - - $css = '.r' . $index . '{'; - $css.=self::writeCSSUA('transform', 'rotate(' . $rotation . 'deg)') . ';'; - $css.=self::writeCSSUA('transform-origin', 'left top'); - $css.='}'; - $res[] = $css; - } - - foreach ($this->cssFont as $font => $index) { - list($font, $document_id) = explode('//', $font); - $res[] = "@font-face{font-family:F" . $index . ";src:url('F" . $index . ".eot');src:url('F" . $index . ".eot?#iefix') format('eot'),url('F" . $index . ".ttf') format('truetype'),url('F" . $index . ".svgz#" . $font . "') format('svgz'),url('F" . $index . ".svg#" . $font . "') format('svg')}"; - $res[] = '.f' . $index . '{font-family:F' . $index . ',Arial,Helvetica}'; - } - - $res = array_chunk($res, 4000); - foreach ($res as $k => $css) { - file_put_contents(sprintf($file, $k), implode("\n", $css)); - } - } - - protected function writeCSSUA($property, $value) { - $res = array(); - foreach (self::$uaPrefixes as $prefix) { - $res[] = $prefix . $property . ':' . $value; - } - return implode(';', $res); - } - - protected function addLine($line, $document_id) { - $res = array(); - foreach ($line->a as $group) { - $res[] = $this->addGroup($group, $document_id); - } - return array('x' => $this->normalizeFloatValue($line['x']), - 'y' => $this->normalizeFloatValue($line['y']), - 'rotation' => $this->getCSSRotation($this->normalizeFloatValue($line['rotation'], 0)), - "groups" => $res); - } - - protected function addGroup($group, $document_id) { - $alpha = intval(substr($group['color'], 1, 2), 16); - if ($alpha == 0) { - return false; - } - - $res = array(); - - $first = true; - foreach ($group->s as $span) { - if ($first) { - $x = $span['x']; - $first = false; - } - $newSpan = $this->addSpan($span, $document_id); - - array_push($res, $newSpan); - } - - return array( - 'color' => $this->getCSSColor($group['color']), - 'size' => $this->getCSSSize($group['size']), - 'font' => $this->getCSSFont($group['font'], $document_id), - 'letterspacing' => $this->getCSSLetterSpacing($group['letterspacing']), - 'wordspacing' => $this->getCSSWordSpacing($group['wordspacing']), - 'y' => ($group['size']) / -1.2, - 'x' => $x, - 'spans' => $res); - } - - protected function addSpan($span, $document_id) { - - $text = (string) $span; - if ($text == '') { - return false; - } - return array('text' => $text); - } - - protected function getCSSSize(&$size) { - $size = $this->normalizeFloatValue($size * $this->multiply, 3); - $sizer = $size; - return $this->getIndex($sizer, $this->cssSize); - } - - protected function getCSSFont($font, $document_id) { - return $this->getIndex($font . "//" . $document_id, $this->cssFont); - } - - protected function getCSSColor($color) { - $color = trim($color, '#'); - if (strlen($color) > 6) { - $color = substr($color, 2, 6); - } - if ($color == '000000') { - return null; - } - return $this->getIndex($color, $this->cssColor); - } - - protected function getCSSLetterSpacing($letterspacing) { - - $letterspacing = $this->normalizeFloatValue($letterspacing, 5); - - if ($letterspacing == 0) { - return null; - } - return $this->getIndex($letterspacing, $this->cssLetterSpacing); - } - - protected function getCSSWordSpacing($wordspacing) { - $wordspacing = $this->normalizeFloatValue($wordspacing, 5); - - if ($wordspacing == 0) { - return null; - } - return $this->getIndex($wordspacing, $this->cssWordSpacing); - } - - protected function getCSSRotation($rotation) { - $rotation = $this->normalizeFloatValue($rotation, 0); - if ($rotation == 0) { - return null; - } - return $this->getIndex($rotation, $this->cssRotation); - } - - protected function getCSSX($x) { - $x = round($x); - if ($x == 0) { - return null; - } - return $this->getIndex($x, $this->cssX); - } - - protected function getCSSY($y) { - $y = round($y); - if ($y == 0) { - return null; - } - return $this->getIndex($y, $this->cssY); - } - - protected function getIndex($value, &$tab) { - $value = (string) $value; - if (isset($tab[$value])) { - return $tab[$value]; - } - $res = $this->base62(count($tab)); - $tab[$value] = $res; - return $res; - } - - protected function normalizeFloatValue($value, $round=3) { - $value = str_replace(',', '.', $value); - $value = (float) $value; - $value = round($value, $round); - return $value; - } - - protected function base62($val) { - $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; - $base = strlen($chars); - $str = ''; - do { - $i = $val % $base; - $str = $chars[$i] . $str; - $val = ($val - $i) / $base; - } while ($val > 0); - return $str; - } - - public static function escapeHTML($in) { - $in = htmlentities($in, ENT_NOQUOTES, "UTF-8"); - $in = str_replace(' ', '', $in); - - return $in; - } - - public function __destruct() { - - } - - public static function colorToCSS($color) { - if (strlen($color) == 6) { - return '#' . $color; - } else { - $alpha = substr($color, 0, 2); - $red = substr($color, 2, 2); - $green = substr($color, 4, 2); - $blue = substr($color, 6, 2); - - $components = array('alpha', 'red', 'green', 'blue'); - foreach ($components as $k => $name) { - $hex = substr($color, $k * 2, 2); - $$name = intval($hex, 16); - } - $alpha/=255; - if ($alpha == 0) { - return 'transparent'; - } elseif ($alpha == 1) { - return '#' . substr($color, 2, 6); - } - return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')'; - } - } - -} - -?> \ No newline at end of file diff --git a/inc/ws/Util/packager/class.ws.packager.php b/inc/ws/Util/packager/class.ws.packager.php index b91988566..00543ecbb 100644 --- a/inc/ws/Util/packager/class.ws.packager.php +++ b/inc/ws/Util/packager/class.ws.packager.php @@ -10,6 +10,7 @@ class wsPackager { protected $version; protected $book_id; protected $themeRoot; + protected $daoBook; public static function package($book_id, $version) { if ($version == 'html') { @@ -40,9 +41,9 @@ class wsPackager { mkdir($this->dir, 0777, true); } - $daoBook = new wsDAOBook($core->con); - $this->book = $daoBook->selectById($book_id); - $this->pages = $daoBook->getPagesOfBook($book_id); + $this->daoBook = new wsDAOBook($core->con); + $this->book = $this->daoBook->selectById($book_id); + $this->pages = $this->daoBook->getPagesOfBook($book_id); $daoTheme = new wsDAOTheme($core->con); $this->theme = $daoTheme->getThemeOfBook($book_id, true); diff --git a/inc/ws/Util/packager/html5/class.ws.packager.html5.php b/inc/ws/Util/packager/html5/class.ws.packager.html5.php new file mode 100644 index 000000000..3defc0aa6 --- /dev/null +++ b/inc/ws/Util/packager/html5/class.ws.packager.html5.php @@ -0,0 +1,578 @@ +version = 'html5'; + } + + protected function preparePackage() { + parent::preparePackage(); + + foreach ($this->pages as $page => $infos) { + $file = WS_DOCS . '/' . $infos['document_id'] . '/html/p' . $infos['document_page'] . '.layout'; + if (file_exists($file)) { + $this->layouts[$page] = simplexml_load_file($file, null, LIBXML_ERR_WARNING); + } + } + + $imagesize = getimagesize(WS_DOCS . '/' . $this->pages[1]['document_id'] . '/html/h72-' . $this->pages[1]['document_page'] . '.jpg'); + $this->pdf2htmlRatio = $imagesize[0] / $this->layouts[1]['width']; + $this->scale = 4; + $this->multiply = $this->pdf2htmlRatio * $this->scale; + + $this->createHTML(); + } + + public function makePackage() { + parent::makePackage(); + return $this->zip(); + } + + protected function createHTML() { + foreach ($this->layouts as $page => $layout) { + $this->div[$page] = array(); + $document_id = $this->pages[$page]['document_id']; + foreach ($layout->l as $line) { + $this->div[$page][] = $this->addLine($line, $document_id); + } + } + mkdir($this->vdir . '/data/images', 0777, true); + mkdir($this->vdir . '/data/contents', 0777, true); + mkdir($this->vdir . '/data/background', 0777, true); + mkdir($this->vdir . '/data/style', 0777, true); + + foreach ($this->div as $n => $page) { + file_put_contents($this->vdir . '/data/contents/p' . $n . '.html', $this->writePage($page)); + } + $this->writeFonts(); + $this->writeImages(); + $this->writeLinks(); + $this->writeCSS($this->vdir . '/data/style/style_%d.css'); + + file_put_contents($this->vdir . '/data/datas.js', $this->writeConfig()); + } + + protected function writeLinks() { + global $core; + $daoDoc = new wsDAODocument($core->con); + $daoDoc->getLinksAndRulers($this->book_id, $links, $rulers); + + foreach ($links as $link) { + fb($link); + } + } + + protected function writeConfig() { + $config = cubeObject::merge($this->book->parametres->toStandardObject(), $this->theme->parametres->toStandardObject()); + $config->id = $this->book->book_id; + return 'DATAS=' . json_encode($config) . ';'; + } + + protected function writeFonts() { + $fext = array('ttf', 'eot', 'svg', 'svgz', 'woff'); + foreach ($this->cssFont as $font => $index) { + list($font, $document_id) = explode('//', $font); + foreach ($fext as $ext) { + copy(WS_DOCS . '/' . $document_id . "/fonts/web/" . $font . '.' . $ext, $this->vdir . '/data/style/F' . $index . '.' . $ext); + } + } + } + + protected function writeIcons() { + $res = array(); + // Get the colors used to colorize graphics + if ($this->theme->parametres->colorizeIcons) { + $couleurI = '#' . $this->theme->parametres->couleurI; + } else { + $couleurI = '#FFFFFF'; + } + $arrowsColor = '#' . $this->theme->parametres->arrowsColor; + // Set the icon list with the color + $icons = array('nav-bookmark' => $couleurI, 'nav-friend' => $couleurI, 'nav-help' => $couleurI, 'nav-index' => $couleurI, 'nav-sommaire' => $couleurI, + 'next' => $arrowsColor, 'previous' => $arrowsColor, 'search' => $couleurI); + + foreach ($icons as $icon => $color) { + wsTools::colorizeAndRasterizeIcon($this->theme->parametres->iconSet, $icon, $color, $this->vdir . '/data/images/',4, $w, $h); + $res[] = '#icon-' . $icon . '{width:' . $w . 'px;height:' . $h . 'px;background-image:url(data/images/' . $icon . '.svg), url(data/images/' . $icon . '.png);background-repeat:no-repeat;background-size:' . $w . 'px ' . $h . 'px}'; + } + return $res; + } + + protected function writeImages() { + + foreach (self::$resolutions as $r) { + mkdir($this->vdir . '/data/background/' . $r, 0777); + } + foreach ($this->pages as $page => $infos) { + foreach (self::$resolutions as $r) { + copy(WS_DOCS . '/' . $infos['document_id'] . '/html/h' . $r . '-' . $infos['document_page'] . '.jpg', $this->vdir . '/data/background/' . $r . '/p' . $page . '.jpg'); + } + } + } + + protected function writePage($page) { + $res = ''; + foreach ($page as $line) { + $res .= $this->writeLine($line); + } + return $res; + } + + protected function writeLine($line) { + $line['x'] = $this->getCSSX($line['x'] * $this->multiply); + $line['y'] = $this->getCSSY($line['y'] * $this->multiply); + + $class = array('l'); + if (!is_null($line['rotation'])) { + $class[] = 'r' . $line['rotation']; + } + if (!is_null($line['x'])) { + $class[] = 'x' . $line['x']; + } + if (!is_null($line['y'])) { + $class[] = 'y' . $line['y']; + } + $class = implode(' ', $class); + + + $res = '
'; + foreach ($line['groups'] as $group) { + $res.=$this->writeGroup($group); + } + $res.='
'; + return $res; + } + + protected function writeGroup($group) { + if ($group === false) { + return ''; + } + + $group['y'] = $this->getCSSY($group['y'] * $this->multiply); + $group['x'] = $this->getCSSX($group['x'] * $this->multiply); + + $class = array('g'); + if (!is_null($group['color'])) { + $class[] = 'c' . $group['color']; + } + if (!is_null($group['size'])) { + $class[] = 's' . $group['size']; + } + if (!is_null($group['font'])) { + $class[] = 'f' . $group['font']; + } + if (!is_null($group['x'])) { + $class[] = 'x' . $group['x']; + } + if (!is_null($group['y'])) { + $class[] = 'y' . $group['y']; + } + if (!is_null($group['letterspacing'])) { + $class[] = 'l' . $group['letterspacing']; + } + if (!is_null($group['wordspacing'])) { + $class[] = 'w' . $group['wordspacing']; + } + $class = implode(' ', $class); + + $res = '
'; + foreach ($group['spans'] as $span) { + $res.=$this->writeSpan($span); + } + $res.='
'; + return $res; + } + + protected function writeSpan($span) { + if ($span === false) { + return ''; + } + + return self::escapeHTML($span['text']); + } + + protected function writeCSS($file) { + $res = array(); + + // General theme + $h = $this->book->parametres->height . 'px'; + $w2 = ($this->book->parametres->width * 2) . 'px'; + $w = $this->book->parametres->width . 'px'; + $wm = ($this->book->parametres->width * $this->multiply) . 'px'; + $hm = ($this->book->parametres->height * $this->multiply) . 'px'; + + + $navTop = ($this->book->parametres->height - 40 - 100) / 2; + $res[] = '.portrait #pages,.portrait .doublePage.page,.page,.portrait #shadow,#shadow.single{width:' . $w . ';max-width:' . $w . ';height:' . $h . ';max-height:' . $h . '}'; + $res[] = '.background{' . $this->writeCSSUA('transform-origin', 'top left') . ';}'; + foreach (self::$resolutions as $r) { + $ratio = 72 / $r; + $wr = $this->book->parametres->width / $ratio; + $hr = $this->book->parametres->height / $ratio; + + $br = '.background.r' . $r . '{'; + if ($ratio != 1) { + $br.=$this->writeCSSUA('transform', 'scale(' . $ratio . ')') . ';'; + } + $br.='width:' . $wr . 'px;height:' . $hr . 'px;}'; + $res[] = $br; + } + $res[] = '.doublePage,#pages,.landscape #shadow.double{width:' . $w2 . ';max-width:' . $w2 . ';height:' . $h . ';max-height:' . $h . '}'; + $res[] = '.landscape #shadow.single.right{left: ' . $w . ';}'; + $res[] = '.landscape .page.right{left:' . $w . '}'; + $texts = '.texts{' . $this->writeCSSUA('transform-origin', 'top left') . ';'; + $texts.=$this->writeCSSUA('transform', 'scale(' . (1 / $this->multiply) . ')') . ';'; + $texts.='width:' . $wm . '; max-width:' . $wm . ';'; + $texts.='height:' . $hm . '; max-height:' . $hm . ';'; + $texts.='}'; + $res[] = $texts; + + // Theme + // Background + $body = '#main{'; + $body.='background-color:#' . $this->theme->parametres->backgroundColor . ';'; + switch ($this->theme->parametres->repeat) { + case wsTheme::REPEAT: + $body.='background-repeat:repeat;'; + break; + case wsTheme::NONE: + $body.='background-repeat:no-repeat;'; + break; + case wsTheme::RATIO: + $body.='background-repeat:no-repeat;'; + break; + case wsTheme::STRETCH: + $body.='background-repeat:no-repeat;'; + $body.='background-size:100% 100%;'; + break; + } + if ($this->theme->parametres->backgroundImage != '') { + copy($this->themeRoot . '/' . $this->theme->parametres->backgroundImage, $this->vdir . '/data/images/' . $this->theme->parametres->backgroundImage); + $body.='background-image:url(../images/' . $this->theme->parametres->backgroundImage . ');'; + $body.='background-position:'; + + + switch ($this->theme->parametres->backgroundVAlign) { + case wsTheme::TOP: + $body.='top'; + break; + case wsTheme::MIDDLE: + $body.='center'; + break; + case wsTheme::BOTTOM: + $body.='bottom'; + break; + } + $body.=' '; + switch ($this->theme->parametres->backgroundHAlign) { + case wsTheme::LEFT: + $body.='left'; + break; + case wsTheme::CENTER: + $body.='center'; + break; + case wsTheme::RIGHT: + $body.='right'; + break; + } + $body.=';'; + } + + $body.='}'; + $res[] = $body; + + // Header + $header = '#header{'; + $header.='height:' . $this->theme->parametres->menuHeight . 'px;'; + $header.='background-color:' . self::colorToCSS($this->theme->parametres->menuColor) . ';'; + if ($this->theme->parametres->menuImage != '') { + copy($this->themeRoot . '/' . $this->theme->parametres->menuImage, $this->vdir . '/data/images/' . $this->theme->parametres->menuImage); + $header.='background-image:url(../images/' . $this->theme->parametres->menuImage . ');'; + $header.='background-repeat:no-repeat;'; + $header.='background-size:100% ' . $this->theme->parametres->menuHeight . 'px;'; + } + $header.='}'; + $res[] = $header; + + //Icons + $res = array_merge($res, $this->writeIcons()); + + // Logo + $logo = '#logo{'; + if ($this->theme->parametres->logo) { + copy($this->themeRoot . '/' . $this->theme->parametres->logo, $this->vdir . '/data/images/' . $this->theme->parametres->logo); + $dim = getimagesize($this->vdir . '/data/images/' . $this->theme->parametres->logo); + $logo.='background-image:url(../images/' . $this->theme->parametres->logo . ');width:' . $dim[0] . 'px;height:' . $dim[1] . 'px;'; + } + $logo.='}'; + $res[] = $logo; + + // Credits + $res[] = '#credits,#credits a{color:' . self::colorToCSS($this->theme->parametres->creditsColor) . ';}'; + + // Arrows + $res[] = '#next,#previous{background-color:' . self::colorToCSS($this->theme->parametres->couleurA) . ';}'; + // Book shadow + $shadowColor = self::colorToCSS($this->theme->parametres->bookShadeColor); + if ($shadowColor != 'transparent') { + $res[] = '#shadow{' . $this->writeCSSUA('box-shadow', '0 0 20px ' . $shadowColor) . '}'; + } + + // Pages styles + foreach ($this->cssColor as $color => $index) { + $res[] = '.c' . $index . '{color:#' . $color . '}'; + } + + foreach ($this->cssSize as $size => $index) { + $res[] = '.s' . $index . '{font-size:' . $size . 'px}'; + } + + foreach ($this->cssLetterSpacing as $letterspacing => $index) { + $res[] = '.l' . $index . '{letter-spacing:' . $letterspacing . 'em}'; + } + + foreach ($this->cssWordSpacing as $wordspacing => $index) { + $res[] = '.w' . $index . '{word-spacing:' . $wordspacing . 'em}'; + } + + foreach ($this->cssX as $x => $index) { + $res[] = '.x' . $index . '{left:' . $x . 'px}'; + } + + foreach ($this->cssY as $y => $index) { + $res[] = '.y' . $index . '{top:' . $y . 'px}'; + } + + foreach ($this->cssRotation as $rotation => $index) { + $rotation*= - 1; + + + $to = 'transform-origin:left top;'; + + $css = '.r' . $index . '{'; + $css.=self::writeCSSUA('transform', 'rotate(' . $rotation . 'deg)') . ';'; + $css.=self::writeCSSUA('transform-origin', 'left top'); + $css.='}'; + $res[] = $css; + } + + foreach ($this->cssFont as $font => $index) { + list($font, $document_id) = explode('//', $font); + $res[] = "@font-face{font-family:F" . $index . ";src:url('F" . $index . ".eot');src:url('F" . $index . ".eot?#iefix') format('eot'),url('F" . $index . ".ttf') format('truetype'),url('F" . $index . ".svgz#" . $font . "') format('svgz'),url('F" . $index . ".svg#" . $font . "') format('svg')}"; + $res[] = '.f' . $index . '{font-family:F' . $index . ',Arial,Helvetica}'; + } + + $res = array_chunk($res, 4000); + foreach ($res as $k => $css) { + file_put_contents(sprintf($file, $k), implode("\n", $css)); + } + } + + protected function writeCSSUA($property, $value) { + $res = array(); + foreach (self::$uaPrefixes as $prefix) { + $res[] = $prefix . $property . ':' . $value; + } + return implode(';', $res); + } + + protected function addLine($line, $document_id) { + $res = array(); + foreach ($line->a as $group) { + $res[] = $this->addGroup($group, $document_id); + } + return array('x' => $this->normalizeFloatValue($line['x']), + 'y' => $this->normalizeFloatValue($line['y']), + 'rotation' => $this->getCSSRotation($this->normalizeFloatValue($line['rotation'], 0)), + "groups" => $res); + } + + protected function addGroup($group, $document_id) { + $alpha = intval(substr($group['color'], 1, 2), 16); + if ($alpha == 0) { + return false; + } + + $res = array(); + + $first = true; + foreach ($group->s as $span) { + if ($first) { + $x = $span['x']; + $first = false; + } + $newSpan = $this->addSpan($span, $document_id); + + array_push($res, $newSpan); + } + + return array( + 'color' => $this->getCSSColor($group['color']), + 'size' => $this->getCSSSize($group['size']), + 'font' => $this->getCSSFont($group['font'], $document_id), + 'letterspacing' => $this->getCSSLetterSpacing($group['letterspacing']), + 'wordspacing' => $this->getCSSWordSpacing($group['wordspacing']), + 'y' => ($group['size']) / -1.2, + 'x' => $x, + 'spans' => $res); + } + + protected function addSpan($span, $document_id) { + + $text = (string) $span; + if ($text == '') { + return false; + } + return array('text' => $text); + } + + protected function getCSSSize(&$size) { + $size = $this->normalizeFloatValue($size * $this->multiply, 3); + $sizer = $size; + return $this->getIndex($sizer, $this->cssSize); + } + + protected function getCSSFont($font, $document_id) { + return $this->getIndex($font . "//" . $document_id, $this->cssFont); + } + + protected function getCSSColor($color) { + $color = trim($color, '#'); + if (strlen($color) > 6) { + $color = substr($color, 2, 6); + } + if ($color == '000000') { + return null; + } + return $this->getIndex($color, $this->cssColor); + } + + protected function getCSSLetterSpacing($letterspacing) { + + $letterspacing = $this->normalizeFloatValue($letterspacing, 5); + + if ($letterspacing == 0) { + return null; + } + return $this->getIndex($letterspacing, $this->cssLetterSpacing); + } + + protected function getCSSWordSpacing($wordspacing) { + $wordspacing = $this->normalizeFloatValue($wordspacing, 5); + + if ($wordspacing == 0) { + return null; + } + return $this->getIndex($wordspacing, $this->cssWordSpacing); + } + + protected function getCSSRotation($rotation) { + $rotation = $this->normalizeFloatValue($rotation, 0); + if ($rotation == 0) { + return null; + } + return $this->getIndex($rotation, $this->cssRotation); + } + + protected function getCSSX($x) { + $x = round($x); + if ($x == 0) { + return null; + } + return $this->getIndex($x, $this->cssX); + } + + protected function getCSSY($y) { + $y = round($y); + if ($y == 0) { + return null; + } + return $this->getIndex($y, $this->cssY); + } + + protected function getIndex($value, &$tab) { + $value = (string) $value; + if (isset($tab[$value])) { + return $tab[$value]; + } + $res = $this->base62(count($tab)); + $tab[$value] = $res; + return $res; + } + + protected function normalizeFloatValue($value, $round=3) { + $value = str_replace(',', '.', $value); + $value = (float) $value; + $value = round($value, $round); + return $value; + } + + protected function base62($val) { + $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; + $base = strlen($chars); + $str = ''; + do { + $i = $val % $base; + $str = $chars[$i] . $str; + $val = ($val - $i) / $base; + } while ($val > 0); + return $str; + } + + public static function escapeHTML($in) { + $in = htmlentities($in, ENT_NOQUOTES, "UTF-8"); + $in = str_replace(' ', '', $in); + + return $in; + } + + public function __destruct() { + + } + + public static function colorToCSS($color) { + if (strlen($color) == 6) { + return '#' . $color; + } else { + $alpha = substr($color, 0, 2); + $red = substr($color, 2, 2); + $green = substr($color, 4, 2); + $blue = substr($color, 6, 2); + + $components = array('alpha', 'red', 'green', 'blue'); + foreach ($components as $k => $name) { + $hex = substr($color, $k * 2, 2); + $$name = intval($hex, 16); + } + $alpha/=255; + if ($alpha == 0) { + return 'transparent'; + } elseif ($alpha == 1) { + return '#' . substr($color, 2, 6); + } + return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')'; + } + } + +} + +?> \ No newline at end of file