Paragraph.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Style;
  18. use PhpOffice\PhpWord\SimpleType\Jc;
  19. /**
  20. * Paragraph style HTML writer
  21. *
  22. * @since 0.10.0
  23. */
  24. class Paragraph extends AbstractStyle
  25. {
  26. /**
  27. * Write style
  28. *
  29. * @return string
  30. */
  31. public function write()
  32. {
  33. $style = $this->getStyle();
  34. if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) {
  35. return '';
  36. }
  37. $css = array();
  38. // Alignment
  39. if ('' !== $style->getAlignment()) {
  40. $textAlign = '';
  41. switch ($style->getAlignment()) {
  42. case Jc::CENTER:
  43. $textAlign = 'center';
  44. break;
  45. case Jc::END:
  46. case Jc::MEDIUM_KASHIDA:
  47. case Jc::HIGH_KASHIDA:
  48. case Jc::LOW_KASHIDA:
  49. case Jc::RIGHT:
  50. $textAlign = 'right';
  51. break;
  52. case Jc::BOTH:
  53. case Jc::DISTRIBUTE:
  54. case Jc::THAI_DISTRIBUTE:
  55. case Jc::JUSTIFY:
  56. $textAlign = 'justify';
  57. break;
  58. default: //all others, align left
  59. $textAlign = 'left';
  60. break;
  61. }
  62. $css['text-align'] = $textAlign;
  63. }
  64. // Spacing
  65. $spacing = $style->getSpace();
  66. if (!is_null($spacing)) {
  67. $before = $spacing->getBefore();
  68. $after = $spacing->getAfter();
  69. $css['margin-top'] = $this->getValueIf(!is_null($before), ($before / 20) . 'pt');
  70. $css['margin-bottom'] = $this->getValueIf(!is_null($after), ($after / 20) . 'pt');
  71. } else {
  72. $css['margin-top'] = '0';
  73. $css['margin-bottom'] = '0';
  74. }
  75. return $this->assembleCss($css);
  76. }
  77. }