Added line height methods to mirror the line height settings in Word in the paragraph styling

This commit is contained in:
Gabriel Bull 2014-03-09 14:26:32 -04:00
parent 3519477535
commit 69a8192652
3 changed files with 101 additions and 34 deletions

View File

@ -48,7 +48,7 @@ class PHPWord_Section_Text
/** /**
* Paragraph style * Paragraph style
* *
* @var PHPWord_Style_Font * @var \PHPWord_Style_Paragraph
*/ */
private $_styleParagraph; private $_styleParagraph;
@ -116,7 +116,9 @@ class PHPWord_Section_Text
/** /**
* Set Paragraph style * Set Paragraph style
* *
* @return PHPWord_Style_Paragraph * @param array|\PHPWord_Style_Paragraph $styleParagraph
* @return \PHPWord_Style_Paragraph
* @throws \Exception
*/ */
public function setParagraphStyle($styleParagraph) public function setParagraphStyle($styleParagraph)
{ {
@ -124,14 +126,19 @@ class PHPWord_Section_Text
$this->_styleParagraph = new PHPWord_Style_Paragraph(); $this->_styleParagraph = new PHPWord_Style_Paragraph();
foreach ($styleParagraph as $key => $value) { foreach ($styleParagraph as $key => $value) {
if (substr($key, 0, 1) != '_') { if ($key === 'line-height') {
null;
} elseif (substr($key, 0, 1) != '_') {
$key = '_' . $key; $key = '_' . $key;
} }
$this->_styleParagraph->setStyleValue($key, $value); $this->_styleParagraph->setStyleValue($key, $value);
} }
} else { } elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
$this->_styleParagraph = $styleParagraph; $this->_styleParagraph = $styleParagraph;
} else {
throw new Exception('Expected array or PHPWord_Style_Paragraph');
} }
return $this->_styleParagraph;
} }
/** /**

View File

@ -25,11 +25,21 @@
* @version 0.7.0 * @version 0.7.0
*/ */
use PHPWord\Exceptions\InvalidStyleException;
/** /**
* PHPWord_Style_Paragraph * PHPWord_Style_Paragraph
*/ */
class PHPWord_Style_Paragraph class PHPWord_Style_Paragraph
{ {
const LINE_HEIGHT = 240;
/*
* Text line height
*
* @var int
*/
private $lineHeight;
/** /**
* Paragraph alignment * Paragraph alignment
@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph
* *
* @var string * @var string
*/ */
private $_basedOn; private $_basedOn = 'Normal';
/** /**
* Style for next paragraph * Style for next paragraph
@ -99,63 +109,46 @@ class PHPWord_Style_Paragraph
* *
* @var bool * @var bool
*/ */
private $_widowControl; private $_widowControl = true;
/** /**
* Keep paragraph with next paragraph * Keep paragraph with next paragraph
* *
* @var bool * @var bool
*/ */
private $_keepNext; private $_keepNext = false;
/** /**
* Keep all lines on one page * Keep all lines on one page
* *
* @var bool * @var bool
*/ */
private $_keepLines; private $_keepLines = false;
/** /**
* Start paragraph on next page * Start paragraph on next page
* *
* @var bool * @var bool
*/ */
private $_pageBreakBefore; private $_pageBreakBefore = false;
/**
* New Paragraph Style
*/
public function __construct()
{
$this->_align = null;
$this->_spaceBefore = null;
$this->_spaceAfter = null;
$this->_spacing = null;
$this->_tabs = null;
$this->_indent = null;
$this->_hanging = null;
$this->_basedOn = 'Normal';
$this->_next = null;
$this->_widowControl = true;
$this->_keepNext = false;
$this->_keepLines = false;
$this->_pageBreakBefore = false;
}
/** /**
* Set Style value * Set Style value
* *
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
*/ */
public function setStyleValue($key, $value) public function setStyleValue($key, $value)
{ {
if ($key == '_indent' || $key == '_hanging') { if ($key == '_indent' || $key == '_hanging') {
$value = $value * 720; $value = $value * 720;
} } elseif ($key == '_spacing') {
if ($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips $value += 240; // because line height of 1 matches 240 twips
} elseif ($key === 'line-height') {
$this->setLineHeight($value);
return;
} }
$this->$key = $value;
$method = 'set' . substr($key, 1); $method = 'set' . substr($key, 1);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->$method($value); $this->$method($value);
@ -466,4 +459,33 @@ class PHPWord_Style_Paragraph
return $this; return $this;
} }
/**
* Set the line height
*
* @param int|float|string $lineHeight
* @return $this
* @throws \PHPWord\Exceptions\InvalidStyleException
*/
public function setLineHeight($lineHeight)
{
if (is_string($lineHeight)) {
$lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
}
if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
throw new InvalidStyleException('Line height must be a valid number');
}
$this->lineHeight = $lineHeight;
$this->setSpacing($lineHeight * self::LINE_HEIGHT);
return $this;
}
/**
* @return int
*/
public function getLineHeight()
{
return $this->lineHeight;
}
} }

View File

@ -2,8 +2,10 @@
namespace PHPWord\Tests\Style; namespace PHPWord\Tests\Style;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_Style_Paragraph; use PHPWord_Style_Paragraph;
use PHPWord_Style_Tab; use PHPWord_Style_Tab;
use PHPWord\Tests\TestHelperDOCX;
/** /**
* Class ParagraphTest * Class ParagraphTest
@ -13,6 +15,11 @@ use PHPWord_Style_Tab;
*/ */
class ParagraphTest extends \PHPUnit_Framework_TestCase class ParagraphTest extends \PHPUnit_Framework_TestCase
{ {
public function tearDown()
{
TestHelperDOCX::clear();
}
/** /**
* Test setting style values with null or empty value * Test setting style values with null or empty value
*/ */
@ -85,4 +92,35 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
)); ));
$this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs()); $this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs());
} }
public function testLineHeight()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
// Test style array
$text = $section->addText('This is a test', array(), array(
'line-height' => 2.0
));
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
$lineHeight = $element->getAttribute('w:line');
$lineRule = $element->getAttribute('w:lineRule');
$this->assertEquals(480, $lineHeight);
$this->assertEquals('auto', $lineRule);
// Test setter
$text->getParagraphStyle()->setLineHeight(3.0);
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing');
$lineHeight = $element->getAttribute('w:line');
$lineRule = $element->getAttribute('w:lineRule');
$this->assertEquals(720, $lineHeight);
$this->assertEquals('auto', $lineRule);
}
} }