TextBreak: Allow font/paragraph styling for text break
This commit is contained in:
parent
088caa2d36
commit
44d2501293
|
|
@ -156,12 +156,14 @@ class PHPWord_Section
|
|||
/**
|
||||
* Add a TextBreak Element
|
||||
*
|
||||
* @param int $count
|
||||
* @param int $count
|
||||
* @param null|string|array|PHPWord_Style_Font $fontStyle
|
||||
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
|
||||
*/
|
||||
public function addTextBreak($count = 1)
|
||||
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -79,14 +79,16 @@ class PHPWord_Section_Footer
|
|||
}
|
||||
|
||||
/**
|
||||
* Add a TextBreak Element
|
||||
* Add TextBreak
|
||||
*
|
||||
* @param int $count
|
||||
* @param int $count
|
||||
* @param null|string|array|PHPWord_Style_Font $fontStyle
|
||||
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
|
||||
*/
|
||||
public function addTextBreak($count = 1)
|
||||
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -108,14 +108,16 @@ class PHPWord_Section_Header
|
|||
}
|
||||
|
||||
/**
|
||||
* Add a TextBreak Element
|
||||
* Add TextBreak
|
||||
*
|
||||
* @param int $count
|
||||
* @param int $count
|
||||
* @param null|string|array|PHPWord_Style_Font $fontStyle
|
||||
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
|
||||
*/
|
||||
public function addTextBreak($count = 1)
|
||||
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -146,13 +146,17 @@ class PHPWord_Section_Table_Cell
|
|||
}
|
||||
|
||||
/**
|
||||
* Add a TextBreak Element
|
||||
* Add TextBreak
|
||||
*
|
||||
* @param int $count
|
||||
* @param int $count
|
||||
* @param null|string|array|PHPWord_Style_Font $fontStyle
|
||||
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
|
||||
*/
|
||||
public function addTextBreak()
|
||||
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -30,11 +30,91 @@
|
|||
*/
|
||||
class PHPWord_Section_TextBreak
|
||||
{
|
||||
/**
|
||||
* Paragraph style
|
||||
*
|
||||
* @var PHPWord_Style_Pagaraph
|
||||
*/
|
||||
private $paragraphStyle = null;
|
||||
|
||||
/**
|
||||
* Text style
|
||||
*
|
||||
* @var PHPWord_Style_Font
|
||||
*/
|
||||
private $fontStyle = null;
|
||||
|
||||
/**
|
||||
* Create a new TextBreak Element
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct($fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
if (!is_null($paragraphStyle)) {
|
||||
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
|
||||
}
|
||||
if (!is_null($fontStyle)) {
|
||||
$this->setFontStyle($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Text style
|
||||
*
|
||||
* @param null|array|\PHPWord_Style_Font $style
|
||||
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function setFontStyle($style = null, $paragraphStyle = null)
|
||||
{
|
||||
if ($style instanceof PHPWord_Style_Font) {
|
||||
$this->fontStyle = $style;
|
||||
$this->setParagraphStyle($paragraphStyle);
|
||||
} elseif (is_array($style)) {
|
||||
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
|
||||
$this->fontStyle->setArrayStyle($style);
|
||||
} else {
|
||||
$this->fontStyle = $style;
|
||||
$this->setParagraphStyle($paragraphStyle);
|
||||
}
|
||||
return $this->fontStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Text style
|
||||
*
|
||||
* @return PHPWord_Style_Font
|
||||
*/
|
||||
public function getFontStyle()
|
||||
{
|
||||
return $this->fontStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Paragraph style
|
||||
*
|
||||
* @param null|array|\PHPWord_Style_Paragraph $style
|
||||
* @return null|\PHPWord_Style_Paragraph
|
||||
*/
|
||||
public function setParagraphStyle($style = null)
|
||||
{
|
||||
if (is_array($style)) {
|
||||
$this->paragraphStyle = new PHPWord_Style_Paragraph;
|
||||
$this->paragraphStyle->setArrayStyle($style);
|
||||
} elseif ($style instanceof PHPWord_Style_Paragraph) {
|
||||
$this->paragraphStyle = $style;
|
||||
} else {
|
||||
$this->paragraphStyle = $style;
|
||||
}
|
||||
return $this->paragraphStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Paragraph style
|
||||
*
|
||||
* @return PHPWord_Style_Paragraph
|
||||
*/
|
||||
public function getParagraphStyle()
|
||||
{
|
||||
return $this->paragraphStyle;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,14 +132,16 @@ class PHPWord_Section_TextRun
|
|||
}
|
||||
|
||||
/**
|
||||
* Add a Text Break
|
||||
* Add TextBreak
|
||||
*
|
||||
* @param int $count
|
||||
* @param int $count
|
||||
* @param null|string|array|PHPWord_Style_Font $fontStyle
|
||||
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
|
||||
*/
|
||||
public function addTextBreak($count = 1)
|
||||
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
|
||||
{
|
||||
for ($i=1; $i<=$count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -477,10 +477,51 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
|
||||
/**
|
||||
* Write text break
|
||||
*
|
||||
* @param PHPWord_Shared_XMLWriter $objWriter
|
||||
* @param PHPWord_Section_TextBreak $element
|
||||
*/
|
||||
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
|
||||
protected function _writeTextBreak($objWriter, $element = null)
|
||||
{
|
||||
$objWriter->writeElement('w:p', null);
|
||||
$hasStyle = false;
|
||||
if (!is_null($element)) {
|
||||
$fontStyle = $element->getFontStyle();
|
||||
$sfIsObject = ($fontStyle instanceof PHPWord_Style_Font) ? true : false;
|
||||
$paragraphStyle = $element->getParagraphStyle();
|
||||
$spIsObject = ($paragraphStyle instanceof PHPWord_Style_Paragraph) ? true : false;
|
||||
$hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
|
||||
}
|
||||
if ($hasStyle) {
|
||||
// Paragraph style
|
||||
$objWriter->startElement('w:p');
|
||||
if ($spIsObject) {
|
||||
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
|
||||
} elseif (!$spIsObject && !is_null($paragraphStyle)) {
|
||||
$objWriter->startElement('w:pPr');
|
||||
$objWriter->startElement('w:pStyle');
|
||||
$objWriter->writeAttribute('w:val', $paragraphStyle);
|
||||
$objWriter->endElement(); // w:pStyle
|
||||
$objWriter->endElement(); // w:pPr
|
||||
}
|
||||
// Font style
|
||||
if (!is_null($fontStyle)) {
|
||||
$objWriter->startElement('w:pPr');
|
||||
if ($sfIsObject) {
|
||||
$this->_writeTextStyle($objWriter, $fontStyle);
|
||||
} elseif (!$sfIsObject && !is_null($fontStyle)) {
|
||||
$objWriter->startElement('w:rPr');
|
||||
$objWriter->startElement('w:rStyle');
|
||||
$objWriter->writeAttribute('w:val', $fontStyle);
|
||||
$objWriter->endElement(); // w:rStyle
|
||||
$objWriter->endElement(); // w:rPr
|
||||
}
|
||||
$objWriter->endElement(); // w:pPr
|
||||
}
|
||||
$objWriter->endElement(); // w:p
|
||||
} else {
|
||||
// Null element. No paragraph nor font style
|
||||
$objWriter->writeElement('w:p', null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -570,7 +611,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
|
|||
} elseif ($element instanceof PHPWord_Section_Link) {
|
||||
$this->_writeLink($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_TextBreak) {
|
||||
$this->_writeTextBreak($objWriter);
|
||||
$this->_writeTextBreak($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_ListItem) {
|
||||
$this->_writeListItem($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_Image ||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base
|
|||
} elseif ($element instanceof PHPWord_Section_Title) {
|
||||
$this->_writeTitle($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_TextBreak) {
|
||||
$this->_writeTextBreak($objWriter);
|
||||
$this->_writeTextBreak($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_PageBreak) {
|
||||
$this->_writePageBreak($objWriter);
|
||||
} elseif ($element instanceof PHPWord_Section_Table) {
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class PHPWord_Writer_Word2007_Footer extends PHPWord_Writer_Word2007_Base
|
|||
} elseif ($element instanceof PHPWord_Section_TextRun) {
|
||||
$this->_writeTextRun($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_TextBreak) {
|
||||
$this->_writeTextBreak($objWriter);
|
||||
$this->_writeTextBreak($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_Table) {
|
||||
$this->_writeTable($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_Image ||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class PHPWord_Writer_Word2007_Header extends PHPWord_Writer_Word2007_Base
|
|||
} elseif ($element instanceof PHPWord_Section_TextRun) {
|
||||
$this->_writeTextRun($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_TextBreak) {
|
||||
$this->_writeTextBreak($objWriter);
|
||||
$this->_writeTextBreak($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_Table) {
|
||||
$this->_writeTable($objWriter, $element);
|
||||
} elseif ($element instanceof PHPWord_Section_Image ||
|
||||
|
|
|
|||
|
|
@ -2,17 +2,59 @@
|
|||
namespace PHPWord\Tests\Section;
|
||||
|
||||
use PHPWord_Section_TextBreak;
|
||||
use PHPWord_Style_Paragraph;
|
||||
use PHPWord_Style_Font;
|
||||
|
||||
/**
|
||||
* @package PHPWord\Tests
|
||||
* @coversDefaultClass PHPWord_Section_TextBreak
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class TextBreakTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Executed before each method of the class
|
||||
* Construct with empty value
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
// Section Settings
|
||||
$oTextBreak = new PHPWord_Section_TextBreak();
|
||||
$object = new PHPWord_Section_TextBreak();
|
||||
$this->assertNull($object->getFontStyle());
|
||||
$this->assertNull($object->getParagraphStyle());
|
||||
}
|
||||
|
||||
$this->assertInstanceOf('PHPWord_Section_TextBreak', $oTextBreak);
|
||||
/**
|
||||
* Construct with style object
|
||||
*/
|
||||
public function testConstructWithStyleObject()
|
||||
{
|
||||
$fStyle = new PHPWord_Style_Font();
|
||||
$pStyle = new PHPWord_Style_Paragraph();
|
||||
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
|
||||
$this->assertEquals($fStyle, $object->getFontStyle());
|
||||
$this->assertEquals($pStyle, $object->getParagraphStyle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct with style array
|
||||
*/
|
||||
public function testConstructWithStyleArray()
|
||||
{
|
||||
$fStyle = array('size' => 12);
|
||||
$pStyle = array('spacing' => 240);
|
||||
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
|
||||
$this->assertInstanceOf('PHPWord_Style_Font', $object->getFontStyle());
|
||||
$this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct with style name
|
||||
*/
|
||||
public function testConstructWithStyleName()
|
||||
{
|
||||
$fStyle = 'fStyle';
|
||||
$pStyle = 'pStyle';
|
||||
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
|
||||
$this->assertEquals($fStyle, $object->getFontStyle());
|
||||
$this->assertEquals($pStyle, $object->getParagraphStyle());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
namespace PHPWord\Tests\Writer\Word2007;
|
||||
|
||||
use PHPWord;
|
||||
use PHPWord_Style;
|
||||
use PHPWord\Tests\TestHelperDOCX;
|
||||
|
||||
/**
|
||||
|
|
@ -103,6 +104,30 @@ class BaseTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals('PAGE', $preserve->nodeValue);
|
||||
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
|
||||
}
|
||||
/**
|
||||
* covers ::_writeTextBreak
|
||||
*/
|
||||
public function testWriteTextBreak()
|
||||
{
|
||||
$fArray = array('size' => 12);
|
||||
$pArray = array('spacing' => 240);
|
||||
$fName = 'fStyle';
|
||||
$pName = 'pStyle';
|
||||
|
||||
$PHPWord = new PHPWord();
|
||||
$PHPWord->addFontStyle($fName, $fArray);
|
||||
$PHPWord->addParagraphStyle($pName, $pArray);
|
||||
$section = $PHPWord->createSection();
|
||||
$section->addTextBreak();
|
||||
$section->addTextBreak(1, $fArray, $pArray);
|
||||
$section->addTextBreak(1, $fName, $pName);
|
||||
$doc = TestHelperDOCX::getDocument($PHPWord);
|
||||
|
||||
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:rPr/w:rStyle');
|
||||
$this->assertEquals($fName, $element->getAttribute('w:val'));
|
||||
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:pStyle');
|
||||
$this->assertEquals($pName, $element->getAttribute('w:val'));
|
||||
}
|
||||
|
||||
/**
|
||||
* covers ::_writeParagraphStyle
|
||||
|
|
|
|||
|
|
@ -50,9 +50,14 @@ Changes in branch for release 0.8.0 :
|
|||
- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
|
||||
- Feature: (ivanlanin) - Paragraph: setTabs() function
|
||||
- Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF
|
||||
- Feature: (ivanlanin) - Reader: Initial effort for Word2007
|
||||
- Feature: (ivanlanin) - MemoryImage: Allow remote image when allow_url_open = on
|
||||
- Bugfix: (ivanlanin) - Footnote: Corrupt DOCX reported by MS Word when sections > 1 and not every sections have footnote
|
||||
- Feature: (ivanlanin) GH-104 - Reader: Basic Reader for Word2007
|
||||
- Feature: (bskrtich ) GH-109 - TextRun: Allow Text Break in Text Run
|
||||
- Feature: (jhfangying) GH-111 GH-118 - General: Support for East Asian fontstyle
|
||||
- Feature: (gabrielbull) GH-114 - Image: Use exif_imagetype to check image format instead of extension name
|
||||
- Feature: (bskrtich ) GH-103 - General: Setting for XMLWriter Compatibility option
|
||||
- Feature: (ivanlanin) GH-122 - MemoryImage: Allow remote image when allow_url_open = on
|
||||
- Bugfix: (ivanlanin) GH-125 - Footnote: Corrupt DOCX reported by MS Word when sections > 1 and not every sections have footnote
|
||||
- Feature: (ivanlanin) GH-18 - TextBreak: Allow style for font break
|
||||
- QA: (Progi1984) - UnitTests
|
||||
|
||||
Changes in branch for release 0.7.0 :
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
/**
|
||||
* Generic template for creating PHPWord samples
|
||||
*/
|
||||
|
||||
// Init
|
||||
error_reporting(E_ALL);
|
||||
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
|
||||
require_once '../Classes/PHPWord.php';
|
||||
|
||||
// New Word document
|
||||
echo date('H:i:s'), " Create new PHPWord object", EOL;
|
||||
$PHPWord = new PHPWord();
|
||||
|
||||
// Begin code
|
||||
$fontStyle = array('size' => 24);
|
||||
$paragraphStyle = array('spacing' => 240, 'size' => 24);
|
||||
$PHPWord->addFontStyle('fontStyle', array('size' => 9));
|
||||
$PHPWord->addParagraphStyle('paragraphStyle', array('spacing' => 480));
|
||||
$fontStyle = array('size' => 24);
|
||||
|
||||
$section = $PHPWord->createSection();
|
||||
$section->addText('Text break with no style:');
|
||||
$section->addTextBreak();
|
||||
$section->addText('Text break with defined font style:');
|
||||
$section->addTextBreak(1, 'fontStyle');
|
||||
$section->addText('Text break with defined paragraph style:');
|
||||
$section->addTextBreak(1, null, 'paragraphStyle');
|
||||
$section->addText('Text break with inline font style:');
|
||||
$section->addTextBreak(1, $fontStyle);
|
||||
$section->addText('Text break with inline paragraph style:');
|
||||
$section->addTextBreak(1, null, $paragraphStyle);
|
||||
$section->addText('Done.');
|
||||
|
||||
// End code
|
||||
|
||||
// Save file
|
||||
$name = basename(__FILE__, '.php');
|
||||
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
|
||||
foreach ($writers as $writer => $extension) {
|
||||
echo date('H:i:s'), " Write to {$writer} format", EOL;
|
||||
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
|
||||
$objWriter->save("{$name}.{$extension}");
|
||||
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
|
||||
}
|
||||
|
||||
// Done
|
||||
echo date('H:i:s'), " Done writing file(s)", EOL;
|
||||
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
|
||||
Loading…
Reference in New Issue