Bez popisu

Font.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Writer\Word2007\Style;
  18. /**
  19. * Font style writer
  20. *
  21. * @since 0.10.0
  22. */
  23. class Font extends AbstractStyle
  24. {
  25. /**
  26. * Is inline in element
  27. *
  28. * @var bool
  29. */
  30. private $isInline = false;
  31. /**
  32. * Write style.
  33. *
  34. * @return void
  35. */
  36. public function write()
  37. {
  38. $xmlWriter = $this->getXmlWriter();
  39. $isStyleName = $this->isInline && !is_null($this->style) && is_string($this->style);
  40. if ($isStyleName) {
  41. $xmlWriter->startElement('w:rPr');
  42. $xmlWriter->startElement('w:rStyle');
  43. $xmlWriter->writeAttribute('w:val', $this->style);
  44. $xmlWriter->endElement();
  45. $xmlWriter->endElement();
  46. } else {
  47. $this->writeStyle();
  48. }
  49. }
  50. /**
  51. * Write full style.
  52. *
  53. * @return void
  54. */
  55. private function writeStyle()
  56. {
  57. $style = $this->getStyle();
  58. if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
  59. return;
  60. }
  61. $xmlWriter = $this->getXmlWriter();
  62. $xmlWriter->startElement('w:rPr');
  63. // Style name
  64. if ($this->isInline === true) {
  65. $styleName = $style->getStyleName();
  66. $xmlWriter->writeElementIf($styleName !== null, 'w:rStyle', 'w:val', $styleName);
  67. }
  68. // Font name/family
  69. $font = $style->getName();
  70. $hint = $style->getHint();
  71. if ($font !== null) {
  72. $xmlWriter->startElement('w:rFonts');
  73. $xmlWriter->writeAttribute('w:ascii', $font);
  74. $xmlWriter->writeAttribute('w:hAnsi', $font);
  75. $xmlWriter->writeAttribute('w:eastAsia', $font);
  76. $xmlWriter->writeAttribute('w:cs', $font);
  77. $xmlWriter->writeAttributeIf($hint !== null, 'w:hint', $hint);
  78. $xmlWriter->endElement();
  79. }
  80. // Color
  81. $color = $style->getColor();
  82. $xmlWriter->writeElementIf($color !== null, 'w:color', 'w:val', $color);
  83. // Size
  84. $size = $style->getSize();
  85. $xmlWriter->writeElementIf($size !== null, 'w:sz', 'w:val', $size * 2);
  86. $xmlWriter->writeElementIf($size !== null, 'w:szCs', 'w:val', $size * 2);
  87. // Bold, italic
  88. $xmlWriter->writeElementIf($style->isBold(), 'w:b');
  89. $xmlWriter->writeElementIf($style->isItalic(), 'w:i');
  90. $xmlWriter->writeElementIf($style->isItalic(), 'w:iCs');
  91. // Strikethrough, double strikethrough
  92. $xmlWriter->writeElementIf($style->isStrikethrough(), 'w:strike');
  93. $xmlWriter->writeElementIf($style->isDoubleStrikethrough(), 'w:dstrike');
  94. // Small caps, all caps
  95. $xmlWriter->writeElementIf($style->isSmallCaps(), 'w:smallCaps');
  96. $xmlWriter->writeElementIf($style->isAllCaps(), 'w:caps');
  97. // Underline
  98. $xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline());
  99. // Foreground-Color
  100. $xmlWriter->writeElementIf($style->getFgColor() !== null, 'w:highlight', 'w:val', $style->getFgColor());
  101. // Superscript/subscript
  102. $xmlWriter->writeElementIf($style->isSuperScript(), 'w:vertAlign', 'w:val', 'superscript');
  103. $xmlWriter->writeElementIf($style->isSubScript(), 'w:vertAlign', 'w:val', 'subscript');
  104. // Spacing
  105. $xmlWriter->writeElementIf($style->getScale() !== null, 'w:w', 'w:val', $style->getScale());
  106. $xmlWriter->writeElementIf($style->getSpacing() !== null, 'w:spacing', 'w:val', $style->getSpacing());
  107. $xmlWriter->writeElementIf($style->getKerning() !== null, 'w:kern', 'w:val', $style->getKerning() * 2);
  108. // Background-Color
  109. $shading = $style->getShading();
  110. if (!is_null($shading)) {
  111. $styleWriter = new Shading($xmlWriter, $shading);
  112. $styleWriter->write();
  113. }
  114. // RTL
  115. if ($this->isInline === true) {
  116. $styleName = $style->getStyleName();
  117. $xmlWriter->writeElementIf($styleName === null && $style->isRTL(), 'w:rtl');
  118. }
  119. $xmlWriter->endElement();
  120. }
  121. /**
  122. * Set is inline.
  123. *
  124. * @param bool $value
  125. * @return void
  126. */
  127. public function setIsInline($value)
  128. {
  129. $this->isInline = $value;
  130. }
  131. }