Completed
Pull Request — develop (#187)
by Franck
08:38
created

RichText::getLineSpaceReduction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Shape;
19
20
use PhpOffice\PhpPresentation\AbstractShape;
21
use PhpOffice\PhpPresentation\ComparableInterface;
22
use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
23
use PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface;
24
25
/**
26
 * \PhpOffice\PhpPresentation\Shape\RichText
27
 */
28
class RichText extends AbstractShape implements ComparableInterface
29
{
30
    /** Wrapping */
31
    const WRAP_NONE = 'none';
32
    const WRAP_SQUARE = 'square';
33
34
    /** Autofit */
35
    const AUTOFIT_DEFAULT = 'spAutoFit';
36
    const AUTOFIT_SHAPE = 'spAutoFit';
37
    const AUTOFIT_NOAUTOFIT = 'noAutofit';
38
    const AUTOFIT_NORMAL = 'normAutofit';
39
40
    /** Overflow */
41
    const OVERFLOW_CLIP = 'clip';
42
    const OVERFLOW_OVERFLOW = 'overflow';
43
44
    /**
45
     * Rich text paragraphs
46
     *
47
     * @var \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[]
48
     */
49
    private $richTextParagraphs;
50
51
    /**
52
     * Active paragraph
53
     *
54
     * @var int
55
     */
56
    private $activeParagraph = 0;
57
58
    /**
59
     * Text wrapping
60
     *
61
     * @var string
62
     */
63
    private $wrap = self::WRAP_SQUARE;
64
65
    /**
66
     * Autofit
67
     *
68
     * @var string
69
     */
70
    private $autoFit = self::AUTOFIT_DEFAULT;
71
72
    /**
73
     * Horizontal overflow
74
     *
75
     * @var string
76
     */
77
    private $horizontalOverflow = self::OVERFLOW_OVERFLOW;
78
79
    /**
80
     * Vertical overflow
81
     *
82
     * @var string
83
     */
84
    private $verticalOverflow = self::OVERFLOW_OVERFLOW;
85
86
    /**
87
     * Text upright?
88
     *
89
     * @var boolean
90
     */
91
    private $upright = false;
92
93
    /**
94
     * Vertical text?
95
     *
96
     * @var boolean
97
     */
98
    private $vertical = false;
99
100
    /**
101
     * Number of columns (1 - 16)
102
     *
103
     * @var int
104
     */
105
    private $columns = 1;
106
107
    /**
108
     * Bottom inset (in pixels)
109
     *
110
     * @var float
111
     */
112
    private $bottomInset = 4.8;
113
114
    /**
115
     * Left inset (in pixels)
116
     *
117
     * @var float
118
     */
119
    private $leftInset = 9.6;
120
121
    /**
122
     * Right inset (in pixels)
123
     *
124
     * @var float
125
     */
126
    private $rightInset = 9.6;
127
128
    /**
129
     * Top inset (in pixels)
130
     *
131
     * @var float
132
     */
133
    private $topInset = 4.8;
134
135
    /**
136
     * Horizontal Auto Shrink
137
     * @var boolean
138
     */
139
    private $autoShrinkHorizontal;
140
141
    /**
142
     * Vertical Auto Shrink
143
     * @var boolean
144
     */
145
    private $autoShrinkVertical;
146
    
147
    /**
148
     * The percentage of the original font size to which the text is scaled
149
     * @var float
150
     */
151
    private $fontScale;
152
    
153
    /**
154
     * The percentage of the reduction of the line spacing
155
     * @var float
156
     */
157
    private $lnSpcReduction;
158
159
    /**
160
     * Create a new \PhpOffice\PhpPresentation\Shape\RichText instance
161
     */
162 75
    public function __construct()
163
    {
164
        // Initialise variables
165 75
        $this->richTextParagraphs = array(
166 75
            new Paragraph()
167 75
        );
168 75
        $this->activeParagraph    = 0;
169
170
        // Initialize parent
171 75
        parent::__construct();
172 75
    }
173
174
    /**
175
     * Get active paragraph index
176
     *
177
     * @return int
178
     */
179 3
    public function getActiveParagraphIndex()
180
    {
181 3
        return $this->activeParagraph;
182
    }
183
184
    /**
185
     * Get active paragraph
186
     *
187
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
188
     */
189 22
    public function getActiveParagraph()
190
    {
191 22
        return $this->richTextParagraphs[$this->activeParagraph];
192
    }
193
194
    /**
195
     * Set active paragraph
196
     *
197
     * @param  int $index
198
     * @throws \Exception
199
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
200
     */
201 7
    public function setActiveParagraph($index = 0)
202
    {
203 7
        if ($index >= count($this->richTextParagraphs)) {
204 1
            throw new \Exception("Invalid paragraph count.");
205
        }
206
207 6
        $this->activeParagraph = $index;
208
209 6
        return $this->getActiveParagraph();
210
    }
211
212
    /**
213
     * Get paragraph
214
     *
215
     * @param  int $index
216
     * @throws \Exception
217
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
218
     */
219 3
    public function getParagraph($index = 0)
220
    {
221 3
        if ($index >= count($this->richTextParagraphs)) {
222 1
            throw new \Exception("Invalid paragraph count.");
223
        }
224
225 2
        return $this->richTextParagraphs[$index];
226
    }
227
228
    /**
229
     * Create paragraph
230
     *
231
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
232
     */
233 11
    public function createParagraph()
234
    {
235 11
        $numParagraphs = count($this->richTextParagraphs);
236 11
        if ($numParagraphs > 0) {
237 10
            $alignment   = clone $this->getActiveParagraph()->getAlignment();
238 10
            $font        = clone $this->getActiveParagraph()->getFont();
239 10
            $bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
240 10
        }
241
242 11
        $this->richTextParagraphs[] = new Paragraph();
243 11
        $this->activeParagraph      = count($this->richTextParagraphs) - 1;
244
245 11
        if (isset($alignment)) {
246 10
            $this->getActiveParagraph()->setAlignment($alignment);
247 10
        }
248 11
        if (isset($font)) {
249 10
            $this->getActiveParagraph()->setFont($font);
250 10
        }
251 11
        if (isset($bulletStyle)) {
252 10
            $this->getActiveParagraph()->setBulletStyle($bulletStyle);
253 10
        }
254 11
        return $this->getActiveParagraph();
255
    }
256
257
    /**
258
     * Add text
259
     *
260
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element
261
     * @throws \Exception
262
     * @return \PhpOffice\PhpPresentation\Shape\RichText
263
     */
264 1
    public function addText(TextElementInterface $pText = null)
265
    {
266 1
        $this->richTextParagraphs[$this->activeParagraph]->addText($pText);
267
268 1
        return $this;
269
    }
270
271
    /**
272
     * Create text (can not be formatted !)
273
     *
274
     * @param  string                                   $pText Text
275
     * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement
276
     * @throws \Exception
277
     */
278 2
    public function createText($pText = '')
279
    {
280 2
        return $this->richTextParagraphs[$this->activeParagraph]->createText($pText);
281
    }
282
283
    /**
284
     * Create break
285
     *
286
     * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement
287
     * @throws \Exception
288
     */
289 4
    public function createBreak()
290
    {
291 4
        return $this->richTextParagraphs[$this->activeParagraph]->createBreak();
292
    }
293
294
    /**
295
     * Create text run (can be formatted)
296
     *
297
     * @param  string                           $pText Text
298
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Run
299
     * @throws \Exception
300
     */
301 28
    public function createTextRun($pText = '')
302
    {
303 28
        return $this->richTextParagraphs[$this->activeParagraph]->createTextRun($pText);
304
    }
305
306
    /**
307
     * Get plain text
308
     *
309
     * @return string
310
     */
311 1
    public function getPlainText()
312
    {
313
        // Return value
314 1
        $returnValue = '';
315
316
        // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
317 1
        foreach ($this->richTextParagraphs as $p) {
318 1
            $returnValue .= $p->getPlainText();
319 1
        }
320
321
        // Return
322 1
        return $returnValue;
323
    }
324
325
    /**
326
     * Convert to string
327
     *
328
     * @return string
329
     */
330 1
    public function __toString()
331
    {
332 1
        return $this->getPlainText();
333
    }
334
335
    /**
336
     * Get paragraphs
337
     *
338
     * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[]
339
     */
340 23
    public function getParagraphs()
341
    {
342 23
        return $this->richTextParagraphs;
343
    }
344
345
    /**
346
     * Set paragraphs
347
     *
348
     * @param  \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs
349
     * @throws \Exception
350
     * @return \PhpOffice\PhpPresentation\Shape\RichText
351
     */
352 8
    public function setParagraphs($paragraphs = null)
353
    {
354 8
        if (is_array($paragraphs)) {
355 7
            $this->richTextParagraphs = $paragraphs;
356 7
            $this->activeParagraph    = count($this->richTextParagraphs) - 1;
357 7
        } else {
358 1
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.");
359
        }
360
361 7
        return $this;
362
    }
363
364
    /**
365
     * Get text wrapping
366
     *
367
     * @return string
368
     */
369 1
    public function getWrap()
370
    {
371 1
        return $this->wrap;
372
    }
373
374
    /**
375
     * Set text wrapping
376
     *
377
     * @param $value string
378
     * @return \PhpOffice\PhpPresentation\Shape\RichText
379
     */
380 1
    public function setWrap($value = self::WRAP_SQUARE)
381
    {
382 1
        $this->wrap = $value;
383
384 1
        return $this;
385
    }
386
387
    /**
388
     * Get autofit
389
     *
390
     * @return string
391
     */
392 1
    public function getAutoFit()
393
    {
394 1
        return $this->autoFit;
395
    }
396
397
    /**
398
     * Get pourcentage of fontScale
399
     *
400
     * @return float
401
     */
402
    public function getFontScale()
403
    {
404
        return $this->fontScale;
405
    }
406
407
    /**
408
     * Get pourcentage of the line space reduction
409
     *
410
     * @return float
411
     */
412
    public function getLineSpaceReduction()
413
    {
414
        return $this->lnSpcReduction;
415
    }
416
417
    /**
418
     * Set autofit
419
     *
420
     * @param $value string
421
     * @param $fontScale float
422
     * @param $lnSpcReduction float
423
     * @return \PhpOffice\PhpPresentation\Shape\RichText
424
     */
425 2
    public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null)
426
    {
427 2
        $this->autoFit = $value;
428
        
429 2
        if (!is_null($fontScale)) {
430 1
            $this->fontScale = $fontScale;
431 1
        }
432
        
433 2
        if (!is_null($lnSpcReduction)) {
434 1
            $this->lnSpcReduction = $lnSpcReduction;
435 1
        }
436
437 2
        return $this;
438
    }
439
440
    /**
441
     * Get horizontal overflow
442
     *
443
     * @return string
444
     */
445 1
    public function getHorizontalOverflow()
446
    {
447 1
        return $this->horizontalOverflow;
448
    }
449
450
    /**
451
     * Set horizontal overflow
452
     *
453
     * @param $value string
454
     * @return \PhpOffice\PhpPresentation\Shape\RichText
455
     */
456 1
    public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW)
