説明なし

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\Part;
  18. use PhpOffice\PhpWord\Settings;
  19. use PhpOffice\PhpWord\Shared\Converter;
  20. use PhpOffice\PhpWord\Style;
  21. use PhpOffice\PhpWord\Style\Font;
  22. /**
  23. * RTF header part writer
  24. *
  25. * - Character set
  26. * - Font table
  27. * - File table (not supported yet)
  28. * - Color table
  29. * - Style sheet (not supported yet)
  30. * - List table (not supported yet)
  31. *
  32. * @since 0.11.0
  33. * @link http://www.biblioscape.com/rtf15_spec.htm#Heading6
  34. */
  35. class Header extends AbstractPart
  36. {
  37. /**
  38. * Font table
  39. *
  40. * @var array
  41. */
  42. private $fontTable = array();
  43. /**
  44. * Color table
  45. *
  46. * @var array
  47. */
  48. private $colorTable = array();
  49. /**
  50. * Get font table.
  51. *
  52. * @return array
  53. */
  54. public function getFontTable()
  55. {
  56. return $this->fontTable;
  57. }
  58. /**
  59. * Get color table.
  60. *
  61. * @return array
  62. */
  63. public function getColorTable()
  64. {
  65. return $this->colorTable;
  66. }
  67. /**
  68. * Write part
  69. *
  70. * @return string
  71. */
  72. public function write()
  73. {
  74. $this->registerFont();
  75. $content = '';
  76. $content .= $this->writeCharset();
  77. $content .= $this->writeDefaults();
  78. $content .= $this->writeFontTable();
  79. $content .= $this->writeColorTable();
  80. $content .= $this->writeGenerator();
  81. $content .= PHP_EOL;
  82. return $content;
  83. }
  84. /**
  85. * Write character set
  86. *
  87. * @return string
  88. */
  89. private function writeCharset()
  90. {
  91. $content = '';
  92. $content .= '\ansi';
  93. $content .= '\ansicpg1252';
  94. $content .= PHP_EOL;
  95. return $content;
  96. }
  97. /**
  98. * Write header defaults
  99. *
  100. * @return string
  101. */
  102. private function writeDefaults()
  103. {
  104. $content = '';
  105. $content .= '\deff0';
  106. $content .= PHP_EOL;
  107. return $content;
  108. }
  109. /**
  110. * Write font table
  111. *
  112. * @return string
  113. */
  114. private function writeFontTable()
  115. {
  116. $content = '';
  117. $content .= '{';
  118. $content .= '\fonttbl';
  119. foreach ($this->fontTable as $index => $font) {
  120. $content .= "{\\f{$index}\\fnil\\fcharset0 {$font};}";
  121. }
  122. $content .= '}';
  123. $content .= PHP_EOL;
  124. return $content;
  125. }
  126. /**
  127. * Write color table
  128. *
  129. * @return string
  130. */
  131. private function writeColorTable()
  132. {
  133. $content = '';
  134. $content .= '{';
  135. $content .= '\colortbl;';
  136. foreach ($this->colorTable as $color) {
  137. list($red, $green, $blue) = Converter::htmlToRgb($color);
  138. $content .= "\\red{$red}\\green{$green}\\blue{$blue};";
  139. }
  140. $content .= '}';
  141. $content .= PHP_EOL;
  142. return $content;
  143. }
  144. /**
  145. * Write
  146. *
  147. * @return string
  148. */
  149. private function writeGenerator()
  150. {
  151. $content = '';
  152. $content .= '{\*\generator PhpWord;}'; // Set the generator
  153. $content .= PHP_EOL;
  154. return $content;
  155. }
  156. /**
  157. * Register all fonts and colors in both named and inline styles to appropriate header table.
  158. *
  159. * @return void
  160. */
  161. private function registerFont()
  162. {
  163. $phpWord = $this->getParentWriter()->getPhpWord();
  164. $this->fontTable[] = Settings::getDefaultFontName();
  165. // Search named styles
  166. $styles = Style::getStyles();
  167. foreach ($styles as $style) {
  168. $this->registerFontItems($style);
  169. }
  170. // Search inline styles
  171. $sections = $phpWord->getSections();
  172. foreach ($sections as $section) {
  173. $elements = $section->getElements();
  174. $this->registerBorderColor($section->getStyle());
  175. foreach ($elements as $element) {
  176. if (method_exists($element, 'getFontStyle')) {
  177. $style = $element->getFontStyle();
  178. $this->registerFontItems($style);
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * Register border colors.
  185. *
  186. * @param \PhpOffice\PhpWord\Style\Border $style
  187. * @return void
  188. */
  189. private function registerBorderColor($style)
  190. {
  191. $colors = $style->getBorderColor();
  192. foreach ($colors as $color) {
  193. if ($color !== null) {
  194. $this->registerTableItem($this->colorTable, $color);
  195. }
  196. }
  197. }
  198. /**
  199. * Register fonts and colors.
  200. *
  201. * @param \PhpOffice\PhpWord\Style\AbstractStyle $style
  202. * @return void
  203. */
  204. private function registerFontItems($style)
  205. {
  206. $defaultFont = Settings::getDefaultFontName();
  207. $defaultColor = Settings::DEFAULT_FONT_COLOR;
  208. if ($style instanceof Font) {
  209. $this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
  210. $this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
  211. $this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);
  212. }
  213. }
  214. /**
  215. * Register individual font and color.
  216. *
  217. * @param array &$table
  218. * @param string $value
  219. * @param string $default
  220. * @return void
  221. */
  222. private function registerTableItem(&$table, $value, $default = null)
  223. {
  224. if (in_array($value, $table) === false && $value !== null && $value != $default) {
  225. $table[] = $value;
  226. }
  227. }
  228. }