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

Eye rotation doesn't work with inherited colors #174

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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
56 changes: 56 additions & 0 deletions src/Renderer/Eye/PointyEye.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types = 1);

namespace BaconQrCode\Renderer\Eye;

use BaconQrCode\Renderer\Path\Path;

/**
* Renders the outer eye as solid with a curved corner and inner eye as a circle.
*/
final class PointyEye implements EyeInterface
{
/**
* @var self|null
*/
private static $instance;

private function __construct()
{
}

public static function instance() : self
{
return self::$instance ?: self::$instance = new self();
}

public function getExternalPath() : Path
{
return (new Path())
->move(-3.5, 3.5)
->line(-3.5, 0)
->ellipticArc(3.5, 3.5, 0, false, true, 0, -3.5)
->line(3.5, -3.5)
->line(3.5, 3.5)
->close()
->move(2.5, 0)
->ellipticArc(2.5, 2.5, 0, false, true, 0, 2.5)
->ellipticArc(2.5, 2.5, 0, false, true, -2.5, 0)
->ellipticArc(2.5, 2.5, 0, false, true, 0, -2.5)
->ellipticArc(2.5, 2.5, 0, false, true, 2.5, 0)
->close()
;
}

public function getInternalPath() : Path
{
return (new Path())
->move(1.5, 0)
->ellipticArc(1.5, 1.5, 0., false, true, 0., 1.5)
->ellipticArc(1.5, 1.5, 0., false, true, -1.5, 0.)
->ellipticArc(1.5, 1.5, 0., false, true, 0., -1.5)
->ellipticArc(1.5, 1.5, 0., false, true, 1.5, 0.)
->close()
;
}
}
16 changes: 12 additions & 4 deletions src/Renderer/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@
) : Path {
if ($fill->inheritsBothColors()) {
return $modulePath
->append($externalPath->translate($xTranslation, $yTranslation))
->append($internalPath->translate($xTranslation, $yTranslation));
->append(
$externalPath->rotate($rotation)->translate($xTranslation, $yTranslation)
)
->append(
$internalPath->rotate($rotation)->translate($xTranslation, $yTranslation)
);
}

$this->imageBackEnd->push();
Expand All @@ -124,13 +128,17 @@
}

if ($fill->inheritsExternalColor()) {
$modulePath = $modulePath->append($externalPath->translate($xTranslation, $yTranslation));
$modulePath = $modulePath->append(
$externalPath->rotate($rotation)->translate($xTranslation, $yTranslation)

Check warning on line 132 in src/Renderer/ImageRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/ImageRenderer.php#L131-L132

Added lines #L131 - L132 were not covered by tests
);
} else {
$this->imageBackEnd->drawPathWithColor($externalPath, $fill->getExternalColor());
}

if ($fill->inheritsInternalColor()) {
$modulePath = $modulePath->append($internalPath->translate($xTranslation, $yTranslation));
$modulePath = $modulePath->append(
$internalPath->rotate($rotation)->translate($xTranslation, $yTranslation)

Check warning on line 140 in src/Renderer/ImageRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/ImageRenderer.php#L139-L140

Added lines #L139 - L140 were not covered by tests
);
} else {
$this->imageBackEnd->drawPathWithColor($internalPath, $fill->getInternalColor());
}
Expand Down
8 changes: 8 additions & 0 deletions src/Renderer/Path/Close.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public function translate(float $x, float $y) : OperationInterface
{
return $this;
}

/**
* @return self
*/
public function rotate(int $degrees) : OperationInterface
{
return $this;
}
}
24 changes: 24 additions & 0 deletions src/Renderer/Path/Curve.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@
$this->y3 + $y
);
}

/**
* @return self
*/
public function rotate(int $degrees) : OperationInterface

Check warning on line 66 in src/Renderer/Path/Curve.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Path/Curve.php#L66

Added line #L66 was not covered by tests
{
$radians = deg2rad($degrees);
$sin = sin($radians);
$cos = cos($radians);
$x1r = $this->x1 * $cos - $this->y1 * $sin;
$y1r = $this->x1 * $sin + $this->y1 * $cos;
$x2r = $this->x2 * $cos - $this->y2 * $sin;
$y2r = $this->x2 * $sin + $this->y2 * $cos;
$x3r = $this->x3 * $cos - $this->y3 * $sin;
$y3r = $this->x3 * $sin + $this->y3 * $cos;
return new self(
$x1r,
$y1r,
$x2r,
$y2r,
$x3r,
$y3r

Check warning on line 83 in src/Renderer/Path/Curve.php

View check run for this annotation

Codecov / codecov/patch

src/Renderer/Path/Curve.php#L68-L83

Added lines #L68 - L83 were not covered by tests
);
}
}
21 changes: 21 additions & 0 deletions src/Renderer/Path/EllipticArc.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public function translate(float $x, float $y) : OperationInterface
);
}

