Skip to content

Support customising the external eye and new EyeStyles #307

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

Open
wants to merge 5 commits into
base: develop
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": ">=7.2|^8.0",
"ext-gd": "*",
"bacon/bacon-qr-code": "^2.0"
"bacon/bacon-qr-code": "^3.0"
},
"require-dev": {
"mockery/mockery": "~1",
Expand Down
87 changes: 87 additions & 0 deletions src/CustomEyes/RoundedSquareEye.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);

namespace SimpleSoftwareIO\QrCode\CustomEyes;

use BaconQrCode\Renderer\Eye\EyeInterface;
use BaconQrCode\Renderer\Path\Path;

/**
* Renders the eyes with a rounded square shape.
*/
final class RoundedSquareEye implements EyeInterface
{
private static ?RoundedSquareEye $instance = null;

private function __construct()
{
}

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

public function getExternalPath(): Path
{
$outerSize = 3.5;
$outerMidPoint = 1.0;

$innerSize = 2.5;
$innerMidPoint = 1.0;

return (new Path())
->move(0, $outerSize)
->line($outerMidPoint, $outerSize)
->curve($outerMidPoint, $outerSize, $outerSize, $outerSize, $outerSize, $outerMidPoint)
->line($outerSize, 0)
->line($outerSize, -$outerMidPoint)
->curve($outerSize, -$outerMidPoint, $outerSize, -$outerSize, $outerMidPoint, -$outerSize)
->line(0, -$outerSize)
->line(-$outerMidPoint, -$outerSize)
->curve(-$outerMidPoint, -$outerSize, -$outerSize, -$outerSize, -$outerSize, -$outerMidPoint)
->line(-$outerSize, 0)
->line(-$outerSize, $outerMidPoint)
->curve(-$outerSize, $outerMidPoint, -$outerSize, $outerSize, -$outerMidPoint, $outerSize)
->line(0, $outerSize)
->close()

->move(0, $innerSize)
->line($innerMidPoint, $innerSize)
->curve($innerMidPoint, $innerSize, $innerSize, $innerSize, $innerSize, $innerMidPoint)
->line($innerSize, 0)
->line($innerSize, -$innerMidPoint)
->curve($innerSize, -$innerMidPoint, $innerSize, -$innerSize, $innerMidPoint, -$innerSize)
->line(0, -$innerSize)
->line(-$innerMidPoint, -$innerSize)
->curve(-$innerMidPoint, -$innerSize, -$innerSize, -$innerSize, -$innerSize, -$innerMidPoint)
->line(-$innerSize, 0)
->line(-$innerSize, $innerMidPoint)
->curve(-$innerSize, $innerMidPoint, -$innerSize, $innerSize, -$innerMidPoint, $innerSize)
->line(0, $innerSize)
->close();
}


public function getInternalPath(): Path
{
$size = 1.5;
$midPoint = 0.75;

return (new Path())
->move(0, $size)
->line($midPoint, $size)
->curve($midPoint, $size, $size, $size, $size, $midPoint)
->line($size, 0)
->line($size, -$midPoint)
->curve($size, -$midPoint, $size, -$size, $midPoint, -$size)
->line(0, -$size)
->line(-$midPoint, -$size)
->curve(-$midPoint, -$size, -$size, -$size, -$size, -$midPoint)
->line(-$size, 0)
->line(-$size, $midPoint)
->curve(-$size, $midPoint, -$size, $size, -$midPoint, $size)
->line(0, $size)
->close();
}
}
69 changes: 62 additions & 7 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use BaconQrCode\Renderer\Color\Alpha;
use BaconQrCode\Renderer\Color\ColorInterface;
use BaconQrCode\Renderer\Color\Rgb;
use BaconQrCode\Renderer\Eye\CompositeEye;
use BaconQrCode\Renderer\Eye\EyeInterface;
use BaconQrCode\Renderer\Eye\ModuleEye;
use BaconQrCode\Renderer\Eye\PointyEye;
use BaconQrCode\Renderer\Eye\SimpleCircleEye;
use BaconQrCode\Renderer\Eye\SquareEye;
use BaconQrCode\Renderer\Image\EpsImageBackEnd;
Expand All @@ -29,6 +31,7 @@
use BaconQrCode\Writer;
use BadMethodCallException;
use InvalidArgumentException;
use SimpleSoftwareIO\QrCode\CustomEyes\RoundedSquareEye;
use SimpleSoftwareIO\QrCode\DataTypes\DataTypeInterface;

