Merge branch 'textbox' of github.com:basjan/PHPWord

This commit is contained in:
Ivan Lanin 2014-05-08 08:56:34 +07:00
commit c7a940cc4c
11 changed files with 1385 additions and 17 deletions

View File

@ -304,7 +304,7 @@ abstract class AbstractContainer extends AbstractElement
private function checkValidity($method) private function checkValidity($method)
{ {
// Valid containers for each element // Valid containers for each element
$allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote'); $allContainers = array('section', 'header', 'footer', 'cell', 'textrun', 'footnote', 'endnote', 'textbox');
$validContainers = array( $validContainers = array(
'Text' => $allContainers, 'Text' => $allContainers,
'Link' => $allContainers, 'Link' => $allContainers,

View File

@ -27,21 +27,11 @@ use PhpOffice\PhpWord\Style;
*/ */
abstract class AbstractElement abstract class AbstractElement
{ {
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. object
*/
protected $phpWord; protected $phpWord;
/** /**
* Container type section|header|footer|cell|textrun|footnote|endnote * Container type section|header|footer|cell|textrun|footnote|endnote|textbox
* *
* @var string * @var string
*/ */

View File

@ -115,6 +115,21 @@ class Footer extends AbstractContainer
return $this->type = self::EVEN; return $this->type = self::EVEN;
} }
/**
* Add textbox element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\TextBox
* @todo Merge with the same function on Section
*/
public function addTextBox($style = null)
{
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($textbox);
return $textbox;
}
/** /**
* Add table element * Add table element
* *

View File

@ -117,6 +117,21 @@ class Section extends AbstractContainer
$this->addElement(new PageBreak()); $this->addElement(new PageBreak());
} }
/**
* Add textbox element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\TextBox
* @todo Merge with the same function on Footer
*/
public function addTextBox($style = null)
{
$textbox = new TextBox($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($textbox);
return $textbox;
}
/** /**
* Add table element * Add table element
* *

View File

@ -0,0 +1,57 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/**
* Table element
*/
class TextBox extends AbstractContainer
{
/**
* TextBox style
*
* @var \PhpOffice\PhpWord\Style\TextBox
*/
private $style;
/**
* Create a new textbox
*
* @param string $docPart
* @param integer $docPartId
* @param mixed $style
*/
public function __construct($docPart, $docPartId, $style = null)
{
$this->container = 'textbox';
$this->setDocPart($docPart, $docPartId);
$this->style = $this->setStyle(new TextBoxStyle(), $style);
}
/**
* Get textbox style
*
* @return \PhpOffice\PhpWord\Style\TextBox
*/
public function getStyle()
{
return $this->style;
}
}

View File

@ -0,0 +1,641 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Style;
/**
* TextBox style
*/
class TextBox extends AbstractStyle
{
/**
* Wrapping styles
*
* @const string
*/
const WRAPPING_STYLE_INLINE = 'inline';
const WRAPPING_STYLE_SQUARE = 'square';
const WRAPPING_STYLE_TIGHT = 'tight';
const WRAPPING_STYLE_BEHIND = 'behind';
const WRAPPING_STYLE_INFRONT = 'infront';
/**
* Horizontal alignment
*
* @const string
*/
const POSITION_HORIZONTAL_LEFT = 'left';
const POSITION_HORIZONTAL_CENTER = 'center';
const POSITION_HORIZONTAL_RIGHT = 'right';
/**
* Vertical alignment
*
* @const string
*/
const POSITION_VERTICAL_TOP = 'top';
const POSITION_VERTICAL_CENTER = 'center';
const POSITION_VERTICAL_BOTTOM = 'bottom';
const POSITION_VERTICAL_INSIDE = 'inside';
const POSITION_VERTICAL_OUTSIDE = 'outside';
/**
* Position relative to
*
* @const string
*/
const POSITION_RELATIVE_TO_MARGIN = 'margin';
const POSITION_RELATIVE_TO_PAGE = 'page';
const POSITION_RELATIVE_TO_COLUMN = 'column';
const POSITION_RELATIVE_TO_CHAR = 'char';
const POSITION_RELATIVE_TO_LMARGIN = 'left-margin-area';
const POSITION_RELATIVE_TO_RMARGIN = 'right-margin-area';
const POSITION_RELATIVE_TO_IMARGIN = 'inner-margin-area';
const POSITION_RELATIVE_TO_OMARGIN = 'outer-margin-area';
const POSITION_RELATIVE_TO_LINE = 'line';
const POSITION_RELATIVE_TO_TMARGIN = 'top-margin-area';
const POSITION_RELATIVE_TO_BMARGIN = 'bottom-margin-area';
/**
* Position type, relative/absolute
*
* @const string
*/
const POSITION_RELATIVE = 'relative';
const POSITION_ABSOLUTE = 'absolute';
/**
* TextBox width
*
* @var int
*/
private $width;
/**
* TextBox height
*
* @var int
*/
private $height;
/**
* Alignment
*
* @var string
*/
private $align;
/**
* Margin Top
*
* @var int
*/
private $marginTop;
/**
* Margin Left
*
* @var int
*/
private $marginLeft;
/**
* Wrapping style
*
* @var string
*/
private $wrappingStyle;
/**
* Positioning type (relative or absolute)
*
* @var string
*/
private $positioning;
/**
* Horizontal alignment
*
* @var string
*/
private $posHorizontal;
/**
* Horizontal Relation
*
* @var string
*/
private $posHorizontalRel;
/**
* Vertical alignment
*
* @var string
*/
private $posVertical;
/**
* Vertical Relation
*
* @var string
*/
private $posVerticalRel;
/**
* margin top
*
* @var int
*/
private $innerMarginTop = null;
/**
* margin left
*
* @var int
*/
private $innerMarginLeft = null;
/**
* margin right
*
* @var int
*/
private $innerMarginRight = null;
/**
* Cell margin bottom
*
* @var int
*/
private $innerMarginBottom = null;
/**
* border size
*
* @var int
*/
private $borderSize = null;
/**
* border color
*
* @var string
*/
private $borderColor;
/**
* Create new textbox style
*/
public function __construct()
{
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
$this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT);
$this->setPosHorizontalRel(self::POSITION_RELATIVE_TO_CHAR);
$this->setPosVertical(self::POSITION_VERTICAL_TOP);
$this->setPosVerticalRel(self::POSITION_RELATIVE_TO_LINE);
}
/**
* Get width
*/
public function getWidth()
{
return $this->width;
}
/**
* Set width
*
* @param int $value
*/
public function setWidth($value = null)
{
$this->width = $value;
}
/**
* Get height
*/
public function getHeight()
{
return $this->height;
}
/**
* Set height
*
* @param int $value
*/
public function setHeight($value = null)
{
$this->height = $value;
}
/**
* Get alignment
*/
public function getAlign()
{
return $this->align;
}
/**
* Set alignment
*
* @param string $value
*/
public function setAlign($value = null)
{
$this->align = $value;
}
/**
* Get Margin Top
*
* @return int
*/
public function getMarginTop()
{
return $this->marginTop;
}
/**
* Set Margin Top
*
* @param int $value
* @return self
*/
public function setMarginTop($value = null)
{
$this->marginTop = $value;
return $this;
}
/**
* Get Margin Left
*
* @return int
*/
public function getMarginLeft()
{
return $this->marginLeft;
}
/**
* Set Margin Left
*
* @param int $value
* @return self
*/
public function setMarginLeft($value = null)
{
$this->marginLeft = $value;
return $this;
}
/**
* Get wrapping style
*
* @return string
*/
public function getWrappingStyle()
{
return $this->wrappingStyle;
}
/**
* Set wrapping style
*
* @param string $wrappingStyle
* @throws \InvalidArgumentException
* @return self
*/
public function setWrappingStyle($wrappingStyle)
{
$enum = array(self::WRAPPING_STYLE_INLINE, self::WRAPPING_STYLE_INFRONT, self::WRAPPING_STYLE_BEHIND,
self::WRAPPING_STYLE_SQUARE, self::WRAPPING_STYLE_TIGHT);
if (in_array($wrappingStyle, $enum)) {
$this->wrappingStyle = $wrappingStyle;
} else {
throw new \InvalidArgumentException('Invalid wrapping style.');
}
return $this;
}
/**
* Get positioning type
*
* @return string
*/
public function getPositioning()
{
return $this->positioning;
}
/**
* Set positioning type
*
* @param string $positioning
* @throws \InvalidArgumentException
* @return self
*/
public function setPositioning($positioning)
{
$enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE);
if (in_array($positioning, $enum)) {
$this->positioning = $positioning;
} else {
throw new \InvalidArgumentException('Invalid positioning.');
}
return $this;
}
/**
* Get horizontal alignment
*
* @return string
*/
public function getPosHorizontal()
{
return $this->posHorizontal;
}
/**
* Set horizontal alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return self
*/
public function setPosHorizontal($alignment)
{
$enum = array(self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER,
self::POSITION_HORIZONTAL_RIGHT);
if (in_array($alignment, $enum)) {
$this->posHorizontal = $alignment;
} else {
throw new \InvalidArgumentException('Invalid horizontal alignment.');
}
return $this;
}
/**
* Get vertical alignment
*
* @return string
*/
public function getPosVertical()
{
return $this->posVertical;
}
/**
* Set vertical alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return self
*/
public function setPosVertical($alignment)
{
$enum = array(self::POSITION_VERTICAL_TOP, self::POSITION_VERTICAL_CENTER,
self::POSITION_VERTICAL_BOTTOM, self::POSITION_VERTICAL_INSIDE, self::POSITION_VERTICAL_OUTSIDE);
if (in_array($alignment, $enum)) {
$this->posVertical = $alignment;
} else {
throw new \InvalidArgumentException('Invalid vertical alignment.');
}
return $this;
}
/**
* Get horizontal relation
*
* @return string
*/
public function getPosHorizontalRel()
{
return $this->posHorizontalRel;
}
/**
* Set horizontal relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return self
*/
public function setPosHorizontalRel($relto)
{
$enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE,
self::POSITION_RELATIVE_TO_COLUMN, self::POSITION_RELATIVE_TO_CHAR,
self::POSITION_RELATIVE_TO_LMARGIN, self::POSITION_RELATIVE_TO_RMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN);
if (in_array($relto, $enum)) {
$this->posHorizontalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative horizontal alignment.');
}
return $this;
}
/**
* Get vertical relation
*
* @return string
*/
public function getPosVerticalRel()
{
return $this->posVerticalRel;
}
/**
* Set vertical relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return self
*/
public function setPosVerticalRel($relto)
{
$enum = array(self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE,
self::POSITION_RELATIVE_TO_LINE,
self::POSITION_RELATIVE_TO_TMARGIN, self::POSITION_RELATIVE_TO_BMARGIN,
self::POSITION_RELATIVE_TO_IMARGIN, self::POSITION_RELATIVE_TO_OMARGIN);
if (in_array($relto, $enum)) {
$this->posVerticalRel = $relto;
} else {
throw new \InvalidArgumentException('Invalid relative vertical alignment.');
}
return $this;
}
/**
* Set margin top
*
* @param int $value
*/
public function setInnerMarginTop($value = null)
{
$this->innerMarginTop = $value;
}
/**
* Get margin top
*
* @return int
*/
public function getInnerMarginTop()
{
return $this->innerMarginTop;
}
/**
* Set margin left
*
* @param int $value
*/
public function setInnerMarginLeft($value = null)
{
$this->innerMarginLeft = $value;
}
/**
* Get margin left
*
* @return int
*/
public function getInnerMarginLeft()
{
return $this->innerMarginLeft;
}
/**
* Set margin right
*
* @param int $value
*/
public function setInnerMarginRight($value = null)
{
$this->innerMarginRight = $value;
}
/**
* Get margin right
*
* @return int
*/
public function getInnerMarginRight()
{
return $this->innerMarginRight;
}
/**
* Set margin bottom
*
* @param int $value
*/
public function setInnerMarginBottom($value = null)
{
$this->innerMarginBottom = $value;
}
/**
* Get margin bottom
*
* @return int
*/
public function getInnerMarginBottom()
{
return $this->innerMarginBottom;
}
/**
* Set TLRB cell margin
*
* @param int $value Margin in twips
*/
public function setInnerMargin($value = null)
{
$this->setInnerMarginTop($value);
$this->setInnerMarginLeft($value);
$this->setInnerMarginRight($value);
$this->setInnerMarginBottom($value);
}
/**
* Get cell margin
*
* @return int[]
*/
public function getInnerMargin()
{
return array($this->innerMarginLeft, $this->innerMarginTop, $this->innerMarginRight, $this->innerMarginBottom);
}
/**
* Set border size
*
* @param int $value Size in points
*/
public function setBorderSize($value = null)
{
$this->borderSize = $value;
}
/**
* Get border size
*
* @return int
*/
public function getBorderSize()
{
return $this->borderSize;
}
/**
* Set border color
*
* @param string $value
*/
public function setBorderColor($value = null)
{
$this->borderColor = $value;
}
/**
* Get border color
*
* @return string
*/
public function getBorderColor()
{
return $this->borderColor;
}
}

View File

@ -0,0 +1,72 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
use PhpOffice\PhpWord\Writer\Word2007\Style\TextBox as TextBoxStyleWriter;
/**
* TextBox element writer
*
*/
class TextBox extends Element
{
/**
* Write element
*/
public function write()
{
$tbxStyle = $this->element->getStyle();
if ($tbxStyle instanceof TextBoxStyle) {
$styleWriter = new TextBoxStyleWriter($this->xmlWriter, $tbxStyle);
$styleWriter->write();
}
if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p');
if (!is_null($tbxStyle->getAlign())) {
$this->xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:jc');
$this->xmlWriter->writeAttribute('w:val', $tbxStyle->getAlign());
$this->xmlWriter->endElement(); // w:jc
$this->xmlWriter->endElement(); // w:pPr
}
}
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:pict');
$this->xmlWriter->startElement('v:shape');
$this->xmlWriter->writeAttribute('type', '#_x0000_t0202');
$styleWriter->write();
$this->xmlWriter->startElement('v:textbox');
$margins=implode(', ',$tbxStyle->getInnerMargin());
$this->xmlWriter->writeAttribute('inset', $margins);
$this->xmlWriter->startElement('w:txbxContent');
$this->parentWriter->writeContainerElements($this->xmlWriter, $this->element);
$this->xmlWriter->endElement(); // w:txbxContent
$this->xmlWriter->endElement(); // v: textbox
$styleWriter->writeW10Wrap();
$this->xmlWriter->endElement(); // v:shape
$this->xmlWriter->endElement(); // w:pict
$this->xmlWriter->endElement(); // w:r
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
}
}

View File

@ -93,10 +93,11 @@ abstract class AbstractPart
$elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object'); $elmCommon = array('Text', 'Link', 'TextBreak', 'Image', 'Object');
$elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox')); $elmMainCell = array_merge($elmCommon, array('TextRun', 'ListItem', 'CheckBox'));
$allowedElements = array( $allowedElements = array(
'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Title', 'PageBreak', 'TOC')), 'Section' => array_merge($elmMainCell, array('Table', 'Footnote', 'Title', 'PageBreak', 'TOC', 'TextBox')),
'Header' => array_merge($elmMainCell, array('Table', 'PreserveText')), 'Header' => array_merge($elmMainCell, array('Table', 'PreserveText', 'TextBox')),
'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText')), 'Footer' => array_merge($elmMainCell, array('Table', 'PreserveText', 'TextBox')),
'Cell' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')), 'Cell' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')),
'TextBox' => array_merge($elmMainCell, array('PreserveText', 'Footnote', 'Endnote')),
'TextRun' => array_merge($elmCommon, array('Footnote', 'Endnote')), 'TextRun' => array_merge($elmCommon, array('Footnote', 'Endnote')),
'Footnote' => $elmCommon, 'Footnote' => $elmCommon,
'Endnote' => $elmCommon, 'Endnote' => $elmCommon,
@ -104,7 +105,7 @@ abstract class AbstractPart
$containerName = get_class($container); $containerName = get_class($container);
$containerName = substr($containerName, strrpos($containerName, '\\') + 1); $containerName = substr($containerName, strrpos($containerName, '\\') + 1);
if (!array_key_exists($containerName, $allowedElements)) { if (!array_key_exists($containerName, $allowedElements)) {
throw new Exception('Invalid container.'); throw new Exception('Invalid container.'.$containerName. print_r($allowedElements, true));
} }
// Loop through elements // Loop through elements

