Refactor styles: Inherit Image and Line from Frame

This commit is contained in:
Ivan Lanin 2014-06-05 12:52:39 +07:00
parent 2328e34e64
commit 1accec2ff0
14 changed files with 578 additions and 527 deletions

View File

@ -27,6 +27,80 @@ namespace PhpOffice\PhpWord\Style;
*/
class Frame extends AbstractStyle
{
/**
* Length unit
*
* @const string
*/
const UNIT_PT = 'pt'; // Mostly for shapes
const UNIT_PX = 'px'; // Mostly for images
/**
* Position type, relative/absolute
*
* @const string
*/
const POS_ABSOLUTE = 'absolute';
const POS_RELATIVE = 'relative';
/**
* Horizontal/vertical value
*
* @const string
*/
const POS_CENTER = 'center';
const POS_LEFT = 'left';
const POS_RIGHT = 'right';
const POS_TOP = 'top';
const POS_BOTTOM = 'bottom';
const POS_INSIDE = 'inside';
const POS_OUTSIDE = 'outside';
/**
* Position relative to
*
* @const string
*/
const POS_RELTO_MARGIN = 'margin';
const POS_RELTO_PAGE = 'page';
const POS_RELTO_COLUMN = 'column'; // horizontal only
const POS_RELTO_CHAR = 'char'; // horizontal only
const POS_RELTO_TEXT = 'text'; // vertical only
const POS_RELTO_LINE = 'line'; // vertical only
const POS_RELTO_LMARGIN = 'left-margin-area'; // horizontal only
const POS_RELTO_RMARGIN = 'right-margin-area'; // horizontal only
const POS_RELTO_TMARGIN = 'top-margin-area'; // vertical only
const POS_RELTO_BMARGIN = 'bottom-margin-area'; // vertical only
const POS_RELTO_IMARGIN = 'inner-margin-area';
const POS_RELTO_OMARGIN = 'outer-margin-area';
/**
* Wrap type
*
* @const string
*/
const WRAP_INLINE = 'inline';
const WRAP_SQUARE = 'square';
const WRAP_TIGHT = 'tight';
const WRAP_THROUGH = 'through';
const WRAP_TOPBOTTOM = 'topAndBottom';
const WRAP_BEHIND = 'behind';
const WRAP_INFRONT = 'infront';
/**
* Alignment
*
* @var \PhpOffice\PhpWord\Style\Alignment
*/
private $alignment;
/**
* Unit
*
* @var string
*/
private $unit = 'pt';
/**
* Width
*
@ -55,6 +129,48 @@ class Frame extends AbstractStyle
*/
private $top = 0;
/**
* Position type: absolute|relative
*
* @var string
*/
private $pos;
/**
* Horizontal position
*
* @var string
*/
private $hPos;
/**
* Horizontal position relative to
*
* @var string
*/
private $hPosRelTo;
/**
* Vertical position
*
* @var string
*/
private $vPos;
/**
* Vertical position relative to
*
* @var string
*/
private $vPosRelTo;
/**
* Wrap type
*
* @var string
*/
private $wrap;
/**
* Create a new instance
*
@ -62,9 +178,56 @@ class Frame extends AbstractStyle
*/
public function __construct($style = array())
{
$this->alignment = new Alignment();
$this->setStyleByArray($style);
}
/**
* Get alignment
*
* @return string
*/
public function getAlign()
{
return $this->alignment->getValue();
}
/**
* Set alignment
*
* @param string $value
* @return self
*/
public function setAlign($value = null)
{
$this->alignment->setValue($value);
return $this;
}
/**
* Get unit
*
* @return string
*/
public function getUnit()
{
return $this->unit;
}
/**
* Set unit
*
* @param string $value
* @return self
*/
public function setUnit($value)
{
$this->unit = $value;
return $this;
}
/**
* Get width
*
@ -156,4 +319,157 @@ class Frame extends AbstractStyle
return $this;
}
/**
* Get position type
*
* @return string
*/
public function getPos()
{
return $this->pos;
}
/**
* Set position type
*
* @param string $value
* @return self
*/
public function setPos($value)
{
$enum = array(self::POS_RELATIVE, self::POS_ABSOLUTE);
$this->pos = $this->setEnumVal($value, $enum, $this->pos);
return $this;
}
/**
* Get horizontal position
*
* @return string
*/
public function getHPos()
{
return $this->hPos;
}
/**
* Set horizontal position
*
* @param string $value
* @return self
*/
public function setHPos($value)
{
$enum = array(self::POS_LEFT, self::POS_CENTER, self::POS_RIGHT, self::POS_INSIDE, self::POS_OUTSIDE);
$this->hPos = $this->setEnumVal($value, $enum, $this->hPos);
return $this;
}
/**
* Get vertical position
*
* @return string
*/
public function getVPos()
{
return $this->vPos;
}
/**
* Set vertical position
*
* @param string $value
* @return self
*/
public function setVPos($value)
{
$enum = array(self::POS_TOP, self::POS_CENTER, self::POS_BOTTOM, self::POS_INSIDE, self::POS_OUTSIDE);
$this->vPos = $this->setEnumVal($value, $enum, $this->vPos);
return $this;
}
/**
* Get horizontal position relative to
*
* @return string
*/
public function getHPosRelTo()
{
return $this->hPosRelTo;
}
/**
* Set horizontal position relative to
*
* @param string $value
* @return self
*/
public function setHPosRelTo($value)
{
$enum = array(
self::POS_RELTO_MARGIN, self::POS_RELTO_PAGE, self::POS_RELTO_COLUMN, self::POS_RELTO_CHAR,
self::POS_RELTO_LMARGIN, self::POS_RELTO_RMARGIN, self::POS_RELTO_IMARGIN, self::POS_RELTO_OMARGIN,
);
$this->hPosRelTo = $this->setEnumVal($value, $enum, $this->hPosRelTo);
return $this;
}
/**
* Get vertical position relative to
*
* @return string
*/
public function getVPosRelTo()
{
return $this->vPosRelTo;
}
/**
* Set vertical position relative to
*
* @param string $value
* @return self
*/
public function setVPosRelTo($value)
{
$enum = array(
self::POS_RELTO_MARGIN, self::POS_RELTO_PAGE, self::POS_RELTO_TEXT, self::POS_RELTO_LINE,
self::POS_RELTO_TMARGIN, self::POS_RELTO_BMARGIN, self::POS_RELTO_IMARGIN, self::POS_RELTO_OMARGIN,
);
$this->vPosRelTo = $this->setEnumVal($value, $enum, $this->vPosRelTo);
return $this;
}
/**
* Get wrap type
*
* @return string
*/
public function getWrap()
{
return $this->wrap;
}
/**
* Set wrap type
*
* @param string $value
* @return self
*/
public function setWrap($value)
{
$enum = array(
self::WRAP_INLINE, self::WRAP_SQUARE, self::WRAP_TIGHT, self::WRAP_THROUGH,
self::WRAP_TOPBOTTOM, self::WRAP_BEHIND, self::WRAP_INFRONT
);
$this->wrap = $this->setEnumVal($value, $enum, $this->wrap);
return $this;
}
}

View File

@ -19,217 +19,56 @@ namespace PhpOffice\PhpWord\Style;
/**
* Image and memory image style
*/
class Image extends AbstractStyle
class Image extends Frame
{
/**
* Wrapping styles
* Backward compatibility constants
*
* @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'; // horizontal only
const POSITION_RELATIVE_TO_CHAR = 'char'; // horizontal only
const POSITION_RELATIVE_TO_TEXT = 'text'; // vertical only
const POSITION_RELATIVE_TO_LINE = 'line'; // vertical only
const POSITION_RELATIVE_TO_LMARGIN = 'left-margin-area'; // horizontal only
const POSITION_RELATIVE_TO_RMARGIN = 'right-margin-area'; // horizontal only
const POSITION_RELATIVE_TO_TMARGIN = 'top-margin-area'; // vertical only
const POSITION_RELATIVE_TO_BMARGIN = 'bottom-margin-area'; // vertical only
const POSITION_RELATIVE_TO_IMARGIN = 'inner-margin-area';
const POSITION_RELATIVE_TO_OMARGIN = 'outer-margin-area';
/**
* Position type, relative/absolute
*
* @const string
*/
const POSITION_ABSOLUTE = 'absolute';
const POSITION_RELATIVE = 'relative';
/**
* Image width
*
* @var int
*/
private $width;
/**
* Image width
*
* @var int
*/
private $height;
/**
* Alignment
*
* @var \PhpOffice\PhpWord\Style\Alignment
*/
private $alignment;
/**
* Margin Top
*
* @var int|float
*/
private $marginTop = 0;
/**
* Margin Left
*
* @var int|float
*/
private $marginLeft = 0;
/**
* Wrapping style
*
* @var string
*/
private $wrappingStyle = self::WRAPPING_STYLE_INLINE;
/**
* Positioning type (relative or absolute)
*
* @var string
*/
private $positioning;
/**
* Horizontal alignment
*
* @var string
*/
private $posHorizontal = self::POSITION_HORIZONTAL_LEFT;
/**
* Horizontal Relation
*
* @var string
*/
private $posHorizontalRel = self::POSITION_RELATIVE_TO_CHAR;
/**
* Vertical alignment
*
* @var string
*/
private $posVertical = self::POSITION_VERTICAL_TOP;
/**
* Vertical Relation
*
* @var string
*/
private $posVerticalRel = self::POSITION_RELATIVE_TO_LINE;
const WRAPPING_STYLE_INLINE = self::WRAP_INLINE;
const WRAPPING_STYLE_SQUARE = self::WRAP_SQUARE;
const WRAPPING_STYLE_TIGHT = self::WRAP_TIGHT;
const WRAPPING_STYLE_BEHIND = self::WRAP_BEHIND;
const WRAPPING_STYLE_INFRONT = self::WRAP_INFRONT;
const POSITION_HORIZONTAL_LEFT = self::POS_LEFT;
const POSITION_HORIZONTAL_CENTER = self::POS_CENTER;
const POSITION_HORIZONTAL_RIGHT = self::POS_RIGHT;
const POSITION_VERTICAL_TOP = self::POS_TOP;
const POSITION_VERTICAL_CENTER = self::POS_CENTER;
const POSITION_VERTICAL_BOTTOM = self::POS_BOTTOM;
const POSITION_VERTICAL_INSIDE = self::POS_INSIDE;
const POSITION_VERTICAL_OUTSIDE = self::POS_OUTSIDE;
const POSITION_RELATIVE_TO_MARGIN = self::POS_RELTO_MARGIN;
const POSITION_RELATIVE_TO_PAGE = self::POS_RELTO_PAGE;
const POSITION_RELATIVE_TO_COLUMN = self::POS_RELTO_COLUMN;
const POSITION_RELATIVE_TO_CHAR = self::POS_RELTO_CHAR;
const POSITION_RELATIVE_TO_TEXT = self::POS_RELTO_TEXT;
const POSITION_RELATIVE_TO_LINE = self::POS_RELTO_LINE;
const POSITION_RELATIVE_TO_LMARGIN = self::POS_RELTO_LMARGIN;
const POSITION_RELATIVE_TO_RMARGIN = self::POS_RELTO_RMARGIN;
const POSITION_RELATIVE_TO_TMARGIN = self::POS_RELTO_TMARGIN;
const POSITION_RELATIVE_TO_BMARGIN = self::POS_RELTO_BMARGIN;
const POSITION_RELATIVE_TO_IMARGIN = self::POS_RELTO_IMARGIN;
const POSITION_RELATIVE_TO_OMARGIN = self::POS_RELTO_OMARGIN;
const POSITION_ABSOLUTE = self::POS_ABSOLUTE;
const POSITION_RELATIVE = self::POS_RELATIVE;
/**
* Create new instance
*/
public function __construct()
{
$this->alignment = new Alignment();
}
parent::__construct();
$this->setUnit('px');
/**
* Get width
*
* @return int
*/
public function getWidth()
{
return $this->width;
}
/**
* Set width
*
* @param int $value
* @return self
*/
public function setWidth($value = null)
{
$this->width = $value;
return $this;
}
/**
* Get height
*
* @return int
*/
public function getHeight()
{
return $this->height;
}
/**
* Set height
*
* @param int $value
* @return self
*/
public function setHeight($value = null)
{
$this->height = $value;
return $this;
}
/**
* Get alignment
*
* @return string
*/
public function getAlign()
{
return $this->alignment->getValue();
}
/**
* Set alignment
*
* @param string $value
* @return self
*/
public function setAlign($value = null)
{
$this->alignment->setValue($value);
return $this;
// Backward compatilibity setting
// @todo Remove on 1.0.0
$this->setWrap(self::WRAPPING_STYLE_INLINE);
$this->setHPos(self::POSITION_HORIZONTAL_LEFT);
$this->setHPosRelTo(self::POSITION_RELATIVE_TO_CHAR);
$this->setVPos(self::POSITION_VERTICAL_TOP);
$this->setVPosRelTo(self::POSITION_RELATIVE_TO_LINE);
}
/**
@ -239,7 +78,7 @@ class Image extends AbstractStyle
*/
public function getMarginTop()
{
return $this->marginTop;
return $this->getTop();
}
/**
@ -251,7 +90,7 @@ class Image extends AbstractStyle
*/
public function setMarginTop($value = 0)
{
$this->marginTop = $this->setNumericVal($value, 0);
$this->setTop($value);
return $this;
}
@ -263,7 +102,7 @@ class Image extends AbstractStyle
*/
public function getMarginLeft()
{
return $this->marginLeft;
return $this->getLeft();
}
/**
@ -275,7 +114,7 @@ class Image extends AbstractStyle
*/
public function setMarginLeft($value = 0)
{
$this->marginLeft = $this->setNumericVal($value, 0);
$this->setLeft($value);
return $this;
}
@ -287,7 +126,7 @@ class Image extends AbstractStyle
*/
public function getWrappingStyle()
{
return $this->wrappingStyle;
return $this->getWrap();
}
/**
@ -299,12 +138,7 @@ class Image extends AbstractStyle
*/
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,
);
$this->wrappingStyle = $this->setEnumVal($wrappingStyle, $enum, $this->wrappingStyle);
$this->setWrap($wrappingStyle);
return $this;
}
@ -316,7 +150,7 @@ class Image extends AbstractStyle
*/
public function getPositioning()
{
return $this->positioning;
return $this->getPos();
}
/**
@ -328,8 +162,7 @@ class Image extends AbstractStyle
*/
public function setPositioning($positioning)
{
$enum = array(self::POSITION_RELATIVE, self::POSITION_ABSOLUTE);
$this->positioning = $this->setEnumVal($positioning, $enum, $this->positioning);
$this->setPos($positioning);
return $this;
}
@ -341,7 +174,7 @@ class Image extends AbstractStyle
*/
public function getPosHorizontal()
{
return $this->posHorizontal;
return $this->getHPos();
}
/**
@ -353,11 +186,7 @@ class Image extends AbstractStyle
*/
public function setPosHorizontal($alignment)
{
$enum = array(
self::POSITION_HORIZONTAL_LEFT, self::POSITION_HORIZONTAL_CENTER,
self::POSITION_HORIZONTAL_RIGHT, self::POSITION_ABSOLUTE
);
$this->posHorizontal = $this->setEnumVal($alignment, $enum, $this->posHorizontal);
$this->setHPos($alignment);
return $this;
}
@ -369,7 +198,7 @@ class Image extends AbstractStyle
*/
public function getPosVertical()
{
return $this->posVertical;
return $this->getVPos();
}
/**
@ -381,12 +210,7 @@ class Image extends AbstractStyle
*/
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, self::POSITION_ABSOLUTE
);
$this->posVertical = $this->setEnumVal($alignment, $enum, $this->posVertical);
$this->setVPos($alignment);
return $this;
}
@ -398,7 +222,7 @@ class Image extends AbstractStyle
*/
public function getPosHorizontalRel()
{
return $this->posHorizontalRel;
return $this->getHPosRelTo();
}
/**
@ -410,13 +234,7 @@ class Image extends AbstractStyle
*/
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,
);
$this->posHorizontalRel = $this->setEnumVal($relto, $enum, $this->posHorizontalRel);
$this->setHPosRelTo($relto);
return $this;
}
@ -428,7 +246,7 @@ class Image extends AbstractStyle
*/
public function getPosVerticalRel()
{
return $this->posVerticalRel;
return $this->getVPosRelTo();
}
/**
@ -440,13 +258,7 @@ class Image extends AbstractStyle
*/
public function setPosVerticalRel($relto)
{
$enum = array(
self::POSITION_RELATIVE_TO_MARGIN, self::POSITION_RELATIVE_TO_PAGE,
self::POSITION_RELATIVE_TO_TEXT, 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,
);
$this->posVerticalRel = $this->setEnumVal($relto, $enum, $this->posVerticalRel);
$this->setVPosRelTo($relto);
return $this;
}

