Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.59% covered (warning)
70.59%
36 / 51
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Paragraph
70.59% covered (warning)
70.59%
36 / 51
0.00% covered (danger)
0.00%
0 / 1
36.46
0.00% covered (danger)
0.00%
0 / 1
 write
70.59% covered (warning)
70.59%
36 / 51
0.00% covered (danger)
0.00%
0 / 1
36.46
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Writer\HTML\Style;
20
21use PhpOffice\PhpWord\Shared\Converter;
22use PhpOffice\PhpWord\SimpleType\Jc;
23use PhpOffice\PhpWord\Writer\PDF\TCPDF;
24
25/**
26 * Paragraph style HTML writer.
27 *
28 * @since 0.10.0
29 */
30class Paragraph extends AbstractStyle
31{
32    /**
33     * Write style.
34     *
35     * @return string
36     */
37    public function write()
38    {
39        $style = $this->getStyle();
40        if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
41            return '';
42        }
43        $css = [];
44
45        // Alignment
46        if ('' !== $style->getAlignment()) {
47            $textAlign = '';
48
49            switch ($style->getAlignment()) {
50                case Jc::CENTER:
51                    $textAlign = 'center';
52
53                    break;
54                case Jc::END:
55                    $textAlign = $style->isBidi() ? 'left' : 'right';
56
57                    break;
58                case Jc::MEDIUM_KASHIDA:
59                case Jc::HIGH_KASHIDA:
60                case Jc::LOW_KASHIDA:
61                case Jc::RIGHT:
62                    $textAlign = 'right';
63
64                    break;
65                case Jc::BOTH:
66                case Jc::DISTRIBUTE:
67                case Jc::THAI_DISTRIBUTE:
68                case Jc::JUSTIFY:
69                    $textAlign = 'justify';
70
71                    break;
72                case Jc::LEFT:
73                    $textAlign = 'left';
74
75                    break;
76                default: //all others, including Jc::START
77                    $textAlign = $style->isBidi() ? 'right' : 'left';
78
79                    break;
80            }
81
82            $css['text-align'] = $textAlign;
83        }
84
85        // Spacing
86        $spacing = $style->getSpace();
87        if (null !== $spacing) {
88            $before = $spacing->getBefore();
89            $after = $spacing->getAfter();
90            $css['margin-top'] = $this->getValueIf(null !== $before, ($before / 20) . 'pt');
91            $css['margin-bottom'] = $this->getValueIf(null !== $after, ($after / 20) . 'pt');
92        }
93
94        // Line Height
95        $lineHeight = $style->getLineHeight();
96        if (!empty($lineHeight)) {
97            $css['line-height'] = $lineHeight;
98        }
99
100        // Indentation (Margin)
101        $indentation = $style->getIndentation();
102        if ($indentation) {
103            $inches = $indentation->getLeft() * 1.0 / Converter::INCH_TO_TWIP;
104            $css[$this->getParentWriter() instanceof TCPDF ? 'text-indent' : 'margin-left'] = ((string) $inches) . 'in';
105
106            $inches = $indentation->getRight() * 1.0 / Converter::INCH_TO_TWIP;
107            $css['margin-right'] = ((string) $inches) . 'in';
108        }
109
110        // Page Break Before
111        if ($style->hasPageBreakBefore()) {
112            $css['page-break-before'] = 'always';
113        }
114
115        // Bidirectional
116        if ($style->isBidi()) {
117            $css['direction'] = 'rtl';
118        }
119
120        return $this->assembleCss($css);
121    }
122}