View File

@ -0,0 +1,198 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/**
* TextBox style writer
*
*/
class TextBox extends AbstractStyle
{
/**
* w10 namespace wrapping type
*
* @var string
*/
private $w10wrap;
/**
* Write style
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\TextBox)) {
return;
}
$wrapping = $this->style->getWrappingStyle();
$positioning = $this->style->getPositioning();
// Default style array
$styleArray = array(
'mso-width-percent' => '0',
'mso-height-percent' => '0',
'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin',
);
$styleArray = array_merge($styleArray, $this->getElementStyle());
// Absolute/relative positioning
$styleArray['position'] = $positioning;
if ($positioning == TextBoxStyle::POSITION_ABSOLUTE) {
$styleArray['mso-position-horizontal-relative'] = 'page';
$styleArray['mso-position-vertical-relative'] = 'page';
} elseif ($positioning == TextBoxStyle::POSITION_RELATIVE) {
$styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal();
$styleArray['mso-position-vertical'] = $this->style->getPosVertical();
$styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel();
$styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel();
$styleArray['margin-left'] = 0;
$styleArray['margin-top'] = 0;
}
// Wrapping style
if ($wrapping == TextBoxStyle::WRAPPING_STYLE_INLINE) {
// Nothing to do when inline
} elseif ($wrapping == TextBoxStyle::WRAPPING_STYLE_BEHIND) {
$styleArray['z-index'] = -251658752;
} else {
$styleArray['z-index'] = 251659264;
$styleArray['mso-position-horizontal'] = 'absolute';
$styleArray['mso-position-vertical'] = 'absolute';
}
// w10 wrapping
if ($wrapping == TextBoxStyle::WRAPPING_STYLE_SQUARE) {
$this->w10wrap = 'square';
} elseif ($wrapping == TextBoxStyle::WRAPPING_STYLE_TIGHT) {
$this->w10wrap = 'tight';
}
$textboxStyle = $this->assembleStyle($styleArray);
$this->xmlWriter->writeAttribute('style', $textboxStyle);
$borderSize = $this->style->getBorderSize();
if ($borderSize !== null) {
$this->xmlWriter->writeAttribute('strokeweight', $this->style->getBorderSize().'pt');
}
$borderColor = $this->style->getBorderColor();
if (empty($borderColor)) {
$this->xmlWriter->writeAttribute('stroked', 'f');
} else {
$this->xmlWriter->writeAttribute('strokecolor', $borderColor);
}
//@todo <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
}
/**
* Write w10 wrapping
*
* @return array
*/
public function writeW10Wrap()
{
if (!is_null($this->w10wrap)) {
$this->xmlWriter->startElement('w10:wrap');
$this->xmlWriter->writeAttribute('type', $this->w10wrap);
switch ($this->style->getPositioning()) {
case TextBoxStyle::POSITION_ABSOLUTE:
$this->xmlWriter->writeAttribute('anchorx', "page");
$this->xmlWriter->writeAttribute('anchory', "page");
break;
case TextBoxStyle::POSITION_RELATIVE:
switch ($this->style->getPosVerticalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchory', "page");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN:
$this->xmlWriter->writeAttribute('anchory', "page");
break;
}
switch ($this->style->getPosHorizontalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchorx', "page");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "page");
break;
}
}
$this->xmlWriter->endElement(); // w10:wrap
}
}
/**
* Get element style
*
* @return array
*/
private function getElementStyle()
{
$styles = array();
$styleValues = array(
'width' => $this->style->getWidth(),
'height' => $this->style->getHeight(),
'margin-top' => $this->style->getMarginTop(),
'margin-left' => $this->style->getMarginLeft()
);
foreach ($styleValues as $key => $value) {
if (!is_null($value) && $value != '') {
$styles[$key] = $value . 'px';
}
}
return $styles;
}
/**
* Assemble style array into style string
*
* @param array $styles
* @return string
*/
private function assembleStyle($styles = array())
{
$style = '';
foreach ($styles as $key => $value) {
if (!is_null($value) && $value != '') {
$style .= "{$key}:{$value}; ";
}
}
return trim($style);
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\Element\TextBox;
/**
* Test class for PhpOffice\PhpWord\Element\TextBox
*
* @coversDefaultClass \PhpOffice\PhpWord\Element\TextBox
* @runTestsInSeparateProcesses
*/
class TextBoxTest extends \PHPUnit_Framework_TestCase
{
/**
* Create new instance
*/
public function testConstruct()
{
$oTextBox = new TextBox('section', 1);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextBox', $oTextBox);
$this->assertEquals($oTextBox->getStyle(), null);
}
/**
* Get style name
*/
public function testStyleText()
{
$oTextBox = new TextBox('section', 1, 'textBoxStyle');
$this->assertEquals($oTextBox->getStyle(), 'textBoxStyle');
}
/**
* Get style array
*/
public function testStyleArray()
{
$oTextBox = new TextBox(
'section',
1,
array(
'width' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(4.5),
'height' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(17.5),
'positioning' => 'absolute',
'marginLeft' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(15.4),
'marginTop' => \PhpOffice\PhpWord\Shared\Drawing::centimetersToPixels(9.9),
'stroke' => 0,
'innerMargin' => 0,
'borderSize' => 1,
'borderColor' => ''
)
);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\TextBox', $oTextBox->getStyle());
}
}

View File

@ -0,0 +1,305 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Style;
use PhpOffice\PhpWord\Style\TextBox;
/**
* Test class for PhpOffice\PhpWord\Style\Image
*
* @coversDefaultClass \PhpOffice\PhpWord\Style\Image
* @runTestsInSeparateProcesses
*/
class TextBoxTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new TextBox();
$properties = array(
'width' => 200,
'height' => 200,
'align' => 'left',
'marginTop' => 240,
'marginLeft' => 240,
'wrappingStyle' => 'inline',
'positioning' => 'absolute',
'posHorizontal' => 'center',
'posVertical' => 'top',
'posHorizontalRel' => 'margin',
'posVerticalRel' => 'page',
'innerMarginTop' => '5',
'innerMarginRight' => '5',
'innerMarginBottom' => '5',
'innerMarginLeft' => '5',
'borderSize' => '2',
'borderColor' => 'red'
);
foreach ($properties as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test setStyleValue method
*/
public function testSetStyleValue()
{
$object = new TextBox();
$properties = array(
'width' => 200,
'height' => 200,
'align' => 'left',
'marginTop' => 240,
'marginLeft' => 240,
'wrappingStyle' => 'inline',
'positioning' => 'absolute',
'posHorizontal' => 'center',
'posVertical' => 'top',
'posHorizontalRel' => 'margin',
'posVerticalRel' => 'page',
'innerMarginTop' => '5',
'innerMarginRight' => '5',
'innerMarginBottom' => '5',
'innerMarginLeft' => '5',
'borderSize' => '2',
'borderColor' => 'red'
);
foreach ($properties as $key => $value) {
$get = "get{$key}";
$object->setStyleValue("{$key}", $value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test setWrappingStyle exception
*
* @expectedException \InvalidArgumentException
*/
public function testSetWrappingStyleException()
{
$object = new TextBox();
$object->setWrappingStyle('foo');
}
/**
* Test set/get width
*/
public function testSetGetWidth()
{
$expected=200;
$object = new TextBox();
$object->setWidth($expected);
$this->assertEquals($expected, $object->getWidth());
}
/**
* Test set/get height
*/
public function testSetGetHeight()
{
$expected=200;
$object = new TextBox();
$object->setHeight($expected);
$this->assertEquals($expected, $object->getHeight());
}
/**
* Test set/get height
*/
public function testSetGetAlign()
{
$expected='left';
$object = new TextBox();
$object->setAlign($expected);
$this->assertEquals($expected, $object->getAlign());
}
/**
* Test set/get marginTop
*/
public function testSetGetMarginTop()
{
$expected=5;
$object = new TextBox();
$object->setMarginTop($expected);
$this->assertEquals($expected, $object->getMarginTop());
}
/**
* Test set/get marginLeft
*/
public function testSetGetMarginLeft()
{
$expected=5;
$object = new TextBox();
$object->setMarginLeft($expected);
$this->assertEquals($expected, $object->getMarginLeft());
}
/**
* Test set/get innerMarginTop
*/
public function testSetGetInnerMarginTop()
{
$expected=5;
$object = new TextBox();
$object->setInnerMarginTop($expected);
$this->assertEquals($expected, $object->getInnerMarginTop());
}
/**
* Test set/get wrappingStyle
*/
public function testSetGetWrappingStyle()
{
$expected='inline';
$object = new TextBox();
$object->setWrappingStyle($expected);
$this->assertEquals($expected, $object->getWrappingStyle());
}
/**
* Test set/get positioning
*/
public function testSetGetPositioning()
{
$expected='absolute';
$object = new TextBox();
$object->setPositioning($expected);
$this->assertEquals($expected, $object->getPositioning());
}
/**
* Test set/get posHorizontal
*/
public function testSetGetPosHorizontal()
{
$expected='center';
$object = new TextBox();
$object->setPosHorizontal($expected);
$this->assertEquals($expected, $object->getPosHorizontal());
}
/**
* Test set/get posVertical
*/
public function testSetGetPosVertical()
{
$expected='top';
$object = new TextBox();
$object->setPosVertical($expected);
$this->assertEquals($expected, $object->getPosVertical());
}
/**
* Test set/get posHorizontalRel
*/
public function testSetGetPosHorizontalRel()
{
$expected='margin';
$object = new TextBox();
$object->setPosHorizontalRel($expected);
$this->assertEquals($expected, $object->getPosHorizontalRel());
}
/**
* Test set/get posVerticalRel
*/
public function testSetGetPosVerticalRel()
{
$expected='page';
$object = new TextBox();
$object->setPosVerticalRel($expected);
$this->assertEquals($expected, $object->getPosVerticalRel());
}
/**
* Test set/get innerMarginRight
*/
public function testSetGetInnerMarginRight()
{
$expected=5;
$object = new TextBox();
$object->setInnerMarginRight($expected);
$this->assertEquals($expected, $object->getInnerMarginRight());
}
/**
* Test set/get innerMarginBottom
*/
public function testSetGetInnerMarginBottom()
{
$expected=5;
$object = new TextBox();
$object->setInnerMarginBottom($expected);
$this->assertEquals($expected, $object->getInnerMarginBottom());
}
/**
* Test set/get innerMarginLeft
*/
public function testSetGetInnerMarginLeft()
{
$expected=5;
$object = new TextBox();
$object->setInnerMarginLeft($expected);
$this->assertEquals($expected, $object->getInnerMarginLeft());
}
/**
* Test set/get innerMarginLeft
*/
public function testSetGetInnerMargin()
{
$expected=5;
$object = new TextBox();
$object->setInnerMargin($expected);
$this->assertEquals(array($expected, $expected, $expected, $expected), $object->getInnerMargin());
}
/**
* Test set/get borderSize
*/
public function testSetGetBorderSize()
{
$expected=2;
$object = new TextBox();
$object->setBorderSize($expected);
$this->assertEquals($expected, $object->getBorderSize());
}
/**
* Test set/get borderColor
*/
public function testSetGetBorderColor()
{
$expected='red';
$object = new TextBox();
$object->setBorderColor($expected);
$this->assertEquals($expected, $object->getBorderColor());
}
}