Section.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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\Settings;
  19. use PhpOffice\PhpWord\SimpleType\VerticalJc;
  20. /**
  21. * Section settings
  22. */
  23. class Section extends Border
  24. {
  25. /**
  26. * Page orientation
  27. *
  28. * @const string
  29. */
  30. const ORIENTATION_PORTRAIT = 'portrait';
  31. const ORIENTATION_LANDSCAPE = 'landscape';
  32. /**
  33. * Page default constants
  34. *
  35. * @const int|float
  36. */
  37. const DEFAULT_WIDTH = 11905.511811024; // In twips.
  38. const DEFAULT_HEIGHT = 16837.79527559; // In twips.
  39. const DEFAULT_MARGIN = 1440; // In twips.
  40. const DEFAULT_GUTTER = 0; // In twips.
  41. const DEFAULT_HEADER_HEIGHT = 720; // In twips.
  42. const DEFAULT_FOOTER_HEIGHT = 720; // In twips.
  43. const DEFAULT_COLUMN_COUNT = 1;
  44. const DEFAULT_COLUMN_SPACING = 720; // In twips.
  45. /**
  46. * Page Orientation
  47. *
  48. * @var string
  49. * @see http://www.schemacentral.com/sc/ooxml/a-w_orient-1.html
  50. */
  51. private $orientation = self::ORIENTATION_PORTRAIT;
  52. /**
  53. * Paper size
  54. *
  55. * @var \PhpOffice\PhpWord\Style\Paper
  56. */
  57. private $paper;
  58. /**
  59. * Page Size Width
  60. *
  61. * @var int|float
  62. */
  63. private $pageSizeW = self::DEFAULT_WIDTH;
  64. /**
  65. * Page Size Height
  66. *
  67. * @var int|float
  68. */
  69. private $pageSizeH = self::DEFAULT_HEIGHT;
  70. /**
  71. * Top margin spacing
  72. *
  73. * @var int|float
  74. */
  75. private $marginTop = self::DEFAULT_MARGIN;
  76. /**
  77. * Left margin spacing
  78. *
  79. * @var int|float
  80. */
  81. private $marginLeft = self::DEFAULT_MARGIN;
  82. /**
  83. * Right margin spacing
  84. *
  85. * @var int|float
  86. */
  87. private $marginRight = self::DEFAULT_MARGIN;
  88. /**
  89. * Bottom margin spacing
  90. *
  91. * @var int|float
  92. */
  93. private $marginBottom = self::DEFAULT_MARGIN;
  94. /**
  95. * Page gutter spacing
  96. *
  97. * @var int|float
  98. * @see http://www.schemacentral.com/sc/ooxml/e-w_pgMar-1.html
  99. */
  100. private $gutter = self::DEFAULT_GUTTER;
  101. /**
  102. * Header height
  103. *
  104. * @var int|float
  105. */
  106. private $headerHeight = self::DEFAULT_HEADER_HEIGHT;
  107. /**
  108. * Footer height
  109. *
  110. * @var int|float
  111. */
  112. private $footerHeight = self::DEFAULT_FOOTER_HEIGHT;
  113. /**
  114. * Page Numbering Start
  115. *
  116. * @var int
  117. */
  118. private $pageNumberingStart;
  119. /**
  120. * Section columns count
  121. *
  122. * @var int
  123. */
  124. private $colsNum = self::DEFAULT_COLUMN_COUNT;
  125. /**
  126. * Section spacing between columns
  127. *
  128. * @var int|float
  129. */
  130. private $colsSpace = self::DEFAULT_COLUMN_SPACING;
  131. /**
  132. * Section break type
  133. *
  134. * Options:
  135. * - nextPage: Next page section break
  136. * - nextColumn: Column section break
  137. * - continuous: Continuous section break
  138. * - evenPage: Even page section break
  139. * - oddPage: Odd page section break
  140. *
  141. * @var string
  142. */
  143. private $breakType;
  144. /**
  145. * Line numbering
  146. *
  147. * @var \PhpOffice\PhpWord\Style\LineNumbering
  148. * @see http://www.schemacentral.com/sc/ooxml/e-w_lnNumType-1.html
  149. */
  150. private $lineNumbering;
  151. /**
  152. * Vertical Text Alignment on Page
  153. * One of \PhpOffice\PhpWord\SimpleType\VerticalJc
  154. *
  155. * @var string
  156. */
  157. private $vAlign;
  158. /**
  159. * Create new instance
  160. */
  161. public function __construct()
  162. {
  163. $this->setPaperSize();
  164. }
  165. /**
  166. * Get paper size
  167. *
  168. * @return string
  169. */
  170. public function getPaperSize()
  171. {
  172. return $this->paper->getSize();
  173. }
  174. /**
  175. * Set paper size
  176. *
  177. * @param string $value
  178. * @return self
  179. */
  180. public function setPaperSize($value = '')
  181. {
  182. if (!$value) {
  183. $value = Settings::getDefaultPaper();
  184. }
  185. if ($this->paper === null) {
  186. $this->paper = new Paper();
  187. }
  188. $this->paper->setSize($value);
  189. $this->pageSizeW = $this->paper->getWidth();
  190. $this->pageSizeH = $this->paper->getHeight();
  191. return $this;
  192. }
  193. /**
  194. * Set Setting Value
  195. *
  196. * @param string $key
  197. * @param string $value
  198. * @return self
  199. */
  200. public function setSettingValue($key, $value)
  201. {
  202. return $this->setStyleValue($key, $value);
  203. }
  204. /**
  205. * Set orientation
  206. *
  207. * @param string $value
  208. * @return self
  209. */
  210. public function setOrientation($value = null)
  211. {
  212. $enum = array(self::ORIENTATION_PORTRAIT, self::ORIENTATION_LANDSCAPE);
  213. $this->orientation = $this->setEnumVal($value, $enum, $this->orientation);
  214. /** @var int|float $longSide Type hint */
  215. $longSide = $this->pageSizeW >= $this->pageSizeH ? $this->pageSizeW : $this->pageSizeH;
  216. /** @var int|float $shortSide Type hint */
  217. $shortSide = $this->pageSizeW < $this->pageSizeH ? $this->pageSizeW : $this->pageSizeH;
  218. if ($this->orientation == self::ORIENTATION_PORTRAIT) {
  219. $this->pageSizeW = $shortSide;
  220. $this->pageSizeH = $longSide;
  221. } else {
  222. $this->pageSizeW = $longSide;
  223. $this->pageSizeH = $shortSide;
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Get Page Orientation
  229. *
  230. * @return string
  231. */
  232. public function getOrientation()
  233. {
  234. return $this->orientation;
  235. }
  236. /**
  237. * Set Portrait Orientation
  238. *
  239. * @return self
  240. */
  241. public function setPortrait()
  242. {
  243. return $this->setOrientation(self::ORIENTATION_PORTRAIT);
  244. }
  245. /**
  246. * Set Landscape Orientation
  247. *
  248. * @return self
  249. */
  250. public function setLandscape()
  251. {
  252. return $this->setOrientation(self::ORIENTATION_LANDSCAPE);
  253. }
  254. /**
  255. * Get Page Size Width
  256. *
  257. * @return int|float|null
  258. *
  259. * @since 0.12.0
  260. */
  261. public function getPageSizeW()
  262. {
  263. return $this->pageSizeW;
  264. }
  265. /**
  266. * @param int|float|null $value
  267. *
  268. * @return \PhpOffice\PhpWord\Style\Section
  269. *
  270. * @since 0.12.0
  271. */
  272. public function setPageSizeW($value = null)
  273. {
  274. $this->pageSizeW = $this->setNumericVal($value, self::DEFAULT_WIDTH);
  275. return $this;
  276. }
  277. /**
  278. * Get Page Size Height
  279. *
  280. * @return int|float|null
  281. *
  282. * @since 0.12.0
  283. */
  284. public function getPageSizeH()
  285. {
  286. return $this->pageSizeH;
  287. }
  288. /**
  289. * @param int|float|null $value
  290. *
  291. * @return \PhpOffice\PhpWord\Style\Section
  292. *
  293. * @since 0.12.0
  294. */
  295. public function setPageSizeH($value = null)
  296. {
  297. $this->pageSizeH = $this->setNumericVal($value, self::DEFAULT_HEIGHT);
  298. return $this;
  299. }
  300. /**
  301. * Get Margin Top
  302. *
  303. * @return int|float
  304. */
  305. public function getMarginTop()
  306. {
  307. return $this->marginTop;
  308. }
  309. /**
  310. * Set Margin Top
  311. *
  312. * @param int|float $value
  313. * @return self
  314. */
  315. public function setMarginTop($value = null)
  316. {
  317. $this->marginTop = $this->setNumericVal($value, self::DEFAULT_MARGIN);
  318. return $this;
  319. }
  320. /**
  321. * Get Margin Left
  322. *
  323. * @return int|float
  324. */
  325. public function getMarginLeft()
  326. {
  327. return $this->marginLeft;
  328. }
  329. /**
  330. * Set Margin Left
  331. *
  332. * @param int|float $value
  333. * @return self
  334. */
  335. public function setMarginLeft($value = null)
  336. {
  337. $this->marginLeft = $this->setNumericVal($value, self::DEFAULT_MARGIN);
  338. return $this;
  339. }
  340. /**
  341. * Get Margin Right
  342. *
  343. * @return int|float
  344. */
  345. public function getMarginRight()
  346. {
  347. return $this->marginRight;
  348. }
  349. /**
  350. * Set Margin Right
  351. *
  352. * @param int|float $value
  353. * @return self
  354. */
  355. public function setMarginRight($value = null)
  356. {
  357. $this->marginRight = $this->setNumericVal($value, self::DEFAULT_MARGIN);
  358. return $this;
  359. }
  360. /**
  361. * Get Margin Bottom
  362. *
  363. * @return int|float
  364. */
  365. public function getMarginBottom()
  366. {
  367. return $this->marginBottom;
  368. }
  369. /**
  370. * Set Margin Bottom
  371. *
  372. * @param int|float $value
  373. * @return self
  374. */
  375. public function setMarginBottom($value = null)
  376. {
  377. $this->marginBottom = $this->setNumericVal($value, self::DEFAULT_MARGIN);
  378. return $this;
  379. }
  380. /**
  381. * Get gutter
  382. *
  383. * @return int|float
  384. */
  385. public function getGutter()
  386. {
  387. return $this->gutter;
  388. }
  389. /**
  390. * Set gutter
  391. *
  392. * @param int|float $value
  393. * @return self
  394. */
  395. public function setGutter($value = null)
  396. {
  397. $this->gutter = $this->setNumericVal($value, self::DEFAULT_GUTTER);
  398. return $this;
  399. }
  400. /**
  401. * Get Header Height
  402. *
  403. * @return int|float
  404. */
  405. public function getHeaderHeight()
  406. {
  407. return $this->headerHeight;
  408. }
  409. /**
  410. * Set Header Height
  411. *
  412. * @param int|float $value
  413. * @return self
  414. */
  415. public function setHeaderHeight($value = null)
  416. {
  417. $this->headerHeight = $this->setNumericVal($value, self::DEFAULT_HEADER_HEIGHT);
  418. return $this;
  419. }
  420. /**
  421. * Get Footer Height
  422. *
  423. * @return int|float
  424. */
  425. public function getFooterHeight()
  426. {
  427. return $this->footerHeight;
  428. }
  429. /**
  430. * Set Footer Height
  431. *
  432. * @param int|float $value
  433. * @return self
  434. */
  435. public function setFooterHeight($value = null)
  436. {
  437. $this->footerHeight = $this->setNumericVal($value, self::DEFAULT_FOOTER_HEIGHT);
  438. return $this;
  439. }
  440. /**
  441. * Get page numbering start
  442. *
  443. * @return null|int
  444. */
  445. public function getPageNumberingStart()
  446. {
  447. return $this->pageNumberingStart;
  448. }
  449. /**
  450. * Set page numbering start
  451. *
  452. * @param null|int $pageNumberingStart
  453. * @return self
  454. */
  455. public function setPageNumberingStart($pageNumberingStart = null)
  456. {
  457. $this->pageNumberingStart = $pageNumberingStart;
  458. return $this;
  459. }
  460. /**
  461. * Get Section Columns Count
  462. *
  463. * @return int
  464. */
  465. public function getColsNum()
  466. {
  467. return $this->colsNum;
  468. }
  469. /**
  470. * Set Section Columns Count
  471. *
  472. * @param int $value
  473. * @return self
  474. */
  475. public function setColsNum($value = null)
  476. {
  477. $this->colsNum = $this->setIntVal($value, self::DEFAULT_COLUMN_COUNT);
  478. return $this;
  479. }
  480. /**
  481. * Get Section Space Between Columns
  482. *
  483. * @return int|float
  484. */
  485. public function getColsSpace()
  486. {
  487. return $this->colsSpace;
  488. }
  489. /**
  490. * Set Section Space Between Columns
  491. *
  492. * @param int|float $value
  493. * @return self
  494. */
  495. public function setColsSpace($value = null)
  496. {
  497. $this->colsSpace = $this->setNumericVal($value, self::DEFAULT_COLUMN_SPACING);
  498. return $this;
  499. }
  500. /**
  501. * Get Break Type
  502. *
  503. * @return string
  504. */
  505. public function getBreakType()
  506. {
  507. return $this->breakType;
  508. }
  509. /**
  510. * Set Break Type
  511. *
  512. * @param string $value
  513. * @return self
  514. */
  515. public function setBreakType($value = null)
  516. {
  517. $this->breakType = $value;
  518. return $this;
  519. }
  520. /**
  521. * Get line numbering
  522. *
  523. * @return \PhpOffice\PhpWord\Style\LineNumbering
  524. */
  525. public function getLineNumbering()
  526. {
  527. return $this->lineNumbering;
  528. }
  529. /**
  530. * Set line numbering
  531. *
  532. * @param mixed $value
  533. * @return self
  534. */
  535. public function setLineNumbering($value = null)
  536. {
  537. $this->setObjectVal($value, 'LineNumbering', $this->lineNumbering);
  538. return $this;
  539. }
  540. /**
  541. * Get vertical alignment
  542. *
  543. * @return string
  544. */
  545. public function getVAlign()
  546. {
  547. return $this->vAlign;
  548. }
  549. /**
  550. * Set vertical alignment
  551. *
  552. * @param string $value
  553. * @return self
  554. */
  555. public function setVAlign($value = null)
  556. {
  557. VerticalJc::validate($value);
  558. $this->vAlign = $value;
  559. return $this;
  560. }
  561. }