Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
94 / 94
100.00% covered (success)
100.00%
37 / 37
CRAP
100.00% covered (success)
100.00%
1 / 1
Font
100.00% covered (success)
100.00%
94 / 94
100.00% covered (success)
100.00%
37 / 37
54
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getPanose
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPanose
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 getPitchFamily
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPitchFamily
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCharset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCharset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCharacterSpacing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCharacterSpacing
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isBold
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBold
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isItalic
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setItalic
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setBaseline
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getBaseline
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSuperScript
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSuperScript
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 isSubScript
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSubScript
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
 getCapitalization
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCapitalization
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getUnderline
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUnderline
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isStrikethrough
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStrikethrough
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setStrikethrough
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setColor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFormat
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getHashCode
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 getHashIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setHashIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Style;
21
22use PhpOffice\PhpPresentation\ComparableInterface;
23use PhpOffice\PhpPresentation\Exception\InvalidParameterException;
24use PhpOffice\PhpPresentation\Exception\NotAllowedValueException;
25
26/**
27 * \PhpOffice\PhpPresentation\Style\Font.
28 */
29class Font implements ComparableInterface
30{
31    // Capitalization type
32    public const CAPITALIZATION_NONE = 'none';
33    public const CAPITALIZATION_SMALL = 'small';
34    public const CAPITALIZATION_ALL = 'all';
35
36    // Charset type
37    public const CHARSET_DEFAULT = 0x01;
38
39    // Format type
40    public const FORMAT_LATIN = 'latin';
41    public const FORMAT_EAST_ASIAN = 'ea';
42    public const FORMAT_COMPLEX_SCRIPT = 'cs';
43
44    // Strike type
45    public const STRIKE_NONE = 'noStrike';
46    public const STRIKE_SINGLE = 'sngStrike';
47    public const STRIKE_DOUBLE = 'dblStrike';
48
49    // Underline types
50    public const UNDERLINE_NONE = 'none';
51    public const UNDERLINE_DASH = 'dash';
52    public const UNDERLINE_DASHHEAVY = 'dashHeavy';
53    public const UNDERLINE_DASHLONG = 'dashLong';
54    public const UNDERLINE_DASHLONGHEAVY = 'dashLongHeavy';
55    public const UNDERLINE_DOUBLE = 'dbl';
56    public const UNDERLINE_DOTHASH = 'dotDash';
57    public const UNDERLINE_DOTHASHHEAVY = 'dotDashHeavy';
58    public const UNDERLINE_DOTDOTDASH = 'dotDotDash';
59    public const UNDERLINE_DOTDOTDASHHEAVY = 'dotDotDashHeavy';
60    public const UNDERLINE_DOTTED = 'dotted';
61    public const UNDERLINE_DOTTEDHEAVY = 'dottedHeavy';
62    public const UNDERLINE_HEAVY = 'heavy';
63    public const UNDERLINE_SINGLE = 'sng';
64    public const UNDERLINE_WAVY = 'wavy';
65    public const UNDERLINE_WAVYDOUBLE = 'wavyDbl';
66    public const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
67    public const UNDERLINE_WORDS = 'words';
68
69    // Script sub and super values
70    public const BASELINE_SUPERSCRIPT = 300000;
71    public const BASELINE_SUBSCRIPT = -250000;
72
73    /**
74     * Name.
75     *
76     * @var string
77     */
78    private $name = 'Calibri';
79
80    /**
81     * Panose.
82     *
83     * @var string
84     */
85    private $panose = '';
86
87    /**
88     * Pitch Family.
89     *
90     * @var int
91     */
92    private $pitchFamily = 0;
93
94    /**
95     * Charset.
96     *
97     * @var int
98     */
99    private $charset = self::CHARSET_DEFAULT;
100
101    /**
102     * Font Size.
103     *
104     * @var int
105     */
106    private $size = 10;
107
108    /**
109     * Bold.
110     *
111     * @var bool
112     */
113    private $bold = false;
114
115    /**
116     * Italic.
117     *
118     * @var bool
119     */
120    private $italic = false;
121
122    /**
123     * Baseline.
124     *
125     * @var int
126     */
127    private $baseline = 0;
128
129    /**
130     * Capitalization.
131     *
132     * @var string
133     */
134    private $capitalization = self::CAPITALIZATION_NONE;
135
136    /**
137     * Underline.
138     *
139     * @var string
140     */
141    private $underline = self::UNDERLINE_NONE;
142
143    /**
144     * Strikethrough.
145     *
146     * @var string
147     */
148    private $strikethrough = self::STRIKE_NONE;
149
150    /**
151     * Foreground color.
152     *
153     * @var Color
154     */
155    private $color;
156
157    /**
158     * Character Spacing.
159     *
160     * @var float
161     */
162    private $characterSpacing = 0;
163
164    /**
165     * Format.
166     *
167     * @var string
168     */
169    private $format = self::FORMAT_LATIN;
170
171    /**
172     * Hash index.
173     *
174     * @var int
175     */
176    private $hashIndex;
177
178    public function __construct()
179    {
180        $this->color = new Color(Color::COLOR_BLACK);
181    }
182
183    /**
184     * Get Name.
185     */
186    public function getName(): string
187    {
188        return $this->name;
189    }
190
191    /**
192     * Set Name.
193     */
194    public function setName(string $pValue = 'Calibri'): self
195    {
196        if ('' == $pValue) {
197            $pValue = 'Calibri';
198        }
199        $this->name = $pValue;
200
201        return $this;
202    }
203
204    /**
205     * Get panose.
206     */
207    public function getPanose(): string
208    {
209        return $this->panose;
210    }
211
212    /**
213     * Set panose.
214     */
215    public function setPanose(string $pValue): self
216    {
217        if (mb_strlen($pValue) !== 10) {
218            throw new InvalidParameterException('pValue', $pValue, 'The length is not equals to 10');
219        }
220
221        $allowedChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
222        foreach (mb_str_split($pValue) as $char) {
223            if (!in_array($char, $allowedChars)) {
224                throw new InvalidParameterException(
225                    'pValue',
226                    $pValue,
227                    sprintf('The character "%s" is not allowed', $char)
228                );
229            }
230        }
231
232        $this->panose = $pValue;
233
234        return $this;
235    }
236
237    /**
238     * Get pitchFamily.
239     */
240    public function getPitchFamily(): int
241    {
242        return $this->pitchFamily;
243    }
244
245    /**
246     * Set pitchFamily.
247     */
248    public function setPitchFamily(int $pValue): self
249    {
250        $this->pitchFamily = $pValue;
251
252        return $this;
253    }
254
255    /**
256     * Get charset.
257     */
258    public function getCharset(): int
259    {
260        return $this->charset;
261    }
262
263    /**
264     * Set charset.
265     */
266    public function setCharset(int $pValue): self
267    {
268        $this->charset = $pValue;
269
270        return $this;
271    }
272
273    /**
274     * Get Character Spacing.
275     */
276    public function getCharacterSpacing(): float
277    {
278        return $this->characterSpacing;
279    }
280
281    /**
282     * Set Character Spacing
283     * Value in pt.
284     */
285    public function setCharacterSpacing(float $pValue = 0): self
286    {
287        $this->characterSpacing = $pValue * 100;
288
289        return $this;
290    }
291
292    /**
293     * Get Size.
294     */
295    public function getSize(): int
296    {
297        return $this->size;
298    }
299
300    /**
301     * Set Size.
302     */
303    public function setSize(int $pValue = 10): self
304    {
305        $this->size = $pValue;
306
307        return $this;
308    }
309
310    /**
311     * Get Bold.
312     */
313    public function isBold(): bool
314    {
315        return $this->bold;
316    }
317
318    /**
319     * Set Bold.
320     */
321    public function setBold(bool $pValue = false): self
322    {
323        $this->bold = $pValue;
324
325        return $this;
326    }
327
328    /**
329     * Get Italic.
330     */
331    public function isItalic(): bool
332    {
333        return $this->italic;
334    }
335
336    /**
337     * Set Italic.
338     */
339    public function setItalic(bool $pValue = false): self
340    {
341        $this->italic = $pValue;
342
343        return $this;
344    }
345
346    /**
347     * Set Baseline.
348     */
349    public function setBaseline(int $pValue): self
350    {
351        $this->baseline = $pValue;
352
353        return $this;
354    }
355
356    /**
357     * Get Baseline.
358     */
359    public function getBaseline(): int
360    {
361        return $this->baseline;
362    }
363
364    /**
365     * Get SuperScript.
366     *
367     * @deprecated getBaseline() === self::BASELINE_SUPERSCRIPT
368     */
369    public function isSuperScript(): bool
370    {
371        return $this->getBaseline() === self::BASELINE_SUPERSCRIPT;
372    }
373
374    /**
375     * Set SuperScript.
376     *
377     * @deprecated setBaseline(self::BASELINE_SUPERSCRIPT)
378     */
379    public function setSuperScript(bool $pValue = false): self
380    {
381        return $this->setBaseline($pValue ? self::BASELINE_SUPERSCRIPT : ($this->getBaseline() == self::BASELINE_SUBSCRIPT ? $this->getBaseline() : 0));
382    }
383
384    /**
385     * Get SubScript.
386     *
387     * @deprecated getBaseline() === self::BASELINE_SUBSCRIPT
388     */
389    public function isSubScript(): bool
390    {
391        return $this->getBaseline() === self::BASELINE_SUBSCRIPT;
392    }
393
394    /**
395     * Set SubScript.
396     *
397     * @deprecated setBaseline(self::BASELINE_SUBSCRIPT)
398     */
399    public function setSubScript(bool $pValue = false): self
400    {
401        return $this->setBaseline($pValue ? self::BASELINE_SUBSCRIPT : ($this->getBaseline() == self::BASELINE_SUPERSCRIPT ? $this->getBaseline() : 0));
402    }
403
404    /**
405     * Get Capitalization.
406     */
407    public function getCapitalization(): string
408    {
409        return $this->capitalization;
410    }
411
412    /**
413     * Set Capitalization.
414     */
415    public function setCapitalization(string $pValue = self::CAPITALIZATION_NONE): self
416    {
417        if (!in_array(
418            $pValue,
419            [self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]
420        )) {
421            throw new NotAllowedValueException($pValue, [self::CAPITALIZATION_NONE, self::CAPITALIZATION_ALL, self::CAPITALIZATION_SMALL]);
422        }
423
424        $this->capitalization = $pValue;
425
426        return $this;
427    }
428
429    /**
430     * Get Underline.
431     */
432    public function getUnderline(): string
433    {
434        return $this->underline;
435    }
436
437    /**
438     * Set Underline.
439     *
440     * @param string $pValue Underline type
441     */
442    public function setUnderline(string $pValue = self::UNDERLINE_NONE): self
443    {
444        if ('' == $pValue) {
445            $pValue = self::UNDERLINE_NONE;
446        }
447        $this->underline = $pValue;
448
449        return $this;
450    }
451
452    /**
453     * Get Strikethrough.
454     *
455     * @deprecated Use `getStrikethrough`
456     */
457    public function isStrikethrough(): bool
458    {
459        return $this->strikethrough !== self::STRIKE_NONE;
460    }
461
462    /**
463     * Get Strikethrough.
464     */
465    public function getStrikethrough(): string
466    {
467        return $this->strikethrough;
468    }
469
470    /**
471     * Set Strikethrough.
472     *
473     * @deprecated $pValue as boolean
474     *
475     * @param bool|string $pValue
476     *
477     * @return self
478     */
479    public function setStrikethrough($pValue = false)
480    {
481        if (is_bool($pValue)) {
482            $pValue = $pValue ? self::STRIKE_SINGLE : self::STRIKE_NONE;
483        }
484        if (in_array($pValue, [
485            self::STRIKE_NONE,
486            self::STRIKE_SINGLE,
487            self::STRIKE_DOUBLE,
488        ])) {
489            $this->strikethrough = $pValue;
490        }
491
492        return $this;
493    }
494
495    /**
496     * Get Color.
497     */
498    public function getColor(): Color
499    {
500        return $this->color;
501    }
502
503    /**
504     * Set Color.
505     */
506    public function setColor(Color $pValue): self
507    {
508        $this->color = $pValue;
509
510        return $this;
511    }
512
513    /**
514     * Get format.
515     */
516    public function getFormat(): string
517    {
518        return $this->format;
519    }
520
521    /**
522     * Set format.
523     */
524    public function setFormat(string $value = self::FORMAT_LATIN): self
525    {
526        if (in_array($value, [
527            self::FORMAT_COMPLEX_SCRIPT,
528            self::FORMAT_EAST_ASIAN,
529            self::FORMAT_LATIN,
530        ])) {
531            $this->format = $value;
532        }
533
534        return $this;
535    }
536
537    /**
538     * Get hash code.
539     *
540     * @return string Hash code
541     */
542    public function getHashCode(): string
543    {
544        return md5(
545            $this->name
546            . $this->size
547            . ($this->bold ? 't' : 'f')
548            . ($this->italic ? 't' : 'f')
549            . $this->baseline
550            . $this->underline
551            . ($this->strikethrough ? 't' : 'f')
552            . $this->format
553            . $this->color->getHashCode()
554            . __CLASS__
555        );
556    }
557
558    /**
559     * Get hash index.
560     *
561     * Note that this index may vary during script execution! Only reliable moment is
562     * while doing a write of a workbook and when changes are not allowed.
563     *
564     * @return null|int Hash index
565     */
566    public function getHashIndex(): ?int
567    {
568        return $this->hashIndex;
569    }
570
571    /**
572     * Set hash index.
573     *
574     * Note that this index may vary during script execution! Only reliable moment is
575     * while doing a write of a workbook and when changes are not allowed.
576     *
577     * @param int $value Hash index
578     *
579     * @return $this
580     */
581    public function setHashIndex(int $value)
582    {
583        $this->hashIndex = $value;
584
585        return $this;
586    }
587}