• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 9582863352

19 Jun 2024 01:25PM UTC coverage: 97.209% (+0.001%) from 97.208%
9582863352

Pull #2626

github

web-flow
Merge 3e882f0f8 into 2daa50c6f
Pull Request #2626: Prevented fatal errors when opening corrupt files or "doc" files

3 of 3 new or added lines in 1 file covered. (100.0%)

11598 of 11931 relevant lines covered (97.21%)

30.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/PhpWord/Writer/HTML/Style/Paragraph.php
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

18
namespace PhpOffice\PhpWord\Writer\HTML\Style;
19

20
use PhpOffice\PhpWord\Shared\Converter;
21
use PhpOffice\PhpWord\SimpleType\Jc;
22
use PhpOffice\PhpWord\Writer\PDF\TCPDF;
23

24
/**
25
 * Paragraph style HTML writer.
26
 *
27
 * @since 0.10.0
28
 */
29
class Paragraph extends AbstractStyle
30
{
31
    /**
32
     * Write style.
33
     *
34
     * @return string
35
     */
36
    public function write()
37
    {
38
        $style = $this->getStyle();
29✔
39
        if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
29✔
40
            return '';
1✔
41
        }
42
        $css = [];
28✔
43

44
        // Alignment
45
        if ('' !== $style->getAlignment()) {
28✔
46
            $textAlign = '';
2✔
47

48
            switch ($style->getAlignment()) {
2✔
49
                case Jc::CENTER:
50
                    $textAlign = 'center';
1✔
51

52
                    break;
1✔
53
                case Jc::END:
54
                    $textAlign = $style->isBidi() ? 'left' : 'right';
1✔
55

56
                    break;
1✔
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';
1✔
69

70
                    break;
1✔
71
                case Jc::LEFT:
72
                    $textAlign = 'left';
×
73

74
                    break;
×
75
                default: //all others, including Jc::START
76
                    $textAlign = $style->isBidi() ? 'right' : 'left';
2✔
77

78
                    break;
2✔
79
            }
80

81
            $css['text-align'] = $textAlign;
2✔
82
        }
83

84
        // Spacing
85
        $spacing = $style->getSpace();
28✔
86
        if (null !== $spacing) {
28✔
87
            $before = $spacing->getBefore();
7✔
88
            $after = $spacing->getAfter();
7✔
89
            $css['margin-top'] = $this->getValueIf(null !== $before, ($before / 20) . 'pt');
7✔
90
            $css['margin-bottom'] = $this->getValueIf(null !== $after, ($after / 20) . 'pt');
7✔
91
        }
92

93
        // Line Height
94
        $lineHeight = $style->getLineHeight();
28✔
95
        if (!empty($lineHeight)) {
28✔
96
            $css['line-height'] = $lineHeight;
3✔
97
        }
98

99
        // Indentation (Margin)
100
        $indentation = $style->getIndentation();
28✔
101
        if ($indentation) {
28✔
102
            $inches = $indentation->getLeft() * 1.0 / Converter::INCH_TO_TWIP;
2✔
103
            $css[$this->getParentWriter() instanceof TCPDF ? 'text-indent' : 'margin-left'] = ((string) $inches) . 'in';
2✔
104

105
            $inches = $indentation->getRight() * 1.0 / Converter::INCH_TO_TWIP;
2✔
106
            $css['margin-right'] = ((string) $inches) . 'in';
2✔
107
        }
108

109
        // Page Break Before
110
        if ($style->hasPageBreakBefore()) {
28✔
111
            $css['page-break-before'] = 'always';
1✔
112
        }
113

114
        // Bidirectional
115
        if ($style->isBidi()) {
28✔
116
            $css['direction'] = 'rtl';
1✔
117
        }
118

119
        return $this->assembleCss($css);
28✔
120
    }
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc