BCGFontFile::setText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
4
/**
5
 *--------------------------------------------------------------------
6
 *
7
 * Holds font family and size.
8
 *
9
 *--------------------------------------------------------------------
10
 * Copyright (C) Jean-Sebastien Goupil
11
 * http://www.barcodephp.com
12
 */
13
class BCGFontFile implements BCGFont {
14
    const PHP_BOX_FIX = 0;
15
16
    private $path;
17
    private $size;
18
    private $text = '';
19
    private $foregroundColor;
20
    private $rotationAngle;
21
    private $box;
22
    private $boxFix;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param string $fontPath path to the file
28
     * @param int $size size in point
29
     */
30
    public function __construct($fontPath, $size) {
31
        if (!file_exists($fontPath)) {
32
            throw new BCGArgumentException('The font path is incorrect.', 'fontPath');
33
        }
34
35
        $this->path = $fontPath;
36
        $this->size = $size;
37
        $this->foregroundColor = new BCGColor('black');
38
        $this->setRotationAngle(0);
39
        $this->setBoxFix(self::PHP_BOX_FIX);
40
    }
41
42
    /**
43
     * Gets the text associated to the font.
44
     *
45
     * @return string
46
     */
47
    public function getText() {
48
        return $this->text;
49
    }
50
51
    /**
52
     * Sets the text associated to the font.
53
     *
54
     * @param string text
0 ignored issues
show
Bug introduced by
The type tinymeng\code\Gateways\barcode\text 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...
55
     */
56
    public function setText($text) {
57
        $this->text = $text;
58
        $this->box = null;
59
    }
60
61
    /**
62
     * Gets the rotation in degree.
63
     *
64
     * @return int
65
     */
66
    public function getRotationAngle() {
67
        return (360 - $this->rotationAngle) % 360;
68
    }
69
70
    /**
71
     * Sets the rotation in degree.
72
     *
73
     * @param int
74
     */
75
    public function setRotationAngle($rotationAngle) {
76
        $this->rotationAngle = (int)$rotationAngle;
77
        if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
78
            $this->rotationAngle = 0;
79
        }
80
81
        $this->rotationAngle = (360 - $this->rotationAngle) % 360;
82
83
        $this->box = null;
84
    }
85
86
    /**
87
     * Gets the background color.
88
     *
89
     * @return BCGColor
90
     */
91
    public function getBackgroundColor() {
92
    }
93
94
    /**
95
     * Sets the background color.
96
     *
97
     * @param BCGColor $backgroundColor
98
     */
99
    public function setBackgroundColor($backgroundColor) {
100
    }
101
102
    /**
103
     * Gets the foreground color.
104
     *
105
     * @return BCGColor
106
     */
107
    public function getForegroundColor() {
108
        return $this->foregroundColor;
109
    }
110
111
    /**
112
     * Sets the foreground color.
113
     *
114
     * @param BCGColor $foregroundColor
115
     */
116
    public function setForegroundColor($foregroundColor) {
117
        $this->foregroundColor = $foregroundColor;
118
    }
119
120
    /**
121
     * Gets the box fix information.
122
     *
123
     * @return int
124
     */
125
    public function getBoxFix() {
126
        return $this->boxFix;
127
    }
128
129
    /**
130
     * Sets the box fix information.
131
     *
132
     * @param int $value
133
     */
134
    public function setBoxFix($value) {
135
        $this->boxFix = intval($value);
136
    }
137
138
    /**
139
     * Returns the width and height that the text takes to be written.
140
     *
141
     * @return int[]
142
     */
143
    public function getDimension() {
144
        $w = 0.0;
145
        $h = 0.0;
146
        $box = $this->getBox();
147
148
        if ($box !== null) {
149
            $minX = min(array($box[0], $box[2], $box[4], $box[6]));
150
            $maxX = max(array($box[0], $box[2], $box[4], $box[6]));
151
            $minY = min(array($box[1], $box[3], $box[5], $box[7]));
152
            $maxY = max(array($box[1], $box[3], $box[5], $box[7]));
153
154
            $w = $maxX - $minX;
155
            $h = $maxY - $minY;
156
        }
157
158
        $rotationAngle = $this->getRotationAngle();
159
        if ($rotationAngle === 90 || $rotationAngle === 270) {
160
            return array($h + self::PHP_BOX_FIX, $w);
161
        } else {
162
            return array($w + self::PHP_BOX_FIX, $h);
163
        }
164
    }
165
166
    /**
167
     * Draws the text on the image at a specific position.
168
     * $x and $y represent the left bottom corner.
169
     *
170
     * @param resource $im
171
     * @param int $x
172
     * @param int $y
173
     */
174
    public function draw($im, $x, $y) {
175
        $drawingPosition = $this->getDrawingPosition($x, $y);
176
        imagettftext($im, $this->size, $this->rotationAngle, $drawingPosition[0], $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text);
0 ignored issues
show
Bug introduced by
It seems like $drawingPosition[1] can also be of type double; however, parameter $y of imagettftext() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

176
        imagettftext($im, $this->size, $this->rotationAngle, $drawingPosition[0], /** @scrutinizer ignore-type */ $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text);
Loading history...
Bug introduced by
It seems like $drawingPosition[0] can also be of type double; however, parameter $x of imagettftext() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

176
        imagettftext($im, $this->size, $this->rotationAngle, /** @scrutinizer ignore-type */ $drawingPosition[0], $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text);
Loading history...
177
    }
178
179
    private function getDrawingPosition($x, $y) {
180
        $dimension = $this->getDimension();
181
        $box = $this->getBox();
182
        $rotationAngle = $this->getRotationAngle();
183
        if ($rotationAngle === 0) {
184
            $y += abs(min($box[5], $box[7]));
185
        } elseif ($rotationAngle === 90) {
186
            $x += abs(min($box[5], $box[7]));
187
            $y += $dimension[1];
188
        } elseif ($rotationAngle === 180) {
189
            $x += $dimension[0];
190
            $y += abs(max($box[1], $box[3]));
191
        } elseif ($rotationAngle === 270) {
192
            $x += abs(max($box[1], $box[3]));
193
        }
194
195
        return array($x, $y);
196
    }
197
198
    private function getBox() {
199
        if ($this->box === null) {
200
            $gd = imagecreate(1, 1);
201
            $this->box = imagettftext($gd, $this->size, 0, 0, 0, 0, $this->path, $this->text);
202
        }
203
204
        return $this->box;
205
    }
206
}
207
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...