Completed
Push — master ( 6ffc3f...4609e3 )
by Antonio
51:14 queued 46:44
created

EpsWriter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-qrcode-component project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\QrCode\Writer;
13
14
use BaconQrCode\Renderer\Image\Eps;
15
use BaconQrCode\Writer;
16
use Da\QrCode\Contracts\QrCodeInterface;
17
18
class EpsWriter extends AbstractWriter
19
{
20
    /**
21
     * EpsWriter constructor.
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct(new Eps());
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function writeString(QrCodeInterface $qrCode)
32
    {
33
        /** @var Eps $renderer */
34
        $renderer = $this->renderer;
35
        $renderer->setWidth($qrCode->getSize());
36
        $renderer->setHeight($qrCode->getSize());
37
        $renderer->setMargin(0);
38
        $renderer->setForegroundColor($this->convertColor($qrCode->getForegroundColor()));
39
        $renderer->setBackgroundColor($this->convertColor($qrCode->getBackgroundColor()));
40
        $writer = new Writer($renderer);
41
        $string = $writer->writeString(
42
            $qrCode->getText(),
43
            $qrCode->getEncoding(),
44
            $this->convertErrorCorrectionLevel($qrCode->getErrorCorrectionLevel())
45
        );
46
        $string = $this->addMargin($string, $qrCode);
47
48
        return $string;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getContentType()
55
    {
56
        return 'image/eps';
57
    }
58
59
    /**
60
     * @param string          $string
61
     * @param QrCodeInterface $qrCode
62
     *
63
     * @return string
64
     */
65
    protected function addMargin($string, QrCodeInterface $qrCode)
66
    {
67
        $targetSize = $qrCode->getSize() + $qrCode->getMargin() * 2;
68
        $lines = explode("\n", $string);
69
        $sourceBlockSize = 0;
70
        $additionalWhitespace = $qrCode->getSize();
71
        foreach ($lines as $line) {
72
            if (preg_match('#[0-9]+ [0-9]+ [0-9]+ [0-9]+ F#i', $line) && strpos(
73
                    $line,
74
                    $qrCode->getSize() . ' ' . $qrCode->getSize() . ' F'
75
                ) === false) {
76
                $parts = explode(' ', $line);
77
                $sourceBlockSize = $parts[2];
78
                $additionalWhitespace = min($additionalWhitespace, $parts[0]);
79
            }
80
        }
81
        $blockCount = ($qrCode->getSize() - 2 * $additionalWhitespace) / $sourceBlockSize;
82
        $targetBlockSize = $qrCode->getSize() / $blockCount;
83
        foreach ($lines as &$line) {
84
            if (strpos($line, 'BoundingBox') !== false) {
85
                $line = '%%BoundingBox: 0 0 ' . $targetSize . ' ' . $targetSize;
86
            } elseif (strpos($line, $qrCode->getSize() . ' ' . $qrCode->getSize() . ' F') !== false) {
87
                $line = '0 0 ' . $targetSize . ' ' . $targetSize . ' F';
88
            } elseif (preg_match('#[0-9]+ [0-9]+ [0-9]+ [0-9]+ F#i', $line)) {
89
                $parts = explode(' ', $line);
90
                $parts[0] = $qrCode->getMargin(
91
                    ) + $targetBlockSize * ($parts[0] - $additionalWhitespace) / $sourceBlockSize;
92
                $parts[1] = $qrCode->getMargin(
93
                    ) + $targetBlockSize * ($parts[1] - $sourceBlockSize - $additionalWhitespace) / $sourceBlockSize;
94
                $parts[2] = $targetBlockSize;
95
                $parts[3] = $targetBlockSize;
96
                $line = implode(' ', $parts);
97
            }
98
        }
99
        $string = implode("\n", $lines);
100
101
        return $string;
102
    }
103
}
104