From 090bbeb0f1f3ab0e4e856a9ab3c118d8b09a440d Mon Sep 17 00:00:00 2001 From: codemasher Date: Fri, 1 Mar 2019 15:26:29 +0100 Subject: [PATCH] :octocat: QRMarkup::svg() removed width/height in favor of viewBox #30 --- examples/svg.php | 2 +- src/Output/QRMarkup.php | 17 +++++++++++------ src/QROptions.php | 1 + src/QROptionsTrait.php | 11 +++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/svg.php b/examples/svg.php index a89f6440b..a7a159d70 100644 --- a/examples/svg.php +++ b/examples/svg.php @@ -21,7 +21,7 @@ $options = new QROptions([ 'version' => 7, 'outputType' => QRCode::OUTPUT_MARKUP_SVG, 'eccLevel' => QRCode::ECC_L, - 'scale' => 5, + 'svgViewBoxSize' => 530, 'addQuietzone' => true, 'cssClass' => 'my-css-class', 'svgOpacity' => 1.0, diff --git a/src/Output/QRMarkup.php b/src/Output/QRMarkup.php index bc046db9e..7316698f1 100644 --- a/src/Output/QRMarkup.php +++ b/src/Output/QRMarkup.php @@ -24,6 +24,13 @@ class QRMarkup extends QROutputAbstract{ */ protected $defaultMode = QRCode::OUTPUT_MARKUP_SVG; + /** + * @see \sprintf() + * + * @var string + */ + protected $svgHeader = ''; + /** * @return void */ @@ -74,11 +81,9 @@ class QRMarkup extends QROutputAbstract{ * @return string */ protected function svg():string{ - $scale = $this->options->scale; - $length = $this->moduleCount * $scale; $matrix = $this->matrix->matrix(); - $svg = '' + $svg = sprintf($this->svgHeader, $this->options->svgViewBoxSize ?? $this->moduleCount) .$this->options->eol .''.$this->options->svgDefs.'' .$this->options->eol; @@ -97,7 +102,7 @@ class QRMarkup extends QROutputAbstract{ $count++; if($start === null){ - $start = $x * $scale; + $start = $x; } if($row[$x + 1] ?? false){ @@ -106,8 +111,8 @@ class QRMarkup extends QROutputAbstract{ } if($count > 0){ - $len = $count * $scale; - $path .= 'M' .$start. ' ' .($y * $scale). ' h'.$len.' v'.$scale.' h-'.$len.'Z '; + $len = $count; + $path .= 'M' .$start. ' ' .$y. ' h'.$len.' v1 h-'.$len.'Z '; // reset count $count = 0; diff --git a/src/QROptions.php b/src/QROptions.php index 170e02be2..bb6f186e0 100644 --- a/src/QROptions.php +++ b/src/QROptions.php @@ -33,6 +33,7 @@ use chillerlan\Settings\SettingsContainerAbstract; * @property string $cssClass * @property string $svgOpacity * @property string $svgDefs + * @property int $svgViewBoxSize * * @property string $textDark * @property string $textLight diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index aaa65bb9f..e3311312d 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -137,6 +137,17 @@ trait QROptionsTrait{ */ protected $svgDefs = ''; + /** + * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute. + * + * viewBox="0 0 x x" + * + * @see https://css-tricks.com/scale-svg/#article-header-id-3 + * + * @var int + */ + protected $svgViewBoxSize; + /** * string substitute for dark *