2db47680
hexiaodong
chore: 初始提交 php 遗...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\ODText\Style;
use PhpOffice\PhpWord\Shared\Converter;
/**
* Font style writer
*
* @since 0.10.0
*/
class Paragraph extends AbstractStyle
{
/**
* Write style.
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
return;
}
$xmlWriter = $this->getXmlWriter();
$marginTop = $style->getSpaceBefore();
$marginBottom = $style->getSpaceAfter();
$xmlWriter->startElement('style:style');
$styleName = $style->getStyleName();
$styleAuto = false;
$mpm = '';
$psm = '';
$pagestart = -1;
$breakafter = $breakbefore = $breakauto = false;
if ($style->isAuto()) {
if (substr($styleName, 0, 2) === 'PB') {
$styleAuto = true;
$breakafter = true;
} elseif (substr($styleName, 0, 2) === 'SB') {
$styleAuto = true;
$mpm = 'Standard' . substr($styleName, 2);
$psn = $style->getNumLevel();
$pagestart = $psn;
} elseif (substr($styleName, 0, 2) === 'HD') {
$styleAuto = true;
$psm = 'Heading_' . substr($styleName, 2);
$stylep = \PhpOffice\PhpWord\Style::getStyle($psm);
if ($stylep instanceof \PhpOffice\PhpWord\Style\Font) {
if (method_exists($stylep, 'getParagraph')) {
$stylep = $stylep->getParagraph();
}
}
if ($stylep instanceof \PhpOffice\PhpWord\Style\Paragraph) {
if ($stylep->hasPageBreakBefore()) {
$breakbefore = true;
}
}
} elseif (substr($styleName, 0, 2) === 'HE') {
$styleAuto = true;
$psm = 'Heading_' . substr($styleName, 2);
$breakauto = true;
} else {
$styleAuto = true;
$psm = 'Normal';
if (preg_match('/^P\\d+_(\\w+)$/', $styleName, $matches)) {
$psm = $matches[1];
}
}
}
$xmlWriter->writeAttribute('style:name', $style->getStyleName());
$xmlWriter->writeAttribute('style:family', 'paragraph');
if ($styleAuto) {
$xmlWriter->writeAttributeIf($psm !== '', 'style:parent-style-name', $psm);
$xmlWriter->writeAttributeIf($mpm !== '', 'style:master-page-name', $mpm);
}
$xmlWriter->startElement('style:paragraph-properties');
if ($styleAuto) {
if ($breakafter) {
$xmlWriter->writeAttribute('fo:break-after', 'page');
$xmlWriter->writeAttribute('fo:margin-top', '0cm');
$xmlWriter->writeAttribute('fo:margin-bottom', '0cm');
} elseif ($breakbefore) {
$xmlWriter->writeAttribute('fo:break-before', 'page');
} elseif ($breakauto) {
$xmlWriter->writeAttribute('fo:break-before', 'auto');
}
if ($pagestart > 0) {
$xmlWriter->writeAttribute('style:page-number', $pagestart);
}
}
if (!$breakafter && !$breakbefore && !$breakauto) {
$twipToPoint = Converter::INCH_TO_TWIP / Converter::INCH_TO_POINT; // 20
$xmlWriter->writeAttributeIf($marginTop !== null, 'fo:margin-top', ($marginTop / $twipToPoint) . 'pt');
$xmlWriter->writeAttributeIf($marginBottom !== null, 'fo:margin-bottom', ($marginBottom / $twipToPoint) . 'pt');
}
$temp = $style->getAlignment();
$xmlWriter->writeAttributeIf($temp !== '', 'fo:text-align', $temp);
$temp = $style->getLineHeight();
$xmlWriter->writeAttributeIf($temp !== null, 'fo:line-height', ((string) ($temp * 100) . '%'));
$xmlWriter->writeAttributeIf($style->hasPageBreakBefore() === true, 'fo:break-before', 'page');
$tabs = $style->getTabs();
if ($tabs !== null && count($tabs) > 0) {
$xmlWriter->startElement('style:tab-stops');
foreach ($tabs as $tab) {
$xmlWriter->startElement('style:tab-stop');
$xmlWriter->writeAttribute('style:type', $tab->getType());
$xmlWriter->writeAttribute('style:position', (string) ($tab->getPosition() / Converter::INCH_TO_TWIP) . 'in');
$xmlWriter->endElement();
}
$xmlWriter->endElement();
}
//Right to left
$xmlWriter->writeAttributeIf($style->isBidi(), 'style:writing-mode', 'rl-tb');
//Indentation
$indent = $style->getIndentation();
//if ($indent instanceof \PhpOffice\PhpWord\Style\Indentation) {
if (!empty($indent)) {
$marg = $indent->getLeft();
$xmlWriter->writeAttributeIf($marg !== null, 'fo:margin-left', (string) ($marg / Converter::INCH_TO_TWIP) . 'in');
$marg = $indent->getRight();
$xmlWriter->writeAttributeIf($marg !== null, 'fo:margin-right', (string) ($marg / Converter::INCH_TO_TWIP) . 'in');
}
$xmlWriter->endElement(); //style:paragraph-properties
if ($styleAuto && substr($styleName, 0, 2) === 'SB') {
$xmlWriter->startElement('style:text-properties');
$xmlWriter->writeAttribute('text:display', 'none');
$xmlWriter->endElement();
}
$xmlWriter->endElement(); //style:style
}
}
|