457
    {
458 1
        $this->horizontalOverflow = $value;
459
460 1
        return $this;
461
    }
462
463
    /**
464
     * Get vertical overflow
465
     *
466
     * @return string
467
     */
468 1
    public function getVerticalOverflow()
469
    {
470 1
        return $this->verticalOverflow;
471
    }
472
473
    /**
474
     * Set vertical overflow
475
     *
476
     * @param $value string
477
     * @return \PhpOffice\PhpPresentation\Shape\RichText
478
     */
479 1
    public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW)
480
    {
481 1
        $this->verticalOverflow = $value;
482
483 1
        return $this;
484
    }
485
486
    /**
487
     * Get upright
488
     *
489
     * @return boolean
490
     */
491 1
    public function isUpright()
492
    {
493 1
        return $this->upright;
494
    }
495
496
    /**
497
     * Set vertical
498
     *
499
     * @param $value boolean
500
     * @return \PhpOffice\PhpPresentation\Shape\RichText
501
     */
502 2
    public function setUpright($value = false)
503
    {
504 2
        $this->upright = $value;
505
506 2
        return $this;
507
    }
508
509
    /**
510
     * Get vertical
511
     *
512
     * @return boolean
513
     */
514 1
    public function isVertical()
515
    {
516 1
        return $this->vertical;
517
    }
