Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EPS & SVG Rendering usable on international locales #177

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/Renderer/Image/EpsImageBackEnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@
final class EpsImageBackEnd implements ImageBackEndInterface
{
private const PRECISION = 3;
private const S_SAFE = "%1.".self::PRECISION."F";
private const SS_SAFE = self::S_SAFE." ".self::S_SAFE;
private const SSS_SAFE = self::SS_SAFE." ".self::S_SAFE;
private const SSSS_SAFE = self::SS_SAFE." ".self::SS_SAFE;
private const SCALE_FORMAT = "%1\$1.".self::PRECISION."F %1\$1.".self::PRECISION."F s\n";
private const TRANSLATE_FORMAT = self::SS_SAFE." t\n";
private const MOVE_FORMAT = self::SS_SAFE." m";
private const LINE_FORMAT = self::SS_SAFE." l";
private const CURVE_FORMAT = self::SSS_SAFE." ".self::SSS_SAFE." c";
private const GRADIENTH_FORMAT = " /Coords [ ".self::SSSS_SAFE." ]\n";
private const GRADIENTV_FORMAT = " /Coords [ ".self::SSSS_SAFE." ]\n";
private const GRADIENTD_FORMAT = " /Coords [ ".self::SSSS_SAFE." ]\n";
private const GRADIENTID_FORMAT = " /Coords [ ".self::SSSS_SAFE." ]\n";
private const GRADIENTR_FORMAT = " /Coords [ ".self::SS_SAFE." 0 ".self::SSS_SAFE." ]\n";
private const RGBCOLOR_FORMAT = self::SSS_SAFE;
private const CMYKCOLOR_FORMAT = self::SSSS_SAFE;


private ?string $eps;

Expand Down Expand Up @@ -72,7 +89,7 @@
throw new RuntimeException('No image has been started');
}

$this->eps .= sprintf("%1\$s %1\$s s\n", round($size, self::PRECISION));
$this->eps .= sprintf(self::SCALE_FORMAT, round($size, self::PRECISION));

Check warning on line 92 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L92

Added line #L92 was not covered by tests
}

public function translate(float $x, float $y) : void
Expand All @@ -81,7 +98,7 @@
throw new RuntimeException('No image has been started');
}

$this->eps .= sprintf("%s %s t\n", round($x, self::PRECISION), round($y, self::PRECISION));
$this->eps .= sprintf(self::TRANSLATE_FORMAT, round($x, self::PRECISION), round($y, self::PRECISION));

Check warning on line 101 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L101

Added line #L101 was not covered by tests
}

public function rotate(int $degrees) : void
Expand Down Expand Up @@ -173,13 +190,13 @@
case $op instanceof Move:
$fromX = $toX = round($op->getX(), self::PRECISION);
$fromY = $toY = round($op->getY(), self::PRECISION);
$pathData[] = sprintf('%s %s m', $toX, $toY);
$pathData[] = sprintf(self::MOVE_FORMAT, $toX, $toY);

Check warning on line 193 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L193

Added line #L193 was not covered by tests
break;

case $op instanceof Line:
$fromX = $toX = round($op->getX(), self::PRECISION);
$fromY = $toY = round($op->getY(), self::PRECISION);
$pathData[] = sprintf('%s %s l', $toX, $toY);
$pathData[] = sprintf(self::LINE_FORMAT, $toX, $toY);

Check warning on line 199 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L199

Added line #L199 was not covered by tests
break;

case $op instanceof EllipticArc:
Expand All @@ -193,7 +210,7 @@
$y2 = round($op->getY2(), self::PRECISION);
$fromX = $x3 = round($op->getX3(), self::PRECISION);
$fromY = $y3 = round($op->getY3(), self::PRECISION);
$pathData[] = sprintf('%s %s %s %s %s %s c', $x1, $y1, $x2, $y2, $x3, $y3);
$pathData[] = sprintf(self::CURVE_FORMAT, $x1, $y1, $x2, $y2, $x3, $y3);

Check warning on line 213 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L213

Added line #L213 was not covered by tests
break;

