Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/usage/styles/font.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Available Font style options:
- ``superScript``. Superscript, *true* or *false*.
- ``underline``. Underline, *single*, *dash*, *dotted*, etc.
See ``\PhpOffice\PhpWord\Style\Font::UNDERLINE_...`` class constants for possible values
- ``underlineColor``. Font color, e.g. *FF0000*.
- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
- ``position``. The text position, raised or lowered, in half points
- ``hidden``. Hidden text, *true* or *false*.
- ``whiteSpace``. How white space is handled when generating html/pdf. Possible values are *pre-wrap* and *normal* (other css values for white space are accepted, but are not expected to be useful).
- ``fallbackFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted).
- ``whiteSpace``. How white space is handled when generating html/pdf. Possible values are *pre-wrap* and *normal* (other CSS values for white space are accepted, but are not expected to be useful).
- ``fallbackFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other CSS values for generic fonts are accepted).
5 changes: 5 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ private function readFormField(XMLReader $xmlReader, array $domNodes, $parent, $
if (null !== $formField->getValue()) {
/** @phpstan-ignore offsetAccess.notFound */
$formField->setText($listEntries[$formField->getValue()]);
$value = $formField->getValue();
$index = is_numeric($value) ? (int) $value : null;
if ($index !== null && array_key_exists($index, $listEntries)) {
$formField->setText($listEntries[$index]);
}
}

break;
Expand Down
31 changes: 31 additions & 0 deletions src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ class Font extends AbstractStyle
* @var string
*/
private $underline = self::UNDERLINE_NONE;
/**
* Underline color.
*
* @var string
*/
private $underlineColor = '';

/**
* Superscript.
Expand Down Expand Up @@ -318,6 +324,7 @@ public function getStyleValues()
'bold' => $this->isBold(),
'italic' => $this->isItalic(),
'underline' => $this->getUnderline(),
'underlineColor' => $this->getUnderlineColor(),
Comment thread
ger82 marked this conversation as resolved.
Resolved
'strike' => $this->isStrikethrough(),
'dStrike' => $this->isDoubleStrikethrough(),
'super' => $this->isSuperScript(),
Expand Down Expand Up @@ -518,6 +525,30 @@ public function setUnderline($value = self::UNDERLINE_NONE)
return $this;
}

/**
* Get underlineColor.
*
* @return string
Comment on lines +530 to +531

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* @return string

*/
public function getUnderlineColor(): string
{
return $this->underlineColor;
}

/**
* Set underlineColor.
*
* @param ?string $value
*
* @return self
Comment on lines +540 to +543

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* @param string|null $value
*
* @return self

*/
public function setUnderlineColor(?string $value = ''): self
{
$this->underlineColor = $value ?? '';

return $this;
}

/**
* Get superscript.
*
Expand Down
17 changes: 12 additions & 5 deletions src/PhpWord/Writer/HTML/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,27 @@ public function write()
$color = $style->getColor();
$fgColor = $style->getFgColor();
$underline = $style->getUnderline() != FontStyle::UNDERLINE_NONE;
$underlineColor = $style->getUnderlineColor();
$lineThrough = $style->isStrikethrough() || $style->isDoubleStrikethrough();

$css['font-family'] = $this->getValueIf(!empty($font), $font);
$css['font-size'] = $this->getValueIf($size !== null, "{$size}pt");
$css['color'] = $this->getValueIf($color !== null, "#{$color}");
$css['font-size'] = $this->getValueIf($size !== null, $size."pt");
$css['color'] = $this->getValueIf($color !== null, "#".$color);
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
$css['font-weight'] = $this->getValueIf($style->isBold(), 'bold');
$css['font-style'] = $this->getValueIf($style->isItalic(), 'italic');
$css['vertical-align'] = '';
$css['vertical-align'] .= $this->getValueIf($style->isSuperScript(), 'super');
$css['vertical-align'] .= $this->getValueIf($style->isSubScript(), 'sub');
$css['text-decoration'] = '';
$css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
$css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
$decorations = [];
if ($underline) {
$decorations[] = 'underline';
}
if ($lineThrough) {
$decorations[] = 'line-through';
}
$css['text-decoration'] = implode(' ', $decorations);
$css['text-decoration-color'] = $this->getValueIf($underline && $underlineColor !== '', '#'.$underlineColor );
$css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase');
$css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps');
$css['display'] = $this->getValueIf($style->isHidden(), 'none');
Expand Down
6 changes: 5 additions & 1 deletion src/PhpWord/Writer/ODText/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace PhpOffice\PhpWord\Writer\ODText\Style;

use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Language;

Expand Down Expand Up @@ -65,7 +66,7 @@ public function write(): void

// Color
$color = $style->getColor();
$xmlWriter->writeAttributeIf($color != '', 'fo:color', '#' . \PhpOffice\PhpWord\Shared\Converter::stringToRgb($color));
$xmlWriter->writeAttributeIf($color != '', 'fo:color', '#' . Converter::stringToRgb($color));

// Bold & italic
$xmlWriter->writeAttributeIf($style->isBold(), 'fo:font-weight', 'bold');
Expand All @@ -79,6 +80,9 @@ public function write(): void
$underline = $style->getUnderline();
$xmlWriter->writeAttributeIf($underline != 'none', 'style:text-underline-style', 'solid');

$underlineColor = $style->getUnderlineColor();
$xmlWriter->writeAttributeIf($underlineColor != '', 'style:text-underline-color', '#' . Converter::stringToRgb($underlineColor));

// Strikethrough, double strikethrough
$xmlWriter->writeAttributeIf($style->isStrikethrough(), 'style:text-line-through-type', 'single');
$xmlWriter->writeAttributeIf($style->isDoubleStrikethrough(), 'style:text-line-through-type', 'double');
Expand Down
20 changes: 14 additions & 6 deletions src/PhpWord/Writer/Word2007/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Style;

use PhpOffice\PhpWord\Style;

/**
* Font style writer.
*
Expand All @@ -39,14 +41,14 @@ public function write(): void
{
$xmlWriter = $this->getXmlWriter();

$isStyleName = $this->isInline && null !== $this->style && is_string($this->style);
$isStyleName = $this->isInline && is_string($this->style);
if ($isStyleName) {
$xmlWriter->startElement('w:rPr');
$xmlWriter->startElement('w:rStyle');
$xmlWriter->writeAttribute('w:val', $this->style);
$xmlWriter->endElement();
$style = \PhpOffice\PhpWord\Style::getStyle($this->style);
if ($style instanceof \PhpOffice\PhpWord\Style\Font) {
$style = Style::getStyle($this->style);
if ($style instanceof Style\Font) {
$xmlWriter->writeElementIf($style->isRTL(), 'w:rtl');
}
$xmlWriter->endElement();
Expand All @@ -61,7 +63,7 @@ public function write(): void
private function writeStyle(): void
{
$style = $this->getStyle();
if (!$style instanceof \PhpOffice\PhpWord\Style\Font) {
if (!$style instanceof Style\Font) {
return;
}

Expand Down Expand Up @@ -128,8 +130,14 @@ private function writeStyle(): void
//Hidden text
$xmlWriter->writeElementIf($style->isHidden(), 'w:vanish', 'w:val', $this->writeOnOf($style->isHidden()));

// Underline
$xmlWriter->writeElementIf($style->getUnderline() != 'none', 'w:u', 'w:val', $style->getUnderline());
// Underline and UnderlineColor
$underline = $style->getUnderline();
if ($underline !== 'none') {
$xmlWriter->startElement('w:u');
$xmlWriter->writeAttribute('w:val', $underline);
$xmlWriter->writeAttributeIf($style->getUnderlineColor() !== '', 'w:color', $style->getUnderlineColor());
$xmlWriter->endElement();
}

// Foreground-Color
$xmlWriter->writeElementIf($style->getFgColor() !== null, 'w:highlight', 'w:val', $style->getFgColor());
Expand Down
15 changes: 12 additions & 3 deletions tests/PhpWordTests/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@

namespace PhpOffice\PhpWordTests\Style;

use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\InvalidStyleException;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWordTests\TestHelperDOCX;

use PHPUnit\Framework\TestCase;

/**
* Test class for PhpOffice\PhpWord\Style\Font.
*
* @runTestsInSeparateProcesses
*/
class FontTest extends \PHPUnit\Framework\TestCase
class FontTest extends TestCase
{
/**
* Tear down after each test.
Expand All @@ -48,7 +53,7 @@ public function testInitiation(): void
$object = new Font('text', ['alignment' => Jc::BOTH]);

self::assertEquals('text', $object->getStyleType());
self::assertInstanceOf(\PhpOffice\PhpWord\Style\Paragraph::class, $object->getParagraph());
self::assertInstanceOf(Paragraph::class, $object->getParagraph());
self::assertIsArray($object->getStyleValues());
}

Expand All @@ -62,6 +67,7 @@ public static function providerStyleValueEmpty(): array
['bold', false],
['italic', false],
['underline', Font::UNDERLINE_NONE],
['underlineColor', ''],
['superScript', false],
['subScript', false],
['strikethrough', false],
Expand Down Expand Up @@ -109,6 +115,7 @@ public static function providerStyleValueNormal(): array
['bold', true],
['italic', true],
['underline', Font::UNDERLINE_HEAVY],
['underlineColor', '000000'],
['superScript', true],
['subScript', false],
['strikethrough', true],
Expand Down Expand Up @@ -141,12 +148,14 @@ public function testSetStyleValueNormal(string $key, $value): void
{
$object = new Font();
$object->setStyleValue($key, $value);
$key = ucfirst($key); // correction for CamelCase isAllCaps or getAllCaps
$get = is_bool($value) ? "is{$key}" : "get{$key}";
self::assertEquals($value, $object->$get());
}

/**
* Test set line height.
* @throws CreateTemporaryFileException
*/
public function testLineHeight(): void
{
Expand Down Expand Up @@ -193,7 +202,7 @@ public function testLineHeightFloatval(): void
*/
public function testLineHeightException(): void
{
$this->expectException(\PhpOffice\PhpWord\Exception\InvalidStyleException::class);
$this->expectException(InvalidStyleException::class);
$object = new Font();
$object->setLineHeight('a');
}
Expand Down
Loading
Loading