|
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
|
|
|
const FONT_SIZE_MULTIPLIER = 4.2; |
|
11
|
|
|
|
|
12
|
|
|
const LETTER_SPACING_ADJUSTMENT = 0.97; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var callable |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $fontResolver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param callable $fontResolver |
|
21
|
|
|
*/ |
|
22
|
|
|
public function setFontResolver(callable $fontResolver) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->fontResolver = $fontResolver; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return callable |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function getFontResolver() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->fontResolver; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function getFontFace() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->parser->getFontFace(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return int |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getFontSize() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->parser->getFontSize() * self::FONT_SIZE_MULTIPLIER; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getText() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->parser->getText(); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getOrientation() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->parser->getOrientation(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return Intervention/Image/Image |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function render() |
|
71
|
|
|
{ |
|
72
|
1 |
|
$fontResolver = $this->getFontResolver(); |
|
73
|
|
|
|
|
74
|
1 |
|
$fontPath = $fontResolver($this->getFontFace()); |
|
75
|
|
|
|
|
76
|
|
|
// create the text here so it can be measured |
|
77
|
1 |
|
$fontOverride = new Font($this->getText()); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
1 |
|
$fontOverride->file($fontPath); |
|
80
|
1 |
|
$fontOverride->size($this->getFontSize()); |
|
81
|
1 |
|
$fontOverride->color('#000'); |
|
82
|
1 |
|
$fontOverride->valign('top'); |
|
83
|
|
|
|
|
84
|
1 |
|
$size = $fontOverride->getBoxSize(); |
|
85
|
|
|
|
|
86
|
1 |
|
$fontCallback = function (&$font) use ($fontOverride) { |
|
87
|
|
|
// override the font created by Canvas::text() |
|
88
|
1 |
|
$font = $fontOverride; |
|
89
|
1 |
|
}; |
|
90
|
|
|
|
|
91
|
1 |
|
$canvas = $this->getImageManager()->canvas($size['width'], $size['height'], $this->getTracerColour()); |
|
92
|
|
|
|
|
93
|
1 |
|
$canvas->text('', null, null, $fontCallback); |
|
94
|
|
|
|
|
95
|
1 |
|
$canvas->resize($size['width'] * self::LETTER_SPACING_ADJUSTMENT, $size['height']); |
|
96
|
|
|
|
|
97
|
1 |
|
if ($this->getOrientation()) { |
|
98
|
1 |
|
$canvas->rotate(360 - $this->getOrientation()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $canvas; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
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: