暫無描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\RTF\Element;
  18. use PhpOffice\PhpWord\Element\Cell as CellElement;
  19. use PhpOffice\PhpWord\Element\Row as RowElement;
  20. use PhpOffice\PhpWord\Element\Table as TableElement;
  21. /**
  22. * Table element RTF writer
  23. *
  24. * @since 0.11.0
  25. */
  26. class Table extends AbstractElement
  27. {
  28. /**
  29. * Write element
  30. *
  31. * @return string
  32. */
  33. public function write()
  34. {
  35. if (!$this->element instanceof TableElement) {
  36. return '';
  37. }
  38. $element = $this->element;
  39. // No nesting table for now
  40. if ($element->getNestedLevel() >= 1) {
  41. return '';
  42. }
  43. $content = '';
  44. $rows = $element->getRows();
  45. $rowCount = count($rows);
  46. if ($rowCount > 0) {
  47. $content .= '\pard' . PHP_EOL;
  48. for ($i = 0; $i < $rowCount; $i++) {
  49. $content .= '\trowd ';
  50. $content .= $this->writeRowDef($rows[$i]);
  51. $content .= PHP_EOL;
  52. $content .= $this->writeRow($rows[$i]);
  53. $content .= '\row' . PHP_EOL;
  54. }
  55. }
  56. return $content;
  57. }
  58. /**
  59. * Write column
  60. *
  61. * @param \PhpOffice\PhpWord\Element\Row $row
  62. * @return string
  63. */
  64. private function writeRowDef(RowElement $row)
  65. {
  66. $content = '';
  67. $rightMargin = 0;
  68. foreach ($row->getCells() as $cell) {
  69. $width = $cell->getWidth();
  70. $vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
  71. if ($width === null) {
  72. $width = 720; // Arbitrary default width
  73. }
  74. $rightMargin += $width;
  75. $content .= "{$vMerge}\cellx{$rightMargin} ";
  76. }
  77. return $content;
  78. }
  79. /**
  80. * Write row
  81. *
  82. * @param \PhpOffice\PhpWord\Element\Row $row
  83. * @return string
  84. */
  85. private function writeRow(RowElement $row)
  86. {
  87. $content = '';
  88. // Write cells
  89. foreach ($row->getCells() as $cell) {
  90. $content .= $this->writeCell($cell);
  91. }
  92. return $content;
  93. }
  94. /**
  95. * Write cell
  96. *
  97. * @param \PhpOffice\PhpWord\Element\Cell $cell
  98. * @return string
  99. */
  100. private function writeCell(CellElement $cell)
  101. {
  102. $content = '\intbl' . PHP_EOL;
  103. // Write content
  104. $writer = new Container($this->parentWriter, $cell);
  105. $content .= $writer->write();
  106. $content .= '\cell' . PHP_EOL;
  107. return $content;
  108. }
  109. /**
  110. * Get vertical merge style
  111. *
  112. * @param string $value
  113. * @return string
  114. * @todo Move to style
  115. */
  116. private function getVMerge($value)
  117. {
  118. $style = '';
  119. if ($value == 'restart') {
  120. $style = '\clvmgf';
  121. } elseif ($value == 'continue') {
  122. $style = '\clvmrg';
  123. }
  124. return $style;
  125. }
  126. }