|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Field\Renderer; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Field\Renderer\AbstractdRenderer; |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getFontSize() |
|
60
|
|
|
{ |
|
61
|
|
|
$size = $this->parser->getFontSize(); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getOrientation() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->parser->getOrientation(); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Intervention/Image/Image |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function render() |
|
89
|
|
|
{ |
|
90
|
1 |
|
$fontResolver = $this->getFontResolver(); |
|
91
|
1 |
|
$fontPath = $fontResolver($this->getFontFace()); |
|
92
|
|
|
|
|
93
|
|
|
// split text into lines |
|
94
|
1 |
|
$lines = explode("\n", $this->getText()); |
|
95
|
1 |
|
$texts = []; |
|
96
|
1 |
|
$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
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// store dimensions ready to create a canvas |
|
105
|
1 |
|
$width = max($width, $text->getWidth()); |
|
106
|
1 |
|
$height += $text->getHeight(); |
|
107
|
|
|
|
|
108
|
1 |
|
$texts[] = $text; |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
// 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
|
1 |
|
$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
|
|
|
// increase the offset for the next line |
|
121
|
1 |
|
$offsetY += $text->getHeight(); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
if ($this->getOrientation()) { |
|
125
|
1 |
|
$canvas->rotate(360 - $this->getOrientation()); |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return $canvas; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $fontPath |
|
133
|
|
|
* @param string $text |
|
134
|
|
|
* @return Intervention/Image/Image |
|
|
|
|
|
|
135
|
|
|
*/ |
|
136
|
1 |
|
private function renderLine($fontPath, $text) |
|
137
|
|
|
{ |
|
138
|
|
|
// create the text here so it can be measured |
|
139
|
1 |
|
$fontOverride = new Font($text); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
1 |
|
$fontOverride->file($fontPath); |
|
142
|
1 |
|
$fontOverride->size($this->getFontSize()); |
|
143
|
1 |
|
$fontOverride->color('#000'); |
|
144
|
1 |
|
$fontOverride->valign('top'); |
|
145
|
|
|
|
|
146
|
1 |
|
$size = $fontOverride->getBoxSize(); |
|
147
|
|
|
|
|
148
|
|
|
// if text consists of invisible chars, measured size will be zero, nothing to print |
|
149
|
1 |
|
if ($size['width'] < 1 || $size['height'] < 1) { |
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
1 |
|
$fontCallback = function (&$font) use ($fontOverride) { |
|
154
|
|
|
// override the font created by Canvas::text() |
|
155
|
1 |
|
$font = $fontOverride; |
|
156
|
1 |
|
}; |
|
157
|
|
|
|
|
158
|
1 |
|
$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
|
1 |
|
$canvas->text('', null, null, $fontCallback); |
|
162
|
|
|
|
|
163
|
1 |
|
$canvas->resize(round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
1 |
|
return $canvas; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths