Skip to content

Commit 6b48451

Browse files
authored
Merge pull request #1310 from troosan/add_image_wrap_distance
add text wrapping distance
2 parents 8bafe00 + a0111be commit 6b48451

File tree

9 files changed

+277
-33
lines changed

9 files changed

+277
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ v0.15.0 (?? ??? 2018)
1717
- Add support for MACROBUTTON field @phryneas @troosan #1021
1818
- Add support for Hyphenation @Trainmaster #1282 (Document: `autoHyphenation`, `consecutiveHyphenLimit`, `hyphenationZone`, `doNotHyphenateCaps`, Paragraph: `suppressAutoHyphens`)
1919
- Added support for Floating Table Positioning (tblpPr) @anrikun #639
20+
- Added support for Image text wrapping distance @troosan #1310
2021

2122
### Fixed
2223
- Fix reading of docx default style - @troosan #1238

docs/styles.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ Available Image style options:
154154
- ``marginTop``. Top margin in inches, can be negative.
155155
- ``width``. Width in pixels.
156156
- ``wrappingStyle``. Wrapping style, *inline*, *square*, *tight*, *behind*, or *infront*.
157+
- ``wrapDistanceTop``. Top text wrapping in pixels.
158+
- ``wrapDistanceBottom``. Bottom text wrapping in pixels.
159+
- ``wrapDistanceLeft``. Left text wrapping in pixels.
160+
- ``wrapDistanceRight``. Right text wrapping in pixels.
157161

158162
.. _numbering-level-style:
159163

samples/Sample_13_Images.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
use PhpOffice\PhpWord\Element\Section;
3+
use PhpOffice\PhpWord\Shared\Converter;
4+
25
include_once 'Sample_Header.php';
36

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

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

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

2326
// Image from string
27+
printSeparator($section);
2428
$source = 'resources/_mars.jpg';
2529
$fileContent = file_get_contents($source);
2630
$section->addText('Image from string');
2731
$section->addImage($fileContent);
2832

2933
//Wrapping style
30-
$text = str_repeat('Hello World! ', 15);
34+
printSeparator($section);
35+
$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);
3136
$wrappingStyles = array('inline', 'behind', 'infront', 'square', 'tight');
3237
foreach ($wrappingStyles as $wrappingStyle) {
33-
$section->addTextBreak(5);
3438
$section->addText("Wrapping style {$wrappingStyle}");
3539
$section->addImage(
3640
'resources/_earth.jpg',
3741
array(
38-
'positioning' => 'relative',
39-
'marginTop' => -1,
40-
'marginLeft' => 1,
41-
'width' => 80,
42-
'height' => 80,
43-
'wrappingStyle' => $wrappingStyle,
42+
'positioning' => 'relative',
43+
'marginTop' => -1,
44+
'marginLeft' => 1,
45+
'width' => 80,
46+
'height' => 80,
47+
'wrappingStyle' => $wrappingStyle,
48+
'wrapDistanceRight' => Converter::cmToPoint(1),
49+
'wrapDistanceBottom' => Converter::cmToPoint(1),
4450
)
4551
);
4652
$section->addText($text);
53+
printSeparator($section);
4754
}
4855

4956
//Absolute positioning
50-
$section->addTextBreak(3);
5157
$section->addText('Absolute positioning: see top right corner of page');
5258
$section->addImage(
5359
'resources/_mars.jpg',
@@ -64,7 +70,7 @@
6470
);
6571

6672
//Relative positioning
67-
$section->addTextBreak(3);
73+
printSeparator($section);
6874
$section->addText('Relative positioning: Horizontal position center relative to column,');
6975
$section->addText('Vertical position top relative to line');
7076
$section->addImage(
@@ -80,6 +86,14 @@
8086
)
8187
);
8288

