1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Renderer; |
4
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Parser\ParserDocument; |
6
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererFactory; |
7
|
|
|
use Intervention\Image\ImageManager; |
8
|
|
|
use \SimpleXMLElement; |
9
|
|
|
|
10
|
|
|
class RendererDocument |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ParserDocument |
14
|
|
|
*/ |
15
|
|
|
private $parserDocument; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var FieldRendererFactory |
19
|
|
|
*/ |
20
|
|
|
private $fieldRendererFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ImageManager |
24
|
|
|
*/ |
25
|
|
|
private $imageManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ParserDocument $parserDocument |
29
|
|
|
* @param FieldRendererFactory $fieldRendererFactory |
30
|
|
|
* @param ImageManager $imageManager |
31
|
|
|
*/ |
32
|
1 |
|
public function __construct( |
33
|
|
|
ParserDocument $parserDocument, |
34
|
|
|
FieldRendererFactory $fieldRendererFactory, |
35
|
|
|
ImageManager $imageManager |
36
|
|
|
) { |
37
|
1 |
|
$this->parserDocument = $parserDocument; |
38
|
1 |
|
$this->fieldRendererFactory = $fieldRendererFactory; |
39
|
1 |
|
$this->imageManager = $imageManager; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param SimpleXMLElement $xml |
44
|
|
|
* @param callable $fontResolver |
45
|
|
|
* @param callable $graphicResolver |
46
|
|
|
* @return \Intervention\Image\Image |
47
|
|
|
*/ |
48
|
1 |
|
public function render( |
49
|
|
|
SimpleXMLElement $xml, |
50
|
|
|
callable $fontResolver = null, |
51
|
|
|
callable $graphicResolver = null |
52
|
|
|
) { |
53
|
1 |
|
$canvas = $this->imageManager->canvas( |
54
|
1 |
|
$this->parserDocument->getWidth($xml), |
55
|
1 |
|
$this->parserDocument->getHeight($xml), |
56
|
1 |
|
'#fff' |
57
|
|
|
); |
58
|
|
|
|
59
|
1 |
|
$fieldParsers = $this->parserDocument->getFieldParsers($xml); |
60
|
1 |
|
foreach ($fieldParsers as $fieldParser) { |
61
|
1 |
|
if (!$fieldParser->isDisplayed()) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$renderer = $this->fieldRendererFactory->getFieldRenderer($fieldParser); |
66
|
1 |
|
$image = $renderer->render( |
67
|
1 |
|
$this->imageManager, |
68
|
1 |
|
$fieldParser, |
69
|
1 |
|
$fontResolver, |
70
|
1 |
|
$graphicResolver |
71
|
|
|
); |
72
|
|
|
|
73
|
1 |
|
if (is_null($image)) { |
74
|
|
|
// nothing to render |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Intervention/Image requires dimensions as ints |
79
|
1 |
|
$positionX = (int) ($fieldParser->getPositionX()); |
80
|
1 |
|
$positionY = (int) ($fieldParser->getPositionY()); |
81
|
|
|
|
82
|
1 |
|
$canvas->insert($image, 'top-left', $positionX, $positionY); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $canvas; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return RendererDocument |
90
|
|
|
*/ |
91
|
|
|
public static function factory() |
92
|
|
|
{ |
93
|
|
|
return new static( |
94
|
|
|
ParserDocument::factory(), |
95
|
|
|
FieldRendererFactory::factory(), |
96
|
|
|
new ImageManager() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|