Added line height to font style
This commit is contained in:
parent
5efcec842e
commit
9966508a62
|
|
@ -30,46 +30,63 @@
|
||||||
*/
|
*/
|
||||||
class PHPWord_Section_Text
|
class PHPWord_Section_Text
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text content
|
* Text content
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_text;
|
private $text;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text style
|
* Text style
|
||||||
*
|
*
|
||||||
* @var PHPWord_Style_Font
|
* @var PHPWord_Style_Font
|
||||||
*/
|
*/
|
||||||
private $_styleFont;
|
private $fontStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paragraph style
|
* Paragraph style
|
||||||
*
|
*
|
||||||
* @var \PHPWord_Style_Paragraph
|
* @var PHPWord_Style_Paragraph
|
||||||
*/
|
*/
|
||||||
private $_styleParagraph;
|
private $paragraphStyle;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Text Element
|
* Create a new Text Element
|
||||||
*
|
*
|
||||||
* @var string $text
|
* @param string $text
|
||||||
* @var mixed $style
|
* @param null|array|\PHPWord_Style_Font $fontStyle
|
||||||
|
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
|
||||||
*/
|
*/
|
||||||
public function __construct($text = null, $styleFont = null, $styleParagraph = null)
|
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
|
||||||
{
|
{
|
||||||
// Set font style
|
$this->setText($text);
|
||||||
$this->setFontStyle($styleFont);
|
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
|
||||||
|
$this->setFontStyle($fontStyle, $paragraphStyle);
|
||||||
|
}
|
||||||
|
|
||||||
// Set paragraph style
|
/**
|
||||||
$this->setParagraphStyle($styleParagraph);
|
* Set Text style
|
||||||
|
*
|
||||||
$this->_text = $text;
|
* @param null|array|\PHPWord_Style_Font $style
|
||||||
|
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
|
||||||
return $this;
|
* @return PHPWord_Style_Font
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function setFontStyle($style = null, $paragraphStyle = null)
|
||||||
|
{
|
||||||
|
if ($style instanceof PHPWord_Style_Font) {
|
||||||
|
$this->fontStyle = $style;
|
||||||
|
} elseif (is_array($style)) {
|
||||||
|
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
|
||||||
|
$this->fontStyle->setArrayStyle($style);
|
||||||
|
} elseif (null === $style) {
|
||||||
|
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
|
||||||
|
} else {
|
||||||
|
throw new Exception('Expected array or PHPWord_Style_Font');
|
||||||
|
}
|
||||||
|
return $this->fontStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -79,28 +96,29 @@ class PHPWord_Section_Text
|
||||||
*/
|
*/
|
||||||
public function getFontStyle()
|
public function getFontStyle()
|
||||||
{
|
{
|
||||||
return $this->_styleFont;
|
return $this->fontStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Text style
|
* Set Paragraph style
|
||||||
*
|
*
|
||||||
* @return PHPWord_Style_Font
|
* @param null|array|\PHPWord_Style_Paragraph $style
|
||||||
|
* @return null|\PHPWord_Style_Paragraph
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function setFontStyle($styleFont)
|
public function setParagraphStyle($style = null)
|
||||||
{
|
{
|
||||||
if (is_array($styleFont)) {
|
if (is_array($style)) {
|
||||||
$this->_styleFont = new PHPWord_Style_Font('text');
|
$this->paragraphStyle = new PHPWord_Style_Paragraph;
|
||||||
|
$this->paragraphStyle->setArrayStyle($style);
|
||||||
foreach ($styleFont as $key => $value) {
|
} elseif ($style instanceof PHPWord_Style_Paragraph) {
|
||||||
if (substr($key, 0, 1) != '_') {
|
$this->paragraphStyle = $style;
|
||||||
$key = '_' . $key;
|
} elseif (null === $style) {
|
||||||
}
|
$this->paragraphStyle = new PHPWord_Style_Paragraph;
|
||||||
$this->_styleFont->setStyleValue($key, $value);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$this->_styleFont = $styleFont;
|
throw new Exception('Expected array or PHPWord_Style_Paragraph');
|
||||||
}
|
}
|
||||||
|
return $this->paragraphStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -110,35 +128,17 @@ class PHPWord_Section_Text
|
||||||
*/
|
*/
|
||||||
public function getParagraphStyle()
|
public function getParagraphStyle()
|
||||||
{
|
{
|
||||||
return $this->_styleParagraph;
|
return $this->paragraphStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Paragraph style
|
* @param string $text
|
||||||
*
|
* @return $this
|
||||||
* @param array|\PHPWord_Style_Paragraph $styleParagraph
|
|
||||||
* @return \PHPWord_Style_Paragraph
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function setParagraphStyle($styleParagraph)
|
public function setText($text)
|
||||||
{
|
{
|
||||||
if (is_array($styleParagraph)) {
|
$this->text = $text;
|
||||||
$this->_styleParagraph = new PHPWord_Style_Paragraph();
|
return $this;
|
||||||
|
|
||||||
foreach ($styleParagraph as $key => $value) {
|
|
||||||
if ($key === 'line-height') {
|
|
||||||
null;
|
|
||||||
} elseif (substr($key, 0, 1) != '_') {
|
|
||||||
$key = '_' . $key;
|
|
||||||
}
|
|
||||||
$this->_styleParagraph->setStyleValue($key, $value);
|
|
||||||
}
|
|
||||||
} elseif ($styleParagraph instanceof PHPWord_Style_Paragraph) {
|
|
||||||
$this->_styleParagraph = $styleParagraph;
|
|
||||||
} else {
|
|
||||||
throw new Exception('Expected array or PHPWord_Style_Paragraph');
|
|
||||||
}
|
|
||||||
return $this->_styleParagraph;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -148,6 +148,6 @@ class PHPWord_Section_Text
|
||||||
*/
|
*/
|
||||||
public function getText()
|
public function getText()
|
||||||
{
|
{
|
||||||
return $this->_text;
|
return $this->text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@
|
||||||
* @version 0.7.0
|
* @version 0.7.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use PHPWord\Exceptions\InvalidStyleException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PHPWord_Style_Font
|
* Class PHPWord_Style_Font
|
||||||
*/
|
*/
|
||||||
|
|
@ -84,105 +86,120 @@ class PHPWord_Style_Font
|
||||||
*
|
*
|
||||||
* @var int|float
|
* @var int|float
|
||||||
*/
|
*/
|
||||||
private $_name;
|
private $_name = PHPWord::DEFAULT_FONT_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Font size
|
* Font size
|
||||||
*
|
*
|
||||||
* @var int|float
|
* @var int|float
|
||||||
*/
|
*/
|
||||||
private $_size;
|
private $_size = PHPWord::DEFAULT_FONT_SIZE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bold
|
* Bold
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_bold;
|
private $_bold = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Italics
|
* Italics
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_italic;
|
private $_italic = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Superscript
|
* Superscript
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_superScript;
|
private $_superScript = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscript
|
* Subscript
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_subScript;
|
private $_subScript = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Underline mode
|
* Underline mode
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_underline;
|
private $_underline = PHPWord_Style_Font::UNDERLINE_NONE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strikethrough
|
* Strikethrough
|
||||||
*
|
*
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_strikethrough;
|
private $_strikethrough = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Font color
|
* Font color
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_color;
|
private $_color = PHPWord::DEFAULT_FONT_COLOR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Foreground/highlight
|
* Foreground/highlight
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_fgColor;
|
private $_fgColor = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text line height
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $lineHeight = 1.0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* New font style
|
* New font style
|
||||||
*
|
*
|
||||||
* @param string $type Type of font
|
* @param string $type Type of font
|
||||||
* @param array $styleParagraph Paragraph styles definition
|
* @param array $paragraphStyle Paragraph styles definition
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function __construct($type = 'text', $styleParagraph = null)
|
public function __construct($type = 'text', $paragraphStyle = null)
|
||||||
{
|
{
|
||||||
$this->_type = $type;
|
$this->_type = $type;
|
||||||
$this->_name = PHPWord::DEFAULT_FONT_NAME;
|
|
||||||
$this->_size = PHPWord::DEFAULT_FONT_SIZE;
|
|
||||||
$this->_bold = false;
|
|
||||||
$this->_italic = false;
|
|
||||||
$this->_superScript = false;
|
|
||||||
$this->_subScript = false;
|
|
||||||
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
|
|
||||||
$this->_strikethrough = false;
|
|
||||||
$this->_color = PHPWord::DEFAULT_FONT_COLOR;
|
|
||||||
$this->_fgColor = null;
|
|
||||||
|
|
||||||
if (!is_null($styleParagraph)) {
|
if ($paragraphStyle instanceof PHPWord_Style_Paragraph) {
|
||||||
$paragraph = new PHPWord_Style_Paragraph();
|
$this->_paragraphStyle = $paragraphStyle;
|
||||||
foreach ($styleParagraph as $key => $value) {
|
} elseif (is_array($paragraphStyle)) {
|
||||||
if (substr($key, 0, 1) != '_') {
|
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
|
||||||
$key = '_' . $key;
|
$this->_paragraphStyle->setArrayStyle($paragraphStyle);
|
||||||
}
|
} elseif (null === $paragraphStyle) {
|
||||||
$paragraph->setStyleValue($key, $value);
|
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
|
||||||
}
|
|
||||||
$this->_paragraphStyle = $paragraph;
|
|
||||||
} else {
|
} else {
|
||||||
$this->_paragraphStyle = null;
|
throw new Exception('Expected array or PHPWord_Style_Paragraph');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $style
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setArrayStyle(array $style = array())
|
||||||
|
{
|
||||||
|
foreach ($style as $key => $value) {
|
||||||
|
if ($key === 'line-height') {
|
||||||
|
$this->setLineHeight($value);
|
||||||
|
null;
|
||||||
|
} elseif (substr($key, 0, 1) !== '_') {
|
||||||
|
$key = '_' . $key;
|
||||||
|
}
|
||||||
|
$this->setStyleValue($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set style value
|
* Set style value
|
||||||
*
|
*
|
||||||
|
|
@ -465,4 +482,35 @@ class PHPWord_Style_Font
|
||||||
{
|
{
|
||||||
return $this->_paragraphStyle;
|
return $this->_paragraphStyle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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->getParagraphStyle()->setLineHeight($lineHeight);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int|float
|
||||||
|
*/
|
||||||
|
public function getLineHeight()
|
||||||
|
{
|
||||||
|
return $this->lineHeight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class PHPWord_Style_Paragraph
|
||||||
{
|
{
|
||||||
const LINE_HEIGHT = 240;
|
const LINE_HEIGHT = 240;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Text line height
|
* Text line height
|
||||||
*
|
*
|
||||||
* @var int
|
* @var int
|
||||||
|
|
@ -132,6 +132,24 @@ class PHPWord_Style_Paragraph
|
||||||
*/
|
*/
|
||||||
private $_pageBreakBefore = false;
|
private $_pageBreakBefore = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $style
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setArrayStyle(array $style = array())
|
||||||
|
{
|
||||||
|
foreach ($style as $key => $value) {
|
||||||
|
if ($key === 'line-height') {
|
||||||
|
null;
|
||||||
|
} elseif (substr($key, 0, 1) !== '_') {
|
||||||
|
$key = '_' . $key;
|
||||||
|
}
|
||||||
|
$this->setStyleValue($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set Style value
|
* Set Style value
|
||||||
*
|
*
|
||||||
|
|
@ -482,7 +500,7 @@ class PHPWord_Style_Paragraph
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int|float
|
||||||
*/
|
*/
|
||||||
public function getLineHeight()
|
public function getLineHeight()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
16
README.md
16
README.md
|
|
@ -39,6 +39,7 @@ the following lines to your ``composer.json``.
|
||||||
* [Section settings](#section-settings)
|
* [Section settings](#section-settings)
|
||||||
* [Section page numbering](#section-page-numbering)
|
* [Section page numbering](#section-page-numbering)
|
||||||
3. [Texts](#texts)
|
3. [Texts](#texts)
|
||||||
|
* [Attributes](#text-attributes)
|
||||||
4. [Paragraph Style](#paragraph-style)
|
4. [Paragraph Style](#paragraph-style)
|
||||||
* [Attributes](#paragraph-style-attributes)
|
* [Attributes](#paragraph-style-attributes)
|
||||||
5. [Tables](#tables)
|
5. [Tables](#tables)
|
||||||
|
|
@ -184,6 +185,21 @@ $textrun->addText('I am italic, array('italic' => true));
|
||||||
$textrun->addText('I am colored, array('color' => 'AACC00'));
|
$textrun->addText('I am colored, array('color' => 'AACC00'));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
<a name="text-attributes"></a>
|
||||||
|
##### Attributes
|
||||||
|
|
||||||
|
* ``size`` text size, e.g. _20_, _22_,
|
||||||
|
* ``name`` font name, e.g. _Arial_
|
||||||
|
* ``bold`` text is bold, _true_ or _false_
|
||||||
|
* ``italic`` text is italic, _true_ or _false_
|
||||||
|
* ``superScript`` text is super script, _true_ or _false_
|
||||||
|
* ``subScript`` text is sub script, _true_ or _false_
|
||||||
|
* ``underline`` text is underline, _true_ or _false_
|
||||||
|
* ``strikethrough`` text is strikethrough, _true_ or _false_
|
||||||
|
* ``color`` text color, e.g. _FF0000_
|
||||||
|
* ``fgColor`` fgColor
|
||||||
|
* ``line-height`` text line height, e.g. _1.0_, _1.5_, ect...
|
||||||
|
|
||||||
<a name="paragraph-style"></a>
|
<a name="paragraph-style"></a>
|
||||||
#### Paragraph Style
|
#### Paragraph Style
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace PHPWord\Tests\Style;
|
||||||
use PHPUnit_Framework_TestCase;
|
use PHPUnit_Framework_TestCase;
|
||||||
use PHPWord;
|
use PHPWord;
|
||||||
use PHPWord_Style_Font;
|
use PHPWord_Style_Font;
|
||||||
|
use PHPWord\Tests\TestHelperDOCX;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FontTest
|
* Class FontTest
|
||||||
|
|
@ -13,6 +14,11 @@ use PHPWord_Style_Font;
|
||||||
*/
|
*/
|
||||||
class FontTest extends \PHPUnit_Framework_TestCase
|
class FontTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
TestHelperDOCX::clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test initiation for style type and paragraph style
|
* Test initiation for style type and paragraph style
|
||||||
*/
|
*/
|
||||||
|
|
@ -77,4 +83,35 @@ class FontTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals($value, $object->$get());
|
$this->assertEquals($value, $object->$get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLineHeight()
|
||||||
|
{
|
||||||
|
$PHPWord = new PHPWord();
|
||||||
|
$section = $PHPWord->createSection();
|
||||||
|
|
||||||
|
// Test style array
|
||||||
|
$text = $section->addText('This is a test', 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->getFontStyle()->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