Styles.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Reader\Word2007;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. /**
  21. * Styles reader
  22. *
  23. * @since 0.10.0
  24. */
  25. class Styles extends AbstractPart
  26. {
  27. /**
  28. * Read styles.xml.
  29. *
  30. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  31. * @return void
  32. */
  33. public function read(PhpWord $phpWord)
  34. {
  35. $xmlReader = new XMLReader();
  36. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  37. $nodes = $xmlReader->getElements('w:style');
  38. if ($nodes->length > 0) {
  39. foreach ($nodes as $node) {
  40. $type = $xmlReader->getAttribute('w:type', $node);
  41. $name = $xmlReader->getAttribute('w:styleId', $node);
  42. if (is_null($name)) {
  43. $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
  44. }
  45. preg_match('/Heading(\d)/', $name, $headingMatches);
  46. // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
  47. switch ($type) {
  48. case 'paragraph':
  49. $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
  50. $fontStyle = $this->readFontStyle($xmlReader, $node);
  51. if (!empty($headingMatches)) {
  52. $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
  53. } else {
  54. if (empty($fontStyle)) {
  55. if (is_array($paragraphStyle)) {
  56. $phpWord->addParagraphStyle($name, $paragraphStyle);
  57. }
  58. } else {
  59. $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
  60. }
  61. }
  62. break;
  63. case 'character':
  64. $fontStyle = $this->readFontStyle($xmlReader, $node);
  65. if (!empty($fontStyle)) {
  66. $phpWord->addFontStyle($name, $fontStyle);
  67. }
  68. break;
  69. case 'table':
  70. $tStyle = $this->readTableStyle($xmlReader, $node);
  71. if (!empty($tStyle)) {
  72. $phpWord->addTableStyle($name, $tStyle);
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. }