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