1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Renderer\FieldRenderer; |
4
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface; |
6
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface; |
7
|
|
|
use Intervention\Image\ImageManager; |
8
|
|
|
use Intervention\Image\Gd\Font; |
9
|
|
|
|
10
|
|
|
class FieldRendererFixedText implements FieldRendererInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var float |
14
|
|
|
*/ |
15
|
|
|
const LETTER_SPACING_ADJUSTMENT = 0.97; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Font |
19
|
|
|
*/ |
20
|
|
|
private $font; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Font $font |
24
|
|
|
*/ |
25
|
6 |
|
public function __construct(Font $font) |
26
|
|
|
{ |
27
|
6 |
|
$this->font = $font; |
28
|
6 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ImageManager $imageManager |
32
|
|
|
* @param FieldParserInterface $parser |
33
|
|
|
* @param null|callable $fontResolver |
34
|
|
|
* @param null|callable $graphicResolver |
35
|
|
|
* @return \Intervention\Image\Image |
36
|
|
|
*/ |
37
|
1 |
|
public function render( |
38
|
|
|
ImageManager $imageManager, |
39
|
|
|
FieldParserInterface $parser, |
40
|
|
|
callable $fontResolver = null, |
41
|
|
|
callable $graphicResolver = null |
42
|
|
|
) { |
43
|
1 |
|
$fontPath = $fontResolver($parser->getFontFace()); |
44
|
|
|
|
45
|
|
|
// split text into lines |
46
|
1 |
|
$lines = explode("\n", $parser->getText()); |
47
|
1 |
|
$texts = []; |
48
|
1 |
|
$width = 0; |
49
|
1 |
|
$height = 0; |
50
|
1 |
|
foreach ($lines as $line) { |
51
|
1 |
|
$text = $this->renderLine($imageManager, $parser, $fontPath, $line); |
52
|
1 |
|
if (is_null($text)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// store dimensions ready to create a canvas |
57
|
1 |
|
$width = max($width, $text->getWidth()); |
58
|
1 |
|
$height += $text->getHeight(); |
59
|
|
|
|
60
|
1 |
|
$texts[] = $text; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
if (empty($texts)) { |
64
|
|
|
// nothing to render |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// create a canvas to add each line of text to |
69
|
1 |
|
$canvas = $imageManager->canvas($width, $height); |
70
|
|
|
|
71
|
1 |
|
$offsetX = 0; |
72
|
1 |
|
$offsetY = 0; |
73
|
1 |
|
foreach ($texts as $text) { |
74
|
|
|
// add the text to the canvas |
75
|
1 |
|
$canvas->insert($text, 'top-left', (int) $offsetX, (int) $offsetY); |
76
|
|
|
|
77
|
|
|
// increase the offset for the next line |
78
|
1 |
|
$offsetY += $text->getHeight(); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
$orientation = $parser->getOrientation(); |
82
|
1 |
|
if ($orientation) { |
83
|
1 |
|
$canvas->rotate(360 - $orientation); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
return $canvas; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param ImageManager $imageManager |
91
|
|
|
* @param FieldParserInterface $parser |
92
|
|
|
* @param string $fontPath |
93
|
|
|
* @param string $text |
94
|
|
|
* @return \Intervention\Image\Image |
95
|
|
|
*/ |
96
|
1 |
|
private function renderLine(ImageManager $imageManager, FieldParserInterface $parser, $fontPath, $text) |
97
|
|
|
{ |
98
|
|
|
// create the text here so it can be measured |
99
|
1 |
|
$this->font->text($text); |
100
|
|
|
|
101
|
1 |
|
$this->font->file($fontPath); |
102
|
1 |
|
$this->font->size($parser->getFontSize()); |
103
|
1 |
|
$this->font->color('#000'); |
104
|
1 |
|
$this->font->valign('top'); |
105
|
|
|
|
106
|
1 |
|
$size = $this->font->getBoxSize(); |
107
|
|
|
|
108
|
|
|
// if text consists of invisible chars, measured size will be zero, nothing to print |
109
|
1 |
|
if ($size['width'] < 1 || $size['height'] < 1) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
$fontCallback = function (&$font) { |
114
|
|
|
// override the font created by Canvas::text() |
115
|
1 |
|
$font = $this->font; |
116
|
1 |
|
}; |
117
|
|
|
|
118
|
1 |
|
$canvas = $imageManager->canvas($size['width'], $size['height']); |
119
|
|
|
|
120
|
|
|
// no need to pass the text in here as we override the font in the callback |
121
|
1 |
|
$canvas->text('', null, null, $fontCallback); |
122
|
|
|
|
123
|
1 |
|
$width = (int) round($size['width'] * self::LETTER_SPACING_ADJUSTMENT); |
124
|
1 |
|
$canvas->resize($width, $size['height']); |
125
|
|
|
|
126
|
1 |
|
return $canvas; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return FieldRendererInterface |
131
|
|
|
*/ |
132
|
5 |
|
public static function factory() |
133
|
|
|
{ |
134
|
5 |
|
return new static( |
135
|
5 |
|
new Font() |
136
|
5 |
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|