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
|
|
|
|
92
|
1 |
|
$fontPath = $fontResolver($this->getFontFace()); |
93
|
|
|
|
94
|
|
|
// create the text here so it can be measured |
95
|
1 |
|
$fontOverride = new Font($this->getText()); |
|
|
|
|
96
|
|
|
|
97
|
1 |
|
$fontOverride->file($fontPath); |
98
|
1 |
|
$fontOverride->size($this->getFontSize()); |
99
|
1 |
|
$fontOverride->color('#000'); |
100
|
1 |
|
$fontOverride->valign('top'); |
101
|
|
|
|
102
|
1 |
|
$size = $fontOverride->getBoxSize(); |
103
|
|
|
|
104
|
|
|
// if text consists of invisible chars, measured size will be zero, nothing to print |
105
|
1 |
|
if ($size['width'] < 1 || $size['height'] < 1) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$fontCallback = function (&$font) use ($fontOverride) { |
110
|
|
|
// override the font created by Canvas::text() |
111
|
1 |
|
$font = $fontOverride; |
112
|
1 |
|
}; |
113
|
|
|
|
114
|
1 |
|
$canvas = $this->getImageManager()->canvas($size['width'], $size['height'], $this->getTracerColour()); |
115
|
|
|
|
116
|
1 |
|
$canvas->text('', null, null, $fontCallback); |
117
|
|
|
|
118
|
1 |
|
$canvas->resize(round($size['width'] * self::LETTER_SPACING_ADJUSTMENT), $size['height']); |
119
|
|
|
|
120
|
1 |
|
if ($this->getOrientation()) { |
121
|
1 |
|
$canvas->rotate(360 - $this->getOrientation()); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
return $canvas; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: