Text.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\HTML\Element;
  18. use PhpOffice\PhpWord\Element\TrackChange;
  19. use PhpOffice\PhpWord\Settings;
  20. use PhpOffice\PhpWord\Style\Font;
  21. use PhpOffice\PhpWord\Style\Paragraph;
  22. use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
  23. use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
  24. /**
  25. * Text element HTML writer
  26. *
  27. * @since 0.10.0
  28. */
  29. class Text extends AbstractElement
  30. {
  31. /**
  32. * Text written after opening
  33. *
  34. * @var string
  35. */
  36. private $openingText = '';
  37. /**
  38. * Text written before closing
  39. *
  40. * @var string
  41. */
  42. private $closingText = '';
  43. /**
  44. * Opening tags
  45. *
  46. * @var string
  47. */
  48. private $openingTags = '';
  49. /**
  50. * Closing tag
  51. *
  52. * @var string
  53. */
  54. private $closingTags = '';
  55. /**
  56. * Write text
  57. *
  58. * @return string
  59. */
  60. public function write()
  61. {
  62. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  63. $element = $this->element;
  64. $this->getFontStyle();
  65. $content = '';
  66. $content .= $this->writeOpening();
  67. $content .= $this->openingText;
  68. $content .= $this->openingTags;
  69. if (Settings::isOutputEscapingEnabled()) {
  70. $content .= $this->escaper->escapeHtml($element->getText());
  71. } else {
  72. $content .= $element->getText();
  73. }
  74. $content .= $this->closingTags;
  75. $content .= $this->closingText;
  76. $content .= $this->writeClosing();
  77. return $content;
  78. }
  79. /**
  80. * Set opening text.
  81. *
  82. * @param string $value
  83. */
  84. public function setOpeningText($value)
  85. {
  86. $this->openingText = $value;
  87. }
  88. /**
  89. * Set closing text.
  90. *
  91. * @param string $value
  92. */
  93. public function setClosingText($value)
  94. {
  95. $this->closingText = $value;
  96. }
  97. /**
  98. * Write opening
  99. *
  100. * @return string
  101. */
  102. protected function writeOpening()
  103. {
  104. $content = '';
  105. if (!$this->withoutP) {
  106. $style = '';
  107. if (method_exists($this->element, 'getParagraphStyle')) {
  108. $style = $this->getParagraphStyle();
  109. }
  110. $content .= "<p{$style}>";
  111. }
  112. //open track change tag
  113. $content .= $this->writeTrackChangeOpening();
  114. return $content;
  115. }
  116. /**
  117. * Write ending
  118. *
  119. * @return string
  120. */
  121. protected function writeClosing()
  122. {
  123. $content = '';
  124. //close track change tag
  125. $content .= $this->writeTrackChangeClosing();
  126. if (!$this->withoutP) {
  127. if (Settings::isOutputEscapingEnabled()) {
  128. $content .= $this->escaper->escapeHtml($this->closingText);
  129. } else {
  130. $content .= $this->closingText;
  131. }
  132. $content .= '</p>' . PHP_EOL;
  133. }
  134. return $content;
  135. }
  136. /**
  137. * writes the track change opening tag
  138. *
  139. * @return string the HTML, an empty string if no track change information
  140. */
  141. private function writeTrackChangeOpening()
  142. {
  143. $changed = $this->element->getTrackChange();
  144. if ($changed == null) {
  145. return '';
  146. }
  147. $content = '';
  148. if (($changed->getChangeType() == TrackChange::INSERTED)) {
  149. $content .= '<ins data-phpword-prop=\'';
  150. } elseif ($changed->getChangeType() == TrackChange::DELETED) {
  151. $content .= '<del data-phpword-prop=\'';
  152. }
  153. $changedProp = array('changed' => array('author'=> $changed->getAuthor(), 'id' => $this->element->getElementId()));
  154. if ($changed->getDate() != null) {
  155. $changedProp['changed']['date'] = $changed->getDate()->format('Y-m-d\TH:i:s\Z');
  156. }
  157. $content .= json_encode($changedProp);
  158. $content .= '\' ';
  159. $content .= 'title="' . $changed->getAuthor();
  160. if ($changed->getDate() != null) {
  161. $dateUser = $changed->getDate()->format('Y-m-d H:i:s');
  162. $content .= ' - ' . $dateUser;
  163. }
  164. $content .= '">';
  165. return $content;
  166. }
  167. /**
  168. * writes the track change closing tag
  169. *
  170. * @return string the HTML, an empty string if no track change information
  171. */
  172. private function writeTrackChangeClosing()
  173. {
  174. $changed = $this->element->getTrackChange();
  175. if ($changed == null) {
  176. return '';
  177. }
  178. $content = '';
  179. if (($changed->getChangeType() == TrackChange::INSERTED)) {
  180. $content .= '</ins>';
  181. } elseif ($changed->getChangeType() == TrackChange::DELETED) {
  182. $content .= '</del>';
  183. }
  184. return $content;
  185. }
  186. /**
  187. * Write paragraph style
  188. *
  189. * @return string
  190. */
  191. private function getParagraphStyle()
  192. {
  193. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  194. $element = $this->element;
  195. $style = '';
  196. if (!method_exists($element, 'getParagraphStyle')) {
  197. return $style;
  198. }
  199. $paragraphStyle = $element->getParagraphStyle();
  200. $pStyleIsObject = ($paragraphStyle instanceof Paragraph);
  201. if ($pStyleIsObject) {
  202. $styleWriter = new ParagraphStyleWriter($paragraphStyle);
  203. $style = $styleWriter->write();
  204. } elseif (is_string($paragraphStyle)) {
  205. $style = $paragraphStyle;
  206. }
  207. if ($style) {
  208. $attribute = $pStyleIsObject ? 'style' : 'class';
  209. $style = " {$attribute}=\"{$style}\"";
  210. }
  211. return $style;
  212. }
  213. /**
  214. * Get font style.
  215. */
  216. private function getFontStyle()
  217. {
  218. /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
  219. $element = $this->element;
  220. $style = '';
  221. $fontStyle = $element->getFontStyle();
  222. $fStyleIsObject = ($fontStyle instanceof Font);
  223. if ($fStyleIsObject) {
  224. $styleWriter = new FontStyleWriter($fontStyle);
  225. $style = $styleWriter->write();
  226. } elseif (is_string($fontStyle)) {
  227. $style = $fontStyle;
  228. }
  229. if ($style) {
  230. $attribute = $fStyleIsObject ? 'style' : 'class';
  231. $this->openingTags = "<span {$attribute}=\"{$style}\">";
  232. $this->closingTags = '</span>';
  233. }
  234. }
  235. }