Skip to content

Commit 1fc73d4

Browse files
committed
Rasamassen PR's 2817, 2834, and 2835
I made RTF usable some time ago. @rasamassen has recently done a lot of useful work to cover many missing areas. Since who knows when those changes will be merged, I intend to incorporate much of that work. I will do this with several pushes, each based on one or more of those changes. This one is based on PR PHPOffice#2817, PR PHPOffice#2834, and PR PHPOffice#2835.
1 parent cbda0b6 commit 1fc73d4

File tree

5 files changed

+223
-5
lines changed

5 files changed

+223
-5
lines changed

docs/usage/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $fontStyle = new \PhpOffice\PhpWord\Style\Font();
5555
$fontStyle->setBold(true);
5656
$fontStyle->setName('Tahoma');
5757
$fontStyle->setSize(13);
58-
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
58+
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodore Roosevelt)');
5959
$myTextElement->setFontStyle($fontStyle);
6060

6161
// Saving the document as OOXML file...

src/PhpWord/Writer/RTF/Part/Document.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ private function writeFormatting()
116116
$content .= '\lang' . $langId;
117117
$content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
118118
$content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
119-
if ($docSettings->hasEvenAndOddHeaders()) {
119+
if ($docSettings->hasEvenAndOddHeaders() || $docSettings->hasMirrorMargins()) {
120120
$content .= '\\facingp';
121121
}
122+
if ($docSettings->hasMirrorMargins()) {
123+
$content .= '\\margmirror';
124+
}
125+
if ($docSettings->hasBookFoldPrinting()) {
126+
$content .= '\\bookfold\\landscape';
127+
}
122128
$content .= PHP_EOL;
123129

124130
return $content;

src/PhpWord/Writer/RTF/Style/Section.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\RTF\Style;
2020

21+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
2122
use PhpOffice\PhpWord\Style\Section as SectionStyle;
2223

2324
/**
@@ -44,17 +45,48 @@ public function write()
4445
$content .= '\sectd ';
4546

4647
// Size & margin
47-
$content .= $this->getValueIf($style->getPageSizeW() !== null, '\pgwsxn' . round($style->getPageSizeW()));
48-
$content .= $this->getValueIf($style->getPageSizeH() !== null, '\pghsxn' . round($style->getPageSizeH()));
48+
if ($this->getParentWriter() !== null && $this->getParentWriter()->getPhpWord()->getSettings()->hasBookFoldPrinting()) {
49+
$style->setOrientation(SectionStyle::ORIENTATION_LANDSCAPE);
50+
$content .= $this->getValueIf($style->getPageSizeW() !== null, '\pgwsxn' . round($style->getPageSizeW()) / 2);
51+
$content .= $this->getValueIf($style->getPageSizeH() !== null, '\pghsxn' . round($style->getPageSizeH()));
52+
} else {
53+
$content .= $this->getValueIf($style->getPageSizeW() !== null, '\pgwsxn' . round($style->getPageSizeW()));
54+
$content .= $this->getValueIf($style->getPageSizeH() !== null, '\pghsxn' . round($style->getPageSizeH()));
55+
}
4956
$content .= ' ';
57+
5058
$content .= $this->getValueIf($style->getMarginTop() !== null, '\margtsxn' . round($style->getMarginTop()));
5159
$content .= $this->getValueIf($style->getMarginRight() !== null, '\margrsxn' . round($style->getMarginRight()));
5260
$content .= $this->getValueIf($style->getMarginBottom() !== null, '\margbsxn' . round($style->getMarginBottom()));
5361
$content .= $this->getValueIf($style->getMarginLeft() !== null, '\marglsxn' . round($style->getMarginLeft()));
5462
$content .= $this->getValueIf($style->getHeaderHeight() !== null, '\headery' . round($style->getHeaderHeight()));
5563
$content .= $this->getValueIf($style->getFooterHeight() !== null, '\footery' . round($style->getFooterHeight()));
56-
$content .= $this->getValueIf($style->getGutter() !== null, '\guttersxn' . round($style->getGutter()));
64+
$content .= $this->getValueIf($style->getGutter() !== null && $style->getGutter() !== SectionStyle::DEFAULT_GUTTER, '\guttersxn' . round($style->getGutter()));
5765
$content .= $this->getValueIf($style->getPageNumberingStart() !== null, '\pgnstarts' . $style->getPageNumberingStart() . '\pgnrestart');
66+
67+
$content .= $this->getValueIf($style->getColsNum() !== null && $style->getColsNum() !== SectionStyle::DEFAULT_COLUMN_COUNT, '\cols' . $style->getColsNum());
68+
$content .= $this->getValueIf($style->getColsSpace() !== null && $style->getColsNum() !== SectionStyle::DEFAULT_COLUMN_COUNT, '\colsx' . round($style->getColsSpace()));
69+
70+
// Break Type
71+
$breakTypes = [
72+
'nextPage' => '\sbkpage',
73+
'nextColumn' => '\sbkcol',
74+
'continuous' => '\sbknone',
75+
'evenPage' => '\sbkeven',
76+
'oddPage' => '\sbkodd',
77+
];
78+
if (isset($breakTypes[$style->getBreakType()])) {
79+
$content .= $breakTypes[$style->getBreakType()];
80+
}
81+
82+
// Vertical Align
83+
$verticalAlign = [
84+
VerticalJc::TOP => '\vertalt',
85+
VerticalJc::CENTER => '\vertalc',
86+
VerticalJc::BOTH => '\vertalj',
87+
VerticalJc::BOTTOM => '\vertalb',
88+
];
89+
$content .= $verticalAlign[(string) $style->getVAlign()] ?? '';
5890
$content .= ' ';
5991

6092
// Borders
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
19+
namespace PhpOffice\PhpWordTests\Writer\RTF;
20+
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Writer\RTF;
23+
24+
/**
25+
* Test class for PhpOffice\PhpWord\Writer\RTF\Style subnamespace.
26+
*/
27+
class BookFoldTest extends \PHPUnit\Framework\TestCase
28+
{
29+
public function testBookFold(): void
30+
{
31+
$phpWord = new PhpWord();
32+
$phpWord->getSettings()->setBookFoldPrinting(true);
33+
$section1 = $phpWord->addSection();
34+
$textRun1 = $section1->addTextRun();
35+
$textRun1->addText('Section 1 Paragraph 1');
36+
$writer = new RTF($phpWord);
37+
$content = $writer->getContent();
38+
$expected = '\bookfold\landscape';
39+
self::assertStringContainsString($expected, $content);
40+
}
41+
42+
public function testMirrorMargins(): void
43+
{
44+
$phpWord = new PhpWord();
45+
$phpWord->getSettings()->setMirrorMargins(true);
46+
$section1 = $phpWord->addSection();
47+
$textRun1 = $section1->addTextRun();
48+
$textRun1->addText('Section 1 Paragraph 1');
49+
$writer = new RTF($phpWord);
50+
$content = $writer->getContent();
51+
$expected = '\margmirror';
52+
self::assertStringContainsString($expected, $content);
53+
$expected = '\facingp';
54+
self::assertStringContainsString($expected, $content);
55+
}
56+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
19+
namespace PhpOffice\PhpWordTests\Writer\RTF\Style;
20+
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
23+
use PhpOffice\PhpWord\Writer\RTF;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class SectionTest extends TestCase
27+
{
28+
/** @dataProvider verticalAlignProvider */
29+
public function testVerticalAlign(string $vAlign, string $expect): void
30+
{
31+
$phpWord = new PhpWord();
32+
$section = $phpWord->addSection();
33+
$style = $section->getStyle();
34+
$style->setVAlign($vAlign);
35+
$writer = new RTF($phpWord);
36+
self::assertStringContainsString($expect, $writer->getContent());
37+
}
38+
39+
public static function verticalAlignProvider(): array
40+
{
41+
return [
42+
[VerticalJc::TOP, '\vertalt'],
43+
[VerticalJc::CENTER, '\vertalc'],
44+
[VerticalJc::BOTH, '\vertalj'],
45+
[VerticalJc::BOTTOM, '\vertalb'],
46+
];
47+
}
48+
49+
public function testNoVerticalAlignNoBreakType(): void
50+
{
51+
$phpWord = new PhpWord();
52+
$section = $phpWord->addSection();
53+
$style = $section->getStyle();
54+
$writer = new RTF($phpWord);
55+
$content = $writer->getContent();
56+
self::assertStringNotContainsString('vert', $content);
57+
self::assertStringNotContainsString('sbk', $content);
58+
}
59+
60+
/** @dataProvider breakTypeProvider */
61+
public function testBreakType(string $breakType, string $expect): void
62+
{
63+
$phpWord = new PhpWord();
64+
$section = $phpWord->addSection();
65+
$style = $section->getStyle();
66+
$style->setBreakType($breakType);
67+
$writer = new RTF($phpWord);
68+
$content = $writer->getContent();
69+
self::assertStringContainsString($expect, $content);
70+
}
71+
72+
public static function breakTypeProvider(): array
73+
{
74+
return [
75+
['nextPage', '\sbkpage'],
76+
['nextColumn', '\sbkcol'],
77+
['continuous', '\sbknone'],
78+
['evenPage', '\sbkeven'],
79+
['oddPage', '\sbkodd'],
80+
];
81+
}
82+
83+
public function testColsNum(): void
84+
{
85+
$phpWord = new PhpWord();
86+
$section = $phpWord->addSection();
87+
$style = $section->getStyle();
88+
$style->setColsNum(5);
89+
$style->setColsSpace(7);
90+
$writer = new RTF($phpWord);
91+
$content = $writer->getContent();
92+
self::assertStringContainsString('\cols5', $content);
93+
self::assertStringContainsString('\colsx7', $content);
94+
}
95+
96+
public function testPageSize(): void
97+
{
98+
$phpWord = new PhpWord();
99+
$section = $phpWord->addSection();
100+
$temp1 = $section->getStyle()->getOrientation();
101+
self::assertSame('portrait', $temp1);
102+
$writer = new RTF($phpWord);
103+
$content = $writer->getContent();
104+
105+
self::assertStringContainsString('\sectd \pgwsxn11906\pghsxn16838', $content);
106+
$temp2 = $section->getStyle()->getOrientation();
107+
self::assertSame('portrait', $temp2);
108+
}
109+
110+
public function testPageSizeBookFold(): void
111+
{
112+
$phpWord = new PhpWord();
113+
$section = $phpWord->addSection();
114+
$phpWord->getSettings()->setBookFoldPrinting(true);
115+
$temp1 = $section->getStyle()->getOrientation();
116+
self::assertSame('portrait', $temp1);
117+
$writer = new RTF($phpWord);
118+
$content = $writer->getContent();
119+
120+
self::assertStringContainsString('\sectd \pgwsxn8419\pghsxn11906', $content);
121+
$temp2 = $section->getStyle()->getOrientation();
122+
self::assertSame('landscape', $temp2);
123+
}
124+
}

0 commit comments

Comments
 (0)