Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ v0.15.0 (?? ??? 2018)
- Add support for MACROBUTTON field @phryneas @troosan #1021
- Add support for Hyphenation @Trainmaster #1282 (Document: `autoHyphenation`, `consecutiveHyphenLimit`, `hyphenationZone`, `doNotHyphenateCaps`, Paragraph: `suppressAutoHyphens`)
- Added support for Floating Table Positioning (tblpPr) @anrikun #639
- Added support for Image text wrapping distance @troosan #1310

### Fixed
- Fix reading of docx default style - @troosan #1238
Expand Down
4 changes: 4 additions & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ Available Image style options:
- ``marginTop``. Top margin in inches, can be negative.
- ``width``. Width in pixels.
- ``wrappingStyle``. Wrapping style, *inline*, *square*, *tight*, *behind*, or *infront*.
- ``wrapDistanceTop``. Top text wrapping in pixels.
- ``wrapDistanceBottom``. Bottom text wrapping in pixels.
- ``wrapDistanceLeft``. Left text wrapping in pixels.
- ``wrapDistanceRight``. Right text wrapping in pixels.

.. _numbering-level-style:

Expand Down
38 changes: 26 additions & 12 deletions samples/Sample_13_Images.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Shared\Converter;

include_once 'Sample_Header.php';

// New Word document
Expand All @@ -9,45 +12,48 @@
$section = $phpWord->addSection();
$section->addText('Local image without any styles:');
$section->addImage('resources/_mars.jpg');
$section->addTextBreak(2);

printSeparator($section);
$section->addText('Local image with styles:');
$section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section->addTextBreak(2);

// Remote image
printSeparator($section);
$source = 'http://php.net/images/logos/php-med-trans-light.gif';
$section->addText("Remote image from: {$source}");
$section->addImage($source);

// Image from string
printSeparator($section);
$source = 'resources/_mars.jpg';
$fileContent = file_get_contents($source);
$section->addText('Image from string');
$section->addImage($fileContent);

//Wrapping style
$text = str_repeat('Hello World! ', 15);
printSeparator($section);
$text = str_repeat('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ', 2);
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
foreach ($wrappingStyles as $wrappingStyle) {
$section->addTextBreak(5);
$section->addText("Wrapping style {$wrappingStyle}");
$section->addImage(
'resources/_earth.jpg',
array(
'positioning' => 'relative',
'marginTop' => -1,
'marginLeft' => 1,
'width' => 80,
'height' => 80,
'wrappingStyle' => $wrappingStyle,
'positioning' => 'relative',
'marginTop' => -1,
'marginLeft' => 1,
'width' => 80,
'height' => 80,
'wrappingStyle' => $wrappingStyle,
'wrapDistanceRight' => Converter::cmToPoint(1),
'wrapDistanceBottom' => Converter::cmToPoint(1),
)
);
$section->addText($text);
printSeparator($section);
}

//Absolute positioning
$section->addTextBreak(3);
$section->addText('Absolute positioning: see top right corner of page');
$section->addImage(
'resources/_mars.jpg',
Expand All @@ -64,7 +70,7 @@
);

//Relative positioning
$section->addTextBreak(3);
printSeparator($section);
$section->addText('Relative positioning: Horizontal position center relative to column,');
$section->addText('Vertical position top relative to line');
$section->addImage(
Expand All @@ -80,6 +86,14 @@
)
);

function printSeparator(Section $section)
{
$section->addTextBreak();
$lineStyle = array('weight' => 0.2, 'width' => 150, 'height' => 0, 'align' => 'center');
$section->addLine($lineStyle);
$section->addTextBreak(2);
}

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
Expand Down
120 changes: 120 additions & 0 deletions src/PhpWord/Style/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ class Frame extends AbstractStyle
*/
private $wrap;

/**
* Top wrap distance
*
* @var float
*/
private $wrapDistanceTop;

/**
* Bottom wrap distance
*
* @var float
*/
private $wrapDistanceBottom;

/**
* Left wrap distance
*
* @var float
*/
private $wrapDistanceLeft;

/**
* Right wrap distance
*
* @var float
*/
private $wrapDistanceRight;

/**
* Vertically raised or lowered text
*
Expand Down Expand Up @@ -547,6 +575,98 @@ public function setWrap($value)
return $this;
}

/**
* Get top distance from text wrap
*
* @return float
*/
public function getWrapDistanceTop()
{
return $this->wrapDistanceTop;
}

/**
* Set top distance from text wrap
*
* @param int $value
* @return self
*/
public function setWrapDistanceTop($value = null)
{
$this->wrapDistanceTop = $this->setFloatVal($value, null);

return $this;
}

/**
* Get bottom distance from text wrap
*
* @return float
*/
public function getWrapDistanceBottom()
{
return $this->wrapDistanceBottom;
}

