Styles.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base {
  28. private $_document;
  29. public function writeStyles(PHPWord $pPHPWord = null) {
  30. // Create XML writer
  31. $objWriter = null;
  32. if($this->getParentWriter()->getUseDiskCaching()) {
  33. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  34. } else {
  35. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
  36. }
  37. $this->_document = $pPHPWord;
  38. // XML header
  39. $objWriter->startDocument('1.0','UTF-8','yes');
  40. $objWriter->startElement('w:styles');
  41. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  42. $objWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
  43. // Write DocDefaults
  44. $this->_writeDocDefaults($objWriter);
  45. // Write Style Definitions
  46. $styles = PHPWord_Style::getStyles();
  47. if(count($styles) > 0) {
  48. foreach($styles as $styleName => $style) {
  49. if($style instanceof PHPWord_Style_Font) {
  50. $paragraphStyle = $style->getParagraphStyle();
  51. $styleType = $style->getStyleType();
  52. $type = ($styleType == 'title') ? 'paragraph' : 'character';
  53. if(!is_null($paragraphStyle)) {
  54. $type = 'paragraph';
  55. }
  56. $objWriter->startElement('w:style');
  57. $objWriter->writeAttribute('w:type', $type);
  58. if($styleType == 'title') {
  59. $arrStyle = explode('_', $styleName);
  60. $styleId = 'Heading'.$arrStyle[1];
  61. $styleName = 'heading '.$arrStyle[1];
  62. $styleLink = 'Heading'.$arrStyle[1].'Char';
  63. $objWriter->writeAttribute('w:styleId', $styleId);
  64. $objWriter->startElement('w:link');
  65. $objWriter->writeAttribute('w:val', $styleLink);
  66. $objWriter->endElement();
  67. }
  68. $objWriter->startElement('w:name');
  69. $objWriter->writeAttribute('w:val', $styleName);
  70. $objWriter->endElement();
  71. if(!is_null($paragraphStyle)) {
  72. $this->_writeParagraphStyle($objWriter, $paragraphStyle);
  73. }
  74. $this->_writeTextStyle($objWriter, $style);
  75. $objWriter->endElement();
  76. } elseif($style instanceof PHPWord_Style_Paragraph) {
  77. $objWriter->startElement('w:style');
  78. $objWriter->writeAttribute('w:type', 'paragraph');
  79. $objWriter->writeAttribute('w:customStyle', '1');
  80. $objWriter->writeAttribute('w:styleId', $styleName);
  81. $objWriter->startElement('w:name');
  82. $objWriter->writeAttribute('w:val', $styleName);
  83. $objWriter->endElement();
  84. $this->_writeParagraphStyle($objWriter, $style);
  85. $objWriter->endElement();
  86. } elseif($style instanceof PHPWord_Style_TableFull) {
  87. $objWriter->startElement('w:style');
  88. $objWriter->writeAttribute('w:type', 'table');
  89. $objWriter->writeAttribute('w:customStyle', '1');
  90. $objWriter->writeAttribute('w:styleId', $styleName);
  91. $objWriter->startElement('w:name');
  92. $objWriter->writeAttribute('w:val', $styleName);
  93. $objWriter->endElement();
  94. $objWriter->startElement('w:uiPriority');
  95. $objWriter->writeAttribute('w:val', '99');
  96. $objWriter->endElement();
  97. $this->_writeFullTableStyle($objWriter, $style);
  98. $objWriter->endElement();
  99. }
  100. }
  101. }
  102. $objWriter->endElement(); // w:styles
  103. // Return
  104. return $objWriter->getData();
  105. }
  106. private function _writeFullTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_TableFull $style) {
  107. $brdSz = $style->getBorderSize();
  108. $brdCol = $style->getBorderColor();
  109. $bgColor = $style->getBgColor();
  110. $cellMargin = $style->getCellMargin();
  111. $tableAlign = $style->getTableAlign();
  112. $tableIndent = $style->getTableIndent();
  113. $bTop = (!is_null($brdSz[0])) ? true : false;
  114. $bLeft = (!is_null($brdSz[1])) ? true : false;
  115. $bRight = (!is_null($brdSz[2])) ? true : false;
  116. $bBottom = (!is_null($brdSz[3])) ? true : false;
  117. $bInsH = (!is_null($brdSz[4])) ? true : false;
  118. $bInsV = (!is_null($brdSz[5])) ? true : false;
  119. $borders = ($bTop || $bLeft || $bRight || $bBottom || $bInsH || $bInsV) ? true : false;
  120. $mTop = (!is_null($cellMargin[0])) ? true : false;
  121. $mLeft = (!is_null($cellMargin[1])) ? true : false;
  122. $mRight = (!is_null($cellMargin[2])) ? true : false;
  123. $mBottom = (!is_null($cellMargin[3])) ? true : false;
  124. $margins = ($mTop || $mLeft || $mRight || $mBottom) ? true : false;
  125. $objWriter->startElement('w:tblPr');
  126. if ($tableAlign)
  127. {
  128. $objWriter->startElement('w:jc');
  129. $objWriter->startElement('w:val', $tableAlign);
  130. $objWriter->endElement();
  131. }
  132. if ($tableIndent)
  133. {
  134. $objWriter->startElement('w:tblInd');
  135. $objWriter->startElement('w:w', $tableIndent);
  136. $objWriter->startElement('w:type', 'dxa');
  137. $objWriter->endElement();
  138. }
  139. if($margins) {
  140. $objWriter->startElement('w:tblCellMar');
  141. if($mTop) {
  142. echo $margins[0];
  143. $objWriter->startElement('w:top');
  144. $objWriter->writeAttribute('w:w', $cellMargin[0]);
  145. $objWriter->writeAttribute('w:type', 'dxa');
  146. $objWriter->endElement();
  147. }
  148. if($mLeft) {
  149. $objWriter->startElement('w:left');
  150. $objWriter->writeAttribute('w:w', $cellMargin[1]);
  151. $objWriter->writeAttribute('w:type', 'dxa');
  152. $objWriter->endElement();
  153. }
  154. if($mRight) {
  155. $objWriter->startElement('w:right');
  156. $objWriter->writeAttribute('w:w', $cellMargin[2]);
  157. $objWriter->writeAttribute('w:type', 'dxa');
  158. $objWriter->endElement();
  159. }
  160. if($mBottom) {
  161. $objWriter->startElement('w:bottom');
  162. $objWriter->writeAttribute('w:w', $cellMargin[3]);
  163. $objWriter->writeAttribute('w:type', 'dxa');
  164. $objWriter->endElement();
  165. }
  166. $objWriter->endElement();
  167. }
  168. if($borders) {
  169. $objWriter->startElement('w:tblBorders');
  170. if($bTop) {
  171. $objWriter->startElement('w:top');
  172. $objWriter->writeAttribute('w:val', 'single');
  173. $objWriter->writeAttribute('w:sz', $brdSz[0]);
  174. $objWriter->writeAttribute('w:color', $brdCol[0]);
  175. $objWriter->endElement();
  176. }
  177. if($bLeft) {
  178. $objWriter->startElement('w:left');
  179. $objWriter->writeAttribute('w:val', 'single');
  180. $objWriter->writeAttribute('w:sz', $brdSz[1]);
  181. $objWriter->writeAttribute('w:color', $brdCol[1]);
  182. $objWriter->endElement();
  183. }
  184. if($bRight) {
  185. $objWriter->startElement('w:right');
  186. $objWriter->writeAttribute('w:val', 'single');
  187. $objWriter->writeAttribute('w:sz', $brdSz[2]);
  188. $objWriter->writeAttribute('w:color', $brdCol[2]);
  189. $objWriter->endElement();
  190. }
  191. if($bBottom) {
  192. $objWriter->startElement('w:bottom');
  193. $objWriter->writeAttribute('w:val', 'single');
  194. $objWriter->writeAttribute('w:sz', $brdSz[3]);
  195. $objWriter->writeAttribute('w:color', $brdCol[3]);
  196. $objWriter->endElement();
  197. }
  198. if($bInsH) {
  199. $objWriter->startElement('w:insideH');
  200. $objWriter->writeAttribute('w:val', 'single');
  201. $objWriter->writeAttribute('w:sz', $brdSz[4]);
  202. $objWriter->writeAttribute('w:color', $brdCol[4]);
  203. $objWriter->endElement();
  204. }
  205. if($bInsV) {
  206. $objWriter->startElement('w:insideV');
  207. $objWriter->writeAttribute('w:val', 'single');
  208. $objWriter->writeAttribute('w:sz', $brdSz[5]);
  209. $objWriter->writeAttribute('w:color', $brdCol[5]);
  210. $objWriter->endElement();
  211. }
  212. $objWriter->endElement();
  213. }
  214. $objWriter->endElement();
  215. if(!is_null($bgColor)) {
  216. $objWriter->startElement('w:tcPr');
  217. $objWriter->startElement('w:shd');
  218. $objWriter->writeAttribute('w:val', 'clear');
  219. $objWriter->writeAttribute('w:color', 'auto');
  220. $objWriter->writeAttribute('w:fill', $bgColor);
  221. $objWriter->endElement();
  222. $objWriter->endElement();
  223. }
  224. // First Row
  225. $firstRow = $style->getFirstRow();
  226. if(!is_null($firstRow)) {
  227. $this->_writeRowStyle($objWriter, 'firstRow', $firstRow);
  228. }
  229. }
  230. private function _writeRowStyle(PHPWord_Shared_XMLWriter $objWriter = null, $type, PHPWord_Style_TableFull $style) {
  231. $brdSz = $style->getBorderSize();
  232. $brdCol = $style->getBorderColor();
  233. $bgColor = $style->getBgColor();
  234. $bTop = (!is_null($brdSz[0])) ? true : false;
  235. $bLeft = (!is_null($brdSz[1])) ? true : false;
  236. $bRight = (!is_null($brdSz[2])) ? true : false;
  237. $bBottom = (!is_null($brdSz[3])) ? true : false;
  238. $borders = ($bTop || $bLeft || $bRight || $bBottom) ? true : false;
  239. $objWriter->startElement('w:tblStylePr');
  240. $objWriter->writeAttribute('w:type', $type);
  241. $objWriter->startElement('w:tcPr');
  242. if(!is_null($bgColor)) {
  243. $objWriter->startElement('w:shd');
  244. $objWriter->writeAttribute('w:val', 'clear');
  245. $objWriter->writeAttribute('w:color', 'auto');
  246. $objWriter->writeAttribute('w:fill', $bgColor);
  247. $objWriter->endElement();
  248. }
  249. $objWriter->startElement('w:tcBorders');
  250. if($bTop) {
  251. $objWriter->startElement('w:top');
  252. $objWriter->writeAttribute('w:val', 'single');
  253. $objWriter->writeAttribute('w:sz', $brdSz[0]);
  254. $objWriter->writeAttribute('w:color', $brdCol[0]);
  255. $objWriter->endElement();
  256. }
  257. if($bLeft) {
  258. $objWriter->startElement('w:left');
  259. $objWriter->writeAttribute('w:val', 'single');
  260. $objWriter->writeAttribute('w:sz', $brdSz[1]);
  261. $objWriter->writeAttribute('w:color', $brdCol[1]);
  262. $objWriter->endElement();
  263. }
  264. if($bRight) {
  265. $objWriter->startElement('w:right');
  266. $objWriter->writeAttribute('w:val', 'single');
  267. $objWriter->writeAttribute('w:sz', $brdSz[2]);
  268. $objWriter->writeAttribute('w:color', $brdCol[2]);
  269. $objWriter->endElement();
  270. }
  271. if($bBottom) {
  272. $objWriter->startElement('w:bottom');
  273. $objWriter->writeAttribute('w:val', 'single');
  274. $objWriter->writeAttribute('w:sz', $brdSz[3]);
  275. $objWriter->writeAttribute('w:color', $brdCol[3]);
  276. $objWriter->endElement();
  277. }
  278. $objWriter->endElement();
  279. $objWriter->endElement();
  280. $objWriter->endElement();
  281. }
  282. private function _writeDocDefaults(PHPWord_Shared_XMLWriter $objWriter = null) {
  283. $fontName = $this->_document->getDefaultFontName();
  284. $fontSize = $this->_document->getDefaultFontSize();
  285. $objWriter->startElement('w:docDefaults');
  286. $objWriter->startElement('w:rPrDefault');
  287. $objWriter->startElement('w:rPr');
  288. $objWriter->startElement('w:rFonts');
  289. $objWriter->writeAttribute('w:ascii', $fontName);
  290. $objWriter->writeAttribute('w:hAnsi', $fontName);
  291. $objWriter->writeAttribute('w:eastAsia', $fontName);
  292. $objWriter->writeAttribute('w:cs', $fontName);
  293. $objWriter->endElement();
  294. $objWriter->startElement('w:sz');
  295. $objWriter->writeAttribute('w:val', $fontSize);
  296. $objWriter->endElement();
  297. $objWriter->startElement('w:szCs');
  298. $objWriter->writeAttribute('w:val', $fontSize);
  299. $objWriter->endElement();
  300. $objWriter->endElement();
  301. $objWriter->endElement();
  302. $objWriter->endElement();
  303. }
  304. }
  305. ?>