Skip to content

Commit 410ce4b

Browse files
committed
ODT: Enable section and column
1 parent b91e320 commit 410ce4b

File tree

9 files changed

+182
-234
lines changed

9 files changed

+182
-234
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
2222
- Config: Ability to use a config file to store various common settings - @ivanlanin GH-200
2323
- ODT: Enable inline font style in TextRun - @ivanlanin
2424
- ODT: Enable underline, strike/doublestrike, smallcaps/allcaps, superscript/subscript font style - @ivanlanin
25+
- ODT: Enable section and column - @ivanlanin
2526

2627
### Bugfixes
2728

samples/Sample_05_Multicolumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'colsNum' => 2,
1919
'colsSpace' => 1440,
2020
'breakType' => 'continuous'));
21-
$section->addText('Three columns, one inch (1440 twips) spacing. ' . $filler);
21+
$section->addText('Two columns, one inch (1440 twips) spacing. ' . $filler);
2222

2323
// Normal
2424
$section = $phpWord->addSection(array('breakType' => 'continuous'));

src/PhpWord/Writer/ODText/Part/Content.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use PhpOffice\PhpWord\Style\Font;
2828
use PhpOffice\PhpWord\Style\Paragraph;
2929
use PhpOffice\PhpWord\Writer\ODText\Element\Container;
30+
use PhpOffice\PhpWord\Writer\ODText\Style\Paragraph as ParagraphStyleWriter;
31+
use PhpOffice\PhpWord\Writer\ODText\Style\Section as SectionStyleWriter;
3032