/**
* Set bottom distance from text wrap
*
* @param float $value
* @return self
*/
public function setWrapDistanceBottom($value = null)
{
$this->wrapDistanceBottom = $this->setFloatVal($value, null);

return $this;
}

/**
* Get left distance from text wrap
*
* @return float
*/
public function getWrapDistanceLeft()
{
return $this->wrapDistanceLeft;
}

/**
* Set left distance from text wrap
*
* @param float $value
* @return self
*/
public function setWrapDistanceLeft($value = null)
{
$this->wrapDistanceLeft = $this->setFloatVal($value, null);

return $this;
}

/**
* Get right distance from text wrap
*
* @return float
*/
public function getWrapDistanceRight()
{
return $this->wrapDistanceRight;
}

/**
* Set right distance from text wrap
*
* @param float $value
* @return self
*/
public function setWrapDistanceRight($value = null)
{
$this->wrapDistanceRight = $this->setFloatVal($value, null);

return $this;
}

/**
* Get position
*
Expand Down
13 changes: 8 additions & 5 deletions src/PhpWord/Writer/Word2007/Style/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ public function write()
$zIndices = array(FrameStyle::WRAP_INFRONT => $maxZIndex, FrameStyle::WRAP_BEHIND => -$maxZIndex);

$properties = array(
'width' => 'width',
'height' => 'height',
'left' => 'margin-left',
'top' => 'margin-top',
'width' => 'width',
'height' => 'height',
'left' => 'margin-left',
'top' => 'margin-top',
'wrapDistanceTop' => 'mso-wrap-distance-top',
'wrapDistanceBottom' => 'mso-wrap-distance-bottom',
'wrapDistanceLeft' => 'mso-wrap-distance-left',
'wrapDistanceRight' => 'mso-wrap-distance-right',
);
$sizeStyles = $this->getStyles($style, $properties, $style->getUnit());

Expand All @@ -57,7 +61,6 @@ public function write()
'hPos' => 'mso-position-horizontal',
'vPos' => 'mso-position-vertical',
'hPosRelTo' => 'mso-position-horizontal-relative',
'vPosRelTo' => 'mso-position-vertical-relative',
);
$posStyles = $this->getStyles($style, $properties);

Expand Down
1 change: 1 addition & 0 deletions tests/PhpWord/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function testSetStyleValueNormal()
'spacing' => 240,
'kerning' => 10,
'rtl' => true,
'noProof' => true,
'lang' => new Language(Language::EN_US),
);
$object->setStyleByArray($attributes);
Expand Down
41 changes: 25 additions & 16 deletions tests/PhpWord/Style/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ public function testSetGetNormal()
$object = new Image();

$properties = array(
'width' => 200,
'height' => 200,
'alignment' => Jc::START,
'marginTop' => 240,
'marginLeft' => 240,
'wrappingStyle' => 'inline',
'width' => 200,
'height' => 200,
'alignment' => Jc::START,
'marginTop' => 240,
'marginLeft' => 240,
'wrappingStyle' => 'inline',
'wrapDistanceLeft' => 10,
'wrapDistanceRight' => 20,
'wrapDistanceTop' => 30,
'wrapDistanceBottom' => 40,
);
foreach ($properties as $key => $value) {
$set = "set{$key}";
Expand All @@ -58,16 +62,21 @@ public function testSetStyleValue()
$object = new Image();

$properties = array(
'width' => 200,
'height' => 200,
'alignment' => Jc::START,
'marginTop' => 240,
'marginLeft' => 240,
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_IMARGIN,
'width' => 200,
'height' => 200,
'alignment' => Jc::START,
'marginTop' => 240,
'marginLeft' => 240,
'position' => 10,
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_IMARGIN,
'wrapDistanceLeft' => 10,
'wrapDistanceRight' => 20,
'wrapDistanceTop' => 30,
'wrapDistanceBottom' => 40,
);
foreach ($properties as $key => $value) {
$get = "get{$key}";
Expand Down
23 changes: 23 additions & 0 deletions tests/PhpWord/Writer/HTML/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace PhpOffice\PhpWord\Writer\HTML;

use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML\Element\Text;

Expand Down Expand Up @@ -54,4 +56,25 @@ public function testWriteTextElement()

$this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write());
}

/**
* Test write TrackChange
*/
public function testWriteTrackChanges()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$text = $section->addText('my dummy text');
$text->setChangeInfo(TrackChange::INSERTED, 'author name');
$text2 = $section->addText('my other text');
$text2->setTrackChange(new TrackChange(TrackChange::DELETED, 'another author', new \DateTime()));

$htmlWriter = new HTML($phpWord);
$dom = new \DOMDocument();
$dom->loadHTML($htmlWriter->getContent());
$xpath = new \DOMXpath($dom);

$this->assertTrue($xpath->query('/html/body/p[1]/ins')->length == 1);
$this->assertTrue($xpath->query('/html/body/p[2]/del')->length == 1);
}
}
Loading