Merge "Fix for tex-statement definecolor"

This commit is contained in:
jenkins-bot 2023-03-02 11:16:20 +00:00 committed by Gerrit Code Review
commit 35257884ea
3 changed files with 86 additions and 2 deletions

View file

@ -58,4 +58,34 @@ class MMLParsingUtil {
}
return $args;
}
/**
* Parses an expression that defines a color; this is usually an argument in Literal.
* Example expression is: "\definecolor {ultramarine}{rgb}{0,0.12549019607843,0.37647058823529}"
* @param string $input tex-string, which contains the expression
* @return array|null either an array which contains hex of parsed expression or null if not parsable
*/
public static function parseDefineColorExpression( string $input ): ?array {
$returnObj = null;
$matches = [];
$matched = preg_match_all( '/\{(.*?)\}/', $input, $matches );
if ( !$matched ) {
return null;
}
$ctr = count( $matches[1] ?? [] );
if ( $ctr == 3 && $matches[1][1] === "rgb" ) {
$returnObj = [];
$rgbValues = explode( ",", $matches[1][2] );
$r = round( floatval( $rgbValues[0] ) * 255 );
$g = round( floatval( $rgbValues[1] ) * 255 );
$b = round( floatval( $rgbValues[2] ) * 255 );
$color = sprintf( "#%02x%02x%02x", $r, $g, $b );
$returnObj["name"] = $matches[1][0];
$returnObj["type"] = "rgb";
$returnObj["hex"] = $color;
}
return $returnObj;
}
}

View file

@ -5,6 +5,7 @@ declare( strict_types = 1 );
namespace MediaWiki\Extension\Math\TexVC\Nodes;
use InvalidArgumentException;
use MediaWiki\Extension\Math\TexVC\MMLmappings\Util\MMLParsingUtil;
use MediaWiki\Extension\Math\TexVC\MMLmappings\Util\MMLutil;
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmstyle;
@ -64,6 +65,16 @@ class TexArray extends TexNode {
return [ false, null ];
}
public function checkForColorDefinition( TexNode $node ) {
if ( $node instanceof Literal ) {
$name = trim( $node->getArg() );
if ( str_contains( $name, "\\definecolor" ) ) {
return MMLParsingUtil::parseDefineColorExpression( $node->getArg() );
}
}
return null;
}
public function renderMML( $arguments = [], $state = [] ) {
// Everything here is for parsing displaystyle, probably refactored to TexVC grammar later
$fullRenderedArray = "";
@ -76,7 +87,12 @@ class TexArray extends TexNode {
} else {
$next = null;
}
// Check if there is a new color definition and add it to state
$foundColorDef = $this->checkForColorDefinition( $current );
if ( $foundColorDef ) {
$state["colorDefinitions"][$foundColorDef["name"]] = $foundColorDef;
continue;
}
// Pass preceding color info to state
$foundColor = $this->checkForColor( $current );
if ( $foundColor[0] ) {
@ -107,7 +123,12 @@ class TexArray extends TexNode {
private function renderMMLwithColor( $currentColor, $currentNode, $state, $arguments ) {
if ( $currentColor ) {
$mmlStyleColor = new MMLmstyle( "", [ "mathcolor" => $currentColor ] );
if ( array_key_exists( $currentColor, $state["colorDefinitions"] ?? [] ) ) {
$displayedColor = $state["colorDefinitions"][$currentColor]["hex"];
} else {
$displayedColor = $currentColor;
}
$mmlStyleColor = new MMLmstyle( "", [ "mathcolor" => $displayedColor ] );
return $mmlStyleColor->encapsulateRaw( $currentNode->renderMML( $arguments, $state ) );
} else {
return $currentNode->renderMML( $arguments, $state );

View file

@ -0,0 +1,33 @@
<?php
namespace MediaWiki\Extension\Math\Tests\TexVC\MMLmappings;
use MediaWiki\Extension\Math\TexVC\MMLmappings\Util\MMLParsingUtil;
use MediaWikiUnitTestCase;
/**
* @covers \MediaWiki\Extension\Math\TexVC\MMLmappings\Util\MMLParsingUtil
*/
class MMLParsingUtilTest extends MediaWikiUnitTestCase {
public function testInvalidColor() {
$result = MMLParsingUtil::parseDefineColorExpression( "INVALID" );
$this->assertNull( $result );
}
public function testRGBOne() {
$result = MMLParsingUtil::parseDefineColorExpression(
"\\definecolor {ultramarine}{rgb}{0,0.12549019607843,0.37647058823529}" );
$this->assertEquals( 'ultramarine', $result['name'] );
$this->assertEquals( 'rgb', $result['type'] );
$this->assertEquals( '#002060', $result['hex'] );
}
public function testInvalidColorString() {
$result = MMLParsingUtil::parseDefineColorExpression(
"\\definecolor {gray}{0.123}" );
$this->assertNull( $result );
}
}