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