No Description

Table.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\Table as TableStyle;
  21. /**
  22. * Table style writer
  23. *
  24. * @since 0.10.0
  25. */
  26. class Table extends AbstractStyle
  27. {
  28. /**
  29. * @var int Table width
  30. */
  31. private $width;
  32. /**
  33. * Write style.
  34. *
  35. * @return void
  36. */
  37. public function write()
  38. {
  39. $style = $this->getStyle();
  40. $xmlWriter = $this->getXmlWriter();
  41. if ($style instanceof TableStyle) {
  42. $this->writeStyle($xmlWriter, $style);
  43. } elseif (is_string($style)) {
  44. $xmlWriter->startElement('w:tblPr');
  45. $xmlWriter->startElement('w:tblStyle');
  46. $xmlWriter->writeAttribute('w:val', $style);
  47. $xmlWriter->endElement();
  48. if ($this->width !== null) {
  49. $this->writeWidth($xmlWriter, $this->width, 'pct');
  50. }
  51. $xmlWriter->endElement();
  52. }
  53. }
  54. /**
  55. * Write full style.
  56. *
  57. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  58. * @param \PhpOffice\PhpWord\Style\Table $style
  59. * @return void
  60. */
  61. private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
  62. {
  63. // w:tblPr
  64. $xmlWriter->startElement('w:tblPr');
  65. // Alignment
  66. $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign())));
  67. $styleWriter->write();
  68. $this->writeWidth($xmlWriter, $style->getWidth(), $style->getUnit());
  69. $this->writeMargin($xmlWriter, $style);
  70. $this->writeBorder($xmlWriter, $style);
  71. $xmlWriter->endElement(); // w:tblPr
  72. $this->writeShading($xmlWriter, $style);
  73. // First row style
  74. $firstRow = $style->getFirstRow();
  75. if ($firstRow instanceof TableStyle) {
  76. $this->writeFirstRow($xmlWriter, $firstRow);
  77. }
  78. }
  79. /**
  80. * Write width.
  81. *
  82. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  83. * @param int $width
  84. * @param string $unit
  85. * @return void
  86. */
  87. private function writeWidth(XMLWriter $xmlWriter, $width, $unit)
  88. {
  89. $xmlWriter->startElement('w:tblW');
  90. $xmlWriter->writeAttribute('w:w', $width);
  91. $xmlWriter->writeAttribute('w:type', $unit);
  92. $xmlWriter->endElement(); // w:tblW
  93. }
  94. /**
  95. * Write margin.
  96. *
  97. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  98. * @param \PhpOffice\PhpWord\Style\Table $style
  99. * @return void
  100. */
  101. private function writeMargin(XMLWriter $xmlWriter, TableStyle $style)
  102. {
  103. if ($style->hasMargin()) {
  104. $xmlWriter->startElement('w:tblCellMar');
  105. $styleWriter = new MarginBorder($xmlWriter);
  106. $styleWriter->setSizes($style->getCellMargin());
  107. $styleWriter->write();
  108. $xmlWriter->endElement(); // w:tblCellMar
  109. }
  110. }
  111. /**
  112. * Write border.
  113. *
  114. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  115. * @param \PhpOffice\PhpWord\Style\Table $style
  116. * @return void
  117. */
  118. private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
  119. {
  120. if ($style->hasBorder()) {
  121. $xmlWriter->startElement('w:tblBorders');
  122. $styleWriter = new MarginBorder($xmlWriter);
  123. $styleWriter->setSizes($style->getBorderSize());
  124. $styleWriter->setColors($style->getBorderColor());
  125. $styleWriter->write();
  126. $xmlWriter->endElement(); // w:tblBorders
  127. }
  128. }
  129. /**
  130. * Write row style.
  131. *
  132. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  133. * @param \PhpOffice\PhpWord\Style\Table $style
  134. * @return void
  135. */
  136. private function writeFirstRow(XMLWriter $xmlWriter, TableStyle $style)
  137. {
  138. $xmlWriter->startElement('w:tblStylePr');
  139. $xmlWriter->writeAttribute('w:type', 'firstRow');
  140. $xmlWriter->startElement('w:tcPr');
  141. $this->writeBorder($xmlWriter, $style);
  142. $this->writeShading($xmlWriter, $style);
  143. $xmlWriter->endElement(); // w:tcPr
  144. $xmlWriter->endElement(); // w:tblStylePr
  145. }
  146. /**
  147. * Write shading.
  148. *
  149. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  150. * @param \PhpOffice\PhpWord\Style\Table $style
  151. * @return void
  152. */
  153. private function writeShading(XMLWriter $xmlWriter, TableStyle $style)
  154. {
  155. if ($style->getShading() !== null) {
  156. $xmlWriter->startElement('w:tcPr');
  157. $styleWriter = new Shading($xmlWriter, $style->getShading());
  158. $styleWriter->write();
  159. $xmlWriter->endElement();
  160. }
  161. }
  162. /**
  163. * Set width.
  164. *
  165. * @param int $value
  166. * @return void
  167. */
  168. public function setWidth($value = null)
  169. {
  170. $this->width = $value;
  171. }
  172. }