Merge pull request #1310 from troosan/add_image_wrap_distance
add text wrapping distance
This commit is contained in:
commit
6b48451ebc
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
use PhpOffice\PhpWord\Element\Section;
|
||||
use PhpOffice\PhpWord\Shared\Converter;
|
||||
|
||||
include_once 'Sample_Header.php';
|
||||
|
||||
// New Word document
|
||||
|
|
@ -9,45 +12,48 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
|
|||
$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',
|
||||
|
|
@ -64,7 +70,7 @@ $section->addImage(
|
|||
);
|
||||
|
||||
//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(
|
||||
|
|
@ -80,6 +86,14 @@ $section->addImage(
|
|||
)
|
||||
);
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
@ -547,6 +575,98 @@ class Frame extends AbstractStyle
|
|||
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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -45,10 +45,14 @@ class Frame extends AbstractStyle
|
|||
$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());
|
||||
|
||||
|
|
@ -57,7 +61,6 @@ class Frame extends AbstractStyle
|
|||
'hPos' => 'mso-position-horizontal',
|
||||
'vPos' => 'mso-position-vertical',
|
||||
'hPosRelTo' => 'mso-position-horizontal-relative',
|
||||
'vPosRelTo' => 'mso-position-vertical-relative',
|
||||
);
|
||||
$posStyles = $this->getStyles($style, $properties);
|
||||
|
||||
|
|
|
|||
|
|
@ -115,6 +115,7 @@ class FontTest extends \PHPUnit\Framework\TestCase
|
|||
'spacing' => 240,
|
||||
'kerning' => 10,
|
||||
'rtl' => true,
|
||||
'noProof' => true,
|
||||
'lang' => new Language(Language::EN_US),
|
||||
);
|
||||
$object->setStyleByArray($attributes);
|
||||
|
|
|
|||
|
|
@ -35,12 +35,16 @@ class ImageTest extends \PHPUnit\Framework\TestCase
|
|||
$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}";
|
||||
|
|
@ -58,16 +62,21 @@ class ImageTest extends \PHPUnit\Framework\TestCase
|
|||
$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}";
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
@ -54,4 +56,25 @@ class ElementTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
<?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.
|
||||
*
|
||||
* @see https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2010-2018 PHPWord contributors
|
||||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
|
||||
|
||||
use PhpOffice\PhpWord\Style\Image;
|
||||
use PhpOffice\PhpWord\TestHelperDOCX;
|
||||
|
||||
/**
|
||||
* Test class for PhpOffice\PhpWord\Writer\Word2007\Style\Font
|
||||
*
|
||||
* @coversDefaultClass \PhpOffice\PhpWord\Writer\Word2007\Style\Frame
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class ImageTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Executed before each method of the class
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
TestHelperDOCX::clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test writing image wrapping
|
||||
*/
|
||||
public function testWrapping()
|
||||
{
|
||||
$styles = array(
|
||||
'wrap' => Image::WRAP_INLINE,
|
||||
'wrapDistanceLeft' => 10,
|
||||
'wrapDistanceRight' => 20,
|
||||
'wrapDistanceTop' => 30,
|
||||
'wrapDistanceBottom' => 40,
|
||||
);
|
||||
|
||||
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||
$section = $phpWord->addSection();
|
||||
$section->addImage(__DIR__ . '/../../../_files/images/earth.jpg', $styles);
|
||||
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
|
||||
|
||||
$path = '/w:document/w:body/w:p[1]/w:r/w:pict/v:shape';
|
||||
$this->assertTrue($doc->elementExists($path . '/w10:wrap'));
|
||||
$this->assertEquals('inline', $doc->getElementAttribute($path . '/w10:wrap', 'type'));
|
||||
|
||||
$this->assertTrue($doc->elementExists($path));
|
||||
$style = $doc->getElement($path)->getAttribute('style');
|
||||
$this->assertNotNull($style);
|
||||
$this->assertContains('mso-wrap-distance-left:10pt;', $style);
|
||||
$this->assertContains('mso-wrap-distance-right:20pt;', $style);
|
||||
$this->assertContains('mso-wrap-distance-top:30pt;', $style);
|
||||
$this->assertContains('mso-wrap-distance-bottom:40pt;', $style);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue