暫無描述

Border.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Style;
  18. /**
  19. * Border style writer
  20. *
  21. * @since 0.12.0
  22. */
  23. class Border extends AbstractStyle
  24. {
  25. /**
  26. * Sizes
  27. *
  28. * @var array
  29. */
  30. private $sizes = array();
  31. /**
  32. * Colors
  33. *
  34. * @var array
  35. */
  36. private $colors = array();
  37. /**
  38. * Write style
  39. *
  40. * @return string
  41. */
  42. public function write()
  43. {
  44. $content = '';
  45. $sides = array('top', 'left', 'right', 'bottom');
  46. $sizeCount = count($this->sizes) - 1;
  47. // Page border measure
  48. // 8 = from text, infront off; 32 = from edge, infront on; 40 = from edge, infront off
  49. $content .= '\pgbrdropt32';
  50. for ($i = 0; $i < $sizeCount; $i++) {
  51. if ($this->sizes[$i] !== null) {
  52. $color = null;
  53. if (isset($this->colors[$i])) {
  54. $color = $this->colors[$i];
  55. }
  56. $content .= $this->writeSide($sides[$i], $this->sizes[$i], $color);
  57. }
  58. }
  59. return $content;
  60. }
  61. /**
  62. * Write side
  63. *
  64. * @param string $side
  65. * @param int $width
  66. * @param string $color
  67. * @return string
  68. */
  69. private function writeSide($side, $width, $color = '')
  70. {
  71. /** @var \PhpOffice\PhpWord\Writer\RTF $rtfWriter */
  72. $rtfWriter = $this->getParentWriter();
  73. $colorIndex = 0;
  74. if ($rtfWriter !== null) {
  75. $colorTable = $rtfWriter->getColorTable();
  76. $index = array_search($color, $colorTable);
  77. if ($index !== false && $colorIndex !== null) {
  78. $colorIndex = $index + 1;
  79. }
  80. }
  81. $content = '';
  82. $content .= '\pgbrdr' . substr($side, 0, 1);
  83. $content .= '\brdrs'; // Single-thickness border; @todo Get other type of border
  84. $content .= '\brdrw' . $width; // Width
  85. $content .= '\brdrcf' . $colorIndex; // Color
  86. $content .= '\brsp480'; // Space in twips between borders and the paragraph (24pt, following OOXML)
  87. $content .= ' ';
  88. return $content;
  89. }
  90. /**
  91. * Set sizes.
  92. *
  93. * @param integer[] $value
  94. * @return void
  95. */
  96. public function setSizes($value)
  97. {
  98. $this->sizes = $value;
  99. }
  100. /**
  101. * Set colors.
  102. *
  103. * @param string[] $value
  104. * @return void
  105. */
  106. public function setColors($value)
  107. {
  108. $this->colors = $value;
  109. }
  110. }