Style.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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;
  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|\PhpOffice\PhpWord\Style\AbstractStyle $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|\PhpOffice\PhpWord\Style\AbstractStyle $fontStyle
  50. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $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|\PhpOffice\PhpWord\Style\AbstractStyle $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|\PhpOffice\PhpWord\Style\AbstractStyle $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|null $depth Provide null to set title font
  84. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $fontStyle
  85. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $paragraphStyle
  86. * @return \PhpOffice\PhpWord\Style\Font
  87. */
  88. public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
  89. {
  90. if (empty($depth)) {
  91. $styleName = 'Title';
  92. } else {
  93. $styleName = "Heading_{$depth}";
  94. }
  95. return self::setStyleValues($styleName, new Font('title', $paragraphStyle), $fontStyle);
  96. }
  97. /**
  98. * Add table style
  99. *
  100. * @param string $styleName
  101. * @param array $styleTable
  102. * @param array|null $styleFirstRow
  103. * @return \PhpOffice\PhpWord\Style\Table
  104. */
  105. public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
  106. {
  107. return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
  108. }
  109. /**
  110. * Count styles
  111. *
  112. * @return int
  113. * @since 0.10.0
  114. */
  115. public static function countStyles()
  116. {
  117. return count(self::$styles);
  118. }
  119. /**
  120. * Reset styles.
  121. *
  122. * @since 0.10.0
  123. */
  124. public static function resetStyles()
  125. {
  126. self::$styles = array();
  127. }
  128. /**
  129. * Set default paragraph style
  130. *
  131. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $styles Paragraph style definition
  132. * @return \PhpOffice\PhpWord\Style\Paragraph
  133. */
  134. public static function setDefaultParagraphStyle($styles)
  135. {
  136. return self::addParagraphStyle('Normal', $styles);
  137. }
  138. /**
  139. * Get all styles
  140. *
  141. * @return \PhpOffice\PhpWord\Style\AbstractStyle[]
  142. */
  143. public static function getStyles()
  144. {
  145. return self::$styles;
  146. }
  147. /**
  148. * Get style by name
  149. *
  150. * @param string $styleName
  151. * @return \PhpOffice\PhpWord\Style\AbstractStyle Paragraph|Font|Table|Numbering
  152. */
  153. public static function getStyle($styleName)
  154. {
  155. if (isset(self::$styles[$styleName])) {
  156. return self::$styles[$styleName];
  157. }
  158. return null;
  159. }
  160. /**
  161. * Set style values and put it to static style collection
  162. *
  163. * The $styleValues could be an array or object
  164. *
  165. * @param string $name
  166. * @param \PhpOffice\PhpWord\Style\AbstractStyle $style
  167. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $value
  168. * @return \PhpOffice\PhpWord\Style\AbstractStyle
  169. */
  170. private static function setStyleValues($name, $style, $value = null)
  171. {
  172. if (!isset(self::$styles[$name])) {
  173. if ($value !== null) {
  174. if (is_array($value)) {
  175. $style->setStyleByArray($value);
  176. } elseif ($value instanceof AbstractStyle) {
  177. if (get_class($style) == get_class($value)) {
  178. $style = $value;
  179. }
  180. }
  181. }
  182. $style->setStyleName($name);
  183. $style->setIndex(self::countStyles() + 1); // One based index
  184. self::$styles[$name] = $style;
  185. }
  186. return self::getStyle($name);
  187. }
  188. }