Paragraph.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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\Style;
  18. use PhpOffice\PhpWord\Exception\InvalidStyleException;
  19. use PhpOffice\PhpWord\Shared\Text;
  20. use PhpOffice\PhpWord\SimpleType\Jc;
  21. use PhpOffice\PhpWord\SimpleType\TextAlignment;
  22. /**
  23. * Paragraph style
  24. *
  25. * OOXML:
  26. * - General: alignment, outline level
  27. * - Indentation: left, right, firstline, hanging
  28. * - Spacing: before, after, line spacing
  29. * - Pagination: widow control, keep next, keep line, page break before
  30. * - Formatting exception: suppress line numbers, don't hyphenate
  31. * - Textbox options
  32. * - Tabs
  33. * - Shading
  34. * - Borders
  35. *
  36. * OpenOffice:
  37. * - Indents & spacing
  38. * - Alignment
  39. * - Text flow
  40. * - Outline & numbering
  41. * - Tabs
  42. * - Dropcaps
  43. * - Tabs
  44. * - Borders
  45. * - Background
  46. *
  47. * @see http://www.schemacentral.com/sc/ooxml/t-w_CT_PPr.html
  48. */
  49. class Paragraph extends Border
  50. {
  51. /**
  52. * @const int One line height equals 240 twip
  53. */
  54. const LINE_HEIGHT = 240;
  55. /**
  56. * Aliases
  57. *
  58. * @var array
  59. */
  60. protected $aliases = array('line-height' => 'lineHeight', 'line-spacing' => 'spacing');
  61. /**
  62. * Parent style
  63. *
  64. * @var string
  65. */
  66. private $basedOn = 'Normal';
  67. /**
  68. * Style for next paragraph
  69. *
  70. * @var string
  71. */
  72. private $next;
  73. /**
  74. * @var string
  75. */
  76. private $alignment = '';
  77. /**
  78. * Indentation
  79. *
  80. * @var \PhpOffice\PhpWord\Style\Indentation|null
  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. * Ignore Spacing Above and Below When Using Identical Styles
  145. *
  146. * @var bool
  147. */
  148. private $contextualSpacing = false;
  149. /**
  150. * Right to Left Paragraph Layout
  151. *
  152. * @var bool
  153. */
  154. private $bidi = false;
  155. /**
  156. * Vertical Character Alignment on Line
  157. *
  158. * @var string
  159. */
  160. private $textAlignment;
  161. /**
  162. * Suppress hyphenation for paragraph
  163. *
  164. * @var bool
  165. */
  166. private $suppressAutoHyphens = false;
  167. /**
  168. * Set Style value
  169. *
  170. * @param string $key
  171. * @param mixed $value
  172. * @return self
  173. */
  174. public function setStyleValue($key, $value)
  175. {
  176. $key = Text::removeUnderscorePrefix($key);
  177. if ('indent' == $key || 'hanging' == $key) {
  178. $value = $value * 720; // 720 twips is 0.5 inch
  179. }
  180. return parent::setStyleValue($key, $value);
  181. }
  182. /**
  183. * Get style values
  184. *
  185. * An experiment to retrieve all style values in one function. This will
  186. * reduce function call and increase cohesion between functions. Should be
  187. * implemented in all styles.
  188. *
  189. * @ignoreScrutinizerPatch
  190. * @return array
  191. */
  192. public function getStyleValues()
  193. {
  194. $styles = array(
  195. 'name' => $this->getStyleName(),
  196. 'basedOn' => $this->getBasedOn(),
  197. 'next' => $this->getNext(),
  198. 'alignment' => $this->getAlignment(),
  199. 'indentation' => $this->getIndentation(),
  200. 'spacing' => $this->getSpace(),
  201. 'pagination' => array(
  202. 'widowControl' => $this->hasWidowControl(),
  203. 'keepNext' => $this->isKeepNext(),
  204. 'keepLines' => $this->isKeepLines(),
  205. 'pageBreak' => $this->hasPageBreakBefore(),
  206. ),
  207. 'numbering' => array(
  208. 'style' => $this->getNumStyle(),
  209. 'level' => $this->getNumLevel(),
  210. ),
  211. 'tabs' => $this->getTabs(),
  212. 'shading' => $this->getShading(),
  213. 'contextualSpacing' => $this->hasContextualSpacing(),
  214. 'bidi' => $this->isBidi(),
  215. 'textAlignment' => $this->getTextAlignment(),
  216. 'suppressAutoHyphens' => $this->hasSuppressAutoHyphens(),
  217. );
  218. return $styles;
  219. }
  220. /**
  221. * @since 0.13.0
  222. *
  223. * @return string
  224. */
  225. public function getAlignment()
  226. {
  227. return $this->alignment;
  228. }
  229. /**
  230. * @since 0.13.0
  231. *
  232. * @param string $value
  233. *
  234. * @return self
  235. */
  236. public function setAlignment($value)
  237. {
  238. if (Jc::isValid($value)) {
  239. $this->alignment = $value;
  240. }
  241. return $this;
  242. }
  243. /**
  244. * @deprecated 0.13.0 Use the `getAlignment` method instead.
  245. *
  246. * @return string
  247. *
  248. * @codeCoverageIgnore
  249. */
  250. public function getAlign()
  251. {
  252. return $this->getAlignment();
  253. }
  254. /**
  255. * @deprecated 0.13.0 Use the `setAlignment` method instead.
  256. *
  257. * @param string $value
  258. *
  259. * @return self
  260. *
  261. * @codeCoverageIgnore
  262. */
  263. public function setAlign($value = null)
  264. {
  265. return $this->setAlignment($value);
  266. }
  267. /**
  268. * Get parent style ID
  269. *
  270. * @return string
  271. */
  272. public function getBasedOn()
  273. {
  274. return $this->basedOn;
  275. }
  276. /**
  277. * Set parent style ID
  278. *
  279. * @param string $value
  280. * @return self
  281. */
  282. public function setBasedOn($value = 'Normal')
  283. {
  284. $this->basedOn = $value;
  285. return $this;
  286. }
  287. /**
  288. * Get style for next paragraph
  289. *
  290. * @return string
  291. */
  292. public function getNext()
  293. {
  294. return $this->next;
  295. }
  296. /**
  297. * Set style for next paragraph
  298. *
  299. * @param string $value
  300. * @return self
  301. */
  302. public function setNext($value = null)
  303. {
  304. $this->next = $value;
  305. return $this;
  306. }
  307. /**
  308. * Get shading
  309. *
  310. * @return \PhpOffice\PhpWord\Style\Indentation
  311. */
  312. public function getIndentation()
  313. {
  314. return $this->indentation;
  315. }
  316. /**
  317. * Set shading
  318. *
  319. * @param mixed $value
  320. * @return self
  321. */
  322. public function setIndentation($value = null)
  323. {
  324. $this->setObjectVal($value, 'Indentation', $this->indentation);
  325. return $this;
  326. }
  327. /**
  328. * Get indentation
  329. *
  330. * @return int
  331. */
  332. public function getIndent()
  333. {
  334. return $this->getChildStyleValue($this->indentation, 'left');
  335. }
  336. /**
  337. * Set indentation
  338. *
  339. * @param int $value
  340. * @return self
  341. */
  342. public function setIndent($value = null)
  343. {
  344. return $this->setIndentation(array('left' => $value));
  345. }
  346. /**
  347. * Get hanging
  348. *
  349. * @return int
  350. */
  351. public function getHanging()
  352. {
  353. return $this->getChildStyleValue($this->indentation, 'hanging');
  354. }
  355. /**
  356. * Set hanging
  357. *
  358. * @param int $value
  359. * @return self
  360. */
  361. public function setHanging($value = null)
  362. {
  363. return $this->setIndentation(array('hanging' => $value));
  364. }
  365. /**
  366. * Get spacing
  367. *
  368. * @return \PhpOffice\PhpWord\Style\Spacing
  369. * @todo Rename to getSpacing in 1.0
  370. */
  371. public function getSpace()
  372. {
  373. return $this->spacing;
  374. }
  375. /**
  376. * Set spacing
  377. *
  378. * @param mixed $value
  379. * @return self
  380. * @todo Rename to setSpacing in 1.0
  381. */
  382. public function setSpace($value = null)
  383. {
  384. $this->setObjectVal($value, 'Spacing', $this->spacing);
  385. return $this;
  386. }
  387. /**
  388. * Get space before paragraph
  389. *
  390. * @return int
  391. */
  392. public function getSpaceBefore()
  393. {
  394. return $this->getChildStyleValue($this->spacing, 'before');
  395. }
  396. /**
  397. * Set space before paragraph
  398. *
  399. * @param int $value
  400. * @return self
  401. */
  402. public function setSpaceBefore($value = null)
  403. {
  404. return $this->setSpace(array('before' => $value));
  405. }
  406. /**
  407. * Get space after paragraph
  408. *
  409. * @return int
  410. */
  411. public function getSpaceAfter()
  412. {
  413. return $this->getChildStyleValue($this->spacing, 'after');
  414. }
  415. /**
  416. * Set space after paragraph
  417. *
  418. * @param int $value
  419. * @return self
  420. */
  421. public function setSpaceAfter($value = null)
  422. {
  423. return $this->setSpace(array('after' => $value));
  424. }
  425. /**
  426. * Get spacing between lines
  427. *
  428. * @return int|float
  429. */
  430. public function getSpacing()
  431. {
  432. return $this->getChildStyleValue($this->spacing, 'line');
  433. }
  434. /**
  435. * Set spacing between lines
  436. *
  437. * @param int|float $value
  438. * @return self
  439. */
  440. public function setSpacing($value = null)
  441. {
  442. return $this->setSpace(array('line' => $value));
  443. }
  444. /**
  445. * Get spacing line rule
  446. *
  447. * @return string
  448. */
  449. public function getSpacingLineRule()
  450. {
  451. return $this->getChildStyleValue($this->spacing, 'lineRule');
  452. }
  453. /**
  454. * Set the spacing line rule
  455. *
  456. * @param string $value Possible values are defined in LineSpacingRule
  457. * @return \PhpOffice\PhpWord\Style\Paragraph
  458. */
  459. public function setSpacingLineRule($value)
  460. {
  461. return $this->setSpace(array('lineRule' => $value));
  462. }
  463. /**
  464. * Get line height
  465. *
  466. * @return int|float
  467. */
  468. public function getLineHeight()
  469. {
  470. return $this->lineHeight;
  471. }
  472. /**
  473. * Set the line height
  474. *
  475. * @param int|float|string $lineHeight
  476. *
  477. * @throws \PhpOffice\PhpWord\Exception\InvalidStyleException
  478. * @return self
  479. */
  480. public function setLineHeight($lineHeight)
  481. {
  482. if (is_string($lineHeight)) {
  483. $lineHeight = (float) (preg_replace('/[^0-9\.\,]/', '', $lineHeight));
  484. }
  485. if ((!is_int($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
  486. throw new InvalidStyleException('Line height must be a valid number');
  487. }
  488. $this->lineHeight = $lineHeight;
  489. $this->setSpacing(($lineHeight - 1) * self::LINE_HEIGHT);
  490. $this->setSpacingLineRule(\PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO);
  491. return $this;
  492. }
  493. /**
  494. * Get allow first/last line to display on a separate page setting
  495. *
  496. * @return bool
  497. */
  498. public function hasWidowControl()
  499. {
  500. return $this->widowControl;
  501. }
  502. /**
  503. * Set keep paragraph with next paragraph setting
  504. *
  505. * @param bool $value
  506. * @return self
  507. */
  508. public function setWidowControl($value = true)
  509. {
  510. $this->widowControl = $this->setBoolVal($value, $this->widowControl);
  511. return $this;
  512. }
  513. /**
  514. * Get keep paragraph with next paragraph setting
  515. *
  516. * @return bool
  517. */
  518. public function isKeepNext()
  519. {
  520. return $this->keepNext;
  521. }
  522. /**
  523. * Set keep paragraph with next paragraph setting
  524. *
  525. * @param bool $value
  526. * @return self
  527. */
  528. public function setKeepNext($value = true)
  529. {
  530. $this->keepNext = $this->setBoolVal($value, $this->keepNext);
  531. return $this;
  532. }
  533. /**
  534. * Get keep all lines on one page setting
  535. *
  536. * @return bool
  537. */
  538. public function isKeepLines()
  539. {
  540. return $this->keepLines;
  541. }
  542. /**
  543. * Set keep all lines on one page setting
  544. *
  545. * @param bool $value
  546. * @return self
  547. */
  548. public function setKeepLines($value = true)
  549. {
  550. $this->keepLines = $this->setBoolVal($value, $this->keepLines);
  551. return $this;
  552. }
  553. /**
  554. * Get start paragraph on next page setting
  555. *
  556. * @return bool
  557. */
  558. public function hasPageBreakBefore()
  559. {
  560. return $this->pageBreakBefore;
  561. }
  562. /**
  563. * Set start paragraph on next page setting
  564. *
  565. * @param bool $value
  566. * @return self
  567. */
  568. public function setPageBreakBefore($value = true)
  569. {
  570. $this->pageBreakBefore = $this->setBoolVal($value, $this->pageBreakBefore);
  571. return $this;
  572. }
  573. /**
  574. * Get numbering style name
  575. *
  576. * @return string
  577. */
  578. public function getNumStyle()
  579. {
  580. return $this->numStyle;
  581. }
  582. /**
  583. * Set numbering style name
  584. *
  585. * @param string $value
  586. * @return self
  587. */
  588. public function setNumStyle($value)
  589. {
  590. $this->numStyle = $value;
  591. return $this;
  592. }
  593. /**
  594. * Get numbering level
  595. *
  596. * @return int
  597. */
  598. public function getNumLevel()
  599. {
  600. return $this->numLevel;
  601. }
  602. /**
  603. * Set numbering level
  604. *
  605. * @param int $value
  606. * @return self
  607. */
  608. public function setNumLevel($value = 0)
  609. {
  610. $this->numLevel = $this->setIntVal($value, $this->numLevel);
  611. return $this;
  612. }
  613. /**
  614. * Get tabs
  615. *
  616. * @return \PhpOffice\PhpWord\Style\Tab[]
  617. */
  618. public function getTabs()
  619. {
  620. return $this->tabs;
  621. }
  622. /**
  623. * Set tabs
  624. *
  625. * @param array $value
  626. * @return self
  627. */
  628. public function setTabs($value = null)
  629. {
  630. if (is_array($value)) {
  631. $this->tabs = $value;
  632. }
  633. return $this;
  634. }
  635. /**
  636. * Get allow first/last line to display on a separate page setting
  637. *
  638. * @deprecated 0.10.0
  639. *
  640. * @codeCoverageIgnore
  641. */
  642. public function getWidowControl()
  643. {
  644. return $this->hasWidowControl();
  645. }
  646. /**
  647. * Get keep paragraph with next paragraph setting
  648. *
  649. * @deprecated 0.10.0
  650. *
  651. * @codeCoverageIgnore
  652. */
  653. public function getKeepNext()
  654. {
  655. return $this->isKeepNext();
  656. }
  657. /**
  658. * Get keep all lines on one page setting
  659. *
  660. * @deprecated 0.10.0
  661. *
  662. * @codeCoverageIgnore
  663. */
  664. public function getKeepLines()
  665. {
  666. return $this->isKeepLines();
  667. }
  668. /**
  669. * Get start paragraph on next page setting
  670. *
  671. * @deprecated 0.10.0
  672. *
  673. * @codeCoverageIgnore
  674. */
  675. public function getPageBreakBefore()
  676. {
  677. return $this->hasPageBreakBefore();
  678. }
  679. /**
  680. * Get shading
  681. *
  682. * @return \PhpOffice\PhpWord\Style\Shading
  683. */
  684. public function getShading()
  685. {
  686. return $this->shading;
  687. }
  688. /**
  689. * Set shading
  690. *
  691. * @param mixed $value
  692. * @return self
  693. */
  694. public function setShading($value = null)
  695. {
  696. $this->setObjectVal($value, 'Shading', $this->shading);
  697. return $this;
  698. }
  699. /**
  700. * Get contextualSpacing
  701. *
  702. * @return bool
  703. */
  704. public function hasContextualSpacing()
  705. {
  706. return $this->contextualSpacing;
  707. }
  708. /**
  709. * Set contextualSpacing
  710. *
  711. * @param bool $contextualSpacing
  712. * @return self
  713. */
  714. public function setContextualSpacing($contextualSpacing)
  715. {
  716. $this->contextualSpacing = $contextualSpacing;
  717. return $this;
  718. }
  719. /**
  720. * Get bidirectional
  721. *
  722. * @return bool
  723. */
  724. public function isBidi()
  725. {
  726. return $this->bidi;
  727. }
  728. /**
  729. * Set bidi
  730. *
  731. * @param bool $bidi
  732. * Set to true to write from right to left
  733. * @return self
  734. */
  735. public function setBidi($bidi)
  736. {
  737. $this->bidi = $bidi;
  738. return $this;
  739. }
  740. /**
  741. * Get textAlignment
  742. *
  743. * @return string
  744. */
  745. public function getTextAlignment()
  746. {
  747. return $this->textAlignment;
  748. }
  749. /**
  750. * Set textAlignment
  751. *
  752. * @param string $textAlignment
  753. * @return self
  754. */
  755. public function setTextAlignment($textAlignment)
  756. {
  757. TextAlignment::validate($textAlignment);
  758. $this->textAlignment = $textAlignment;
  759. return $this;
  760. }
  761. /**
  762. * @return bool
  763. */
  764. public function hasSuppressAutoHyphens()
  765. {
  766. return $this->suppressAutoHyphens;
  767. }
  768. /**
  769. * @param bool $suppressAutoHyphens
  770. */
  771. public function setSuppressAutoHyphens($suppressAutoHyphens)
  772. {
  773. $this->suppressAutoHyphens = (bool) $suppressAutoHyphens;
  774. }
  775. }