Bez popisu

Frame.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Shared\XMLWriter;
  19. use PhpOffice\PhpWord\Style\Alignment as AlignmentStyle;
  20. use PhpOffice\PhpWord\Style\Frame as FrameStyle;
  21. /**
  22. * Frame style writer
  23. *
  24. * @since 0.12.0
  25. */
  26. class Frame extends AbstractStyle
  27. {
  28. /**
  29. * Write style.
  30. *
  31. * @return void
  32. */
  33. public function write()
  34. {
  35. $style = $this->getStyle();
  36. if (!$style instanceof FrameStyle) {
  37. return;
  38. }
  39. $xmlWriter = $this->getXmlWriter();
  40. $zIndices = array(FrameStyle::WRAP_INFRONT => PHP_INT_MAX, FrameStyle::WRAP_BEHIND => -PHP_INT_MAX);
  41. $properties = array(
  42. 'width' => 'width',
  43. 'height' => 'height',
  44. 'left' => 'margin-left',
  45. 'top' => 'margin-top',
  46. );
  47. $sizeStyles = $this->getStyles($style, $properties, $style->getUnit());
  48. $properties = array(
  49. 'pos' => 'position',
  50. 'hPos' => 'mso-position-horizontal',
  51. 'vPos' => 'mso-position-vertical',
  52. 'hPosRelTo' => 'mso-position-horizontal-relative',
  53. 'vPosRelTo' => 'mso-position-vertical-relative',
  54. );
  55. $posStyles = $this->getStyles($style, $properties);
  56. $styles = array_merge($sizeStyles, $posStyles);
  57. // zIndex for infront & behind wrap
  58. $wrap = $style->getWrap();
  59. if ($wrap !== null && isset($zIndices[$wrap])) {
  60. $styles['z-index'] = $zIndices[$wrap];
  61. $wrap = null;
  62. }
  63. // Style attribute
  64. $xmlWriter->writeAttribute('style', $this->assembleStyle($styles));
  65. $this->writeWrap($xmlWriter, $style, $wrap);
  66. }
  67. /**
  68. * Write alignment.
  69. *
  70. * @return void
  71. */
  72. public function writeAlignment()
  73. {
  74. $style = $this->getStyle();
  75. if (!$style instanceof FrameStyle) {
  76. return;
  77. }
  78. $xmlWriter = $this->getXmlWriter();
  79. $xmlWriter->startElement('w:pPr');
  80. $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
  81. $styleWriter->write();
  82. $xmlWriter->endElement(); // w:pPr
  83. }
  84. /**
  85. * Write alignment.
  86. *
  87. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  88. * @param \PhpOffice\PhpWord\Style\Frame $style
  89. * @param string $wrap
  90. * @return void
  91. */
  92. private function writeWrap(XMLWriter $xmlWriter, FrameStyle $style, $wrap)
  93. {
  94. if ($wrap !== null) {
  95. $xmlWriter->startElement('w10:wrap');
  96. $xmlWriter->writeAttribute('type', $wrap);
  97. $relativePositions = array(
  98. FrameStyle::POS_RELTO_MARGIN => 'margin',
  99. FrameStyle::POS_RELTO_PAGE => 'page',
  100. FrameStyle::POS_RELTO_TMARGIN => 'margin',
  101. FrameStyle::POS_RELTO_BMARGIN => 'page',
  102. FrameStyle::POS_RELTO_LMARGIN => 'margin',
  103. FrameStyle::POS_RELTO_RMARGIN => 'page',
  104. );
  105. $pos = $style->getPos();
  106. $hPos = $style->getHPosRelTo();
  107. $vPos = $style->getVPosRelTo();
  108. if ($pos == FrameStyle::POS_ABSOLUTE) {
  109. $xmlWriter->writeAttribute('anchorx', "page");
  110. $xmlWriter->writeAttribute('anchory', "page");
  111. } elseif ($pos == FrameStyle::POS_RELATIVE) {
  112. if (isset($relativePositions[$hPos])) {
  113. $xmlWriter->writeAttribute('anchorx', $relativePositions[$hPos]);
  114. }
  115. if (isset($relativePositions[$vPos])) {
  116. $xmlWriter->writeAttribute('anchory', $relativePositions[$vPos]);
  117. }
  118. }
  119. $xmlWriter->endElement(); // w10:wrap
  120. }
  121. }
  122. /**
  123. * Get style values in associative array
  124. *
  125. * @param \PhpOffice\PhpWord\Style\Frame $style
  126. * @param array $properties
  127. * @param string $suffix
  128. * @return array
  129. */
  130. private function getStyles(FrameStyle $style, $properties, $suffix = '')
  131. {
  132. $styles = array();
  133. foreach ($properties as $key => $property) {
  134. $method = "get{$key}";
  135. $value = $style->$method();
  136. if ($value !== null) {
  137. $styles[$property] = $style->$method() . $suffix;
  138. }
  139. }
  140. return $styles;
  141. }
  142. }