Passed
Pull Request — master (#30)
by John
02:53
created

FixedTextRenderer::renderLine()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 30
rs 9.7998
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
namespace Graze\CiffRenderer\Field\Renderer;
4
5
use Graze\CiffRenderer\Field\Renderer\AbstractdRenderer;
0 ignored issues
show
Bug introduced by
The type Graze\CiffRenderer\Field...derer\AbstractdRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Intervention\Image\Gd\Font;
7
8
class FixedTextRenderer extends AbstractRenderer
9
{
10
    /**
11
     * Increase Ciff's 'pitch' value to approximate size in pixels.
12
     *
13
     * @var float
14
     */
15
    const FONT_SIZE_MULTIPLIER = 4.2;
16
17
    /**
18
     * @var float
19
     */
20
    const LETTER_SPACING_ADJUSTMENT = 0.97;
21
22
    /**
23
     * @var int
24
     */
25
    const DEFAULT_FONT_SIZE = 13;
26
27
    /**
28
     * @var callable
29
     */
30
    protected $fontResolver;
31
32
    /**
33
     * @param callable $fontResolver
34
     */
35
    public function setFontResolver(callable $fontResolver)
36
    {
37
        $this->fontResolver = $fontResolver;
38
    }
39
40
    /**
41
     * @return callable
42
     */
43
    protected function getFontResolver()
44
    {
45
        return $this->fontResolver;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function getFontFace()
52
    {
53
        return $this->parser->getFontFace();
0 ignored issues
show
Bug introduced by
The method getFontFace() does not exist on Graze\CiffRenderer\Field\Parser\ParserInterface. It seems like you code against a sub-type of Graze\CiffRenderer\Field\Parser\ParserInterface such as Graze\CiffRenderer\Field\Parser\FixedTextParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return $this->parser->/** @scrutinizer ignore-call */ getFontFace();
Loading history...
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    protected function getFontSize()
60
    {
61
        $size = $this->parser->getFontSize();
0 ignored issues
show
Bug introduced by
The method getFontSize() does not exist on Graze\CiffRenderer\Field\Parser\ParserInterface. It seems like you code against a sub-type of Graze\CiffRenderer\Field\Parser\ParserInterface such as Graze\CiffRenderer\Field\Parser\FixedTextParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        /** @scrutinizer ignore-call */ 
62
        $size = $this->parser->getFontSize();
Loading history...
62
        if (!$size) {
63
            $size = self::DEFAULT_FONT_SIZE;
64
        }
65
66
        return $size * self::FONT_SIZE_MULTIPLIER;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getText()
73
    {
74
        return $this->parser->getText();
0 ignored issues
show
Bug introduced by
The method getText() does not exist on Graze\CiffRenderer\Field\Parser\ParserInterface. It seems like you code against a sub-type of Graze\CiffRenderer\Field\Parser\ParserInterface such as Graze\CiffRenderer\Field\Parser\FixedTextParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        return $this->parser->/** @scrutinizer ignore-call */ getText();
Loading history...
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    protected function getOrientation()
81
    {
82
        return $this->parser->getOrientation();
0 ignored issues
show
Bug introduced by
The method getOrientation() does not exist on Graze\CiffRenderer\Field\Parser\ParserInterface. It seems like you code against a sub-type of Graze\CiffRenderer\Field\Parser\ParserInterface such as Graze\CiffRenderer\Field\Parser\FixedTextParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        return $this->parser->/** @scrutinizer ignore-call */ getOrientation();
Loading history...
83
    }
84
85
    /**
86
     * @return Intervention/Image/Image
0 ignored issues
show
Documentation Bug introduced by
The doc comment Intervention/Image/Image at position 0 could not be parsed: Unknown type name 'Intervention/Image/Image' at position 0 in Intervention/Image/Image.
Loading history...
87
     */
88 1
    public function render()
89
    {
90 1
        $fontResolver = $this->getFontResolver();
91
        $fontPath = $fontResolver($this->getFontFace());
92 1
93
        // split text into lines
94
        $lines = explode("\n", $this->getText());
95 1
        $texts = [];
96
        $width = 0;
97 1
        $height = 0;
98 1
        foreach ($lines as $line) {
99 1
            $text = $this->renderLine($fontPath, $line);
100 1
            if (is_null($text)) {
101
                continue;
102 1
            }
103
104
            // store dimensions ready to create a canvas
105 1
            $width = max($width, $text->getWidth());
106
            $height += $text->getHeight();
107
108
            $texts[] = $text;
109 1
        }
110
111 1
        // create a canvas to add each line of text to
112 1
        $canvas = $this->getImageManager()->canvas($width, $height, $this->getTracerColour());
113
114 1
        $offsetX = 0;
115
        $offsetY = 0;
116 1
        foreach ($texts as $text) {
117
            // add the text to the canvas
118 1
            $canvas->insert($text, 'top-left', (int) $offsetX, (int) $offsetY);
119
120 1
            // increase the offset for the next line
121 1
            $offsetY += $text->getHeight();
122 1
        }
123
124 1
        if ($this->getOrientation()) {
125
            $canvas->rotate(360 - $this->getOrientation());
126
        }
127
128
        return $canvas;
129
    }
130
131
    /**
132
     * @param string $fontPath
133
     * @param string $text
134
     * @return Intervention/Image/Image
0 ignored issues
show
Documentation Bug introduced by
The doc comment Intervention/Image/Image at position 0 could not be parsed: Unknown type name 'Intervention/Image/Image' at position 0 in Intervention/Image/Image.
Loading history...
135
     */
136
    private function renderLine($fontPath, $text)
137
    {
138
        // create the text here so it can be measured
139
        $fontOverride = new Font($text);
0 ignored issues
show
Bug introduced by
$text of type string is incompatible with the type Intervention\Image\Strinf expected by parameter $text of Intervention\Image\Gd\Font::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
        $fontOverride = new Font(/** @scrutinizer ignore-type */ $text);
Loading history...
140
141
        $fontOverride->file($fontPath);
142
        $fontOverride->size($this->getFontSize());
143
        $fontOverride->color('#000');
144
        $fontOverride->valign('top');
145
146
        $size = $fontOverride->getBoxSize();
147
148
        // if text consists of invisible chars, measured size will be zero, nothing to print
149
        if ($size['width'] < 1 || $size['height'] < 1) {
150
            return null;
151
        }
152
153
        $fontCallback = function (&$font) use ($fontOverride) {
154
            // override the font created by Canvas::text()
155
            $font = $fontOverride;
156
        };
157
158
        $canvas = $this->getImageManager()->canvas($size['width'], $size['height'], $this->getTracerColour());
159
160
        // no need to pass the text in here as we override the font in the callback
161
        $canvas->text('', null, null, $fontCallback);
162
163
        $canvas->resize(round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
0 ignored issues
show
Bug introduced by
round($size['width'] * s...TER_SPACING_ADJUSTMENT) of type double is incompatible with the type integer expected by parameter $width of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        $canvas->resize(/** @scrutinizer ignore-type */ round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']);
Loading history...
164
165
        return $canvas;
166
    }
167
}
168