Ei kuvausta

Font.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. /**
  19. * Font style
  20. */
  21. class Font extends AbstractStyle
  22. {
  23. /**
  24. * Underline types
  25. *
  26. * @const string
  27. */
  28. const UNDERLINE_NONE = 'none';
  29. const UNDERLINE_DASH = 'dash';
  30. const UNDERLINE_DASHHEAVY = 'dashHeavy';
  31. const UNDERLINE_DASHLONG = 'dashLong';
  32. const UNDERLINE_DASHLONGHEAVY = 'dashLongHeavy';
  33. const UNDERLINE_DOUBLE = 'dbl';
  34. const UNDERLINE_DOTHASH = 'dotDash';
  35. const UNDERLINE_DOTHASHHEAVY = 'dotDashHeavy';
  36. const UNDERLINE_DOTDOTDASH = 'dotDotDash';
  37. const UNDERLINE_DOTDOTDASHHEAVY = 'dotDotDashHeavy';
  38. const UNDERLINE_DOTTED = 'dotted';
  39. const UNDERLINE_DOTTEDHEAVY = 'dottedHeavy';
  40. const UNDERLINE_HEAVY = 'heavy';
  41. const UNDERLINE_SINGLE = 'single';
  42. const UNDERLINE_WAVY = 'wavy';
  43. const UNDERLINE_WAVYDOUBLE = 'wavyDbl';
  44. const UNDERLINE_WAVYHEAVY = 'wavyHeavy';
  45. const UNDERLINE_WORDS = 'words';
  46. /**
  47. * Foreground colors
  48. *
  49. * @const string
  50. */
  51. const FGCOLOR_YELLOW = 'yellow';
  52. const FGCOLOR_LIGHTGREEN = 'green';
  53. const FGCOLOR_CYAN = 'cyan';
  54. const FGCOLOR_MAGENTA = 'magenta';
  55. const FGCOLOR_BLUE = 'blue';
  56. const FGCOLOR_RED = 'red';
  57. const FGCOLOR_DARKBLUE = 'darkBlue';
  58. const FGCOLOR_DARKCYAN = 'darkCyan';
  59. const FGCOLOR_DARKGREEN = 'darkGreen';
  60. const FGCOLOR_DARKMAGENTA = 'darkMagenta';
  61. const FGCOLOR_DARKRED = 'darkRed';
  62. const FGCOLOR_DARKYELLOW = 'darkYellow';
  63. const FGCOLOR_DARKGRAY = 'darkGray';
  64. const FGCOLOR_LIGHTGRAY = 'lightGray';
  65. const FGCOLOR_BLACK = 'black';
  66. /**
  67. * Aliases
  68. *
  69. * @var array
  70. */
  71. protected $aliases = array('line-height' => 'lineHeight');
  72. /**
  73. * Font style type
  74. *
  75. * @var string
  76. */
  77. private $type;
  78. /**
  79. * Font name
  80. *
  81. * @var string
  82. */
  83. private $name;
  84. /**
  85. * Font Content Type
  86. *
  87. * @var string
  88. */
  89. private $hint;
  90. /**
  91. * Font size
  92. *
  93. * @var int|float
  94. */
  95. private $size;
  96. /**
  97. * Font color
  98. *
  99. * @var string
  100. */
  101. private $color;
  102. /**
  103. * Bold
  104. *
  105. * @var bool
  106. */
  107. private $bold = false;
  108. /**
  109. * Italic
  110. *
  111. * @var bool
  112. */
  113. private $italic = false;
  114. /**
  115. * Undeline
  116. *
  117. * @var string
  118. */
  119. private $underline = self::UNDERLINE_NONE;
  120. /**
  121. * Superscript
  122. *
  123. * @var bool
  124. */
  125. private $superScript = false;
  126. /**
  127. * Subscript
  128. *
  129. * @var bool
  130. */
  131. private $subScript = false;
  132. /**
  133. * Strikethrough
  134. *
  135. * @var bool
  136. */
  137. private $strikethrough = false;
  138. /**
  139. * Double strikethrough
  140. *
  141. * @var bool
  142. */
  143. private $doubleStrikethrough = false;
  144. /**
  145. * Small caps
  146. *
  147. * @var bool
  148. * @link http://www.schemacentral.com/sc/ooxml/e-w_smallCaps-1.html
  149. */
  150. private $smallCaps = false;
  151. /**
  152. * All caps
  153. *
  154. * @var bool
  155. * @link http://www.schemacentral.com/sc/ooxml/e-w_caps-1.html
  156. */
  157. private $allCaps = false;
  158. /**
  159. * Foreground/highlight
  160. *
  161. * @var string
  162. */
  163. private $fgColor;
  164. /**
  165. * Expanded/compressed text: 0-600 (percent)
  166. *
  167. * @var int
  168. * @since 0.12.0
  169. * @link http://www.schemacentral.com/sc/ooxml/e-w_w-1.html
  170. */
  171. private $scale;
  172. /**
  173. * Character spacing adjustment: twip
  174. *
  175. * @var int|float
  176. * @since 0.12.0
  177. * @link http://www.schemacentral.com/sc/ooxml/e-w_spacing-2.html
  178. */
  179. private $spacing;
  180. /**
  181. * Font kerning: halfpoint
  182. *
  183. * @var int|float
  184. * @since 0.12.0
  185. * @link http://www.schemacentral.com/sc/ooxml/e-w_kern-1.html
  186. */
  187. private $kerning;
  188. /**
  189. * Paragraph style
  190. *
  191. * @var \PhpOffice\PhpWord\Style\Paragraph
  192. */
  193. private $paragraph;
  194. /**
  195. * Shading
  196. *
  197. * @var \PhpOffice\PhpWord\Style\Shading
  198. */
  199. private $shading;
  200. /**
  201. * Right to left languages
  202. * @var boolean
  203. */
  204. private $rtl = false;
  205. /**
  206. * Create new font style
  207. *
  208. * @param string $type Type of font
  209. * @param array $paragraph Paragraph styles definition
  210. */
  211. public function __construct($type = 'text', $paragraph = null)
  212. {
  213. $this->type = $type;
  214. $this->setParagraph($paragraph);
  215. }
  216. /**
  217. * Get style values
  218. *
  219. * @return array
  220. * @since 0.12.0
  221. */
  222. public function getStyleValues()
  223. {
  224. $styles = array(
  225. 'name' => $this->getStyleName(),
  226. 'basic' => array(
  227. 'name' => $this->getName(),
  228. 'size' => $this->getSize(),
  229. 'color' => $this->getColor(),
  230. 'hint' => $this->getHint(),
  231. ),
  232. 'style' => array(
  233. 'bold' => $this->isBold(),
  234. 'italic' => $this->isItalic(),
  235. 'underline' => $this->getUnderline(),
  236. 'strike' => $this->isStrikethrough(),
  237. 'dStrike' => $this->isDoubleStrikethrough(),
  238. 'super' => $this->isSuperScript(),
  239. 'sub' => $this->isSubScript(),
  240. 'smallCaps' => $this->isSmallCaps(),
  241. 'allCaps' => $this->isAllCaps(),
  242. 'fgColor' => $this->getFgColor(),
  243. ),
  244. 'spacing' => array(
  245. 'scale' => $this->getScale(),
  246. 'spacing' => $this->getSpacing(),
  247. 'kerning' => $this->getKerning(),
  248. ),
  249. 'paragraph' => $this->getParagraph(),
  250. 'rtl' => $this->isRTL(),
  251. 'shading' => $this->getShading(),
  252. );
  253. return $styles;
  254. }
  255. /**
  256. * Get style type
  257. *
  258. * @return string
  259. */
  260. public function getStyleType()
  261. {
  262. return $this->type;
  263. }
  264. /**
  265. * Get font name
  266. *
  267. * @return string
  268. */
  269. public function getName()
  270. {
  271. return $this->name;
  272. }
  273. /**
  274. * Set font name
  275. *
  276. * @param string $value
  277. * @return self
  278. */
  279. public function setName($value = null)
  280. {
  281. $this->name = $value;
  282. return $this;
  283. }
  284. /**
  285. * Get Font Content Type
  286. *
  287. * @return string
  288. */
  289. public function getHint()
  290. {
  291. return $this->hint;
  292. }
  293. /**
  294. * Set Font Content Type
  295. *
  296. * @param string $value
  297. * @return self
  298. */
  299. public function setHint($value = null)
  300. {
  301. $this->hint = $value;
  302. return $this;
  303. }
  304. /**
  305. * Get font size
  306. *
  307. * @return int|float
  308. */
  309. public function getSize()
  310. {
  311. return $this->size;
  312. }
  313. /**
  314. * Set font size
  315. *
  316. * @param int|float $value
  317. * @return self
  318. */
  319. public function setSize($value = null)
  320. {
  321. $this->size = $this->setNumericVal($value, $this->size);
  322. return $this;
  323. }
  324. /**
  325. * Get font color
  326. *
  327. * @return string
  328. */
  329. public function getColor()
  330. {
  331. return $this->color;
  332. }
  333. /**
  334. * Set font color
  335. *
  336. * @param string $value
  337. * @return self
  338. */
  339. public function setColor($value = null)
  340. {
  341. $this->color = $value;
  342. return $this;
  343. }
  344. /**
  345. * Get bold
  346. *
  347. * @return bool
  348. */
  349. public function isBold()
  350. {
  351. return $this->bold;
  352. }
  353. /**
  354. * Set bold
  355. *
  356. * @param bool $value
  357. * @return self
  358. */
  359. public function setBold($value = true)
  360. {
  361. $this->bold = $this->setBoolVal($value, $this->bold);
  362. return $this;
  363. }
  364. /**
  365. * Get italic
  366. *
  367. * @return bool
  368. */
  369. public function isItalic()
  370. {
  371. return $this->italic;
  372. }
  373. /**
  374. * Set italic
  375. *
  376. * @param bool $value
  377. * @return self
  378. */
  379. public function setItalic($value = true)
  380. {
  381. $this->italic = $this->setBoolVal($value, $this->italic);
  382. return $this;
  383. }
  384. /**
  385. * Get underline
  386. *
  387. * @return string
  388. */
  389. public function getUnderline()
  390. {
  391. return $this->underline;
  392. }
  393. /**
  394. * Set underline
  395. *
  396. * @param string $value
  397. * @return self
  398. */
  399. public function setUnderline($value = self::UNDERLINE_NONE)
  400. {
  401. $this->underline = $this->setNonEmptyVal($value, self::UNDERLINE_NONE);
  402. return $this;
  403. }
  404. /**
  405. * Get superscript
  406. *
  407. * @return bool
  408. */
  409. public function isSuperScript()
  410. {
  411. return $this->superScript;
  412. }
  413. /**
  414. * Set superscript
  415. *
  416. * @param bool $value
  417. * @return self
  418. */
  419. public function setSuperScript($value = true)
  420. {
  421. return $this->setPairedVal($this->superScript, $this->subScript, $value);
  422. }
  423. /**
  424. * Get subscript
  425. *
  426. * @return bool
  427. */
  428. public function isSubScript()
  429. {
  430. return $this->subScript;
  431. }
  432. /**
  433. * Set subscript
  434. *
  435. * @param bool $value
  436. * @return self
  437. */
  438. public function setSubScript($value = true)
  439. {
  440. return $this->setPairedVal($this->subScript, $this->superScript, $value);
  441. }
  442. /**
  443. * Get strikethrough
  444. *
  445. * @return bool
  446. */
  447. public function isStrikethrough()
  448. {
  449. return $this->strikethrough;
  450. }
  451. /**
  452. * Set strikethrough
  453. *
  454. * @param bool $value
  455. * @return self
  456. */
  457. public function setStrikethrough($value = true)
  458. {
  459. return $this->setPairedVal($this->strikethrough, $this->doubleStrikethrough, $value);
  460. }
  461. /**
  462. * Get double strikethrough
  463. *
  464. * @return bool
  465. */
  466. public function isDoubleStrikethrough()
  467. {
  468. return $this->doubleStrikethrough;
  469. }
  470. /**
  471. * Set double strikethrough
  472. *
  473. * @param bool $value
  474. * @return self
  475. */
  476. public function setDoubleStrikethrough($value = true)
  477. {
  478. return $this->setPairedVal($this->doubleStrikethrough, $this->strikethrough, $value);
  479. }
  480. /**
  481. * Get small caps
  482. *
  483. * @return bool
  484. */
  485. public function isSmallCaps()
  486. {
  487. return $this->smallCaps;
  488. }
  489. /**
  490. * Set small caps
  491. *
  492. * @param bool $value
  493. * @return self
  494. */
  495. public function setSmallCaps($value = true)
  496. {
  497. return $this->setPairedVal($this->smallCaps, $this->allCaps, $value);
  498. }
  499. /**
  500. * Get all caps
  501. *
  502. * @return bool
  503. */
  504. public function isAllCaps()
  505. {
  506. return $this->allCaps;
  507. }
  508. /**
  509. * Set all caps
  510. *
  511. * @param bool $value
  512. * @return self
  513. */
  514. public function setAllCaps($value = true)
  515. {
  516. return $this->setPairedVal($this->allCaps, $this->smallCaps, $value);
  517. }
  518. /**
  519. * Get foreground/highlight color
  520. *
  521. * @return string
  522. */
  523. public function getFgColor()
  524. {
  525. return $this->fgColor;
  526. }
  527. /**
  528. * Set foreground/highlight color
  529. *
  530. * @param string $value
  531. * @return self
  532. */
  533. public function setFgColor($value = null)
  534. {
  535. $this->fgColor = $value;
  536. return $this;
  537. }
  538. /**
  539. * Get background
  540. *
  541. * @return string
  542. */
  543. public function getBgColor()
  544. {
  545. return $this->getChildStyleValue($this->shading, 'fill');
  546. }
  547. /**
  548. * Set background
  549. *
  550. * @param string $value
  551. * @return \PhpOffice\PhpWord\Style\Table
  552. */
  553. public function setBgColor($value = null)
  554. {
  555. $this->setShading(array('fill' => $value));
  556. }
  557. /**
  558. * Get scale
  559. *
  560. * @return int
  561. */
  562. public function getScale()
  563. {
  564. return $this->scale;
  565. }
  566. /**
  567. * Set scale
  568. *
  569. * @param int $value
  570. * @return self
  571. */
  572. public function setScale($value = null)
  573. {
  574. $this->scale = $this->setIntVal($value, null);
  575. return $this;
  576. }
  577. /**
  578. * Get font spacing
  579. *
  580. * @return int|float
  581. */
  582. public function getSpacing()
  583. {
  584. return $this->spacing;
  585. }
  586. /**
  587. * Set font spacing
  588. *
  589. * @param int|float $value
  590. * @return self
  591. */
  592. public function setSpacing($value = null)
  593. {
  594. $this->spacing = $this->setNumericVal($value, null);
  595. return $this;
  596. }
  597. /**
  598. * Get font kerning
  599. *
  600. * @return int|float
  601. */
  602. public function getKerning()
  603. {
  604. return $this->kerning;
  605. }
  606. /**
  607. * Set font kerning
  608. *
  609. * @param int|float $value
  610. * @return self
  611. */
  612. public function setKerning($value = null)
  613. {
  614. $this->kerning = $this->setNumericVal($value, null);
  615. return $this;
  616. }
  617. /**
  618. * Get line height
  619. *
  620. * @return int|float
  621. */
  622. public function getLineHeight()
  623. {
  624. return $this->getParagraph()->getLineHeight();
  625. }
  626. /**
  627. * Set lineheight
  628. *
  629. * @param int|float|string $value
  630. * @return self
  631. */
  632. public function setLineHeight($value)
  633. {
  634. $this->setParagraph(array('lineHeight' => $value));
  635. return $this;
  636. }
  637. /**
  638. * Get paragraph style
  639. *
  640. * @return \PhpOffice\PhpWord\Style\Paragraph
  641. */
  642. public function getParagraph()
  643. {
  644. return $this->paragraph;
  645. }
  646. /**
  647. * Set shading
  648. *
  649. * @param mixed $value
  650. * @return self
  651. */
  652. public function setParagraph($value = null)
  653. {
  654. $this->setObjectVal($value, 'Paragraph', $this->paragraph);
  655. return $this;
  656. }
  657. /**
  658. * Get rtl
  659. *
  660. * @return bool
  661. */
  662. public function isRTL()
  663. {
  664. return $this->rtl;
  665. }
  666. /**
  667. * Set rtl
  668. *
  669. * @param bool $value
  670. * @return self
  671. */
  672. public function setRTL($value = true)
  673. {
  674. $this->rtl = $this->setBoolVal($value, $this->rtl);
  675. return $this;
  676. }
  677. /**
  678. * Get shading
  679. *
  680. * @return \PhpOffice\PhpWord\Style\Shading
  681. */
  682. public function getShading()
  683. {
  684. return $this->shading;
  685. }
  686. /**
  687. * Set shading
  688. *
  689. * @param mixed $value
  690. * @return self
  691. */
  692. public function setShading($value = null)
  693. {
  694. $this->setObjectVal($value, 'Shading', $this->shading);
  695. return $this;
  696. }
  697. /**
  698. * Get bold
  699. *
  700. * @deprecated 0.10.0
  701. * @codeCoverageIgnore
  702. */
  703. public function getBold()
  704. {
  705. return $this->isBold();
  706. }
  707. /**
  708. * Get italic
  709. *
  710. * @deprecated 0.10.0
  711. * @codeCoverageIgnore
  712. */
  713. public function getItalic()
  714. {
  715. return $this->isItalic();
  716. }
  717. /**
  718. * Get superscript
  719. *
  720. * @deprecated 0.10.0
  721. * @codeCoverageIgnore
  722. */
  723. public function getSuperScript()
  724. {
  725. return $this->isSuperScript();
  726. }
  727. /**
  728. * Get subscript
  729. *
  730. * @deprecated 0.10.0
  731. * @codeCoverageIgnore
  732. */
  733. public function getSubScript()
  734. {
  735. return $this->isSubScript();
  736. }
  737. /**
  738. * Get strikethrough
  739. *
  740. * @deprecated 0.10.0
  741. * @codeCoverageIgnore
  742. */
  743. public function getStrikethrough()
  744. {
  745. return $this->isStrikethrough();
  746. }
  747. /**
  748. * Get paragraph style
  749. *
  750. * @deprecated 0.11.0
  751. * @codeCoverageIgnore
  752. */
  753. public function getParagraphStyle()
  754. {
  755. return $this->getParagraph();
  756. }
  757. }