TsplTextBlock::translate()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 28
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 43
ccs 0
cts 35
cp 0
crap 42
rs 8.8497
1
<?php
2
3
/**
4
 * This file is part of PhpAidc LabelPrinter package.
5
 *
6
 * © Appwilio (https://appwilio.com)
7
 * © JhaoDa (https://github.com/jhaoda)
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace PhpAidc\LabelPrinter\Language\Tspl;
16
17
use PhpAidc\LabelPrinter\Command\Bitmap;
18
use PhpAidc\LabelPrinter\Command\TextBlock;
19
use PhpAidc\LabelPrinter\Emulation\EmulateText;
20
21
/**
22
 * This printers doesn't support BLOCK:
23
 *  - Atol BP4x
24
 *  - Rongta RP4xx
25
 */
26
final class TsplTextBlock
27
{
28
    use EmulateText;
29
30
    public function translate(TextBlock $command): iterable
31
    {
32
        if ($command->shouldEmulate()) {
33
            $image = $this->emulate(
34
                $command,
35
                $command->getBoxWidth(),
36
                $command->getBoxHeight(),
37
                $command->getSpacing()
38
            );
39
40
            yield from (new TsplBitmap())->translate(
41
                new Bitmap($command->getX(), $command->getY(), $image)
42
            );
43
44
            return;
45
        }
46
47
        $size = $command->getFontSize();
48
49
        if (\is_string($size)) {
50
            $size += 0;
51
        }
52
53
        if (empty($size)) {
54
            $size = 1;
55
        }
56
57
        $specifier = \is_float($size) ? '%.2F' : '%d';
58
59
        $format = "%d,%d,%d,%d,\"%s\",%d,{$specifier},{$specifier},0,%d,0,\"%s\"";
60
61
        yield \vsprintf('BLOCK '.$format, [
62
            $command->getX(),
63
            $command->getY(),
64
            $command->getBoxWidth(),
65
            $command->getBoxHeight(),
66
            $command->getFontName(),
67
            $command->getRotation()->getDegrees(),
68
            $size,
69
            $size,
70
            $command->getSpacing() ?? 0,
71
            $command->getAnchor() ? $command->getAnchor()->getValue() : 0,
72
            \str_replace(['"', "\n", "\r"], ['\["]', '\[L]', '\[R]'], $command->getText()),
73
        ]);
74
    }
75
}
76