No Description

Paragraph.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Style;
  18. use PhpOffice\PhpWord\Shared\XMLWriter;
  19. use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
  20. use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
  21. use PhpOffice\PhpWord\Style;
  22. /**
  23. * Paragraph style writer
  24. *
  25. * @since 0.10.0
  26. */
  27. class Paragraph extends AbstractStyle
  28. {
  29. /**
  30. * Without w:pPr
  31. *
  32. * @var bool
  33. */
  34. private $withoutPPR = false;
  35. /**
  36. * Is inline in element
  37. *
  38. * @var bool
  39. */
  40. private $isInline = false;
  41. /**
  42. * Write style.
  43. *
  44. * @return void
  45. */
  46. public function write()
  47. {
  48. $xmlWriter = $this->getXmlWriter();
  49. $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
  50. if ($isStyleName) {
  51. if (!$this->withoutPPR) {
  52. $xmlWriter->startElement('w:pPr');
  53. }
  54. $xmlWriter->startElement('w:pStyle');
  55. $xmlWriter->writeAttribute('w:val', $this->style);
  56. $xmlWriter->endElement();
  57. if (!$this->withoutPPR) {
  58. $xmlWriter->endElement();
  59. }
  60. } else {
  61. $this->writeStyle();
  62. }
  63. }
  64. /**
  65. * Write full style.
  66. *
  67. * @return void
  68. */
  69. private function writeStyle()
  70. {
  71. $style = $this->getStyle();
  72. if (!$style instanceof ParagraphStyle) {
  73. return;
  74. }
  75. $xmlWriter = $this->getXmlWriter();
  76. $styles = $style->getStyleValues();
  77. if (!$this->withoutPPR) {
  78. $xmlWriter->startElement('w:pPr');
  79. }
  80. // Style name
  81. if ($this->isInline === true) {
  82. $xmlWriter->writeElementIf($styles['name'] !== null, 'w:pStyle', 'w:val', $styles['name']);
  83. }
  84. // Alignment
  85. $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $styles['alignment'])));
  86. $styleWriter->write();
  87. // Pagination
  88. $xmlWriter->writeElementIf($styles['pagination']['widowControl'] === false, 'w:widowControl', 'w:val', '0');
  89. $xmlWriter->writeElementIf($styles['pagination']['keepNext'] === true, 'w:keepNext', 'w:val', '1');
  90. $xmlWriter->writeElementIf($styles['pagination']['keepLines'] === true, 'w:keepLines', 'w:val', '1');
  91. $xmlWriter->writeElementIf($styles['pagination']['pageBreak'] === true, 'w:pageBreakBefore', 'w:val', '1');
  92. // Child style: indentation, spacing, and shading
  93. $this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
  94. $this->writeChildStyle($xmlWriter, 'Spacing', $styles['spacing']);
  95. $this->writeChildStyle($xmlWriter, 'Shading', $styles['shading']);
  96. // Tabs
  97. $this->writeTabs($xmlWriter, $styles['tabs']);
  98. // Numbering
  99. $this->writeNumbering($xmlWriter, $styles['numbering']);
  100. // Border
  101. if ($style->hasBorder()) {
  102. $xmlWriter->startElement('w:pBdr');
  103. $styleWriter = new MarginBorder($xmlWriter);
  104. $styleWriter->setSizes($style->getBorderSize());
  105. $styleWriter->setColors($style->getBorderColor());
  106. $styleWriter->write();
  107. $xmlWriter->endElement();
  108. }
  109. if (!$this->withoutPPR) {
  110. $xmlWriter->endElement(); // w:pPr
  111. }
  112. }
  113. /**
  114. * Write tabs.
  115. *
  116. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  117. * @param \PhpOffice\PhpWord\Style\Tab[] $tabs
  118. * @return void
  119. */
  120. private function writeTabs(XMLWriter $xmlWriter, $tabs)
  121. {
  122. if (!empty($tabs)) {
  123. $xmlWriter->startElement("w:tabs");
  124. foreach ($tabs as $tab) {
  125. $styleWriter = new Tab($xmlWriter, $tab);
  126. $styleWriter->write();
  127. }
  128. $xmlWriter->endElement();
  129. }
  130. }
  131. /**
  132. * Write numbering.
  133. *
  134. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  135. * @param array $numbering
  136. * @return void
  137. */
  138. private function writeNumbering(XMLWriter $xmlWriter, $numbering)
  139. {
  140. $numStyle = $numbering['style'];
  141. $numLevel = $numbering['level'];
  142. /** @var \PhpOffice\PhpWord\Style\Numbering $numbering */
  143. $numbering = Style::getStyle($numStyle);
  144. if ($numStyle !== null && $numbering !== null) {
  145. $xmlWriter->startElement('w:numPr');
  146. $xmlWriter->startElement('w:numId');
  147. $xmlWriter->writeAttribute('w:val', $numbering->getIndex());
  148. $xmlWriter->endElement(); // w:numId
  149. $xmlWriter->startElement('w:ilvl');
  150. $xmlWriter->writeAttribute('w:val', $numLevel);
  151. $xmlWriter->endElement(); // w:ilvl
  152. $xmlWriter->endElement(); // w:numPr
  153. $xmlWriter->startElement('w:outlineLvl');
  154. $xmlWriter->writeAttribute('w:val', $numLevel);
  155. $xmlWriter->endElement(); // w:outlineLvl
  156. }
  157. }
  158. /**
  159. * Set without w:pPr.
  160. *
  161. * @param bool $value
  162. * @return void
  163. */
  164. public function setWithoutPPR($value)
  165. {
  166. $this->withoutPPR = $value;
  167. }
  168. /**
  169. * Set is inline.
  170. *
  171. * @param bool $value
  172. * @return void
  173. */
  174. public function setIsInline($value)
  175. {
  176. $this->isInline = $value;
  177. }
  178. }