暂无描述

Text.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\ODText\Element;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. /**
  20. * Text element writer
  21. *
  22. * @since 0.10.0
  23. */
  24. class Text extends AbstractElement
  25. {
  26. /**
  27. * Write element
  28. */
  29. public function write()
  30. {
  31. $xmlWriter = $this->getXmlWriter();
  32. $element = $this->getElement();
  33. if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
  34. return;
  35. }
  36. $fontStyle = $element->getFontStyle();
  37. $paragraphStyle = $element->getParagraphStyle();
  38. // @todo Commented for TextRun. Should really checkout this value
  39. // $fStyleIsObject = ($fontStyle instanceof Font) ? true : false;
  40. $fStyleIsObject = false;
  41. if ($fStyleIsObject) {
  42. // Don't never be the case, because I browse all sections for cleaning all styles not declared
  43. throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
  44. } else {
  45. if (!$this->withoutP) {
  46. $xmlWriter->startElement('text:p'); // text:p
  47. }
  48. if (empty($fontStyle)) {
  49. if (empty($paragraphStyle)) {
  50. $xmlWriter->writeAttribute('text:style-name', 'P1');
  51. } elseif (is_string($paragraphStyle)) {
  52. $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
  53. }
  54. $xmlWriter->writeRaw($element->getText());
  55. } else {
  56. if (empty($paragraphStyle)) {
  57. $xmlWriter->writeAttribute('text:style-name', 'Standard');
  58. } elseif (is_string($paragraphStyle)) {
  59. $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
  60. }
  61. // text:span
  62. $xmlWriter->startElement('text:span');
  63. if (is_string($fontStyle)) {
  64. $xmlWriter->writeAttribute('text:style-name', $fontStyle);
  65. }
  66. $xmlWriter->writeRaw($element->getText());
  67. $xmlWriter->endElement();
  68. }
  69. if (!$this->withoutP) {
  70. $xmlWriter->endElement(); // text:p
  71. }
  72. }
  73. }
  74. }