Skip to content

Commit 27d18fd

Browse files
committed
Merge branch '#234-#235-basjan' into develop
2 parents 17e2f02 + 8c9e511 commit 27d18fd

File tree

10 files changed

+355
-8
lines changed

10 files changed

+355
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1010

1111
- Image: Ability to define relative and absolute positioning - @basjan GH-217
1212
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
13-
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
13+
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 GH-229
1414
- TextBox: Ability to add table inside textbox - @basjan GH-231
1515
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
16+
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
1617

1718
### Bugfixes
1819

1920
- Header: All images added to the second header were assigned to the first header - @basjan GH-222
21+
- Conversion: Fix conversion from cm to pixel, pixel to cm, and pixel to point - @basjan GH-233 GH-234
2022

2123
### Deprecated
2224

samples/Sample_14_ListItem.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@
5454
$section->addListItem('List Item 7', 0, 'myOwnStyle', $predefinedMultilevel, 'P-Style');
5555
$section->addTextBreak(2);
5656

57+
$section->addText('List with inline formatting.');
58+
$listItemRun = $section->addListItemRun();
59+
$listItemRun->addText('List item 1');
60+
$listItemRun->addText(' in bold', array('bold'=>true));
61+
$listItemRun = $section->addListItemRun();
62+
$listItemRun->addText('List item 2');
63+
$listItemRun->addText(' in italic', array('italic'=>true));
64+
$listItemRun = $section->addListItemRun();
65+
$listItemRun->addText('List item 3');
66+
$listItemRun->addText(' underlined', array('underline'=>'dash'));
67+
$section->addTextBreak(2);
68+
5769
// Save file
5870
echo write($phpWord, basename(__FILE__, '.php'), $writers);
5971
if (!CLI) {

src/PhpWord/Element/AbstractContainer.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme
106106
$elementClass = substr(get_class($this), 0, strrpos(get_class($this), '\\')) . '\\' . $elementName;
107107

108108
// Reset paragraph style for footnote and textrun. They have their own
109-
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
109+
if (in_array($this->container, array('textrun', 'footnote', 'endnote', 'listitemrun'))) {
110110
$paragraphStyle = null;
111111
}
112112

@@ -205,6 +205,26 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n
205205
return $element;
206206
}
207207

208+
/**
209+
* Add listitemrun element
210+
*
211+
* @param int $depth
212+
* @param mixed $fontStyle
213+
* @param mixed $listStyle
214+
* @param mixed $paragraphStyle
215+
* @return \PhpOffice\PhpWord\Element\ListItemRun
216+
*/
217+
public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
218+
{
219+
$this->checkValidity('ListItemRun');
220+
221+
$element = new ListItemRun($depth, $fontStyle, $listStyle, $paragraphStyle);
222+
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
223+
$this->addElement($element);
224+
225+
return $element;
226+
}
227+
208228
/**
209229
* Add table element
210230
*
@@ -345,7 +365,7 @@ public function addTextBox($style = null)
345365
private function checkValidity($method)
346366
{
347367
// Valid containers for each element
348-
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox');
368+
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox', 'listitemrun');
349369
$validContainers = array(
350370
'Text' => $allContainers,
351371
'Link' => $allContainers,
@@ -354,6 +374,7 @@ private function checkValidity($method)
354374
'Object' => $allContainers,
355375
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
356376
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
377+
'ListItemRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
357378
'Table' => array('section', 'header', 'footer', 'textbox'),
358379
'CheckBox' => array('section', 'header', 'footer', 'cell'),
359380
'TextBox' => array('section', 'header', 'footer', 'cell'),
@@ -395,7 +416,7 @@ private function checkValidity($method)
395416
*/
396417
private function checkElementDocPart()
397418
{
398-
$inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox'));
419+
$inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox', 'listitemrun'));
399420
$docPart = $inOtherPart ? $this->getDocPart() : $this->container;
400421
$docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId;
401422
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');

src/PhpWord/Element/ListItemRun.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Element;
19+
20+
use PhpOffice\PhpWord\Shared\String;
21+
use PhpOffice\PhpWord\Style\ListItem as ListItemStyle;
22+
use PhpOffice\PhpWord\Style\Paragraph;
23+
24+
/**
25+
* List item element
26+
*/
27+
class ListItemRun extends TextRun
28+
{
29+
/**
30+
* ListItem Style
31+
*
32+
* @var \PhpOffice\PhpWord\Style\ListItem
33+
*/
34+
private $style;
35+
36+
/**
37+
* ListItem Depth
38+
*
39+
* @var int
40+
*/
41+
private $depth;
42+
43+
/**
44+
* Create a new ListItem
45+
*
46+
* @param int $depth
47+
* @param mixed $fontStyle
48+
* @param array|string|null $listStyle
49+
* @param mixed $paragraphStyle
50+
*/
51+
public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null)
52+
{
53+
$this->container = 'listitemrun';
54+
$this->depth = $depth;
55+
56+
// Version >= 0.10.0 will pass numbering style name. Older version will use old method
57+
if (!is_null($listStyle) && is_string($listStyle)) {
58+
$this->style = new ListItemStyle($listStyle);
59+
} else {
60+
$this->style = $this->setStyle(new ListItemStyle(), $listStyle, true);
61+
}
62+
$this->paragraphStyle = $this->setStyle(new Paragraph(), $paragraphStyle);
63+
}
64+
65+
/**
66+
* Get ListItem style
67+
*/
68+
public function getStyle()
69+
{
70+
return $this->style;
71+
}
72+
73+
/**
74+
* Get ListItem depth
75+
*/
76+
public function getDepth()
77+
{
78+
return $this->depth;
79+
}
80+
}

