Bez popisu

Style.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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;
  18. use PhpOffice\PhpWord\Style\AbstractStyle;
  19. use PhpOffice\PhpWord\Style\Font;
  20. use PhpOffice\PhpWord\Style\Numbering;
  21. use PhpOffice\PhpWord\Style\Paragraph;
  22. use PhpOffice\PhpWord\Style\Table;
  23. /**
  24. * Style collection
  25. */
  26. class Style
  27. {
  28. /**
  29. * Style register
  30. *
  31. * @var array
  32. */
  33. private static $styles = array();
  34. /**
  35. * Add paragraph style
  36. *
  37. * @param string $styleName
  38. * @param array $styles
  39. * @return \PhpOffice\PhpWord\Style\Paragraph
  40. */
  41. public static function addParagraphStyle($styleName, $styles)
  42. {
  43. return self::setStyleValues($styleName, new Paragraph(), $styles);
  44. }
  45. /**
  46. * Add font style
  47. *
  48. * @param string $styleName
  49. * @param array $fontStyle
  50. * @param array $paragraphStyle
  51. * @return \PhpOffice\PhpWord\Style\Font
  52. */
  53. public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
  54. {
  55. return self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
  56. }
  57. /**
  58. * Add link style
  59. *
  60. * @param string $styleName
  61. * @param array $styles
  62. * @return \PhpOffice\PhpWord\Style\Font
  63. */
  64. public static function addLinkStyle($styleName, $styles)
  65. {
  66. return self::setStyleValues($styleName, new Font('link'), $styles);
  67. }
  68. /**
  69. * Add numbering style
  70. *
  71. * @param string $styleName
  72. * @param array $styleValues
  73. * @return \PhpOffice\PhpWord\Style\Numbering
  74. * @since 0.10.0
  75. */
  76. public static function addNumberingStyle($styleName, $styleValues)
  77. {
  78. return self::setStyleValues($styleName, new Numbering(), $styleValues);
  79. }
  80. /**
  81. * Add title style
  82. *
  83. * @param int $depth
  84. * @param array $fontStyle
  85. * @param array $paragraphStyle
  86. * @return \PhpOffice\PhpWord\Style\Font
  87. */
  88. public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
  89. {
  90. return self::setStyleValues("Heading_{$depth}", new Font('title', $paragraphStyle), $fontStyle);
  91. }
  92. /**
  93. * Add table style
  94. *
  95. * @param string $styleName
  96. * @param array $styleTable
  97. * @param array|null $styleFirstRow
  98. * @return \PhpOffice\PhpWord\Style\Table
  99. */
  100. public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
  101. {
  102. return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
  103. }
  104. /**
  105. * Count styles
  106. *
  107. * @return int
  108. * @since 0.10.0
  109. */
  110. public static function countStyles()
  111. {
  112. return count(self::$styles);
  113. }
  114. /**
  115. * Reset styles.
  116. *
  117. * @since 0.10.0
  118. *
  119. * @return void
  120. */
  121. public static function resetStyles()
  122. {
  123. self::$styles = array();
  124. }
  125. /**
  126. * Set default paragraph style
  127. *
  128. * @param array $styles Paragraph style definition
  129. * @return \PhpOffice\PhpWord\Style\Paragraph
  130. */
  131. public static function setDefaultParagraphStyle($styles)
  132. {
  133. return self::addParagraphStyle('Normal', $styles);
  134. }
  135. /**
  136. * Get all styles
  137. *
  138. * @return \PhpOffice\PhpWord\Style\AbstractStyle[]
  139. */
  140. public static function getStyles()
  141. {
  142. return self::$styles;
  143. }
  144. /**
  145. * Get style by name
  146. *
  147. * @param string $styleName
  148. * @return \PhpOffice\PhpWord\Style\AbstractStyle Paragraph|Font|Table|Numbering
  149. */
  150. public static function getStyle($styleName)
  151. {
  152. if (isset(self::$styles[$styleName])) {
  153. return self::$styles[$styleName];
  154. } else {
  155. return null;
  156. }
  157. }
  158. /**
  159. * Set style values and put it to static style collection
  160. *
  161. * The $styleValues could be an array or object
  162. *
  163. * @param string $name
  164. * @param \PhpOffice\PhpWord\Style\AbstractStyle $style
  165. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $value
  166. * @return \PhpOffice\PhpWord\Style\AbstractStyle
  167. */
  168. private static function setStyleValues($name, $style, $value = null)
  169. {
  170. if (!isset(self::$styles[$name])) {
  171. if ($value !== null) {
  172. if (is_array($value)) {
  173. $style->setStyleByArray($value);
  174. } elseif ($value instanceof AbstractStyle) {
  175. if (get_class($style) == get_class($value)) {
  176. $style = $value;
  177. }
  178. }
  179. }
  180. $style->setStyleName($name);
  181. $style->setIndex(self::countStyles() + 1); // One based index
  182. self::$styles[$name] = $style;
  183. }
  184. return self::getStyle($name);
  185. }
  186. }