Added line height methods to mirror the line height settings in Word in the paragraph styling
This commit is contained in:
parent
3519477535
commit
69a8192652
|
|
@ -48,7 +48,7 @@ class PHPWord_Section_Text
|
|||
/**
|
||||
* Paragraph style
|
||||
*
|
||||
* @var PHPWord_Style_Font
|
||||
* @var \PHPWord_Style_Paragraph
|
||||
*/
|
||||
private $_styleParagraph;
|
||||
|
||||
|
|
@ -116,7 +116,9 @@ class PHPWord_Section_Text
|
|||
/**
|
||||
* Set Paragraph style
|
||||
*
|
||||
* @return PHPWord_Style_Paragraph
|
||||
* @param array|\PHPWord_Style_Paragraph $styleParagraph
|
||||
* @return \PHPWord_Style_Paragraph
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setParagraphStyle($styleParagraph)
|
||||
{
|
||||
|
|
@ -124,14 +126,19 @@ class PHPWord_Section_Text
|
|||
$this->_styleParagraph = new PHPWord_Style_Paragraph();
|
||||
|
||||
foreach ($styleParagraph as $key => $value) {
|
||||
if (substr($key, 0, 1) != '_') {
|
||||
if ($key === 'line-height') {
|
||||
null;
|
||||
} elseif (substr($key, 0, 1) != '_') {
|
||||
$key = '_' . $key;
|
||||
}
|
||||
$this->_styleParagraph->setStyleValue($key, $value);
|
||||
}
|
||||
} else {
|
||||
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
|
||||
$this->_styleParagraph = $styleParagraph;
|
||||
} else {
|
||||
throw new Exception('Expected array or PHPWord_Style_Paragraph');
|
||||
}
|
||||
return $this->_styleParagraph;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -25,11 +25,21 @@
|
|||
* @version 0.7.0
|
||||
*/
|
||||
|
||||
use PHPWord\Exceptions\InvalidStyleException;
|
||||
|
||||
/**
|
||||
* PHPWord_Style_Paragraph
|
||||
*/
|
||||
class PHPWord_Style_Paragraph
|
||||
{
|
||||
const LINE_HEIGHT = 240;
|
||||
|
||||
/*
|
||||
* Text line height
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $lineHeight;
|
||||
|
||||
/**
|
||||
* Paragraph alignment
|
||||
|
|
@ -85,7 +95,7 @@ class PHPWord_Style_Paragraph
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_basedOn;
|
||||
private $_basedOn = 'Normal';
|
||||
|
||||
/**
|
||||
* Style for next paragraph
|
||||
|
|
@ -99,48 +109,28 @@ class PHPWord_Style_Paragraph
|
|||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_widowControl;
|
||||
private $_widowControl = true;
|
||||
|
||||
/**
|
||||
* Keep paragraph with next paragraph
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_keepNext;
|
||||
private $_keepNext = false;
|
||||
|
||||
/**
|
||||
* Keep all lines on one page
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_keepLines;
|
||||
private $_keepLines = false;
|
||||
|
||||
/**
|
||||
* Start paragraph on next page
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $_pageBreakBefore;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
private $_pageBreakBefore = false;
|
||||
|
||||
/**
|
||||
* Set Style value
|
||||
|
|
@ -152,10 +142,13 @@ class PHPWord_Style_Paragraph
|
|||
{
|
||||
if ($key == '_indent' || $key == '_hanging') {
|
||||
$value = $value * 720;
|
||||
}
|
||||
if ($key == '_spacing') {
|
||||
} elseif ($key == '_spacing') {
|
||||
$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);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
|
|
@ -466,4 +459,33 @@ class PHPWord_Style_Paragraph
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,8 +2,10 @@
|
|||
namespace PHPWord\Tests\Style;
|
||||
|
||||
use PHPUnit_Framework_TestCase;
|
||||
use PHPWord;
|
||||
use PHPWord_Style_Paragraph;
|
||||
use PHPWord_Style_Tab;
|
||||
use PHPWord\Tests\TestHelperDOCX;
|
||||
|
||||
/**
|
||||
* Class ParagraphTest
|
||||
|
|
@ -13,6 +15,11 @@ use PHPWord_Style_Tab;
|
|||
*/
|
||||
class ParagraphTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
TestHelperDOCX::clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue