Ingen beskrivning

Styles.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\Part;
  18. use PhpOffice\PhpWord\Settings as PhpWordSettings;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. use PhpOffice\PhpWord\Style;
  21. use PhpOffice\PhpWord\Style\Font as FontStyle;
  22. use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
  23. use PhpOffice\PhpWord\Style\Table as TableStyle;
  24. use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter;
  25. use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter;
  26. use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
  27. /**
  28. * Word2007 styles part writer: word/styles.xml
  29. *
  30. * @todo Do something with the numbering style introduced in 0.10.0
  31. * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For writeFontStyle, writeParagraphStyle, and writeTableStyle
  32. */
  33. class Styles extends AbstractPart
  34. {
  35. /**
  36. * Write part
  37. *
  38. * @return string
  39. */
  40. public function write()
  41. {
  42. $xmlWriter = $this->getXmlWriter();
  43. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  44. $xmlWriter->startElement('w:styles');
  45. $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  46. $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
  47. // Write default styles
  48. $styles = Style::getStyles();
  49. $this->writeDefaultStyles($xmlWriter, $styles);
  50. // Write styles
  51. if (count($styles) > 0) {
  52. foreach ($styles as $styleName => $style) {
  53. if ($styleName == 'Normal') {
  54. continue;
  55. }
  56. // Get style class and execute if the private method exists
  57. $styleClass = substr(get_class($style), strrpos(get_class($style), '\\') + 1);
  58. $method = "write{$styleClass}Style";
  59. if (method_exists($this, $method)) {
  60. $this->$method($xmlWriter, $styleName, $style);
  61. }
  62. }
  63. }
  64. $xmlWriter->endElement(); // w:styles
  65. return $xmlWriter->getData();
  66. }
  67. /**
  68. * Write default font and other default styles.
  69. *
  70. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  71. * @param \PhpOffice\PhpWord\Style\AbstractStyle[] $styles
  72. * @return void
  73. */
  74. private function writeDefaultStyles(XMLWriter $xmlWriter, $styles)
  75. {
  76. $fontName = PhpWordSettings::getDefaultFontName();
  77. $fontSize = PhpWordSettings::getDefaultFontSize();
  78. // Default font
  79. $xmlWriter->startElement('w:docDefaults');
  80. $xmlWriter->startElement('w:rPrDefault');
  81. $xmlWriter->startElement('w:rPr');
  82. $xmlWriter->startElement('w:rFonts');
  83. $xmlWriter->writeAttribute('w:ascii', $fontName);
  84. $xmlWriter->writeAttribute('w:hAnsi', $fontName);
  85. $xmlWriter->writeAttribute('w:eastAsia', $fontName);
  86. $xmlWriter->writeAttribute('w:cs', $fontName);
  87. $xmlWriter->endElement(); // w:rFonts
  88. $xmlWriter->startElement('w:sz');
  89. $xmlWriter->writeAttribute('w:val', $fontSize * 2);
  90. $xmlWriter->endElement(); // w:sz
  91. $xmlWriter->startElement('w:szCs');
  92. $xmlWriter->writeAttribute('w:val', $fontSize * 2);
  93. $xmlWriter->endElement(); // w:szCs
  94. $xmlWriter->endElement(); // w:rPr
  95. $xmlWriter->endElement(); // w:rPrDefault
  96. $xmlWriter->endElement(); // w:docDefaults
  97. // Normal style
  98. $xmlWriter->startElement('w:style');
  99. $xmlWriter->writeAttribute('w:type', 'paragraph');
  100. $xmlWriter->writeAttribute('w:default', '1');
  101. $xmlWriter->writeAttribute('w:styleId', 'Normal');
  102. $xmlWriter->startElement('w:name');
  103. $xmlWriter->writeAttribute('w:val', 'Normal');
  104. $xmlWriter->endElement(); // w:name
  105. if (isset($styles['Normal'])) {
  106. $styleWriter = new ParagraphStyleWriter($xmlWriter, $styles['Normal']);
  107. $styleWriter->write();
  108. }
  109. $xmlWriter->endElement(); // w:style
  110. // FootnoteReference style
  111. if (!isset($styles['FootnoteReference'])) {
  112. $xmlWriter->startElement('w:style');
  113. $xmlWriter->writeAttribute('w:type', 'character');
  114. $xmlWriter->writeAttribute('w:styleId', 'FootnoteReference');
  115. $xmlWriter->startElement('w:name');
  116. $xmlWriter->writeAttribute('w:val', 'Footnote Reference');
  117. $xmlWriter->endElement(); // w:name
  118. $xmlWriter->writeElement('w:semiHidden');
  119. $xmlWriter->writeElement('w:unhideWhenUsed');
  120. $xmlWriter->startElement('w:rPr');
  121. $xmlWriter->startElement('w:vertAlign');
  122. $xmlWriter->writeAttribute('w:val', 'superscript');
  123. $xmlWriter->endElement(); // w:vertAlign
  124. $xmlWriter->endElement(); // w:rPr
  125. $xmlWriter->endElement(); // w:style
  126. }
  127. }
  128. /**
  129. * Write font style.
  130. *
  131. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  132. * @param string $styleName
  133. * @param \PhpOffice\PhpWord\Style\Font $style
  134. * @return void
  135. */
  136. private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $style)
  137. {
  138. $paragraphStyle = $style->getParagraph();
  139. $styleType = $style->getStyleType();
  140. $type = ($styleType == 'title') ? 'paragraph' : 'character';
  141. if (!is_null($paragraphStyle)) {
  142. $type = 'paragraph';
  143. }
  144. $xmlWriter->startElement('w:style');
  145. $xmlWriter->writeAttribute('w:type', $type);
  146. // Heading style
  147. if ($styleType == 'title') {
  148. $arrStyle = explode('_', $styleName);
  149. $styleId = 'Heading' . $arrStyle[1];
  150. $styleName = 'heading ' . $arrStyle[1];
  151. $styleLink = 'Heading' . $arrStyle[1] . 'Char';
  152. $xmlWriter->writeAttribute('w:styleId', $styleId);
  153. $xmlWriter->startElement('w:link');
  154. $xmlWriter->writeAttribute('w:val', $styleLink);
  155. $xmlWriter->endElement();
  156. }
  157. // Style name
  158. $xmlWriter->startElement('w:name');
  159. $xmlWriter->writeAttribute('w:val', $styleName);
  160. $xmlWriter->endElement();
  161. // Parent style
  162. $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
  163. // w:pPr
  164. if (!is_null($paragraphStyle)) {
  165. $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
  166. $styleWriter->write();
  167. }
  168. // w:rPr
  169. $styleWriter = new FontStyleWriter($xmlWriter, $style);
  170. $styleWriter->write();
  171. $xmlWriter->endElement();
  172. }
  173. /**
  174. * Write paragraph style.
  175. *
  176. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  177. * @param string $styleName
  178. * @param \PhpOffice\PhpWord\Style\Paragraph $style
  179. * @return void
  180. */
  181. private function writeParagraphStyle(XMLWriter $xmlWriter, $styleName, ParagraphStyle $style)
  182. {
  183. $xmlWriter->startElement('w:style');
  184. $xmlWriter->writeAttribute('w:type', 'paragraph');
  185. $xmlWriter->writeAttribute('w:customStyle', '1');
  186. $xmlWriter->writeAttribute('w:styleId', $styleName);
  187. $xmlWriter->startElement('w:name');
  188. $xmlWriter->writeAttribute('w:val', $styleName);
  189. $xmlWriter->endElement();
  190. // Parent style
  191. $basedOn = $style->getBasedOn();
  192. $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn);
  193. // Next paragraph style
  194. $next = $style->getNext();
  195. $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next);
  196. // w:pPr
  197. $styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
  198. $styleWriter->write();
  199. $xmlWriter->endElement();
  200. }
  201. /**
  202. * Write table style.
  203. *
  204. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  205. * @param string $styleName
  206. * @param \PhpOffice\PhpWord\Style\Table $style
  207. * @return void
  208. */
  209. private function writeTableStyle(XMLWriter $xmlWriter, $styleName, TableStyle $style)
  210. {
  211. $xmlWriter->startElement('w:style');
  212. $xmlWriter->writeAttribute('w:type', 'table');
  213. $xmlWriter->writeAttribute('w:customStyle', '1');
  214. $xmlWriter->writeAttribute('w:styleId', $styleName);
  215. $xmlWriter->startElement('w:name');
  216. $xmlWriter->writeAttribute('w:val', $styleName);
  217. $xmlWriter->endElement();
  218. $xmlWriter->startElement('w:uiPriority');
  219. $xmlWriter->writeAttribute('w:val', '99');
  220. $xmlWriter->endElement();
  221. $styleWriter = new TableStyleWriter($xmlWriter, $style);
  222. $styleWriter->write();
  223. $xmlWriter->endElement(); // w:style
  224. }
  225. }