TsplTextLine::translate()   B
last analyzed

Complexity

Conditions 7
Paths 17

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 1
b 0
f 0
nc 17
nop 1
dl 0
loc 47
ccs 0
cts 33
cp 0
crap 56
rs 8.5866
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\TextLine;
19
use PhpAidc\LabelPrinter\Emulation\EmulateText;
20
21
final class TsplTextLine
22
{
23
    use EmulateText;
24
25
    public function translate(TextLine $command): iterable
26
    {
27
        if ($command->shouldEmulate()) {
28
            $image = $this->emulate($command, $command->getMaxWidth());
29
30
            yield from (new TsplBitmap())->translate(
31
                new Bitmap($command->getX(), $command->getY(), $image)
32
            );
33
34
            return;
35
        }
36
37
        $size = $command->getFontSize();
38
39
        if (\is_string($size)) {
40
            $size += 0;
41
        }
42
43
        if (empty($size)) {
44
            $size = 1;
45
        }
46
47
        $specifier = \is_float($size) ? '%.2F' : '%d';
48
49
        $format = ['%d', '%d', '"%s"', '%d', $specifier, $specifier];
50
51
        // common parameters
52
        $values = [
53
            $command->getX(),
54
            $command->getY(),
55
            $command->getFontName(),
56
            $command->getRotation()->getDegrees(),
57
            $size,
58
            $size,
59
        ];
60
61
        // Rongta RP 4xx/Atol BP4x  doesn't support alignment and print void
62
        if ($command->getAnchor()) {
63
            $format[] = '%d';
64
            $values[] = (($command->getAnchor()->getValue() % 3) ?: 3);
65
        }
66
67
        // text
68
        $format[] = '"%s"';
69
        $values[] = \str_replace('"', '\["]', $command->getText());
70
71
        yield \vsprintf('TEXT '.\implode(',', $format), $values);
72
    }
73
}
74