Add support for font color
This commit is contained in:
1 parent
2699ffbcae
commit
9467b5a810
8 files changed
+241
-5
No files matched your search
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Exception;
|
||||
|
||||
/**
|
||||
* Class InvalidColorException
|
||||
*
|
||||
* @package Box\Spout\Writer\Exception
|
||||
*/
|
||||
class InvalidColorException extends WriterException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Style;
|
||||
|
||||
use Box\Spout\Writer\Exception\InvalidColorException;
|
||||
|
||||
/**
|
||||
* Class Color
|
||||
* This class provides constants and functions to work with colors
|
||||
*
|
||||
* @package Box\Spout\Writer\Style
|
||||
*/
|
||||
class Color
|
||||
{
|
||||
/** Standard colors - based on Office Online */
|
||||
const BLACK = 'FF000000';
|
||||
const WHITE = 'FFFFFFFF';
|
||||
const RED = 'FFFF0000';
|
||||
const DARK_RED = 'FFC00000';
|
||||
const ORANGE = 'FFFFC000';
|
||||
const YELLOW = 'FFFFFF00';
|
||||
const LIGHT_GREEN = 'FF92D040';
|
||||
const GREEN = 'FF00B050';
|
||||
const LIGHT_BLUE = 'FF00B0E0';
|
||||
const BLUE = 'FF0070C0';
|
||||
const DARK_BLUE = 'FF002060';
|
||||
const PURPLE = 'FF7030A0';
|
||||
|
||||
/**
|
||||
* Returns an ARGB color from R, G and B values
|
||||
* Alpha is assumed to always be 1
|
||||
*
|
||||
* @param int $red Red component, 0 - 255
|
||||
* @param int $green Green component, 0 - 255
|
||||
* @param int $blue Blue component, 0 - 255
|
||||
* @return string ARGB color
|
||||
*/
|
||||
public static function rgb($red, $green, $blue)
|
||||
{
|
||||
self::throwIfInvalidColorComponentValue($red);
|
||||
self::throwIfInvalidColorComponentValue($green);
|
||||
self::throwIfInvalidColorComponentValue($blue);
|
||||
|
||||
return strtoupper(
|
||||
'FF' .
|
||||
self::convertColorComponentToHex($red) .
|
||||
self::convertColorComponentToHex($green) .
|
||||
self::convertColorComponentToHex($blue)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception is the color component value is outside of bounds (0 - 255)
|
||||
*
|
||||
* @param int $colorComponent
|
||||
* @return void
|
||||
* @throws \Box\Spout\Writer\Exception\InvalidColorException
|
||||
*/
|
||||
protected static function throwIfInvalidColorComponentValue($colorComponent)
|
||||
{
|
||||
if (!is_int($colorComponent) || $colorComponent < 0 || $colorComponent > 255) {
|
||||
throw new InvalidColorException("The RGB components must be between 0 and 255. Received: $colorComponent");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the color component to its corresponding hexadecimal value
|
||||
*
|
||||
* @param int $colorComponent Color component, 0 - 255
|
||||
* @return string Corresponding hexadecimal value, with a leading 0 if needed. E.g "0f", "2d"
|
||||
*/
|
||||
protected static function convertColorComponentToHex($colorComponent)
|
||||
{
|
||||
return str_pad(dechex($colorComponent), 2, '0', 0);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ class Style
|
||||
{
|
||||
/** Default font values */
|
||||
const DEFAULT_FONT_SIZE = 11;
|
||||
const DEFAULT_FONT_COLOR = Color::BLACK;
|
||||
const DEFAULT_FONT_NAME = 'Arial';
|
||||
|
||||
/** @var int|null Style ID */
|
||||
@@ -42,6 +43,11 @@ class Style
|
||||
/** @var bool Whether the font size property was set */
|
||||
protected $hasSetFontSize = false;
|
||||
|
||||
/** @var string Font color */
|
||||
protected $fontColor = self::DEFAULT_FONT_COLOR;
|
||||
/** @var bool Whether the font color property was set */
|
||||
protected $hasSetFontColor = false;
|
||||
|
||||
/** @var string Font name */
|
||||
protected $fontName = self::DEFAULT_FONT_NAME;
|
||||
/** @var bool Whether the font name property was set */
|
||||
@@ -169,6 +175,28 @@ class Style
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFontColor()
|
||||
{
|
||||
return $this->fontColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font color.
|
||||
*
|
||||
* @param string $fontColor ARGB color (@see Color)
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontColor($fontColor)
|
||||
{
|
||||
$this->fontColor = $fontColor;
|
||||
$this->hasSetFontColor = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -265,6 +293,9 @@ class Style
|
||||
if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
|
||||
$mergedStyle->setFontSize($baseStyle->getFontSize());
|
||||
}
|
||||
if (!$this->hasSetFontSize && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
|
||||
$mergedStyle->setFontColor($baseStyle->getFontColor());
|
||||
}
|
||||
if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
|
||||
$mergedStyle->setFontName($baseStyle->getFontName());
|
||||
}
|
||||
|
||||
@@ -77,6 +77,18 @@ class StyleBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font color.
|
||||
*
|
||||
* @param string $fontColor ARGB color (@see Color)
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontColor($fontColor)
|
||||
{
|
||||
$this->style->setFontColor($fontColor);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font name.
|
||||
*
|
||||
|
||||
@@ -150,9 +150,14 @@ EOD;
|
||||
{
|
||||
$content = ' <fonts count="' . count($this->styleIdToStyleMappingTable) . '">' . PHP_EOL;
|
||||
|
||||
/** @var \Box\Spout\Writer\Style\Style $style */
|
||||
foreach ($this->styleIdToStyleMappingTable as $style) {
|
||||
$content .= ' <font>' . PHP_EOL;
|
||||
|
||||
$content .= ' <sz val="' . $style->getFontSize() . '"/>' . PHP_EOL;
|
||||
$content .= ' <color rgb="' . $style->getFontColor() . '"/>' . PHP_EOL;
|
||||
$content .= ' <name val="' . $style->getFontName() . '"/>' . PHP_EOL;
|
||||
|
||||
if ($style->isFontBold()) {
|
||||
$content .= ' <b/>' . PHP_EOL;
|
||||
}
|
||||
@@ -166,8 +171,6 @@ EOD;
|
||||
$content .= ' <strike/>' . PHP_EOL;
|
||||
}
|
||||
|
||||
$content .= ' <sz val="' . $style->getFontSize() . '"/>' . PHP_EOL;
|
||||
$content .= ' <name val="' . $style->getFontName() . '"/>' . PHP_EOL;
|
||||
$content .= ' </font>' . PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user