add text wrapping distance

This commit is contained in:
troosan 2018-03-11 13:27:35 +01:00
parent 77f2b16bc1
commit e29a3e7c10
7 changed files with 272 additions and 33 deletions

View File

@ -1,4 +1,7 @@
<?php <?php
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Shared\Converter;
include_once 'Sample_Header.php'; include_once 'Sample_Header.php';
// New Word document // New Word document
@ -9,45 +12,48 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(); $section = $phpWord->addSection();
$section->addText('Local image without any styles:'); $section->addText('Local image without any styles:');
$section->addImage('resources/_mars.jpg'); $section->addImage('resources/_mars.jpg');
$section->addTextBreak(2);
printSeparator($section);
$section->addText('Local image with styles:'); $section->addText('Local image with styles:');
$section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); $section->addImage('resources/_earth.jpg', array('width' => 210, 'height' => 210, 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section->addTextBreak(2);
// Remote image // Remote image
printSeparator($section);
$source = 'http://php.net/images/logos/php-med-trans-light.gif'; $source = 'http://php.net/images/logos/php-med-trans-light.gif';
$section->addText("Remote image from: {$source}"); $section->addText("Remote image from: {$source}");
$section->addImage($source); $section->addImage($source);
// Image from string // Image from string
printSeparator($section);
$source = 'resources/_mars.jpg'; $source = 'resources/_mars.jpg';
$fileContent = file_get_contents($source); $fileContent = file_get_contents($source);
$section->addText('Image from string'); $section->addText('Image from string');
$section->addImage($fileContent); $section->addImage($fileContent);
//Wrapping style //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'); $wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
foreach ($wrappingStyles as $wrappingStyle) { foreach ($wrappingStyles as $wrappingStyle) {
$section->addTextBreak(5);
$section->addText("Wrapping style {$wrappingStyle}"); $section->addText("Wrapping style {$wrappingStyle}");
$section->addImage( $section->addImage(
'resources/_earth.jpg', 'resources/_earth.jpg',
array( array(
'positioning' => 'relative', 'positioning' => 'relative',
'marginTop' => -1, 'marginTop' => -1,
'marginLeft' => 1, 'marginLeft' => 1,
'width' => 80, 'width' => 80,
'height' => 80, 'height' => 80,
'wrappingStyle' => $wrappingStyle, 'wrappingStyle' => $wrappingStyle,
'wrapDistanceRight' => Converter::cmToPoint(1),
'wrapDistanceBottom' => Converter::cmToPoint(1),
) )
); );
$section->addText($text); $section->addText($text);
printSeparator($section);
} }
//Absolute positioning //Absolute positioning
$section->addTextBreak(3);
$section->addText('Absolute positioning: see top right corner of page'); $section->addText('Absolute positioning: see top right corner of page');
$section->addImage( $section->addImage(
'resources/_mars.jpg', 'resources/_mars.jpg',
@ -64,7 +70,7 @@ $section->addImage(
); );
//Relative positioning //Relative positioning
$section->addTextBreak(3); printSeparator($section);
$section->addText('Relative positioning: Horizontal position center relative to column,'); $section->addText('Relative positioning: Horizontal position center relative to column,');
$section->addText('Vertical position top relative to line'); $section->addText('Vertical position top relative to line');
$section->addImage( $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 // Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers); echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) { if (!CLI) {

View File

@ -171,6 +171,34 @@ class Frame extends AbstractStyle
*/ */
private $wrap; 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 * Vertically raised or lowered text
* *
@ -547,6 +575,98 @@ class Frame extends AbstractStyle
return $this; 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 * Get position
* *

View File

@ -45,10 +45,14 @@ class Frame extends AbstractStyle
$zIndices = array(FrameStyle::WRAP_INFRONT => $maxZIndex, FrameStyle::WRAP_BEHIND => -$maxZIndex); $zIndices = array(FrameStyle::WRAP_INFRONT => $maxZIndex, FrameStyle::WRAP_BEHIND => -$maxZIndex);
$properties = array( $properties = array(
'width' => 'width', 'width' => 'width',
'height' => 'height', 'height' => 'height',
'left' => 'margin-left', 'left' => 'margin-left',
'top' => 'margin-top', '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()); $sizeStyles = $this->getStyles($style, $properties, $style->getUnit());
@ -57,7 +61,6 @@ class Frame extends AbstractStyle
'hPos' => 'mso-position-horizontal', 'hPos' => 'mso-position-horizontal',
'vPos' => 'mso-position-vertical', 'vPos' => 'mso-position-vertical',
'hPosRelTo' => 'mso-position-horizontal-relative', 'hPosRelTo' => 'mso-position-horizontal-relative',
'vPosRelTo' => 'mso-position-vertical-relative',
); );
$posStyles = $this->getStyles($style, $properties); $posStyles = $this->getStyles($style, $properties);

View File

@ -115,6 +115,7 @@ class FontTest extends \PHPUnit\Framework\TestCase
'spacing' => 240, 'spacing' => 240,
'kerning' => 10, 'kerning' => 10,
'rtl' => true, 'rtl' => true,
'noProof' => true,
'lang' => new Language(Language::EN_US), 'lang' => new Language(Language::EN_US),
); );
$object->setStyleByArray($attributes); $object->setStyleByArray($attributes);

View File

@ -35,12 +35,16 @@ class ImageTest extends \PHPUnit\Framework\TestCase
$object = new Image(); $object = new Image();
$properties = array( $properties = array(
'width' => 200, 'width' => 200,
'height' => 200, 'height' => 200,
'alignment' => Jc::START, 'alignment' => Jc::START,
'marginTop' => 240, 'marginTop' => 240,
'marginLeft' => 240, 'marginLeft' => 240,
'wrappingStyle' => 'inline', 'wrappingStyle' => 'inline',
'wrapDistanceLeft' => 10,
'wrapDistanceRight' => 20,
'wrapDistanceTop' => 30,
'wrapDistanceBottom' => 40,
); );
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
$set = "set{$key}"; $set = "set{$key}";
@ -58,16 +62,21 @@ class ImageTest extends \PHPUnit\Framework\TestCase
$object = new Image(); $object = new Image();
$properties = array( $properties = array(
'width' => 200, 'width' => 200,
'height' => 200, 'height' => 200,
'alignment' => Jc::START, 'alignment' => Jc::START,
'marginTop' => 240, 'marginTop' => 240,
'marginLeft' => 240, 'marginLeft' => 240,
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE, 'position' => 10,
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER, 'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP, 'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN, 'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_IMARGIN, '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) { foreach ($properties as $key => $value) {
$get = "get{$key}"; $get = "get{$key}";

View File

@ -17,7 +17,9 @@
namespace PhpOffice\PhpWord\Writer\HTML; namespace PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Element\Text as TextElement; use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\Writer\HTML; use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML\Element\Text; 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()); $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);
}
} }

View File

@ -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);
}
}