Skip to content

Commit 0ed956a

Browse files
committed
Merge pull request #87 from ivanlanin/develop
Point to twip converter, set normal paragraph style, and parent & next style for paragraph
2 parents f92e577 + 307f568 commit 0ed956a

File tree

9 files changed

+269
-8
lines changed

9 files changed

+269
-8
lines changed

Classes/PHPWord.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
198198
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
199199
}
200200

201+
/**
202+
* Set default paragraph style definition to styles.xml
203+
*
204+
* @param array $styles Paragraph style definition
205+
*/
206+
public function setDefaultParagraphStyle($styles)
207+
{
208+
PHPWord_Style::setDefaultParagraphStyle($styles);
209+
}
210+
201211
/**
202212
* Adds a hyperlink style to styles.xml
203213
*

Classes/PHPWord/Shared/Font.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,16 @@ public static function pixelSizeToTwips($sizeInPixel = 1)
7777
{
7878
return self::centimeterSizeToTwips($sizeInPixel / 37.795275591);
7979
}
80+
81+
/**
82+
* Calculate twip based on point size, used mainly for paragraph spacing
83+
*
84+
* @param int|float $sizeInPoint Size in point
85+
* @return int|float Size (in twips)
86+
*/
87+
public static function pointSizeToTwips($sizeInPoint = 1)
88+
{
89+
return ($sizeInPoint * 20);
90+
}
91+
8092
}

Classes/PHPWord/Style.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public static function addTitleStyle($titleCount, $styleFont, $styleParagraph =
140140
}
141141
}
142142

143+
/**
144+
* Set default paragraph style
145+
*
146+
* @param array $styles Paragraph style definition
147+
*/
148+
public static function setDefaultParagraphStyle($styles)
149+
{
150+
self::addParagraphStyle('Normal', $styles);
151+
}
152+
143153
/**
144154
* Get all styles
145155
*

Classes/PHPWord/Style/Paragraph.php

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ class PHPWord_Style_Paragraph
8080
*/
8181
private $_hanging;
8282

83+
/**
84+
* Parent style
85+
*
86+
* @var string
87+
*/
88+
private $_basedOn;
89+
90+
/**
91+
* Style for next paragraph
92+
*
93+
* @var string
94+
*/
95+
private $_next;
96+
8397
/**
8498
* New Paragraph Style
8599
*/
@@ -92,6 +106,8 @@ public function __construct()
92106
$this->_tabs = null;
93107
$this->_indent = null;
94108
$this->_hanging = null;
109+
$this->_basedOn = 'Normal';
110+
$this->_next = null;
95111
}
96112

97113
/**
@@ -231,6 +247,16 @@ public function setIndent($pValue = null)
231247
return $this;
232248
}
233249

250+
/**
251+
* Get hanging
252+
*
253+
* @return int
254+
*/
255+
public function getHanging()
256+
{
257+
return $this->_hanging;
258+
}
259+
234260
/**
235261
* Set hanging
236262
*
@@ -244,22 +270,57 @@ public function setHanging($pValue = null)
244270
}
245271

246272
/**
247-
* Get hanging
273+
* Get tabs
248274
*
249-
* @return int
275+
* @return PHPWord_Style_Tabs
250276
*/
251-
public function getHanging()
277+
public function getTabs()
252278
{
253-
return $this->_hanging;
279+
return $this->_tabs;
254280
}
255281

256282
/**
257-
* Get tabs
283+
* Get parent style ID
258284
*
259-
* @return PHPWord_Style_Tabs
285+
* @return string
260286
*/
261-
public function getTabs()
287+
public function getBasedOn()
262288
{
263-
return $this->_tabs;
289+
return $this->_basedOn;
290+
}
291+
292+
/**
293+
* Set parent style ID
294+
*
295+
* @param string $pValue
296+
* @return PHPWord_Style_Paragraph
297+
*/
298+
public function setBasedOn($pValue = 'Normal')
299+
{
300+
$this->_basedOn = $pValue;
301+
return $this;
264302
}
303+
304+
/**
305+
* Get style for next paragraph
306+
*
307+
* @return string
308+
*/
309+
public function getNext()
310+
{
311+
return $this->_next;
312+
}
313+
314+
/**
315+
* Set style for next paragraph
316+
*
317+
* @param string $pValue
318+
* @return PHPWord_Style_Paragraph
319+
*/
320+
public function setNext($pValue = null)
321+
{
322+
$this->_next = $pValue;
323+
return $this;
324+
}
325+
265326
}

