Skip to content

Add TextBreak styling #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/PHPWord/HashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

/**
* PHPWord_HashTable
*
* @codeCoverageIgnore Legacy from PHPExcel
*/
class PHPWord_HashTable
{
Expand Down
8 changes: 5 additions & 3 deletions Classes/PHPWord/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,14 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null, $stylePar
/**
* 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);
}
}

Expand Down
10 changes: 6 additions & 4 deletions Classes/PHPWord/Section/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
}

/**
* 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);
}
}

Expand Down
10 changes: 6 additions & 4 deletions Classes/PHPWord/Section/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
}

/**
* 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);
}
}

Expand Down
12 changes: 8 additions & 4 deletions Classes/PHPWord/Section/Table/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ public function addLink($linkSrc, $linkName = null, $style = null)
}

/**
* 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);
}
}

/**
Expand Down
82 changes: 81 additions & 1 deletion Classes/PHPWord/Section/TextBreak.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 7 additions & 5 deletions Classes/PHPWord/Section/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,16 @@ public function addImage($imageSrc, $style = null)
}

/**
* 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);
}
}

Expand Down
5 changes: 2 additions & 3 deletions Classes/PHPWord/Shared/ZipStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
* @version 0.7.0
*/

/** Register new zip wrapper */
PHPWord_Shared_ZipStreamWrapper::register();

/**
* Class PHPWord_Shared_ZipStreamWrapper
*
* @codeCoverageIgnore Legacy from PHPExcel
*/
class PHPWord_Shared_ZipStreamWrapper
{
Expand Down
3 changes: 2 additions & 1 deletion Classes/PHPWord/Writer/ODText.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public function save($pFilename = null)
// Add META-INF/manifest.xml
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document));

// Add media
// Add media. Has not used yet. Legacy from PHPExcel.
// @codeCoverageIgnoreStart
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
$imageContents = null;
Expand Down
2 changes: 2 additions & 0 deletions Classes/PHPWord/Writer/ODText/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function writeManifest(PHPWord $pPHPWord = null)
$objWriter->writeAttribute('manifest:full-path', 'styles.xml');
$objWriter->endElement();

// Not used yet. Legacy from PHPExcel
// @codeCoverageIgnoreStart
for ($i = 0; $i < $this->getParentWriter()->getDrawingHashTable()->count(); ++$i) {
if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
$extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
Expand Down
47 changes: 44 additions & 3 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,51 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P

/**
* 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);
}
}

/**
Expand Down Expand Up @@ -570,7 +611,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
} 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 ||
Expand Down
2 changes: 1 addition & 1 deletion Classes/PHPWord/Writer/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function writeDocument(PHPWord $pPHPWord = null)
} 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) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/PHPWord/Writer/Word2007/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function writeFooter(PHPWord_Section_Footer $footer)
} 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 ||
Expand Down
2 changes: 1 addition & 1 deletion Classes/PHPWord/Writer/Word2007/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function writeHeader(PHPWord_Section_Header $header)
} 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 ||
Expand Down
50 changes: 46 additions & 4 deletions Tests/PHPWord/Section/TextBreakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
* 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());
}

$this->assertInstanceOf('PHPWord_Section_TextBreak', $oTextBreak);
/**
* 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());
}
}
Loading