Paragraph.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\RTF\Style;
  18. use PhpOffice\PhpWord\SimpleType\Jc;
  19. /**
  20. * RTF paragraph style writer
  21. *
  22. * @since 0.11.0
  23. */
  24. class Paragraph extends AbstractStyle
  25. {
  26. /**
  27. * Depth of table container nested level; Primarily used for RTF writer/reader
  28. *
  29. * 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc.
  30. *
  31. * @var int
  32. */
  33. private $nestedLevel = 0;
  34. /**
  35. * Write style
  36. *
  37. * @return string
  38. */
  39. public function write()
  40. {
  41. $style = $this->getStyle();
  42. if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
  43. return '';
  44. }
  45. $alignments = array(
  46. Jc::START => '\ql',
  47. Jc::END => '\qr',
  48. Jc::CENTER => '\qc',
  49. Jc::BOTH => '\qj',
  50. );
  51. $spaceAfter = $style->getSpaceAfter();
  52. $spaceBefore = $style->getSpaceBefore();
  53. $content = '';
  54. if ($this->nestedLevel == 0) {
  55. $content .= '\pard\nowidctlpar ';
  56. }
  57. if (isset($alignments[$style->getAlignment()])) {
  58. $content .= $alignments[$style->getAlignment()];
  59. }
  60. $content .= $this->writeIndentation($style->getIndentation());
  61. $content .= $this->getValueIf($spaceBefore !== null, '\sb' . round($spaceBefore));
  62. $content .= $this->getValueIf($spaceAfter !== null, '\sa' . round($spaceAfter));
  63. $lineHeight = $style->getLineHeight();
  64. if ($lineHeight) {
  65. $lineHeightAdjusted = (int) ($lineHeight * 240);
  66. $content .= "\\sl$lineHeightAdjusted\\slmult1";
  67. }
  68. if ($style->hasPageBreakBefore()) {
  69. $content .= '\\page';
  70. }
  71. $styles = $style->getStyleValues();
  72. $content .= $this->writeTabs($styles['tabs']);
  73. return $content;
  74. }
  75. /**
  76. * Writes an \PhpOffice\PhpWord\Style\Indentation
  77. *
  78. * @param null|\PhpOffice\PhpWord\Style\Indentation $indent
  79. * @return string
  80. */
  81. private function writeIndentation($indent = null)
  82. {
  83. if (isset($indent) && $indent instanceof \PhpOffice\PhpWord\Style\Indentation) {
  84. $writer = new Indentation($indent);
  85. return $writer->write();
  86. }
  87. return '';
  88. }
  89. /**
  90. * Writes tabs
  91. *
  92. * @param \PhpOffice\PhpWord\Style\Tab[] $tabs
  93. * @return string
  94. */
  95. private function writeTabs($tabs = null)
  96. {
  97. $content = '';
  98. if (!empty($tabs)) {
  99. foreach ($tabs as $tab) {
  100. $styleWriter = new Tab($tab);
  101. $content .= $styleWriter->write();
  102. }
  103. }
  104. return $content;
  105. }
  106. /**
  107. * Set nested level.
  108. *
  109. * @param int $value
  110. */
  111. public function setNestedLevel($value)
  112. {
  113. $this->nestedLevel = $value;
  114. }
  115. }