Ingen beskrivning

Styles.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\ODText\Part;
  18. use PhpOffice\PhpWord\Settings;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. use PhpOffice\PhpWord\Style;
  21. /**
  22. * ODText styles part writer: styles.xml
  23. */
  24. class Styles extends AbstractPart
  25. {
  26. /**
  27. * Write part
  28. *
  29. * @return string
  30. */
  31. public function write()
  32. {
  33. $xmlWriter = $this->getXmlWriter();
  34. // XML header
  35. $xmlWriter->startDocument('1.0', 'UTF-8');
  36. $xmlWriter->startElement('office:document-styles');
  37. $this->writeCommonRootAttributes($xmlWriter);
  38. // Font declarations
  39. $this->writeFontFaces($xmlWriter);
  40. // Office styles
  41. $xmlWriter->startElement('office:styles');
  42. $this->writeDefault($xmlWriter);
  43. $this->writeNamed($xmlWriter);
  44. $xmlWriter->endElement();
  45. // Automatic styles
  46. $xmlWriter->startElement('office:automatic-styles');
  47. $this->writePageLayout($xmlWriter);
  48. $this->writeMaster($xmlWriter);
  49. $xmlWriter->endElement();
  50. $xmlWriter->endElement(); // office:document-styles
  51. return $xmlWriter->getData();
  52. }
  53. /**
  54. * Write default styles.
  55. *
  56. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  57. * @return void
  58. */
  59. private function writeDefault(XMLWriter $xmlWriter)
  60. {
  61. $xmlWriter->startElement('style:default-style');
  62. $xmlWriter->writeAttribute('style:family', 'paragraph');
  63. // Paragraph
  64. $xmlWriter->startElement('style:paragraph-properties');
  65. $xmlWriter->writeAttribute('fo:hyphenation-ladder-count', 'no-limit');
  66. $xmlWriter->writeAttribute('style:text-autospace', 'ideograph-alpha');
  67. $xmlWriter->writeAttribute('style:punctuation-wrap', 'hanging');
  68. $xmlWriter->writeAttribute('style:line-break', 'strict');
  69. $xmlWriter->writeAttribute('style:tab-stop-distance', '1.249cm');
  70. $xmlWriter->writeAttribute('style:writing-mode', 'page');
  71. $xmlWriter->endElement(); // style:paragraph-properties
  72. // Font
  73. $xmlWriter->startElement('style:text-properties');
  74. $xmlWriter->writeAttribute('style:use-window-font-color', 'true');
  75. $xmlWriter->writeAttribute('style:font-name', Settings::getDefaultFontName());
  76. $xmlWriter->writeAttribute('fo:font-size', Settings::getDefaultFontSize() . 'pt');
  77. $xmlWriter->writeAttribute('fo:language', 'fr');
  78. $xmlWriter->writeAttribute('fo:country', 'FR');
  79. $xmlWriter->writeAttribute('style:letter-kerning', 'true');
  80. $xmlWriter->writeAttribute('style:font-name-asian', Settings::getDefaultFontName() . '2');
  81. $xmlWriter->writeAttribute('style:font-size-asian', Settings::getDefaultFontSize() . 'pt');
  82. $xmlWriter->writeAttribute('style:language-asian', 'zh');
  83. $xmlWriter->writeAttribute('style:country-asian', 'CN');
  84. $xmlWriter->writeAttribute('style:font-name-complex', Settings::getDefaultFontName() . '2');
  85. $xmlWriter->writeAttribute('style:font-size-complex', Settings::getDefaultFontSize() . 'pt');
  86. $xmlWriter->writeAttribute('style:language-complex', 'hi');
  87. $xmlWriter->writeAttribute('style:country-complex', 'IN');
  88. $xmlWriter->writeAttribute('fo:hyphenate', 'false');
  89. $xmlWriter->writeAttribute('fo:hyphenation-remain-char-count', '2');
  90. $xmlWriter->writeAttribute('fo:hyphenation-push-char-count', '2');
  91. $xmlWriter->endElement(); // style:text-properties
  92. $xmlWriter->endElement(); // style:default-style
  93. }
  94. /**
  95. * Write named styles.
  96. *
  97. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  98. * @return void
  99. */
  100. private function writeNamed(XMLWriter $xmlWriter)
  101. {
  102. $styles = Style::getStyles();
  103. if (count($styles) > 0) {
  104. foreach ($styles as $style) {
  105. if ($style->isAuto() === false) {
  106. $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style));
  107. if (class_exists($styleClass)) {
  108. /** @var $styleWriter \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle Type hint */
  109. $styleWriter = new $styleClass($xmlWriter, $style);
  110. $styleWriter->write();
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Write page layout styles.
  118. *
  119. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  120. * @return void
  121. */
  122. private function writePageLayout(XMLWriter $xmlWriter)
  123. {
  124. $xmlWriter->startElement('style:page-layout');
  125. $xmlWriter->writeAttribute('style:name', 'Mpm1');
  126. $xmlWriter->startElement('style:page-layout-properties');
  127. $xmlWriter->writeAttribute('fo:page-width', "21.001cm");
  128. $xmlWriter->writeAttribute('fo:page-height', '29.7cm');
  129. $xmlWriter->writeAttribute('style:num-format', '1');
  130. $xmlWriter->writeAttribute('style:print-orientation', 'portrait');
  131. $xmlWriter->writeAttribute('fo:margin-top', '2.501cm');
  132. $xmlWriter->writeAttribute('fo:margin-bottom', '2cm');
  133. $xmlWriter->writeAttribute('fo:margin-left', '2.501cm');
  134. $xmlWriter->writeAttribute('fo:margin-right', '2.501cm');
  135. $xmlWriter->writeAttribute('style:writing-mode', 'lr-tb');
  136. $xmlWriter->writeAttribute('style:layout-grid-color', '#c0c0c0');
  137. $xmlWriter->writeAttribute('style:layout-grid-lines', '25199');
  138. $xmlWriter->writeAttribute('style:layout-grid-base-height', '0.423cm');
  139. $xmlWriter->writeAttribute('style:layout-grid-ruby-height', '0cm');
  140. $xmlWriter->writeAttribute('style:layout-grid-mode', 'none');
  141. $xmlWriter->writeAttribute('style:layout-grid-ruby-below', 'false');
  142. $xmlWriter->writeAttribute('style:layout-grid-print', 'false');
  143. $xmlWriter->writeAttribute('style:layout-grid-display', 'false');
  144. $xmlWriter->writeAttribute('style:layout-grid-base-width', '0.37cm');
  145. $xmlWriter->writeAttribute('style:layout-grid-snap-to', 'true');
  146. $xmlWriter->writeAttribute('style:footnote-max-height', '0cm');
  147. $xmlWriter->startElement('style:footnote-sep');
  148. $xmlWriter->writeAttribute('style:width', '0.018cm');
  149. $xmlWriter->writeAttribute('style:line-style', 'solid');
  150. $xmlWriter->writeAttribute('style:adjustment', 'left');
  151. $xmlWriter->writeAttribute('style:rel-width', '25%');
  152. $xmlWriter->writeAttribute('style:color', '#000000');
  153. $xmlWriter->endElement(); //style:footnote-sep
  154. $xmlWriter->endElement(); // style:page-layout-properties
  155. $xmlWriter->startElement('style:header-style');
  156. $xmlWriter->endElement(); // style:header-style
  157. $xmlWriter->startElement('style:footer-style');
  158. $xmlWriter->endElement(); // style:footer-style
  159. $xmlWriter->endElement(); // style:page-layout
  160. }
  161. /**
  162. * Write master style.
  163. *
  164. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  165. * @return void
  166. */
  167. private function writeMaster(XMLWriter $xmlWriter)
  168. {
  169. $xmlWriter->startElement('office:master-styles');
  170. $xmlWriter->startElement('style:master-page');
  171. $xmlWriter->writeAttribute('style:name', 'Standard');
  172. $xmlWriter->writeAttribute('style:page-layout-name', 'Mpm1');
  173. $xmlWriter->endElement(); // style:master-page
  174. $xmlWriter->endElement(); // office:master-styles
  175. }
  176. }