Skip to content

Commit

Permalink
Added line height to font style
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbull committed Mar 9, 2014
1 parent 5efcec8 commit 9966508
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 92 deletions.
112 changes: 56 additions & 56 deletions Classes/PHPWord/Section/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// Set paragraph style
$this->setParagraphStyle($styleParagraph);

$this->_text = $text;
$this->setText($text);
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
$this->setFontStyle($fontStyle, $paragraphStyle);
}

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);

This comment has been minimized.

Copy link
@ivanlanin

ivanlanin Mar 13, 2014

Contributor

@gabrielbull This line causes bug on #118 reported by @jhfangying. We should allow a font style to be defined without paragraph style (i.e. with null value). Please test. Thanks.

This comment has been minimized.

Copy link
@gabrielbull

gabrielbull Mar 13, 2014

Author Member

Bug #118 is closed, is there still a problem? This part of the code has since been changed.

This comment has been minimized.

Copy link
@ivanlanin

ivanlanin Mar 13, 2014

Contributor

No, it's ok now.

} else {
throw new Exception('Expected array or PHPWord_Style_Font');
}
return $this->fontStyle;
}

/**
Expand All @@ -79,28 +96,29 @@ public function __construct($text = null, $styleFont = null, $styleParagraph = n
*/
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;
}

/**
Expand All @@ -110,35 +128,17 @@ public function setFontStyle($styleFont)
*/
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;
}

/**
Expand All @@ -148,6 +148,6 @@ public function setParagraphStyle($styleParagraph)
*/
public function getText()
{
return $this->_text;
return $this->text;
}
}
}
116 changes: 82 additions & 34 deletions Classes/PHPWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* @version 0.7.0
*/

use PHPWord\Exceptions\InvalidStyleException;

/**
* Class PHPWord_Style_Font
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -465,4 +482,35 @@ public function getParagraphStyle()
{
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;
}
}
Loading

0 comments on commit 9966508

Please sign in to comment.