89+
function printSeparator(Section $section)
90+
{
91+
$section->addTextBreak();
92+
$lineStyle = array('weight' => 0.2, 'width' => 150, 'height' => 0, 'align' => 'center');
93+
$section->addLine($lineStyle);
94+
$section->addTextBreak(2);
95+
}
96+
8397
// Save file
8498
echo write($phpWord, basename(__FILE__, '.php'), $writers);
8599
if (!CLI) {

src/PhpWord/Style/Frame.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,34 @@ class Frame extends AbstractStyle
171171
*/
172172
private $wrap;
173173

174+
/**
175+
* Top wrap distance
176+
*
177+
* @var float
178+
*/
179+
private $wrapDistanceTop;
180+
181+
/**
182+
* Bottom wrap distance
183+
*
184+
* @var float
185+
*/
186+
private $wrapDistanceBottom;
187+
188+
/**
189+
* Left wrap distance
190+
*
191+
* @var float
192+
*/
193+
private $wrapDistanceLeft;
194+
195+
/**
196+
* Right wrap distance
197+
*
198+
* @var float
199+
*/
200+
private $wrapDistanceRight;
201+
174202
/**
175203
* Vertically raised or lowered text
176204
*
@@ -547,6 +575,98 @@ public function setWrap($value)
547575
return $this;
548576
}
549577

578+
/**
579+
* Get top distance from text wrap
580+
*
581+
* @return float
582+
*/
583+
public function getWrapDistanceTop()
584+
{
585+
return $this->wrapDistanceTop;
586+
}
587+
588+
/**
589+
* Set top distance from text wrap
590+
*
591+
* @param int $value
592+
* @return self
593+
*/
594+
public function setWrapDistanceTop($value = null)
595+
{
596+
$this->wrapDistanceTop = $this->setFloatVal($value, null);
597+
598+
return $this;
599+
}
600+
601+
/**
602+
* Get bottom distance from text wrap
603+
*
604+
* @return float
605+
*/
606+
public function getWrapDistanceBottom()
607+
{
608+
return $this->wrapDistanceBottom;
609+
}
610+
611+
/**
612+
* Set bottom distance from text wrap
613+
*
614+
* @param float $value
615+
* @return self
616+
*/
617+
public function setWrapDistanceBottom($value = null)
618+
{
619+
$this->wrapDistanceBottom = $this->setFloatVal($value, null);
620+
621+
return $this;
622+
}
623+
624+
/**
625+
* Get left distance from text wrap
626+
*
627+
* @return float
628+
*/
629+
public function getWrapDistanceLeft()
630+
{
631+
return $this->wrapDistanceLeft;
632+
}
633+
634+
/**
635+
* Set left distance from text wrap
636+
*
637+
* @param float $value
638+
* @return self
639+
*/
640+
public function setWrapDistanceLeft($value = null)
641+
{
642+
$this->wrapDistanceLeft = $this->setFloatVal($value, null);
643+
644+
return $this;
645+
}
646+
647+
/**
648+
* Get right distance from text wrap
649+
*
650+
* @return float
651+
*/
652+
public function getWrapDistanceRight()
653+
{
654+
return $this->wrapDistanceRight;
655+
}
656+
657+
/**
658+
* Set right distance from text wrap
659+
*
660+
* @param float $value
661+
* @return self
662+
*/
663+
public function setWrapDistanceRight($value = null)
664+
{
665+
$this->wrapDistanceRight = $this->setFloatVal($value, null);
666+
667+
return $this;
668+
}
669+
550670
/**
551671
* Get position
552672
*

src/PhpWord/Writer/Word2007/Style/Frame.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ public function write()
4545
$zIndices = array(FrameStyle::WRAP_INFRONT => $maxZIndex, FrameStyle::WRAP_BEHIND => -$maxZIndex);
4646

4747
$properties = array(
48-
'width' => 'width',
49-
'height' => 'height',
50-
'left' => 'margin-left',
51-
'top' => 'margin-top',
48+
'width' => 'width',
49+
'height' => 'height',
50+
'left' => 'margin-left',
51+
'top' => 'margin-top',
52+
'wrapDistanceTop' => 'mso-wrap-distance-top',
53+
'wrapDistanceBottom' => 'mso-wrap-distance-bottom',
54+
'wrapDistanceLeft' => 'mso-wrap-distance-left',
55+
'wrapDistanceRight' => 'mso-wrap-distance-right',
5256
);
5357
$sizeStyles = $this->getStyles($style, $properties, $style->getUnit());
5458

@@ -57,7 +61,6 @@ public function write()
5761
'hPos' => 'mso-position-horizontal',
5862
'vPos' => 'mso-position-vertical',
5963
'hPosRelTo' => 'mso-position-horizontal-relative',
60-
'vPosRelTo' => 'mso-position-vertical-relative',
6164
);
6265
$posStyles = $this->getStyles($style, $properties);
6366

tests/PhpWord/Style/FontTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function testSetStyleValueNormal()
115115
'spacing' => 240,
116116
'kerning' => 10,
117117
'rtl' => true,
118+
'noProof' => true,
118119
'lang' => new Language(Language::EN_US),
119120
);
120121
$object->setStyleByArray($attributes);

tests/PhpWord/Style/ImageTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ public function testSetGetNormal()
3535
$object = new Image();
3636

3737
$properties = array(
38-
'width' => 200,
39-
'height' => 200,
40-
'alignment' => Jc::START,
41-
'marginTop' => 240,
42-
'marginLeft' => 240,
43-
'wrappingStyle' => 'inline',
38+
'width' => 200,
39+
'height' => 200,
40+
'alignment' => Jc::START,
41+
'marginTop' => 240,
42+
'marginLeft' => 240,
43+
'wrappingStyle' => 'inline',
44+
'wrapDistanceLeft' => 10,
45+
'wrapDistanceRight' => 20,
46+
'wrapDistanceTop' => 30,
47+
'wrapDistanceBottom' => 40,
4448
);
4549
foreach ($properties as $key => $value) {
4650
$set = "set{$key}";
@@ -58,16 +62,21 @@ public function testSetStyleValue()
5862
$object = new Image();
5963

6064
$properties = array(
61-
'width' => 200,
62-
'height' => 200,
63-
'alignment' => Jc::START,
64-
'marginTop' => 240,
65-
'marginLeft' => 240,
66-
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
67-
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
68-
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
69-
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
70-
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_IMARGIN,
65+
'width' => 200,
66+
'height' => 200,
67+
'alignment' => Jc::START,
68+
'marginTop' => 240,
69+
'marginLeft' => 240,
70+
'position' => 10,
71+
'positioning' => \PhpOffice\PhpWord\Style\Image::POSITION_ABSOLUTE,
72+
'posHorizontal' => \PhpOffice\PhpWord\Style\Image::POSITION_HORIZONTAL_CENTER,
73+
'posVertical' => \PhpOffice\PhpWord\Style\Image::POSITION_VERTICAL_TOP,
74+
'posHorizontalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_COLUMN,
75+
'posVerticalRel' => \PhpOffice\PhpWord\Style\Image::POSITION_RELATIVE_TO_IMARGIN,
76+
'wrapDistanceLeft' => 10,
77+
'wrapDistanceRight' => 20,
78+
'wrapDistanceTop' => 30,
79+
'wrapDistanceBottom' => 40,
7180
);
7281
foreach ($properties as $key => $value) {
7382
$get = "get{$key}";

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace PhpOffice\PhpWord\Writer\HTML;
1919

2020
use PhpOffice\PhpWord\Element\Text as TextElement;
21+
use PhpOffice\PhpWord\Element\TrackChange;
22+
use PhpOffice\PhpWord\PhpWord;
2123
use PhpOffice\PhpWord\Writer\HTML;
2224
use PhpOffice\PhpWord\Writer\HTML\Element\Text;
2325

@@ -54,4 +56,25 @@ public function testWriteTextElement()
5456

5557
$this->assertEquals(htmlspecialchars('-A-', ENT_COMPAT, 'UTF-8'), $object->write());
5658
}
59+
60+
/**
61+
* Test write TrackChange
62+
*/
63+
public function testWriteTrackChanges()
64+
{
65+
$phpWord = new PhpWord();
66+
$section = $phpWord->addSection();
67+
$text = $section->addText('my dummy text');
68+
$text->setChangeInfo(TrackChange::INSERTED, 'author name');
69+
$text2 = $section->addText('my other text');
70+
$text2->setTrackChange(new TrackChange(TrackChange::DELETED, 'another author', new \DateTime()));
71+
72+
$htmlWriter = new HTML($phpWord);
73+
$dom = new \DOMDocument();
74+
$dom->loadHTML($htmlWriter->getContent());
75+
$xpath = new \DOMXpath($dom);
76+
77+
$this->assertTrue($xpath->query('/html/body/p[1]/ins')->length == 1);
78+
$this->assertTrue($xpath->query('/html/body/p[2]/del')->length == 1);
79+
}
5780
}

0 commit comments

Comments
 (0)