| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\Tag; |
| 6 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\TexClass; |
| 7 |
use MediaWiki\Extension\Math\WikiTexVC\MMLmappings\TexConstants\Variants; |
| 8 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmo; |
| 9 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmpadded; |
| 10 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmrow; |
| 11 |
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmtext; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class MMLParsingUtil { |
| 19 |
public static function renderApplyFunction() { |
| 20 |
$mo = new MMLmo(); |
| 21 |
return $mo->encapsulateRaw( "⁡" ); |
| 22 |
} |
| 23 |
|
| 24 |
public static function getFontArgs( string $name, ?string $variant, ?array $passedArgs ): array { |
| 25 |
$args = []; |
| 26 |
switch ( $name ) { |
| 27 |
case "cal": |
| 28 |
case "mathcal": |
| 29 |
$args = [ Tag::MJXVARIANT => "-tex-calligraphic", "mathvariant" => Variants::SCRIPT ]; |
| 30 |
break; |
| 31 |
case "it": |
| 32 |
case "mathit": |
| 33 |
$args = [ Tag::MJXVARIANT => $variant, "mathvariant" => Variants::ITALIC ]; |
| 34 |
break; |
| 35 |
case "bf": |
| 36 |
case "mathbf": |
| 37 |
$args = [ "mathvariant" => $variant ]; |
| 38 |
break; |
| 39 |
|
| 40 |
case "textit": |
| 41 |
$args = [ "mathvariant" => Variants::ITALIC ]; |
| 42 |
break; |
| 43 |
case "textbf": |
| 44 |
$args = [ "mathvariant" => Variants::BOLD ]; |
| 45 |
break; |
| 46 |
case "textsf": |
| 47 |
$args = [ "mathvariant" => Variants::SANSSERIF ]; |
| 48 |
break; |
| 49 |
case "texttt": |
| 50 |
$args = [ "mathvariant" => Variants::MONOSPACE ]; |
| 51 |
break; |
| 52 |
case "textrm": |
| 53 |
break; |
| 54 |
case "emph": |
| 55 |
|
| 56 |
if ( isset( $passedArgs["mathvariant"] ) ) { |
| 57 |
if ( $passedArgs["mathvariant"] === Variants::ITALIC ) { |
| 58 |
$args = [ "mathvariant" => Variants::NORMAL ]; |
| 59 |
} |
| 60 |
} else { |
| 61 |
$args = [ "mathvariant" => Variants::ITALIC ]; |
| 62 |
} |
| 63 |
break; |
| 64 |
default: |
| 65 |
$args = [ "mathvariant" => $variant ]; |
| 66 |
|
| 67 |
} |
| 68 |
return $args; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static function parseDefineColorExpression( string $input ): ?array { |
| 78 |
$returnObj = null; |
| 79 |
$matches = []; |
| 80 |
$matched = preg_match_all( '/\{(.*?)\}/', $input, $matches ); |
| 81 |
if ( !$matched ) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
$ctr = count( $matches[1] ?? [] ); |
| 85 |
|
| 86 |
if ( $ctr == 3 && $matches[1][1] === "rgb" ) { |
| 87 |
$returnObj = []; |
| 88 |
$rgbValues = explode( ",", $matches[1][2] ); |
| 89 |
$r = round( floatval( $rgbValues[0] ) * 255 ); |
| 90 |
$g = round( floatval( $rgbValues[1] ) * 255 ); |
| 91 |
$b = round( floatval( $rgbValues[2] ) * 255 ); |
| 92 |
$color = sprintf( "#%02x%02x%02x", $r, $g, $b ); |
| 93 |
$returnObj["name"] = $matches[1][0]; |
| 94 |
$returnObj["type"] = "rgb"; |
| 95 |
$returnObj["hex"] = $color; |
| 96 |
} |
| 97 |
|
| 98 |
return $returnObj; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static function createNot() { |
| 106 |
$mmlMrow = new MMLmrow( TexClass::REL ); |
| 107 |
$mpadded = new MMLmpadded( "", [ "width" => "0" ] ); |
| 108 |
$mtext = new MMLmtext(); |
| 109 |
return $mmlMrow->encapsulateRaw( $mpadded->encapsulateRaw( $mtext->encapsulateRaw( "⧸" ) ) ); |
| 110 |
} |
| 111 |
|
| 112 |
public static function mapToDoubleStruckUnicode( string $inputString ): string { |
| 113 |
$map = [ |
| 114 |
'0' => '𝟘', '1' => '𝟙', '2' => '𝟚', '3' => '𝟛', '4' => '𝟜', |
| 115 |
'5' => '𝟝', '6' => '𝟞', '7' => '𝟟', '8' => '𝟠', '9' => '𝟡', |
| 116 |
'A' => '𝔸', 'B' => '𝔹', 'C' => 'ℂ', 'D' => '𝔻', 'E' => '𝔼', |
| 117 |
'F' => '𝔽', 'G' => '𝔾', 'H' => 'ℍ', 'I' => '𝕀', 'J' => '𝕁', |
| 118 |
'K' => '𝕂', 'L' => '𝕃', 'M' => '𝕄', 'N' => 'ℕ', 'O' => '𝕆', |
| 119 |
'P' => 'ℙ', 'Q' => 'ℚ', 'R' => 'ℝ', 'S' => '𝕊', 'T' => '𝕋', |
| 120 |
'U' => '𝕌', 'V' => '𝕍', 'W' => '𝕎', 'X' => '𝕏', 'Y' => '𝕐', |
| 121 |
'Z' => 'ℤ', 'a' => '𝕒', 'b' => '𝕓', 'c' => '𝕔', 'd' => '𝕕', |
| 122 |
'e' => '𝕖', 'f' => '𝕗', 'g' => '𝕘', 'h' => '𝕙', 'i' => '𝕚', |
| 123 |
'j' => '𝕛', 'k' => '𝕜', 'l' => '𝕝', 'm' => '𝕞', 'n' => '𝕟', |
| 124 |
'o' => '𝕠', 'p' => '𝕡', 'q' => '𝕢', 'r' => '𝕣', 's' => '𝕤', |
| 125 |
't' => '𝕥', 'u' => '𝕦', 'v' => '𝕧', 'w' => '𝕨', 'x' => '𝕩', |
| 126 |
'y' => '𝕪', 'z' => '𝕫' |
| 127 |
]; |
| 128 |
|
| 129 |
return self::matchAlphanumeric( $inputString, $map ); |
| 130 |
} |
| 131 |
|
| 132 |
public static function mapToCaligraphicUnicode( string $inputString ): string { |
| 133 |
$map = [ |
| 134 |
'0' => '𝟎', '1' => '𝟏', '2' => '𝟐', '3' => '𝟑', '4' => '𝟒', |
| 135 |
'5' => '𝟓', '6' => '𝟔', '7' => '𝟕', '8' => '𝟖', '9' => '𝟗', |
| 136 |
'A' => '𝒜', 'B' => 'ℬ', 'C' => '𝒞', 'D' => '𝒟', 'E' => 'ℰ', |
| 137 |
'F' => 'ℱ', 'G' => '𝒢', 'H' => 'ℋ', 'I' => 'ℐ', 'J' => '𝒥', |
| 138 |
'K' => '𝒦', 'L' => 'ℒ', 'M' => 'ℳ', 'N' => '𝒩', 'O' => '𝒪', |
| 139 |
'P' => '𝒫', 'Q' => '𝒬', 'R' => 'ℛ', 'S' => '𝒮', 'T' => '𝒯', |
| 140 |
'U' => '𝒰', 'V' => '𝒱', 'W' => '𝒲', 'X' => '𝒳', 'Y' => '𝒴', |
| 141 |
'Z' => '𝒵', 'a' => '𝒶', 'b' => '𝒷', 'c' => '𝒸', 'd' => '𝒹', |
| 142 |
'e' => 'ℯ', 'f' => '𝒻', 'g' => 'ℊ', 'h' => '𝒽', 'i' => '𝒾', |
| 143 |
'j' => '𝒿', 'k' => '𝓀', 'l' => '𝓁', 'm' => '𝓂', 'n' => '𝓃', |
| 144 |
'o' => 'ℴ', 'p' => '𝓅', 'q' => '𝓆', 'r' => '𝓇', 's' => '𝓈', |
| 145 |
't' => '𝓉', 'u' => '𝓊', 'v' => '𝓋', 'w' => '𝓌', 'x' => '𝓍', |
| 146 |
'y' => '𝓎', 'z' => '𝓏' |
| 147 |
]; |
| 148 |
|
| 149 |
return self::matchAlphanumeric( $inputString, $map ); |
| 150 |
} |
| 151 |
|
| 152 |
public static function mapToFrakturUnicode( string $inputString ): string { |
| 153 |
$res = ''; |
| 154 |
$specialCases = [ 'C' => 'ℭ', |
| 155 |
'H' => 'ℌ', |
| 156 |
'I' => 'ℑ', |
| 157 |
'R' => 'ℜ', |
| 158 |
'Z' => 'ℤ' ]; |
| 159 |
foreach ( mb_str_split( $inputString ) as $chr ) { |
| 160 |
|
| 161 |
if ( isset( $specialCases[$chr] ) ) { |
| 162 |
$res .= $specialCases[$chr]; |
| 163 |
continue; |
| 164 |
} |
| 165 |
if ( $chr >= 'A' && $chr <= 'Z' ) { |
| 166 |
$code = self::addToChr( $chr, '1D4C3' ); |
| 167 |
$res .= '&#x' . $code . ';'; |
| 168 |
} elseif ( $chr >= 'a' && $chr <= 'z' ) { |
| 169 |
$code = self::addToChr( $chr, '1D4BD' ); |
| 170 |
$res .= '&#x' . $code . ';'; |
| 171 |
} else { |
| 172 |
$res .= $chr; |
| 173 |
} |
| 174 |
} |
| 175 |
return $res; |
| 176 |
} |
| 177 |
|
| 178 |
public static function mapToBoldUnicode( string $inputString ): string { |
| 179 |
$res = ''; |
| 180 |
foreach ( mb_str_split( $inputString ) as $chr ) { |
| 181 |
|
| 182 |
if ( $chr >= 'A' && $chr <= 'Z' ) { |
| 183 |
$code = self::addToChr( $chr, '1D3BF' ); |
| 184 |
$res .= '&#x' . $code . ';'; |
| 185 |
} elseif ( $chr >= 'a' && $chr <= 'z' ) { |
| 186 |
$code = self::addToChr( $chr, '1D3B9' ); |
| 187 |
$res .= '&#x' . $code . ';'; |
| 188 |
} elseif ( $chr >= '0' && $chr <= '9' ) { |
| 189 |
$code = self::addToChr( $chr, '1D79E' ); |
| 190 |
$res .= '&#x' . $code . ';'; |
| 191 |
} else { |
| 192 |
$res .= $chr; |
| 193 |
} |
| 194 |
} |
| 195 |
return $res; |
| 196 |
} |
| 197 |
|
| 198 |
private static function addToChr( string $chr, string $base ): string { |
| 199 |
return strtoupper( dechex( mb_ord( $chr ) + hexdec( $base ) ) ); |
| 200 |
} |
| 201 |
|
| 202 |
public static function matchAlphanumeric( string $inputString, array $map ): string { |
| 203 |
|
| 204 |
return preg_replace_callback( '/[A-Za-z0-9]/u', static function ( $matches ) use ( $map ) { |
| 205 |
return $map[$matches[0]] ?? $matches[0]; |
| 206 |
}, $inputString ); |
| 207 |
} |
| 208 |
|
| 209 |
public static function getIntentContent( ?string $input ): ?string { |
| 210 |
if ( !$input ) { |
| 211 |
return null; |
| 212 |
} |
| 213 |
$matchesInt = []; |
| 214 |
$matchInt = preg_match( "/intent=[\'\"](.*)[\'\"]/", $input, $matchesInt ); |
| 215 |
if ( $matchInt && count( $matchesInt ) >= 2 ) { |
| 216 |
return $matchesInt[1]; |
| 217 |
} |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
public static function getIntentParams( ?string $intentContent ): ?array { |
| 222 |
if ( !$intentContent ) { |
| 223 |
return null; |
| 224 |
} |
| 225 |
$matchesParams = []; |
| 226 |
|
| 227 |
$matchParams = preg_match_all( "/\\\$([a-zA-Z]+)/", $intentContent, $matchesParams ); |
| 228 |
if ( $matchParams && count( $matchesParams ) >= 2 ) { |
| 229 |
return $matchesParams[1]; |
| 230 |
} |
| 231 |
return null; |
| 232 |
} |
| 233 |
|
| 234 |
public static function getIntentArgs( ?string $input ): ?string { |
| 235 |
if ( !$input ) { |
| 236 |
return null; |
| 237 |
} |
| 238 |
$matchesArgs = []; |
| 239 |
$matchArg = preg_match( "/arg\s*=\s*[\'\"](.*?)[\'\"]/", $input, $matchesArgs ); |
| 240 |
if ( $matchArg && count( $matchesArgs ) >= 2 ) { |
| 241 |
return $matchesArgs[1]; |
| 242 |
} |
| 243 |
return null; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public static function forgeIntentToTopElement( string $renderedMML, $intentContentAtr ) { |
| 257 |
if ( !$intentContentAtr || !$renderedMML ) { |
| 258 |
return $renderedMML; |
| 259 |
} |
| 260 |
|
| 261 |
return self::addAttributesToMML( $renderedMML, $intentContentAtr, "" ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public static function addAttributesToMML( |
| 273 |
string $renderedMML, array $intentContentAtr, string $elementTag, bool $useFoundNodes = false |
| 274 |
): string { |
| 275 |
$xml = simplexml_load_string( $renderedMML ); |
| 276 |
if ( !$xml ) { |
| 277 |
return ""; |
| 278 |
} |
| 279 |
if ( $useFoundNodes ) { |
| 280 |
$foundNodes = $xml->xpath( $elementTag ); |
| 281 |
if ( !( $foundNodes !== null && count( $foundNodes ) >= 1 ) ) { |
| 282 |
return $renderedMML; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if ( isset( $intentContentAtr["intent"] ) ) { |
| 287 |
if ( isset( $xml["intent"] ) ) { |
| 288 |
$xml["intent"] = $intentContentAtr["intent"]; |
| 289 |
} elseif ( $intentContentAtr["intent"] != null && is_string( $intentContentAtr["intent"] ) ) { |
| 290 |
$xml->addAttribute( "intent", $intentContentAtr["intent"] ); |
| 291 |
} |
| 292 |
} |
| 293 |
if ( isset( $intentContentAtr["arg"] ) ) { |
| 294 |
if ( isset( $xml["arg"] ) ) { |
| 295 |
$xml["arg"] = $intentContentAtr["arg"]; |
| 296 |
} elseif ( $intentContentAtr["arg"] != null && is_string( $intentContentAtr["arg"] ) ) { |
| 297 |
$xml->addAttribute( "arg", $intentContentAtr["arg"] ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
$hackyXML = str_replace( "<?xml version=\"1.0\"?>", "", $xml->asXML() ); |
| 302 |
return str_replace( "\n", "", $hackyXML ); |
| 303 |
} |
| 304 |
|
| 305 |
public static function forgeIntentToSpecificElement( |
| 306 |
string $renderedMML, array $intentContentAtr, string $elementTag |
| 307 |
): string { |
| 308 |
if ( !$intentContentAtr || !$renderedMML || !$elementTag ) { |
| 309 |
return $renderedMML; |
| 310 |
} |
| 311 |
return self::addAttributesToMML( $elementTag, $intentContentAtr, $elementTag, true ); |
| 312 |
} |
| 313 |
|
| 314 |
} |