Completed
Pull Request — develop (#304)
by Franck
06:53
created

RichText::getInsetRight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
 * @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 57
    public function __construct()
163
    {
164
        // Initialise variables
165 57
        $this->richTextParagraphs = array(
166 57
            new Paragraph()
167
        );
168 57
        $this->activeParagraph    = 0;
169
170
        // Initialize parent
171 57
        parent::__construct();
172 57
    }
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 16
    public function getActiveParagraph()
190
    {
191 16
        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 8
    public function setActiveParagraph($index = 0)
202
    {
203 8
        if ($index >= count($this->richTextParagraphs)) {
204 1
            throw new \Exception("Invalid paragraph count.");
205
        }
206
207 7
        $this->activeParagraph = $index;
208
209 7
        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 10
    public function createParagraph()
234
    {
235 10
        $numParagraphs = count($this->richTextParagraphs);
236 10
        if ($numParagraphs > 0) {
237 9
            $alignment   = clone $this->getActiveParagraph()->getAlignment();
238 9
            $font        = clone $this->getActiveParagraph()->getFont();
239 9
            $bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
240
        }
241
242 10
        $this->richTextParagraphs[] = new Paragraph();
243 10
        $this->activeParagraph      = count($this->richTextParagraphs) - 1;
244
245 10
        if (isset($alignment)) {
246 9
            $this->getActiveParagraph()->setAlignment($alignment);
247
        }
248 10
        if (isset($font)) {
249 9
            $this->getActiveParagraph()->setFont($font);
250
        }
251 10
        if (isset($bulletStyle)) {
252 9
            $this->getActiveParagraph()->setBulletStyle($bulletStyle);
253
        }
254 10
        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 3
    public function createBreak()
290
    {
291 3
        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 13
    public function createTextRun($pText = '')
302
    {
303 13
        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
        }
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 25
    public function getParagraphs()
341
    {
342 25
        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 9
    public function setParagraphs($paragraphs = null)
353
    {
354 9
        if (!is_array($paragraphs)) {
355 1
            throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.");
356
        }
357
358 8
        $this->richTextParagraphs = $paragraphs;
359 8
        $this->activeParagraph    = count($this->richTextParagraphs) - 1;
360 8
        return $this;
361
    }
362
363
    /**
364
     * Get text wrapping
365
     *
366
     * @return string
367
     */
368 1
    public function getWrap()
369
    {
370 1
        return $this->wrap;
371
    }
372
373
    /**
374
     * Set text wrapping
375
     *
376
     * @param $value string
377
     * @return \PhpOffice\PhpPresentation\Shape\RichText
378
     */
379 1
    public function setWrap($value = self::WRAP_SQUARE)
380
    {
381 1
        $this->wrap = $value;
382
383 1
        return $this;
384
    }
385
386
    /**
387
     * Get autofit
388
     *
389
     * @return string
390
     */
391 1
    public function getAutoFit()
392
    {
393 1
        return $this->autoFit;
394
    }
395
396
    /**
397
     * Get pourcentage of fontScale
398
     *
399
     * @return float
400
     */
401
    public function getFontScale()
402
    {
403
        return $this->fontScale;
404
    }
405
406
    /**
407
     * Get pourcentage of the line space reduction
408
     *
409
     * @return float
410
     */
411
    public function getLineSpaceReduction()
412
    {
413
        return $this->lnSpcReduction;
414
    }
415
416
    /**
417
     * Set autofit
418
     *
419
     * @param $value string
420
     * @param $fontScale float
421
     * @param $lnSpcReduction float
422
     * @return \PhpOffice\PhpPresentation\Shape\RichText
423
     */
424 1
    public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null)
425
    {
426 1
        $this->autoFit = $value;
427
        
428 1
        if (!is_null($fontScale)) {
429
            $this->fontScale = $fontScale;
430
        }
431
        
432 1
        if (!is_null($lnSpcReduction)) {
433
            $this->lnSpcReduction = $lnSpcReduction;
434
        }
435
436 1
        return $this;
437
    }
438
439
    /**
440
     * Get horizontal overflow
441
     *
442
     * @return string
443
     */
444 1
    public function getHorizontalOverflow()
445
    {
446 1
        return $this->horizontalOverflow;
447
    }
448
449
    /**
450
     * Set horizontal overflow
451
     *
452
     * @param $value string
453
     * @return \PhpOffice\PhpPresentation\Shape\RichText
454
     */
455 1
    public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW)
456
    {
457 1
        $this->horizontalOverflow = $value;
458
459 1
        return $this;
460
    }
461
462
    /**
463
     * Get vertical overflow
464
     *
465
     * @return string
466
     */
467 1
    public function getVerticalOverflow()
468
    {
469 1
        return $this->verticalOverflow;
470
    }
471
472
    /**
473
     * Set vertical overflow
474
     *
475
     * @param $value string
476
     * @return \PhpOffice\PhpPresentation\Shape\RichText
477
     */
478 1
    public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW)
479
    {
480 1
        $this->verticalOverflow = $value;
481
482 1
        return $this;
483
    }
484
485
    /**
486
     * Get upright
487
     *
488
     * @return boolean
489
     */
490 1
    public function isUpright()
491
    {
492 1
        return $this->upright;
493
    }
494
495
    /**
496
     * Set vertical
497
     *
498
     * @param $value boolean
499
     * @return \PhpOffice\PhpPresentation\Shape\RichText
500
     */
501 1
    public function setUpright($value = false)
502
    {
503 1
        $this->upright = $value;
504
505 1
        return $this;
506
    }
507
508
    /**
509
     * Get vertical
510
     *
511
     * @return boolean
512
     */
513 1
    public function isVertical()
514
    {
515 1
        return $this->vertical;
516
    }
517
518
    /**
519
     * Set vertical
520
     *
521
     * @param $value boolean
522
     * @return \PhpOffice\PhpPresentation\Shape\RichText
523
     */
524 1
    public function setVertical($value = false)
525
    {
526 1
        $this->vertical = $value;
527
528 1
        return $this;
529
    }
530
531
    /**
532
     * Get columns
533
     *
534
     * @return int
535
     */
536 1
    public function getColumns()
537
    {
538 1
        return $this->columns;
539
    }
540
541
    /**
542
     * Set columns
543
     *
544
     * @param $value int
545
     * @throws \Exception
546
     * @return \PhpOffice\PhpPresentation\Shape\RichText
547
     */
548 2
    public function setColumns($value = 1)
549
    {
550 2
        if ($value > 16 || $value < 1) {
551 1
            throw new \Exception('Number of columns should be 1-16');
552
        }
553
554 1
        $this->columns = $value;
555
556 1
        return $this;
557
    }
558
559
    /**
560
     * Get bottom inset
561
     *
562
     * @return float
563
     */
564 1
    public function getInsetBottom()
565
    {
566 1
        return $this->bottomInset;
567
    }
568
569
    /**
570
     * Set bottom inset
571
     *
572
     * @param $value float
573
     * @return \PhpOffice\PhpPresentation\Shape\RichText
574
     */
575 4
    public function setInsetBottom($value = 4.8)
576
    {
577 4
        $this->bottomInset = $value;
578
579 4
        return $this;
580
    }
581
582
    /**
583
     * Get left inset
584
     *
585
     * @return float
586
     */
587 1
    public function getInsetLeft()
588
    {
589 1
        return $this->leftInset;
590
    }
591
592
    /**
593
     * Set left inset
594
     *
595
     * @param $value float
596
     * @return \PhpOffice\PhpPresentation\Shape\RichText
597
     */
598 4
    public function setInsetLeft($value = 9.6)
599
    {
600 4
        $this->leftInset = $value;
601
602 4
        return $this;
603
    }
604
605
    /**
606
     * Get right inset
607
     *
608
     * @return float
609
     */
610 1
    public function getInsetRight()
611
    {
612 1
        return $this->rightInset;
613
    }
614
615
    /**
616
     * Set left inset
617
     *
618
     * @param $value float
619
     * @return \PhpOffice\PhpPresentation\Shape\RichText
620
     */
621 4
    public function setInsetRight($value = 9.6)
622
    {
623 4
        $this->rightInset = $value;
624
625 4
        return $this;
626
    }
627
628
    /**
629
     * Get top inset
630
     *
631
     * @return float
632
     */
633 1
    public function getInsetTop()
634
    {
635 1
        return $this->topInset;
636
    }
637
638
    /**
639
     * Set top inset
640
     *
641
     * @param $value float
642
     * @return \PhpOffice\PhpPresentation\Shape\RichText
643
     */
644 4
    public function setInsetTop($value = 4.8)
645
    {
646 4
        $this->topInset = $value;
647
648 4
        return $this;
649
    }
650
651
    /**
652
     * Set horizontal auto shrink
653
     * @param bool $value
654
     */
655 2
    public function setAutoShrinkHorizontal($value = null)
656
    {
657 2
        if (is_bool($value)) {
658 2
            $this->autoShrinkHorizontal = $value;
659
        }
660 2
        return $this;
661
    }
662
    
663
    /**
664
     * Get horizontal auto shrink
665
     * @return bool
666
     */
667 15
    public function hasAutoShrinkHorizontal()
668
    {
669 15
        return $this->autoShrinkHorizontal;
670
    }
671
    
672
    /**
673
     * Set vertical auto shrink
674
     * @param bool $value
675
     */
676 2
    public function setAutoShrinkVertical($value = null)
677
    {
678 2
        if (is_bool($value)) {
679 2
            $this->autoShrinkVertical = $value;
680
        }
681 2
        return $this;
682
    }
683
    
684
    /**
685
     * Set vertical auto shrink
686
     * @return bool
687
     */
688 15
    public function hasAutoShrinkVertical()
689
    {
690 15
        return $this->autoShrinkVertical;
691
    }
692
    
693
    /**
694
     * Get hash code
695
     *
696
     * @return string Hash code
697
     */
698 1
    public function getHashCode()
699
    {
700 1
        $hashElements = '';
701 1
        foreach ($this->richTextParagraphs as $element) {
702 1
            $hashElements .= $element->getHashCode();
703
        }
704
705 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__);
706
    }
707
}
708