diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php
index 51489224..64f9d9a2 100755
--- a/Classes/PHPWord.php
+++ b/Classes/PHPWord.php
@@ -39,8 +39,23 @@ if (!defined('PHPWORD_BASE_PATH')) {
class PHPWord
{
+ /**
+ * Default font name (Arial)
+ */
const DEFAULT_FONT_NAME = 'Arial';
- const DEFAULT_FONT_SIZE = 20;
+
+ /**
+ * Default font size in points (10pt)
+ *
+ * OOXML defined font size values in halfpoints, i.e. twice of what PHPWord
+ * use, and the conversion will be conducted during XML writing.
+ */
+ const DEFAULT_FONT_SIZE = 10;
+
+ /**
+ * Default font color (black)
+ */
+ const DEFAULT_FONT_COLOR = '000000';
/**
* Document properties
diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php
index 3363dc66..062fe839 100755
--- a/Classes/PHPWord/Style/Font.php
+++ b/Classes/PHPWord/Style/Font.php
@@ -80,17 +80,82 @@ class PHPWord_Style_Font
*/
private $_paragraphStyle;
- private $_size;
+ /**
+ * Font name
+ *
+ * @var int|float
+ */
private $_name;
+
+ /**
+ * Font size
+ *
+ * @var int|float
+ */
+ private $_size;
+
+ /**
+ * Bold
+ *
+ * @var bool
+ */
private $_bold;
+
+ /**
+ * Italics
+ *
+ * @var bool
+ */
private $_italic;
+
+ /**
+ * Superscript
+ *
+ * @var bool
+ */
private $_superScript;
+
+ /**
+ * Subscript
+ *
+ * @var bool
+ */
private $_subScript;
+
+ /**
+ * Underline mode
+ *
+ * @var string
+ */
private $_underline;
+
+ /**
+ * Strikethrough
+ *
+ * @var bool
+ */
private $_strikethrough;
+
+ /**
+ * Font color
+ *
+ * @var string
+ */
private $_color;
+
+ /**
+ * Foreground/highlight
+ *
+ * @var string
+ */
private $_fgColor;
+ /**
+ * New font style
+ *
+ * @param string $type Type of font
+ * @param array $styleParagraph Paragraph styles definition
+ */
public function __construct($type = 'text', $styleParagraph = null)
{
$this->_type = $type;
@@ -102,7 +167,7 @@ class PHPWord_Style_Font
$this->_subScript = false;
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
$this->_strikethrough = false;
- $this->_color = '000000';
+ $this->_color = PHPWord::DEFAULT_FONT_COLOR;
$this->_fgColor = null;
if (!is_null($styleParagraph)) {
@@ -119,78 +184,139 @@ class PHPWord_Style_Font
}
}
+ /**
+ * Set style value
+ *
+ * @param string $key
+ * @param mixed $value
+ */
+ public function setStyleValue($key, $value)
+ {
+ $method = 'set' . ucwords(substr($key, 1));
+ if (method_exists($this, $method)) {
+ $this->$method($value);
+ }
+ }
+
+ /**
+ * Get font name
+ *
+ * @return bool
+ */
public function getName()
{
return $this->_name;
}
- public function setStyleValue($key, $value)
- {
- if ($key == '_size') {
- $value *= 2;
- }
- $this->$key = $value;
- }
-
+ /**
+ * Set font name
+ *
+ * @param string $pValue
+ * @return PHPWord_Style_Font
+ */
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
{
- if ($pValue == '') {
+ if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_NAME;
}
$this->_name = $pValue;
return $this;
}
+ /**
+ * Get font size
+ *
+ * @return int|float
+ */
public function getSize()
{
return $this->_size;
}
+ /**
+ * Set font size
+ *
+ * @param int|float $pValue
+ * @return PHPWord_Style_Font
+ */
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
{
- if ($pValue == '') {
+ if (!is_numeric($pValue)) {
$pValue = PHPWord::DEFAULT_FONT_SIZE;
}
- $this->_size = ($pValue * 2);
+ $this->_size = $pValue;
return $this;
}
+ /**
+ * Get bold
+ *
+ * @return bool
+ */
public function getBold()
{
return $this->_bold;
}
+ /**
+ * Set bold
+ *
+ * @param bool $pValue
+ * @return PHPWord_Style_Font
+ */
public function setBold($pValue = false)
{
- if ($pValue == '') {
+ if (!is_bool($pValue)) {
$pValue = false;
}
$this->_bold = $pValue;
return $this;
}
+ /**
+ * Get italics
+ *
+ * @return bool
+ */
public function getItalic()
{
return $this->_italic;
}
+ /**
+ * Set italics
+ *
+ * @param bool $pValue
+ * @return PHPWord_Style_Font
+ */
public function setItalic($pValue = false)
{
- if ($pValue == '') {
+ if (!is_bool($pValue)) {
$pValue = false;
}
$this->_italic = $pValue;
return $this;
}
+ /**
+ * Get superscript
+ *
+ * @return bool
+ */
public function getSuperScript()
{
return $this->_superScript;
}
+ /**
+ * Set superscript
+ *
+ * @param bool $pValue
+ * @return PHPWord_Style_Font
+ */
public function setSuperScript($pValue = false)
{
- if ($pValue == '') {
+ if (!is_bool($pValue)) {
$pValue = false;
}
$this->_superScript = $pValue;
@@ -198,14 +324,25 @@ class PHPWord_Style_Font
return $this;
}
+ /**
+ * Get superscript
+ *
+ * @return bool
+ */
public function getSubScript()
{
return $this->_subScript;
}
+ /**
+ * Set subscript
+ *
+ * @param bool $pValue
+ * @return PHPWord_Style_Font
+ */
public function setSubScript($pValue = false)
{
- if ($pValue == '') {
+ if (!is_bool($pValue)) {
$pValue = false;
}
$this->_subScript = $pValue;
@@ -213,11 +350,22 @@ class PHPWord_Style_Font
return $this;
}
+ /**
+ * Get underline
+ *
+ * @return string
+ */
public function getUnderline()
{
return $this->_underline;
}
+ /**
+ * Set underline
+ *
+ * @param string $pValue
+ * @return PHPWord_Style_Font
+ */
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
{
if ($pValue == '') {
@@ -227,49 +375,90 @@ class PHPWord_Style_Font
return $this;
}
+ /**
+ * Get strikethrough
+ *
+ * @return bool
+ */
public function getStrikethrough()
{
return $this->_strikethrough;
}
+ /**
+ * Set strikethrough
+ *
+ * @param bool $pValue
+ * @return PHPWord_Style_Font
+ */
public function setStrikethrough($pValue = false)
{
- if ($pValue == '') {
+ if (!is_bool($pValue)) {
$pValue = false;
}
$this->_strikethrough = $pValue;
return $this;
}
+ /**
+ * Get font color
+ *
+ * @return string
+ */
public function getColor()
{
return $this->_color;
}
- public function setColor($pValue = '000000')
+ /**
+ * Set font color
+ *
+ * @param string $pValue
+ * @return PHPWord_Style_Font
+ */
+ public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
{
+ if (is_null($pValue) || $pValue == '') {
+ $pValue = PHPWord::DEFAULT_FONT_COLOR;
+ }
$this->_color = $pValue;
return $this;
}
+ /**
+ * Get foreground/highlight color
+ *
+ * @return bool
+ */
public function getFgColor()
{
return $this->_fgColor;
}
+ /**
+ * Set foreground/highlight color
+ *
+ * @param string $pValue
+ * @return PHPWord_Style_Font
+ */
public function setFgColor($pValue = null)
{
$this->_fgColor = $pValue;
return $this;
}
+ /**
+ * Get style type
+ *
+ * @return string
+ */
public function getStyleType()
{
return $this->_type;
}
/**
- * Get Paragraph style
+ * Get paragraph style
*
* @return PHPWord_Style_Paragraph
*/
diff --git a/Classes/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php
index fd56b7fa..745da08e 100755
--- a/Classes/PHPWord/Writer/ODText/Styles.php
+++ b/Classes/PHPWord/Writer/ODText/Styles.php
@@ -133,16 +133,16 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
$objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('style:use-window-font-color', 'true');
$objWriter->writeAttribute('style:font-name', PHPWord::DEFAULT_FONT_NAME);
- $objWriter->writeAttribute('fo:font-size', '10pt');
+ $objWriter->writeAttribute('fo:font-size', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('fo:language', 'fr');
$objWriter->writeAttribute('fo:country', 'FR');
$objWriter->writeAttribute('style:letter-kerning', 'true');
- $objWriter->writeAttribute('style:font-name-asian', 'Arial2');
- $objWriter->writeAttribute('style:font-size-asian', '10pt');
+ $objWriter->writeAttribute('style:font-name-asian', PHPWord::DEFAULT_FONT_NAME . '2');
+ $objWriter->writeAttribute('style:font-size-asian', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-asian', 'zh');
$objWriter->writeAttribute('style:country-asian', 'CN');
- $objWriter->writeAttribute('style:font-name-complex', 'Arial2');
- $objWriter->writeAttribute('style:font-size-complex', '10pt');
+ $objWriter->writeAttribute('style:font-name-complex', PHPWord::DEFAULT_FONT_NAME . '2');
+ $objWriter->writeAttribute('style:font-size-complex', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-complex', 'hi');
$objWriter->writeAttribute('style:country-complex', 'IN');
$objWriter->writeAttribute('fo:hyphenate', 'false');
@@ -168,9 +168,9 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
// style:text-properties
$objWriter->startElement('style:text-properties');
- $objWriter->writeAttribute('fo:font-size', ($style->getSize() / 2) . 'pt');
- $objWriter->writeAttribute('style:font-size-asian', ($style->getSize() / 2) . 'pt');
- $objWriter->writeAttribute('style:font-size-complex', ($style->getSize() / 2) . 'pt');
+ $objWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt');
+ $objWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt');
+ $objWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt');
if ($style->getItalic()) {
$objWriter->writeAttribute('fo:font-style', 'italic');
$objWriter->writeAttribute('style:font-style-asian', 'italic');
diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php
index 42b544fb..12400907 100755
--- a/Classes/PHPWord/Writer/RTF.php
+++ b/Classes/PHPWord/Writer/RTF.php
@@ -176,7 +176,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
// Point size (in half-points) above which to kern character pairs
$sRTFContent .= '\kerning1';
// Set the font size in half-points
- $sRTFContent .= '\fs20';
+ $sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
// Body
$sRTFContent .= $this->_getDataContent();
@@ -252,10 +252,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
if ($style instanceof PHPWord_Style_Font) {
$color = $style->getColor();
$fgcolor = $style->getFgColor();
- if (in_array($color, $arrColors) == FALSE && $color != '000000' && !empty($color)) {
+ if (in_array($color, $arrColors) == FALSE && $color != PHPWord::DEFAULT_FONT_COLOR && !empty($color)) {
$arrColors[] = $color;
}
- if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != '000000' && !empty($fgcolor)) {
+ if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != PHPWord::DEFAULT_FONT_COLOR && !empty($fgcolor)) {
$arrColors[] = $fgcolor;
}
}
@@ -400,7 +400,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i';
}
if ($styleFont->getSize()) {
- $sRTFText .= '\fs' . $styleFont->getSize();
+ $sRTFText .= '\fs' . ($styleFont->getSize() * 2);
}
}
if ($this->_lastParagraphStyle != '' || $styleFont) {
@@ -419,7 +419,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i0';
}
if ($styleFont->getSize()) {
- $sRTFText .= '\fs20';
+ $sRTFText .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
}
}
diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php
index 2b99ef45..77f4d0d7 100755
--- a/Classes/PHPWord/Writer/Word2007/Base.php
+++ b/Classes/PHPWord/Writer/Word2007/Base.php
@@ -373,7 +373,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$color = $style->getColor();
$size = $style->getSize();
$fgColor = $style->getFgColor();
- $striketrough = $style->getStrikethrough();
+ $strikethrough = $style->getStrikethrough();
$underline = $style->getUnderline();
$superscript = $style->getSuperScript();
$subscript = $style->getSubScript();
@@ -390,7 +390,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
// Color
- if ($color != '000000') {
+ if ($color != PHPWord::DEFAULT_FONT_COLOR) {
$objWriter->startElement('w:color');
$objWriter->writeAttribute('w:val', $color);
$objWriter->endElement();
@@ -399,10 +399,10 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
// Size
if ($size != PHPWord::DEFAULT_FONT_SIZE) {
$objWriter->startElement('w:sz');
- $objWriter->writeAttribute('w:val', $size);
+ $objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement();
$objWriter->startElement('w:szCs');
- $objWriter->writeAttribute('w:val', $size);
+ $objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement();
}
@@ -424,8 +424,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
- // Striketrough
- if ($striketrough) {
+ // Strikethrough
+ if ($strikethrough) {
$objWriter->writeElement('w:strike', null);
}
diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php
index 2ae07d24..c87d94bf 100755
--- a/Classes/PHPWord/Writer/Word2007/Styles.php
+++ b/Classes/PHPWord/Writer/Word2007/Styles.php
@@ -380,11 +380,11 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
$objWriter->endElement();
$objWriter->startElement('w:sz');
- $objWriter->writeAttribute('w:val', $fontSize);
+ $objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement();
$objWriter->startElement('w:szCs');
- $objWriter->writeAttribute('w:val', $fontSize);
+ $objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement();
$objWriter->endElement();
diff --git a/README.md b/README.md
index a7be92df..09556559 100755
--- a/README.md
+++ b/README.md
@@ -34,8 +34,10 @@ the following lines to your ``composer.json``.
### Table of contents
1. [Basic usage](#basic-usage)
+ * [Measurement units](#measurement-units)
2. [Sections](#sections)
- * [Change Section Page Numbering](#sections-page-numbering)
+ * [Section settings](#section-settings)
+ * [Section page numbering](#section-page-numbering)
3. [Tables](#tables)
* [Cell Style](#tables-cell-style)
4. [Images](#images)
@@ -75,6 +77,7 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');
```
+
##### Measurement units
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
@@ -98,8 +101,51 @@ $sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
#### Sections
-
-##### Change Section Page Numbering
+Every visible element in word is placed inside of a section. To create a section, use the following code:
+
+```php
+$section = $PHPWord->createSection($sectionSettings);
+```
+The `$sectionSettings` is an optional associative array that sets the section. Example:
+
+```php
+$sectionSettings = array(
+ 'orientation' => 'landscape',
+ 'marginTop' => 600,
+ 'colsNum' => 2,
+);
+```
+
+##### Section settings
+
+Below are the available settings for section:
+
+* `orientation` Page orientation, i.e. 'portrait' (default) or 'landscape'
+* `marginTop` Page margin top in twips
+* `marginLeft` Page margin left in twips
+* `marginRight` Page margin right in twips
+* `marginBottom` Page margin bottom in twips
+* `borderTopSize` Border top size in twips
+* `borderTopColor` Border top color
+* `borderLeftSize` Border left size in twips
+* `borderLeftColor` Border left color
+* `borderRightSize` Border right size in twips
+* `borderRightColor` Border right color
+* `borderBottomSize` Border bottom size in twips
+* `borderBottomColor` Border bottom color
+* `headerHeight` Spacing to top of header
+* `footerHeight` Spacing to bottom of footer
+* `colsNum` Number of columns
+* `colsSpace` Spacing between columns
+* `breakType` Section break type (nextPage, nextColumn, continuous, evenPage, oddPage)
+
+The following two settings are automatically set by the use of the `orientation` setting. You can alter them but that's not recommended.
+
+* `pageSizeW` Page width in twips
+* `pageSizeH` Page height in twips
+
+
+##### Section page numbering
You can change a section page numbering.
diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php
new file mode 100644
index 00000000..1238f34b
--- /dev/null
+++ b/Tests/PHPWord/Style/FontTest.php
@@ -0,0 +1,82 @@
+ 'both'));
+
+ $this->assertEquals('text', $object->getStyleType());
+ $this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
+ }
+
+ /**
+ * Test setting style values with null or empty value
+ */
+ public function testSetStyleValueWithNullOrEmpty()
+ {
+ $object = new PHPWord_Style_Font();
+
+ $attributes = array(
+ 'name' => PHPWord::DEFAULT_FONT_NAME,
+ 'size' => PHPWord::DEFAULT_FONT_SIZE,
+ 'bold' => false,
+ 'italic' => false,
+ 'superScript' => false,
+ 'subScript' => false,
+ 'underline' => PHPWord_Style_Font::UNDERLINE_NONE,
+ 'strikethrough' => false,
+ 'color' => PHPWord::DEFAULT_FONT_COLOR,
+ 'fgColor' => null,
+ );
+ foreach ($attributes as $key => $default) {
+ $method = 'get' . ucwords($key);
+ $object->setStyleValue("_$key", null);
+ $this->assertEquals($default, $object->$method());
+ $object->setStyleValue("_$key", '');
+ $this->assertEquals($default, $object->$method());
+ }
+ }
+
+ /**
+ * Test setting style values with normal value
+ */
+ public function testSetStyleValueNormal()
+ {
+ $object = new PHPWord_Style_Font();
+
+ $attributes = array(
+ 'name' => 'Times New Roman',
+ 'size' => 9,
+ 'bold' => true,
+ 'italic' => true,
+ 'superScript' => true,
+ 'subScript' => true,
+ 'underline' => PHPWord_Style_Font::UNDERLINE_HEAVY,
+ 'strikethrough' => true,
+ 'color' => '999999',
+ 'fgColor' => '999999',
+ );
+ foreach ($attributes as $key => $value) {
+ $method = 'get' . ucwords($key);
+ $object->setStyleValue("_$key", $value);
+ $this->assertEquals($value, $object->$method());
+ }
+ }
+
+}