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;
$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');
}
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 = '<div class="' . $class . '">';
+ foreach ($line['groups'] as $group) {
+ $res.=$this->writeGroup($group, $line, $ox);
+ }
+ $res.='</div>';
return $res;
}
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';
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 = '<div class="' . $class . '">';
- $res.=$group['text'];
+ foreach ($group['spans'] as $span) {
+ $res.=$this->writeSpan($span, $line);
+ }
$res.='</div>';
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 = '<div class="' . $class . '">';
+ $res.=self::escapeHTML($span['text']);
+ $res.='</div>';
+ return $res;
}
protected function writeCSS($file, $links) {
$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}';
}
$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;
}
$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);
}
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 = '<span style="left:' . $left . 'px;">' . $s . '</span>';
+ 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) {
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) {
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])) {
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;
}
}
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;