case $op instanceof Close:
Expand Down Expand Up @@ -268,7 +285,7 @@
switch ($gradient->getType()) {
case GradientType::HORIZONTAL():
$this->eps .= sprintf(
" /Coords [ %s %s %s %s ]\n",
self::GRADIENTH_FORMAT,

Check warning on line 288 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L288

Added line #L288 was not covered by tests
round($x, self::PRECISION),
round($y, self::PRECISION),
round($x + $width, self::PRECISION),
Expand All @@ -278,7 +295,7 @@

case GradientType::VERTICAL():
$this->eps .= sprintf(
" /Coords [ %s %s %s %s ]\n",
self::GRADIENTV_FORMAT,

Check warning on line 298 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L298

Added line #L298 was not covered by tests
round($x, self::PRECISION),
round($y, self::PRECISION),
round($x, self::PRECISION),
Expand All @@ -288,7 +305,7 @@

case GradientType::DIAGONAL():
$this->eps .= sprintf(
" /Coords [ %s %s %s %s ]\n",
self::GRADIENTD_FORMAT,

Check warning on line 308 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L308

Added line #L308 was not covered by tests
round($x, self::PRECISION),
round($y, self::PRECISION),
round($x + $width, self::PRECISION),
Expand All @@ -298,7 +315,7 @@

case GradientType::INVERSE_DIAGONAL():
$this->eps .= sprintf(
" /Coords [ %s %s %s %s ]\n",
self::GRADIENTID_FORMAT,

Check warning on line 318 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L318

Added line #L318 was not covered by tests
round($x, self::PRECISION),
round($y + $height, self::PRECISION),
round($x + $width, self::PRECISION),
Expand All @@ -311,7 +328,7 @@
$centerY = ($y + $height) / 2;

$this->eps .= sprintf(
" /Coords [ %s %s 0 %s %s %s ]\n",
self::GRADIENTR_FORMAT,

Check warning on line 331 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L331

Added line #L331 was not covered by tests
round($centerX, self::PRECISION),
round($centerY, self::PRECISION),
round($centerX, self::PRECISION),
Expand Down Expand Up @@ -351,12 +368,12 @@
private function getColorString(ColorInterface $color) : string
{
if ($color instanceof Rgb) {
return sprintf('%s %s %s', $color->getRed() / 255, $color->getGreen() / 255, $color->getBlue() / 255);
return sprintf(self::RGBCOLOR_FORMAT, $color->getRed() / 255, $color->getGreen() / 255, $color->getBlue() / 255);

Check warning on line 371 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L371

Added line #L371 was not covered by tests
}

if ($color instanceof Cmyk) {
return sprintf(
'%s %s %s %s',
self::CMYKCOLOR_FORMAT,

Check warning on line 376 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L376

Added line #L376 was not covered by tests
$color->getCyan() / 100,
$color->getMagenta() / 100,
$color->getYellow() / 100,
Expand All @@ -365,7 +382,7 @@
}

if ($color instanceof Gray) {
return sprintf('%s', $color->getGray() / 100);
return sprintf("%1.".self::PRECISION."F", $color->getGray() / 100);

Check warning on line 385 in src/Renderer/Image/EpsImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/EpsImageBackEnd.php#L385

Added line #L385 was not covered by tests
}

return $this->getColorString($color->toCmyk());
Expand Down
15 changes: 11 additions & 4 deletions src/Renderer/Image/SvgImageBackEnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
final class SvgImageBackEnd implements ImageBackEndInterface
{
private const PRECISION = 3;
private const S_SAFE = '%.' . self::PRECISION . 'F';
private const SS_SAFE = self::S_SAFE.' '.self::S_SAFE;
private const SSS_SAFE = self::S_SAFE.' '.self::S_SAFE;
private const SCALE_FORMAT = 'scale(%.' . self::PRECISION . 'F)';
private const TRANSLATE_FORMAT = 'translate(%.' . self::PRECISION . 'F,%.' . self::PRECISION . 'F)';
private const MOVE_FORMAT = 'M'.self::SS_SAFE;
private const LINE_FORMAT = 'L'.self::SS_SAFE;
private const ARC_FORMAT = 'A'.self::SSS_SAFE.' %u %u '.self::SS_SAFE;
private const CURVE_FORMAT = 'C'.self::SSS_SAFE.' '.self::SSS_SAFE;

private ?XMLWriter $xmlWriter;

Expand Down Expand Up @@ -212,23 +219,23 @@
switch (true) {
case $op instanceof Move:
$pathData[] = sprintf(
'M%s %s',
self::MOVE_FORMAT,

Check warning on line 222 in src/Renderer/Image/SvgImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/SvgImageBackEnd.php#L222

Added line #L222 was not covered by tests
round($op->getX(), self::PRECISION),
round($op->getY(), self::PRECISION)
);
break;

case $op instanceof Line:
$pathData[] = sprintf(
'L%s %s',
self::LINE_FORMAT,

Check warning on line 230 in src/Renderer/Image/SvgImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/SvgImageBackEnd.php#L230

Added line #L230 was not covered by tests
round($op->getX(), self::PRECISION),
round($op->getY(), self::PRECISION)
);
break;

case $op instanceof EllipticArc:
$pathData[] = sprintf(
'A%s %s %s %u %u %s %s',
self::ARC_FORMAT,

Check warning on line 238 in src/Renderer/Image/SvgImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/SvgImageBackEnd.php#L238

Added line #L238 was not covered by tests
round($op->getXRadius(), self::PRECISION),
round($op->getYRadius(), self::PRECISION),
round($op->getXAxisAngle(), self::PRECISION),
Expand All @@ -241,7 +248,7 @@

case $op instanceof Curve:
$pathData[] = sprintf(
'C%s %s %s %s %s %s',
self::CURVE_FORMAT,

Check warning on line 251 in src/Renderer/Image/SvgImageBackEnd.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Image/SvgImageBackEnd.php#L251

Added line #L251 was not covered by tests
round($op->getX1(), self::PRECISION),
round($op->getY1(), self::PRECISION),
round($op->getX2(), self::PRECISION),
Expand Down
Loading