From dfb7728d37ea3fde58100d6ffa26c87ef098553e Mon Sep 17 00:00:00 2001 From: smiley Date: Sat, 14 Oct 2023 23:45:14 +0200 Subject: [PATCH] :octocat: removed QROptionsTrait::$markupDark and QROptionsTrait::$markupLight, added QROptionsTrait::$svgUseFillAttributes --- examples/authenticator.php | 31 +++++++++++++-------------- examples/html.php | 2 -- examples/imagickConvertSVGtoPNG.php | 27 +++++++++++------------ examples/imagickImageAsBackground.php | 31 ++++++++++++++++----------- examples/svg.php | 21 ++++++++---------- examples/svgConvertViaCanvas.php | 21 +++++++++--------- src/Output/QRImagick.php | 2 +- src/Output/QRMarkup.php | 2 +- src/Output/QRMarkupSVG.php | 10 +++++++++ src/QROptionsTrait.php | 24 ++++----------------- 10 files changed, 82 insertions(+), 89 deletions(-) diff --git a/examples/authenticator.php b/examples/authenticator.php index 18db37429..27fda392f 100644 --- a/examples/authenticator.php +++ b/examples/authenticator.php @@ -26,30 +26,29 @@ $options = new class extends SettingsContainerAbstract{ * * @see https://github.com/chillerlan/php-authenticator */ -$options->mode = AuthenticatorInterface::TOTP; -$options->digits = 8; -$options->algorithm = AuthenticatorInterface::ALGO_SHA512; +$options->mode = AuthenticatorInterface::TOTP; +$options->digits = 8; +$options->algorithm = AuthenticatorInterface::ALGO_SHA512; /* * QROptionsTrait */ -$options->version = 7; -$options->addQuietzone = false; -$options->outputBase64 = false; -$options->svgAddXmlHeader = false; -$options->cssClass = 'my-qrcode'; -$options->drawLightModules = false; -$options->markupDark = ''; -$options->markupLight = ''; -$options->drawCircularModules = true; -$options->circleRadius = 0.4; -$options->connectPaths = true; -$options->keepAsSquare = [ +$options->version = 7; +$options->addQuietzone = false; +$options->outputBase64 = false; +$options->svgAddXmlHeader = false; +$options->cssClass = 'my-qrcode'; +$options->drawLightModules = false; +$options->svgUseFillAttributes = false; +$options->drawCircularModules = true; +$options->circleRadius = 0.4; +$options->connectPaths = true; +$options->keepAsSquare = [ QRMatrix::M_FINDER_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT_DARK, ]; -$options->svgDefs = ' +$options->svgDefs = ' diff --git a/examples/html.php b/examples/html.php index 283bf1fb0..38d4b7fbe 100644 --- a/examples/html.php +++ b/examples/html.php @@ -19,8 +19,6 @@ $options = new QROptions; $options->version = 5; $options->outputType = QROutputInterface::MARKUP_HTML; $options->cssClass = 'qrcode'; -$options->markupDark = '#555'; -$options->markupLight = '#CCC'; $options->moduleValues = [ // finder QRMatrix::M_FINDER_DARK => '#A71111', // dark (true) diff --git a/examples/imagickConvertSVGtoPNG.php b/examples/imagickConvertSVGtoPNG.php index be1e81863..c5759caed 100644 --- a/examples/imagickConvertSVGtoPNG.php +++ b/examples/imagickConvertSVGtoPNG.php @@ -84,24 +84,23 @@ class SVGConvert extends QRMarkupSVG{ // SVG from the basic example $options = new QROptions; -$options->version = 7; -$options->outputType = QROutputInterface::CUSTOM; -$options->outputInterface = SVGConvert::class; -$options->imagickFormat = 'png32'; -$options->scale = 20; -$options->outputBase64 = false; -$options->drawLightModules = true; -$options->markupDark = ''; -$options->markupLight = ''; -$options->drawCircularModules = true; -$options->circleRadius = 0.4; -$options->connectPaths = true; -$options->keepAsSquare = [ +$options->version = 7; +$options->outputType = QROutputInterface::CUSTOM; +$options->outputInterface = SVGConvert::class; +$options->imagickFormat = 'png32'; +$options->scale = 20; +$options->outputBase64 = false; +$options->drawLightModules = true; +$options->svgUseFillAttributes = false; +$options->drawCircularModules = true; +$options->circleRadius = 0.4; +$options->connectPaths = true; +$options->keepAsSquare = [ QRMatrix::M_FINDER_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT_DARK, ]; -$options->svgDefs = ' +$options->svgDefs = ' diff --git a/examples/imagickImageAsBackground.php b/examples/imagickImageAsBackground.php index fe063f0ee..0c6ae3fc2 100644 --- a/examples/imagickImageAsBackground.php +++ b/examples/imagickImageAsBackground.php @@ -19,6 +19,14 @@ require_once __DIR__.'/../vendor/autoload.php'; class QRImagickImageAsBackground extends QRImagick{ + /** + * @inheritDoc + */ + protected function getDefaultModuleValue(bool $isDark):ImagickPixel{ + // RGBA, adjust opacity to increase contrast + return $this->prepareModuleValue(($isDark) ? '#00000040' : '#ffffffa0'); + } + /** * @inheritDoc */ @@ -81,18 +89,17 @@ class ImageAsBackgroundOptions extends QROptions{ $options = new ImageAsBackgroundOptions; -$options->background = __DIR__.'/background.jpg'; // setting from the augmented options -$options->version = 5; -$options->outputType = QROutputInterface::CUSTOM; -$options->outputInterface = QRImagickImageAsBackground::class; // use the custom output class -$options->eccLevel = EccLevel::H; -$options->outputBase64 = false; -$options->scale = 10; -$options->drawLightModules = true; -$options->markupDark = '#00000040'; // RGBA, adjust opacity to increase contrast -$options->markupLight = '#ffffffa0'; -$options->invertMatrix = false; -$options->quietzoneSize = 1; +$options->background = __DIR__.'/background.jpg'; // setting from the augmented options +$options->version = 5; +$options->outputType = QROutputInterface::CUSTOM; +$options->outputInterface = QRImagickImageAsBackground::class; // use the custom output class +$options->eccLevel = EccLevel::H; +$options->outputBase64 = false; +$options->scale = 10; +$options->drawLightModules = true; +$options->svgUseFillAttributes = false; +$options->invertMatrix = false; +$options->quietzoneSize = 1; // dump the output, with an additional logo diff --git a/examples/svg.php b/examples/svg.php index 3ae578fe1..b0f221d8e 100644 --- a/examples/svg.php +++ b/examples/svg.php @@ -18,21 +18,19 @@ require_once __DIR__.'/../vendor/autoload.php'; $options = new QROptions; -$options->version = 7; -$options->outputType = QROutputInterface::MARKUP_SVG; -$options->outputBase64 = false; +$options->version = 7; +$options->outputType = QROutputInterface::MARKUP_SVG; +$options->outputBase64 = false; // if set to false, the light modules won't be rendered -$options->drawLightModules = true; -// empty the default value to remove the fill* and opacity* attributes from the elements -$options->markupDark = ''; -$options->markupLight = ''; +$options->drawLightModules = true; +$options->svgUseFillAttributes = false; // draw the modules as circles isntead of squares -$options->drawCircularModules = true; -$options->circleRadius = 0.4; +$options->drawCircularModules = true; +$options->circleRadius = 0.4; // connect paths -$options->connectPaths = true; +$options->connectPaths = true; // keep modules of these types as square -$options->keepAsSquare = [ +$options->keepAsSquare = [ QRMatrix::M_FINDER_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT_DARK, @@ -58,7 +56,6 @@ try{ } catch(Throwable $e){ // handle the exception in whatever way you need - exit($e->getMessage()); } diff --git a/examples/svgConvertViaCanvas.php b/examples/svgConvertViaCanvas.php index 8eb618b3b..530e7a284 100644 --- a/examples/svgConvertViaCanvas.php +++ b/examples/svgConvertViaCanvas.php @@ -18,18 +18,17 @@ require_once __DIR__.'/../vendor/autoload.php'; $options = new QROptions; -$options->version = 7; -$options->outputType = QROutputInterface::MARKUP_SVG; -$options->outputBase64 = false; -$options->svgAddXmlHeader = false; -$options->drawLightModules = false; -$options->markupDark = ''; -$options->markupLight = ''; -$options->drawCircularModules = true; -$options->circleRadius = 0.4; -$options->connectPaths = true; +$options->version = 7; +$options->outputType = QROutputInterface::MARKUP_SVG; +$options->outputBase64 = false; +$options->svgAddXmlHeader = false; +$options->svgUseFillAttributes = false; +$options->drawLightModules = false; +$options->drawCircularModules = true; +$options->circleRadius = 0.4; +$options->connectPaths = true; -$options->keepAsSquare = [ +$options->keepAsSquare = [ QRMatrix::M_FINDER_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT_DARK, diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index 5fd841a71..4ebffea1d 100644 --- a/src/Output/QRImagick.php +++ b/src/Output/QRImagick.php @@ -106,7 +106,7 @@ class QRImagick extends QROutputAbstract{ * @inheritDoc */ protected function getDefaultModuleValue(bool $isDark):ImagickPixel{ - return $this->prepareModuleValue(($isDark) ? $this->options->markupDark : $this->options->markupLight); + return $this->prepareModuleValue(($isDark) ? '#000' : '#fff'); } /** diff --git a/src/Output/QRMarkup.php b/src/Output/QRMarkup.php index d963fdfb3..22551e3e6 100644 --- a/src/Output/QRMarkup.php +++ b/src/Output/QRMarkup.php @@ -65,7 +65,7 @@ abstract class QRMarkup extends QROutputAbstract{ * @inheritDoc */ protected function getDefaultModuleValue(bool $isDark):string{ - return ($isDark) ? $this->options->markupDark : $this->options->markupLight; + return ($isDark) ? '#000' : '#fff'; } /** diff --git a/src/Output/QRMarkupSVG.php b/src/Output/QRMarkupSVG.php index 5a48119f2..cda20e7c4 100644 --- a/src/Output/QRMarkupSVG.php +++ b/src/Output/QRMarkupSVG.php @@ -145,6 +145,16 @@ class QRMarkupSVG extends QRMarkup{ * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path */ protected function path(string $path, int $M_TYPE):string{ + + if($this->options->svgUseFillAttributes){ + return sprintf( + '', + $this->getCssClass($M_TYPE), + $this->getModuleValue($M_TYPE), + $path + ); + } + return sprintf('', $this->getCssClass($M_TYPE), $path); } diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index 0c2e1413c..28be167a6 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -362,17 +362,6 @@ trait QROptionsTrait{ */ protected string $cssClass = 'qrcode'; - /** - * Markup substitute for dark (CSS value) - */ - protected string $markupDark = '#000'; - - /** - * Markup substitute for light (CSS value) - */ - protected string $markupLight = '#fff'; - - /* * QRMarkupSVG settings */ @@ -384,15 +373,6 @@ trait QROptionsTrait{ */ protected bool $svgAddXmlHeader = true; - /** - * SVG path opacity - * - * Sets the value for the SVG "fill-opacity" on a `` element. Only in effect when non-empty values - * for `QROptions::$markupDark` and `QROptions::$markupLight` are given. - * The opacity value is the same for all paths - please use CSS for more sophisticated implementations. - */ - protected float $svgOpacity = 1.0; - /** * Anything in the SVG `` tag * @@ -420,6 +400,10 @@ trait QROptionsTrait{ */ protected string $svgPreserveAspectRatio = 'xMidYMid'; + /** + * Whether to use the SVG `fill` attributes and set them with the given module values + */ + protected bool $svgUseFillAttributes = true; /* * QRString settings