src/PhpWord/Element/TextRun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TextRun extends AbstractContainer
2929
*
3030
* @var string|\PhpOffice\PhpWord\Style\Paragraph
3131
*/
32-
private $paragraphStyle;
32+
protected $paragraphStyle;
3333

3434
/**
3535
* Create new instance

src/PhpWord/Shared/Drawing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function emuToPixels($value = 0)
5656
*/
5757
public static function pixelsToPoints($value = 0)
5858
{
59-
return $value * 0.67777777;
59+
return $value * 0.75;
6060
}
6161

6262
/**

src/PhpWord/Writer/Word2007/Element/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function write()
4141
$xmlWriter = $this->getXmlWriter();
4242
$container = $this->getElement();
4343
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
44-
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
44+
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;
4545

4646
// Loop through subelements
4747
$subelements = $container->getElements();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
19+
20+
use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
21+
22+
/**
23+
* ListItemRun element writer
24+
*
25+
* @since 0.10.0
26+
*/
27+
class ListItemRun extends AbstractElement
28+
{
29+
/**
30+
* Write list item element
31+
*/
32+
public function write()
33+
{
34+
$xmlWriter = $this->getXmlWriter();
35+
$element = $this->getElement();
36+
37+
$xmlWriter->startElement('w:p');
38+
39+
$xmlWriter->startElement('w:pPr');
40+
$paragraphStyle = $element->getParagraphStyle();
41+
$styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
42+
$styleWriter->setIsInline(true);
43+
$styleWriter->write();
44+
45+
$xmlWriter->startElement('w:numPr');
46+
$xmlWriter->startElement('w:ilvl');
47+
$xmlWriter->writeAttribute('w:val', $element->getDepth());
48+
$xmlWriter->endElement(); // w:ilvl
49+
$xmlWriter->startElement('w:numId');
50+
$xmlWriter->writeAttribute('w:val', $element->getStyle()->getNumId());
51+
$xmlWriter->endElement(); // w:numId
52+
$xmlWriter->endElement(); // w:numPr
53+
54+
$xmlWriter->endElement(); // w:pPr
55+
56+
$containerWriter = new Container($xmlWriter, $element);
57+
$containerWriter->write();
58+
59+
$xmlWriter->endElement(); // w:p
60+
}
61+
}

0 commit comments

Comments
 (0)