Paragraph.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\ODText\Style;
  18. use PhpOffice\PhpWord\Shared\Converter;
  19. /**
  20. * Font style writer
  21. *
  22. * @since 0.10.0
  23. */
  24. class Paragraph extends AbstractStyle
  25. {
  26. /**
  27. * Write style.
  28. */
  29. public function write()
  30. {
  31. $style = $this->getStyle();
  32. if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
  33. return;
  34. }
  35. $xmlWriter = $this->getXmlWriter();
  36. $marginTop = $style->getSpaceBefore();
  37. $marginBottom = $style->getSpaceAfter();
  38. $xmlWriter->startElement('style:style');
  39. $styleName = $style->getStyleName();
  40. $styleAuto = false;
  41. $mpm = '';
  42. $psm = '';
  43. $pagestart = -1;
  44. $breakafter = $breakbefore = $breakauto = false;
  45. if ($style->isAuto()) {
  46. if (substr($styleName, 0, 2) === 'PB') {
  47. $styleAuto = true;
  48. $breakafter = true;
  49. } elseif (substr($styleName, 0, 2) === 'SB') {
  50. $styleAuto = true;
  51. $mpm = 'Standard' . substr($styleName, 2);
  52. $psn = $style->getNumLevel();
  53. $pagestart = $psn;
  54. } elseif (substr($styleName, 0, 2) === 'HD') {
  55. $styleAuto = true;
  56. $psm = 'Heading_' . substr($styleName, 2);
  57. $stylep = \PhpOffice\PhpWord\Style::getStyle($psm);
  58. if ($stylep instanceof \PhpOffice\PhpWord\Style\Font) {
  59. if (method_exists($stylep, 'getParagraph')) {
  60. $stylep = $stylep->getParagraph();
  61. }
  62. }
  63. if ($stylep instanceof \PhpOffice\PhpWord\Style\Paragraph) {
  64. if ($stylep->hasPageBreakBefore()) {
  65. $breakbefore = true;
  66. }
  67. }
  68. } elseif (substr($styleName, 0, 2) === 'HE') {
  69. $styleAuto = true;
  70. $psm = 'Heading_' . substr($styleName, 2);
  71. $breakauto = true;
  72. } else {
  73. $styleAuto = true;
  74. $psm = 'Normal';
  75. if (preg_match('/^P\\d+_(\\w+)$/', $styleName, $matches)) {
  76. $psm = $matches[1];
  77. }
  78. }
  79. }
  80. $xmlWriter->writeAttribute('style:name', $style->getStyleName());
  81. $xmlWriter->writeAttribute('style:family', 'paragraph');
  82. if ($styleAuto) {
  83. $xmlWriter->writeAttributeIf($psm !== '', 'style:parent-style-name', $psm);
  84. $xmlWriter->writeAttributeIf($mpm !== '', 'style:master-page-name', $mpm);
  85. }
  86. $xmlWriter->startElement('style:paragraph-properties');
  87. if ($styleAuto) {
  88. if ($breakafter) {
  89. $xmlWriter->writeAttribute('fo:break-after', 'page');
  90. $xmlWriter->writeAttribute('fo:margin-top', '0cm');
  91. $xmlWriter->writeAttribute('fo:margin-bottom', '0cm');
  92. } elseif ($breakbefore) {
  93. $xmlWriter->writeAttribute('fo:break-before', 'page');
  94. } elseif ($breakauto) {
  95. $xmlWriter->writeAttribute('fo:break-before', 'auto');
  96. }
  97. if ($pagestart > 0) {
  98. $xmlWriter->writeAttribute('style:page-number', $pagestart);
  99. }
  100. }
  101. if (!$breakafter && !$breakbefore && !$breakauto) {
  102. $twipToPoint = Converter::INCH_TO_TWIP / Converter::INCH_TO_POINT; // 20
  103. $xmlWriter->writeAttributeIf($marginTop !== null, 'fo:margin-top', ($marginTop / $twipToPoint) . 'pt');
  104. $xmlWriter->writeAttributeIf($marginBottom !== null, 'fo:margin-bottom', ($marginBottom / $twipToPoint) . 'pt');
  105. }
  106. $temp = $style->getAlignment();
  107. $xmlWriter->writeAttributeIf($temp !== '', 'fo:text-align', $temp);
  108. $temp = $style->getLineHeight();
  109. $xmlWriter->writeAttributeIf($temp !== null, 'fo:line-height', ((string) ($temp * 100) . '%'));
  110. $xmlWriter->writeAttributeIf($style->hasPageBreakBefore() === true, 'fo:break-before', 'page');
  111. $tabs = $style->getTabs();
  112. if ($tabs !== null && count($tabs) > 0) {
  113. $xmlWriter->startElement('style:tab-stops');
  114. foreach ($tabs as $tab) {
  115. $xmlWriter->startElement('style:tab-stop');
  116. $xmlWriter->writeAttribute('style:type', $tab->getType());
  117. $xmlWriter->writeAttribute('style:position', (string) ($tab->getPosition() / Converter::INCH_TO_TWIP) . 'in');
  118. $xmlWriter->endElement();
  119. }
  120. $xmlWriter->endElement();
  121. }
  122. //Right to left
  123. $xmlWriter->writeAttributeIf($style->isBidi(), 'style:writing-mode', 'rl-tb');
  124. //Indentation
  125. $indent = $style->getIndentation();
  126. //if ($indent instanceof \PhpOffice\PhpWord\Style\Indentation) {
  127. if (!empty($indent)) {
  128. $marg = $indent->getLeft();
  129. $xmlWriter->writeAttributeIf($marg !== null, 'fo:margin-left', (string) ($marg / Converter::INCH_TO_TWIP) . 'in');
  130. $marg = $indent->getRight();
  131. $xmlWriter->writeAttributeIf($marg !== null, 'fo:margin-right', (string) ($marg / Converter::INCH_TO_TWIP) . 'in');
  132. }
  133. $xmlWriter->endElement(); //style:paragraph-properties
  134. if ($styleAuto && substr($styleName, 0, 2) === 'SB') {
  135. $xmlWriter->startElement('style:text-properties');
  136. $xmlWriter->writeAttribute('text:display', 'none');
  137. $xmlWriter->endElement();
  138. }
  139. $xmlWriter->endElement(); //style:style
  140. }
  141. }