Skip to content

Commit ce49213

Browse files
authored
Merge pull request #1088 from troosan/add_support_for_contextual_spacing
Add support for contextual spacing
2 parents fd418e0 + 0f649f3 commit ce49213

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
7777
- ``spaceAfter``. Space after paragraph.
7878
- ``tabs``. Set of custom tab stops.
7979
- ``widowControl``. Allow first/last line to display on a separate page, *true* or *false*.
80+
- ``contextualSpacing``. Ignore Spacing Above and Below When Using Identical Styles, *true* or *false*.
8081

8182
.. _table-style:
8283

src/PhpWord/Style/Paragraph.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ class Paragraph extends Border
157157
* @var \PhpOffice\PhpWord\Style\Shading
158158
*/
159159
private $shading;
160+
161+
/**
162+
* Ignore Spacing Above and Below When Using Identical Styles
163+
*
164+
* @var bool
165+
*/
166+
private $contextualSpacing = false;
160167

161168
/**
162169
* Set Style value
@@ -208,6 +215,7 @@ public function getStyleValues()
208215
),
209216
'tabs' => $this->getTabs(),
210217
'shading' => $this->getShading(),
218+
'contextualSpacing' => $this->hasContextualSpacing(),
211219
);
212220

213221
return $styles;
@@ -731,4 +739,27 @@ public function setShading($value = null)
731739

732740
return $this;
733741
}
742+
743+
/**
744+
* Get contextualSpacing
745+
*
746+
* @return bool
747+
*/
748+
public function hasContextualSpacing()
749+
{
750+
return $this->contextualSpacing;
751+
}
752+
753+
/**
754+
* Set contextualSpacing
755+
*
756+
* @param bool $contextualSpacing
757+
* @return self
758+
*/
759+
public function setContextualSpacing($contextualSpacing)
760+
{
761+
$this->contextualSpacing = $contextualSpacing;
762+
763+
return $this;
764+
}
734765
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ private function writeStyle()
106106
}
107107
$xmlWriter->endElement();
108108
}
109+
110+
//Paragraph contextualSpacing
111+
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');
109112

110113
// Child style: alignment, indentation, spacing, and shading
111114
$this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);

tests/PhpWord/Style/ParagraphTest.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public function testSetStyleValueWithNullOrEmpty()
4343
$object = new Paragraph();
4444

4545
$attributes = array(
46-
'widowControl' => true,
47-
'keepNext' => false,
48-
'keepLines' => false,
49-
'pageBreakBefore' => false,
46+
'widowControl' => true,
47+
'keepNext' => false,
48+
'keepLines' => false,
49+
'pageBreakBefore' => false,
50+
'contextualSpacing' => false,
5051
);
5152
foreach ($attributes as $key => $default) {
52-
$get = "get{$key}";
53+
$get = $this->findGetter($key, $default, $object);
5354
$object->setStyleValue($key, null);
5455
$this->assertEquals($default, $object->$get());
5556
$object->setStyleValue($key, '');
@@ -65,22 +66,23 @@ public function testSetStyleValueNormal()
6566
$object = new Paragraph();
6667

6768
$attributes = array(
68-
'spaceAfter' => 240,
69-
'spaceBefore' => 240,
70-
'indent' => 1,
71-
'hanging' => 1,
72-
'spacing' => 120,
73-
'basedOn' => 'Normal',
74-
'next' => 'Normal',
75-
'numStyle' => 'numStyle',
76-
'numLevel' => 1,
77-
'widowControl' => false,
78-
'keepNext' => true,
79-
'keepLines' => true,
80-
'pageBreakBefore' => true,
69+
'spaceAfter' => 240,
70+
'spaceBefore' => 240,
71+
'indent' => 1,
72+
'hanging' => 1,
73+
'spacing' => 120,
74+
'basedOn' => 'Normal',
75+
'next' => 'Normal',
76+
'numStyle' => 'numStyle',
77+
'numLevel' => 1,
78+
'widowControl' => false,
79+
'keepNext' => true,
80+
'keepLines' => true,
81+
'pageBreakBefore' => true,
82+
'contextualSpacing' => true,
8183
);
8284
foreach ($attributes as $key => $value) {
83-
$get = "get{$key}";
85+
$get = $this->findGetter($key, $value, $object);
8486
$object->setStyleValue("$key", $value);
8587
if ('indent' == $key || 'hanging' == $key) {
8688
$value = $value * 720;
@@ -91,6 +93,18 @@ public function testSetStyleValueNormal()
9193
}
9294
}
9395

96+
private function findGetter($key, $value, $object)
97+
{
98+
if (is_bool($value)) {
99+
if (method_exists($object, "is{$key}")) {
100+
return "is{$key}";
101+
} else if (method_exists($object, "has{$key}")) {
102+
return "has{$key}";
103+
}
104+
}
105+
return "get{$key}";
106+
}
107+
94108
/**
95109
* Test get null style value
96110
*/
@@ -100,7 +114,7 @@ public function testGetNullStyleValue()
100114

101115
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
102116
foreach ($attributes as $key) {
103-
$get = "get{$key}";
117+
$get = $this->findGetter($key, null, $object);
104118
$this->assertNull($object->$get());
105119
}
106120
}

0 commit comments

Comments
 (0)