Нет описания

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