518
519
    /**
520
     * Set vertical
521
     *
522
     * @param $value boolean
523
     * @return \PhpOffice\PhpPresentation\Shape\RichText
524
     */
525 2
    public function setVertical($value = false)
526
    {
527 2
        $this->vertical = $value;
528
529 2
        return $this;
530
    }
531
532
    /**
533
     * Get columns
534
     *
535
     * @return int
536
     */
537 1
    public function getColumns()
538
    {
539 1
        return $this->columns;
540
    }
541
542
    /**
543
     * Set columns
544
     *
545
     * @param $value int
546
     * @throws \Exception
547
     * @return \PhpOffice\PhpPresentation\Shape\RichText
548
     */
549 2
    public function setColumns($value = 1)
550
    {
551 2
        if ($value > 16 || $value < 1) {
552 1
            throw new \Exception('Number of columns should be 1-16');
553
        }
554
555 1
        $this->columns = $value;
556
557 1
        return $this;
558
    }
559
560
    /**
561
     * Get bottom inset
562
     *
563
     * @return float
564
     */
565 1
    public function getInsetBottom()
566
    {
567 1
        return $this->bottomInset;
568
    }
569
570
    /**
571
     * Set bottom inset
572
     *
573
     * @param $value float
574
     * @return \PhpOffice\PhpPresentation\Shape\RichText
575
     */
