Skip to content

Commit 79a523f

Browse files
ssddanbrownbsweeney
authored andcommitted
Added additional level-3 absolute lengths
Also re-ordered units in style a little and updated rect to have aligned percent handling for width and height.
1 parent 2ea1cb6 commit 79a523f

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

src/Svg/Style.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,23 +408,52 @@ static function convertSize($size, $referenceSize = 11.0, $dpi = 96.0) {
408408
return floatval(substr($size, 0, $pos));
409409
}
410410

411+
if ($pos = strpos($size, "%")) {
412+
return $referenceSize * substr($size, 0, $pos) / 100;
413+
}
414+
411415
if ($pos = strpos($size, "pt")) {
412-
return floatval(substr($size, 0, $pos));
416+
$val = floatval(substr($size, 0, $pos));
417+
return ($val * $dpi) / 72;
413418
}
414419

415420
if ($pos = strpos($size, "cm")) {
416-
return floatval(substr($size, 0, $pos)) * $dpi;
421+
$val = floatval(substr($size, 0, $pos));
422+
return ($val * $dpi) / 2.54;
417423
}
418424

419-
if ($pos = strpos($size, "%")) {
420-
return $referenceSize * substr($size, 0, $pos) / 100;
425+
if ($pos = strpos($size, "mm")) {
426+
$val = floatval(substr($size, 0, $pos));
427+
return (($val * $dpi) / 2.54) / 10;
428+
}
429+
430+
if ($pos = strpos($size, "q")) {
431+
$val = floatval(substr($size, 0, $pos));
432+
return (($val * $dpi) / 2.54) / 40;
433+
}
434+
435+
if ($pos = strpos($size, "in")) {
436+
$val = floatval(substr($size, 0, $pos));
437+
return ($val * $dpi);
438+
}
439+
440+
if ($pos = strpos($size, "pc")) {
441+
$val = floatval(substr($size, 0, $pos));
442+
return ($val * $dpi) / 6;
421443
}
422444

423445
if ($pos = strpos($size, "em")) {
424-
return $referenceSize * substr($size, 0, $pos);
446+
$val = floatval(substr($size, 0, $pos));
447+
return $referenceSize * $val;
425448
}
426449

427-
// TODO cm, mm, pc, in, etc
450+
// TODO - vmin
451+
// TODO - vmax
452+
// TODO - vh
453+
// TODO - vw
454+
// TODO - rem
455+
// TODO - ch
456+
// TODO - ex
428457

429458
return null;
430459
}

src/Svg/Tag/Rect.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,21 @@ class Rect extends Shape
2121

2222
public function start($attributes)
2323
{
24+
$width = $this->document->getWidth();
25+
$height = $this->document->getHeight();
26+
2427
if (isset($attributes['x'])) {
25-
$width = $this->document->getWidth();
2628
$this->x = Style::convertSize($attributes['x'], $width);
2729
}
2830
if (isset($attributes['y'])) {
29-
$height = $this->document->getHeight();
3031
$this->y = Style::convertSize($attributes['y'], $height);
3132
}
3233

3334
if (isset($attributes['width'])) {
34-
if ('%' === substr($attributes['width'], -1)) {
35-
$factor = substr($attributes['width'], 0, -1) / 100;
36-
$this->width = $this->document->getWidth() * $factor;
37-
} else {
38-
$this->width = $attributes['width'];
39-
}
35+
$this->width = Style::convertSize($attributes['width'], $width);
4036
}
4137
if (isset($attributes['height'])) {
42-
if ('%' === substr($attributes['height'], -1)) {
43-
$factor = substr($attributes['height'], 0, -1) / 100;
44-
$this->height = $this->document->getHeight() * $factor;
45-
} else {
46-
$this->height = $attributes['height'];
47-
}
38+
$this->height = Style::convertSize($attributes['height'], $width);
4839
}
4940

5041
if (isset($attributes['rx'])) {

0 commit comments

Comments
 (0)