Namespacing Phase 2 - Styles (#2471)
* WIP Namespacing Phase 2 - Styles This is part 2 of a several-phase process to permit PhpSpreadsheet to handle input Xlsx files which use unexpected namespacing. The first phase, introduced as part of release 1.19.0, essentially handled the reading of data. This phase handles the reading of styles. More phases are planned. It is my intention to leave this in draft status for at least a month. This will give time for additional testing, by me and, I hope, others who might be interested. This fixes the same problem addressed by PR #2458, if it reaches mergeable status before I am ready to take this out of draft status. I do not anticipate any difficult merge conflicts if the other change is merged first. This change is more difficult than I'd hoped. I can't get xpath to work properly with the namespaced style file, even though I don't have difficulties with others. Normally we expect: ```xml <stylesheet xmlns="http://whatever" ... ``` In the namespaced files, we typically see: ```xml <x:stylesheet xmlns:x="http://whatever" ... ``` Simplexml_load_file specifying a namespace handles the two situations the same, as expected. But, for some reason that I cannot figure out, there are significant differences when xpath processes the result. However, I can manipulate the xml if necessary; I'm not proud of doing that, and will gladly accept any suggestions. In the meantime, it seems to work. My major non-standard unit test file had disabled any style-related tests when phase 1 was installed. These are now all enabled. * Scrutinizer Its analysis is wrong, but the "errors" it pointed out are easy to fix. * Eliminate XML Source Manipulation Original solution required XML manipulation to overcome what appears to be an xpath problem. This version replaces xpath with iteration, eliminating the need to manipulate the XML. * Handle Some Edge Cases For example, Style file without a Fills section. * Restore RGB/ARGB Interchangeability Fix #2494. Apparently EPPlus outputs fill colors as `<fgColor rgb="BFBFBF">` while most output fill colors as `<fgColor rgb="FFBFBFBF">`. EPPlus actually makes more sense. Regardless, validating length of rgb/argb is a recent development for PhpSpreadsheet, under the assumption that an incorrect length is a user error. This development invalidates that assumption, so restore the previous behavior. In addition, a comment in Colors.php says that the supplied color is "the ARGB value for the colour, or named colour". However, although named colors are accepted, nothing sensible is done with them - they are passed unchanged to the ARGB value, where Excel treats them as black. The routine should either reject the named color, or convert it to the appropriate ARGB value. This change implements the latter.
This commit is contained in:
parent
88da4e17e5
commit
ad5532e2f4
|
|
@ -539,16 +539,14 @@ class Xlsx extends BaseReader
|
||||||
if ($xpath === null) {
|
if ($xpath === null) {
|
||||||
$xmlStyles = self::testSimpleXml(null);
|
$xmlStyles = self::testSimpleXml(null);
|
||||||
} else {
|
} else {
|
||||||
// I think Nonamespace is okay because I'm using xpath.
|
$xmlStyles = $this->loadZip("$dir/$xpath[Target]", $mainNS);
|
||||||
$xmlStyles = $this->loadZipNonamespace("$dir/$xpath[Target]", $mainNS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$xmlStyles->registerXPathNamespace('smm', Namespaces::MAIN);
|
$fills = self::extractStyles($xmlStyles, 'fills', 'fill');
|
||||||
$fills = self::xpathNoFalse($xmlStyles, 'smm:fills/smm:fill');
|
$fonts = self::extractStyles($xmlStyles, 'fonts', 'font');
|
||||||
$fonts = self::xpathNoFalse($xmlStyles, 'smm:fonts/smm:font');
|
$borders = self::extractStyles($xmlStyles, 'borders', 'border');
|
||||||
$borders = self::xpathNoFalse($xmlStyles, 'smm:borders/smm:border');
|
$xfTags = self::extractStyles($xmlStyles, 'cellXfs', 'xf');
|
||||||
$xfTags = self::xpathNoFalse($xmlStyles, 'smm:cellXfs/smm:xf');
|
$cellXfTags = self::extractStyles($xmlStyles, 'cellStyleXfs', 'xf');
|
||||||
$cellXfTags = self::xpathNoFalse($xmlStyles, 'smm:cellStyleXfs/smm:xf');
|
|
||||||
|
|
||||||
$styles = [];
|
$styles = [];
|
||||||
$cellStyles = [];
|
$cellStyles = [];
|
||||||
|
|
@ -559,6 +557,7 @@ class Xlsx extends BaseReader
|
||||||
if (isset($numFmts) && ($numFmts !== null)) {
|
if (isset($numFmts) && ($numFmts !== null)) {
|
||||||
$numFmts->registerXPathNamespace('sml', $mainNS);
|
$numFmts->registerXPathNamespace('sml', $mainNS);
|
||||||
}
|
}
|
||||||
|
$this->styleReader->setNamespace($mainNS);
|
||||||
if (!$this->readDataOnly/* && $xmlStyles*/) {
|
if (!$this->readDataOnly/* && $xmlStyles*/) {
|
||||||
foreach ($xfTags as $xfTag) {
|
foreach ($xfTags as $xfTag) {
|
||||||
$xf = self::getAttributes($xfTag);
|
$xf = self::getAttributes($xfTag);
|
||||||
|
|
@ -643,6 +642,7 @@ class Xlsx extends BaseReader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->styleReader->setStyleXml($xmlStyles);
|
$this->styleReader->setStyleXml($xmlStyles);
|
||||||
|
$this->styleReader->setNamespace($mainNS);
|
||||||
$this->styleReader->setStyleBaseData($theme, $styles, $cellStyles);
|
$this->styleReader->setStyleBaseData($theme, $styles, $cellStyles);
|
||||||
$dxfs = $this->styleReader->dxfs($this->readDataOnly);
|
$dxfs = $this->styleReader->dxfs($this->readDataOnly);
|
||||||
$styles = $this->styleReader->styles();
|
$styles = $this->styleReader->styles();
|
||||||
|
|
@ -2088,4 +2088,16 @@ class Xlsx extends BaseReader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function extractStyles(?SimpleXMLElement $sxml, string $node1, string $node2): array
|
||||||
|
{
|
||||||
|
$array = [];
|
||||||
|
if ($sxml && $sxml->{$node1}->{$node2}) {
|
||||||
|
foreach ($sxml->{$node1}->{$node2} as $node) {
|
||||||
|
$array[] = $node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,37 @@ class Styles extends BaseParserClass
|
||||||
/** @var SimpleXMLElement */
|
/** @var SimpleXMLElement */
|
||||||
private $styleXml;
|
private $styleXml;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $namespace = '';
|
||||||
|
|
||||||
|
public function setNamespace(string $namespace): void
|
||||||
|
{
|
||||||
|
$this->namespace = $namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast SimpleXMLElement to bool to overcome Scrutinizer problem.
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
private static function castBool($value): bool
|
||||||
|
{
|
||||||
|
return (bool) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getStyleAttributes(SimpleXMLElement $value): SimpleXMLElement
|
||||||
|
{
|
||||||
|
$attr = null;
|
||||||
|
if (self::castBool($value)) {
|
||||||
|
$attr = $value->attributes('');
|
||||||
|
if ($attr === null || count($attr) === 0) {
|
||||||
|
$attr = $value->attributes($this->namespace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Xlsx::testSimpleXml($attr);
|
||||||
|
}
|
||||||
|
|
||||||
public function setStyleXml(SimpleXmlElement $styleXml): void
|
public function setStyleXml(SimpleXmlElement $styleXml): void
|
||||||
{
|
{
|
||||||
$this->styleXml = $styleXml;
|
$this->styleXml = $styleXml;
|
||||||
|
|
@ -52,33 +83,44 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
public function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
|
public function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
|
||||||
{
|
{
|
||||||
if (isset($fontStyleXml->name, $fontStyleXml->name['val'])) {
|
if (isset($fontStyleXml->name)) {
|
||||||
$fontStyle->setName((string) $fontStyleXml->name['val']);
|
$attr = $this->getStyleAttributes($fontStyleXml->name);
|
||||||
|
if (isset($attr['val'])) {
|
||||||
|
$fontStyle->setName((string) $attr['val']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset($fontStyleXml->sz)) {
|
||||||
|
$attr = $this->getStyleAttributes($fontStyleXml->sz);
|
||||||
|
if (isset($attr['val'])) {
|
||||||
|
$fontStyle->setSize((float) $attr['val']);
|
||||||
}
|
}
|
||||||
if (isset($fontStyleXml->sz, $fontStyleXml->sz['val'])) {
|
|
||||||
$fontStyle->setSize((float) $fontStyleXml->sz['val']);
|
|
||||||
}
|
}
|
||||||
if (isset($fontStyleXml->b)) {
|
if (isset($fontStyleXml->b)) {
|
||||||
$fontStyle->setBold(!isset($fontStyleXml->b['val']) || self::boolean((string) $fontStyleXml->b['val']));
|
$attr = $this->getStyleAttributes($fontStyleXml->b);
|
||||||
|
$fontStyle->setBold(!isset($attr['val']) || self::boolean((string) $attr['val']));
|
||||||
}
|
}
|
||||||
if (isset($fontStyleXml->i)) {
|
if (isset($fontStyleXml->i)) {
|
||||||
$fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
|
$attr = $this->getStyleAttributes($fontStyleXml->i);
|
||||||
|
$fontStyle->setItalic(!isset($attr['val']) || self::boolean((string) $attr['val']));
|
||||||
}
|
}
|
||||||
if (isset($fontStyleXml->strike)) {
|
if (isset($fontStyleXml->strike)) {
|
||||||
$fontStyle->setStrikethrough(
|
$attr = $this->getStyleAttributes($fontStyleXml->strike);
|
||||||
!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
|
$fontStyle->setStrikethrough(!isset($attr['val']) || self::boolean((string) $attr['val']));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
$fontStyle->getColor()->setARGB($this->readColor($fontStyleXml->color));
|
$fontStyle->getColor()->setARGB($this->readColor($fontStyleXml->color));
|
||||||
|
|
||||||
if (isset($fontStyleXml->u) && !isset($fontStyleXml->u['val'])) {
|
if (isset($fontStyleXml->u)) {
|
||||||
|
$attr = $this->getStyleAttributes($fontStyleXml->u);
|
||||||
|
if (!isset($attr['val'])) {
|
||||||
$fontStyle->setUnderline(Font::UNDERLINE_SINGLE);
|
$fontStyle->setUnderline(Font::UNDERLINE_SINGLE);
|
||||||
} elseif (isset($fontStyleXml->u, $fontStyleXml->u['val'])) {
|
} else {
|
||||||
$fontStyle->setUnderline((string) $fontStyleXml->u['val']);
|
$fontStyle->setUnderline((string) $attr['val']);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (isset($fontStyleXml->vertAlign, $fontStyleXml->vertAlign['val'])) {
|
if (isset($fontStyleXml->vertAlign)) {
|
||||||
$verticalAlign = strtolower((string) $fontStyleXml->vertAlign['val']);
|
$attr = $this->getStyleAttributes($fontStyleXml->vertAlign);
|
||||||
|
if (!isset($attr['val'])) {
|
||||||
|
$verticalAlign = strtolower((string) $attr['val']);
|
||||||
if ($verticalAlign === 'superscript') {
|
if ($verticalAlign === 'superscript') {
|
||||||
$fontStyle->setSuperscript(true);
|
$fontStyle->setSuperscript(true);
|
||||||
} elseif ($verticalAlign === 'subscript') {
|
} elseif ($verticalAlign === 'subscript') {
|
||||||
|
|
@ -86,14 +128,17 @@ class Styles extends BaseParserClass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
|
private function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
|
||||||
{
|
{
|
||||||
if ($numfmtStyleXml->count() === 0) {
|
if ((string) $numfmtStyleXml['formatCode'] !== '') {
|
||||||
|
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmtStyleXml['formatCode']));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
|
$numfmt = $this->getStyleAttributes($numfmtStyleXml);
|
||||||
if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
|
if (isset($numfmt['formatCode'])) {
|
||||||
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
|
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -103,10 +148,11 @@ class Styles extends BaseParserClass
|
||||||
if ($fillStyleXml->gradientFill) {
|
if ($fillStyleXml->gradientFill) {
|
||||||
/** @var SimpleXMLElement $gradientFill */
|
/** @var SimpleXMLElement $gradientFill */
|
||||||
$gradientFill = $fillStyleXml->gradientFill[0];
|
$gradientFill = $fillStyleXml->gradientFill[0];
|
||||||
if (!empty($gradientFill['type'])) {
|
$attr = $this->getStyleAttributes($gradientFill);
|
||||||
$fillStyle->setFillType((string) $gradientFill['type']);
|
if (!empty($attr['type'])) {
|
||||||
|
$fillStyle->setFillType((string) $attr['type']);
|
||||||
}
|
}
|
||||||
$fillStyle->setRotation((float) ($gradientFill['degree']));
|
$fillStyle->setRotation((float) ($attr['degree']));
|
||||||
$gradientFill->registerXPathNamespace('sml', Namespaces::MAIN);
|
$gradientFill->registerXPathNamespace('sml', Namespaces::MAIN);
|
||||||
$fillStyle->getStartColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
|
$fillStyle->getStartColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
|
||||||
$fillStyle->getEndColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
|
$fillStyle->getEndColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
|
||||||
|
|
@ -121,9 +167,14 @@ class Styles extends BaseParserClass
|
||||||
$defaultFillStyle = Fill::FILL_SOLID;
|
$defaultFillStyle = Fill::FILL_SOLID;
|
||||||
}
|
}
|
||||||
|
|
||||||
$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
|
$type = '';
|
||||||
? (string) $fillStyleXml->patternFill['patternType']
|
if ((string) $fillStyleXml->patternFill['patternType'] !== '') {
|
||||||
: $defaultFillStyle;
|
$type = (string) $fillStyleXml->patternFill['patternType'];
|
||||||
|
} else {
|
||||||
|
$attr = $this->getStyleAttributes($fillStyleXml->patternFill);
|
||||||
|
$type = (string) $attr['patternType'];
|
||||||
|
}
|
||||||
|
$patternType = ($type === '') ? $defaultFillStyle : $type;
|
||||||
|
|
||||||
$fillStyle->setFillType($patternType);
|
$fillStyle->setFillType($patternType);
|
||||||
}
|
}
|
||||||
|
|
@ -131,8 +182,10 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
public function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
|
public function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
|
||||||
{
|
{
|
||||||
$diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
|
$diagonalUp = $this->getAttribute($borderStyleXml, 'diagonalUp');
|
||||||
$diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
|
$diagonalUp = self::boolean($diagonalUp);
|
||||||
|
$diagonalDown = $this->getAttribute($borderStyleXml, 'diagonalDown');
|
||||||
|
$diagonalDown = self::boolean($diagonalDown);
|
||||||
if (!$diagonalUp && !$diagonalDown) {
|
if (!$diagonalUp && !$diagonalDown) {
|
||||||
$borderStyle->setDiagonalDirection(Borders::DIAGONAL_NONE);
|
$borderStyle->setDiagonalDirection(Borders::DIAGONAL_NONE);
|
||||||
} elseif ($diagonalUp && !$diagonalDown) {
|
} elseif ($diagonalUp && !$diagonalDown) {
|
||||||
|
|
@ -150,10 +203,26 @@ class Styles extends BaseParserClass
|
||||||
$this->readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
|
$this->readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getAttribute(SimpleXMLElement $xml, string $attribute): string
|
||||||
|
{
|
||||||
|
$style = '';
|
||||||
|
if ((string) $xml[$attribute] !== '') {
|
||||||
|
$style = (string) $xml[$attribute];
|
||||||
|
} else {
|
||||||
|
$attr = $this->getStyleAttributes($xml);
|
||||||
|
if (isset($attr[$attribute])) {
|
||||||
|
$style = (string) $attr[$attribute];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $style;
|
||||||
|
}
|
||||||
|
|
||||||
private function readBorder(Border $border, SimpleXMLElement $borderXml): void
|
private function readBorder(Border $border, SimpleXMLElement $borderXml): void
|
||||||
{
|
{
|
||||||
if (isset($borderXml['style'])) {
|
$style = $this->getAttribute($borderXml, 'style');
|
||||||
$border->setBorderStyle((string) $borderXml['style']);
|
if ($style !== '') {
|
||||||
|
$border->setBorderStyle((string) $style);
|
||||||
}
|
}
|
||||||
if (isset($borderXml->color)) {
|
if (isset($borderXml->color)) {
|
||||||
$border->getColor()->setARGB($this->readColor($borderXml->color));
|
$border->getColor()->setARGB($this->readColor($borderXml->color));
|
||||||
|
|
@ -162,25 +231,25 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
|
public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
|
||||||
{
|
{
|
||||||
$alignment->setHorizontal((string) $alignmentXml['horizontal']);
|
$horizontal = $this->getAttribute($alignmentXml, 'horizontal');
|
||||||
$alignment->setVertical((string) $alignmentXml['vertical']);
|
$alignment->setHorizontal($horizontal);
|
||||||
|
$vertical = $this->getAttribute($alignmentXml, 'vertical');
|
||||||
|
$alignment->setVertical((string) $vertical);
|
||||||
|
|
||||||
$textRotation = 0;
|
$textRotation = (int) $this->getAttribute($alignmentXml, 'textRotation');
|
||||||
if ((int) $alignmentXml['textRotation'] <= 90) {
|
if ($textRotation > 90) {
|
||||||
$textRotation = (int) $alignmentXml['textRotation'];
|
$textRotation = 90 - $textRotation;
|
||||||
} elseif ((int) $alignmentXml['textRotation'] > 90) {
|
|
||||||
$textRotation = 90 - (int) $alignmentXml['textRotation'];
|
|
||||||
}
|
}
|
||||||
|
$alignment->setTextRotation($textRotation);
|
||||||
|
|
||||||
$alignment->setTextRotation((int) $textRotation);
|
$wrapText = $this->getAttribute($alignmentXml, 'wrapText');
|
||||||
$alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
|
$alignment->setWrapText(self::boolean((string) $wrapText));
|
||||||
$alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
|
$shrinkToFit = $this->getAttribute($alignmentXml, 'shrinkToFit');
|
||||||
$alignment->setIndent(
|
$alignment->setShrinkToFit(self::boolean((string) $shrinkToFit));
|
||||||
(int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
|
$indent = (int) $this->getAttribute($alignmentXml, 'indent');
|
||||||
);
|
$alignment->setIndent(max($indent, 0));
|
||||||
$alignment->setReadOrder(
|
$readingOrder = (int) $this->getAttribute($alignmentXml, 'readingOrder');
|
||||||
(int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
|
$alignment->setReadOrder(max($readingOrder, 0));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function formatGeneral(string $formatString): string
|
private static function formatGeneral(string $formatString): string
|
||||||
|
|
@ -223,8 +292,8 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
// protection
|
// protection
|
||||||
if (isset($style->protection)) {
|
if (isset($style->protection)) {
|
||||||
$this->readProtectionLocked($docStyle, $style);
|
$this->readProtectionLocked($docStyle, $style->protection);
|
||||||
$this->readProtectionHidden($docStyle, $style);
|
$this->readProtectionHidden($docStyle, $style->protection);
|
||||||
}
|
}
|
||||||
|
|
||||||
// top-level style settings
|
// top-level style settings
|
||||||
|
|
@ -235,13 +304,20 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read protection locked attribute.
|
* Read protection locked attribute.
|
||||||
*
|
|
||||||
* @param SimpleXMLElement|stdClass $style
|
|
||||||
*/
|
*/
|
||||||
public function readProtectionLocked(Style $docStyle, $style): void
|
public function readProtectionLocked(Style $docStyle, SimpleXMLElement $style): void
|
||||||
{
|
{
|
||||||
if (isset($style->protection['locked'])) {
|
$locked = '';
|
||||||
if (self::boolean((string) $style->protection['locked'])) {
|
if ((string) $style['locked'] !== '') {
|
||||||
|
$locked = (string) $style['locked'];
|
||||||
|
} else {
|
||||||
|
$attr = $this->getStyleAttributes($style);
|
||||||
|
if (isset($attr['locked'])) {
|
||||||
|
$locked = (string) $attr['locked'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($locked !== '') {
|
||||||
|
if (self::boolean($locked)) {
|
||||||
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
|
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
|
||||||
} else {
|
} else {
|
||||||
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
|
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
|
||||||
|
|
@ -251,13 +327,20 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read protection hidden attribute.
|
* Read protection hidden attribute.
|
||||||
*
|
|
||||||
* @param SimpleXMLElement|stdClass $style
|
|
||||||
*/
|
*/
|
||||||
public function readProtectionHidden(Style $docStyle, $style): void
|
public function readProtectionHidden(Style $docStyle, SimpleXMLElement $style): void
|
||||||
{
|
{
|
||||||
if (isset($style->protection['hidden'])) {
|
$hidden = '';
|
||||||
if (self::boolean((string) $style->protection['hidden'])) {
|
if ((string) $style['hidden'] !== '') {
|
||||||
|
$hidden = (string) $style['hidden'];
|
||||||
|
} else {
|
||||||
|
$attr = $this->getStyleAttributes($style);
|
||||||
|
if (isset($attr['hidden'])) {
|
||||||
|
$hidden = (string) $attr['hidden'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($hidden !== '') {
|
||||||
|
if (self::boolean((string) $hidden)) {
|
||||||
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
|
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
|
||||||
} else {
|
} else {
|
||||||
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
|
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
|
||||||
|
|
@ -267,15 +350,18 @@ class Styles extends BaseParserClass
|
||||||
|
|
||||||
public function readColor(SimpleXMLElement $color, bool $background = false): string
|
public function readColor(SimpleXMLElement $color, bool $background = false): string
|
||||||
{
|
{
|
||||||
if (isset($color['rgb'])) {
|
$attr = $this->getStyleAttributes($color);
|
||||||
return (string) $color['rgb'];
|
if (isset($attr['rgb'])) {
|
||||||
} elseif (isset($color['indexed'])) {
|
return (string) $attr['rgb'];
|
||||||
return Color::indexedColor((int) ($color['indexed'] - 7), $background)->getARGB() ?? '';
|
}
|
||||||
} elseif (isset($color['theme'])) {
|
if (isset($attr['indexed'])) {
|
||||||
|
return Color::indexedColor((int) ($attr['indexed'] - 7), $background)->getARGB() ?? '';
|
||||||
|
}
|
||||||
|
if (isset($attr['theme'])) {
|
||||||
if ($this->theme !== null) {
|
if ($this->theme !== null) {
|
||||||
$returnColour = $this->theme->getColourByIndex((int) $color['theme']);
|
$returnColour = $this->theme->getColourByIndex((int) $attr['theme']);
|
||||||
if (isset($color['tint'])) {
|
if (isset($attr['tint'])) {
|
||||||
$tintAdjust = (float) $color['tint'];
|
$tintAdjust = (float) $attr['tint'];
|
||||||
$returnColour = Color::changeBrightness($returnColour ?? '', $tintAdjust);
|
$returnColour = Color::changeBrightness($returnColour ?? '', $tintAdjust);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,24 @@ class Color extends Supervisor
|
||||||
const COLOR_DARKGREEN = 'FF008000';
|
const COLOR_DARKGREEN = 'FF008000';
|
||||||
const COLOR_YELLOW = 'FFFFFF00';
|
const COLOR_YELLOW = 'FFFFFF00';
|
||||||
const COLOR_DARKYELLOW = 'FF808000';
|
const COLOR_DARKYELLOW = 'FF808000';
|
||||||
|
const COLOR_MAGENTA = 'FFFF00FF';
|
||||||
|
const COLOR_CYAN = 'FF00FFFF';
|
||||||
|
|
||||||
|
const NAMED_COLOR_TRANSLATIONS = [
|
||||||
|
'Black' => self::COLOR_BLACK,
|
||||||
|
'White' => self::COLOR_WHITE,
|
||||||
|
'Red' => self::COLOR_RED,
|
||||||
|
'Green' => self::COLOR_GREEN,
|
||||||
|
'Blue' => self::COLOR_BLUE,
|
||||||
|
'Yellow' => self::COLOR_YELLOW,
|
||||||
|
'Magenta' => self::COLOR_MAGENTA,
|
||||||
|
'Cyan' => self::COLOR_CYAN,
|
||||||
|
];
|
||||||
|
|
||||||
const VALIDATE_ARGB_SIZE = 8;
|
const VALIDATE_ARGB_SIZE = 8;
|
||||||
const VALIDATE_RGB_SIZE = 6;
|
const VALIDATE_RGB_SIZE = 6;
|
||||||
const VALIDATE_COLOR_VALUE = '/^[A-F0-9]{%d}$/i';
|
const VALIDATE_COLOR_6 = '/^[A-F0-9]{6}$/i';
|
||||||
|
const VALIDATE_COLOR_8 = '/^[A-F0-9]{8}$/i';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indexed colors array.
|
* Indexed colors array.
|
||||||
|
|
@ -66,7 +80,7 @@ class Color extends Supervisor
|
||||||
|
|
||||||
// Initialise values
|
// Initialise values
|
||||||
if (!$isConditional) {
|
if (!$isConditional) {
|
||||||
$this->argb = $this->validateColor($colorValue, self::VALIDATE_ARGB_SIZE) ? $colorValue : self::COLOR_BLACK;
|
$this->argb = $this->validateColor($colorValue) ?: self::COLOR_BLACK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,10 +149,23 @@ class Color extends Supervisor
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateColor(string $colorValue, int $size): bool
|
private function validateColor(?string $colorValue): string
|
||||||
{
|
{
|
||||||
return in_array(ucfirst(strtolower($colorValue)), self::NAMED_COLORS) ||
|
if ($colorValue === null || $colorValue === '') {
|
||||||
preg_match(sprintf(self::VALIDATE_COLOR_VALUE, $size), $colorValue);
|
return self::COLOR_BLACK;
|
||||||
|
}
|
||||||
|
$named = ucfirst(strtolower($colorValue));
|
||||||
|
if (array_key_exists($named, self::NAMED_COLOR_TRANSLATIONS)) {
|
||||||
|
return self::NAMED_COLOR_TRANSLATIONS[$named];
|
||||||
|
}
|
||||||
|
if (preg_match(self::VALIDATE_COLOR_8, $colorValue) === 1) {
|
||||||
|
return $colorValue;
|
||||||
|
}
|
||||||
|
if (preg_match(self::VALIDATE_COLOR_6, $colorValue) === 1) {
|
||||||
|
return 'FF' . $colorValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -163,9 +190,8 @@ class Color extends Supervisor
|
||||||
public function setARGB(?string $colorValue = self::COLOR_BLACK)
|
public function setARGB(?string $colorValue = self::COLOR_BLACK)
|
||||||
{
|
{
|
||||||
$this->hasChanged = true;
|
$this->hasChanged = true;
|
||||||
if ($colorValue === '' || $colorValue === null) {
|
$colorValue = $this->validateColor($colorValue);
|
||||||
$colorValue = self::COLOR_BLACK;
|
if ($colorValue === '') {
|
||||||
} elseif (!$this->validateColor($colorValue, self::VALIDATE_ARGB_SIZE)) {
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -200,21 +226,7 @@ class Color extends Supervisor
|
||||||
*/
|
*/
|
||||||
public function setRGB(?string $colorValue = self::COLOR_BLACK)
|
public function setRGB(?string $colorValue = self::COLOR_BLACK)
|
||||||
{
|
{
|
||||||
$this->hasChanged = true;
|
return $this->setARGB($colorValue);
|
||||||
if ($colorValue === '' || $colorValue === null) {
|
|
||||||
$colorValue = '000000';
|
|
||||||
} elseif (!$this->validateColor($colorValue, self::VALIDATE_RGB_SIZE)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isSupervisor) {
|
|
||||||
$styleArray = $this->getStyleArray(['argb' => 'FF' . $colorValue]);
|
|
||||||
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
|
|
||||||
} else {
|
|
||||||
$this->argb = 'FF' . $colorValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ class CommentTest extends TestCase
|
||||||
{
|
{
|
||||||
$comment = new Comment();
|
$comment = new Comment();
|
||||||
$comment->setFillColor(new Color('RED'));
|
$comment->setFillColor(new Color('RED'));
|
||||||
self::assertEquals('RED', $comment->getFillColor()->getARGB());
|
self::assertEquals(Color::COLOR_RED, $comment->getFillColor()->getARGB());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetAlignment(): void
|
public function testSetAlignment(): void
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class Issue2494Test extends TestCase
|
||||||
|
{
|
||||||
|
public function testIssue2494(): void
|
||||||
|
{
|
||||||
|
// Fill style incorrect.
|
||||||
|
$filename = 'tests/data/Reader/XLSX/issue.2494.xlsx';
|
||||||
|
$reader = IOFactory::createReader('Xlsx');
|
||||||
|
$spreadsheet = $reader->load($filename);
|
||||||
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
|
self::assertTrue($sheet->getCell('C3')->getStyle()->getFont()->getBold());
|
||||||
|
self::assertSame('FFBFBFBF', $sheet->getCell('D8')->getStyle()->getFill()->getStartColor()->getArgb());
|
||||||
|
$spreadsheet->disconnectWorksheets();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -77,9 +77,9 @@ class NamespaceNonStdTest extends \PHPUnit\Framework\TestCase
|
||||||
$spreadsheet = $reader->load(self::$testbook);
|
$spreadsheet = $reader->load(self::$testbook);
|
||||||
$sheet = $spreadsheet->getSheet(0);
|
$sheet = $spreadsheet->getSheet(0);
|
||||||
self::assertEquals('SylkTest', $sheet->getTitle());
|
self::assertEquals('SylkTest', $sheet->getTitle());
|
||||||
if (strpos(__FILE__, 'NonStd') !== false) {
|
//if (strpos(__FILE__, 'NonStd') !== false) {
|
||||||
self::markTestIncomplete('Not yet ready');
|
// self::markTestIncomplete('Not yet ready');
|
||||||
}
|
//}
|
||||||
|
|
||||||
self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB());
|
self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB());
|
||||||
self::assertEquals(Fill::FILL_PATTERN_GRAY125, $sheet->getCell('A2')->getStyle()->getFill()->getFillType());
|
self::assertEquals(Fill::FILL_PATTERN_GRAY125, $sheet->getCell('A2')->getStyle()->getFill()->getFillType());
|
||||||
|
|
@ -168,9 +168,9 @@ class NamespaceNonStdTest extends \PHPUnit\Framework\TestCase
|
||||||
$spreadsheet = $reader->load(self::$testbook);
|
$spreadsheet = $reader->load(self::$testbook);
|
||||||
$sheet = $spreadsheet->getSheet(1);
|
$sheet = $spreadsheet->getSheet(1);
|
||||||
self::assertEquals('Second', $sheet->getTitle());
|
self::assertEquals('Second', $sheet->getTitle());
|
||||||
if (strpos(__FILE__, 'NonStd') !== false) {
|
//if (strpos(__FILE__, 'NonStd') !== false) {
|
||||||
self::markTestIncomplete('Not yet ready');
|
// self::markTestIncomplete('Not yet ready');
|
||||||
}
|
//}
|
||||||
self::assertEquals('center', $sheet->getCell('A2')->getStyle()->getAlignment()->getHorizontal());
|
self::assertEquals('center', $sheet->getCell('A2')->getStyle()->getAlignment()->getHorizontal());
|
||||||
self::assertSame('inherit', $sheet->getCell('A2')->getStyle()->getProtection()->getLocked());
|
self::assertSame('inherit', $sheet->getCell('A2')->getStyle()->getProtection()->getLocked());
|
||||||
self::assertEquals('top', $sheet->getCell('A3')->getStyle()->getAlignment()->getVertical());
|
self::assertEquals('top', $sheet->getCell('A3')->getStyle()->getAlignment()->getVertical());
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
||||||
|
|
||||||
|
class RichTextTest extends AbstractFunctional
|
||||||
|
{
|
||||||
|
public function testRichTextColors(): void
|
||||||
|
{
|
||||||
|
$spreadsheetOld = new Spreadsheet();
|
||||||
|
$sheet = $spreadsheetOld->getActiveSheet();
|
||||||
|
$richText = new RichText();
|
||||||
|
$part1 = $richText->createTextRun('Red');
|
||||||
|
$font1 = $part1->getFont();
|
||||||
|
if ($font1 !== null) {
|
||||||
|
$font1->setName('Courier New');
|
||||||
|
$font1->getColor()->setArgb('FFFF0000');
|
||||||
|
}
|
||||||
|
$part2 = $richText->createTextRun('Blue');
|
||||||
|
$font2 = $part2->getFont();
|
||||||
|
if ($font2 !== null) {
|
||||||
|
$font2->setName('Times New Roman');
|
||||||
|
$font2->setItalic(true);
|
||||||
|
$font2->getColor()->setArgb('FF0000FF');
|
||||||
|
}
|
||||||
|
$sheet->setCellValue('A1', $richText);
|
||||||
|
|
||||||
|
$spreadsheet = $this->writeAndReload($spreadsheetOld, 'Xlsx');
|
||||||
|
$spreadsheetOld->disconnectWorksheets();
|
||||||
|
$rsheet = $spreadsheet->getActiveSheet();
|
||||||
|
$value = $rsheet->getCell('A1')->getValue();
|
||||||
|
if ($value instanceof RichText) {
|
||||||
|
$elements = $value->getRichTextElements();
|
||||||
|
self::assertCount(2, $elements);
|
||||||
|
$font1a = $elements[0]->getFont();
|
||||||
|
$font2a = $elements[1]->getFont();
|
||||||
|
self::assertNotNull($font1a);
|
||||||
|
self::assertNotNull($font2a);
|
||||||
|
self::assertSame('Courier New', $font1a->getName());
|
||||||
|
self::assertSame('FFFF0000', $font1a->getColor()->getArgb());
|
||||||
|
self::assertFalse($font1a->getItalic());
|
||||||
|
self::assertSame('Times New Roman', $font2a->getName());
|
||||||
|
self::assertSame('FF0000FF', $font2a->getColor()->getArgb());
|
||||||
|
self::assertTrue($font2a->getItalic());
|
||||||
|
} else {
|
||||||
|
self::fail('Did not see expected RichText');
|
||||||
|
}
|
||||||
|
$spreadsheet->disconnectWorksheets();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -157,4 +157,31 @@ class ColorTest extends TestCase
|
||||||
self::assertEquals(Color::COLOR_BLACK, $color->getARGB());
|
self::assertEquals(Color::COLOR_BLACK, $color->getARGB());
|
||||||
self::assertEquals('000000', $color->getRGB());
|
self::assertEquals('000000', $color->getRGB());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNamedColors(): void
|
||||||
|
{
|
||||||
|
$color = new Color();
|
||||||
|
$color->setARGB('Blue');
|
||||||
|
self::assertEquals(Color::COLOR_BLUE, $color->getARGB());
|
||||||
|
$color->setARGB('black');
|
||||||
|
self::assertEquals(Color::COLOR_BLACK, $color->getARGB());
|
||||||
|
$color->setARGB('wHite');
|
||||||
|
self::assertEquals(Color::COLOR_WHITE, $color->getARGB());
|
||||||
|
$color->setRGB('reD');
|
||||||
|
self::assertEquals(Color::COLOR_RED, $color->getARGB());
|
||||||
|
$color->setRGB('GREEN');
|
||||||
|
self::assertEquals(Color::COLOR_GREEN, $color->getARGB());
|
||||||
|
$color->setRGB('magenta');
|
||||||
|
self::assertEquals(Color::COLOR_MAGENTA, $color->getARGB());
|
||||||
|
$color->setRGB('YeLlOw');
|
||||||
|
self::assertEquals(Color::COLOR_YELLOW, $color->getARGB());
|
||||||
|
$color->setRGB('CYAN');
|
||||||
|
self::assertEquals(Color::COLOR_CYAN, $color->getARGB());
|
||||||
|
$color->setRGB('123456ab');
|
||||||
|
self::assertEquals('123456ab', $color->getARGB());
|
||||||
|
self::assertEquals('3456ab', $color->getRGB());
|
||||||
|
$color->setARGB('3456cd');
|
||||||
|
self::assertEquals('FF3456cd', $color->getARGB());
|
||||||
|
self::assertEquals('3456cd', $color->getRGB());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue