Merge branch 'develop' of github.com:basjan/PHPWord into #217-basjan

This commit is contained in:
Ivan Lanin 2014-05-04 18:16:33 +07:00
commit 6898ada0e0
3 changed files with 324 additions and 43 deletions

View File

@ -249,7 +249,6 @@ abstract class AbstractContainer extends AbstractElement
$rId = Media::addElement($elementDocPart, 'image', $src, $image); $rId = Media::addElement($elementDocPart, 'image', $src, $image);
$image->setRelationId($rId); $image->setRelationId($rId);
$this->addElement($image); $this->addElement($image);
return $image; return $image;
} }
@ -413,7 +412,7 @@ abstract class AbstractContainer extends AbstractElement
$docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPart = $isCellTextrun ? $this->getDocPart() : $this->container;
$docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
$docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId;
return $inHeaderFooter ? $docPart . $docPartId : $docPart; return $inHeaderFooter ? $docPart . $docPartId : $docPart;
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* PHPWord * PHPWord
* *
@ -6,7 +7,6 @@
* @copyright 2014 PHPWord * @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/lgpl.txt LGPL * @license http://www.gnu.org/licenses/lgpl.txt LGPL
*/ */
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
/** /**
@ -14,12 +14,67 @@ namespace PhpOffice\PhpWord\Style;
*/ */
class Image extends AbstractStyle class Image extends AbstractStyle
{ {
const WRAPPING_STYLE_INLINE = 'inline'; const WRAPPING_STYLE_INLINE = 'inline';
const WRAPPING_STYLE_SQUARE = 'square'; const WRAPPING_STYLE_SQUARE = 'square';
const WRAPPING_STYLE_TIGHT = 'tight'; const WRAPPING_STYLE_TIGHT = 'tight';
const WRAPPING_STYLE_BEHIND = 'behind'; const WRAPPING_STYLE_BEHIND = 'behind';
const WRAPPING_STYLE_INFRONT = 'infront'; const WRAPPING_STYLE_INFRONT = 'infront';
const POSITION_HORIZONTAL_LEFT = 'left';
const POSITION_HORIZONTAL_CENTER = 'centered';
const POSITION_HORIZONTAL_RIGHT = 'right';
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';
const POSITION_HORIZONTAL_RELATIVE_MARGIN = 'margin';
const POSITION_HORIZONTAL_RELATIVE_PAGE = 'page';
const POSITION_HORIZONTAL_RELATIVE_COLUMN = 'column';
const POSITION_HORIZONTAL_RELATIVE_CHAR = 'char';
const POSITION_HORIZONTAL_RELATIVE_LMARGIN = 'left-margin-area';
const POSITION_HORIZONTAL_RELATIVE_RMARGIN = 'right-margin-area';
const POSITION_HORIZONTAL_RELATIVE_IMARGIN = 'inner-margin-area';
const POSITION_HORIZONTAL_RELATIVE_OMARGIN = 'outer-margin-area';
const POSITION_VERTICAL_RELATIVE_MARGIN = 'margin';
const POSITION_VERTICAL_RELATIVE_PAGE = 'page';
const POSITION_VERTICAL_RELATIVE_LINE = 'line';
const POSITION_VERTICAL_RELATIVE_TMARGIN = 'top-margin-area';
const POSITION_VERTICAL_RELATIVE_BMARGIN = 'bottom-margin-area';
const POSITION_VERTICAL_RELATIVE_IMARGIN = 'inner-margin-area';
const POSITION_VERTICAL_RELATIVE_OMARGIN = 'outer-margin-area';
const POSITION_RELATIVE = 'relative';
const POSITION_ABSOLUTE = 'absolute';
/** /**
* Image width * Image width
* *
@ -62,17 +117,57 @@ class Image extends AbstractStyle
*/ */
private $wrappingStyle; private $wrappingStyle;
/**
* Horizontal alignment
*
* @var string
*/
private $posHorizontal;
/**
* Horizontal Relation
*
* @var string
*/
private $posHorizontalRel;
/**
* Vertical alignment
*
* @var string
*/
private $posVertical;
/**
* Vertical Relation
*
* @var string
*/
private $posVerticalRel;
/**
* Positioning type (Relative or Absolute)
*
* @var string
*/
private $positioning;
/** /**
* Create new image style * Create new image style
*/ */
public function __construct() public function __construct()
{ {
$this->width = null; $this->width = null;
$this->height = null; $this->height = null;
$this->align = null; $this->align = null;
$this->marginTop = null; $this->marginTop = null;
$this->marginLeft = null; $this->marginLeft = null;
$this->setWrappingStyle(self::WRAPPING_STYLE_INLINE); $this->setWrappingStyle(self::WRAPPING_STYLE_INLINE);
$this->setPositioning(self::POSITION_RELATIVE);
$this->setPosHorizontal(self::POSITION_HORIZONTAL_LEFT);
$this->setPosHorizontalRel(self::POSITION_HORIZONTAL_RELATIVE_CHAR);
$this->setPosVertical(self::POSITION_VERTICAL_TOP);
$this->setPosVerticalRel(self::POSITION_VERTICAL_RELATIVE_LINE);
} }
/** /**
@ -80,17 +175,17 @@ class Image extends AbstractStyle
*/ */
public function getWidth() public function getWidth()
{ {
return $this->width; return $this->width;
} }
/** /**
* Set width * Set width
* *
* @param int $pValue * @param int $pValue
*/ */
public function setWidth($pValue = null) public function setWidth($pValue = null)
{ {
$this->width = $pValue; $this->width = $pValue;
} }
/** /**
@ -98,17 +193,17 @@ class Image extends AbstractStyle
*/ */
public function getHeight() public function getHeight()
{ {
return $this->height; return $this->height;
} }
/** /**
* Set height * Set height
* *
* @param int $pValue * @param int $pValue
*/ */
public function setHeight($pValue = null) public function setHeight($pValue = null)
{ {
$this->height = $pValue; $this->height = $pValue;
} }
/** /**
@ -116,17 +211,17 @@ class Image extends AbstractStyle
*/ */
public function getAlign() public function getAlign()
{ {
return $this->align; return $this->align;
} }
/** /**
* Set alignment * Set alignment
* *
* @param string $pValue * @param string $pValue
*/ */
public function setAlign($pValue = null) public function setAlign($pValue = null)
{ {
$this->align = $pValue; $this->align = $pValue;
} }
/** /**
@ -136,19 +231,19 @@ class Image extends AbstractStyle
*/ */
public function getMarginTop() public function getMarginTop()
{ {
return $this->marginTop; return $this->marginTop;
} }
/** /**
* Set Margin Top * Set Margin Top
* *
* @param int $pValue * @param int $pValue
* @return $this * @return $this
*/ */
public function setMarginTop($pValue = null) public function setMarginTop($pValue = null)
{ {
$this->marginTop = $pValue; $this->marginTop = $pValue;
return $this; return $this;
} }
/** /**
@ -158,25 +253,25 @@ class Image extends AbstractStyle
*/ */
public function getMarginLeft() public function getMarginLeft()
{ {
return $this->marginLeft; return $this->marginLeft;
} }
/** /**
* Set Margin Left * Set Margin Left
* *
* @param int $pValue * @param int $pValue
* @return $this * @return $this
*/ */
public function setMarginLeft($pValue = null) public function setMarginLeft($pValue = null)
{ {
$this->marginLeft = $pValue; $this->marginLeft = $pValue;
return $this; return $this;
} }
/** /**
* Set wrapping style * Set wrapping style
* *
* @param string $wrappingStyle * @param string $wrappingStyle
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @return $this * @return $this
*/ */
@ -203,6 +298,176 @@ class Image extends AbstractStyle
*/ */
public function getWrappingStyle() public function getWrappingStyle()
{ {
return $this->wrappingStyle; return $this->wrappingStyle;
}
/**
* Set positioning type
*
* @param string $positioning
* @throws \InvalidArgumentException
* @return $this
*/
public function setPositioning($positioning)
{
switch ($positioning) {
case self::POSITION_RELATIVE:
case self::POSITION_ABSOLUTE:
$this->positioning = $positioning;
break;
default:
throw new InvalidArgumentException('Positioning does not exists');
break;
}
return $this;
}
/**
* Get positioning type
*
* @return string
*/
public function getPositioning()
{
return $this->positioning;
}
/**
* Set horizontal alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return $this
*/
public function setPosHorizontal($alignment)
{
switch ($alignment) {
case self::POSITION_HORIZONTAL_LEFT:
case self::POSITION_HORIZONTAL_CENTER:
case self::POSITION_HORIZONTAL_RIGHT:
$this->posHorizontal = $alignment;
break;
default:
throw new InvalidArgumentException('Horizontal alignment does not exists');
break;
}
return $this;
}
/**
* Get horizontal alignment
*
* @return string
*/
public function getPosHorizontal()
{
return $this->posHorizontal;
}
/**
* Set vertical alignment
*
* @param string $alignment
* @throws \InvalidArgumentException
* @return $this
*/
public function setPosVertical($alignment)
{
switch ($alignment) {
case self::POSITION_VERTICAL_TOP:
case self::POSITION_VERTICAL_CENTER:
case self::POSITION_VERTICAL_BOTTOM:
case self::POSITION_VERTICAL_INSIDE:
case self::POSITION_VERTICAL_OUTSIDE:
$this->posVertical = $alignment;
break;
default:
throw new InvalidArgumentException('Vertical alignment does not exists');
break;
}
return $this;
}
/**
* Get vertical alignment
*
* @return string
*/
public function getPosVertical()
{
return $this->posVertical;
}
/**
* Set horizontal relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return $this
*/
public function setPosHorizontalRel($relto)
{
switch ($relto) {
case self::POSITION_HORIZONTAL_RELATIVE_MARGIN:
case self::POSITION_HORIZONTAL_RELATIVE_PAGE:
case self::POSITION_HORIZONTAL_RELATIVE_COLUMN:
case self::POSITION_HORIZONTAL_RELATIVE_CHAR:
case self::POSITION_HORIZONTAL_RELATIVE_LMARGIN:
case self::POSITION_HORIZONTAL_RELATIVE_RMARGIN:
case self::POSITION_HORIZONTAL_RELATIVE_IMARGIN:
case self::POSITION_HORIZONTAL_RELATIVE_OMARGIN:
$this->posHorizontalRel = $relto;
break;
default:
throw new InvalidArgumentException('Horizontal relation does not exists');
break;
}
return $this;
}
/**
* Get horizontal relation
*
* @return string
*/
public function getPosHorizontalRel()
{
return $this->posHorizontalRel;
}
/**
* Set vertical relation
*
* @param string $relto
* @throws \InvalidArgumentException
* @return $this
*/
public function setPosVerticalRel($relto)
{
switch ($relto) {
case self::POSITION_VERTICAL_RELATIVE_MARGIN:
case self::POSITION_VERTICAL_RELATIVE_PAGE:
case self::POSITION_VERTICAL_RELATIVE_LINE:
case self::POSITION_VERTICAL_RELATIVE_TMARGIN:
case self::POSITION_VERTICAL_RELATIVE_BMARGIN:
case self::POSITION_VERTICAL_RELATIVE_IMARGIN:
case self::POSITION_VERTICAL_RELATIVE_OMARGIN:
$this->posVerticalRel = $relto;
break;
default:
throw new InvalidArgumentException('Vertical relation does not exists');
break;
}
return $this;
}
/**
* Get vertical relation
*
* @return string
*/
public function getPosVerticalRel()
{
return $this->posVerticalRel;
} }
} }

View File

@ -44,6 +44,7 @@ class Image extends Element
$marginTop = $style->getMarginTop(); $marginTop = $style->getMarginTop();
$marginLeft = $style->getMarginLeft(); $marginLeft = $style->getMarginLeft();
$wrappingStyle = $style->getWrappingStyle(); $wrappingStyle = $style->getWrappingStyle();
$positioning = $style->getPositioning();
$w10wrapType = null; $w10wrapType = null;
$imgStyle = ''; $imgStyle = '';
if (null !== $width) { if (null !== $width) {
@ -53,28 +54,44 @@ class Image extends Element
$imgStyle .= 'height:' . $height . 'px;'; $imgStyle .= 'height:' . $height . 'px;';
} }
if (null !== $marginTop) { if (null !== $marginTop) {
$imgStyle .= 'margin-top:' . $marginTop . 'in;'; $imgStyle .= 'margin-top:' . $marginTop . 'px;';
} }
if (null !== $marginLeft) { if (null !== $marginLeft) {
$imgStyle .= 'margin-left:' . $marginLeft . 'in;'; $imgStyle .= 'margin-left:' . $marginLeft . 'px;';
} }
switch ($wrappingStyle) { $imgStyle.='position:absolute;mso-width-percent:0;mso-height-percent:0;';
case ImageStyle::WRAPPING_STYLE_BEHIND: $imgStyle.='mso-width-relative:margin;mso-height-relative:margin;';
$imgStyle .= 'position:absolute;z-index:-251658752;'; switch ($positioning) {
case ImageStyle::POSITION_RELATIVE:
$imgStyle.='mso-position-horizontal:'.$style->getPosHorizontal().';';
$imgStyle.='mso-position-horizontal-relative:'.$style->getPosHorizontalRel().';';
$imgStyle.='mso-position-vertical:'.$style->getPosVertical().';';
$imgStyle.='mso-position-vertical-relative:'.$style->getPosVerticalRel().';';
$imgStyle.='margin-left:0;margin-top:0;';
break; break;
case ImageStyle::WRAPPING_STYLE_INFRONT: case ImageStyle::POSITION_ABSOLUTE:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;'; $imgStyle.='mso-position-horizontal-relative:page;';
break; $imgStyle.='mso-position-vertical-relative:page;';
case ImageStyle::WRAPPING_STYLE_SQUARE:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'square';
break;
case ImageStyle::WRAPPING_STYLE_TIGHT:
$imgStyle .= 'position:absolute;z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'tight';
break; break;
} }
switch ($wrappingStyle) {
case ImageStyle::WRAPPING_STYLE_BEHIND:
$imgStyle .= 'z-index:-251658752;';
break;
case ImageStyle::WRAPPING_STYLE_INFRONT:
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
break;
case ImageStyle::WRAPPING_STYLE_SQUARE:
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'square';
break;
case ImageStyle::WRAPPING_STYLE_TIGHT:
$imgStyle .= 'z-index:251659264;mso-position-horizontal:absolute;mso-position-vertical:absolute;';
$w10wrapType = 'tight';
break;
}
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p'); $this->xmlWriter->startElement('w:p');
if (!is_null($align)) { if (!is_null($align)) {