| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::MathML | # | ||
| 3 | # | MathML generator for LaTeXML | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | |||
| 13 | package LaTeXML::Post::MathML; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use LaTeXML::Common::XML; | ||
| 17 | use LaTeXML::Util::Unicode; | ||
| 18 | use LaTeXML::Post; | ||
| 19 | use LaTeXML::Common::Font; | ||
| 20 | use List::Util qw(max); | ||
| 21 | use base qw(LaTeXML::Post::MathProcessor); | ||
| 22 | use base qw(Exporter); | ||
| 23 | our @EXPORT = ( | ||
| 24 | qw( &DefMathML ), | ||
| 25 | qw( &pmml &pmml_scriptsize &pmml_smaller | ||
| 26 | &pmml_mi &pmml_mo &pmml_mn &pmml_bigop | ||
| 27 | &pmml_punctuate &pmml_parenthesize | ||
| 28 | &pmml_infix &pmml_script &pmml_summation), | ||
| 29 | qw( &cmml &cmml_share &cmml_shared &cmml_leaf | ||
| 30 | &cmml_or_compose &cmml_synth_not &cmml_synth_complement), | ||
| 31 | ); | ||
| 32 | use LaTeXML::Post::MathML::OperatorDictionary; | ||
| 33 | require LaTeXML::Post::MathML::Presentation; | ||
| 34 | require LaTeXML::Post::MathML::Content; | ||
| 35 | |||
| 36 | my $mmlURI = "http://www.w3.org/1998/Math/MathML"; # CONSTANT | ||
| 37 | |||
| 38 | # ================================================================================ | ||
| 39 | # LaTeXML::MathML Base-level Math Formatter for LaTeXML's Parsed Math. | ||
| 40 | # Cooperate with the parsed math structure generated by LaTeXML::Math and | ||
| 41 | # convert into presentation & content MathML. | ||
| 42 | # (See LaTeXML::Post::MathML::Presentation, LaTeXML::Post::MathML::Content) | ||
| 43 | # ================================================================================ | ||
| 44 | # Some clarity to work out: | ||
| 45 | # We're trying to convert either parsed or unparsed math (sometimes intertwined). | ||
| 46 | # How clearly do these have to be separated? | ||
| 47 | # at least, sub/superscripts do not attach to anything meaningful. | ||
| 48 | # ================================================================================ | ||
| 49 | |||
| 50 | #================================================================================ | ||
| 51 | # Useful switches when creating a converter with special needs. | ||
| 52 | # plane1 : use Unicode plane 1 characters for math letters | ||
| 53 | # hackplane1 : use a hybrid of plane1 for script and fraktur, | ||
| 54 | # otherwise regular chars with mathvariant | ||
| 55 | # nestmath : allow m:math to be nested within m:mtext | ||
| 56 | # otherwise flatten to m:mrow sequence of m:mtext and other math bits. | ||
| 57 | # usemfenced : whether to use mfenced instead of mrow | ||
| 58 | # this would be desired for MathML-CSS profile, | ||
| 59 | # but (I think) mrow usually gets better handling in firefox,..? | ||
| 60 | #================================================================================ | ||
| 61 | |||
| 62 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 63 | # Top level | ||
| 64 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 65 | sub preprocess { | ||
| 66 | my ($self, $doc, @nodes) = @_; | ||
| 67 | # Set up rational, modern, defaults. | ||
| 68 | $$self{hackplane1} = 0 unless $$self{hackplane1}; | ||
| 69 | $$self{plane1} = 1 if $$self{hackplane1} || !defined $$self{plane1}; | ||
| 70 | $$self{nestmath} = 0 unless $$self{nestmath}; | ||
| 71 | $doc->adjust_latexml_doctype('MathML'); # Add MathML if LaTeXML dtd. | ||
| 72 | $doc->addNamespace($mmlURI, 'm'); | ||
| 73 | return; } | ||
| 74 | |||
| 75 | # Works for pmml, cmml | ||
| 76 | sub outerWrapper { | ||
| 77 | my ($self, $doc, $xmath, $mml) = @_; | ||
| 78 | my $math = $xmath->parentNode; | ||
| 79 | my $mode = $math->getAttribute('mode') || 'inline'; | ||
| 80 | my @img = (); | ||
| 81 | if (my $src = $math->getAttribute('imagesrc')) { | ||
| 82 | my $depth = $math->getAttribute('imagedepth'); | ||
| 83 | @img = (altimg => $src, | ||
| 84 | 'altimg-width' => $math->getAttribute('imagewidth') . 'px', | ||
| 85 | 'altimg-height' => $math->getAttribute('imageheight') . 'px', | ||
| 86 | 'altimg-valign' => ($depth ? -$depth . 'px' : undef)); } # Note the sign! | ||
| 87 | my @rdfa = map { my $val = ($math->getAttribute($_) || $xmath->getAttribute($_)); $val ? ($_ => $val) : () } | ||
| 88 | qw(about resource property rel rev typeof datatype content); | ||
| 89 | my $wrapped = ['m:math', { display => ($mode eq 'display' ? 'block' : 'inline'), | ||
| 90 | class => $math->getAttribute('class'), | ||
| 91 | alttext => $math->getAttribute('tex'), | ||
| 92 | #### Handy for debugging math | ||
| 93 | ### title => $math->getAttribute('text'), | ||
| 94 | @rdfa, | ||
| 95 | @img }, | ||
| 96 | $mml]; | ||
| 97 | # Associate the generated node with the source XMath node, but don't cross-reference | ||
| 98 | $self->associateNode($wrapped, $xmath, 1); | ||
| 99 | return $wrapped; } | ||
| 100 | |||
| 101 | # Map mimetype to Official MathML encodings | ||
| 102 | our %ENCODINGS = ( | ||
| 103 | 'application/mathml-presentation+xml' => 'MathML-Presentation', | ||
| 104 | 'application/mathml-content+xml' => 'MathML-Content', | ||
| 105 | 'image/svg+xml' => 'SVG1.1', | ||
| 106 | ); | ||
| 107 | |||
| 108 | sub rawIDSuffix { | ||
| 109 | return '.msvg'; } | ||
| 110 | |||
| 111 | # This works for either pmml or cmml. | ||
| 112 | sub combineParallel { | ||
| 113 | my ($self, $doc, $xmath, $primary, @secondaries) = @_; | ||
| 114 | my $id = $xmath->getAttribute('fragid'); | ||
| 115 | my @alt = (); | ||
| 116 | foreach my $secondary (@secondaries) { | ||
| 117 | my $mimetype = $$secondary{mimetype} || 'unknown'; | ||
| 118 | my $encoding = $ENCODINGS{$mimetype} || $mimetype; | ||
| 119 | if ($mimetype =~ /^application\/mathml/) { # Some flavor of MathML? simple case | ||
| 120 | push(@alt, ['m:annotation-xml', { encoding => $encoding }, | ||
| 121 | $$secondary{xml}]); } | ||
| 122 | elsif (my $xml = $$secondary{xml}) { # Other XML? may need wrapping. | ||
| 123 | push(@alt, ['m:annotation-xml', { encoding => $encoding }, | ||
| 124 | $$secondary{processor}->outerWrapper($doc, $xmath, $xml)]); } | ||
| 125 | elsif (my $src = $$secondary{src}) { # something referred to by a file? Image, maybe? | ||
| 126 | push(@alt, ['m:annotation', { encoding => $encoding, src => $src }]); } | ||
| 127 | elsif (my $string = $$secondary{string}) { # simple string data? | ||
| 128 | push(@alt, ['m:annotation', { encoding => $encoding }, $string]); } | ||
| 129 | # anything else ignore? | ||
| 130 | } | ||
| 131 | return { processor => $self, mimetype => $$primary{mimetype}, | ||
| 132 | xml => (@alt ? ['m:semantics', {}, $$primary{xml}, @alt] : $$primary{xml}) }; } | ||
| 133 | |||
| 134 | # $self->convertNode($doc,$node); | ||
| 135 | # will be handled by specific Presentation or Content MathML converters; See at END. | ||
| 136 | |||
| 137 | # $self->translateNode($doc,$XMath,$style,$embedding) | ||
| 138 | # returns the translation of the XMath node (but doesn't insert it) | ||
| 139 | # $style will be either 'display' or 'text' (if relevant), | ||
| 140 | # The result should be wrapped as necessary for the result to | ||
| 141 | # be embedded within the tag $embedding. | ||
| 142 | # Eg. for parallel markup. | ||
| 143 | |||
| 144 | # See END for presentation, content and parallel versions. | ||
| 145 | |||
| 146 | sub getQName { | ||
| 147 | my ($node) = @_; | ||
| 148 | return $LaTeXML::Post::DOCUMENT->getQName($node); } | ||
| 149 | |||
| 150 | # Add a cross-reference linkage (eg. xref) onto $node to refer to the given $id. | ||
| 151 | # (presumably $id is the id of a node created by another Math Postprocessor | ||
| 152 | # from the same source XMath node that generated $node) | ||
| 153 | sub addCrossref { | ||
| 154 | my ($self, $node, $id) = @_; | ||
| 155 | $node->setAttribute(xref => $id); | ||
| 156 | return; } | ||
| 157 | |||
| 158 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 159 | # General translation utilities. | ||
| 160 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 161 | |||
| 162 | sub realize { | ||
| 163 | my ($node, $branch) = @_; | ||
| 164 | return (ref $node) ? $LaTeXML::Post::DOCUMENT->realizeXMNode($node, $branch) : $node; } | ||
| 165 | |||
| 166 | # For a node that is a (possibly embellished) operator, | ||
| 167 | # find the underlying role. | ||
| 168 | my %EMBELLISHING_ROLE = ( # CONSTANT | ||
| 169 | SUPERSCRIPTOP => 1, SUBSCRIPTOP => 1, | ||
| 170 | OVERACCENT => 1, UNDERACCENT => 1, MODIFIER => 1, MODIFIEROP => 1); | ||
| 171 | |||
| 172 | sub getOperatorRole { | ||
| 173 | my ($node) = @_; | ||
| 174 | if (!$node) { | ||
| 175 | return; } | ||
| 176 | elsif (my $role = $node->getAttribute('role')) { | ||
| 177 | return $role; } | ||
| 178 | elsif (getQName($node) eq 'ltx:XMApp') { | ||
| 179 | my ($op, $base) = element_nodes($node); | ||
| 180 | return ($EMBELLISHING_ROLE{ $op->getAttribute('role') || '' } | ||
| 181 | ? getOperatorRole($base) | ||
| 182 | : undef); } } | ||
| 183 | |||
| 184 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 185 | # Table of Translators for presentation|content | ||
| 186 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 187 | # All translators take XMath XML::LibXML nodes as arguments, | ||
| 188 | # and return an intermediate form (ie. array form) of MathML to be added. | ||
| 189 | |||
| 190 | # DANGER!!! These accumulate all the DefMathML declarations. | ||
| 191 | # They're fixed after the module has been loaded, so are Daemon Safe, | ||
| 192 | # but probably should be going into (post) STATE, so that they are extensible. | ||
| 193 | # IN FACT, I'm already taking baby-steps to export DefMathML (and needed helpers), | ||
| 194 | # in order to assist these extensions, so that will bring up daemon issues pretty quick. | ||
| 195 | our $MMLTable_P = {}; | ||
| 196 | our $MMLTable_C = {}; | ||
| 197 | |||
| 198 | sub DefMathML { | ||
| 199 | my ($key, $presentation, $content) = @_; | ||
| 200 | $$MMLTable_P{$key} = $presentation if $presentation; | ||
| 201 | $$MMLTable_C{$key} = $content if $content; | ||
| 202 | return; } | ||
| 203 | |||
| 204 | sub lookupPresenter { | ||
| 205 | my ($mode, $role, $name) = @_; | ||
| 206 | $name = '?' unless $name; | ||
| 207 | $role = '?' unless $role; | ||
| 208 | return $$MMLTable_P{"$mode:$role:$name"} || $$MMLTable_P{"$mode:?:$name"} | ||
| 209 | || $$MMLTable_P{"$mode:$role:?"} || $$MMLTable_P{"$mode:?:?"}; } | ||
| 210 | |||
| 211 | sub lookupContent { | ||
| 212 | my ($mode, $role, $name) = @_; | ||
| 213 | # Content-first lookup. Idea: | ||
| 214 | # If we have a meaning/name provided, we can make a csymbol. | ||
| 215 | # 1. Sometimes we can make a role-specific adaptation to the symbol, so check that first | ||
| 216 | return $name ? (($role && $$MMLTable_C{"$mode:$role:$name"}) || | ||
| 217 | # 2. Sometimes we want to make a name-specific adaptation, check that second | ||
| 218 | $$MMLTable_C{"$mode:?:$name"} || | ||
| 219 | # 3. If no special code, but we have a name, use a generic handler for this element | ||
| 220 | $$MMLTable_C{"$mode:?:?"}) : ( | ||
| 221 | # 4. If we do not have a name, check for a role-based handler | ||
| 222 | ($role && $$MMLTable_C{"$mode:$role:?"}) || | ||
| 223 | # 5. Always use a default handler if nothing is known | ||
| 224 | $$MMLTable_C{"$mode:?:?"}); } | ||
| 225 | |||
| 226 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 227 | # Various needed maps | ||
| 228 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 229 | my %stylestep = ( # CONSTANT | ||
| 230 | display => 'text', text => 'script', | ||
| 231 | script => 'scriptscript', scriptscript => 'scriptscript'); | ||
| 232 | my %stylesize = ( # CONSTANT | ||
| 233 | display => '100%', text => '100%', | ||
| 234 | script => '70%', scriptscript => '50%'); | ||
| 235 | my %style_script_step = ( # CONSTANT | ||
| 236 | display => 'script', text => 'script', | ||
| 237 | script => 'scriptscript', scriptscript => 'scriptscript'); | ||
| 238 | # Attributes for m:mstyle when changing between two mathstyles | ||
| 239 | my %stylemap = ( # CONSTANT | ||
| 240 | display => { text => { displaystyle => 'false' }, | ||
| 241 | script => { displaystyle => 'false', scriptlevel => '+1' }, | ||
| 242 | scriptscript => { displaystyle => 'false', scriptlevel => '+2' } }, | ||
| 243 | text => { display => { displaystyle => 'true' }, | ||
| 244 | script => { scriptlevel => '+1' }, | ||
| 245 | scriptscript => { scriptlevel => '+2' } }, | ||
| 246 | script => { display => { displaystyle => 'true', scriptlevel => '-1' }, | ||
| 247 | text => { scriptlevel => '-1' }, | ||
| 248 | scriptscript => { scriptlevel => '+1' } }, | ||
| 249 | scriptscript => { display => { displaystyle => 'true', scriptlevel => '-2' }, | ||
| 250 | text => { scriptlevel => '-2' }, | ||
| 251 | script => { scriptlevel => '-1' } }); | ||
| 252 | # Similar to above, but for use when there are no MathML structures used | ||
| 253 | # that NEED displaystyle to be set; presumably only to set a fontsize context | ||
| 254 | my %stylemap2 = ( # CONSTANT | ||
| 255 | display => { text => {}, | ||
| 256 | script => { scriptlevel => '+1' }, | ||
| 257 | scriptscript => { scriptlevel => '+2' } }, | ||
| 258 | text => { display => {}, | ||
| 259 | script => { scriptlevel => '+1' }, | ||
| 260 | scriptscript => { scriptlevel => '+2' } }, | ||
| 261 | script => { display => { displaystyle => 'true', scriptlevel => '-1' }, | ||
| 262 | text => { scriptlevel => '-1' }, | ||
| 263 | scriptscript => { scriptlevel => '+1' } }, | ||
| 264 | scriptscript => { display => { displaystyle => 'true', scriptlevel => '-2' }, | ||
| 265 | text => { scriptlevel => '-2' }, | ||
| 266 | script => { scriptlevel => '-1' } }); | ||
| 267 | |||
| 268 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 269 | # Support functions for Presentation MathML | ||
| 270 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 271 | |||
| 272 | sub pmml_top { | ||
| 273 | my ($self, $node, $style) = @_; | ||
| 274 | # These bindings reflect the style, font, size & color that we are displaying in. | ||
| 275 | # Ie. if you want to draw in that size & color, you'll get it automatically. | ||
| 276 | local $LaTeXML::MathML::STYLE = $style || 'text'; | ||
| 277 | local $LaTeXML::MathML::FONT = find_inherited_attribute($node, 'font'); | ||
| 278 | # $LaTeXML::MathML::FONT = undef | ||
| 279 | # if $LaTeXML::MathML::FONT && !$mathvariants{$LaTeXML::MathML::FONT}; # verify sane font | ||
| 280 | local $LaTeXML::MathML::SIZE = find_inherited_attribute($node, 'fontsize') || '100%'; | ||
| 281 | local $LaTeXML::MathML::COLOR = find_inherited_attribute($node, 'color'); | ||
| 282 | local $LaTeXML::MathML::BGCOLOR = find_inherited_attribute($node, 'backgroundcolor'); | ||
| 283 | local $LaTeXML::MathML::OPACITY = find_inherited_attribute($node, 'opacity'); | ||
| 284 | local $LaTeXML::MathML::DESIRED_SIZE = $LaTeXML::MathML::SIZE; | ||
| 285 | my @result = map { pmml($_) } element_nodes($node); | ||
| 286 | my $result = scalar(@result) > 1 ? ['m:mrow', {}, @result] : $result[0]; | ||
| 287 | $self->adjust_spacing($result); # Resolve spacing TeX vs MathML | ||
| 288 | return $result; } | ||
| 289 | |||
| 290 | sub find_inherited_attribute { | ||
| 291 | my ($node, $attribute) = @_; | ||
| 292 | # Check for inherited style attributes, but stop at non-LaTeXML nodes | ||
| 293 | # [or at least be aware that the attribute may have totally different format or even meaning!] | ||
| 294 | while ($node && isElementNode($node) && ($node->namespaceURI eq 'http://dlmf.nist.gov/LaTeXML')) { | ||
| 295 | if (my $value = $node->getAttribute($attribute)) { | ||
| 296 | return $value; } | ||
| 297 | $node = $node->parentNode; } | ||
| 298 | return; } | ||
| 299 | |||
| 300 | # Convert a node that will automatically be made smaller, due to its context, | ||
| 301 | # such as in the numerator or denominator of a fraction. | ||
| 302 | sub pmml_smaller { | ||
| 303 | my ($node) = @_; | ||
| 304 | local $LaTeXML::MathML::STYLE = $stylestep{$LaTeXML::MathML::STYLE}; | ||
| 305 | local $LaTeXML::MathML::SIZE = $stylesize{$LaTeXML::MathML::STYLE}; | ||
| 306 | return pmml($node); } | ||
| 307 | |||
| 308 | # Convert a node that will automatically be made scriptsize, | ||
| 309 | # such as sub- or superscripts. | ||
| 310 | sub pmml_scriptsize { | ||
| 311 | no warnings 'recursion'; | ||
| 312 | my ($script) = @_; | ||
| 313 | local $LaTeXML::MathML::STYLE = $style_script_step{$LaTeXML::MathML::STYLE}; | ||
| 314 | local $LaTeXML::MathML::SIZE = $stylesize{$LaTeXML::MathML::STYLE}; | ||
| 315 | return ($script ? pmml($script) : ['m:mrow']); } | ||
| 316 | |||
| 317 | sub pmml { | ||
| 318 | no warnings 'recursion'; | ||
| 319 | my ($node) = @_; | ||
| 320 | return unless $node; | ||
| 321 | # [since we follow split/scan, use the fragid, not xml:id! TO SOLVE LATER] | ||
| 322 | # Do the core conversion. | ||
| 323 | # Fetch the "real" node, if this is an XMRef to one; also use the OTHER's id! | ||
| 324 | my $refr; | ||
| 325 | if (getQName($node) eq 'ltx:XMRef') { | ||
| 326 | $refr = $node; | ||
| 327 | $node = realize($node); } | ||
| 328 | # Bind any other style information from the refering node or the current node | ||
| 329 | # so that any tokens synthesized from strings recover that style. | ||
| 330 | local $LaTeXML::MathML::DESIRED_SIZE = _getattr($refr, $node, 'fontsize') || $LaTeXML::MathML::DESIRED_SIZE; | ||
| 331 | local $LaTeXML::MathML::COLOR = _getattr($refr, $node, 'color') || $LaTeXML::MathML::COLOR; | ||
| 332 | local $LaTeXML::MathML::BGCOLOR = _getattr($refr, $node, 'backgroundcolor') | ||
| 333 | || $LaTeXML::MathML::BGCOLOR; | ||
| 334 | local $LaTeXML::MathML::OPACITY = _getattr($refr, $node, 'opacity') || $LaTeXML::MathML::OPACITY; | ||
| 335 | my $result = pmml_internal($node); | ||
| 336 | # Let customization annotate the result. | ||
| 337 | # Now possibly wrap the result in a row, enclose, etc, if needed | ||
| 338 | my $e = _getattr($refr, $node, 'enclose'); | ||
| 339 | my $cl = join(' ', grep { $_ } $refr && $refr->getAttribute('class'), $node->getAttribute('class')); | ||
| 340 | # Wrap in an enclose, if there's an enclose attribute (Ugh!) | ||
| 341 | $result = ['m:menclose', { notation => $e }, $result] if $e; | ||
| 342 | # Add spacing last; outside parens & enclosing (?) But defer until spacing resolution | ||
| 343 | if (ref $result eq 'ARRAY') { | ||
| 344 | my $l = _getspace($refr, $node, 'lpadding'); | ||
| 345 | my $r = _getspace($refr, $node, 'rpadding'); | ||
| 346 | $$result[1]{_lpadding} = $l if $l; | ||
| 347 | $$result[1]{_rpadding} = $r if $r; } | ||
| 348 | |||
| 349 | if ($cl && ((ref $result) eq 'ARRAY')) { # Add classs, if any and different | ||
| 350 | my $ocl = $$result[1]{class}; | ||
| 351 | $$result[1]{class} = (!$ocl || ($ocl eq $cl) ? $cl : "$ocl $cl"); } | ||
| 352 | # Associate the generated node with the source XMath node. | ||
| 353 | if (my $role = _getattr($refr, $node, 'role')) { | ||
| 354 | $$result[1]{_role} = $role; } | ||
| 355 | $LaTeXML::Post::MATHPROCESSOR->associateNode($result, $node); | ||
| 356 | return $result; } | ||
| 357 | |||
| 358 | sub first_element { | ||
| 359 | my ($node) = @_; | ||
| 360 | my $c = $node->firstChild; | ||
| 361 | while ($c) { | ||
| 362 | return $c if $c->nodeType == XML_ELEMENT_NODE; | ||
| 363 | $c = $c->nextSibling; } | ||
| 364 | return; } | ||
| 365 | |||
| 366 | sub _getattr { | ||
| 367 | my ($refr, $node, $attribute) = @_; | ||
| 368 | return ($refr && $refr->getAttribute($attribute)) || $node && $node->getAttribute($attribute); } | ||
| 369 | |||
| 370 | sub _getspace { | ||
| 371 | my ($refr, $node, $attribute) = @_; | ||
| 372 | my $refspace = $refr && $refr->getAttribute($attribute); | ||
| 373 | my $nodespace = $node && $node->getAttribute($attribute); | ||
| 374 | return ($refspace ? getXMHintSpacing($refspace) : 0) | ||
| 375 | + ($nodespace ? getXMHintSpacing($nodespace) : 0); } | ||
| 376 | |||
| 377 | # Needs to be a utility somewhere... | ||
| 378 | # Return in em !! | ||
| 379 | sub getXMHintSpacing { | ||
| 380 | my ($width) = @_; | ||
| 381 | if ($width && ($width =~ /^([\d\.\+\-]+)(pt|mu|em)(\s+plus\s+[\d\.]+pt)?(\s+minus\s+[\d\.]+pt)?$/)) { | ||
| 382 | return ($2 eq 'em' ? $1 : ($2 eq 'mu' ? $1 / 18.0 : $1 / 10.0)); } # Assuming 10pt font!!!! | ||
| 383 | else { | ||
| 384 | return 0; } } | ||
| 385 | |||
| 386 | my $NBSP = pack('U', 0xA0); # CONSTANT | ||
| 387 | |||
| 388 | sub pmml_internal { | ||
| 389 | no warnings 'recursion'; | ||
| 390 | my ($node) = @_; | ||
| 391 | return ['m:merror', {}, ['m:mtext', {}, "Missing Subexpression"]] unless $node; | ||
| 392 | my $self = $LaTeXML::Post::MATHPROCESSOR; | ||
| 393 | my $doc = $LaTeXML::Post::DOCUMENT; | ||
| 394 | my $tag = getQName($node); | ||
| 395 | my $role = $node->getAttribute('role'); | ||
| 396 | if ($tag eq 'ltx:XMath') { | ||
| 397 | return pmml_row(map { pmml($_) } element_nodes($node)); } # Really multiple nodes??? | ||
| 398 | elsif ($tag eq 'ltx:XMDual') { | ||
| 399 | my ($content, $presentation) = element_nodes($node); | ||
| 400 | return pmml($presentation); } | ||
| 401 | elsif (($tag eq 'ltx:XMWrap') || ($tag eq 'ltx:XMArg')) { # Only present if parsing failed! | ||
| 402 | return pmml_maybe_resize($node, pmml_row(map { pmml($_) } element_nodes($node))); } | ||
| 403 | elsif ($tag eq 'ltx:XMApp') { | ||
| 404 | my ($op, @args) = element_nodes($node); | ||
| 405 | if (!$op) { | ||
| 406 | return ['m:merror', {}, ['m:mtext', {}, "Missing Operator"]]; } | ||
| 407 | elsif ($role && ($role =~ /^(FLOAT|POST)(SUB|SUPER)SCRIPT$/)) { | ||
| 408 | # (FLOAT|POST)(SUB|SUPER)SCRIPT's should NOT remain in successfully parsed math. | ||
| 409 | # This conversion creates something "presentable", though doubtfully correct (empty mi?) | ||
| 410 | # Really should mark & make a fake parsing pass to & group open/close pairs & attach scripts | ||
| 411 | return [($2 eq 'SUB' ? 'm:msub' : 'm:msup'), {}, ['m:mi'], | ||
| 412 | pmml_scriptsize($op)]; } | ||
| 413 | else { | ||
| 414 | my $rop = realize($op); | ||
| 415 | my $style = | ||
| 416 | $rop->getAttribute('mathstyle') || $op->getAttribute('mathstyle'); | ||
| 417 | my $ostyle = $LaTeXML::MathML::STYLE; | ||
| 418 | local $LaTeXML::MathML::STYLE | ||
| 419 | = ($style && $stylestep{$style} ? $style : $LaTeXML::MathML::STYLE); | ||
| 420 | my $result = &{ lookupPresenter('Apply', getOperatorRole($rop), $rop->getAttribute('meaning')) | ||
| 421 | }($op, @args); | ||
| 422 | $result = pmml_maybe_resize($node, $result); | ||
| 423 | my $needsmathstyle = needsMathstyle($result); | ||
| 424 | my %styleattr = %{ ($style && ($needsmathstyle | ||
| 425 | ? $stylemap{$ostyle}{$style} | ||
| 426 | : $stylemap2{$ostyle}{$style})) || {} }; | ||
| 427 | $result = ['m:mstyle', {%styleattr}, $result] if keys %styleattr; | ||
| 428 | return $result; } } | ||
| 429 | elsif ($tag eq 'ltx:XMTok') { | ||
| 430 | return &{ lookupPresenter('Token', $role, $node->getAttribute('meaning')) }($node); } | ||
| 431 | elsif ($tag eq 'ltx:XMHint') { | ||
| 432 | return &{ lookupPresenter('Hint', $role, $node->getAttribute('meaning')) }($node); } | ||
| 433 | elsif ($tag eq 'ltx:XMArray') { | ||
| 434 | my $width = $node->getAttribute('width'); | ||
| 435 | my $style = $node->getAttribute('mathstyle'); | ||
| 436 | my $vattach = $node->getAttribute('vattach'); | ||
| 437 | my $rowsep = $node->getAttribute('rowsep') || '0pt'; | ||
| 438 | my $colsep = $node->getAttribute('colsep') || '5pt'; | ||
| 439 | $vattach = 'axis' if !$vattach || ($vattach eq 'middle'); # roughly MathML's axis? | ||
| 440 | $vattach = 'bottom1' if $vattach && ($vattach eq 'top'); | ||
| 441 | my $ostyle = $LaTeXML::MathML::STYLE; | ||
| 442 | local $LaTeXML::MathML::STYLE | ||
| 443 | = ($style && $stylestep{$style} ? $style : $LaTeXML::MathML::STYLE); | ||
| 444 | my @rows = (); | ||
| 445 | my $nrows = 0; | ||
| 446 | my $ncols = 0; | ||
| 447 | |||
| 448 | my @spanned = (); # record columns to be skipped | ||
| 449 | foreach my $row (element_nodes($node)) { | ||
| 450 | my @cols = (); | ||
| 451 | my $nc = 0; | ||
| 452 | $nrows++; | ||
| 453 | foreach my $col (element_nodes($row)) { | ||
| 454 | $nc++; | ||
| 455 | $spanned[$nc - 1]-- if $spanned[$nc - 1]; | ||
| 456 | next if $spanned[$nc - 1]; # Omit this mtd, if spanned by another! | ||
| 457 | my $a = $col->getAttribute('align'); | ||
| 458 | my $b = $col->getAttribute('border'); | ||
| 459 | my $bc = ($b ? join(' ', map { 'ltx_border_' . $_ } split(/\s/, $b)) : $b); | ||
| 460 | my $th = $col->getAttribute('thead'); | ||
| 461 | my $hc = ($th ? join(' ', map { 'ltx_th_' . $_ } split(/\s/, $th)) : ''); | ||
| 462 | my $cl = $col->getAttribute('class'); | ||
| 463 | my $c = ($bc ? ($hc ? "$bc $hc" : $bc) : $hc); | ||
| 464 | my $cs = $col->getAttribute('colspan'); | ||
| 465 | my $rs = $col->getAttribute('rowspan'); | ||
| 466 | my @cell = filter_row(map { pmml($_) } element_nodes($col)); | ||
| 467 | |||
| 468 | if ($rs || $cs) { # Note following cells to be omitted from MathML | ||
| 469 | for (my $i = 0 ; $i < ($cs || 1) ; $i++) { | ||
| 470 | $spanned[$nc - 1 + $i] = ($rs || 1); } } | ||
| 471 | push(@cols, ['m:mtd', { ($a && ($a ne 'center') | ||
| 472 | ? (columnalign => $a, class => 'ltx_align_' . $a) : ()), | ||
| 473 | ($c || $cl ? (class => ($c && $cl ? "$c $cl" : $c || $cl)) : ()), | ||
| 474 | ($cs ? (columnspan => $cs) : ()), | ||
| 475 | ($rs ? (rowspan => $rs) : ()) }, | ||
| 476 | @cell]); } | ||
| 477 | $ncols = $nc if $nc > $ncols; | ||
| 478 | push(@rows, ['m:mtr', {}, @cols]); } | ||
| 479 | $rowsep = undef if $nrows < 2; | ||
| 480 | $colsep = undef if $ncols < 2; | ||
| 481 | my $result = ['m:mtable', { ($vattach ne 'axis' ? (align => $vattach) : ()), | ||
| 482 | ($rowsep ? (rowspacing => $rowsep) : ()), | ||
| 483 | ($colsep ? (columnspacing => $colsep) : ()), | ||
| 484 | ($width ? (width => $width) : ()), | ||
| 485 | # Mozilla seems to need some encouragement? | ||
| 486 | ($LaTeXML::MathML::STYLE eq 'display' ? (displaystyle => 'true') : ()) }, | ||
| 487 | @rows]; | ||
| 488 | my $needsmathstyle = needsMathstyle($result); | ||
| 489 | my %styleattr = %{ ($style && ($needsmathstyle | ||
| 490 | ? $stylemap{$ostyle}{$style} | ||
| 491 | : $stylemap2{$ostyle}{$style})) || {} }; | ||
| 492 | $result = ['m:mstyle', {%styleattr}, $result] if keys %styleattr; | ||
| 493 | $result = pmml_maybe_resize($node, $result); | ||
| 494 | return $result; } | ||
| 495 | elsif ($tag eq 'ltx:XMText') { | ||
| 496 | my @c = $node->childNodes; | ||
| 497 | my $result; | ||
| 498 | if (!$$self{nestmath}) { | ||
| 499 | $result = pmml_row(map { pmml_text_aux($_) } @c); } | ||
| 500 | else { | ||
| 501 | $result = ['m:mtext', {}, $self->convertXMTextContent($doc, 1, @c)]; } | ||
| 502 | return pmml_maybe_resize($node, $result); } | ||
| 503 | elsif ($tag eq 'ltx:ERROR') { | ||
| 504 | my $cl = $node->getAttribute('class'); | ||
| 505 | return ['m:merror', { class => join(' ', grep { $_ } 'ltx_ERROR', $cl) }, | ||
| 506 | ['m:mtext', {}, $node->textContent]]; } | ||
| 507 | else { | ||
| 508 | my $text = $node->textContent; # Spaces are significant here | ||
| 509 | $text =~ s/^\s+/$NBSP/; | ||
| 510 | $text =~ s/\s+$/$NBSP/; | ||
| 511 | return ['m:mtext', {}, $text]; } } | ||
| 512 | |||
| 513 | sub needsMathstyle { | ||
| 514 | no warnings 'recursion'; | ||
| 515 | my ($node) = @_; | ||
| 516 | if (ref $node eq 'ARRAY') { | ||
| 517 | my ($tag, $attr, @children) = @$node; | ||
| 518 | return 1 if $tag eq 'm:mfrac'; | ||
| 519 | return 1 if $$attr{_largeop}; | ||
| 520 | return 0 if ($tag eq 'm:mstyle') && defined $$attr{displaystyle}; | ||
| 521 | return 1 if grep { needsMathstyle($_) } @children; } | ||
| 522 | return; } | ||
| 523 | |||
| 524 | # Use mpadded instead of mrow if size has been given | ||
| 525 | # And maybe this is a convenient place to deal with frames? | ||
| 526 | sub pmml_maybe_resize { | ||
| 527 | my ($node, $result) = @_; | ||
| 528 | return $result unless ref $node; | ||
| 529 | my $parent; | ||
| 530 | # There MAY be relevant attributes on a containing XMDual (if any)!!! | ||
| 531 | if ((ref $node) && ($node->nodeType == XML_ELEMENT_NODE) | ||
| 532 | && ($parent = $node->parentNode) && (getQName($parent) eq 'ltx:XMDual')) { } | ||
| 533 | else { $parent = undef; } | ||
| 534 | my $width = $node->getAttribute('width') || ($parent && $parent->getAttribute('width')); | ||
| 535 | my $height = $node->getAttribute('height') || ($parent && $parent->getAttribute('height')); | ||
| 536 | my $depth = $node->getAttribute('depth') || ($parent && $parent->getAttribute('depth')); | ||
| 537 | my $xoff = $node->getAttribute('xoffset') || ($parent && $parent->getAttribute('xoffset')); | ||
| 538 | my $yoff = $node->getAttribute('yoffset') || ($parent && $parent->getAttribute('yoffset')); | ||
| 539 | if ($width || $height || $depth || $xoff || $yoff) { | ||
| 540 | if ($$result[0] eq 'm:mpadded') { } | ||
| 541 | elsif ($$result[0] eq 'm:mrow') { | ||
| 542 | $$result[0] = 'm:mpadded'; } | ||
| 543 | else { | ||
| 544 | $result = ['m:mpadded', {}, $result]; } | ||
| 545 | my $attr = $$result[1]; | ||
| 546 | # if ($yoff) { # assume this means to move the BOX? (not just the contents?) | ||
| 547 | # if (!$height) { | ||
| 548 | # if ($yoff =~ /^-/) { $height = $yoff; } | ||
| 549 | # else { $height = "+" . $yoff; } } | ||
| 550 | # if (!$depth) { | ||
| 551 | # if ($yoff =~ /^-/) { $depth = $yoff; $depth =~ s/^-/+/; } | ||
| 552 | # else { $depth = "-" . $yoff; } } } | ||
| 553 | $$attr{width} = $width if $width; | ||
| 554 | $$attr{height} = $height if $height; | ||
| 555 | $$attr{depth} = $depth if $depth; | ||
| 556 | $$attr{lspace} = $xoff if $xoff; | ||
| 557 | $$attr{voffset} = $yoff if $yoff; } | ||
| 558 | |||
| 559 | if (my $frame = $node->getAttribute('framed')) { | ||
| 560 | my $attr = $$result[1]; | ||
| 561 | my $c = $$attr{class}; | ||
| 562 | my $class = 'ltx_framed_' . $frame; | ||
| 563 | $$attr{class} = ($c ? $c . ' ' . $class : $class); | ||
| 564 | if (my $color = $node->getAttribute('framecolor')) { | ||
| 565 | my $s = $$attr{style}; | ||
| 566 | my $style = 'border-color: ' . $color; | ||
| 567 | $$attr{style} = ($s ? $s . '; ' . $style : $style); } } | ||
| 568 | return $result; } | ||
| 569 | |||
| 570 | sub filter_row { | ||
| 571 | my (@items) = @_; | ||
| 572 | return grep { !$$_[1]{_ignorable}; } grep { $_ } @items; } | ||
| 573 | |||
| 574 | sub pmml_row { | ||
| 575 | my (@items) = @_; | ||
| 576 | @items = filter_row(@items); | ||
| 577 | return (scalar(@items) == 1 ? $items[0] : ['m:mrow', {}, @items]); } | ||
| 578 | |||
| 579 | sub pmml_unrow { | ||
| 580 | my ($mml) = @_; | ||
| 581 | if ($mml && (ref $mml) && ($$mml[0] eq 'm:mrow') && !scalar(keys %{ $$mml[1] })) { | ||
| 582 | my ($tag, $attr, @children) = @$mml; | ||
| 583 | return @children; } | ||
| 584 | else { | ||
| 585 | return ($mml); } } | ||
| 586 | |||
| 587 | sub pmml_parenthesize { | ||
| 588 | my ($item, $open, $close) = @_; | ||
| 589 | if (!$open && !$close) { | ||
| 590 | return $item; } | ||
| 591 | # OR, maybe we should just use mfenced? | ||
| 592 | # mfenced is better for CSS profile. | ||
| 593 | # when the insides are line-broken, induces a less traditional appearance | ||
| 594 | # (however, line-breaking inside of a mrow w/parens needs some special treatment too! scripts!!) | ||
| 595 | elsif ($$LaTeXML::Post::MATHPROCESSOR{usemfenced}) { | ||
| 596 | return ['m:mfenced', { open => ($open || ''), close => ($close || '') }, $item]; } | ||
| 597 | else { | ||
| 598 | return ['m:mrow', {}, | ||
| 599 | ($open ? (pmml_mo($open, role => 'OPEN')) : ()), | ||
| 600 | $item, | ||
| 601 | ($close ? (pmml_mo($close, role => 'CLOSE')) : ())]; } } | ||
| 602 | |||
| 603 | # never used? | ||
| 604 | sub pmml_punctuate { | ||
| 605 | my ($separators, @items) = @_; | ||
| 606 | $separators = '' unless defined $separators; | ||
| 607 | my $lastsep = ', '; | ||
| 608 | my @arglist; | ||
| 609 | if (@items) { | ||
| 610 | push(@arglist, shift(@items)); | ||
| 611 | while (@items) { | ||
| 612 | if ($separators =~ s/^(.*?)( |$)//) { # delimited by SINGLE SPACE!! | ||
| 613 | $lastsep = $1 if $1; } | ||
| 614 | push(@arglist, pmml_mo($lastsep, role => 'PUNCT'), shift(@items)); } } | ||
| 615 | return pmml_row(@arglist); } | ||
| 616 | |||
| 617 | # args are XMath nodes | ||
| 618 | # This is suitable for use as an Apply handler. | ||
| 619 | sub pmml_infix { | ||
| 620 | no warnings 'recursion'; | ||
| 621 | my ($op, @args) = @_; | ||
| 622 | $op = realize($op) if ref $op; | ||
| 623 | return ['m:mrow', {}] unless $op && @args; # ?? | ||
| 624 | my @items = (); | ||
| 625 | if (scalar(@args) == 1) { # Infix with 1 arg is presumably Prefix! (aka Operator) | ||
| 626 | push(@items, | ||
| 627 | (ref $op && (getQName($op) ne 'ltx:XMTok') ? pmml($op) : pmml_mo($op, role => 'OPERATOR')), | ||
| 628 | pmml($args[0])); } | ||
| 629 | else { | ||
| 630 | ## push(@items, pmml(shift(@args))); | ||
| 631 | # Experiment at flattening? | ||
| 632 | my $role = (ref $op ? getOperatorRole($op) : 'none') || 'none'; | ||
| 633 | my $arg1 = realize(shift(@args)); | ||
| 634 | if (($role eq 'ADDOP') | ||
| 635 | && (getQName($arg1) eq 'ltx:XMApp') | ||
| 636 | && ((getOperatorRole((element_nodes($arg1))[0]) || 'none') eq $role)) { | ||
| 637 | push(@items, pmml_unrow(pmml($arg1))); } | ||
| 638 | else { | ||
| 639 | push(@items, pmml($arg1)); } | ||
| 640 | while (@args) { | ||
| 641 | push(@items, (ref $op ? pmml($op) : pmml_mo($op))); | ||
| 642 | push(@items, pmml(shift(@args))); } } | ||
| 643 | return pmml_row(@items); } | ||
| 644 | |||
| 645 | my %default_token_content = ( | ||
| 646 | MULOP => "\x{2062}", ADDOP => "\x{2064}", PUNCT => "\x{2063}"); | ||
| 647 | |||
| 648 | # Remaps some mathvariants to a simpler subset of Unicode | ||
| 649 | my %plane1hackable = ( # CONSTANT | ||
| 650 | script => 'script', | ||
| 651 | 'bold-script' => 'script', | ||
| 652 | fraktur => 'fraktur', | ||
| 653 | 'bold-fraktur' => 'fraktur', | ||
| 654 | 'double-struck' => 'double-struck'); | ||
| 655 | |||
| 656 | # Given an item (string or token element w/attributes) and latexml attributes, | ||
| 657 | # convert the string to the appropriate unicode (possibly plane1) | ||
| 658 | # & MathML presentation attributes (mathvariant, mathsize, mathcolor, stretchy, ....). | ||
| 659 | # Attempt to only add MathML attributes that are different from the defaults (eg. OperatorDictionary for m:mo) | ||
| 660 | # $tag specifies the element that these attributes will apply to (some attributes disallowed) | ||
| 661 | # The argument %attr overrides the corresponding attributes on $item (if it's an element) | ||
| 662 | sub stylizeContent { | ||
| 663 | my ($item, $tag, %attr) = @_; | ||
| 664 | # Get the basic attributes | ||
| 665 | my $iselement = (ref $item) eq 'XML::LibXML::Element'; # Item can be string or even Text node. | ||
| 666 | my $role = $attr{role} || ($iselement && $item->getAttribute('role')); | ||
| 667 | my $font = $attr{font} || ($iselement && $item->getAttribute('font')) | ||
| 668 | || $LaTeXML::MathML::FONT; | ||
| 669 | my $size = $attr{fontsize} || ($iselement && $item->getAttribute('fontsize')) | ||
| 670 | || $LaTeXML::MathML::DESIRED_SIZE; | ||
| 671 | my $color = $attr{color} || ($iselement && $item->getAttribute('color')) | ||
| 672 | || $LaTeXML::MathML::COLOR; | ||
| 673 | my $bgcolor = $attr{backgroundcolor} && ($iselement && $item->getAttribute('backgroundcolor')) | ||
| 674 | || $LaTeXML::MathML::BGCOLOR; | ||
| 675 | my $opacity = $attr{opacity} || ($iselement && $item->getAttribute('opacity')) | ||
| 676 | || $LaTeXML::MathML::OPACITY; | ||
| 677 | my $class = join(' ', grep { $_; } $attr{class}, ($iselement && $item->getAttribute('class'))); | ||
| 678 | my $cssstyle = join('; ', grep { $_; } $attr{ccsstyle}, ($iselement && $item->getAttribute('cssstyle'))); | ||
| 679 | my $variant = ($font ? unicode_mathvariant($font) : ''); | ||
| 680 | my $istoken = $tag =~ /^m:(?:mi|mo|mn)$/; # mrow? well, no.... | ||
| 681 | my $href = $istoken && ($attr{href} || ($iselement && $item->getAttribute('href'))); | ||
| 682 | my $title = $istoken && ($attr{title} || ($iselement && $item->getAttribute('title'))); | ||
| 683 | |||
| 684 | my $text = (ref $item ? $item->textContent : $item); | ||
| 685 | |||
| 686 | # Implied attributes, relevant for mo operators | ||
| 687 | my $stretchy = ((defined $attr{stretchy} ? $attr{stretchy} : ($iselement && $item->getAttribute('stretchy'))) | ||
| 688 | || 'false') eq 'true'; | ||
| 689 | my $isfence = $role && ($role =~ /^(OPEN|CLOSE|MIDDLE)$/); | ||
| 690 | my $issep = $role && ($role eq 'PUNCT'); | ||
| 691 | my $islargeop = $role && ($role =~ /^(SUMOP|INTOP)$/); | ||
| 692 | my $ismoveop = $role && ($role =~ /^(SUMOP|INTOP|BIGOP|LIMITOP)$/); # Not DIFFOP | ||
| 693 | my $issymm = $islargeop || ($text eq '/'); # WANTS to be symmetric | ||
| 694 | my $pos = ($iselement && $item->getAttribute('scriptpos')) || 'post'; | ||
| 695 | |||
| 696 | # First figure out the actual text content to use; Adjust font, variant, class for styling | ||
| 697 | if ((!defined $text) || ($text eq '')) { # Failsafe for empty tokens? | ||
| 698 | if (my $default = $role && $default_token_content{$role}) { | ||
| 699 | $text = $default; } | ||
| 700 | else { | ||
| 701 | $text = ($iselement ? ($item->getAttribute('name') || $item->getAttribute('meaning') | ||
| 702 | || $role || '') : '?'); | ||
| 703 | $color = 'red'; } } | ||
| 704 | elsif (($text eq '-') && $role && (($role eq 'ADDOP') || ($role eq 'OPERATOR'))) { # MathML Core prefers unicode minus | ||
| 705 | $text = "\x{2212}"; } | ||
| 706 | # Special case for single char identifiers? | ||
| 707 | if (($tag eq 'm:mi') && ($text =~ /^.$/)) { # Single char in mi? (what about m:ci?) | ||
| 708 | if ($variant eq 'italic') { $variant = undef; } # Defaults to italic | ||
| 709 | elsif (!$variant) { $variant = 'normal'; } } # must say so explicitly. | ||
| 710 | elsif ($font && !$variant) { | ||
| 711 | Warn('unexpected', $font, undef, "Unrecognized font variant '$font'"); $variant = ''; } | ||
| 712 | elsif ($variant eq 'normal') { # normal is default for any other tokens, so omit | ||
| 713 | $variant = undef; } | ||
| 714 | # Should we map to Unicode's Plane 1 blocks for Mathematical Alphanumeric Symbols? | ||
| 715 | # Only upper & lower case latin & greek, and also numerals can be mapped. | ||
| 716 | # For each mathvariant, and for each of those 5 groups, there is a linear mapping, | ||
| 717 | # EXCEPT for chars defined before Plain 1, which already exist in lower blocks. | ||
| 718 | # Get desired mapping strategy | ||
| 719 | my $plane1 = $$LaTeXML::Post::MATHPROCESSOR{plane1}; | ||
| 720 | my $plane1hack = $$LaTeXML::Post::MATHPROCESSOR{hackplane1}; | ||
| 721 | my $invisibletimes = $$LaTeXML::Post::MATHPROCESSOR{invisibletimes}; | ||
| 722 | # just in case, default to "true" (classic behavior), if not specified | ||
| 723 | $invisibletimes = 1 unless defined $invisibletimes; | ||
| 724 | my $u_variant = $variant | ||
| 725 | && ($plane1hack ? $plane1hackable{$variant} | ||
| 726 | : ($plane1 ? $variant : undef)); | ||
| 727 | my $u_text = ($tag ne 'm:mtext') && $u_variant && unicode_convert($text, $u_variant); | ||
| 728 | if ((defined $u_text) && ($u_text ne '')) { # didn't remap the text ? Keep text & variant | ||
| 729 | $text = $u_text || ''; | ||
| 730 | $variant = ($plane1hack && ($variant ne $u_variant) && ($variant =~ /^bold/) | ||
| 731 | ? 'bold' : undef); } # Possibly keep variant bold | ||
| 732 | # Use class (css) to patchup some weak translations | ||
| 733 | if ($text =~ /^\p{Format}*$/) { # Only Formatting (eg. InvisibleTImes) (or empty) | ||
| 734 | $font = $variant = $color = $bgcolor = $opacity = undef; } # Needs no viz. styling attributes | ||
| 735 | elsif (!$font) { } | ||
| 736 | elsif ($font =~ /caligraphic/) { | ||
| 737 | # Note that this is unlikely to have effect when plane1 chars are used! | ||
| 738 | $class = ($class ? $class . ' ' : '') . 'ltx_font_mathcaligraphic'; } | ||
| 739 | elsif ($font =~ /script/) { | ||
| 740 | $class = ($class ? $class . ' ' : '') . 'ltx_font_mathscript'; } | ||
| 741 | elsif (($font =~ /fraktur/) && ($text =~ /^[\+\-\d\.]*$/)) { # fraktur number? | ||
| 742 | $class = ($class ? $class . ' ' : '') . 'ltx_font_oldstyle'; } | ||
| 743 | elsif ($font =~ /smallcaps/) { | ||
| 744 | $class = ($class ? $class . ' ' : '') . 'ltx_font_smallcaps'; } | ||
| 745 | elsif ($variant && ($variant ne 'normal')) { # Any left-over mathvariant? Punt to CSS | ||
| 746 | $class = ($class ? $class . ' ' : '') . 'ltx_mathvariant_' . $variant; } | ||
| 747 | if ($tag eq 'm:mtext') { | ||
| 748 | $variant = undef; } | ||
| 749 | |||
| 750 | if ($opacity) { | ||
| 751 | $cssstyle = ($cssstyle ? $cssstyle . ';' : '') . "opacity:$opacity"; } | ||
| 752 | |||
| 753 | # Now, look up any OperatorDictionary properties | ||
| 754 | my %props = ($tag eq 'm:mo' ? opdict_lookup($text, $role) : ()); | ||
| 755 | |||
| 756 | # Resolve stretch & size | ||
| 757 | $stretchy = undef if ($tag ne 'm:mo'); # Only allowed on m:mo! | ||
| 758 | $size = undef if $stretchy; # Ignore size, if we're stretching. | ||
| 759 | my $stretchyhack = undef; | ||
| 760 | if ($text =~ /^[\x{2061}\x{2062}\x{2063}]*$/) { # invisible get no size or stretchiness | ||
| 761 | $stretchy = $size = undef; | ||
| 762 | # If requested, switch implied mo to space | ||
| 763 | if ($text eq "\x{2062}" and not($invisibletimes)) { | ||
| 764 | # and set lspace/rspace to 0 until we have browser interop. | ||
| 765 | $props{lspace} = $props{rspace} = $props{force_lspace} = $props{force_rspace} = '0em'; | ||
| 766 | $text = "\x{200B}"; } } | ||
| 767 | if ($size) { | ||
| 768 | if ($size eq ($LaTeXML::MathML::SIZE || 'text')) { # If default size, no need to mention. | ||
| 769 | $size = undef; } | ||
| 770 | # If requested relative size, and in script or scriptscript, we'll need to adjust the size | ||
| 771 | elsif (($size =~ /%$/) && ($LaTeXML::MathML::STYLE and $LaTeXML::MathML::STYLE =~ /script/)) { | ||
| 772 | my $req = $size; $req =~ s/%$//; | ||
| 773 | my $ex = $stylesize{$LaTeXML::MathML::STYLE}; $ex =~ s/%$//; | ||
| 774 | $size = int(100 * $req / $ex) . '%' if $ex; } | ||
| 775 | # Note that symmetric is only allowed when stretchy, which looks crappy for specific sizes | ||
| 776 | # so we'll pretend that delimiters are still stretchy, but restrict size by minsize & maxsize | ||
| 777 | # (Thanks Peter Krautzberger) | ||
| 778 | # Really we should check the Operator Dictionary to see if it's expected to be symmetric | ||
| 779 | if ($size) { # if non-default size | ||
| 780 | if ($issymm || $props{symmetric}) { # but should be symmetric? | ||
| 781 | $stretchyhack = 1; | ||
| 782 | $stretchy = 1; } # so, pretend we asked for stretchy | ||
| 783 | elsif ($tag eq 'm:mo') { | ||
| 784 | $stretchy = undef; } } } # Conversely, if size specifically set, don't stretch it! | ||
| 785 | |||
| 786 | return ($text, | ||
| 787 | ($variant ? (mathvariant => $variant) : ()), | ||
| 788 | ($size ? ($stretchyhack | ||
| 789 | ? (minsize => $size, maxsize => $size) | ||
| 790 | : (mathsize => $size)) | ||
| 791 | : ()), | ||
| 792 | ($color ? (mathcolor => $color) : ()), | ||
| 793 | ($bgcolor ? (mathbackground => $bgcolor) : ()), | ||
| 794 | ($cssstyle ? (style => $cssstyle) : ()), | ||
| 795 | ($class ? (class => $class) : ()), | ||
| 796 | ($href ? (href => $href) : ()), | ||
| 797 | ($title ? (title => $title) : ()), | ||
| 798 | # mo specific additions | ||
| 799 | (($stretchy xor $props{stretchy}) ? (stretchy => ($stretchy ? 'true' : 'false')) : ()), | ||
| 800 | (($isfence xor $props{fence}) ? (fence => ($isfence ? 'true' : 'false')) : ()), | ||
| 801 | (($issep xor $props{separator}) ? (separator => ($issep ? 'true' : 'false')) : ()), | ||
| 802 | (($islargeop xor $props{largeop}) ? (largeop => ($islargeop ? 'true' : 'false')) : ()), | ||
| 803 | ($islargeop ? (_largeop => 1) : ()), # For needsMathStyle | ||
| 804 | ($issymm && !$props{symmetric} && ($stretchy || $props{stretchy}) | ||
| 805 | ? (symmetric => 'true') : ()), | ||
| 806 | # If an operator has specifically located it's scripts, don't let mathml move them. | ||
| 807 | # A bit non-optimal, as Firefox is rather more generous than OpDict with movablelimits | ||
| 808 | ($ismoveop && (($pos =~ /mid/) || $LaTeXML::MathML::NOMOVABLELIMITS) ? (movablelimits => 'false') : ()), | ||
| 809 | # Store spacing for later spacing resolution | ||
| 810 | (defined $props{lspace} ? (_lspace => $props{lspace}) : ()), | ||
| 811 | (defined $props{rspace} ? (_rspace => $props{rspace}) : ()), | ||
| 812 | # explicitly set for zero-width space, until we get browser interop | ||
| 813 | (defined $props{force_lspace} ? (lspace => $props{force_lspace}) : ()), | ||
| 814 | (defined $props{force_rspace} ? (rspace => $props{force_rspace}) : ()), | ||
| 815 | ); } | ||
| 816 | |||
| 817 | # Generally, $item in the following ought to be a string. | ||
| 818 | sub pmml_mi { | ||
| 819 | my ($item, %attr) = @_; | ||
| 820 | my ($text, %mmlattr) = stylizeContent($item, 'm:mi', %attr); | ||
| 821 | return pmml_maybe_resize($item, ['m:mi', {%mmlattr}, $text]); } | ||
| 822 | |||
| 823 | # Really, the same issues as with mi. | ||
| 824 | sub pmml_mn { | ||
| 825 | my ($item, %attr) = @_; | ||
| 826 | my ($text, %mmlattr) = stylizeContent($item, 'm:mn', %attr); | ||
| 827 | return pmml_maybe_resize($item, ['m:mn', {%mmlattr}, $text]); } | ||
| 828 | |||
| 829 | # Note that $item should be either a string, or at most, an XMTok | ||
| 830 | sub pmml_mo { | ||
| 831 | my ($item, %attr) = @_; | ||
| 832 | my ($text, %mmlattr) = stylizeContent($item, 'm:mo', %attr); | ||
| 833 | return pmml_maybe_resize($item, ['m:mo', { %mmlattr, }, $text]); } | ||
| 834 | |||
| 835 | sub pmml_bigop { | ||
| 836 | my ($op) = @_; | ||
| 837 | my $style = $op->getAttribute('mathstyle'); | ||
| 838 | my %styleattr = %{ ($style && ($style ne $LaTeXML::MathML::STYLE) | ||
| 839 | && $stylemap{$LaTeXML::MathML::STYLE}{$style}) || {} }; | ||
| 840 | local $LaTeXML::MathML::STYLE | ||
| 841 | = ($style && $stylestep{$style} ? $style : $LaTeXML::MathML::STYLE); | ||
| 842 | my $mml = pmml_mo($op); | ||
| 843 | $mml = ['m:mstyle', {%styleattr}, $mml] if keys %styleattr; | ||
| 844 | return $mml; } | ||
| 845 | |||
| 846 | # Since we're keeping track of display style, under/over vs. sub/super | ||
| 847 | # We've got to keep track of MathML's desire to do it for us, | ||
| 848 | # and be prepared to override it. | ||
| 849 | # When we encounter a script, we've got to look into the possibly embellished | ||
| 850 | # operator for more scripts, and attempt to decipher (based on scriptpos attribute) | ||
| 851 | # the various positionings (pre, mid, post) and determine whether | ||
| 852 | # prescripts, multiscripts, munderover or msubsup should be used. | ||
| 853 | # | ||
| 854 | # Depending on which order the pre/post sub/super/over/under scripts appear, | ||
| 855 | # we may end up with a multiscript, or scripts applied to embellished operators. | ||
| 856 | # In particular, we may end up with postscripts applied to an object with over/under; | ||
| 857 | # OR, the other way around. | ||
| 858 | # In the latter case, we may have limits on a primed sum, in which case | ||
| 859 | # we will want to adjust the spacing so the limits center on the sum WITHOUT the primes!!!! | ||
| 860 | # | ||
| 861 | # Moreoever, the inner operator may be a largeop and need to be displaystyle; | ||
| 862 | # since mstyle doesn't nest well inside the scripts, we'll handle that too. | ||
| 863 | # We also make sure the eventual inner operator (if any) has movablelimits disabled. | ||
| 864 | sub pmml_script { | ||
| 865 | my ($op, $base, $script) = @_; | ||
| 866 | # disentangle base & pre/post-scripts | ||
| 867 | my ($innerbase, $prescripts, $midscripts, $postscripts, $emb_left, $emb_right) | ||
| 868 | = pmml_script_decipher($op, $base, $script); | ||
| 869 | # check if base needs displaystyle. | ||
| 870 | my $style = $innerbase->getAttribute('mathstyle'); | ||
| 871 | if ($style && ($style ne $LaTeXML::MathML::STYLE)) { | ||
| 872 | return ['m:mstyle', { displaystyle => ($style eq 'display' ? 'true' : 'false') }, | ||
| 873 | pmml_script_multi_layout( | ||
| 874 | pmml_script_mid_layout($innerbase, $midscripts, $emb_left, $emb_right), | ||
| 875 | $prescripts, $postscripts)]; } | ||
| 876 | else { | ||
| 877 | return pmml_script_multi_layout( | ||
| 878 | pmml_script_mid_layout($innerbase, $midscripts, $emb_left, $emb_right), | ||
| 879 | $prescripts, $postscripts); } } | ||
| 880 | |||
| 881 | sub pmml_script_mid_layout { | ||
| 882 | no warnings 'recursion'; | ||
| 883 | my ($base, $midscripts, $emb_left, $emb_right) = @_; | ||
| 884 | |||
| 885 | if (scalar(@$midscripts) == 0) { | ||
| 886 | ##### TRY this to block an extra mstyle | ||
| 887 | { local $LaTeXML::MathML::STYLE = $base->getAttribute('mathstyle') || $LaTeXML::MathML::STYLE; | ||
| 888 | $base = pmml($base); } | ||
| 889 | return $base; } | ||
| 890 | else { | ||
| 891 | { local $LaTeXML::MathML::NOMOVABLELIMITS = 1; | ||
| 892 | ##### TRY this to block an extra mstyle | ||
| 893 | local $LaTeXML::MathML::STYLE = $base->getAttribute('mathstyle') || $LaTeXML::MathML::STYLE; | ||
| 894 | $base = pmml($base); } | ||
| 895 | # Get the (possibly padded) over & under scripts (if any) | ||
| 896 | |||
| 897 | my $result = $base; | ||
| 898 | for my $midscript (@$midscripts) { | ||
| 899 | my $under = (!defined $$midscript[0] ? undef | ||
| 900 | : pmml_scriptsize_padded($$midscript[0], $emb_left, $emb_right)); | ||
| 901 | my $over = (!defined $$midscript[1] ? undef | ||
| 902 | : pmml_scriptsize_padded($$midscript[1], $emb_left, $emb_right)); | ||
| 903 | if (!defined $over) { | ||
| 904 | $result = ['m:munder', {}, $result, $under]; } | ||
| 905 | elsif (!defined $under) { | ||
| 906 | $result = ['m:mover', {}, $result, $over]; } | ||
| 907 | else { | ||
| 908 | $result = ['m:munderover', {}, $result, $under, $over]; } } | ||
| 909 | return $result; } } | ||
| 910 | |||
| 911 | # Convert an over or under script to scriptsize, | ||
| 912 | # but pad by phantoms of the base's embellishments, if any. | ||
| 913 | # This is to handle primed sums, etc.... | ||
| 914 | sub pmml_scriptsize_padded { | ||
| 915 | my ($script, $emb_left, $emb_right) = @_; | ||
| 916 | return ($emb_left || $emb_right | ||
| 917 | ? ['m:mrow', {}, | ||
| 918 | ($emb_left ? (['m:mphantom', {}, pmml_scriptsize($emb_left)]) : ()), | ||
| 919 | pmml_scriptsize($script), | ||
| 920 | ($emb_right ? (['m:mphantom', {}, pmml_scriptsize($emb_right)]) : ())] | ||
| 921 | : pmml_scriptsize($script)); } | ||
| 922 | |||
| 923 | # base is already converted | ||
| 924 | sub pmml_script_multi_layout { | ||
| 925 | my ($base, $prescripts, $postscripts) = @_; | ||
| 926 | if (scalar(@$prescripts) > 0) { | ||
| 927 | return ['m:mmultiscripts', {}, | ||
| 928 | $base, | ||
| 929 | (map { (pmml_scriptsize($_->[0]), pmml_scriptsize($_->[1])) } @$postscripts), | ||
| 930 | ['m:mprescripts'], | ||
| 931 | (map { (pmml_scriptsize($_->[0]), pmml_scriptsize($_->[1])) } @$prescripts)]; } | ||
| 932 | elsif (scalar(@$postscripts) > 1) { | ||
| 933 | return ['m:mmultiscripts', {}, | ||
| 934 | $base, | ||
| 935 | (map { (pmml_scriptsize($_->[0]), pmml_scriptsize($_->[1])) } @$postscripts)]; } | ||
| 936 | elsif (scalar(@$postscripts) == 0) { | ||
| 937 | return $base; } | ||
| 938 | elsif (!defined $$postscripts[0][1]) { | ||
| 939 | return ['m:msub', {}, $base, pmml_scriptsize($$postscripts[0][0])]; } | ||
| 940 | elsif (!defined $$postscripts[0][0]) { | ||
| 941 | return ['m:msup', {}, $base, pmml_scriptsize($$postscripts[0][1])]; } | ||
| 942 | else { | ||
| 943 | return ['m:msubsup', {}, $base, | ||
| 944 | pmml_scriptsize($$postscripts[0][0]), pmml_scriptsize($$postscripts[0][1])]; } } | ||
| 945 | |||
| 946 | # Various pre, post and even mid scripts can be wrapped around a base element. | ||
| 947 | # Try to decipher such a nesting (in the XMath element) to collect these separate groups | ||
| 948 | # They propbably shouldn't be stirred up, but appear in pre, mid,post order, | ||
| 949 | # otherwise it's not at all clear how this was expected to look; likely an upstream error? | ||
| 950 | # Nor should there be more than a single sub & single sup mid positioned script! | ||
| 951 | sub pmml_script_decipher { | ||
| 952 | my ($op, $base, $script) = @_; | ||
| 953 | my (@pres, @mids, @posts); | ||
| 954 | my ($prelevel, $midlevel, $postlevel) = (0, 0, 0); | ||
| 955 | my $sawmid = 0; | ||
| 956 | my ($emb_left, $emb_right) = (undef, undef); # embellishments of base on left & right | ||
| 957 | my ($y) = ($op->getAttribute('role') || '') =~ /^(SUPER|SUB)SCRIPTOP$/; | ||
| 958 | my ($pos, $level) = ($op->getAttribute('scriptpos') || 'post0') | ||
| 959 | =~ /^(pre|mid|post)?(\d+)?$/; | ||
| 960 | |||
| 961 | if ($pos eq 'pre') { | ||
| 962 | if ($y eq 'SUB') { | ||
| 963 | push(@pres, [$script, undef]); $prelevel = $level; } | ||
| 964 | elsif ($y eq 'SUPER') { | ||
| 965 | push(@pres, [undef, $script]); $prelevel = $level; } } | ||
| 966 | elsif ($pos eq 'mid') { | ||
| 967 | $sawmid = 1; | ||
| 968 | if ($y eq 'SUB') { | ||
| 969 | push(@mids, [$script, undef]); $midlevel = $level; } | ||
| 970 | elsif ($y eq 'SUPER') { | ||
| 971 | push(@mids, [undef, $script]); $midlevel = $level; } } | ||
| 972 | else { # else it's post | ||
| 973 | if ($y eq 'SUB') { | ||
| 974 | push(@posts, [$script, undef]); $postlevel = $level; } | ||
| 975 | elsif ($y eq 'SUPER') { | ||
| 976 | push(@posts, [undef, $script]); $postlevel = $level; } } | ||
| 977 | |||
| 978 | # Examine $base to see if there are nested scripts. | ||
| 979 | # We'll fold them together they seem to be on the appropriate levels | ||
| 980 | # Keep from having multiple scripts when $loc is stack!!! | ||
| 981 | while (1) { | ||
| 982 | $base = realize($base, 'presentation'); | ||
| 983 | last unless getQName($base) eq 'ltx:XMApp'; | ||
| 984 | my ($xop, $xbase, $xscript) = element_nodes($base); | ||
| 985 | last unless (getQName($xop) eq 'ltx:XMTok'); | ||
| 986 | my ($ny) = ($xop->getAttribute('role') || '') =~ /^(SUPER|SUB)SCRIPTOP$/; | ||
| 987 | last unless $ny; | ||
| 988 | my ($nx, $nl) = ($xop->getAttribute('scriptpos') || 'post0') | ||
| 989 | =~ /^(pre|mid|post)?(\d+)?$/; | ||
| 990 | my $spos = ($ny eq 'SUB' ? 0 : 1); | ||
| 991 | |||
| 992 | if ($nx eq 'pre') { | ||
| 993 | push(@pres, [undef, undef]) # New empty pair (?) | ||
| 994 | if ($prelevel ne $nl) || $pres[-1][$spos]; | ||
| 995 | $pres[-1][$spos] = $xscript; $prelevel = $nl; } | ||
| 996 | elsif ($nx eq 'mid') { | ||
| 997 | $sawmid = 1; | ||
| 998 | unshift(@mids, [undef, undef]) # New empty pair (?) | ||
| 999 | if ($midlevel ne $nl) || $mids[0][$spos]; | ||
| 1000 | $mids[0][$spos] = $xscript; $midlevel = $nl; } | ||
| 1001 | else { | ||
| 1002 | if ($sawmid) { # If we already saw mid-scripts (over/under); check for embellishmnt | ||
| 1003 | $emb_right = $xscript; | ||
| 1004 | last; } | ||
| 1005 | unshift(@posts, [undef, undef]) # New empty pair (?) | ||
| 1006 | if ($postlevel ne $nl) || $posts[0][$spos]; | ||
| 1007 | $posts[0][$spos] = $xscript; $postlevel = $nl; } | ||
| 1008 | $base = $xbase; | ||
| 1009 | } | ||
| 1010 | return ($base, [@pres], [@mids], [@posts], $emb_left, $emb_right); } | ||
| 1011 | |||
| 1012 | # Handle text contents. | ||
| 1013 | # Note that (currently) MathML doesn't allow math nested in m:mtext, | ||
| 1014 | # nor in fact any other markup within m:mtext, | ||
| 1015 | # but LaTeXML creates that, if the document is structured that way. | ||
| 1016 | # Here we try to flatten the contents to strings, but keep the math as math | ||
| 1017 | sub pmml_text_aux { | ||
| 1018 | my ($node, %attr) = @_; | ||
| 1019 | return () unless $node; | ||
| 1020 | my $type = $node->nodeType; | ||
| 1021 | if ($type == XML_TEXT_NODE) { | ||
| 1022 | my ($string, %mmlattr) = stylizeContent($node, 'm:mtext', %attr); | ||
| 1023 | $string =~ s/^\s+/$NBSP/; $string =~ s/\s+$/$NBSP/; | ||
| 1024 | return ['m:mtext', {%mmlattr}, $string]; } | ||
| 1025 | elsif ($type == XML_DOCUMENT_FRAG_NODE) { | ||
| 1026 | return map { pmml_text_aux($_, %attr) } $node->childNodes; } | ||
| 1027 | elsif ($type == XML_ELEMENT_NODE) { | ||
| 1028 | if (my $font = $node->getAttribute('font')) { $attr{font} = $font; } | ||
| 1029 | if (my $size = $node->getAttribute('fontsize')) { $attr{fontsize} = $size; } | ||
| 1030 | if (my $color = $node->getAttribute('color')) { $attr{color} = $color; } | ||
| 1031 | if (my $bgcolor = $node->getAttribute('backgroundcolor')) { $attr{backgroundcolor} = $bgcolor; } | ||
| 1032 | if (my $opacity = $node->getAttribute('opacity')) { $attr{opacity} = $opacity; } | ||
| 1033 | my $tag = getQName($node); | ||
| 1034 | |||
| 1035 | if ($tag eq 'ltx:Math') { | ||
| 1036 | # [NOTE BUG!!! we're not passing through the context... (but maybe pick it up anyway)] | ||
| 1037 | # If XMath still there, convert it now. | ||
| 1038 | if (my $xmath = $LaTeXML::Post::DOCUMENT->findnode('ltx:XMath', $node)) { | ||
| 1039 | return pmml($xmath); } | ||
| 1040 | # Otherwise, may already have gotten converted ? return that | ||
| 1041 | elsif (my $mml = $LaTeXML::Post::DOCUMENT->findnode('m:math', $node)) { | ||
| 1042 | return $mml->childNodes; } | ||
| 1043 | else { # ??? | ||
| 1044 | return (); } } | ||
| 1045 | elsif (($tag eq 'ltx:text') # ltx:text element is fine, if we can manage the attributes! | ||
| 1046 | && (!grep { $node->hasAttribute($_) } qw(framed framecolor))) { | ||
| 1047 | return pmml_maybe_resize($node, pmml_row(map { pmml_text_aux($_, %attr) } $node->childNodes)); } | ||
| 1048 | elsif ($tag eq 'ltx:picture') { # Embeded pictures might legitimately have nested math? | ||
| 1049 | return ['m:mtext', {}, | ||
| 1050 | $LaTeXML::Post::MATHPROCESSOR->convertXMTextContent($LaTeXML::Post::DOCUMENT, 1, $node)]; } | ||
| 1051 | else { | ||
| 1052 | # We could just recurse on raw content like this, but it loses a lot... | ||
| 1053 | ### map(pmml_text_aux($_,%attr), $node->childNodes); }} | ||
| 1054 | # So, let's just include the raw latexml markup, let the xslt convert it | ||
| 1055 | # And hope that the ultimate agent can deal with it! | ||
| 1056 | my ($ignore, %mmlattr) = stylizeContent($node, 'm:mtext', %attr); | ||
| 1057 | delete $mmlattr{stretchy}; # not useful (not too sure | ||
| 1058 | Warn('unexpected', 'nested-math', $node, | ||
| 1059 | "We're getting m:math nested within an m:mtext") | ||
| 1060 | if $LaTeXML::Post::DOCUMENT->findnodes('.//ltx:Math', $node); | ||
| 1061 | return ['m:mtext', {%mmlattr}, $LaTeXML::Post::DOCUMENT->cloneNode($node, 'nest')]; } } | ||
| 1062 | else { | ||
| 1063 | return (); } } | ||
| 1064 | |||
| 1065 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 1066 | # Adjust the spacing by comparing TeX & author specified spacing to MathML's defaults | ||
| 1067 | sub adjust_spacing { | ||
| 1068 | my ($self, $mml) = @_; | ||
| 1069 | space_walk($self, $mml); | ||
| 1070 | return; } | ||
| 1071 | |||
| 1072 | our %tag_arg_pattern = ( | ||
| 1073 | (map { $_ => 'atom'; } qw(m:mi m:mo m:mn m:ms m:mtext)), | ||
| 1074 | (map { $_ => 'mrow'; } qw(m:mrow m:mpadded m:msqrt m:mstyle m:merror m:mphantom m:mtd)), | ||
| 1075 | ## (map { $_=>'col'; } qw(m:mfrac m:mroot m:mtable m:mtr | ||
| 1076 | ## m:msub m:msup m:msubsup m:munder m:mover m:munderover m:mmultiscripts)) | ||
| 1077 | ); | ||
| 1078 | |||
| 1079 | # Walk through the MathML tree (ASSUMED represented in ARRAY form) | ||
| 1080 | # to determine spacing between visually adjacent items according to TeX rules | ||
| 1081 | # and adjust if these do not correspond to MathML's rule. | ||
| 1082 | # Since we're unwinding mrows with wild abandon, it is now difficult to insert a new item (eg. m:mspace), | ||
| 1083 | # since we've lost the potitions of the items within the actual tree! | ||
| 1084 | sub space_walk { | ||
| 1085 | my ($self, $node) = @_; | ||
| 1086 | return unless $node; | ||
| 1087 | my ($tag, $attr, @children) = @$node; | ||
| 1088 | my $type = $tag_arg_pattern{$tag} || 'other'; | ||
| 1089 | if ($type eq 'atom') { } # Atomic things don't need any adjustments | ||
| 1090 | elsif ($type eq 'mrow') { # look at adjacent pairs in mrow-like things | ||
| 1091 | my @nodes = @children; | ||
| 1092 | my $prev = shift(@nodes); | ||
| 1093 | while ($prev && ($$prev[0] eq 'm:mrow')) { # Unwrap mrows | ||
| 1094 | unshift(@nodes, @$prev[2 .. $#$prev]); | ||
| 1095 | $prev = shift(@nodes); } | ||
| 1096 | space_walk($self, $prev); | ||
| 1097 | while (my $next = shift(@nodes)) { | ||
| 1098 | my $invisop; # Save Invisible operators as potential target for (l|r)space | ||
| 1099 | if (($$next[0] eq 'm:mo') && $$next[2] && ($$next[2] =~ /^[\x{200B}\x{2061}\x{2062}\x{2063}]*$/)) { | ||
| 1100 | $invisop = $next; | ||
| 1101 | $next = shift(@nodes); | ||
| 1102 | last unless $next; } | ||
| 1103 | if ($$next[0] eq 'm:mrow') { # Unwrap mrows | ||
| 1104 | unshift(@nodes, @$next[2 .. $#$next]); | ||
| 1105 | unshift(@nodes, $invisop) if $invisop; | ||
| 1106 | next; } | ||
| 1107 | # Unwrap scripts, too; Note that TeX doesn't group according to scripts. | ||
| 1108 | # In fact, the scripts attach even to ")" and don't affect the spacing between atoms | ||
| 1109 | if ($$next[0] =~ /^m:(msup|msub|munder|mover|mmultiscripts)/) { | ||
| 1110 | unshift(@nodes, $$next[2]); # Unwrap the base | ||
| 1111 | foreach my $script (@$next[3 .. $#$next]) { # but recurse on the scripts | ||
| 1112 | space_walk($self, $script); } | ||
| 1113 | unshift(@nodes, $invisop) if $invisop; | ||
| 1114 | next; } | ||
| 1115 | space_walk($self, $next); # Recurse, if necessary into any non-atomic atoms. | ||
| 1116 | adjust_pair($self, $prev, $next, $invisop); # Finally, Adjust the spacing between this pair. | ||
| 1117 | $prev = $next; } } | ||
| 1118 | else { # Anything else, just process children individually | ||
| 1119 | map { space_walk($self, $_); } @children; } | ||
| 1120 | return; } | ||
| 1121 | |||
| 1122 | # A minimal size computation; Currently only for tokens. | ||
| 1123 | sub compute_size { | ||
| 1124 | my ($node) = @_; | ||
| 1125 | my ($tag, $attr, @children) = @$node; | ||
| 1126 | my $type = $tag_arg_pattern{$tag} || 'other'; | ||
| 1127 | if ($type eq 'atom') { | ||
| 1128 | my $font = LaTeXML::Common::Font->mathDefault(); | ||
| 1129 | my ($w, $h, $d) = $font->computeStringSize($children[0]); | ||
| 1130 | # Heuristic (ridiculous) hack to accommodate MathJax fonts | ||
| 1131 | if ($w && $$attr{class} && ($$attr{class} =~ /mathscript/)) { | ||
| 1132 | $w = $w->larger(LaTeXML::Common::Dimension->new(10 * 65535)); } # Minimum of 10pt | ||
| 1133 | return ($w, $h, $d); } | ||
| 1134 | else { | ||
| 1135 | return; } } | ||
| 1136 | |||
| 1137 | # This maps LaTeXML's role values to the smaller set of TeX's math atom types | ||
| 1138 | our $role_atomtype = { | ||
| 1139 | ATOM => 'Ord', | ||
| 1140 | UNKNOWN => 'Ord', | ||
| 1141 | ID => 'Ord', | ||
| 1142 | NUMBER => 'Ord', | ||
| 1143 | ARRAY => 'Inner', | ||
| 1144 | RELOP => 'Rel', | ||
| 1145 | OPEN => 'Open', | ||
| 1146 | CLOSE => 'Close', | ||
| 1147 | MIDDLE => 'Ord', | ||
| 1148 | PUNCT => 'Punct', | ||
| 1149 | VERTBAR => 'Punct', | ||
| 1150 | PERIOD => 'Punct', | ||
| 1151 | METARELOP => 'Rel', | ||
| 1152 | MODIFIEROP => 'Rel', | ||
| 1153 | MODIFIER => 'Rel', | ||
| 1154 | ARROW => 'Rel', | ||
| 1155 | ADDOP => 'Bin', | ||
| 1156 | MULOP => 'Bin', | ||
| 1157 | BINOP => 'Bin', | ||
| 1158 | POSTFIX => 'Ord', #? | ||
| 1159 | FUNCTION => 'Ord', | ||
| 1160 | OPFUNCTION => 'Op', | ||
| 1161 | TRIGFUNCTION => 'Op', | ||
| 1162 | APPLYOP => 'Bin', | ||
| 1163 | COMPOSEOP => 'Bin', | ||
| 1164 | SUPOP => 'Ord', #? | ||
| 1165 | BIGOP => 'Op', | ||
| 1166 | SUMOP => 'Op', | ||
| 1167 | INTOP => 'Op', | ||
| 1168 | LIMITOP => 'Op', | ||
| 1169 | DIFFOP => 'Ord', #? Wants to be close to arg, but preferably has \, before | ||
| 1170 | OPERATOR => 'Op', | ||
| 1171 | POSTSUBSCRIPT => 'Inner', | ||
| 1172 | POSTSUPERSCRIPT => 'Inner', | ||
| 1173 | FLOATSUPERSCRIPT => 'Inner', | ||
| 1174 | FLOATSUBSCRIPT => 'Inner', }; | ||
| 1175 | |||
| 1176 | # very rough approximation... | ||
| 1177 | our $atomtype_form = { | ||
| 1178 | Op => 'prefix', Bin => 'infix', Rel => 'infix', Open => 'prefix', Close => 'postfix', Punct => 'postfix' }; | ||
| 1179 | # Need array of left & right atom types to TeX's intended spacing | ||
| 1180 | # 0 : no space; 1 : thin space; 2 : medium space; 3 thick space. | ||
| 1181 | # 0, 3, 4, 5 muskip, respectively | ||
| 1182 | # (use negative for spacing only in display & text style; use 0 for * which means "shouldn't happen") | ||
| 1183 | # THUS, we'll need to know which style!!! | ||
| 1184 | our $atompair_spacing = { | ||
| 1185 | Ord => { Ord => 0, Op => 1, Bin => -2, Rel => -3, Open => 0, Close => 0, Punct => 0, Inner => -1 }, | ||
| 1186 | Op => { Ord => 1, Op => 1, Bin => 0, Rel => -3, Open => 0, Close => 0, Punct => 0, Inner => -1 }, | ||
| 1187 | Bin => { Ord => -2, Op => -2, Bin => 0, Rel => 0, Open => -2, Close => 0, Punct => 0, Inner => -2 }, | ||
| 1188 | Rel => { Ord => -3, Op => -3, Bin => 0, Rel => 0, Open => -3, Close => 0, Punct => 0, Inner => -3 }, | ||
| 1189 | Open => { Ord => 0, Op => 0, Bin => 0, Rel => 0, Open => 0, Close => 0, Punct => 0, Inner => 0 }, | ||
| 1190 | Close => { Ord => 0, Op => 1, Bin => -2, Rel => -3, Open => 0, Close => 0, Punct => 0, Inner => -1 }, | ||
| 1191 | Punct => { Ord => -1, Op => -1, Bin => 0, Rel => -1, Open => -1, Close => -1, Punct => -1, Inner => -1 }, | ||
| 1192 | Inner => { Ord => -1, Op => 1, Bin => -2, Rel => -3, Open => -1, Close => 0, Punct => -1, Inner => -1 }, | ||
| 1193 | ## Inner => { Ord => 0, Op => 1, Bin => -2, Rel => -3, Open => 0, Close => 0, Punct => 0, Inner => -1 }, | ||
| 1194 | }; | ||
| 1195 | # Maybe I'm confused about Inner (\frac??), but the TeX book values seem to give larger spacing than I see in pdf | ||
| 1196 | # It really seems to behave more like an Ord | ||
| 1197 | # The above is NOT quite Knuth's table; but pdf looks to me like \frac[Inner] ( has NO space between! | ||
| 1198 | |||
| 1199 | our @tex_spacing = (0, 0.167, 0.222, 0.2778); # 0,3mu=3/18em, 4mu, 5mu | ||
| 1200 | our %embellisher = map { $_ => 1; } qw(m:msub m:msup m:msubsup m:munder m:mover m:munderover); | ||
| 1201 | ##our %m_atomtype = ('m:mfrac'=>'Inner','m:marray'=>'Inner','m:mspace'=>'Ord'); | ||
| 1202 | # NOTE: I'm treating mfrac as an Ord, NOT an Inner (which seems to get too much spacing, more than even pdf!?) | ||
| 1203 | our %m_atomtype = ('m:mfrac' => 'Ord', 'm:marray' => 'Inner', 'm:mspace' => 'Ord'); | ||
| 1204 | |||
| 1205 | our $epsilon = 0.01; # ems; Ignore any differences below this | ||
| 1206 | our $fudge = 0.3; # ems; don't complain if we can't adjust less than this | ||
| 1207 | |||
| 1208 | sub adjust_pair { | ||
| 1209 | my ($self, $prev, $next, $invisop) = @_; | ||
| 1210 | # Determine the spacing TeX wants between $prev & $next | ||
| 1211 | # Consider the spacing on the right of $prev and left of $next. | ||
| 1212 | # Combine any explict padding with TeX spacing based on math atom categories | ||
| 1213 | my ($iprev, $inext) = ($prev, $next); # Inner (possibly embellished, possibly operator) for OperatorDictionary | ||
| 1214 | while ($iprev && $embellisher{ $$iprev[0] }) { $iprev = $$iprev[2]; } | ||
| 1215 | while ($inext && $embellisher{ $$inext[0] }) { $inext = $$inext[2]; } | ||
| 1216 | my $prev_req_right = $$prev[1]{_rpadding} // 0; # Author spacing, in em | ||
| 1217 | my $next_req_left = $$next[1]{_lpadding} // 0; | ||
| 1218 | my $prev_role = $$iprev[1]{_role} // 'ATOM'; | ||
| 1219 | my $next_role = $$inext[1]{_role} // 'ATOM'; | ||
| 1220 | my $prev_type = $m_atomtype{ $$iprev[0] } || $$role_atomtype{$prev_role} || 'Ord'; | ||
| 1221 | my $next_type = $m_atomtype{ $$inext[0] } || $$role_atomtype{$next_role} || 'Ord'; | ||
| 1222 | my $tex_code = $$atompair_spacing{$prev_type}{$next_type} // 0; | ||
| 1223 | my $tex_space = $tex_spacing[abs($tex_code)]; | ||
| 1224 | my $req_space = $prev_req_right + $next_req_left; | ||
| 1225 | my $target = $req_space + $tex_space; | ||
| 1226 | # Now find the default spacing from MathML's operator dictionary (stored by stylizeContent) | ||
| 1227 | my $prev_dict_right = $$iprev[1]{_rspace} // 0; | ||
| 1228 | my $next_dict_left = $$inext[1]{_lspace} // 0; | ||
| 1229 | my $default = ($prev_dict_right + $next_dict_left); | ||
| 1230 | my $needs_adjustment = abs($target - $default) > $epsilon; # not ignorably small? | ||
| 1231 | Debug("SPACEWALK: TeX $tex_space + $req_space vs OpDict $default : " | ||
| 1232 | . ($needs_adjustment ? colorizeString("ADJUSTMENT $target", 'warning') : " NO adjustment") | ||
| 1233 | # . ($invisop ? " [with invisible op]" : '') | ||
| 1234 | . LaTeXML::Post::MathProcessor::shownode($prev) . " $prev_type " | ||
| 1235 | . LaTeXML::Post::MathProcessor::shownode($next) . " $next_type " | ||
| 1236 | ) if # $needs_adjustment && | ||
| 1237 | $LaTeXML::DEBUG{mathspacing}; | ||
| 1238 | return unless $needs_adjustment; | ||
| 1239 | # Note that in MML Core, neither mspace nor mpadded can have negative width! | ||
| 1240 | # It also does not support relative width using +/- prefix! | ||
| 1241 | # So, the only alternative is to create an mpadded with an ADJUSTED width. | ||
| 1242 | # NOTE: spacing in ems seems to be more portable. | ||
| 1243 | if ($target < 0) { # Ugh. "rewrap" $prev in m:mpadded, IN PLACE! | ||
| 1244 | my ($w, $h, $d) = compute_size($prev); # BAD & Wrong! | ||
| 1245 | if ($w) { | ||
| 1246 | my $reqw = $w->ptValue / 10.0 + $target; | ||
| 1247 | $reqw = 0 if $reqw < 0; | ||
| 1248 | splice(@$prev, 0, $#$prev + 1, 'm:mpadded', { width => fmt_em($reqw) }, [@$prev]); } } | ||
| 1249 | elsif ($$prev[0] eq 'm:mspace') { $$prev[1]{width} = fmt_em($target + getXMHintSpacing($$prev[1]{width})); } | ||
| 1250 | elsif ($$next[0] eq 'm:mspace') { $$next[1]{width} = fmt_em($target + getXMHintSpacing($$next[1]{width})); } | ||
| 1251 | elsif ($invisop) { $$invisop[1]{lspace} = fmt_em($target); } | ||
| 1252 | elsif (($$prev[0] eq 'm:mo') && ($$next[0] eq 'm:mo')) { # BOTH are mo, so account for each's spacing | ||
| 1253 | my $p = $$prev[1]{_rspace} // 0; | ||
| 1254 | my $n = $$next[1]{_lspace} // 0; | ||
| 1255 | my $rem; | ||
| 1256 | if (($rem = $target - $n) >= 0) { | ||
| 1257 | $$prev[1]{rspace} = fmt_em($rem > $epsilon ? $rem : 0); } | ||
| 1258 | elsif (($rem = $target - $p) >= 0) { | ||
| 1259 | $$next[1]{lspace} = fmt_em($rem > $epsilon ? $rem : 0); } | ||
| 1260 | else { | ||
| 1261 | $rem = $target / 2; | ||
| 1262 | $$prev[1]{rspace} = $rem . 'em' if $rem != $p; | ||
| 1263 | $$next[1]{lspace} = $rem . 'em' if $rem != $n; } } | ||
| 1264 | elsif ($$prev[0] eq 'm:mo') { $$prev[1]{rspace} = fmt_em($target); } | ||
| 1265 | elsif ($$next[0] eq 'm:mo') { $$next[1]{lspace} = fmt_em($target); } | ||
| 1266 | elsif (abs($target - $default) > $fudge) { | ||
| 1267 | Info('ignored', 'spacing', undef, | ||
| 1268 | "No place to set spacing to $target (default $default) between" | ||
| 1269 | . LaTeXML::Post::MathProcessor::shownode($prev) | ||
| 1270 | . " and " . LaTeXML::Post::MathProcessor::shownode($next)); } | ||
| 1271 | return; } | ||
| 1272 | |||
| 1273 | sub fmt_em { return ($_[0] ? sprintf("%.3fem", $_[0]) : '0em'); } | ||
| 1274 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 1275 | # Support functions for Content MathML | ||
| 1276 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 1277 | |||
| 1278 | sub cmml_top { | ||
| 1279 | my ($self, $node) = @_; | ||
| 1280 | local $LaTeXML::MathML::STYLE = 'text'; | ||
| 1281 | local $LaTeXML::MathML::FONT = find_inherited_attribute($node, 'font'); | ||
| 1282 | local $LaTeXML::MathML::SIZE = find_inherited_attribute($node, 'fontsize') || '100%'; | ||
| 1283 | local $LaTeXML::MathML::COLOR = find_inherited_attribute($node, 'color'); | ||
| 1284 | local $LaTeXML::MathML::BGCOLOR = find_inherited_attribute($node, 'backgroundcolor'); | ||
| 1285 | local $LaTeXML::MathML::OPACITY = find_inherited_attribute($node, 'opacity'); | ||
| 1286 | local $LaTeXML::MathML::DESIRED_SIZE = $LaTeXML::MathML::SIZE; | ||
| 1287 | return cmml_contents($node); } | ||
| 1288 | |||
| 1289 | sub cmml { | ||
| 1290 | no warnings 'recursion'; | ||
| 1291 | my ($node) = @_; | ||
| 1292 | if (getQName($node) eq 'ltx:XMRef') { | ||
| 1293 | $node = realize($node); } | ||
| 1294 | my $result = cmml_internal($node); | ||
| 1295 | # Associate the generated node with the source XMath node. | ||
| 1296 | $LaTeXML::Post::MATHPROCESSOR->associateNode($result, $node); | ||
| 1297 | return $result; } | ||
| 1298 | |||
| 1299 | sub cmml_internal { | ||
| 1300 | no warnings 'recursion'; | ||
| 1301 | my ($node) = @_; | ||
| 1302 | return ['m:merror', {}, ['m:mtext', {}, "Missing Subexpression"]] unless $node; | ||
| 1303 | $node = realize($node) if getQName($node) eq 'ltx:XMRef'; | ||
| 1304 | my $tag = getQName($node); | ||
| 1305 | if ($tag eq 'ltx:XMDual') { | ||
| 1306 | my ($content, $presentation) = element_nodes($node); | ||
| 1307 | return cmml($content); } | ||
| 1308 | elsif (($tag eq 'ltx:XMWrap') || ($tag eq 'ltx:XMArg')) { # Only present if parsing failed! | ||
| 1309 | return cmml_contents($node); } | ||
| 1310 | elsif ($tag eq 'ltx:XMApp') { | ||
| 1311 | if (my $meaning = $node->getAttribute('meaning')) { | ||
| 1312 | return &{ lookupContent('Token', $node->getAttribute('role'), $meaning) }($node); } | ||
| 1313 | # Experiment: If XMApp has role ID, we treat it as a "Decorated Symbol" | ||
| 1314 | if (($node->getAttribute('role') || '') eq 'ID') { | ||
| 1315 | return cmml_decoratedSymbol($node); } | ||
| 1316 | else { | ||
| 1317 | my ($op, @args) = element_nodes($node); | ||
| 1318 | my $rop = $op; | ||
| 1319 | if (!$op || !($rop = realize($op))) { | ||
| 1320 | return ['m:merror', {}, ['m:mtext', {}, "Missing Operator"]]; } | ||
| 1321 | else { | ||
| 1322 | return &{ lookupContent('Apply', $rop->getAttribute('role'), $rop->getAttribute('meaning')) }($op, @args); } } } | ||
| 1323 | elsif ($tag eq 'ltx:XMTok') { | ||
| 1324 | return &{ lookupContent('Token', $node->getAttribute('role'), $node->getAttribute('meaning')) }($node); } | ||
| 1325 | elsif ($tag eq 'ltx:XMHint') { # ???? | ||
| 1326 | return &{ lookupContent('Hint', $node->getAttribute('role'), $node->getAttribute('meaning')) }($node); } | ||
| 1327 | elsif ($tag eq 'ltx:XMArray') { | ||
| 1328 | return &{ lookupContent('Array', $node->getAttribute('role'), $node->getAttribute('meaning')) }($node); } | ||
| 1329 | elsif ($tag eq 'ltx:XMText') { | ||
| 1330 | return cmml_decoratedSymbol($node); } | ||
| 1331 | else { | ||
| 1332 | return cmml_decoratedSymbol($node); } } | ||
| 1333 | |||
| 1334 | # Convert the contents of a node, which normally should contain a single child. | ||
| 1335 | # It may be empty (assumed to be an error), | ||
| 1336 | # or contain multiple nodes (presumably not properly parsed). | ||
| 1337 | # We really should use m:cerror here, but need to find appropriate csymbol cd:name | ||
| 1338 | sub cmml_contents { | ||
| 1339 | my ($node) = @_; | ||
| 1340 | my ($item, @rest) = element_nodes($node); | ||
| 1341 | if (!$item) { | ||
| 1342 | return ['m:cerror', {}, ['m:csymbol', { cd => 'ambiguous' }, 'missing-subexpression']]; } | ||
| 1343 | elsif (@rest) { | ||
| 1344 | return cmml_unparsed($item, @rest); } | ||
| 1345 | else { | ||
| 1346 | return cmml($item); } } | ||
| 1347 | |||
| 1348 | sub cmml_unparsed { | ||
| 1349 | my (@nodes) = @_; | ||
| 1350 | my @results = (); | ||
| 1351 | foreach my $node (@nodes) { | ||
| 1352 | # Deal with random, unknown symbols, but still record association. | ||
| 1353 | if ((getQName($node) eq 'ltx:XMTok') | ||
| 1354 | && (($node->getAttribute('role') || 'UNKNOWN') eq 'UNKNOWN')) { | ||
| 1355 | my $result = ['m:csymbol', { cd => 'unknown' }, $node->textContent]; | ||
| 1356 | $LaTeXML::Post::MATHPROCESSOR->associateNode($result, $node); | ||
| 1357 | push(@results, $result); } | ||
| 1358 | else { | ||
| 1359 | push(@results, cmml($node)); } } | ||
| 1360 | return ['m:cerror', {}, | ||
| 1361 | ['m:csymbol', { cd => 'ambiguous' }, 'fragments'], | ||
| 1362 | @results]; } | ||
| 1363 | |||
| 1364 | # Or csymbol if there's some kind of "defining" attribute? | ||
| 1365 | sub cmml_leaf { | ||
| 1366 | my ($item) = @_; | ||
| 1367 | if (my $meaning = (ref $item) && $item->getAttribute('meaning')) { | ||
| 1368 | if (my $cd = $item->getAttribute('omcd')) { | ||
| 1369 | return ['m:csymbol', { cd => $cd }, $meaning]; } | ||
| 1370 | elsif (($item->getAttribute('role') || '') eq 'NUMBER') { | ||
| 1371 | # special case, numbers with a meaning attribute | ||
| 1372 | return ['m:cn', { type => ($meaning =~ /^[+-]?\d+$/ ? 'integer' : 'float') }, $meaning]; } | ||
| 1373 | else { | ||
| 1374 | return ['m:csymbol', { cd => 'latexml' }, $meaning]; } } | ||
| 1375 | else { | ||
| 1376 | my ($content, %mmlattr) = stylizeContent($item, 'm:ci'); | ||
| 1377 | if (my $mv = $mmlattr{mathvariant}) { | ||
| 1378 | $content = $mv . "-" . $content; } | ||
| 1379 | return ['m:ci', {}, $content]; } } | ||
| 1380 | |||
| 1381 | # Experimental; for an XMApp with role=ID, we treat it as a ci | ||
| 1382 | # or ultimately as csymbol, if it had defining attributes, | ||
| 1383 | # but we format its contents as pmml | ||
| 1384 | sub cmml_decoratedSymbol { | ||
| 1385 | my ($item) = @_; | ||
| 1386 | # Presumably, if we're claiming this blob has "meaning", we should just get a csymbol | ||
| 1387 | if (my $meaning = (ref $item) && $item->getAttribute('meaning')) { | ||
| 1388 | my $cd = $item->getAttribute('omcd') || 'latexml'; | ||
| 1389 | return ['m:csymbol', { cd => $cd }, $meaning]; } | ||
| 1390 | else { # Otherwise, wrap as needed | ||
| 1391 | return ['m:ci', {}, pmml($item)]; } } | ||
| 1392 | |||
| 1393 | # Return the NOT of the argument. | ||
| 1394 | sub cmml_not { | ||
| 1395 | my ($arg) = @_; | ||
| 1396 | return ['m:apply', {}, ['m:not', {}], cmml($arg)]; } | ||
| 1397 | |||
| 1398 | sub cmml_synth_not { | ||
| 1399 | my ($op, @args) = @_; | ||
| 1400 | return ['m:apply', {}, ['m:not', {}], ['m:apply', {}, [$op, {}], map { cmml($_) } @args]]; } | ||
| 1401 | |||
| 1402 | # Return the result of converting the arguments, but reversed. | ||
| 1403 | sub cmml_synth_complement { | ||
| 1404 | my ($op, @args) = @_; | ||
| 1405 | return ['m:apply', {}, [$op, {}], map { cmml($_) } reverse(@args)]; } | ||
| 1406 | |||
| 1407 | # Given an XMath node, Make sure it has an id so it can be shared, then convert to cmml | ||
| 1408 | sub cmml_shared { | ||
| 1409 | my ($node) = @_; | ||
| 1410 | $LaTeXML::Post::DOCUMENT->generateNodeID($node, 'sh'); | ||
| 1411 | return cmml($node); } | ||
| 1412 | |||
| 1413 | # Given an XMath node, convert to cmml share form | ||
| 1414 | sub cmml_share { | ||
| 1415 | my ($node) = @_; | ||
| 1416 | my $fragid = $node->getAttribute('fragid'); | ||
| 1417 | if ($fragid) { | ||
| 1418 | return ['m:share', { href => '#' . $fragid . $LaTeXML::Post::MATHPROCESSOR->IDSuffix }]; } | ||
| 1419 | else { # No fragid should be error/warning or something??? | ||
| 1420 | Warn('expected', 'fragid', $node, | ||
| 1421 | "Shared node is missing fragid"); | ||
| 1422 | return ['m:share']; } } | ||
| 1423 | |||
| 1424 | sub cmml_or_compose { | ||
| 1425 | my ($operators, @args) = @_; | ||
| 1426 | my @operators = @$operators; | ||
| 1427 | if (scalar(@operators) == 1) { | ||
| 1428 | return ['m:apply', {}, [shift(@operators), {}], map { cmml($_) } @args]; } | ||
| 1429 | else { | ||
| 1430 | my @parts = (['m:apply', {}, [shift(@operators), {}], map { cmml_shared($_) } @args]); | ||
| 1431 | foreach my $op (@operators) { | ||
| 1432 | push(@parts, ['m:apply', {}, [shift(@operators), {}], map { cmml_share($_) } @args]); } | ||
| 1433 | return ['m:or', {}, @parts]; } } | ||
| 1434 | |||
| 1435 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 1436 | # Tranlators | ||
| 1437 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 1438 | # Organized according to the MathML Content element lists. | ||
| 1439 | # As a general rule, presentation conversions are based on role | ||
| 1440 | # (eg "Token:role:?"), whereas content conversions are based | ||
| 1441 | # on meaning or name (eg. "Token:?:meaning"). | ||
| 1442 | |||
| 1443 | #====================================================================== | ||
| 1444 | # Token elements: | ||
| 1445 | # cn, ci, csymbol | ||
| 1446 | |||
| 1447 | DefMathML("Token:?:?", \&pmml_mi, \&cmml_leaf); | ||
| 1448 | DefMathML("Token:PUNCT:?", \&pmml_mo, undef); | ||
| 1449 | DefMathML("Token:PERIOD:?", \&pmml_mo, undef); | ||
| 1450 | DefMathML("Token:OPEN:?", \&pmml_mo, undef); | ||
| 1451 | DefMathML("Token:CLOSE:?", \&pmml_mo, undef); | ||
| 1452 | DefMathML("Token:MIDDLE:?", \&pmml_mo, undef); | ||
| 1453 | DefMathML("Token:VERTBAR:?", \&pmml_mo, undef); | ||
| 1454 | DefMathML("Token:ARROW:?", \&pmml_mo, undef); | ||
| 1455 | DefMathML("Token:OVERACCENT:?", \&pmml_mo, undef); | ||
| 1456 | DefMathML("Token:UNDERACCENT:?", \&pmml_mo, undef); | ||
| 1457 | |||
| 1458 | DefMathML("Token:NUMBER:?", \&pmml_mn, sub { | ||
| 1459 | my $n = $_[0]->textContent; | ||
| 1460 | return ['m:cn', { type => ($n =~ /^[+-]?\d+$/ ? 'integer' : 'float') }, $n]; }); | ||
| 1461 | |||
| 1462 | DefMathML("Token:?:absent", sub { return ['m:mi', {}] }); | ||
| 1463 | |||
| 1464 | # Hints normally would have disappeared during parsing | ||
| 1465 | # (turned into punctuation or padding?) | ||
| 1466 | # but if they survive (unparsed?) turn them into space | ||
| 1467 | DefMathML('Hint:?:?', sub { | ||
| 1468 | my ($node) = @_; | ||
| 1469 | my $w = getXMHintSpacing($node->getAttribute('width')); | ||
| 1470 | # note that we MUST return some node (not undef) | ||
| 1471 | ['m:mspace', { ($w ? (width => $w . 'em') : (_ignorable => 1)) }]; }, | ||
| 1472 | sub { undef; }); # Should Disappear from cmml! | ||
| 1473 | |||
| 1474 | # At presentation level, these are essentially adorned tokens. | ||
| 1475 | # args are (accent,base) | ||
| 1476 | # Note: accents are drawn at same size as base! | ||
| 1477 | DefMathML('Apply:OVERACCENT:?', sub { | ||
| 1478 | my ($accent, $base) = @_; | ||
| 1479 | if (getQName($base) eq 'ltx:XMApp') { | ||
| 1480 | my ($xaccent, $xbase) = element_nodes($base); | ||
| 1481 | if (($xaccent->getAttribute('role') || '') eq 'UNDERACCENT') { | ||
| 1482 | return ['m:munderover', { accent => 'true', accentunder => 'true' }, | ||
| 1483 | pmml($xbase), pmml_scriptsize($xaccent), pmml($accent)]; } } | ||
| 1484 | return ['m:mover', { accent => 'true' }, pmml($base), pmml($accent)]; }); | ||
| 1485 | |||
| 1486 | DefMathML('Apply:UNDERACCENT:?', sub { | ||
| 1487 | my ($accent, $base) = @_; | ||
| 1488 | if (getQName($base) eq 'ltx:XMApp') { | ||
| 1489 | my ($xaccent, $xbase) = element_nodes($base); | ||
| 1490 | if (($xaccent->getAttribute('role') || '') eq 'OVERACCENT') { | ||
| 1491 | return ['m:munderover', { accent => 'true', accentunder => 'true' }, | ||
| 1492 | pmml($xbase), pmml_scriptsize($accent), pmml($xaccent)]; } } | ||
| 1493 | return ['m:munder', { accentunder => 'true' }, pmml($base), pmml($accent)]; }); | ||
| 1494 | |||
| 1495 | DefMathML('Apply:ENCLOSE:?', sub { | ||
| 1496 | my ($op, $base) = @_; | ||
| 1497 | my $enclosure = $op->getAttribute('enclose'); | ||
| 1498 | my $color = $op->getAttribute('color') || $LaTeXML::MathML::COLOR; | ||
| 1499 | return ['m:menclose', { notation => $enclosure, mathcolor => $color }, | ||
| 1500 | ($color ? ['m:mstyle', { mathcolor => $LaTeXML::MathML::COLOR || 'black' }, pmml($base)] | ||
| 1501 | : pmml($base))]; }); | ||
| 1502 | |||
| 1503 | #====================================================================== | ||
| 1504 | # Basic Content elements: | ||
| 1505 | # apply, interval, inverse, sep, condition, declare, lambda, compose, ident, | ||
| 1506 | # domain, codomain, image, domainofapplication, piecewise, piece, otherwise | ||
| 1507 | |||
| 1508 | DefMathML("Token:APPLYOP:?", \&pmml_mo, undef); # APPLYOP is (only) \x{2061}; FUNCTION APPLICATION | ||
| 1509 | DefMathML("Token:OPERATOR:?", \&pmml_mo, undef); | ||
| 1510 | |||
| 1511 | DefMathML('Apply:?:?', sub { | ||
| 1512 | no warnings 'recursion'; | ||
| 1513 | my ($op, @args) = @_; | ||
| 1514 | my $pop = pmml($op); | ||
| 1515 | my $inner = $pop; | ||
| 1516 | while ($inner && ($$inner[0] ne 'm:mo')) { | ||
| 1517 | last unless $$inner[0] =~ /^m:(?:msub|msup|munder|mover|mprescripts)/; | ||
| 1518 | $inner = $$inner[2]; } | ||
| 1519 | my $is_mo = $inner && ($$inner[0] eq 'm:mo'); | ||
| 1520 | return ['m:mrow', {}, | ||
| 1521 | $pop, ($is_mo ? () : pmml_mo("\x{2061}")), # FUNCTION APPLICATION only if not an m:mo | ||
| 1522 | map { pmml($_) } @args]; }, | ||
| 1523 | sub { | ||
| 1524 | no warnings 'recursion'; | ||
| 1525 | my ($op, @args) = @_; | ||
| 1526 | return ['m:apply', {}, cmml($op), map { cmml($_) } @args]; }); | ||
| 1527 | |||
| 1528 | DefMathML('Apply:COMPOSEOP:?', \&pmml_infix, undef); | ||
| 1529 | DefMathML("Token:COMPOSEOP:?", \&pmml_mo, undef); | ||
| 1530 | DefMathML("Token:DIFFOP:?", \&pmml_mo, undef); | ||
| 1531 | |||
| 1532 | # In pragmatic CMML, these are containers | ||
| 1533 | DefMathML("Apply:?:open-interval", undef, sub { | ||
| 1534 | my ($op, @args) = @_; | ||
| 1535 | return ['m:interval', { closure => "open" }, map { cmml($_) } @args]; }); | ||
| 1536 | DefMathML("Apply:?:closed-interval", undef, sub { | ||
| 1537 | my ($op, @args) = @_; | ||
| 1538 | return ['m:interval', { closure => "closed" }, map { cmml($_) } @args]; }); | ||
| 1539 | DefMathML("Apply:?:closed-open-interval", undef, sub { | ||
| 1540 | my ($op, @args) = @_; | ||
| 1541 | return ['m:interval', { closure => "closed-open" }, map { cmml($_) } @args]; }); | ||
| 1542 | DefMathML("Apply:?:open-closed-interval", undef, sub { | ||
| 1543 | my ($op, @args) = @_; | ||
| 1544 | return ['m:interval', { closure => "open-closed" }, map { cmml($_) } @args]; }); | ||
| 1545 | |||
| 1546 | DefMathML("Token:?:inverse", undef, sub { return ['m:inverse']; }); | ||
| 1547 | DefMathML("Token:?:lambda", undef, sub { return ['m:lambda']; }); | ||
| 1548 | DefMathML("Token:?:compose", undef, sub { return ['m:compose']; }); | ||
| 1549 | DefMathML("Token:?:identity", undef, sub { return ['m:ident']; }); | ||
| 1550 | DefMathML("Token:?:domain", undef, sub { return ['m:domain']; }); | ||
| 1551 | DefMathML("Token:?:codomain", undef, sub { return ['m:codomain']; }); | ||
| 1552 | DefMathML("Token:?:image", undef, sub { return ['m:image']; }); | ||
| 1553 | |||
| 1554 | # m:piece, m:piecewise & m:otherwise are generated as part of a cases construct | ||
| 1555 | DefMathML("Array:?:cases", undef, sub { | ||
| 1556 | my ($node) = @_; | ||
| 1557 | my @rows = (); | ||
| 1558 | my @otherwises; | ||
| 1559 | foreach my $row (element_nodes($node)) { | ||
| 1560 | my @items = element_nodes($row); | ||
| 1561 | my $n = scalar(@items); | ||
| 1562 | if ($n == 0) { } # empty row, just skip | ||
| 1563 | elsif ($n == 1) { # No condition? Perhaps it means "otherwise" ? | ||
| 1564 | push(@otherwises, $items[0]); } | ||
| 1565 | elsif ($items[1]->textContent eq 'otherwise') { # more robust test? | ||
| 1566 | push(@otherwises, $items[0]); } | ||
| 1567 | else { # Really, the 2nd cell needs to be "Looked at"; may contain "if","when" or "unless"?!?! | ||
| 1568 | push(@rows, ['m:piece', {}, cmml_contents($items[0]), cmml_contents($items[1])]); } } | ||
| 1569 | if (@otherwises) { | ||
| 1570 | if (@otherwises > 1) { | ||
| 1571 | Warn('unexpected', 'otherwise', $node, | ||
| 1572 | "Cases statement seems to have multiple otherwise clauses", | ||
| 1573 | @otherwises); } | ||
| 1574 | push(@rows, ['m:otherwise', {}, cmml_contents($otherwises[0])]); } | ||
| 1575 | return ['m:piecewise', {}, @rows]; }); | ||
| 1576 | |||
| 1577 | #====================================================================== | ||
| 1578 | # Arithmetic, Algebra and Logic: | ||
| 1579 | # quotient, factorial, divide, max, min, minus, plus, power, rem, times, root | ||
| 1580 | # gcd, and, or, xor, not, implies, forall, exists, abs, conjugate, arg, real, | ||
| 1581 | # imaginary, lcm, floor, ceiling. | ||
| 1582 | |||
| 1583 | # BRM: | ||
| 1584 | |||
| 1585 | DefMathML("Token:ADDOP:?", \&pmml_mo, undef); | ||
| 1586 | DefMathML("Token:ADDOP:plus", undef, sub { return ['m:plus']; }); | ||
| 1587 | DefMathML("Token:ADDOP:minus", undef, sub { return ['m:minus']; }); | ||
| 1588 | DefMathML('Apply:ADDOP:?', \&pmml_infix, undef); | ||
| 1589 | |||
| 1590 | DefMathML("Token:MULOP:?", \&pmml_mo, undef); | ||
| 1591 | DefMathML('Apply:MULOP:?', \&pmml_infix, undef); | ||
| 1592 | |||
| 1593 | # Unsatisfactory BINOP = ADDOP or MULOP ??? | ||
| 1594 | DefMathML("Token:BINOP:?", \&pmml_mo, undef); | ||
| 1595 | DefMathML('Apply:BINOP:?', \&pmml_infix, undef); | ||
| 1596 | |||
| 1597 | DefMathML('Apply:FRACOP:?', sub { | ||
| 1598 | my ($op, $num, $den, @more) = @_; | ||
| 1599 | my $thickness = $op->getAttribute('thickness'); | ||
| 1600 | my $color = $op->getAttribute('color') || $LaTeXML::MathML::COLOR; | ||
| 1601 | my $bevelled = grep { $_ eq 'ltx_bevelled' } split(/\s+/, $op->getAttribute('class') || ''); | ||
| 1602 | return ['m:mfrac', { (defined $thickness ? (linethickness => $thickness) : ()), | ||
| 1603 | ($color ? (mathcolor => $color) : ()), | ||
| 1604 | ($bevelled ? (bevelled => 'true') : ()) }, | ||
| 1605 | pmml_smaller($num), pmml_smaller($den)]; }); | ||
| 1606 | |||
| 1607 | DefMathML('Apply:MODIFIEROP:?', \&pmml_infix, undef); | ||
| 1608 | DefMathML("Token:MODIFIEROP:?", \&pmml_mo, undef); | ||
| 1609 | |||
| 1610 | DefMathML('Apply:MIDDLE:?', \&pmml_infix, undef); | ||
| 1611 | |||
| 1612 | DefMathML("Token:SUPOP:?", \&pmml_mo, undef); | ||
| 1613 | DefMathML('Apply:SUPERSCRIPTOP:?', \&pmml_script, undef); | ||
| 1614 | DefMathML('Apply:SUBSCRIPTOP:?', \&pmml_script, undef); | ||
| 1615 | DefMathML('Token:SUPERSCRIPTOP:?', undef, sub { | ||
| 1616 | return ['m:csymbol', { cd => 'ambiguous' }, 'superscript']; }); | ||
| 1617 | DefMathML('Token:SUBSCRIPTOP:?', undef, sub { | ||
| 1618 | return ['m:csymbol', { cd => 'ambiguous' }, 'subscript']; }); | ||
| 1619 | |||
| 1620 | DefMathML('Apply:POSTFIX:?', sub { # Reverse presentation, no @apply | ||
| 1621 | return ['m:mrow', {}, pmml($_[1]), pmml($_[0])]; }); | ||
| 1622 | DefMathML("Token:POSTFIX:?", \&pmml_mo, undef); | ||
| 1623 | |||
| 1624 | DefMathML('Apply:?:square-root', | ||
| 1625 | sub { | ||
| 1626 | my $color = $_[0]->getAttribute('color') || $LaTeXML::MathML::COLOR; | ||
| 1627 | return ['m:msqrt', { ($color ? (mathcolor => $color) : ()) }, pmml($_[1])]; }, | ||
| 1628 | sub { return ['m:apply', {}, ['m:root', {}], cmml($_[1])]; }); | ||
| 1629 | DefMathML('Apply:?:nth-root', | ||
| 1630 | sub { | ||
| 1631 | my $color = $_[0]->getAttribute('color') || $LaTeXML::MathML::COLOR; | ||
| 1632 | return ['m:mroot', { ($color ? (mathcolor => $color) : ()) }, pmml($_[2]), pmml_scriptsize($_[1])]; }, | ||
| 1633 | sub { return ['m:apply', {}, ['m:root', {}], ['m:degree', {}, cmml($_[1])], cmml($_[2])]; }); | ||
| 1634 | |||
| 1635 | # Note MML's distinction between quotient and divide: quotient yeilds an integer | ||
| 1636 | DefMathML("Token:?:quotient", undef, sub { return ['m:quotient']; }); | ||
| 1637 | DefMathML("Token:?:factorial", undef, sub { return ['m:factorial']; }); | ||
| 1638 | DefMathML("Token:?:divide", undef, sub { return ['m:divide']; }); | ||
| 1639 | DefMathML("Token:?:maximum", undef, sub { return ['m:max']; }); | ||
| 1640 | DefMathML("Token:?:minimum", undef, sub { return ['m:min']; }); | ||
| 1641 | DefMathML("Token:?:minus", undef, sub { return ['m:minus']; }); | ||
| 1642 | DefMathML("Token:?:uminus", undef, sub { return ['m:uminus']; }); | ||
| 1643 | DefMathML("Token:?:plus", undef, sub { return ['m:plus']; }); | ||
| 1644 | DefMathML("Token:?:power", undef, sub { return ['m:power']; }); | ||
| 1645 | DefMathML("Token:?:remainder", undef, sub { return ['m:rem']; }); | ||
| 1646 | DefMathML("Token:?:times", undef, sub { return ['m:times']; }); | ||
| 1647 | DefMathML("Token:?:gcd", undef, sub { return ['m:gcd']; }); | ||
| 1648 | DefMathML("Token:?:and", undef, sub { return ['m:and']; }); | ||
| 1649 | DefMathML("Token:?:or", undef, sub { return ['m:or']; }); | ||
| 1650 | DefMathML("Token:?:xor", undef, sub { return ['m:xor']; }); | ||
| 1651 | DefMathML("Token:?:not", undef, sub { return ['m:not']; }); | ||
| 1652 | DefMathML("Token:?:implies", undef, sub { return ['m:implies']; }); | ||
| 1653 | DefMathML("Token:?:forall", undef, sub { return ['m:forall']; }); | ||
| 1654 | DefMathML("Token:?:exists", undef, sub { return ['m:exists']; }); | ||
| 1655 | DefMathML("Token:?:absolute-value", undef, sub { return ['m:abs']; }); | ||
| 1656 | DefMathML("Token:?:conjugate", undef, sub { return ['m:conjugate']; }); | ||
| 1657 | DefMathML("Token:?:argument", undef, sub { return ['m:arg']; }); | ||
| 1658 | DefMathML("Token:?:real-part", undef, sub { return ['m:real']; }); | ||
| 1659 | DefMathML("Token:?:imaginary-part", undef, sub { return ['m:imaginary']; }); | ||
| 1660 | DefMathML("Token:?:lcm", undef, sub { return ['m:lcm']; }); | ||
| 1661 | DefMathML("Token:?:floor", undef, sub { return ['m:floor']; }); | ||
| 1662 | DefMathML("Token:?:ceiling", undef, sub { return ['m:ceiling']; }); | ||
| 1663 | |||
| 1664 | #====================================================================== | ||
| 1665 | # Relations: | ||
| 1666 | # eq, neq, gt, lt, geq, leq, equivalent, approx, factorof | ||
| 1667 | |||
| 1668 | DefMathML("Token:RELOP:?", \&pmml_mo); | ||
| 1669 | DefMathML("Token:?:equals", undef, sub { return ['m:eq']; }); | ||
| 1670 | DefMathML("Token:?:not-equals", undef, sub { return ['m:neq']; }); | ||
| 1671 | DefMathML("Token:?:greater-than", undef, sub { return ['m:gt']; }); | ||
| 1672 | DefMathML("Token:?:less-than", undef, sub { return ['m:lt']; }); | ||
| 1673 | DefMathML("Token:?:greater-than-or-equals", undef, sub { return ['m:geq']; }); | ||
| 1674 | DefMathML("Token:?:less-than-or-equals", undef, sub { return ['m:leq']; }); | ||
| 1675 | DefMathML("Token:?:equivalent-to", undef, sub { return ['m:equivalent']; }); | ||
| 1676 | DefMathML("Token:?:approximately-equals", undef, sub { return ['m:approx']; }); | ||
| 1677 | DefMathML("Apply:?:not-approximately-equals", undef, sub { | ||
| 1678 | my ($op, @args) = @_; | ||
| 1679 | return cmml_synth_not('m:approx', @args); }); | ||
| 1680 | DefMathML("Apply:?:less-than-or-approximately-equals", undef, sub { | ||
| 1681 | my ($op, @args) = @_; | ||
| 1682 | return cmml_or_compose(['m:lt', 'm:approx'], @args); }); | ||
| 1683 | |||
| 1684 | DefMathML("Token:?:factor-of", undef, sub { return ['m:factorof']; }); | ||
| 1685 | |||
| 1686 | DefMathML("Token:METARELOP:?", \&pmml_mo); | ||
| 1687 | DefMathML('Apply:RELOP:?', \&pmml_infix); | ||
| 1688 | DefMathML('Apply:METARELOP:?', \&pmml_infix); | ||
| 1689 | |||
| 1690 | # Top level relations | ||
| 1691 | DefMathML('Apply:?:formulae', sub { | ||
| 1692 | my ($op, @elements) = @_; | ||
| 1693 | return pmml_row(map { pmml($_) } @elements); }, | ||
| 1694 | sub { | ||
| 1695 | my ($op, @elements) = @_; | ||
| 1696 | return ['m:apply', {}, | ||
| 1697 | ['m:csymbol', { cd => 'ambiguous' }, 'formulae-sequence'], | ||
| 1698 | map { cmml($_) } @elements]; | ||
| 1699 | }); | ||
| 1700 | |||
| 1701 | DefMathML('Apply:?:multirelation', | ||
| 1702 | sub { | ||
| 1703 | my ($op, @elements) = @_; | ||
| 1704 | # This presumes that the relational operators scattered through here | ||
| 1705 | # will be recognized as such by pmml and turned into m:mo | ||
| 1706 | return pmml_row(map { pmml($_) } @elements); }, | ||
| 1707 | sub { | ||
| 1708 | my ($op, @elements) = @_; | ||
| 1709 | my $lhs = cmml(shift(@elements)); | ||
| 1710 | return $lhs unless @elements; | ||
| 1711 | my @relations = (); | ||
| 1712 | while (@elements) { | ||
| 1713 | my $rel = shift(@elements); | ||
| 1714 | my $rhs = shift(@elements); | ||
| 1715 | push(@relations, ['m:apply', {}, cmml($rel), $lhs, cmml_shared($rhs)]); | ||
| 1716 | $lhs = cmml_share($rhs); } | ||
| 1717 | return (scalar(@relations) > 1 ? ['m:apply', {}, ['m:and', {}], @relations] : $relations[0]); } | ||
| 1718 | ); | ||
| 1719 | |||
| 1720 | #====================================================================== | ||
| 1721 | # Calculus and Vector Calculus: | ||
| 1722 | # int, diff, partialdiff, lowlimit, uplimit, bvar, degree, | ||
| 1723 | # divergence, grad, curl, laplacian. | ||
| 1724 | |||
| 1725 | DefMathML("Token:INTOP:?", \&pmml_bigop); | ||
| 1726 | DefMathML("Token:LIMITOP:?", \&pmml_mo); | ||
| 1727 | DefMathML('Apply:ARROW:?', \&pmml_infix); | ||
| 1728 | |||
| 1729 | DefMathML("Token:?:integral", undef, sub { return ['m:int']; }); | ||
| 1730 | DefMathML("Token:?:differential", undef, sub { return ['m:diff']; }); | ||
| 1731 | DefMathML("Token:?:partial-differential", undef, sub { return ['m:partialdiff']; }); | ||
| 1732 | # lowlimit, uplimit, degree ? | ||
| 1733 | DefMathML("Token:?:divergence", undef, sub { return ['m:divergence']; }); | ||
| 1734 | DefMathML("Token:?:gradient", undef, sub { return ['m:grad']; }); | ||
| 1735 | DefMathML("Token:?:curl", undef, sub { return ['m:curl']; }); | ||
| 1736 | DefMathML("Token:?:laplacian", undef, sub { return ['m:laplacian']; }); | ||
| 1737 | |||
| 1738 | #====================================================================== | ||
| 1739 | # Theory of Sets, | ||
| 1740 | # set, list, union, intersect, in, notin, subset, prsubset, notsubset, notprsubset, | ||
| 1741 | # setdiff, card, cartesianproduct. | ||
| 1742 | |||
| 1743 | DefMathML("Apply:?:set", undef, sub { | ||
| 1744 | my ($op, @args) = @_; | ||
| 1745 | return ['m:set', {}, map { cmml($_) } @args]; }); | ||
| 1746 | DefMathML("Apply:?:list", undef, sub { | ||
| 1747 | my ($op, @args) = @_; | ||
| 1748 | return ['m:list', {}, map { cmml($_) } @args]; }); | ||
| 1749 | DefMathML("Token:?:union", undef, sub { return ['m:union']; }); | ||
| 1750 | DefMathML("Token:?:intersection", undef, sub { return ['m:intersect']; }); | ||
| 1751 | DefMathML("Token:?:element-of", undef, sub { return ['m:in']; }); | ||
| 1752 | DefMathML("Token:?:not-element-of", undef, sub { return ['m:notin']; }); | ||
| 1753 | DefMathML("Apply:?:contains", undef, sub { | ||
| 1754 | my ($op, @args) = @_; | ||
| 1755 | return cmml_synth_complement('m:in', @args); }); | ||
| 1756 | DefMathML("Apply:?:not-contains", undef, sub { | ||
| 1757 | my ($op, @args) = @_; | ||
| 1758 | return cmml_synth_complement('m:notin', @args); }); | ||
| 1759 | DefMathML("Token:?:subset-of", undef, sub { return ['m:subset']; }); | ||
| 1760 | DefMathML("Token:?:subset-of-or-equals", undef, sub { return ['m:subset']; }); | ||
| 1761 | DefMathML("Token:?:subset-of-and-not-equals", undef, sub { return ['m:prsubset']; }); | ||
| 1762 | DefMathML("Apply:?:superset-of", undef, sub { | ||
| 1763 | my ($op, @args) = @_; | ||
| 1764 | return cmml_synth_complement('m:subset', @args); }); | ||
| 1765 | DefMathML("Apply:?:superset-of-or-equals", undef, sub { | ||
| 1766 | my ($op, @args) = @_; | ||
| 1767 | return cmml_synth_complement('m:subset', @args); }); | ||
| 1768 | DefMathML("Apply:?:superset-of-and-not-equals", undef, sub { | ||
| 1769 | my ($op, @args) = @_; | ||
| 1770 | return cmml_synth_complement('m:prsubset', @args); }); | ||
| 1771 | DefMathML("Token:?:set-minus", undef, sub { return ['m:setdiff']; }); | ||
| 1772 | DefMathML("Token:?:cardinality", undef, sub { return ['m:card']; }); | ||
| 1773 | DefMathML("Token:?:cartesian-product", undef, sub { return ['m:cartesianproduct']; }); | ||
| 1774 | |||
| 1775 | #====================================================================== | ||
| 1776 | # Sequences and Series: | ||
| 1777 | # sum, product, limit, tendsto | ||
| 1778 | # (but see calculus for limit too!!) | ||
| 1779 | |||
| 1780 | DefMathML("Token:BIGOP:?", \&pmml_bigop); | ||
| 1781 | DefMathML("Token:SUMOP:?", \&pmml_bigop); | ||
| 1782 | |||
| 1783 | # ?? or something.... | ||
| 1784 | sub pmml_summation { | ||
| 1785 | my ($op, $body) = @_; | ||
| 1786 | return ['m:mrow', {}, pmml($op), pmml($body)]; } | ||
| 1787 | |||
| 1788 | DefMathML('Apply:BIGOP:?', \&pmml_summation); | ||
| 1789 | DefMathML('Apply:INTOP:?', \&pmml_summation); | ||
| 1790 | DefMathML('Apply:SUMOP:?', \&pmml_summation); | ||
| 1791 | |||
| 1792 | DefMathML('Apply:?:limit-from', sub { | ||
| 1793 | my ($op, $arg, $dir) = @_; | ||
| 1794 | ['m:mrow', {}, pmml($arg), pmml($dir)]; }); | ||
| 1795 | |||
| 1796 | DefMathML('Apply:?:annotated', sub { | ||
| 1797 | my ($op, $var, $annotation) = @_; | ||
| 1798 | return ['m:mrow', {}, pmml($var), | ||
| 1799 | ['m:mspace', { width => '0.3888888888888889em' }], | ||
| 1800 | pmml($annotation)]; }); | ||
| 1801 | |||
| 1802 | DefMathML("Token:?:sum", undef, sub { return ['m:sum']; }); | ||
| 1803 | DefMathML("Token:?:prod", undef, sub { return ['m:prod']; }); | ||
| 1804 | DefMathML("Token:?:limit", undef, sub { return ['m:limit']; }); | ||
| 1805 | DefMathML("Token:?:tends-to", undef, sub { return ['m:tendsto']; }); | ||
| 1806 | |||
| 1807 | #====================================================================== | ||
| 1808 | # Elementary Classical Functions, | ||
| 1809 | # exp, ln, log, sin, cos tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, | ||
| 1810 | # arcsin, arccos, arctan, arccosh, arccot, arccoth, arccsc, arccsch, arcsec, arcsech, | ||
| 1811 | # arcsinh, arctanh | ||
| 1812 | |||
| 1813 | DefMathML("Token:?:exponential", undef, sub { return ['m:exp']; }); | ||
| 1814 | DefMathML("Token:?:natural-logarithm", undef, sub { return ['m:ln']; }); | ||
| 1815 | DefMathML("Token:?:logarithm", undef, sub { return ['m:log']; }); | ||
| 1816 | DefMathML("Token:?:sine", undef, sub { return ['m:sin']; }); | ||
| 1817 | DefMathML("Token:?:cosine", undef, sub { return ['m:cos']; }); | ||
| 1818 | DefMathML("Token:?:tangent", undef, sub { return ['m:tan']; }); | ||
| 1819 | DefMathML("Token:?:secant", undef, sub { return ['m:sec']; }); | ||
| 1820 | DefMathML("Token:?:cosecant", undef, sub { return ['m:csc']; }); | ||
| 1821 | DefMathML("Token:?:cotangent", undef, sub { return ['m:cot']; }); | ||
| 1822 | DefMathML("Token:?:hyperbolic-sine", undef, sub { return ['m:sinh']; }); | ||
| 1823 | DefMathML("Token:?:hyperbolic-cosine", undef, sub { return ['m:cosh']; }); | ||
| 1824 | DefMathML("Token:?:hyperbolic-tangent", undef, sub { return ['m:tanh']; }); | ||
| 1825 | DefMathML("Token:?:hyperbolic-secant", undef, sub { return ['m:sech']; }); | ||
| 1826 | DefMathML("Token:?:hyperbolic-cosecant", undef, sub { return ['m:csch']; }); | ||
| 1827 | DefMathML("Token:?:hyperbolic-cotantent", undef, sub { return ['m:coth']; }); | ||
| 1828 | DefMathML("Token:?:inverse-sine", undef, sub { return ['m:arcsin']; }); | ||
| 1829 | DefMathML("Token:?:inverse-cosine", undef, sub { return ['m:arccos']; }); | ||
| 1830 | DefMathML("Token:?:inverse-tangent", undef, sub { return ['m:arctan']; }); | ||
| 1831 | DefMathML("Token:?:inverse-secant", undef, sub { return ['m:arcsec']; }); | ||
| 1832 | DefMathML("Token:?:inverse-cosecant", undef, sub { return ['m:arccsc']; }); | ||
| 1833 | DefMathML("Token:?:inverse-cotangent", undef, sub { return ['m:arccot']; }); | ||
| 1834 | DefMathML("Token:?:inverse-hyperbolic-sine", undef, sub { return ['m:arcsinh']; }); | ||
| 1835 | DefMathML("Token:?:inverse-hyperbolic-cosine", undef, sub { return ['m:arccosh']; }); | ||
| 1836 | DefMathML("Token:?:inverse-hyperbolic-tangent", undef, sub { return ['m:arctanh']; }); | ||
| 1837 | DefMathML("Token:?:inverse-hyperbolic-secant", undef, sub { return ['m:arcsech']; }); | ||
| 1838 | DefMathML("Token:?:inverse-hyperbolic-cosecant", undef, sub { return ['m:arccsch']; }); | ||
| 1839 | DefMathML("Token:?:inverse-hyperbolic-cotangent", undef, sub { return ['m:arccoth']; }); | ||
| 1840 | |||
| 1841 | #====================================================================== | ||
| 1842 | # Statistics: | ||
| 1843 | # mean, sdev, variance, median, mode, moment, momentabout | ||
| 1844 | |||
| 1845 | DefMathML("Token:?:mean", undef, sub { return ['m:mean']; }); | ||
| 1846 | DefMathML("Token:?:standard-deviation", undef, sub { return ['m:sdev']; }); | ||
| 1847 | DefMathML("Token:?:variance", undef, sub { return ['m:var']; }); | ||
| 1848 | DefMathML("Token:?:median", undef, sub { return ['m:median']; }); | ||
| 1849 | DefMathML("Token:?:mode", undef, sub { return ['m:mode']; }); | ||
| 1850 | DefMathML("Token:?:moment", undef, sub { return ['m:moment']; }); | ||
| 1851 | # momentabout ??? | ||
| 1852 | |||
| 1853 | #====================================================================== | ||
| 1854 | # Linear Algebra: | ||
| 1855 | # vector, matrix, matrixrow, determinant, transpose, selector, | ||
| 1856 | # vectorproduct, scalarproduct, outerproduct. | ||
| 1857 | |||
| 1858 | DefMathML("Apply:?:vector", undef, sub { | ||
| 1859 | my ($op, @args) = @_; | ||
| 1860 | return ['m:vector', {}, map { cmml($_) } @args]; }); | ||
| 1861 | #DefMathML("Token:?:matrix", undef, sub { return ['m:matrix']; }); | ||
| 1862 | DefMathML("Token:?:determinant", undef, sub { return ['m:determinant']; }); | ||
| 1863 | DefMathML("Token:?:transpose", undef, sub { return ['m:transpose']; }); | ||
| 1864 | DefMathML("Token:?:selector", undef, sub { return ['m:selector']; }); | ||
| 1865 | DefMathML("Token:?:vector-product", undef, sub { return ['m:vectorproduct']; }); | ||
| 1866 | DefMathML("Token:?:scalar-product", undef, sub { return ['m:scalarproduct']; }); | ||
| 1867 | DefMathML("Token:?:outer-product", undef, sub { return ['m:outerproduct']; }); | ||
| 1868 | |||
| 1869 | # So by default any Array is a Matrix? hmmm.... | ||
| 1870 | DefMathML("Array:?:?", undef, sub { | ||
| 1871 | my ($node) = @_; | ||
| 1872 | return ['m:matrix', {}, | ||
| 1873 | map { ['m:matrixrow', {}, map { cmml_contents($_) } element_nodes($_)] } | ||
| 1874 | element_nodes($node)]; }); | ||
| 1875 | |||
| 1876 | #====================================================================== | ||
| 1877 | # Semantic Mapping Elements | ||
| 1878 | # annotation, semantics, annotation-xml | ||
| 1879 | #====================================================================== | ||
| 1880 | # Constant and Symbol Elements | ||
| 1881 | # integers, reals, rationals, naturalnumbers, complexes, primes, | ||
| 1882 | # exponentiale, imaginaryi, notanumber, true, false, emptyset, pi, | ||
| 1883 | # eulergamma, infinity | ||
| 1884 | |||
| 1885 | DefMathML("Token:ID:integers", undef, sub { return ['m:integers']; }); | ||
| 1886 | DefMathML("Token:ID:reals", undef, sub { return ['m:reals']; }); | ||
| 1887 | DefMathML("Token:ID:rationals", undef, sub { return ['m:rationals']; }); | ||
| 1888 | DefMathML("Token:ID:numbers", undef, sub { return ['m:naturalnumbers']; }); | ||
| 1889 | DefMathML("Token:ID:complexes", undef, sub { return ['m:complexes']; }); | ||
| 1890 | DefMathML("Token:ID:primes", undef, sub { return ['m:primes']; }); | ||
| 1891 | DefMathML("Token:ID:exponential-e", undef, sub { return ['m:exponentiale']; }); | ||
| 1892 | DefMathML("Token:ID:imaginary-i", undef, sub { return ['m:imaginaryi']; }); | ||
| 1893 | DefMathML("Token:ID:notanumber", undef, sub { return ['m:notanumber']; }); | ||
| 1894 | DefMathML("Token:ID:true", undef, sub { return ['m:true']; }); | ||
| 1895 | DefMathML("Token:ID:false", undef, sub { return ['m:false']; }); | ||
| 1896 | DefMathML("Token:ID:empty-set", undef, sub { return ['m:emptyset']; }); | ||
| 1897 | DefMathML("Token:ID:circular-pi", undef, sub { return ['m:pi']; }); | ||
| 1898 | DefMathML("Token:ID:Euler-constant", undef, sub { return ['m:eulergamma']; }); | ||
| 1899 | DefMathML("Token:ID:infinity", undef, sub { return ['m:infinity']; }); | ||
| 1900 | |||
| 1901 | #====================================================================== | ||
| 1902 | # Purely presentational constructs. | ||
| 1903 | # An issue here: | ||
| 1904 | # Some constructs are pretty purely presentational. Hopefully, these would | ||
| 1905 | # only appear in XWrap's or in the presentation branch of an XMDual, so we won't | ||
| 1906 | # attempt to convert them to content. But if we do, should we warn? | ||
| 1907 | |||
| 1908 | # ================================================================================ | ||
| 1909 | # More exotic things | ||
| 1910 | |||
| 1911 | # ================================================================================ | ||
| 1912 | # cfrac! Ugh! | ||
| 1913 | |||
| 1914 | # Have to deal w/ screwy structure: | ||
| 1915 | # If denom is a sum/diff then last summand can be: cdots, cfrac | ||
| 1916 | # or invisibleTimes of cdots and something which could also be a cfrac! | ||
| 1917 | # There is some really messy manipulation of display/text style...probably not all correct. | ||
| 1918 | # This really should be handled earlier by an XMDual. | ||
| 1919 | sub do_cfrac { | ||
| 1920 | my ($numer, $denom) = @_; | ||
| 1921 | if (getQName($denom) eq 'ltx:XMApp') { # Denominator is some kind of application | ||
| 1922 | my ($denomop, @denomargs) = element_nodes($denom); | ||
| 1923 | if ((($denomop->getAttribute('role') || '') eq 'ADDOP') # Is it a sum or difference? | ||
| 1924 | || (($denomop->textContent || '') eq "\x{22EF}")) { # OR a \cdots | ||
| 1925 | my $last = pop(@denomargs); # Check last operand in denominator. | ||
| 1926 | # this is the current contribution to the cfrac (if we match the last term) | ||
| 1927 | my $curr = ['m:mfrac', {}, pmml_smaller($numer), | ||
| 1928 | ['m:mrow', {}, | ||
| 1929 | (@denomargs > 1 ? pmml_infix($denomop, @denomargs) : pmml_smaller($denomargs[0])), | ||
| 1930 | pmml_smaller($denomop)]]; | ||
| 1931 | if (($last->textContent || '') eq "\x{22EF}") { # Denom ends w/ \cdots | ||
| 1932 | return ($curr, pmml_smaller($last)); } # bring dots up to toplevel | ||
| 1933 | elsif (getQName($last) eq 'ltx:XMApp') { # Denom ends w/ application --- what kind? | ||
| 1934 | my ($lastop, @lastargs) = element_nodes($last); | ||
| 1935 | if (($lastop->getAttribute('meaning') || '') eq 'continued-fraction') { # Denom ends w/ cfrac, pull it to toplevel | ||
| 1936 | return ($curr, do_cfrac(@lastargs)); } | ||
| 1937 | elsif ((($lastop->textContent || '') eq "\x{2062}") # Denom ends w/ * (invisible) | ||
| 1938 | && (scalar(@lastargs) == 2) && (($lastargs[0]->textContent || '') eq "\x{22EF}")) { | ||
| 1939 | return ($curr, pmml_smaller($lastargs[0]), pmml_smaller($lastargs[1])); } } } } | ||
| 1940 | return ['m:mfrac', {}, pmml_smaller($numer), pmml_smaller($denom)]; } | ||
| 1941 | |||
| 1942 | DefMathML('Apply:?:continued-fraction', sub { | ||
| 1943 | my ($op, $numer, $denom) = @_; | ||
| 1944 | my $style = (($op->getAttribute('name') || '') eq 'cfrac-inline' ? 'inline' : 'display'); | ||
| 1945 | if ($style eq 'inline') { | ||
| 1946 | return pmml_row(do_cfrac($numer, $denom)); } | ||
| 1947 | else { | ||
| 1948 | return ['m:mfrac', {}, pmml_smaller($numer), pmml_smaller($denom)]; } }); | ||
| 1949 | |||
| 1950 | #================================================================================ | ||
| 1951 | # A Hack for Demo/Testing Purposes ONLY!!! | ||
| 1952 | # [Illustrates that we'd like these to be defineable in bindings!] | ||
| 1953 | DefMathML('Apply:?:hack-definite-integral', undef, | ||
| 1954 | sub { | ||
| 1955 | my ($op, $lower, $upper, $integrand, $variable) = @_; | ||
| 1956 | return ['m:apply', {}, | ||
| 1957 | ['m:int'], | ||
| 1958 | ['m:bvar', {}, cmml($variable)], | ||
| 1959 | ['m:lowlimit', {}, cmml($lower)], | ||
| 1960 | ['m:uplimit', {}, cmml($upper)], | ||
| 1961 | cmml($integrand)]; }); | ||
| 1962 | |||
| 1963 | #================================================================================ | ||
| 1964 | 1; | ||
| 1965 | |||
| 1966 | __END__ |