Font.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Writer\RTF\Style;
  18. use PhpOffice\PhpWord\Style\Font as FontStyle;
  19. /**
  20. * RTF font style writer
  21. *
  22. * @since 0.11.0
  23. */
  24. class Font extends AbstractStyle
  25. {
  26. /**
  27. * @var int Font name index
  28. */
  29. private $nameIndex = 0;
  30. /**
  31. * @var int Font color index
  32. */
  33. private $colorIndex = 0;
  34. /**
  35. * Write style
  36. *
  37. * @return string
  38. */
  39. public function write()
  40. {
  41. $style = $this->getStyle();
  42. if (!$style instanceof FontStyle) {
  43. return '';
  44. }
  45. $content = '';
  46. $content .= $this->getValueIf($style->isRTL(), '\rtlch');
  47. $content .= '\cf' . $this->colorIndex;
  48. $content .= '\f' . $this->nameIndex;
  49. $size = $style->getSize();
  50. $content .= $this->getValueIf(is_numeric($size), '\fs' . round($size * 2));
  51. $content .= $this->getValueIf($style->isBold(), '\b');
  52. $content .= $this->getValueIf($style->isItalic(), '\i');
  53. $content .= $this->getValueIf($style->getUnderline() != FontStyle::UNDERLINE_NONE, '\ul');
  54. $content .= $this->getValueIf($style->isStrikethrough(), '\strike');
  55. $content .= $this->getValueIf($style->isSuperScript(), '\super');
  56. $content .= $this->getValueIf($style->isSubScript(), '\sub');
  57. return $content . ' ';
  58. }
  59. /**
  60. * Set font name index.
  61. *
  62. *
  63. * @param int $value
  64. */
  65. public function setNameIndex($value = 0)
  66. {
  67. $this->nameIndex = $value;
  68. }
  69. /**
  70. * Set font color index.
  71. *
  72. * @param int $value
  73. */
  74. public function setColorIndex($value = 0)
  75. {
  76. $this->colorIndex = $value;
  77. }
  78. }