Completed
Push — master ( 0ae2b7...d7633b )
by Daniel
03:14
created

ColorConversionTrait::getColorConversionStrategy()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 8.8571
nc 5
cc 5
eloc 12
nop 0
crap 30
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\Devices\DistillerParameters;
9
10
use GravityMedia\Ghostscript\Enum\ColorConversionStrategy;
11
use GravityMedia\Ghostscript\Enum\DefaultRenderingIntent;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
use GravityMedia\Ghostscript\Enum\TransferFunctionInfo;
14
use GravityMedia\Ghostscript\Enum\UcrAndBgInfo;
15
16
/**
17
 * The color conversion distiller parameters trait
18
 *
19
 * @package GravityMedia\Ghostscript\Devices\DistillerParameters
20
 */
21
trait ColorConversionTrait
22
{
23
    /**
24
     * Get argument value
25
     *
26
     * @param string $name
27
     *
28
     * @return string
29
     */
30
    abstract protected function getArgumentValue($name);
31
32
    /**
33
     * Set argument
34
     *
35
     * @param string $argument
36
     *
37
     * @return $this
38
     */
39
    abstract protected function setArgument($argument);
40
41
    /**
42
     * Get PDF settings
43
     *
44
     * @return string
45
     */
46
    abstract public function getPdfSettings();
47
48
    /**
49
     * Get cal CMYK profile
50
     *
51
     * @return null|string
52
     */
53
    public function getCalCmykProfile()
54
    {
55
        $value = $this->getArgumentValue('-dCalCMYKProfile');
56
        if (null === $value) {
57
            return null;
58
        }
59
60
        return substr($value, 1, -1);
61
    }
62
63
    /**
64
     * Set cal CMYK profile
65
     *
66
     * @param string $valCmykProfile
67
     *
68
     * @return $this
69
     */
70
    public function setCalCmykProfile($valCmykProfile)
71
    {
72
        $this->setArgument(sprintf('-dCalCMYKProfile=(%s)', $valCmykProfile));
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get cal gray profile
79
     *
80
     * @return null|string
81
     */
82
    public function getCalGrayProfile()
83
    {
84
        $value = $this->getArgumentValue('-dCalGrayProfile');
85
        if (null === $value) {
86
            return null;
87
        }
88
89
        return substr($value, 1, -1);
90
    }
91
92
    /**
93
     * Set cal gray profile
94
     *
95
     * @param string $valGrayProfile
96
     *
97
     * @return $this
98
     */
99
    public function setCalGrayProfile($valGrayProfile)
100
    {
101
        $this->setArgument(sprintf('-dCalGrayProfile=(%s)', $valGrayProfile));
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get cal RGB profile
108
     *
109
     * @return null|string
110
     */
111
    public function getCalRgbProfile()
112
    {
113
        $value = $this->getArgumentValue('-dCalRGBProfile');
114
        if (null === $value) {
115
            return null;
116
        }
117
118
        return substr($value, 1, -1);
119
    }
120
121
    /**
122
     * Set cal RGB profile
123
     *
124
     * @param string $valRgbProfile
125
     *
126
     * @return $this
127
     */
128
    public function setCalRgbProfile($valRgbProfile)
129
    {
130
        $this->setArgument(sprintf('-dCalRGBProfile=(%s)', $valRgbProfile));
131
132
        return $this;
133
    }
134
135
    /**
136
     * Get color conversion strategy
137
     *
138
     * @return string
139
     */
140
    public function getColorConversionStrategy()
141
    {
142
        $value = $this->getArgumentValue('-dColorConversionStrategy');
143
        if (null === $value) {
144
            switch ($this->getPdfSettings()) {
145
                case PdfSettings::SCREEN:
146
                case PdfSettings::EBOOK:
147
                    return ColorConversionStrategy::SRGB;
148
                case PdfSettings::PRINTER:
149
                    return ColorConversionStrategy::USE_DEVICE_INDEPENDENT_COLOR;
150
                default:
151
                    return ColorConversionStrategy::LEAVE_COLOR_UNCHANGED;
152
            }
153
        }
154
155
        return substr($value, 1);
156
    }
157
158
    /**
159
     * Set color conversion strategy
160
     *
161
     * @param string $colorConversionStrategy
162
     *
163
     * @throws \InvalidArgumentException
164
     *
165
     * @return $this
166
     */
167
    public function setColorConversionStrategy($colorConversionStrategy)
168
    {
169
        $colorConversionStrategy = ltrim($colorConversionStrategy, '/');
170
        if (!in_array($colorConversionStrategy, ColorConversionStrategy::values())) {
171
            throw new \InvalidArgumentException('Invalid color conversion strategy argument');
172
        }
173
174
        $this->setArgument(sprintf('-dColorConversionStrategy=/%s', $colorConversionStrategy));
175
176
        return $this;
177
    }
178
179
    /**
180
     * Whether to convert CMYK images to RGB
181
     *
182
     * @return bool
183
     */
184
    public function isConvertCmykImagesToRgb()
185
    {
186
        $value = $this->getArgumentValue('-dConvertCMYKImagesToRGB');
187
        if (null === $value) {
188
            return false;
189
        }
190
191
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
192
    }
193
194
    /**
195
     * Set convert convert CMYK images to RGB flag
196
     *
197
     * @param bool $convertCmykImagesToRgb
198
     *
199
     * @return $this
200
     */
201
    public function setConvertCmykImagesToRgb($convertCmykImagesToRgb)
202
    {
203
        $this->setArgument(sprintf('-dConvertCMYKImagesToRGB=%s', $convertCmykImagesToRgb ? 'true' : 'false'));
204
205
        return $this;
206
    }
207
208
    /**
209
     * Whether to convert images to indexed
210
     *
211
     * @return bool
212
     */
213
    public function isConvertImagesToIndexed()
214
    {
215
        $value = $this->getArgumentValue('-dConvertImagesToIndexed');
216
        if (null === $value) {
217
            return false;
218
        }
219
220
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
221
    }
222
223
    /**
224
     * Set convert images to indexed flag
225
     *
226
     * @param bool $convertImagesToIndexed
227
     *
228
     * @return $this
229
     */
230
    public function setConvertImagesToIndexed($convertImagesToIndexed)
231
    {
232
        $this->setArgument(sprintf('-dConvertImagesToIndexed=%s', $convertImagesToIndexed ? 'true' : 'false'));
233
234
        return $this;
235
    }
236
237
    /**
238
     * Get default rendering intent
239
     *
240
     * @return string
241
     */
242
    public function getDefaultRenderingIntent()
243
    {
244
        $value = $this->getArgumentValue('-dDefaultRenderingIntent');
245
        if (null === $value) {
246
            return DefaultRenderingIntent::__DEFAULT;
247
        }
248
249
        return substr($value, 1);
250
    }
251
252
    /**
253
     * Set default rendering intent
254
     *
255
     * @param string $defaultRenderingIntent
256
     *
257
     * @throws \InvalidArgumentException
258
     *
259
     * @return $this
260
     */
261
    public function setDefaultRenderingIntent($defaultRenderingIntent)
262
    {
263
        $defaultRenderingIntent = ltrim($defaultRenderingIntent, '/');
264
        if (!in_array($defaultRenderingIntent, DefaultRenderingIntent::values())) {
265
            throw new \InvalidArgumentException('Invalid default rendering intent argument');
266
        }
267
268
        $this->setArgument(sprintf('-dDefaultRenderingIntent=/%s', $defaultRenderingIntent));
269
270
        return $this;
271
    }
272
273
    /**
274
     * Whether to preserve halftone info
275
     *
276
     * @return bool
277
     */
278
    public function isPreserveHalftoneInfo()
279
    {
280
        $value = $this->getArgumentValue('-dPreserveHalftoneInfo');
281
        if (null === $value) {
282
            return false;
283
        }
284
285
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
286
    }
287
288
    /**
289
     * Set preserve halftone info flag
290
     *
291
     * @param bool $preserveHalftoneInfo
292
     *
293
     * @return $this
294
     */
295
    public function setPreserveHalftoneInfo($preserveHalftoneInfo)
296
    {
297
        $this->setArgument(sprintf('-dPreserveHalftoneInfo=%s', $preserveHalftoneInfo ? 'true' : 'false'));
298
299
        return $this;
300
    }
301
302
    /**
303
     * Whether to preserve overprint settings
304
     *
305
     * @return bool
306
     */
307
    public function isPreserveOverprintSettings()
308
    {
309
        $value = $this->getArgumentValue('-dPreserveOverprintSettings');
310
        if (null === $value) {
311
            switch ($this->getPdfSettings()) {
312
                case PdfSettings::PRINTER:
313
                case PdfSettings::PREPRESS:
314
                    return true;
315
                default:
316
                    return false;
317
            }
318
        }
319
320
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
321
    }
322
323
    /**
324
     * Set preserve overprint settings flag
325
     *
326
     * @param bool $preserveOverprintSettings
327
     *
328
     * @return $this
329
     */
330
    public function setPreserveOverprintSettings($preserveOverprintSettings)
331
    {
332
        $this->setArgument(sprintf('-dPreserveOverprintSettings=%s', $preserveOverprintSettings ? 'true' : 'false'));
333
334
        return $this;
335
    }
336
337
    /**
338
     * Get sRGB profile
339
     *
340
     * @return null|string
341
     */
342
    public function getSRgbProfile()
343
    {
344
        $value = $this->getArgumentValue('-dsRGBProfile');
345
        if (null === $value) {
346
            return null;
347
        }
348
349
        return substr($value, 1, -1);
350
    }
351
352
    /**
353
     * Set sRGB profile
354
     *
355
     * @param string $sRgbProfile
356
     *
357
     * @return $this
358
     */
359
    public function setSRgbProfile($sRgbProfile)
360
    {
361
        $this->setArgument(sprintf('-dsRGBProfile=(%s)', $sRgbProfile));
362
363
        return $this;
364
    }
365
366
    /**
367
     * Get transfer function info
368
     *
369
     * @return string
370
     */
371
    public function getTransferFunctionInfo()
372
    {
373
        $value = $this->getArgumentValue('-dTransferFunctionInfo');
374
        if (null === $value) {
375
            return TransferFunctionInfo::PRESERVE;
376
        }
377
378
        return substr($value, 1);
379
    }
380
381
    /**
382
     * Set transfer function info
383
     *
384
     * @param string $transferFunctionInfo
385
     *
386
     * @throws \InvalidArgumentException
387
     *
388
     * @return $this
389
     */
390
    public function setTransferFunctionInfo($transferFunctionInfo)
391
    {
392
        $transferFunctionInfo = ltrim($transferFunctionInfo, '/');
393
        if (!in_array($transferFunctionInfo, TransferFunctionInfo::values())) {
394
            throw new \InvalidArgumentException('Invalid transfer function info argument');
395
        }
396
397
        $this->setArgument(sprintf('-dTransferFunctionInfo=/%s', $transferFunctionInfo));
398
399
        return $this;
400
    }
401
402
    /**
403
     * Get UCR and BG info
404
     *
405
     * @return string
406
     */
407
    public function getUcrAndBgInfo()
408
    {
409
        $value = $this->getArgumentValue('-dUCRandBGInfo');
410
        if (null === $value) {
411
            switch ($this->getPdfSettings()) {
412
                case PdfSettings::PRINTER:
413
                case PdfSettings::PREPRESS:
414
                    return UcrAndBgInfo::PRESERVE;
415
                default:
416
                    return UcrAndBgInfo::REMOVE;
417
            }
418
        }
419
420
        return substr($value, 1);
421
    }
422
423
    /**
424
     * Set UCR and BG info
425
     *
426
     * @param string $ucrAndBgInfo
427
     *
428
     * @throws \InvalidArgumentException
429
     *
430
     * @return $this
431
     */
432
    public function setUcrAndBgInfo($ucrAndBgInfo)
433
    {
434
        $ucrAndBgInfo = ltrim($ucrAndBgInfo, '/');
435
        if (!in_array($ucrAndBgInfo, UcrAndBgInfo::values())) {
436
            throw new \InvalidArgumentException('Invalid UCR and BG info argument');
437
        }
438
439
        $this->setArgument(sprintf('-dUCRandBGInfo=/%s', $ucrAndBgInfo));
440
441
        return $this;
442
    }
443
}
444