Aucune description

Paragraph.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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\Exception\InvalidStyleException;
  19. use PhpOffice\PhpWord\Shared\String;
  20. /**
  21. * Paragraph style
  22. *
  23. * OOXML:
  24. * - General: alignment, outline level
  25. * - Indentation: left, right, firstline, hanging
  26. * - Spacing: before, after, line spacing
  27. * - Pagination: widow control, keep next, keep line, page break before
  28. * - Formatting exception: suppress line numbers, don't hyphenate
  29. * - Textbox options
  30. * - Tabs
  31. * - Shading
  32. * - Borders
  33. *
  34. * OpenOffice:
  35. * - Indents & spacing
  36. * - Alignment
  37. * - Text flow
  38. * - Outline & numbering
  39. * - Tabs
  40. * - Dropcaps
  41. * - Tabs
  42. * - Borders
  43. * - Background
  44. *
  45. * @link http://www.schemacentral.com/sc/ooxml/t-w_CT_PPr.html
  46. */
  47. class Paragraph extends Border
  48. {
  49. /**
  50. * @const int One line height equals 240 twip
  51. */
  52. const LINE_HEIGHT = 240;
  53. /**
  54. * Aliases
  55. *
  56. * @var array
  57. */
  58. protected $aliases = array('line-height' => 'lineHeight');
  59. /**
  60. * Parent style
  61. *
  62. * @var string
  63. */
  64. private $basedOn = 'Normal';
  65. /**
  66. * Style for next paragraph
  67. *
  68. * @var string
  69. */
  70. private $next;
  71. /**
  72. * Alignment
  73. *
  74. * @var \PhpOffice\PhpWord\Style\Alignment
  75. */
  76. private $alignment;
  77. /**
  78. * Indentation
  79. *
  80. * @var \PhpOffice\PhpWord\Style\Indentation
  81. */
  82. private $indentation;
  83. /**
  84. * Spacing
  85. *
  86. * @var \PhpOffice\PhpWord\Style\Spacing
  87. */
  88. private $spacing;
  89. /**
  90. * Text line height
  91. *
  92. * @var int
  93. */
  94. private $lineHeight;
  95. /**
  96. * Allow first/last line to display on a separate page
  97. *
  98. * @var bool
  99. */
  100. private $widowControl = true;
  101. /**
  102. * Keep paragraph with next paragraph
  103. *
  104. * @var bool
  105. */
  106. private $keepNext = false;
  107. /**
  108. * Keep all lines on one page
  109. *
  110. * @var bool
  111. */
  112. private $keepLines = false;
  113. /**
  114. * Start paragraph on next page
  115. *
  116. * @var bool
  117. */
  118. private $pageBreakBefore = false;
  119. /**
  120. * Numbering style name
  121. *
  122. * @var string
  123. */
  124. private $numStyle;
  125. /**
  126. * Numbering level
  127. *
  128. * @var int
  129. */
  130. private $numLevel = 0;
  131. /**
  132. * Set of Custom Tab Stops
  133. *
  134. * @var \PhpOffice\PhpWord\Style\Tab[]
  135. */
  136. private $tabs = array();
  137. /**
  138. * Shading
  139. *
  140. * @var \PhpOffice\PhpWord\Style\Shading
  141. */
  142. private $shading;
  143. /**
  144. * Create new instance
  145. */
  146. public function __construct()
  147. {
  148. $this->alignment = new Alignment();
  149. }
  150. /**
  151. * Set Style value
  152. *
  153. * @param string $key
  154. * @param mixed $value
  155. * @return self
  156. */
  157. public function setStyleValue($key, $value)
  158. {
  159. $key = String::removeUnderscorePrefix($key);
  160. if ($key == 'indent' || $key == 'hanging') {
  161. $value = $value * 720;
  162. } elseif ($key == 'spacing') {
  163. $value += 240; // because line height of 1 matches 240 twips
  164. }
  165. return parent::setStyleValue($key, $value);
  166. }
  167. /**
  168. * Get style values
  169. *
  170. * An experiment to retrieve all style values in one function. This will
  171. * reduce function call and increase cohesion between functions. Should be
  172. * implemented in all styles.
  173. *
  174. * @ignoreScrutinizerPatch
  175. * @return array
  176. */
  177. public function getStyleValues()
  178. {
  179. $styles = array(
  180. 'name' => $this->getStyleName(),
  181. 'basedOn' => $this->getBasedOn(),
  182. 'next' => $this->getNext(),
  183. 'alignment' => $this->getAlign(),
  184. 'indentation' => $this->getIndentation(),
  185. 'spacing' => $this->getSpace(),
  186. 'pagination' => array(
  187. 'widowControl' => $this->hasWidowControl(),
  188. 'keepNext' => $this->isKeepNext(),
  189. 'keepLines' => $this->isKeepLines(),
  190. 'pageBreak' => $this->hasPageBreakBefore(),
  191. ),
  192. 'numbering' => array(
  193. 'style' => $this->getNumStyle(),
  194. 'level' => $this->getNumLevel(),
  195. ),
  196. 'tabs' => $this->getTabs(),
  197. 'shading' => $this->getShading(),
  198. );
  199. return $styles;
  200. }
  201. /**
  202. * Get alignment
  203. *
  204. * @return string
  205. */
  206. public function getAlign()
  207. {
  208. return $this->alignment->getValue();
  209. }
  210. /**
  211. * Set alignment
  212. *
  213. * @param string $value
  214. * @return self
  215. */
  216. public function setAlign($value = null)
  217. {
  218. $this->alignment->setValue($value);
  219. return $this;
  220. }
  221. /**
  222. * Get parent style ID
  223. *
  224. * @return string
  225. */
  226. public function getBasedOn()
  227. {
  228. return $this->basedOn;
  229. }
  230. /**
  231. * Set parent style ID
  232. *
  233. * @param string $value
  234. * @return self
  235. */
  236. public function setBasedOn($value = 'Normal')
  237. {
  238. $this->basedOn = $value;
  239. return $this;
  240. }
  241. /**
  242. * Get style for next paragraph
  243. *
  244. * @return string
  245. */
  246. public function getNext()
  247. {
  248. return $this->next;
  249. }
  250. /**
  251. * Set style for next paragraph
  252. *
  253. * @param string $value
  254. * @return self
  255. */
  256. public function setNext($value = null)
  257. {
  258. $this->next = $value;
  259. return $this;
  260. }
  261. /**
  262. * Get shading
  263. *
  264. * @return \PhpOffice\PhpWord\Style\Indentation
  265. */
  266. public function getIndentation()
  267. {
  268. return $this->indentation;
  269. }
  270. /**
  271. * Set shading
  272. *
  273. * @param mixed $value
  274. * @return self
  275. */
  276. public function setIndentation($value = null)
  277. {
  278. $this->setObjectVal($value, 'Indentation', $this->indentation);
  279. return $this;
  280. }
  281. /**
  282. * Get indentation
  283. *
  284. * @return int
  285. */
  286. public function getIndent()
  287. {
  288. return $this->getChildStyleValue($this->indentation, 'left');
  289. }
  290. /**
  291. * Set indentation
  292. *
  293. * @param int $value
  294. * @return self
  295. */
  296. public function setIndent($value = null)
  297. {
  298. return $this->setIndentation(array('left' => $value));
  299. }
  300. /**
  301. * Get hanging
  302. *
  303. * @return int
  304. */
  305. public function getHanging()
  306. {
  307. return $this->getChildStyleValue($this->indentation, 'hanging');
  308. }
  309. /**
  310. * Set hanging
  311. *
  312. * @param int $value
  313. * @return self
  314. */
  315. public function setHanging($value = null)
  316. {
  317. return $this->setIndentation(array('hanging' => $value));
  318. }
  319. /**
  320. * Get spacing
  321. *
  322. * @return \PhpOffice\PhpWord\Style\Spacing
  323. * @todo Rename to getSpacing in 1.0
  324. */
  325. public function getSpace()
  326. {
  327. return $this->spacing;
  328. }
  329. /**
  330. * Set spacing
  331. *
  332. * @param mixed $value
  333. * @return self
  334. * @todo Rename to setSpacing in 1.0
  335. */
  336. public function setSpace($value = null)
  337. {
  338. $this->setObjectVal($value, 'Spacing', $this->spacing);
  339. return $this;
  340. }
  341. /**
  342. * Get space before paragraph
  343. *
  344. * @return integer
  345. */
  346. public function getSpaceBefore()
  347. {
  348. return $this->getChildStyleValue($this->spacing, 'before');
  349. }
  350. /**
  351. * Set space before paragraph
  352. *
  353. * @param int $value
  354. * @return self
  355. */
  356. public function setSpaceBefore($value = null)
  357. {
  358. return $this->setSpace(array('before' => $value));
  359. }
  360. /**
  361. * Get space after paragraph
  362. *
  363. * @return integer
  364. */
  365. public function getSpaceAfter()
  366. {
  367. return $this->getChildStyleValue($this->spacing, 'after');
  368. }
  369. /**
  370. * Set space after paragraph
  371. *
  372. * @param int $value
  373. * @return self
  374. */
  375. public function setSpaceAfter($value = null)
  376. {
  377. return $this->setSpace(array('after' => $value));
  378. }
  379. /**
  380. * Get spacing between lines
  381. *
  382. * @return int
  383. */
  384. public function getSpacing()
  385. {
  386. return $this->getChildStyleValue($this->spacing, 'line');
  387. }
  388. /**
  389. * Set spacing between lines
  390. *
  391. * @param int $value
  392. * @return self
  393. */
  394. public function setSpacing($value = null)
  395. {
  396. return $this->setSpace(array('line' => $value));
  397. }
  398. /**
  399. * Get line height
  400. *
  401. * @return int|float
  402. */
  403. public function getLineHeight()
  404. {
  405. return $this->lineHeight;
  406. }
  407. /**
  408. * Set the line height
  409. *
  410. * @param int|float|string $lineHeight
  411. * @return self
  412. * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException
  413. */
  414. public function setLineHeight($lineHeight)
  415. {
  416. if (is_string($lineHeight)) {
  417. $lineHeight = floatval(preg_replace('/[^0-9\.\,]/', '', $lineHeight));
  418. }
  419. if ((!is_integer($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
  420. throw new InvalidStyleException('Line height must be a valid number');
  421. }
  422. $this->lineHeight = $lineHeight;
  423. $this->setSpacing($lineHeight * self::LINE_HEIGHT);
  424. return $this;
  425. }
  426. /**
  427. * Get allow first/last line to display on a separate page setting
  428. *
  429. * @return bool
  430. */
  431. public function hasWidowControl()
  432. {
  433. return $this->widowControl;
  434. }
  435. /**
  436. * Set keep paragraph with next paragraph setting
  437. *
  438. * @param bool $value
  439. * @return self
  440. */
  441. public function setWidowControl($value = true)
  442. {
  443. $this->widowControl = $this->setBoolVal($value, $this->widowControl);
  444. return $this;
  445. }
  446. /**
  447. * Get keep paragraph with next paragraph setting
  448. *
  449. * @return bool
  450. */
  451. public function isKeepNext()
  452. {
  453. return $this->keepNext;
  454. }
  455. /**
  456. * Set keep paragraph with next paragraph setting
  457. *
  458. * @param bool $value
  459. * @return self
  460. */
  461. public function setKeepNext($value = true)
  462. {
  463. $this->keepNext = $this->setBoolVal($value, $this->keepNext);
  464. return $this;
  465. }
  466. /**
  467. * Get keep all lines on one page setting
  468. *
  469. * @return bool
  470. */
  471. public function isKeepLines()
  472. {
  473. return $this->keepLines;
  474. }
  475. /**
  476. * Set keep all lines on one page setting
  477. *
  478. * @param bool $value
  479. * @return self
  480. */
  481. public function setKeepLines($value = true)
  482. {
  483. $this->keepLines = $this->setBoolVal($value, $this->keepLines);
  484. return $this;
  485. }
  486. /**
  487. * Get start paragraph on next page setting
  488. *
  489. * @return bool
  490. */
  491. public function hasPageBreakBefore()
  492. {
  493. return $this->pageBreakBefore;
  494. }
  495. /**
  496. * Set start paragraph on next page setting
  497. *
  498. * @param bool $value
  499. * @return self
  500. */
  501. public function setPageBreakBefore($value = true)
  502. {
  503. $this->pageBreakBefore = $this->setBoolVal($value, $this->pageBreakBefore);
  504. return $this;
  505. }
  506. /**
  507. * Get numbering style name
  508. *
  509. * @return string
  510. */
  511. public function getNumStyle()
  512. {
  513. return $this->numStyle;
  514. }
  515. /**
  516. * Set numbering style name
  517. *
  518. * @param string $value
  519. * @return self
  520. */
  521. public function setNumStyle($value)
  522. {
  523. $this->numStyle = $value;
  524. return $this;
  525. }
  526. /**
  527. * Get numbering level
  528. *
  529. * @return int
  530. */
  531. public function getNumLevel()
  532. {
  533. return $this->numLevel;
  534. }
  535. /**
  536. * Set numbering level
  537. *
  538. * @param int $value
  539. * @return self
  540. */
  541. public function setNumLevel($value = 0)
  542. {
  543. $this->numLevel = $this->setIntVal($value, $this->numLevel);
  544. return $this;
  545. }
  546. /**
  547. * Get tabs
  548. *
  549. * @return \PhpOffice\PhpWord\Style\Tab[]
  550. */
  551. public function getTabs()
  552. {
  553. return $this->tabs;
  554. }
  555. /**
  556. * Set tabs
  557. *
  558. * @param array $value
  559. * @return self
  560. */
  561. public function setTabs($value = null)
  562. {
  563. if (is_array($value)) {
  564. $this->tabs = $value;
  565. }
  566. return $this;
  567. }
  568. /**
  569. * Get allow first/last line to display on a separate page setting
  570. *
  571. * @deprecated 0.10.0
  572. * @codeCoverageIgnore
  573. */
  574. public function getWidowControl()
  575. {
  576. return $this->hasWidowControl();
  577. }
  578. /**
  579. * Get keep paragraph with next paragraph setting
  580. *
  581. * @deprecated 0.10.0
  582. * @codeCoverageIgnore
  583. */
  584. public function getKeepNext()
  585. {
  586. return $this->isKeepNext();
  587. }
  588. /**
  589. * Get keep all lines on one page setting
  590. *
  591. * @deprecated 0.10.0
  592. * @codeCoverageIgnore
  593. */
  594. public function getKeepLines()
  595. {
  596. return $this->isKeepLines();
  597. }
  598. /**
  599. * Get start paragraph on next page setting
  600. *
  601. * @deprecated 0.10.0
  602. * @codeCoverageIgnore
  603. */
  604. public function getPageBreakBefore()
  605. {
  606. return $this->hasPageBreakBefore();
  607. }
  608. /**
  609. * Get shading
  610. *
  611. * @return \PhpOffice\PhpWord\Style\Shading
  612. */
  613. public function getShading()
  614. {
  615. return $this->shading;
  616. }
  617. /**
  618. * Set shading
  619. *
  620. * @param mixed $value
  621. * @return self
  622. */
  623. public function setShading($value = null)
  624. {
  625. $this->setObjectVal($value, 'Shading', $this->shading);
  626. return $this;
  627. }
  628. }