576 4
    public function setInsetBottom($value = 4.8)
577
    {
578 4
        $this->bottomInset = $value;
579
580 4
        return $this;
581
    }
582
583
    /**
584
     * Get left inset
585
     *
586
     * @return float
587
     */
588 1
    public function getInsetLeft()
589
    {
590 1
        return $this->leftInset;
591
    }
592
593
    /**
594
     * Set left inset
595
     *
596
     * @param $value float
597
     * @return \PhpOffice\PhpPresentation\Shape\RichText
598
     */
599 4
    public function setInsetLeft($value = 9.6)
600
    {
601 4
        $this->leftInset = $value;
602
603 4
        return $this;
604
    }
605
606
    /**
607
     * Get right inset
608
     *
609
     * @return float
610
     */
611 1
    public function getInsetRight()
612
    {
613 1
        return $this->rightInset;
614
    }
615
616
    /**
617
     * Set left inset
618
     *
619
     * @param $value float
620
     * @return \PhpOffice\PhpPresentation\Shape\RichText
621
     */
622 4
    public function setInsetRight($value = 9.6)
623
    {
624 4
        $this->rightInset = $value;
625
626 4
        return $this;
627
    }
628
629
    /**
630
     * Get top inset
631
     *
632
     * @return float
633
     */
634 1
    public function getInsetTop()
635
    {
636 1
        return $this->topInset;
637
    }
638
639
    /**
640
     * Set top inset
641
     *
642
     * @param $value float
643
     * @return \PhpOffice\PhpPresentation\Shape\RichText
644
     */
645 4
    public function setInsetTop($value = 4.8)
646
    {
647 4
        $this->topInset = $value;
648
649 4
        return $this;
650
    }
651
652
    /**
653
     * Set horizontal auto shrink
654
     * @param bool $value
655
     */
656 2
    public function setAutoShrinkHorizontal($value = null)
657
    {
658 2
        if (is_bool($value)) {
659 2
            $this->autoShrinkHorizontal = $value;
660 2
        }
661 2
        return $this;
662
    }
663
    
664
    /**
665
     * Get horizontal auto shrink
666
     * @return bool
667
     */
668 14
    public function hasAutoShrinkHorizontal()
669
    {
670 14
        return $this->autoShrinkHorizontal;
671
    }
672
    
673
    /**
674
     * Set vertical auto shrink
675
     * @param bool $value
676
     */
677 2
    public function setAutoShrinkVertical($value = null)
678
    {
679 2
        if (is_bool($value)) {
680 2
            $this->autoShrinkVertical = $value;
681 2
        }
682 2
        return $this;
683
    }
684
    
685
    /**
686
     * Set vertical auto shrink
687
     * @return bool
688
     */
689 14
    public function hasAutoShrinkVertical()
690
    {
691 14
        return $this->autoShrinkVertical;
692
    }
693
    
694
    /**
695
     * Get hash code
696
     *
697
     * @return string Hash code
698
     */
699 1
    public function getHashCode()
700
    {
701 1
        $hashElements = '';
702 1
        foreach ($this->richTextParagraphs as $element) {
703 1
            $hashElements .= $element->getHashCode();
704 1
        }
705
706 1
        return md5($hashElements . $this->wrap . $this->autoFit . $this->horizontalOverflow . $this->verticalOverflow . ($this->upright ? '1' : '0') . ($this->vertical ? '1' : '0') . $this->columns . $this->bottomInset . $this->leftInset . $this->rightInset . $this->topInset . parent::getHashCode() . __CLASS__);
707
    }
708
}
709