class Generator
Expand Down Expand Up @@ -94,13 +97,21 @@ class Generator
protected $styleSize = null;

/**
* The style to apply to the eye.
* Possible values are circle and square.
* The style to apply to the internal eye.
* Possible values are circle, square and rounded.
*
* @var string|null
*/
protected $eyeStyle = null;

/**
* The style to apply to the external eye.
* Possible values are circle, square, pointy and rounded.
*
* @var string|null
*/
protected $externalEyeStyle = null;

/**
* The foreground color of the QrCode.
*
Expand Down Expand Up @@ -334,15 +345,33 @@ public function gradient($startRed, $startGreen, $startBlue, $endRed, $endGreen,
*/
public function eye(string $style): self
{
if (! in_array($style, ['square', 'circle'])) {
throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style.");
if (! in_array($style, ['square', 'circle', 'rounded'])) {
throw new InvalidArgumentException("\$style must be square, rounded or circle. {$style} is not a valid eye style.");
}

$this->eyeStyle = $style;

return $this;
}

/**
* Sets the external eye style.
*
* @param string $style
* @return Generator
* @throws InvalidArgumentException
*/
public function externalEye(string $style): self
{
if (! in_array($style, ['square', 'circle', 'pointy', 'rounded'])) {
throw new InvalidArgumentException("\$style must be square, rounded, pointy or circle. {$style} is not a valid eye style.");
}

$this->externalEyeStyle = $style;

return $this;
}

/**
* Sets the style of the blocks for the QrCode.
*
Expand Down Expand Up @@ -496,15 +525,41 @@ public function getModule(): ModuleInterface
*/
public function getEye(): EyeInterface
{
// defaults
$internalEye = new ModuleEye($this->getModule());
$externalEye = new ModuleEye($this->getModule());

// external eye
if ($this->externalEyeStyle === 'square') {
$externalEye = SquareEye::instance();
}

if ($this->externalEyeStyle === 'circle') {
$externalEye = SimpleCircleEye::instance();
}

if ($this->externalEyeStyle === 'pointy') {
$externalEye = PointyEye::instance();
}

if ($this->externalEyeStyle === 'rounded') {
$externalEye = RoundedSquareEye::instance();
}

// internal eye
if ($this->eyeStyle === 'square') {
return SquareEye::instance();
$internalEye = SquareEye::instance();
}

if ($this->eyeStyle === 'circle') {
return SimpleCircleEye::instance();
$internalEye = SimpleCircleEye::instance();
}

if ($this->eyeStyle === 'rounded') {
$internalEye = RoundedSquareEye::instance();
}

return new ModuleEye($this->getModule());
return new CompositeEye($externalEye, $internalEye);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use BaconQrCode\Renderer\Eye\SimpleCircleEye;
use BaconQrCode\Renderer\Eye\SquareEye;
use BaconQrCode\Renderer\Image\EpsImageBackEnd;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
Expand Down Expand Up @@ -138,19 +136,16 @@ public function test_grandient_is_set()
$this->assertInstanceOf(Gradient::class, $generator->getFill()->getForegroundGradient());
}

public function test_eye_style_is_set()
public function test_invalid_eye_throws_an_exception()
{
$generator = (new Generator)->eye('circle');
$this->assertInstanceOf(SimpleCircleEye::class, $generator->getEye());

$generator = (new Generator)->eye('square');
$this->assertInstanceOf(SquareEye::class, $generator->getEye());
$this->expectException(InvalidArgumentException::class);
(new Generator)->eye('foo');
}

public function test_invalid_eye_throws_an_exception()
public function test_invalid_external_eye_throws_an_exception()
{
$this->expectException(InvalidArgumentException::class);
(new Generator)->eye('foo');
(new Generator)->externalEye('foo');
}

public function test_style_is_set()
Expand Down