Completed
Push — master ( d7633b...7db5b7 )
by Daniel
02:36
created

FontTrait::setCannotEmbedFontPolicy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * This file is part of the Ghostscript package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ghostscript\Device\DistillerParameters;
9
10
use GravityMedia\Ghostscript\Enum\CannotEmbedFontPolicy;
11
use GravityMedia\Ghostscript\Enum\PdfSettings;
12
13
/**
14
 * The font distiller parameters trait
15
 *
16
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
17
 */
18
trait FontTrait
19
{
20
    /**
21
     * Get argument value
22
     *
23
     * @param string $name
24
     *
25
     * @return string
26
     */
27
    abstract protected function getArgumentValue($name);
28
29
    /**
30
     * Set argument
31
     *
32
     * @param string $argument
33
     *
34
     * @return $this
35
     */
36
    abstract protected function setArgument($argument);
37
38
    /**
39
     * Get PDF settings
40
     *
41
     * @return string
42
     */
43
    abstract public function getPdfSettings();
44
45
    /**
46
     * Get cannot embed font policy
47
     *
48
     * @return string
49
     */
50 View Code Duplication
    public function getCannotEmbedFontPolicy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $value = $this->getArgumentValue('-dCannotEmbedFontPolicy');
53
        if (null === $value) {
54
            switch ($this->getPdfSettings()) {
55
                case PdfSettings::PREPRESS:
56
                    return CannotEmbedFontPolicy::ERROR;
57
                default:
58
                    return CannotEmbedFontPolicy::WARNING;
59
            }
60
        }
61
62
        return substr($value, 1);
63
    }
64
65
    /**
66
     * Set cannot embed font policy
67
     *
68
     * @param string $cannotEmbedFontPolicy
69
     *
70
     * @param \InvalidArgumentException
71
     *
72
     * @return $this
73
     */
74
    public function setCannotEmbedFontPolicy($cannotEmbedFontPolicy)
75
    {
76
        $cannotEmbedFontPolicy = ltrim($cannotEmbedFontPolicy, '/');
77
        if (!in_array($cannotEmbedFontPolicy, CannotEmbedFontPolicy::values())) {
78
            throw new \InvalidArgumentException('Invalid cannot embed font policy argument');
79
        }
80
81
        $this->setArgument(sprintf('-dCannotEmbedFontPolicy=/%s', $cannotEmbedFontPolicy));
82
83
        return $this;
84
    }
85
86
    /**
87
     * Whether to embed all fonts
88
     *
89
     * @return bool
90
     */
91
    public function isEmbedAllFonts()
92
    {
93
        $value = $this->getArgumentValue('-dEmbedAllFonts');
94
        if (null === $value) {
95
            switch ($this->getPdfSettings()) {
96
                case PdfSettings::SCREEN:
97
                    return false;
98
                default:
99
                    return true;
100
            }
101
        }
102
103
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
104
    }
105
106
    /**
107
     * Set embed all fonts flag
108
     *
109
     * @param string $embedAllFonts
110
     *
111
     * @return $this
112
     */
113
    public function setEmbedAllFonts($embedAllFonts)
114
    {
115
        $this->setArgument(sprintf('-dEmbedAllFonts=%s', $embedAllFonts ? 'true' : 'false'));
116
117
        return $this;
118
    }
119
120
    /**
121
     * Get max subset pct
122
     *
123
     * @return int
124
     */
125
    public function getMaxSubsetPct()
126
    {
127
        $value = $this->getArgumentValue('-dMaxSubsetPct');
128
        if (null === $value) {
129
            return 100;
130
        }
131
132
        return intval($value);
133
    }
134
135
    /**
136
     * Set max subset pct
137
     *
138
     * @param int $maxSubsetPct
139
     *
140
     * @return $this
141
     */
142
    public function setMaxSubsetPct($maxSubsetPct)
143
    {
144
        $this->setArgument(sprintf('-dMaxSubsetPct=%s', $maxSubsetPct));
145
146
        return $this;
147
    }
148
149
    /**
150
     * Whether to subset fonts
151
     *
152
     * @return bool
153
     */
154
    public function isSubsetFonts()
155
    {
156
        $value = $this->getArgumentValue('-dSubsetFonts');
157
        if (null === $value) {
158
            return true;
159
        }
160
161
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
162
    }
163
164
    /**
165
     * Set subset fonts flag
166
     *
167
     * @param bool $subsetFonts
168
     *
169
     * @return $this
170
     */
171
    public function setSubsetFonts($subsetFonts)
172
    {
173
        $this->setArgument(sprintf('-dSubsetFonts=%s', $subsetFonts ? 'true' : 'false'));
174
175
        return $this;
176
    }
177
}
178