From 81db76e4f4f12896f8f22770c4f7dca6c29cf787 Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Sun, 11 Sep 2011 21:45:25 +0000 Subject: [PATCH] --- inc/ws/Util/html5/class.ws.html5.compiler.php | 241 ++++++++---------- 1 file changed, 108 insertions(+), 133 deletions(-) diff --git a/inc/ws/Util/html5/class.ws.html5.compiler.php b/inc/ws/Util/html5/class.ws.html5.compiler.php index 6b18736a4..4370b5c3c 100644 --- a/inc/ws/Util/html5/class.ws.html5.compiler.php +++ b/inc/ws/Util/html5/class.ws.html5.compiler.php @@ -38,9 +38,10 @@ class wsHTML5Compiler { protected $cssFont = array(); protected $cssWordSpacing = array(); protected $cssLetterSpacing = array(); - protected $cssRotation = array(); + protected $cssTransform = array(); protected $cssX = array(); protected $cssY = array(); + protected $cssWidths = array(); protected $pdf2htmlRatio; protected $scale; protected $multiply; @@ -356,7 +357,7 @@ class wsHTML5Compiler { $formats = array('ttf', 'woff', 'svg'); foreach ($this->cssFont as $font => $infos) { - $index=$infos['index']; + $index = $infos['index']; foreach ($formats as $format) { $fontforge = new cubeCommandLine('convert.pe'); @@ -453,11 +454,20 @@ class wsHTML5Compiler { } protected function writeLine($line) { - $res = ''; - foreach ($line['groups'] as $group) { + $line['y'] = $this->getCSSY($line['y'] * $this->multiply); - $res.=$this->writeGroup($group, $line); + $class = array('l'); + if (!is_null($line['y'])) { + $class[] = 'y' . $line['y']; } + + $class = implode(' ', $class); + + $res = '
'; + foreach ($line['groups'] as $group) { + $res.=$this->writeGroup($group, $line, $ox); + } + $res.='
'; return $res; } @@ -466,11 +476,6 @@ class wsHTML5Compiler { return ''; } - $group['y'] = $this->getCSSY(($group['y'] + $line['y']) * $this->multiply); - $group['x'] = $this->getCSSX(($group['x']) * $this->multiply); - $group['letterspacing'] = $this->getCSSLetterSpacing($group['letterspacing']); - $group['wordspacing'] = $this->getCSSWordSpacing($group['wordspacing']); - $class = array('g'); if ($group['oblique']) { $class[] = 'o'; @@ -485,39 +490,49 @@ class wsHTML5Compiler { if (!is_null($group['font']['index'])) { $class[] = 'f' . $group['font']['index']; } - if (!is_null($group['x'])) { - $class[] = 'x' . $group['x']; - } - if (!is_null($group['y'])) { - $class[] = 'y' . $group['y']; - } - if (!is_null($line['rotation'])) { - $class[] = 'r' . $line['rotation']; - } - if (!is_null($group['letterspacing'])) { - $class[] = 'l' . $group['letterspacing']; - } - if (!is_null($group['wordspacing'])) { - $class[] = 'w' . $group['wordspacing']; - } + $class = implode(' ', $class); $res = '
'; - $res.=$group['text']; + foreach ($group['spans'] as $span) { + $res.=$this->writeSpan($span, $line); + } $res.='
'; return $res; } - protected function writeSpan($span) { + protected function writeSpan($span, $line) { if ($span === false) { return ''; } - if ($span['space']) { - return $span['text']; - } else { - return self::escapeHTML($span['text']); + + $span['x'] = $this->getCSSX(($span['x']) * $this->multiply); + + $class = array('s'); + if ($span['text'] == ' ') { + $class[] = 'b'; } + if (!is_null($span['x'])) { + $class[] = 'x' . $span['x']; + } + if ($span['text'] == ' ') { + $class[] = 'b' . $this->getCSSWidth($span['width'] * $this->multiply); + } + if (!is_null($line['transform'])) { + $class[] = 't' . $line['transform']; + } + + if (!is_null($span['letterspacing'])) { + $class[] = 'l' . $span['letterspacing']; + } + + $class = implode(' ', $class); + + $res = '
'; + $res.=self::escapeHTML($span['text']); + $res.='
'; + return $res; } protected function writeCSS($file, $links) { @@ -698,10 +713,6 @@ class wsHTML5Compiler { $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}'; } @@ -710,17 +721,27 @@ class wsHTML5Compiler { $res[] = '.y' . $index . '{top:' . $y . 'px}'; } - foreach ($this->cssRotation as $rotation => $index) { - $rotation*= - 1; + foreach ($this->cssWidths as $w => $index) { + $res[] = '.b' . $index . '{width:' . $w . 'px}'; + } + + foreach ($this->cssTransform as $transform => $index) { + list($rotation, $scaleX, $scaleY) = explode(',', $transform); - $to = 'transform-origin:left top;'; + $rotation*= - 1; - $css = '.r' . $index . '{'; - $css.=self::writeCSSUA('transform', 'rotate(' . $rotation . 'deg)') . ';'; + $t = array(); + if ($rotation != 0) { + $t[] = 'rotate(' . $rotation . 'deg)'; + } + if ($scaleX != 1 || $scaleY != 1) { + $t[] = 'scale(' . $scaleX . ',' . $scaleY . ')'; + } + + $css = '.t' . $index . '{'; + $css.=self::writeCSSUA('transform', implode(' ', $t)) . ';'; $css.=self::writeCSSUA('transform-origin', 'left top') . ';'; - $css.='margin-top:' . round(cos(deg2rad($rotation)) * -1, 2) . 'em !important;'; - $css.='margin-left:' . round(sin(deg2rad($rotation)), 2) . 'em !important;'; $css.='}'; $res[] = $css; } @@ -753,11 +774,11 @@ class wsHTML5Compiler { $line = $this->sortLine($line); } foreach ($line->a as $group) { - $res = array_merge($res, $this->addGroup($group, $document_id)); + $res[] = $this->addGroup($group, $document_id); } return array( 'y' => $this->normalizeFloatValue($line['y']), - 'rotation' => $this->getCSSRotation($this->normalizeFloatValue($line['rotation'], 0)), + 'transform' => $this->getCSSTransform($line['rotation'], $line['scaleX'], $line['scaleY']), "groups" => $res); } @@ -824,95 +845,31 @@ class wsHTML5Compiler { if ($alpha == 0) { return false; } - $res = array(); - $groups = $this->separateGroups($group); - - foreach ($groups as $g) { - $first = true; - $letterSpacing = 0; - $letters = 0; - $wordSpacing = 0; - $spaces = 0; - - $lastWidth = 0; - $nextX = 0; - $text = ''; - - foreach ($g['spans'] as $span) { - $char = $span['text']; - - if ($first && trim($char) == "") { - continue; - } - if ($first) { - $x = floatval($span['x']); - $first = false; - } - - $text.=$char; - } - - - $ws = floatval($group['wordspacing']); - $ls = floatval($group['letterspacing']); - $size = floatval($group['size']) * $this->fontScale; - - $res[] = array( - 'color' => $this->getCSSColor($group['color']), - 'size' => $this->getCSSSize($size), - 'font' => $this->getCSSFont($group['font'], $group['oblique'] == 'true', $document_id), - 'letterspacing' => $ls, - 'wordspacing' => $ws, - 'y' => 0, - 'x' => $x, - 'oblique' => ($group['oblique'] == 'true'), - 'text' => $text); - } - return $res; - } - - protected function separateGroups($group) { - $res = array(); - - $first = true; - $currentGroup = array('spans' => array()); - $nextX = 0; - $x = 0; - - $lastIsSpace = false; + $spans = array(); foreach ($group->s as $span) { - $char = (string) $span; - - if ($first && trim($char) == "") { - continue; - } - if ($first) { - $x = floatval($span['x']); - $first = false; - } - - if ($char == " ") { - $lastIsSpace = true; - continue; - } - - $sox = floatval($span['x']); - $sx = $sox - $x; - $sw = floatval($span['width']); - - $diff = $sx - $nextX; + $spans[] = $this->addSpan($span); + } - $s = $char; - $left = round($sx * $this->multiply, 2); - $s = '' . $s . ''; + return array( + 'color' => $this->getCSSColor($group['color']), + 'size' => $this->getCSSSize($group['size'] * $this->fontScale), + 'font' => $this->getCSSFont($group['font'], $group['oblique'] == 'true', $document_id), + 'y' => 0, + 'x' => $group['x'], + 'oblique' => ($group['oblique'] == 'true'), + 'spans' => $spans + ); + } - $newSpan = array('text' => $s, 'x' => $sox); - $currentGroup['spans'][] = $newSpan; - } - $res[] = $currentGroup; - return $res; + protected function addSpan($span) { + return array( + 'x' => $span['x'], + 'width' => $span['width'], + 'letterspacing' => $this->getCSSLetterSpacing($span['letterspacing']), + 'text' => (string) $span + ); } protected function getCSSSize($size) { @@ -961,12 +918,21 @@ class wsHTML5Compiler { return $this->getIndex($wordspacing, $this->cssWordSpacing); } - protected function getCSSRotation($rotation) { + protected function getCSSTransform($rotation, $scaleX, $scaleY) { $rotation = $this->normalizeFloatValue($rotation, 0); - if ($rotation == 0) { + $scaleX = $this->normalizeFloatValue($scaleX, 3); + $scaleY = $this->normalizeFloatValue($scaleY, 3); + if ($scaleX < 0) { + $scaleX*=-1; + } + if ($scaleY < 0) { + $scaleY*=-1; + } + + if ($rotation == 0 && $scaleX == 1 && $scaleY = 1) { return null; } - return $this->getIndex($rotation, $this->cssRotation); + return $this->getIndex($rotation . ',' . $scaleX . ',' . $scaleY, $this->cssTransform); } protected function getCSSX($x) { @@ -985,6 +951,14 @@ class wsHTML5Compiler { return $this->getIndex($y, $this->cssY); } + protected function getCSSWidth($w) { + $w = $this->normalizeFloatValue($w, 4); + if ($w == 0) { + return null; + } + return $this->getIndex($w, $this->cssWidths); + } + protected function getIndex($value, &$tab, $params=array()) { $value = (string) $value; if (isset($tab[$value])) { @@ -994,7 +968,7 @@ class wsHTML5Compiler { if (!count($params)) { $tab[$value] = $res; } else { - $tab[$value] = array_merge(array('index'=>$res), $params); + $tab[$value] = array_merge(array('index' => $res), $params); } return $res; } @@ -1051,7 +1025,8 @@ class wsHTML5Compiler { } public static function escapeHTML($in) { - $in = htmlentities($in, ENT_NOQUOTES, "UTF-8"); + + //$in = htmlentities($in, ENT_NOQUOTES, "UTF-8"); $in = str_replace(' ', '', $in); return $in; -- 2.39.5