Styles.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader\Word2007;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. use PhpOffice\PhpWord\Style\Language;
  21. /**
  22. * Styles reader
  23. *
  24. * @since 0.10.0
  25. */
  26. class Styles extends AbstractPart
  27. {
  28. /**
  29. * Read styles.xml.
  30. *
  31. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  32. */
  33. public function read(PhpWord $phpWord)
  34. {
  35. $xmlReader = new XMLReader();
  36. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  37. $fontDefaults = $xmlReader->getElement('w:docDefaults/w:rPrDefault');
  38. if ($fontDefaults !== null) {
  39. $fontDefaultStyle = $this->readFontStyle($xmlReader, $fontDefaults);
  40. if (array_key_exists('name', $fontDefaultStyle)) {
  41. $phpWord->setDefaultFontName($fontDefaultStyle['name']);
  42. }
  43. if (array_key_exists('size', $fontDefaultStyle)) {
  44. $phpWord->setDefaultFontSize($fontDefaultStyle['size']);
  45. }
  46. if (array_key_exists('lang', $fontDefaultStyle)) {
  47. $phpWord->getSettings()->setThemeFontLang(new Language($fontDefaultStyle['lang']));
  48. }
  49. }
  50. $paragraphDefaults = $xmlReader->getElement('w:docDefaults/w:pPrDefault');
  51. if ($paragraphDefaults !== null) {
  52. $paragraphDefaultStyle = $this->readParagraphStyle($xmlReader, $paragraphDefaults);
  53. if ($paragraphDefaultStyle != null) {
  54. $phpWord->setDefaultParagraphStyle($paragraphDefaultStyle);
  55. }
  56. }
  57. $nodes = $xmlReader->getElements('w:style');
  58. if ($nodes->length > 0) {
  59. foreach ($nodes as $node) {
  60. $type = $xmlReader->getAttribute('w:type', $node);
  61. $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
  62. if (is_null($name)) {
  63. $name = $xmlReader->getAttribute('w:styleId', $node);
  64. }
  65. $headingMatches = array();
  66. preg_match('/Heading\s*(\d)/i', $name, $headingMatches);
  67. // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
  68. switch ($type) {
  69. case 'paragraph':
  70. $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
  71. $fontStyle = $this->readFontStyle($xmlReader, $node);
  72. if (!empty($headingMatches)) {
  73. $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
  74. } else {
  75. if (empty($fontStyle)) {
  76. if (is_array($paragraphStyle)) {
  77. $phpWord->addParagraphStyle($name, $paragraphStyle);
  78. }
  79. } else {
  80. $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
  81. }
  82. }
  83. break;
  84. case 'character':
  85. $fontStyle = $this->readFontStyle($xmlReader, $node);
  86. if (!empty($fontStyle)) {
  87. $phpWord->addFontStyle($name, $fontStyle);
  88. }
  89. break;
  90. case 'table':
  91. $tStyle = $this->readTableStyle($xmlReader, $node);
  92. if (!empty($tStyle)) {
  93. $phpWord->addTableStyle($name, $tStyle);
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }