Font.php 20 KB

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