3133
/**
3234
* ODText content part writer: content.xml
@@ -58,9 +60,10 @@ public function write()
5860

5961
// Automatic styles
6062
$xmlWriter->startElement('office:automatic-styles');
61-
$this->writeTextAutoStyles($xmlWriter);
62-
$this->writeImageAutoStyles($xmlWriter);
63-
$this->writeTableAutoStyles($xmlWriter, $phpWord);
63+
$this->writeSectionStyles($xmlWriter, $phpWord);
64+
$this->writeTextStyles($xmlWriter);
65+
$this->writeImageStyles($xmlWriter);
66+
$this->writeTableStyles($xmlWriter, $phpWord);
6467
$xmlWriter->endElement(); // office:automatic-styles
6568

6669
// Body
@@ -81,10 +84,13 @@ public function write()
8184
// Sections
8285
$sections = $phpWord->getSections();
8386
foreach ($sections as $section) {
84-
// $xmlWriter->startElement('text:section');
87+
$name = 'Section' . $section->getSectionId();
88+
$xmlWriter->startElement('text:section');
89+
$xmlWriter->writeAttribute('text:name', $name);
90+
$xmlWriter->writeAttribute('text:style-name', $name);
8591
$containerWriter = new Container($xmlWriter, $section);
8692
$containerWriter->write();
87-
// $xmlWriter->endElement(); // text:section
93+
$xmlWriter->endElement(); // text:section
8894
}
8995

9096
$xmlWriter->endElement(); // office:text
@@ -95,10 +101,27 @@ public function write()
95101
return $xmlWriter->getData();
96102
}
97103

104+
/**
105+
* Write section automatic styles
106+
*
107+
* @since 0.11.0
108+
* @todo Put more section properties/styles
109+
*/
110+
private function writeSectionStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
111+
{
112+
$sections = $phpWord->getSections();
113+
foreach ($sections as $section) {
114+
$style = $section->getSettings();
115+
$style->setStyleName("Section{$section->getSectionId()}");
116+
$styleWriter = new SectionStyleWriter($xmlWriter, $style);
117+
$styleWriter->write();
118+
}
119+
}
120+
98121
/**
99122
* Write automatic styles
100123
*/
101-
private function writeTextAutoStyles(XMLWriter $xmlWriter)
124+
private function writeTextStyles(XMLWriter $xmlWriter)
102125
{
103126
$styles = Style::getStyles();
104127
$paragraphStyleCount = 0;
@@ -119,7 +142,7 @@ private function writeTextAutoStyles(XMLWriter $xmlWriter)
119142
$style = new Paragraph();
120143
$style->setStyleName('P1');
121144
$style->setAuto();
122-
$styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style);
145+
$styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
123146
$styleWriter->write();
124147
}
125148
}
@@ -128,7 +151,7 @@ private function writeTextAutoStyles(XMLWriter $xmlWriter)
128151
/**
129152
* Write image automatic styles
130153
*/
131-
private function writeImageAutoStyles(XMLWriter $xmlWriter)
154+
private function writeImageStyles(XMLWriter $xmlWriter)
132155
{
133156
$images = Media::getElements('section');
134157
foreach ($images as $image) {
@@ -149,7 +172,7 @@ private function writeImageAutoStyles(XMLWriter $xmlWriter)
149172
/**
150173
* Write table automatic styles
151174
*/
152-
private function writeTableAutoStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
175+
private function writeTableStyles(XMLWriter $xmlWriter, PhpWord $phpWord)
153176
{
154177
$sections = $phpWord->getSections();
155178
foreach ($sections as $section) {

src/PhpWord/Writer/ODText/Part/Styles.php

Lines changed: 129 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1919

20+
use PhpOffice\PhpWord\Settings;
2021
use PhpOffice\PhpWord\Shared\XMLWriter;
22+
use PhpOffice\PhpWord\Style;
2123

2224
/**
23-
* ODText styloes part writer: styles.xml
25+
* ODText styles part writer: styles.xml
2426
*/
2527
class Styles extends AbstractPart
2628
{
@@ -43,14 +45,14 @@ public function write()
4345

4446
// Office styles
4547
$xmlWriter->startElement('office:styles');
46-
$this->writePart($xmlWriter, 'Default');
47-
$this->writePart($xmlWriter, 'Named');
48+
$this->writeDefault($xmlWriter);
49+
$this->writeNamed($xmlWriter);
4850
$xmlWriter->endElement();
4951

5052
// Automatic styles
5153
$xmlWriter->startElement('office:automatic-styles');
52-
$this->writePart($xmlWriter, 'PageLayout');
53-
$this->writePart($xmlWriter, 'Master');
54+
$this->writePageLayout($xmlWriter);
55+
$this->writeMaster($xmlWriter);
5456
$xmlWriter->endElement();
5557

5658
$xmlWriter->endElement(); // office:document-styles
@@ -59,15 +61,129 @@ public function write()
5961
}
6062

6163
/**
62-
* Write style part
63-
*
64-
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
65-
* @param string $subStyle
64+
* Write default styles
65+
*/
66+
private function writeDefault(XMLWriter $xmlWriter)
67+
{
68+
$xmlWriter->startElement('style:default-style');
69+
$xmlWriter->writeAttribute('style:family', 'paragraph');
70+
71+
// Paragraph
72+
$xmlWriter->startElement('style:paragraph-properties');
73+
$xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit');
74+
$xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha');
75+
$xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging');
76+
$xmlWriter->writeAttribute('style:line-break', 'strict');
77+
$xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm');
78+
$xmlWriter->writeAttribute('style:writing-mode', 'page');
79+
$xmlWriter->endElement(); // style:paragraph-properties
80+
81+
// Font
82+
$xmlWriter->startElement('style:text-properties');
83+
$xmlWriter->writeAttribute('style:use-window-font-color', 'true');
84+
$xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName());
85+
$xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt');
86+
$xmlWriter->writeAttribute('fo:language', 'fr');
87+
$xmlWriter->writeAttribute('fo:country', 'FR');
88+
$xmlWriter->writeAttribute('style:letter-kerning', 'true');
89+
$xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2');
90+
$xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt');
91+
$xmlWriter->writeAttribute('style:language-asian', 'zh');
92+
$xmlWriter->writeAttribute('style:country-asian', 'CN');
93+
$xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2');
94+
$xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt');
95+
$xmlWriter->writeAttribute('style:language-complex', 'hi');
96+
$xmlWriter->writeAttribute('style:country-complex', 'IN');
97+
$xmlWriter->writeAttribute('fo:hyphenate', 'false');
98+
$xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2');
99+
$xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2');
100+
$xmlWriter->endElement(); // style:text-properties
101+
102+
$xmlWriter->endElement(); // style:default-style
103+
}
104+
105+
/**
106+
* Write named styles
107+
*/
108+
private function writeNamed(XMLWriter $xmlWriter)
109+
{
110+
$styles = Style::getStyles();
111+
if (count($styles) > 0) {
112+
foreach ($styles as $style) {
113+
if ($style->isAuto() === false) {
114+
$styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style));
115+
if (class_exists($styleClass)) {
116+
/** @var $styleWriter \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle Type hint */
117+
$styleWriter = new $styleClass($xmlWriter, $style);
118+
$styleWriter->write();
119+
}
120+
}
121+
}
122+
}
123+
}
124+
/**
125+
* Write page layout styles
126+
*/
127+
private function writePageLayout(XMLWriter $xmlWriter)
128+
{
129+
$xmlWriter->startElement('style:page-layout');
130+
$xmlWriter->writeAttribute('style:name', 'Mpm1');
131+
132+
$xmlWriter->startElement('style:page-layout-properties');
133+
$xmlWriter->writeAttribute('fo:page-width', "21.001cm");
134+
$xmlWriter->writeAttribute('fo:page-height', '29.7cm');
135+
$xmlWriter->writeAttribute('style:num-format', '1');
136+
$xmlWriter->writeAttribute('style:print-orientation', 'portrait');
137+
$xmlWriter->writeAttribute('fo:margin-top', '2.501cm');
138+
$xmlWriter->writeAttribute('fo:margin-bottom', '2cm');
139+
$xmlWriter->writeAttribute('fo:margin-left', '2.501cm');
140+
$xmlWriter->writeAttribute('fo:margin-right', '2.501cm');
141+
$xmlWriter->writeAttribute('style:writing-mode', 'lr-tb');
142+
$xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0');
143+
$xmlWriter->writeAttribute('style:layout-grid-lines', '25199');
144+
$xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm');
145+
$xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm');
146+
$xmlWriter->writeAttribute('style:layout-grid-mode', 'none');
147+
$xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false');
148+
$xmlWriter->writeAttribute('style:layout-grid-print', 'false');
149+
$xmlWriter->writeAttribute('style:layout-grid-display', 'false');
150+
$xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm');
151+
$xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true');
152+
$xmlWriter->writeAttribute('style:footnote-max-height', '0cm');
153+
154+
$xmlWriter->startElement('style:footnote-sep');
155+
$xmlWriter->writeAttribute('style:width', '0.018cm');
156+
$xmlWriter->writeAttribute('style:line-style', 'solid');
157+
$xmlWriter->writeAttribute('style:adjustment', 'left');
158+
$xmlWriter->writeAttribute('style:rel-width', '25%');
159+
$xmlWriter->writeAttribute('style:color', '#000000');
160+
$xmlWriter->endElement(); //style:footnote-sep
161+
162+
$xmlWriter->endElement(); // style:page-layout-properties
163+
164+
165+
$xmlWriter->startElement('style:header-style');
166+
$xmlWriter->endElement(); // style:header-style
167+
168+
$xmlWriter->startElement('style:footer-style');
169+
$xmlWriter->endElement(); // style:footer-style
170+
171+
$xmlWriter->endElement(); // style:page-layout
172+
}
173+
/**
174+
* Write master style
66175
*/
67-
private function writePart(XMLWriter $xmlWriter, $subStyle)
176+
private function writeMaster(XMLWriter $xmlWriter)
68177
{
69-
$writerClass = "PhpOffice\\PhpWord\\Writer\\ODText\\Style\\{$subStyle}Style";
70-
$styleWriter = new $writerClass($xmlWriter);
71-
$styleWriter->write();
178+
$xmlWriter = $this->getXmlWriter();
179+
180+
$xmlWriter->startElement('office:master-styles');
181+
182+
$xmlWriter->startElement('style:master-page');
183+
$xmlWriter->writeAttribute('style:name', 'Standard');
184+
$xmlWriter->writeAttribute('style:page-layout-name', 'Mpm1');
185+
$xmlWriter->endElement(); // style:master-page
186+
187+
$xmlWriter->endElement(); // office:master-styles
72188
}
73189
}

src/PhpWord/Writer/ODText/Style/DefaultStyle.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/PhpWord/Writer/ODText/Style/MasterStyle.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)