/**
* @return self
*/
public function rotate(int $degrees) : OperationInterface
{
$radians = deg2rad($degrees);
$sin = sin($radians);
$cos = cos($radians);
$xr = $this->x * $cos - $this->y * $sin;
$yr = $this->x * $sin + $this->y * $cos;
return new self(
$this->xRadius,
$this->yRadius,
$this->xAxisAngle,
$this->largeArc,
$this->sweep,
$xr,
$yr
);
}

/**
* Converts the elliptic arc to multiple curves.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Renderer/Path/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ public function translate(float $x, float $y) : OperationInterface
{
return new self($this->x + $x, $this->y + $y);
}

/**
* @return self
*/
public function rotate(int $degrees) : OperationInterface
{
$radians = deg2rad($degrees);
$sin = sin($radians);
$cos = cos($radians);
$xr = $this->x * $cos - $this->y * $sin;
$yr = $this->x * $sin + $this->y * $cos;
return new self($xr, $yr);
}
}
13 changes: 13 additions & 0 deletions src/Renderer/Path/Move.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ public function translate(float $x, float $y) : OperationInterface
{
return new self($this->x + $x, $this->y + $y);
}

/**
* @return self
*/
public function rotate(int $degrees) : OperationInterface
{
$radians = deg2rad($degrees);
$sin = sin($radians);
$cos = cos($radians);
$xr = $this->x * $cos - $this->y * $sin;
$yr = $this->x * $sin + $this->y * $cos;
return new self($xr, $yr);
}
}
5 changes: 5 additions & 0 deletions src/Renderer/Path/OperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ interface OperationInterface
* Translates the operation's coordinates.
*/
public function translate(float $x, float $y) : self;

/**
* Rotates the operation's coordinates.
*/
public function rotate(int $degrees) : self;
}
11 changes: 11 additions & 0 deletions src/Renderer/Path/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ public function translate(float $x, float $y) : self
return $path;
}

public function rotate(int $degrees) : self
{
$path = new self();

foreach ($this->operations as $operation) {
$path->operations[] = $operation->rotate($degrees);
}

return $path;
}

/**
* @return Traversable<int, OperationInterface>
*/
Expand Down
43 changes: 43 additions & 0 deletions test/Integration/ImagickRenderingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use BaconQrCode\Renderer\Color\Rgb;
use BaconQrCode\Renderer\Eye\SquareEye;
use BaconQrCode\Renderer\Eye\PointyEye;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Module\SquareModule;
Expand Down Expand Up @@ -65,4 +66,46 @@ public function testIssue79() : void
$this->assertMatchesFileSnapshot($tempName);
unlink($tempName);
}

public function testIssue105() : void
{
$squareModule = SquareModule::instance();
$pointyEye = PointyEye::instance();

$renderer1 = new ImageRenderer(
new RendererStyle(
400,
2,
$squareModule,
$pointyEye,
Fill::uniformColor(new Rgb(255, 255, 255), new Rgb(0, 0, 255))
),
new ImagickImageBackEnd()
);
$writer1 = new Writer($renderer1);
$tempName1 = tempnam(sys_get_temp_dir(), 'test') . '.png';
$writer1->writeFile('rotation without eye color', $tempName1);

$this->assertMatchesFileSnapshot($tempName1);
unlink($tempName1);

$eyeFill = new EyeFill(new Rgb(255, 0, 0), new Rgb(0, 255, 0));

$renderer2 = new ImageRenderer(
new RendererStyle(
400,
2,
$squareModule,
$pointyEye,
Fill::withForegroundColor(new Rgb(255, 255, 255), new Rgb(0, 0, 255), $eyeFill, $eyeFill, $eyeFill)
),
new ImagickImageBackEnd()
);
$writer2 = new Writer($renderer2);
$tempName2 = tempnam(sys_get_temp_dir(), 'test') . '.png';
$writer2->writeFile('rotation with eye color', $tempName2);

$this->assertMatchesFileSnapshot($tempName2);
unlink($tempName2);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading