diff --git a/Classes/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php index a4e95c9f..6b87ae40 100755 --- a/Classes/PHPWord/Section/Text.php +++ b/Classes/PHPWord/Section/Text.php @@ -30,46 +30,63 @@ */ class PHPWord_Section_Text { - /** * Text content * * @var string */ - private $_text; + private $text; /** * Text style * * @var PHPWord_Style_Font */ - private $_styleFont; + private $fontStyle; /** * Paragraph style * - * @var \PHPWord_Style_Paragraph + * @var PHPWord_Style_Paragraph */ - private $_styleParagraph; + private $paragraphStyle; /** * Create a new Text Element * - * @var string $text - * @var mixed $style + * @param string $text + * @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->setFontStyle($styleFont); + $this->setText($text); + $paragraphStyle = $this->setParagraphStyle($paragraphStyle); + $this->setFontStyle($fontStyle, $paragraphStyle); + } - // Set paragraph style - $this->setParagraphStyle($styleParagraph); - - $this->_text = $text; - - return $this; + /** + * Set Text style + * + * @param null|array|\PHPWord_Style_Font $style + * @param null|array|\PHPWord_Style_Paragraph $paragraphStyle + * @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() { - 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)) { - $this->_styleFont = new PHPWord_Style_Font('text'); - - foreach ($styleFont as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_styleFont->setStyleValue($key, $value); - } + if (is_array($style)) { + $this->paragraphStyle = new PHPWord_Style_Paragraph; + $this->paragraphStyle->setArrayStyle($style); + } elseif ($style instanceof PHPWord_Style_Paragraph) { + $this->paragraphStyle = $style; + } elseif (null === $style) { + $this->paragraphStyle = new PHPWord_Style_Paragraph; } 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() { - return $this->_styleParagraph; + return $this->paragraphStyle; } /** - * Set Paragraph style - * - * @param array|\PHPWord_Style_Paragraph $styleParagraph - * @return \PHPWord_Style_Paragraph - * @throws \Exception + * @param string $text + * @return $this */ - public function setParagraphStyle($styleParagraph) + public function setText($text) { - if (is_array($styleParagraph)) { - $this->_styleParagraph = new PHPWord_Style_Paragraph(); - - 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; + $this->text = $text; + return $this; } /** @@ -148,6 +148,6 @@ class PHPWord_Section_Text */ public function getText() { - return $this->_text; + return $this->text; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index 987553c2..e7f89705 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -25,6 +25,8 @@ * @version 0.7.0 */ +use PHPWord\Exceptions\InvalidStyleException; + /** * Class PHPWord_Style_Font */ @@ -84,105 +86,120 @@ class PHPWord_Style_Font * * @var int|float */ - private $_name; + private $_name = PHPWord::DEFAULT_FONT_NAME; /** * Font size * * @var int|float */ - private $_size; + private $_size = PHPWord::DEFAULT_FONT_SIZE; /** * Bold * * @var bool */ - private $_bold; + private $_bold = false; /** * Italics * * @var bool */ - private $_italic; + private $_italic = false; /** * Superscript * * @var bool */ - private $_superScript; + private $_superScript = false; /** * Subscript * * @var bool */ - private $_subScript; + private $_subScript = false; /** * Underline mode * * @var string */ - private $_underline; + private $_underline = PHPWord_Style_Font::UNDERLINE_NONE; /** * Strikethrough * * @var bool */ - private $_strikethrough; + private $_strikethrough = false; /** * Font color * * @var string */ - private $_color; + private $_color = PHPWord::DEFAULT_FONT_COLOR; /** * Foreground/highlight * * @var string */ - private $_fgColor; + private $_fgColor = null; + + /** + * Text line height + * + * @var int + */ + private $lineHeight = 1.0; /** * New font style * - * @param string $type Type of font - * @param array $styleParagraph Paragraph styles definition + * @param string $type Type of font + * @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->_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)) { - $paragraph = new PHPWord_Style_Paragraph(); - foreach ($styleParagraph as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $paragraph->setStyleValue($key, $value); - } - $this->_paragraphStyle = $paragraph; + if ($paragraphStyle instanceof PHPWord_Style_Paragraph) { + $this->_paragraphStyle = $paragraphStyle; + } elseif (is_array($paragraphStyle)) { + $this->_paragraphStyle = new PHPWord_Style_Paragraph; + $this->_paragraphStyle->setArrayStyle($paragraphStyle); + } elseif (null === $paragraphStyle) { + $this->_paragraphStyle = new PHPWord_Style_Paragraph; } 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 * @@ -465,4 +482,35 @@ class PHPWord_Style_Font { 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; + } } diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 574a28e8..5c8395a7 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -34,7 +34,7 @@ class PHPWord_Style_Paragraph { const LINE_HEIGHT = 240; - /* + /** * Text line height * * @var int @@ -132,6 +132,24 @@ class PHPWord_Style_Paragraph */ 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 * @@ -482,7 +500,7 @@ class PHPWord_Style_Paragraph } /** - * @return int + * @return int|float */ public function getLineHeight() { diff --git a/README.md b/README.md index c2b896f5..fc9a59d0 100755 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ the following lines to your ``composer.json``. * [Section settings](#section-settings) * [Section page numbering](#section-page-numbering) 3. [Texts](#texts) + * [Attributes](#text-attributes) 4. [Paragraph Style](#paragraph-style) * [Attributes](#paragraph-style-attributes) 5. [Tables](#tables) @@ -184,6 +185,21 @@ $textrun->addText('I am italic, array('italic' => true)); $textrun->addText('I am colored, array('color' => 'AACC00')); ``` + +##### 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... + #### Paragraph Style diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php index b8fec514..eb29cd5c 100644 --- a/Tests/PHPWord/Style/FontTest.php +++ b/Tests/PHPWord/Style/FontTest.php @@ -4,6 +4,7 @@ namespace PHPWord\Tests\Style; use PHPUnit_Framework_TestCase; use PHPWord; use PHPWord_Style_Font; +use PHPWord\Tests\TestHelperDOCX; /** * Class FontTest @@ -13,6 +14,11 @@ use PHPWord_Style_Font; */ class FontTest extends \PHPUnit_Framework_TestCase { + public function tearDown() + { + TestHelperDOCX::clear(); + } + /** * Test initiation for style type and paragraph style */ @@ -77,4 +83,35 @@ class FontTest extends \PHPUnit_Framework_TestCase $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); + } } \ No newline at end of file