Title.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  19. * Title element RTF writer; extends from text
  20. *
  21. * @since 0.11.0
  22. */
  23. class Title extends Text
  24. {
  25. protected function getStyles()
  26. {
  27. /** @var \PhpOffice\PhpWord\Element\Title $element Type hint */
  28. $element = $this->element;
  29. $style = $element->getStyle();
  30. $style = str_replace('Heading', 'Heading_', $style);
  31. $style = \PhpOffice\PhpWord\Style::getStyle($style);
  32. if ($style instanceof \PhpOffice\PhpWord\Style\Font) {
  33. $this->fontStyle = $style;
  34. $pstyle = $style->getParagraph();
  35. if ($pstyle instanceof \PhpOffice\PhpWord\Style\Paragraph && $pstyle->hasPageBreakBefore()) {
  36. $sect = $element->getParent();
  37. if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
  38. $elems = $sect->getElements();
  39. if ($elems[0] === $element) {
  40. $pstyle = clone $pstyle;
  41. $pstyle->setPageBreakBefore(false);
  42. }
  43. }
  44. }
  45. $this->paragraphStyle = $pstyle;
  46. }
  47. }
  48. /**
  49. * Write element
  50. *
  51. * @return string
  52. */
  53. public function write()
  54. {
  55. /** @var \PhpOffice\PhpWord\Element\Title $element Type hint */
  56. $element = $this->element;
  57. $elementClass = str_replace('\\Writer\\RTF', '', get_class($this));
  58. if (!$element instanceof $elementClass || !is_string($element->getText())) {
  59. return '';
  60. }
  61. $this->getStyles();
  62. $content = '';
  63. $content .= $this->writeOpening();
  64. $endout = '';
  65. $style = $element->getStyle();
  66. if (is_string($style)) {
  67. $style = str_replace('Heading', '', $style);
  68. if (is_numeric($style)) {
  69. $style = (int) $style - 1;
  70. if ($style >= 0 && $style <= 8) {
  71. $content .= '{\\outlinelevel' . $style;
  72. $endout = '}';
  73. }
  74. }
  75. }
  76. $content .= '{';
  77. $content .= $this->writeFontStyle();
  78. $content .= $this->writeText($element->getText());
  79. $content .= '}';
  80. $content .= $this->writeClosing();
  81. $content .= $endout;
  82. return $content;
  83. }
  84. }