Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Element;
  18. use PhpOffice\PhpWord\Element\Shape as ShapeElement;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. use PhpOffice\PhpWord\Style\Shape as ShapeStyle;
  21. use PhpOffice\PhpWord\Writer\Word2007\Style\Shape as ShapeStyleWriter;
  22. /**
  23. * Shape element writer
  24. *
  25. * @since 0.12.0
  26. * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
  27. */
  28. class Shape extends AbstractElement
  29. {
  30. /**
  31. * Write element.
  32. *
  33. * @return void
  34. */
  35. public function write()
  36. {
  37. $xmlWriter = $this->getXmlWriter();
  38. $element = $this->getElement();
  39. if (!$element instanceof ShapeElement) {
  40. return;
  41. }
  42. $style = $element->getStyle();
  43. $styleWriter = new ShapeStyleWriter($xmlWriter, $style);
  44. $type = $element->getType();
  45. if ($type == 'rect' && $style->getRoundness() !== null) {
  46. $type = 'roundrect';
  47. }
  48. $method = "write{$type}";
  49. if (!$this->withoutP) {
  50. $xmlWriter->startElement('w:p');
  51. }
  52. $xmlWriter->startElement('w:r');
  53. $xmlWriter->startElement('w:pict');
  54. $xmlWriter->startElement("v:{$type}");
  55. // Element style
  56. if (method_exists($this, $method)) {
  57. $this->$method($xmlWriter, $style);
  58. }
  59. // Child style
  60. $styleWriter->write();
  61. $xmlWriter->endElement(); // v:$type
  62. $xmlWriter->endElement(); // w:pict
  63. $xmlWriter->endElement(); // w:r
  64. $this->endElementP(); // w:p
  65. }
  66. /**
  67. * Write arc.
  68. *
  69. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  70. * @param \PhpOffice\PhpWord\Style\Shape $style
  71. * @return void
  72. */
  73. private function writeArc(XMLWriter $xmlWriter, ShapeStyle $style)
  74. {
  75. $points = $this->getPoints('arc', $style->getPoints());
  76. $xmlWriter->writeAttributeIf($points['start'] !== null, 'startAngle', $points['start']);
  77. $xmlWriter->writeAttributeIf($points['end'] !== null, 'endAngle', $points['end']);
  78. }
  79. /**
  80. * Write curve.
  81. *
  82. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  83. * @param \PhpOffice\PhpWord\Style\Shape $style
  84. * @return void
  85. */
  86. private function writeCurve(XMLWriter $xmlWriter, ShapeStyle $style)
  87. {
  88. $points = $this->getPoints('curve', $style->getPoints());
  89. $this->writeLine($xmlWriter, $style);
  90. $xmlWriter->writeAttributeIf($points['point1'] !== null, 'control1', $points['point1']);
  91. $xmlWriter->writeAttributeIf($points['point2'] !== null, 'control2', $points['point2']);
  92. }
  93. /**
  94. * Write line.
  95. *
  96. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  97. * @param \PhpOffice\PhpWord\Style\Shape $style
  98. * @return void
  99. */
  100. private function writeLine(XMLWriter $xmlWriter, ShapeStyle $style)
  101. {
  102. $points = $this->getPoints('line', $style->getPoints());
  103. $xmlWriter->writeAttributeIf($points['start'] !== null, 'from', $points['start']);
  104. $xmlWriter->writeAttributeIf($points['end'] !== null, 'to', $points['end']);
  105. }
  106. /**
  107. * Write polyline.
  108. *
  109. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  110. * @param \PhpOffice\PhpWord\Style\Shape $style
  111. * @return void
  112. */
  113. private function writePolyline(XMLWriter $xmlWriter, ShapeStyle $style)
  114. {
  115. $xmlWriter->writeAttributeIf($style->getPoints() !== null, 'points', $style->getPoints());
  116. }
  117. /**
  118. * Write rectangle.
  119. *
  120. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  121. * @param \PhpOffice\PhpWord\Style\Shape $style
  122. * @return void
  123. */
  124. private function writeRoundRect(XMLWriter $xmlWriter, ShapeStyle $style)
  125. {
  126. $xmlWriter->writeAttribute('arcsize', $style->getRoundness());
  127. }
  128. /**
  129. * Set points
  130. *
  131. * @param string $type
  132. * @param string $value
  133. * @return array
  134. */
  135. private function getPoints($type, $value)
  136. {
  137. $points = array();
  138. switch ($type) {
  139. case 'arc':
  140. case 'line':
  141. $points = explode(' ', $value);
  142. @list($start, $end) = $points;
  143. $points = array('start' => $start, 'end' => $end);
  144. break;
  145. case 'curve':
  146. $points = explode(' ', $value);
  147. @list($start, $end, $point1, $point2) = $points;
  148. $points = array('start' => $start, 'end' => $end, 'point1' => $point1, 'point2' => $point2);
  149. break;
  150. }
  151. return $points;
  152. }
  153. }