Classes/PHPWord/Writer/Word2007/Styles.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,30 @@ public function writeStyles(PHPWord $pPHPWord = null)
5959

6060
// Write Style Definitions
6161
$styles = PHPWord_Style::getStyles();
62+
63+
// Write normal paragraph style
64+
$normalStyle = null;
65+
if (array_key_exists('Normal', $styles)) {
66+
$normalStyle = $styles['Normal'];
67+
}
68+
$objWriter->startElement('w:style');
69+
$objWriter->writeAttribute('w:type', 'paragraph');
70+
$objWriter->writeAttribute('w:default', '1');
71+
$objWriter->writeAttribute('w:styleId', 'Normal');
72+
$objWriter->startElement('w:name');
73+
$objWriter->writeAttribute('w:val', 'Normal');
74+
$objWriter->endElement();
75+
if (!is_null($normalStyle)) {
76+
$this->_writeParagraphStyle($objWriter, $normalStyle);
77+
}
78+
$objWriter->endElement();
79+
80+
// Write other styles
6281
if (count($styles) > 0) {
6382
foreach ($styles as $styleName => $style) {
83+
if ($styleName == 'Normal') {
84+
continue;
85+
}
6486
if ($style instanceof PHPWord_Style_Font) {
6587

6688
$paragraphStyle = $style->getParagraphStyle();
@@ -92,6 +114,10 @@ public function writeStyles(PHPWord $pPHPWord = null)
92114
$objWriter->endElement();
93115

94116
if (!is_null($paragraphStyle)) {
117+
// Point parent style to Normal
118+
$objWriter->startElement('w:basedOn');
119+
$objWriter->writeAttribute('w:val', 'Normal');
120+
$objWriter->endElement();
95121
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
96122
}
97123

@@ -109,6 +135,22 @@ public function writeStyles(PHPWord $pPHPWord = null)
109135
$objWriter->writeAttribute('w:val', $styleName);
110136
$objWriter->endElement();
111137

138+
// Parent style
139+
$basedOn = $style->getBasedOn();
140+
if (!is_null($basedOn)) {
141+
$objWriter->startElement('w:basedOn');
142+
$objWriter->writeAttribute('w:val', $basedOn);
143+
$objWriter->endElement();
144+
}
145+
146+
// Next paragraph style
147+
$next = $style->getNext();
148+
if (!is_null($next)) {
149+
$objWriter->startElement('w:next');
150+
$objWriter->writeAttribute('w:val', $next);
151+
$objWriter->endElement();
152+
}
153+
112154
$this->_writeParagraphStyle($objWriter, $style);
113155
$objWriter->endElement();
114156

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
7373
$objWriter->save('helloWorld.docx');
7474
```
7575

76+
##### Measurement units
77+
78+
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
79+
80+
You can use PHPWord helper functions to convert inches, centimeters, or points to twips.
81+
82+
```php
83+
// Paragraph with 6 points space after
84+
$PHPWord->addParagraphStyle('My Style', array(
85+
'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6))
86+
);
87+
88+
$section = $PHPWord->createSection();
89+
$sectionStyle = $section->getSettings();
90+
// half inch left margin
91+
$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5));
92+
// 2 cm right margin
93+
$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
94+
```
95+
7696
<a name="sections"></a>
7797
#### Sections
7898

Tests/PHPWord/Shared/FontTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord_Shared_Font;
7+
8+
/**
9+
* Class PHPWord_Writer_Shared_FontTest
10+
*
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class PHPWord_Writer_Shared_FontTest extends \PHPUnit_Framework_TestCase
15+
{
16+
17+
/**
18+
* Test various conversions
19+
*/
20+
public function testConversions()
21+
{
22+
$PHPWord = new PHPWord();
23+
24+
$original = 1;
25+
26+
$result = PHPWord_Shared_Font::fontSizeToPixels($original);
27+
$this->assertEquals($original * 16 / 12, $result);
28+
29+
$result = PHPWord_Shared_Font::inchSizeToPixels($original);
30+
$this->assertEquals($original * 96, $result);
31+
32+
$result = PHPWord_Shared_Font::centimeterSizeToPixels($original);
33+
$this->assertEquals($original * 37.795275591, $result);
34+
35+
$result = PHPWord_Shared_Font::centimeterSizeToTwips($original);
36+
$this->assertEquals($original * 565.217, $result);
37+
38+
$result = PHPWord_Shared_Font::inchSizeToTwips($original);
39+
$this->assertEquals($original * 565.217 * 2.54, $result);
40+
41+
$result = PHPWord_Shared_Font::pixelSizeToTwips($original);
42+
$this->assertEquals($original * 565.217 / 37.795275591, $result);
43+
44+
$result = PHPWord_Shared_Font::pointSizeToTwips($original);
45+
$this->assertEquals($original * 20, $result);
46+
}
47+
48+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord_Writer_Word2007_Styles;
7+
8+
/**
9+
* Class PHPWord_Writer_Word2007_StylesTest
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class PHPWord_Writer_Word2007_StylesTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* Executed before each method of the class
17+
*/
18+
public function tearDown()
19+
{
20+
TestHelperDOCX::clear();
21+
}
22+
23+
/**
24+
* Test write styles
25+
*/
26+
public function testWriteStyles()
27+
{
28+
$PHPWord = new PHPWord();
29+
30+
$defaultStyle = array('align' => 'both');
31+
$baseStyle = array('basedOn' => 'Normal');
32+
$newStyle = array('basedOn' => 'Base Style', 'next' => 'Normal');
33+
$PHPWord->setDefaultParagraphStyle($defaultStyle);
34+
$PHPWord->addParagraphStyle('Base Style', $baseStyle);
35+
$PHPWord->addParagraphStyle('New Style', $newStyle);
36+
$doc = TestHelperDOCX::getDocument($PHPWord);
37+
$file = 'word/styles.xml';
38+
39+
// Normal style generated?
40+
$path = '/w:styles/w:style[@w:styleId="Normal"]/w:name';
41+
$element = $doc->getElement($path, $file);
42+
$this->assertEquals('Normal', $element->getAttribute('w:val'));
43+
44+
// Parent style referenced?
45+
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:basedOn';
46+
$element = $doc->getElement($path, $file);
47+
$this->assertEquals('Base Style', $element->getAttribute('w:val'));
48+
49+
// Next paragraph style correct?
50+
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:next';
51+
$element = $doc->getElement($path, $file);
52+
$this->assertEquals('Normal', $element->getAttribute('w:val'));
53+
}
54+
55+
}

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Changes in branch for release 0.7.1 :
3939
- Feature: (ivanlanin) GH-48 GH-86 - Section: Multicolumn and section break
4040
- Feature: (RomanSyroeshko) GH-46 GH-47 GH-83 - Template : Ability to apply XSL style sheet to Template
4141
- QA: (Progi1984) - UnitTests
42+
- Feature: (ivanlanin) - General: PHPWord_Shared_Font::pointSizeToTwips() converter
43+
- Feature: (ivanlanin) - Paragraph: Ability to define normal paragraph style with PHPWord::setNormalStyle()
44+
- Feature: (ivanlanin) - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next)
4245

4346
Changes in branch for release 0.7.0 :
4447
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found

0 commit comments

Comments
 (0)