Font.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\ODText\Style;
  18. /**
  19. * Font style writer
  20. *
  21. * @since 0.10.0
  22. */
  23. class Font extends AbstractStyle
  24. {
  25. /**
  26. * Write style.
  27. */
  28. public function write()
  29. {
  30. $style = $this->getStyle();
  31. if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
  32. return;
  33. }
  34. $xmlWriter = $this->getXmlWriter();
  35. $stylep = (method_exists($style, 'getParagraph')) ? $style->getParagraph() : null;
  36. if ($stylep instanceof \PhpOffice\PhpWord\Style\Paragraph) {
  37. $temp1 = clone $stylep;
  38. $temp1->setStyleName($style->getStyleName());
  39. $temp2 = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $temp1);
  40. $temp2->write();
  41. }
  42. $xmlWriter->startElement('style:style');
  43. $xmlWriter->writeAttribute('style:name', $style->getStyleName());
  44. $xmlWriter->writeAttribute('style:family', 'text');
  45. $xmlWriter->startElement('style:text-properties');
  46. // Name
  47. $font = $style->getName();
  48. $xmlWriter->writeAttributeIf($font != '', 'style:font-name', $font);
  49. $xmlWriter->writeAttributeIf($font != '', 'style:font-name-complex', $font);
  50. $size = $style->getSize();
  51. // Size
  52. $xmlWriter->writeAttributeIf(is_numeric($size), 'fo:font-size', $size . 'pt');
  53. $xmlWriter->writeAttributeIf(is_numeric($size), 'style:font-size-asian', $size . 'pt');
  54. $xmlWriter->writeAttributeIf(is_numeric($size), 'style:font-size-complex', $size . 'pt');
  55. // Color
  56. $color = $style->getColor();
  57. $xmlWriter->writeAttributeIf($color != '', 'fo:color', '#' . \PhpOffice\PhpWord\Shared\Converter::stringToRgb($color));
  58. // Bold & italic
  59. $xmlWriter->writeAttributeIf($style->isBold(), 'fo:font-weight', 'bold');
  60. $xmlWriter->writeAttributeIf($style->isBold(), 'style:font-weight-asian', 'bold');
  61. $xmlWriter->writeAttributeIf($style->isItalic(), 'fo:font-style', 'italic');
  62. $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-asian', 'italic');
  63. $xmlWriter->writeAttributeIf($style->isItalic(), 'style:font-style-complex', 'italic');
  64. // Underline
  65. // @todo Various mode of underline
  66. $underline = $style->getUnderline();
  67. $xmlWriter->writeAttributeIf($underline != 'none', 'style:text-underline-style', 'solid');
  68. // Strikethrough, double strikethrough
  69. $xmlWriter->writeAttributeIf($style->isStrikethrough(), 'style:text-line-through-type', 'single');
  70. $xmlWriter->writeAttributeIf($style->isDoubleStrikethrough(), 'style:text-line-through-type', 'double');
  71. // Small caps, all caps
  72. $xmlWriter->writeAttributeIf($style->isSmallCaps(), 'fo:font-variant', 'small-caps');
  73. $xmlWriter->writeAttributeIf($style->isAllCaps(), 'fo:text-transform', 'uppercase');
  74. //Hidden text
  75. $xmlWriter->writeAttributeIf($style->isHidden(), 'text:display', 'none');
  76. // Superscript/subscript
  77. $xmlWriter->writeAttributeIf($style->isSuperScript(), 'style:text-position', 'super');
  78. $xmlWriter->writeAttributeIf($style->isSubScript(), 'style:text-position', 'sub');
  79. if ($style->isNoProof()) {
  80. $xmlWriter->writeAttribute('fo:language', 'zxx');
  81. $xmlWriter->writeAttribute('style:language-asian', 'zxx');
  82. $xmlWriter->writeAttribute('style:language-complex', 'zxx');
  83. $xmlWriter->writeAttribute('fo:country', 'none');
  84. $xmlWriter->writeAttribute('style:country-asian', 'none');
  85. $xmlWriter->writeAttribute('style:country-complex', 'none');
  86. }
  87. // @todo Foreground-Color
  88. // @todo Background color
  89. $xmlWriter->endElement(); // style:text-properties
  90. $xmlWriter->endElement(); // style:style
  91. }
  92. }