暫無描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Style;
  18. use PhpOffice\PhpWord\Style;
  19. /**
  20. * List item style
  21. *
  22. * Before version 0.10.0, numbering style is defined statically with $listType.
  23. * After version 0.10.0, numbering style is defined by using Numbering and
  24. * recorded by $numStyle. $listStyle is maintained for backward compatility
  25. */
  26. class ListItem extends AbstractStyle
  27. {
  28. const TYPE_SQUARE_FILLED = 1;
  29. const TYPE_BULLET_FILLED = 3; // default
  30. const TYPE_BULLET_EMPTY = 5;
  31. const TYPE_NUMBER = 7;
  32. const TYPE_NUMBER_NESTED = 8;
  33. const TYPE_ALPHANUM = 9;
  34. /**
  35. * Legacy list type
  36. *
  37. * @var integer
  38. */
  39. private $listType;
  40. /**
  41. * Numbering style name
  42. *
  43. * @var string
  44. * @since 0.10.0
  45. */
  46. private $numStyle;
  47. /**
  48. * Numbering definition instance ID
  49. *
  50. * @var integer
  51. * @since 0.10.0
  52. */
  53. private $numId;
  54. /**
  55. * Create new instance
  56. *
  57. * @param string $numStyle
  58. */
  59. public function __construct($numStyle = null)
  60. {
  61. if ($numStyle !== null) {
  62. $this->setNumStyle($numStyle);
  63. } else {
  64. $this->setListType();
  65. }
  66. }
  67. /**
  68. * Get List Type
  69. *
  70. * @return integer
  71. */
  72. public function getListType()
  73. {
  74. return $this->listType;
  75. }
  76. /**
  77. * Set legacy list type for version < 0.10.0
  78. *
  79. * @param integer $value
  80. * @return self
  81. */
  82. public function setListType($value = self::TYPE_BULLET_FILLED)
  83. {
  84. $enum = array(
  85. self::TYPE_SQUARE_FILLED, self::TYPE_BULLET_FILLED,
  86. self::TYPE_BULLET_EMPTY, self::TYPE_NUMBER,
  87. self::TYPE_NUMBER_NESTED, self::TYPE_ALPHANUM
  88. );
  89. $this->listType = $this->setEnumVal($value, $enum, $this->listType);
  90. $this->getListTypeStyle();
  91. return $this;
  92. }
  93. /**
  94. * Get numbering style name
  95. *
  96. * @return string
  97. */
  98. public function getNumStyle()
  99. {
  100. return $this->numStyle;
  101. }
  102. /**
  103. * Set numbering style name
  104. *
  105. * @param string $value
  106. * @return self
  107. */
  108. public function setNumStyle($value)
  109. {
  110. $this->numStyle = $value;
  111. $numStyleObject = Style::getStyle($this->numStyle);
  112. if ($numStyleObject instanceof Numbering) {
  113. $this->numId = $numStyleObject->getIndex();
  114. $numStyleObject->setNumId($this->numId);
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Get numbering Id
  120. *
  121. * @return integer
  122. */
  123. public function getNumId()
  124. {
  125. return $this->numId;
  126. }
  127. /**
  128. * Get legacy numbering definition
  129. *
  130. * @return array
  131. * @since 0.10.0
  132. */
  133. private function getListTypeStyle()
  134. {
  135. // Check if legacy style already registered in global Style collection
  136. $numStyle = "PHPWordList{$this->listType}";
  137. if (Style::getStyle($numStyle) !== null) {
  138. $this->setNumStyle($numStyle);
  139. return;
  140. }
  141. // Property mapping for numbering level information
  142. $properties = array('start', 'format', 'text', 'align', 'tabPos', 'left', 'hanging', 'font', 'hint');
  143. // Legacy level information
  144. $listTypeStyles = array(
  145. self::TYPE_SQUARE_FILLED => array(
  146. 'type' => 'hybridMultilevel',
  147. 'levels' => array(
  148. 0 => '1, bullet, , left, 720, 720, 360, Wingdings, default',
  149. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  150. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  151. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  152. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  153. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  154. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  155. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  156. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  157. ),
  158. ),
  159. self::TYPE_BULLET_FILLED => array(
  160. 'type' => 'hybridMultilevel',
  161. 'levels' => array(
  162. 0 => '1, bullet, , left, 720, 720, 360, Symbol, default',
  163. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  164. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  165. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  166. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  167. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  168. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  169. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  170. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  171. ),
  172. ),
  173. self::TYPE_BULLET_EMPTY => array(
  174. 'type' => 'hybridMultilevel',
  175. 'levels' => array(
  176. 0 => '1, bullet, o, left, 720, 720, 360, Courier New, default',
  177. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  178. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  179. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  180. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  181. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  182. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  183. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  184. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  185. ),
  186. ),
  187. self::TYPE_NUMBER => array(
  188. 'type' => 'hybridMultilevel',
  189. 'levels' => array(
  190. 0 => '1, decimal, %1., left, 720, 720, 360, , default',
  191. 1 => '1, bullet, o, left, 1440, 1440, 360, Courier New, default',
  192. 2 => '1, bullet, , left, 2160, 2160, 360, Wingdings, default',
  193. 3 => '1, bullet, , left, 2880, 2880, 360, Symbol, default',
  194. 4 => '1, bullet, o, left, 3600, 3600, 360, Courier New, default',
  195. 5 => '1, bullet, , left, 4320, 4320, 360, Wingdings, default',
  196. 6 => '1, bullet, , left, 5040, 5040, 360, Symbol, default',
  197. 7 => '1, bullet, o, left, 5760, 5760, 360, Courier New, default',
  198. 8 => '1, bullet, , left, 6480, 6480, 360, Wingdings, default',
  199. ),
  200. ),
  201. self::TYPE_NUMBER_NESTED => array(
  202. 'type' => 'multilevel',
  203. 'levels' => array(
  204. 0 => '1, decimal, %1., left, 360, 360, 360, , ',
  205. 1 => '1, decimal, %1.%2., left, 792, 792, 432, , ',
  206. 2 => '1, decimal, %1.%2.%3., left, 1224, 1224, 504, , ',
  207. 3 => '1, decimal, %1.%2.%3.%4., left, 1800, 1728, 648, , ',
  208. 4 => '1, decimal, %1.%2.%3.%4.%5., left, 2520, 2232, 792, , ',
  209. 5 => '1, decimal, %1.%2.%3.%4.%5.%6., left, 2880, 2736, 936, , ',
  210. 6 => '1, decimal, %1.%2.%3.%4.%5.%6.%7., left, 3600, 3240, 1080, , ',
  211. 7 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8., left, 3960, 3744, 1224, , ',
  212. 8 => '1, decimal, %1.%2.%3.%4.%5.%6.%7.%8.%9., left, 4680, 4320, 1440, , ',
  213. ),
  214. ),
  215. self::TYPE_ALPHANUM => array(
  216. 'type' => 'multilevel',
  217. 'levels' => array(
  218. 0 => '1, decimal, %1., left, 720, 720, 360, , ',
  219. 1 => '1, lowerLetter, %2., left, 1440, 1440, 360, , ',
  220. 2 => '1, lowerRoman, %3., right, 2160, 2160, 180, , ',
  221. 3 => '1, decimal, %4., left, 2880, 2880, 360, , ',
  222. 4 => '1, lowerLetter, %5., left, 3600, 3600, 360, , ',
  223. 5 => '1, lowerRoman, %6., right, 4320, 4320, 180, , ',
  224. 6 => '1, decimal, %7., left, 5040, 5040, 360, , ',
  225. 7 => '1, lowerLetter, %8., left, 5760, 5760, 360, , ',
  226. 8 => '1, lowerRoman, %9., right, 6480, 6480, 180, , ',
  227. ),
  228. ),
  229. );
  230. // Populate style and register to global Style register
  231. $style = $listTypeStyles[$this->listType];
  232. foreach ($style['levels'] as $key => $value) {
  233. $level = array();
  234. $levelProperties = explode(', ', $value);
  235. $level['level'] = $key;
  236. for ($i = 0; $i < count($properties); $i++) {
  237. $property = $properties[$i];
  238. $level[$property] = $levelProperties[$i];
  239. }
  240. $style['levels'][$key] = $level;
  241. }
  242. Style::addNumberingStyle($numStyle, $style);
  243. $this->setNumStyle($numStyle);
  244. }
  245. }