View File

@ -21,6 +21,7 @@ namespace PhpOffice\PhpWord\Style;
* Outline defines the line/border of the object
*
* @link http://www.schemacentral.com/sc/ooxml/t-v_CT_Stroke.html
* @link http://www.w3.org/TR/1998/NOTE-VML-19980513#_Toc416858395
* @since 0.12.0
*/
class Outline extends AbstractStyle
@ -37,6 +38,16 @@ class Outline extends AbstractStyle
const LINE_THICK_THIN = 'thickThin';
const LINE_THICK_BETWEEN_THIN = 'thickBetweenThin';
/**
* Line style constants
*
* @link http://www.schemacentral.com/sc/ooxml/t-v_ST_StrokeEndCap.html
* @const string
*/
const ENDCAP_FLAT = 'flat';
const ENDCAP_SQUARE = 'square';
const ENDCAP_ROUND = 'round';
/**
* Arrowhead type constants
*
@ -50,6 +61,13 @@ class Outline extends AbstractStyle
const ARROW_DIAMOND = 'diamond';
const ARROW_OPEN = 'open';
/**
* Unit; No set method for now
*
* @var string
*/
private $unit = 'pt';
/**
* Outline weight
*
@ -78,6 +96,14 @@ class Outline extends AbstractStyle
*/
private $line;
/**
* End cap
*
* @var string
* @link http://www.schemacentral.com/sc/ooxml/t-v_ST_StrokeEndCap.html
*/
private $endCap;
/**
* Start arrow type
*
@ -102,6 +128,16 @@ class Outline extends AbstractStyle
$this->setStyleByArray($style);
}
/**
* Get unit
*
* @return string
*/
public function getUnit()
{
return $this->unit;
}
/**
* Get weight
*
@ -196,6 +232,30 @@ class Outline extends AbstractStyle
return $this;
}
/**
* Get endCap style
*
* @return string
*/
public function getEndCap()
{
return $this->endCap;
}
/**
* Set endCap style
*
* @param string $value
* @return self
*/
public function setEndCap($value = null)
{
$enum = array(self::ENDCAP_FLAT, self::ENDCAP_SQUARE, self::ENDCAP_ROUND);
$this->endCap = $this->setEnumVal($value, $enum, null);
return $this;
}
/**
* Get startArrow
*

View File

@ -52,7 +52,6 @@ class Image extends AbstractElement
private function writeImage(XMLWriter $xmlWriter, ImageElement $element)
{
$rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$style = $element->getStyle();
$styleWriter = new ImageStyleWriter($xmlWriter, $style);
@ -67,7 +66,6 @@ class Image extends AbstractElement
$xmlWriter->writeAttribute('type', '#_x0000_t75');
$styleWriter->write();
$styleWriter->writeW10Wrap();
$xmlWriter->startElement('v:imagedata');
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
@ -78,7 +76,7 @@ class Image extends AbstractElement
$xmlWriter->endElement(); // w:pict
$xmlWriter->endElement(); // w:r
$this->endElementP(); // w:p
$this->endElementP();
}
/**
* Write watermark element
@ -86,29 +84,25 @@ class Image extends AbstractElement
private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element)
{
$rId = $element->getRelationId();
$style = $element->getStyle();
$style->setPositioning('absolute');
$styleWriter = new ImageStyleWriter($xmlWriter, $style);
$xmlWriter->startElement('w:p');
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');
$style->setPositioning('absolute');
$styleWriter->write();
$xmlWriter->startElement('v:imagedata');
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
$xmlWriter->writeAttribute('o:title', '');
$xmlWriter->endElement(); // v:imagedata
$xmlWriter->endElement(); // v:shape
$xmlWriter->endElement(); // w:pict
$xmlWriter->endElement(); // w:r
$xmlWriter->endElement(); // w:p
}
}

View File

@ -76,7 +76,6 @@ class Line extends AbstractElement
$xmlWriter->writeAttribute('type', '#_x0000_t32'); //type should correspond to shapetype id
$styleWriter->write();
$styleWriter->writeW10Wrap();
$styleWriter->writeStroke();
$xmlWriter->endElement(); // v:shape

View File

@ -24,7 +24,7 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\TextBox as TextBoxStyleWriter;
*
* @since 0.11.0
*/
class TextBox extends AbstractElement
class TextBox extends Image
{
/**
* Write element
@ -36,7 +36,6 @@ class TextBox extends AbstractElement
if (!$element instanceof \PhpOffice\PhpWord\Element\TextBox) {
return;
}
$style = $element->getStyle();
$styleWriter = new TextBoxStyleWriter($xmlWriter, $style);
@ -51,6 +50,7 @@ class TextBox extends AbstractElement
$xmlWriter->writeAttribute('type', '#_x0000_t0202');
$styleWriter->write();
$styleWriter->writeBorder();
$xmlWriter->startElement('v:textbox');
$styleWriter->writeInnerMargin();
@ -62,11 +62,11 @@ class TextBox extends AbstractElement
$xmlWriter->endElement(); // w:txbxContent
$xmlWriter->endElement(); // v: textbox
$styleWriter->writeW10Wrap();
$xmlWriter->endElement(); // v:shape
$xmlWriter->endElement(); // w:pict
$xmlWriter->endElement(); // w:r
$this->endElementP(); // w:p
$this->endElementP();
}
}

View File

@ -17,6 +17,10 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
use PhpOffice\PhpWord\Style\Frame as FrameStyle;
/**
* Frame style writer
*
@ -30,27 +34,122 @@ class Frame extends AbstractStyle
public function write()
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Frame) {
if (!$style instanceof FrameStyle) {
return;
}
$xmlWriter = $this->getXmlWriter();
$styles = array();
$zIndices = array(FrameStyle::WRAP_INFRONT => PHP_INT_MAX, FrameStyle::WRAP_BEHIND => -PHP_INT_MAX);
$properties = array(
'left' => 'margin-left',
'top' => 'margin-top',
'width' => 'width',
'height' => 'height',
'left' => 'margin-left',
'top' => 'margin-top',
);
$sizeStyles = $this->getStyles($style, $properties, $style->getUnit());
$properties = array(
'pos' => 'position',
'hPos' => 'mso-position-horizontal',
'vPos' => 'mso-position-vertical',
'hPosRelTo' => 'mso-position-horizontal-relative',
'vPosRelTo' => 'mso-position-vertical-relative',
);
$posStyles = $this->getStyles($style, $properties);
$styles = array_merge($sizeStyles, $posStyles);
// zIndex for infront & behind wrap
$wrap = $style->getWrap();
if ($wrap !== null && array_key_exists($wrap, $zIndices)) {
$styles['z-index'] = $zIndices[$wrap];
$wrap = null;
}
// Style attribute
$xmlWriter->writeAttribute('style', $this->assembleStyle($styles));
$this->writeWrap($xmlWriter, $style, $wrap);
}
/**
* Write alignment
*/
public function writeAlignment()
{
$style = $this->getStyle();
if (!$style instanceof FrameStyle) {
return;
}
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:pPr');
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
$styleWriter->write();
$xmlWriter->endElement(); // w:pPr
}
/**
* Write alignment
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Style\Frame $style
* @param string $wrap
*/
private function writeWrap(XMLWriter $xmlWriter, FrameStyle $style, $wrap)
{
if ($wrap !== null) {
$xmlWriter->startElement('w10:wrap');
$xmlWriter->writeAttribute('type', $wrap);
$relativePositions = array(
FrameStyle::POS_RELTO_MARGIN => 'margin',
FrameStyle::POS_RELTO_PAGE => 'page',
FrameStyle::POS_RELTO_TMARGIN => 'margin',
FrameStyle::POS_RELTO_BMARGIN => 'page',
FrameStyle::POS_RELTO_LMARGIN => 'margin',
FrameStyle::POS_RELTO_RMARGIN => 'page',
);
$pos = $style->getPos();
$hPos = $style->getHPosRelTo();
$vPos = $style->getVPosRelTo();
if ($pos == FrameStyle::POS_ABSOLUTE) {
$xmlWriter->writeAttribute('anchorx', "page");
$xmlWriter->writeAttribute('anchory', "page");
} elseif ($pos == FrameStyle::POS_RELATIVE) {
if (array_key_exists($hPos, $relativePositions)) {
$xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]);
}
if (array_key_exists($vPos, $relativePositions)) {
$xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]);
}
}
$xmlWriter->endElement(); // w10:wrap
}
}
/**
* Get style values in associative array
*
* @param array $properties
* @param string $suffix
* @return array
*/
private function getStyles(FrameStyle $style, $properties, $suffix = '')
{
$styles = array();
foreach ($properties as $key => $property) {
$method = "get{$key}";
$value = $style->$method();
if ($value !== null) {
$styles[$property] = $style->$method() . 'pt';
$styles[$property] = $style->$method() . $suffix;
}
}
$xmlWriter->writeAttribute('style', $this->assembleStyle($styles));
return $styles;
}
}

View File

@ -17,139 +17,11 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
use PhpOffice\PhpWord\Style\Image as ImageStyle;
/**
* Image style writer
*
* @since 0.10.0
*/
class Image extends AbstractStyle
class Image extends Frame
{
/**
* w10 namespace wrapping type
*
* @var string
*/
protected $w10wrap;
/**
* Write style
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof ImageStyle) {
return;
}
$this->writeStyle($style);
}
/**
* Write style attribute
*
* @param \PhpOffice\PhpWord\Style\Image $style
*/
protected function writeStyle($style)
{
$xmlWriter = $this->getXmlWriter();
$styles = $this->getElementStyle($style);
$imageStyle = $this->assembleStyle($styles);
$xmlWriter->writeAttribute('style', $imageStyle);
}
/**
* Write alignment
*/
public function writeAlignment()
{
$style = $this->getStyle();
if (!$style instanceof ImageStyle) {
return;
}
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w:pPr');
$styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
$styleWriter->write();
$xmlWriter->endElement(); // w:pPr
}
/**
* Write w10 wrapping
*/
public function writeW10Wrap()
{
if (is_null($this->w10wrap)) {
return;
}
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w10:wrap');
$xmlWriter->writeAttribute('type', $this->w10wrap);
$xmlWriter->endElement(); // w10:wrap
}
/**
* Get element style
*
* @param \PhpOffice\PhpWord\Style\Image $style
* @return array
*/
protected function getElementStyle(ImageStyle $style)
{
$styles = array(
'mso-width-percent' => '0',
'mso-height-percent' => '0',
'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin',
);
// Dimension
$dimensions = array(
'width' => $style->getWidth(),
'height' => $style->getHeight(),
'margin-top' => $style->getMarginTop(),
'margin-left' => $style->getMarginLeft()
);
foreach ($dimensions as $key => $value) {
if ($value !== null) {
$styles[$key] = $value . 'px';
}
}
// Absolute/relative positioning
$positioning = $style->getPositioning();
$styles['position'] = $positioning;
if ($positioning !== null) {
$styles['mso-position-horizontal'] = $style->getPosHorizontal();
$styles['mso-position-vertical'] = $style->getPosVertical();
$styles['mso-position-horizontal-relative'] = $style->getPosHorizontalRel();
$styles['mso-position-vertical-relative'] = $style->getPosVerticalRel();
}
// Wrapping style
$wrapping = $style->getWrappingStyle();
if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) {
// Nothing to do when inline
} elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) {
$styles['z-index'] = -251658752;
} else {
$styles['z-index'] = 251659264;
$styles['mso-position-horizontal'] = 'absolute';
$styles['mso-position-vertical'] = 'absolute';
}
// w10 wrapping
if ($wrapping == ImageStyle::WRAPPING_STYLE_SQUARE) {
$this->w10wrap = 'square';
} elseif ($wrapping == ImageStyle::WRAPPING_STYLE_TIGHT) {
$this->w10wrap = 'tight';
}
return $styles;
}
}

View File

@ -23,50 +23,12 @@ use PhpOffice\PhpWord\Style\Line as LineStyle;
* Line style writer
*
*/
class Line extends Image
class Line extends Frame
{
/**
* Write style
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof LineStyle) {
return;
}
$this->writeStyle($style);
}
/**
* Write style attribute
*
* @param \PhpOffice\PhpWord\Style\Line $style
*/
protected function writeStyle($style)
{
$xmlWriter = $this->getXmlWriter();
$styles = $this->getElementStyle($style);
if ($style->isFlip()) {
$styles['flip'] = 'y';
}
$imageStyle = $this->assembleStyle($styles);
$xmlWriter->writeAttribute('style', $imageStyle);
// Connector type
$xmlWriter->writeAttribute('o:connectortype', $style->getConnectorType());
// Weight
$weight = $style->getWeight();
$xmlWriter->writeAttributeIf($weight !== null, 'strokeweight', $weight . 'pt');
// Color
$color = $style->getColor();
$xmlWriter->writeAttributeIf($color !== null, 'strokecolor', $color);
}
/**
* Write Line stroke
*
* @todo Merge with `Stroke` style
*/
public function writeStroke()
{
@ -77,8 +39,6 @@ class Line extends Image
}
$dash = $style->getDash();
$beginArrow = $style->getBeginArrow();
$endArrow = $style->getEndArrow();
$dashStyles = array(
LineStyle::DASH_STYLE_DASH => 'dash',
LineStyle::DASH_STYLE_ROUND_DOT => '1 1',
@ -89,11 +49,12 @@ class Line extends Image
LineStyle::DASH_STYLE_LONG_DASH_DOT_DOT => 'longDashDotDot',
);
if (($dash !== null) || ($beginArrow !== null) || ($endArrow !== null)) {
$xmlWriter->startElement('v:stroke');
$xmlWriter->writeAttributeIf($beginArrow !== null, 'startarrow', $beginArrow);
$xmlWriter->writeAttributeIf($endArrow !== null, 'endarrow', $endArrow);
$xmlWriter->writeAttributeIf($style->getWeight() !== null, 'weight', $style->getWeight() . 'pt');
$xmlWriter->writeAttributeIf($style->getColor() !== null, 'color', $style->getColor());
$xmlWriter->writeAttributeIf($style->getBeginArrow() !== null, 'startarrow', $style->getBeginArrow());
$xmlWriter->writeAttributeIf($style->getEndArrow() !== null, 'endarrow', $style->getEndArrow());
if ($dash !== null) {
if (array_key_exists($dash, $dashStyles)) {
@ -107,4 +68,3 @@ class Line extends Image
$xmlWriter->endElement(); //v:stroke
}
}
}

View File

@ -38,9 +38,10 @@ class Outline extends AbstractStyle
$xmlWriter->startElement("v:stroke");
$xmlWriter->writeAttribute('on', 't');
$xmlWriter->writeAttributeIf($style->getColor() !== null, 'color', $style->getColor());
$xmlWriter->writeAttributeIf($style->getWeight() !== null, 'weight', $style->getWeight() . 'pt');
$xmlWriter->writeAttributeIf($style->getWeight() !== null, 'weight', $style->getWeight() . $style->getUnit());
$xmlWriter->writeAttributeIf($style->getDash() !== null, 'dashstyle', $style->getDash());
$xmlWriter->writeAttributeIf($style->getLine() !== null, 'linestyle', $style->getLine());
$xmlWriter->writeAttributeIf($style->getEndCap() !== null, 'endcap', $style->getEndCap());
$xmlWriter->writeAttributeIf($style->getStartArrow() !== null, 'startarrow', $style->getStartArrow());
$xmlWriter->writeAttributeIf($style->getEndArrow() !== null, 'endarrow', $style->getEndArrow());
$xmlWriter->endElement();

View File

@ -24,67 +24,8 @@ use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
*
* @since 0.11.0
*/
class TextBox extends Image
class TextBox extends Frame
{
/**
* Write style
*/
public function write()
{
$style = $this->getStyle();
if (!$style instanceof TextBoxStyle) {
return;
}
$this->writeStyle($style);
$this->writeBorder($style);
}
/**
* Write w10 wrapping
*
* @return array
*/
public function writeW10Wrap()
{
if (is_null($this->w10wrap)) {
return;
}
$style = $this->getStyle();
if (!$style instanceof TextBoxStyle) {
return;
}
$relativePositions = array(
TextBoxStyle::POSITION_RELATIVE_TO_MARGIN => 'margin',
TextBoxStyle::POSITION_RELATIVE_TO_PAGE => 'page',
TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN => 'margin',
TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN => 'page',
TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN => 'margin',
TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN => 'page',
);
$pos = $style->getPositioning();
$vPos = $style->getPosVerticalRel();
$hPos = $style->getPosHorizontalRel();
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startElement('w10:wrap');
$xmlWriter->writeAttribute('type', $this->w10wrap);
if ($pos == TextBoxStyle::POSITION_ABSOLUTE) {
$xmlWriter->writeAttribute('anchorx', "page");
$xmlWriter->writeAttribute('anchory', "page");
} elseif ($pos == TextBoxStyle::POSITION_RELATIVE) {
if (array_key_exists($vPos, $relativePositions)) {
$xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]);
}
if (array_key_exists($hPos, $relativePositions)) {
$xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]);
}
}
$xmlWriter->endElement(); // w10:wrap
}
/**
* Writer inner margin
*/
@ -97,29 +38,24 @@ class TextBox extends Image
$xmlWriter = $this->getXmlWriter();
$margins = implode(', ', $style->getInnerMargin());
$xmlWriter->writeAttribute('inset', $margins);
}
/**
* Writer border
*/
private function writeBorder(TextBoxStyle $style)
public function writeBorder()
{
$style = $this->getStyle();
if (!$style instanceof TextBoxStyle) {
return;
}
$xmlWriter = $this->getXmlWriter();
// Border size
$borderSize = $style->getBorderSize();
if ($borderSize !== null) {
$xmlWriter->writeAttribute('strokeweight', $borderSize . 'pt');
}
// Border color
$borderColor = $style->getBorderColor();
if (empty($borderColor)) {
$xmlWriter->writeAttribute('stroked', 'f');
} else {
$xmlWriter->writeAttribute('strokecolor', $borderColor);
}
//@todo <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
$xmlWriter->startElement('v:stroke');
$xmlWriter->writeAttributeIf($style->getBorderSize() !== null, 'weight', $style->getBorderSize() . 'pt');
$xmlWriter->writeAttributeIf($style->getBorderColor() !== null, 'color', $style->getBorderColor());
$xmlWriter->endElement(); // v:stroke
}
}

View File

@ -46,6 +46,7 @@ class FontTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('text', $object->getStyleType());
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $object->getParagraphStyle());
$this->assertTrue(is_array($object->getStyleValues()));
}
/**

View File

@ -103,7 +103,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
array(
'points' => '1,1 150,30',
'outline' => array('color' => '#cc00ff', 'line' => 'thickThin', 'weight' => 3,
'startArrow' => 'oval', 'endArrow' => 'classic'),
'startArrow' => 'oval', 'endArrow' => 'classic', 'endCap' => 'round'),
)
);

View File

@ -49,8 +49,9 @@ class StyleTest extends \PHPUnit_Framework_TestCase
public function testMethodExceptions()
{
$styles = array(
'Image' => 'writeAlignment',
'Frame' => 'writeAlignment',
'Line' => 'writeStroke',
'TextBox' => 'writeBorder',
);
foreach ($styles as $style => $method) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $style;