Debian Codegraph @

extensions/Math/src/WikiTexVC/MMLmappings/Util/MMLParsingUtil.php

raw symbols SCIP
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 * This class contains functionalities for MML-node
15 * parsing which can be extracted and are used
16 * for multiple functions.
17 */
18 class MMLParsingUtil {
19 public static function renderApplyFunction() {
20 $mo = new MMLmo();
21 return $mo->encapsulateRaw( "&#x2061;" );
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 // Sstatements from here come from other fct ok ? otherwise create second fct
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 // Toggle by passed args in emph
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 * Parses an expression that defines a color; this is usually an argument in Literal.
73 * Example expression is: "\definecolor {ultramarine}{rgb}{0,0.12549019607843,0.37647058823529}"
74 * @param string $input tex-string, which contains the expression
75 * @return array|null either an array which contains hex of parsed expression or null if not parsable
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 * Creates a negation block in MathML, usually preceding the negated statement
103 * @return string negation block as MathML
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( "&#x29F8;" ) ) );
110 }
111
112 public static function mapToDoubleStruckUnicode( string $inputString ): string {
113 $map = [
114 '0' => '&#x1D7D8;', '1' => '&#x1D7D9;', '2' => '&#x1D7DA;', '3' => '&#x1D7DB;', '4' => '&#x1D7DC;',
115 '5' => '&#x1D7DD;', '6' => '&#x1D7DE;', '7' => '&#x1D7DF;', '8' => '&#x1D7E0;', '9' => '&#x1D7E1;',
116 'A' => '&#x1D538;', 'B' => '&#x1D539;', 'C' => '&#x2102;', 'D' => '&#x1D53B;', 'E' => '&#x1D53C;',
117 'F' => '&#x1D53D;', 'G' => '&#x1D53E;', 'H' => '&#x210D;', 'I' => '&#x1D540;', 'J' => '&#x1D541;',
118 'K' => '&#x1D542;', 'L' => '&#x1D543;', 'M' => '&#x1D544;', 'N' => '&#x2115;', 'O' => '&#x1D546;',
119 'P' => '&#x2119;', 'Q' => '&#x211A;', 'R' => '&#x211D;', 'S' => '&#x1D54A;', 'T' => '&#x1D54B;',
120 'U' => '&#x1D54C;', 'V' => '&#x1D54D;', 'W' => '&#x1D54E;', 'X' => '&#x1D54F;', 'Y' => '&#x1D550;',
121 'Z' => '&#x2124;', 'a' => '&#x1D552;', 'b' => '&#x1D553;', 'c' => '&#x1D554;', 'd' => '&#x1D555;',
122 'e' => '&#x1D556;', 'f' => '&#x1D557;', 'g' => '&#x1D558;', 'h' => '&#x1D559;', 'i' => '&#x1D55A;',
123 'j' => '&#x1D55B;', 'k' => '&#x1D55C;', 'l' => '&#x1D55D;', 'm' => '&#x1D55E;', 'n' => '&#x1D55F;',
124 'o' => '&#x1D560;', 'p' => '&#x1D561;', 'q' => '&#x1D562;', 'r' => '&#x1D563;', 's' => '&#x1D564;',
125 't' => '&#x1D565;', 'u' => '&#x1D566;', 'v' => '&#x1D567;', 'w' => '&#x1D568;', 'x' => '&#x1D569;',
126 'y' => '&#x1D56A;', 'z' => '&#x1D56B;'
127 ];
128
129 return self::matchAlphanumeric( $inputString, $map );
130 }
131
132 public static function mapToCaligraphicUnicode( string $inputString ): string {
133 $map = [
134 '0' => '&#x1D7CE;', '1' => '&#x1D7CF;', '2' => '&#x1D7D0;', '3' => '&#x1D7D1;', '4' => '&#x1D7D2;',
135 '5' => '&#x1D7D3;', '6' => '&#x1D7D4;', '7' => '&#x1D7D5;', '8' => '&#x1D7D6;', '9' => '&#x1D7D7;',
136 'A' => '&#x1D49C;', 'B' => '&#x212C;', 'C' => '&#x1D49E;', 'D' => '&#x1D49F;', 'E' => '&#x2130;',
137 'F' => '&#x2131;', 'G' => '&#x1D4A2;', 'H' => '&#x210B;', 'I' => '&#x2110;', 'J' => '&#x1D4A5;',
138 'K' => '&#x1D4A6;', 'L' => '&#x2112;', 'M' => '&#x2133;', 'N' => '&#x1D4A9;', 'O' => '&#x1D4AA;',
139 'P' => '&#x1D4AB;', 'Q' => '&#x1D4AC;', 'R' => '&#x211B;', 'S' => '&#x1D4AE;', 'T' => '&#x1D4AF;',
140 'U' => '&#x1D4B0;', 'V' => '&#x1D4B1;', 'W' => '&#x1D4B2;', 'X' => '&#x1D4B3;', 'Y' => '&#x1D4B4;',
141 'Z' => '&#x1D4B5;', 'a' => '&#x1D4B6;', 'b' => '&#x1D4B7;', 'c' => '&#x1D4B8;', 'd' => '&#x1D4B9;',
142 'e' => '&#x212F;', 'f' => '&#x1D4BB;', 'g' => '&#x210A;', 'h' => '&#x1D4BD;', 'i' => '&#x1D4BE;',
143 'j' => '&#x1D4BF;', 'k' => '&#x1D4C0;', 'l' => '&#x1D4C1;', 'm' => '&#x1D4C2;', 'n' => '&#x1D4C3;',
144 'o' => '&#x2134;', 'p' => '&#x1D4C5;', 'q' => '&#x1D4C6;', 'r' => '&#x1D4C7;', 's' => '&#x1D4C8;',
145 't' => '&#x1D4C9;', 'u' => '&#x1D4CA;', 'v' => '&#x1D4CB;', 'w' => '&#x1D4CC;', 'x' => '&#x1D4CD;',
146 'y' => '&#x1D4CE;', 'z' => '&#x1D4CF;'
147 ];
148
149 return self::matchAlphanumeric( $inputString, $map );
150 }
151
152 public static function mapToFrakturUnicode( string $inputString ): string {
153 $res = '';
154 $specialCases = [ 'C' => '&#x0212D;',
155 'H' => '&#x0210C;',
156 'I' => '&#x02111;',
157 'R' => '&#x0211C;',
158 'Z' => '&#x02124;' ];
159 foreach ( mb_str_split( $inputString ) as $chr ) {
160 // see https://www.w3.org/TR/mathml-core/#fraktur-mappings
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 // see https://www.w3.org/TR/mathml-core/#bold-mappings
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 // Replace each character in the input string with its caligraphic Unicode equivalent
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 // tbd eventually not only alphanumerical chars valid in intent params
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 * Converts a rendered MathML string to a XML tree and adds the attributes from input
248 * to the top-level element.Valid attributes for adding are "arg" and "intent.
249 * It overwrites pre-existing attributes in the top-level element.
250 * TBD: currently contains a hacky way to remove xml header in the output string
251 * example:" <msup intent="_($op,_of,$arg)">" intent attributes comes from input variables
252 * @param string $renderedMML defines input MathML string
253 * @param array $intentContentAtr defines attributes to add
254 * @return string MML with added attributes
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 * Add parameters from aattributes to the MML string
266 * @param string $renderedMML defines input MathML string
267 * @param array $intentContentAtr defines attributes to add
268 * @param string $elementTag element tag when using foundNodes
269 * @param bool $useFoundNodes use found nodes
270 * @return string MML with added attributes
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 }