AbstractElement.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\Element;
  18. use PhpOffice\PhpWord\Element\AbstractElement as Element;
  19. use PhpOffice\PhpWord\Escaper\Rtf;
  20. use PhpOffice\PhpWord\Settings;
  21. use PhpOffice\PhpWord\Shared\Text as SharedText;
  22. use PhpOffice\PhpWord\Style;
  23. use PhpOffice\PhpWord\Style\Font as FontStyle;
  24. use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
  25. use PhpOffice\PhpWord\Writer\AbstractWriter;
  26. use PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement as HTMLAbstractElement;
  27. use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
  28. use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter;
  29. /**
  30. * Abstract RTF element writer
  31. *
  32. * @since 0.11.0
  33. */
  34. abstract class AbstractElement extends HTMLAbstractElement
  35. {
  36. /**
  37. * Font style
  38. *
  39. * @var \PhpOffice\PhpWord\Style\Font
  40. */
  41. protected $fontStyle;
  42. /**
  43. * Paragraph style
  44. *
  45. * @var \PhpOffice\PhpWord\Style\Paragraph
  46. */
  47. protected $paragraphStyle;
  48. public function __construct(AbstractWriter $parentWriter, Element $element, $withoutP = false)
  49. {
  50. parent::__construct($parentWriter, $element, $withoutP);
  51. $this->escaper = new Rtf();
  52. }
  53. /**
  54. * Get font and paragraph styles.
  55. */
  56. protected function getStyles()
  57. {
  58. /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */
  59. $parentWriter = $this->parentWriter;
  60. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  61. $element = $this->element;
  62. // Font style
  63. if (method_exists($element, 'getFontStyle')) {
  64. $this->fontStyle = $element->getFontStyle();
  65. if (is_string($this->fontStyle)) {
  66. $this->fontStyle = Style::getStyle($this->fontStyle);
  67. }
  68. }
  69. // Paragraph style
  70. if (method_exists($element, 'getParagraphStyle')) {
  71. $this->paragraphStyle = $element->getParagraphStyle();
  72. if (is_string($this->paragraphStyle)) {
  73. $this->paragraphStyle = Style::getStyle($this->paragraphStyle);
  74. }
  75. if ($this->paragraphStyle !== null && !$this->withoutP) {
  76. if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) {
  77. $parentWriter->setLastParagraphStyle($element->getParagraphStyle());
  78. } else {
  79. $parentWriter->setLastParagraphStyle();
  80. $this->paragraphStyle = null;
  81. }
  82. } else {
  83. $parentWriter->setLastParagraphStyle();
  84. $this->paragraphStyle = null;
  85. }
  86. }
  87. }
  88. /**
  89. * Write opening
  90. *
  91. * @return string
  92. */
  93. protected function writeOpening()
  94. {
  95. if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) {
  96. return '';
  97. }
  98. $styleWriter = new ParagraphStyleWriter($this->paragraphStyle);
  99. $styleWriter->setNestedLevel($this->element->getNestedLevel());
  100. return $styleWriter->write();
  101. }
  102. /**
  103. * Write text
  104. *
  105. * @param string $text
  106. * @return string
  107. */
  108. protected function writeText($text)
  109. {
  110. if (Settings::isOutputEscapingEnabled()) {
  111. return $this->escaper->escape($text);
  112. }
  113. return SharedText::toUnicode($text); // todo: replace with `return $text;` later.
  114. }
  115. /**
  116. * Write closing
  117. *
  118. * @return string
  119. */
  120. protected function writeClosing()
  121. {
  122. if ($this->withoutP) {
  123. return '';
  124. }
  125. return '\par' . PHP_EOL;
  126. }
  127. /**
  128. * Write font style
  129. *
  130. * @return string
  131. */
  132. protected function writeFontStyle()
  133. {
  134. if (!$this->fontStyle instanceof FontStyle) {
  135. return '';
  136. }
  137. /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */
  138. $parentWriter = $this->parentWriter;
  139. // Create style writer and set color/name index
  140. $styleWriter = new FontStyleWriter($this->fontStyle);
  141. if ($this->fontStyle->getColor() != null) {
  142. $colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable());
  143. if ($colorIndex !== false) {
  144. $styleWriter->setColorIndex($colorIndex + 1);
  145. }
  146. }
  147. if ($this->fontStyle->getName() != null) {
  148. $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable());
  149. if ($fontIndex !== false) {
  150. $styleWriter->setNameIndex($fontIndex);
  151. }
  152. }
  153. // Write style
  154. $content = $styleWriter->write();
  155. return $content;
  156. }
  157. }