暫無描述

Settings.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /**
  19. * Word2007 settings part writer: word/settings.xml
  20. *
  21. * @link http://www.schemacentral.com/sc/ooxml/t-w_CT_Settings.html
  22. */
  23. class Settings extends AbstractPart
  24. {
  25. /**
  26. * Settings value
  27. *
  28. * @var array
  29. */
  30. private $settings = array();
  31. /**
  32. * Write part
  33. *
  34. * @return string
  35. */
  36. public function write()
  37. {
  38. $this->getSettings();
  39. $xmlWriter = $this->getXmlWriter();
  40. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  41. $xmlWriter->startElement('w:settings');
  42. $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  43. $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
  44. $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
  45. $xmlWriter->writeAttribute('xmlns:sl', 'http://schemas.openxmlformats.org/schemaLibrary/2006/main');
  46. $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  47. $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  48. $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
  49. foreach ($this->settings as $settingKey => $settingValue) {
  50. $this->writeSetting($xmlWriter, $settingKey, $settingValue);
  51. }
  52. $xmlWriter->endElement(); // w:settings
  53. return $xmlWriter->getData();
  54. }
  55. /**
  56. * Write indivual setting, recursive to any child settings.
  57. *
  58. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  59. * @param string $settingKey
  60. * @param array|string $settingValue
  61. * @return void
  62. */
  63. protected function writeSetting($xmlWriter, $settingKey, $settingValue)
  64. {
  65. if ($settingValue == '') {
  66. $xmlWriter->writeElement($settingKey);
  67. } else {
  68. $xmlWriter->startElement($settingKey);
  69. /** @var array $settingValue Type hint */
  70. foreach ($settingValue as $childKey => $childValue) {
  71. if ($childKey == '@attributes') {
  72. foreach ($childValue as $key => $val) {
  73. $xmlWriter->writeAttribute($key, $val);
  74. }
  75. } else {
  76. $this->writeSetting($xmlWriter, $childKey, $childValue);
  77. }
  78. }
  79. $xmlWriter->endElement();
  80. }
  81. }
  82. /**
  83. * Get settings.
  84. *
  85. * @return void
  86. */
  87. private function getSettings()
  88. {
  89. // Default settings
  90. $this->settings = array(
  91. 'w:zoom' => array('@attributes' => array('w:percent' => '100')),
  92. 'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
  93. 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
  94. 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
  95. 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
  96. 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
  97. 'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
  98. 'w:compat' => '',
  99. 'm:mathPr' => array(
  100. 'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
  101. 'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
  102. 'm:brkBinSub' => array('@attributes' => array('m:val' => '--')),
  103. 'm:smallFrac' => array('@attributes' => array('m:val' => 'off')),
  104. 'm:dispDef' => '',
  105. 'm:lMargin' => array('@attributes' => array('m:val' => '0')),
  106. 'm:rMargin' => array('@attributes' => array('m:val' => '0')),
  107. 'm:defJc' => array('@attributes' => array('m:val' => 'centerGroup')),
  108. 'm:wrapIndent' => array('@attributes' => array('m:val' => '1440')),
  109. 'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
  110. 'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
  111. ),
  112. 'w:clrSchemeMapping' => array(
  113. '@attributes' => array(
  114. 'w:bg1' => 'light1',
  115. 'w:t1' => 'dark1',
  116. 'w:bg2' => 'light2',
  117. 'w:t2' => 'dark2',
  118. 'w:accent1' => 'accent1',
  119. 'w:accent2' => 'accent2',
  120. 'w:accent3' => 'accent3',
  121. 'w:accent4' => 'accent4',
  122. 'w:accent5' => 'accent5',
  123. 'w:accent6' => 'accent6',
  124. 'w:hyperlink' => 'hyperlink',
  125. 'w:followedHyperlink' => 'followedHyperlink',
  126. ),
  127. ),
  128. );
  129. // Other settings
  130. $this->getProtection();
  131. $this->getCompatibility();
  132. }
  133. /**
  134. * Get protection settings.
  135. *
  136. * @return void
  137. */
  138. private function getProtection()
  139. {
  140. $protection = $this->getParentWriter()->getPhpWord()->getProtection();
  141. if ($protection->getEditing() !== null) {
  142. $this->settings['w:documentProtection'] = array(
  143. '@attributes' => array(
  144. 'w:enforcement' => 1,
  145. 'w:edit' => $protection->getEditing(),
  146. )
  147. );
  148. }
  149. }
  150. /**
  151. * Get compatibility setting.
  152. *
  153. * @return void
  154. */
  155. private function getCompatibility()
  156. {
  157. $compatibility = $this->getParentWriter()->getPhpWord()->getCompatibility();
  158. if ($compatibility->getOoxmlVersion() !== null) {
  159. $this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array(
  160. 'w:name' => 'compatibilityMode',
  161. 'w:uri' => 'http://schemas.microsoft.com/office/word',
  162. 'w:val' => $compatibility->getOoxmlVersion(),
  163. ));
  164. }
  165. }
  166. }