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