Keine Beschreibung

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\HTML\Element;
  18. use PhpOffice\PhpWord\Style\Font;
  19. use PhpOffice\PhpWord\Style\Paragraph;
  20. use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
  21. use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
  22. /**
  23. * Text element HTML writer
  24. *
  25. * @since 0.10.0
  26. */
  27. class Text extends AbstractElement
  28. {
  29. /**
  30. * Text written after opening
  31. *
  32. * @var string
  33. */
  34. private $openingText = '';
  35. /**
  36. * Text written before closing
  37. *
  38. * @var string
  39. */
  40. private $closingText = '';
  41. /**
  42. * Opening tags
  43. *
  44. * @var string
  45. */
  46. private $openingTags = '';
  47. /**
  48. * Closing tag
  49. *
  50. * @var string
  51. */
  52. private $closingTags = '';
  53. /**
  54. * Write text
  55. *
  56. * @return string
  57. */
  58. public function write()
  59. {
  60. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  61. $element = $this->element;
  62. $this->getFontStyle();
  63. $content = '';
  64. $content .= $this->writeOpening();
  65. $content .= $this->openingText;
  66. $content .= $this->openingTags;
  67. $content .= $element->getText();
  68. $content .= $this->closingTags;
  69. $content .= $this->closingText;
  70. $content .= $this->writeClosing();
  71. return $content;
  72. }
  73. /**
  74. * Set opening text.
  75. *
  76. * @param string $value
  77. * @return void
  78. */
  79. public function setOpeningText($value)
  80. {
  81. $this->openingText = $value;
  82. }
  83. /**
  84. * Set closing text.
  85. *
  86. * @param string $value
  87. * @return void
  88. */
  89. public function setClosingText($value)
  90. {
  91. $this->closingText = $value;
  92. }
  93. /**
  94. * Write opening
  95. *
  96. * @return string
  97. */
  98. protected function writeOpening()
  99. {
  100. $content = '';
  101. if (!$this->withoutP) {
  102. $style = '';
  103. if (method_exists($this->element, 'getParagraphStyle')) {
  104. $style = $this->getParagraphStyle();
  105. }
  106. $content .= "<p{$style}>";
  107. }
  108. return $content;
  109. }
  110. /**
  111. * Write ending
  112. *
  113. * @return string
  114. */
  115. protected function writeClosing()
  116. {
  117. $content = '';
  118. if (!$this->withoutP) {
  119. $content .= $this->closingText;
  120. $content .= "</p>" . PHP_EOL;
  121. }
  122. return $content;
  123. }
  124. /**
  125. * Write paragraph style
  126. *
  127. * @return string
  128. */
  129. private function getParagraphStyle()
  130. {
  131. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  132. $element = $this->element;
  133. $style = '';
  134. if (!method_exists($element, 'getParagraphStyle')) {
  135. return $style;
  136. }
  137. $paragraphStyle = $element->getParagraphStyle();
  138. $pStyleIsObject = ($paragraphStyle instanceof Paragraph);
  139. if ($pStyleIsObject) {
  140. $styleWriter = new ParagraphStyleWriter($paragraphStyle);
  141. $style = $styleWriter->write();
  142. }
  143. if ($style) {
  144. $attribute = $pStyleIsObject ? 'style' : 'class';
  145. $style = " {$attribute}=\"{$style}\"";
  146. }
  147. return $style;
  148. }
  149. /**
  150. * Get font style.
  151. *
  152. * @return void
  153. */
  154. private function getFontStyle()
  155. {
  156. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  157. $element = $this->element;
  158. $style = '';
  159. $fontStyle = $element->getFontStyle();
  160. $fStyleIsObject = ($fontStyle instanceof Font);
  161. if ($fStyleIsObject) {
  162. $styleWriter = new FontStyleWriter($fontStyle);
  163. $style = $styleWriter->write();
  164. }
  165. if ($style) {
  166. $attribute = $fStyleIsObject ? 'style' : 'class';
  167. $this->openingTags = "<span {$attribute}=\"{$style}\">";
  168. $this->closingTags = "</span>";
  169. }
  170. }
  171. }