Nav apraksta

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Word2007\Style;
  18. use PhpOffice\PhpWord\Style\Section as SectionStyle;
  19. /**
  20. * Section style writer
  21. *
  22. * @since 0.10.0
  23. */
  24. class Section extends AbstractStyle
  25. {
  26. /**
  27. * Write style.
  28. *
  29. * @return void
  30. */
  31. public function write()
  32. {
  33. $style = $this->getStyle();
  34. if (!$style instanceof SectionStyle) {
  35. return;
  36. }
  37. $xmlWriter = $this->getXmlWriter();
  38. // Break type
  39. $breakType = $style->getBreakType();
  40. $xmlWriter->writeElementIf(!is_null($breakType), 'w:type', 'w:val', $breakType);
  41. // Page size & orientation
  42. $xmlWriter->startElement('w:pgSz');
  43. $xmlWriter->writeAttribute('w:orient', $style->getOrientation());
  44. $xmlWriter->writeAttribute('w:w', $style->getPageSizeW());
  45. $xmlWriter->writeAttribute('w:h', $style->getPageSizeH());
  46. $xmlWriter->endElement(); // w:pgSz
  47. // Margins
  48. $margins = array(
  49. 'w:top' => array('getMarginTop', SectionStyle::DEFAULT_MARGIN),
  50. 'w:right' => array('getMarginRight', SectionStyle::DEFAULT_MARGIN),
  51. 'w:bottom' => array('getMarginBottom', SectionStyle::DEFAULT_MARGIN),
  52. 'w:left' => array('getMarginLeft', SectionStyle::DEFAULT_MARGIN),
  53. 'w:header' => array('getHeaderHeight', SectionStyle::DEFAULT_HEADER_HEIGHT),
  54. 'w:footer' => array('getFooterHeight', SectionStyle::DEFAULT_FOOTER_HEIGHT),
  55. 'w:gutter' => array('getGutter', SectionStyle::DEFAULT_GUTTER),
  56. );
  57. $xmlWriter->startElement('w:pgMar');
  58. foreach ($margins as $attribute => $value) {
  59. list($method, $default) = $value;
  60. $xmlWriter->writeAttribute($attribute, $this->convertTwip($style->$method(), $default));
  61. }
  62. $xmlWriter->endElement();
  63. // Borders
  64. if ($style->hasBorder()) {
  65. $xmlWriter->startElement('w:pgBorders');
  66. $xmlWriter->writeAttribute('w:offsetFrom', 'page');
  67. $styleWriter = new MarginBorder($xmlWriter);
  68. $styleWriter->setSizes($style->getBorderSize());
  69. $styleWriter->setColors($style->getBorderColor());
  70. $styleWriter->setAttributes(array('space' => '24'));
  71. $styleWriter->write();
  72. $xmlWriter->endElement();
  73. }
  74. // Columns
  75. $colsSpace = $style->getColsSpace();
  76. $xmlWriter->startElement('w:cols');
  77. $xmlWriter->writeAttribute('w:num', $style->getColsNum());
  78. $xmlWriter->writeAttribute('w:space', $this->convertTwip($colsSpace, SectionStyle::DEFAULT_COLUMN_SPACING));
  79. $xmlWriter->endElement();
  80. // Page numbering start
  81. $pageNum = $style->getPageNumberingStart();
  82. $xmlWriter->writeElementIf(!is_null($pageNum), 'w:pgNumType', 'w:start', $pageNum);
  83. // Line numbering
  84. $styleWriter = new LineNumbering($xmlWriter, $style->getLineNumbering());
  85. $styleWriter->write();
  86. }
  87. }