| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::MathML::Presentation | # | ||
| 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::Presentation; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use base qw(LaTeXML::Post::MathML); | ||
| 17 | |||
| 18 | sub preprocess { | ||
| 19 | my ($self, $doc, @maths) = @_; | ||
| 20 | $self->SUPER::preprocess($doc, @maths); | ||
| 21 | if ($$self{linelength}) { # If we're doing linebreaking... | ||
| 22 | $self->preprocess_linebreaking($doc, @maths); } | ||
| 23 | return; } | ||
| 24 | |||
| 25 | # This would be the non-linebreaking version | ||
| 26 | sub convertNode_simple { | ||
| 27 | my ($self, $doc, $xmath, $style) = @_; | ||
| 28 | return $self->pmml_top($xmath, $style); } | ||
| 29 | |||
| 30 | # Convert a node and compute it's linebroken layout | ||
| 31 | sub convertNode_linebreak { | ||
| 32 | my ($self, $doc, $xmath, $style) = @_; | ||
| 33 | my $breaker = $$self{linebreaker}; | ||
| 34 | if (!$breaker) { | ||
| 35 | require LaTeXML::Post::MathML::Linebreaker; | ||
| 36 | $breaker = $$self{linebreaker} = LaTeXML::Post::MathML::Linebreaker->new(); } | ||
| 37 | |||
| 38 | my $pmml = $self->convertNode_simple($doc, $xmath, $style); | ||
| 39 | my $layout = $breaker->bestFitToWidth($xmath, $pmml, $$self{linelength}, 1); | ||
| 40 | if ($$layout{hasbreak}) { # YES it did linebreak! | ||
| 41 | $pmml = $breaker->applyLayout($pmml, $layout); } | ||
| 42 | return ($pmml, $$layout{hasbreak}); } | ||
| 43 | |||
| 44 | sub convertNode { | ||
| 45 | my ($self, $doc, $xmath) = @_; | ||
| 46 | my $style = (($xmath->parentNode->getAttribute('mode') || 'inline') eq 'display' | ||
| 47 | ? 'display' : 'text'); | ||
| 48 | my $id = $xmath->parentNode->getAttribute('xml:id'); | ||
| 49 | # If this node has already been pre-converted | ||
| 50 | |||
| 51 | my $pmml; | ||
| 52 | if ($pmml = $id && $$doc{converted_pmml_cache}{$id}) { } | ||
| 53 | # A straight displayed Math will have been handled by preprocess_linebreaking (below), | ||
| 54 | # and, if it needed line-breaking, will have generated a MathFork/MathBranch. | ||
| 55 | # Other math, in the non-semantic side of a MathFork, may want to line break here as well. | ||
| 56 | # It presumably will NOT be display style(?) | ||
| 57 | # NEXT better strategy will be to scan columns of MathBranches to establish desired line length? | ||
| 58 | elsif ($$self{linelength} # If line breaking | ||
| 59 | && ($doc->findnodes('ancestor::ltx:MathBranch', $xmath)) # In formatted side of MathFork? | ||
| 60 | # But ONLY if last column!! (until we can adapt LineBreaker!) | ||
| 61 | && !$doc->findnodes('parent::ltx:Math/parent::ltx:td/following-sibling::ltx:td', $xmath)) { | ||
| 62 | my ($pmmlb, $broke) = $self->convertNode_linebreak($doc, $xmath, $style); | ||
| 63 | $pmml = $pmmlb; } | ||
| 64 | else { | ||
| 65 | $pmml = $self->convertNode_simple($doc, $xmath, $style); } | ||
| 66 | return { processor => $self, xml => $pmml, mimetype => 'application/mathml-presentation+xml' }; } | ||
| 67 | |||
| 68 | sub rawIDSuffix { | ||
| 69 | return '.pmml'; } | ||
| 70 | |||
| 71 | sub associateNodeHook { | ||
| 72 | my ($self, $node, $sourcenode) = @_; | ||
| 73 | # TODO: Shouldn't we have a single getQName shared for the entire latexml codebase | ||
| 74 | # in LaTeXML::Common or LaTeXML::Util ? | ||
| 75 | my $name = LaTeXML::Post::MathML::getQName($node); | ||
| 76 | if ($name =~ /^m:(?:mi|mo|mn)$/) { | ||
| 77 | if (my $href = $sourcenode->getAttribute('href')) { | ||
| 78 | if (ref $node eq 'ARRAY') { | ||
| 79 | $$node[1]{href} = $href; } | ||
| 80 | else { | ||
| 81 | $node->setAttribute('href', $href); } } | ||
| 82 | if (my $title = $sourcenode->getAttribute('title')) { | ||
| 83 | if (ref $node eq 'ARRAY') { | ||
| 84 | $$node[1]{title} = $title; } | ||
| 85 | else { | ||
| 86 | $node->setAttribute('title', $title); } } } | ||
| 87 | return; } | ||
| 88 | |||
| 89 | #================================================================================ | ||
| 90 | # Presentation MathML with Line breaking | ||
| 91 | # Not at all sure how this will integrate with Parallel markup... | ||
| 92 | |||
| 93 | # Any displayed formula is a candidate for line-breaking. | ||
| 94 | # If it is not already in a MathFork, and needs line-breaking, | ||
| 95 | # then we ought to wrap in a MathFork, so as to preserve the | ||
| 96 | # slightly "semantically meaningful" form. | ||
| 97 | # If we're mangling the document structure in this way, | ||
| 98 | # it needs to be done before the main scan-all-math's loop, | ||
| 99 | # since it moves the maths around. | ||
| 100 | # However, since we also have to check whether it NEEDS line breaking beforehand, | ||
| 101 | # we might as well linebreak & store that line-broken result alongside. | ||
| 102 | # [it will get stored WITHOUT an XMath expression, though, so we won't be asked to redo it] | ||
| 103 | # convertNode will be called later on the main fork (unbroken). | ||
| 104 | # Also, other subexpressions inside MathFork/MathBranch that were created by | ||
| 105 | # the usual means (bindings for eqnarray, or whatever) will still need to | ||
| 106 | # be converted (convertNode). | ||
| 107 | # And in fact they also should be line-broken -- we just don't know the width!! | ||
| 108 | sub preprocess_linebreaking { | ||
| 109 | my ($self, $doc, @maths) = @_; | ||
| 110 | |||
| 111 | # Rewrap every displayed ltx:Math in an ltx:MathFork (if it isn't ALREADY in a MathFork). | ||
| 112 | # This is so that we can preserve the "more semantic" non-linebroken form as the main branch. | ||
| 113 | foreach my $math (@maths) { | ||
| 114 | my $mode = $math->getAttribute('mode') || 'inline'; | ||
| 115 | next unless $mode eq 'display'; # SKIP if not in display mode? | ||
| 116 | my $style = ($mode eq 'display' ? 'display' : 'text'); | ||
| 117 | # If already has in a MathBranch, we can't really know if, or how wide, to line break!?!?! | ||
| 118 | next if $doc->findnodes('ancestor::ltx:MathFork', $math); # SKIP if already in a branch? | ||
| 119 | # Now let's do the layout & see if it actually needs line breaks! | ||
| 120 | # next if $math isn't really so wide .. | ||
| 121 | my $id = $math->getAttribute('xml:id'); | ||
| 122 | my $xmath = $doc->findnode('ltx:XMath', $math); | ||
| 123 | my ($pmml, $broke) = $self->convertNode_linebreak($doc, $xmath, $style); | ||
| 124 | if ($broke) { # YES it did linebreak! | ||
| 125 | # Replace the Math node with a MathFork that contains the Math node. | ||
| 126 | # And a MathBranch that ONLY contains the line-broken pmml. | ||
| 127 | # That branch won't get other parallel markup, | ||
| 128 | # but the main, more semantic(?) one, will and will get the unbroken pmml (?), as well. | ||
| 129 | my $p = $math->parentNode; | ||
| 130 | $id = $id . ".mbr" if $id; | ||
| 131 | $doc->replaceNode($math, ['ltx:MathFork', {}, $math, | ||
| 132 | ['ltx:MathBranch', {}, | ||
| 133 | ['ltx:Math', { 'xml:id' => $id }, | ||
| 134 | $self->outerWrapper($doc, $xmath, $pmml)]]]); | ||
| 135 | # Now,RE-mark, since the insertion removed internal attributes! | ||
| 136 | $doc->markXMNodeVisibility; } | ||
| 137 | # cache the converted pmml? | ||
| 138 | # But note that applyLayout MAY have MODIFIED the orignal $pmml, so it may have linebreaks! | ||
| 139 | # but then, it will have a modified id, as well!!! (.mbr appended) | ||
| 140 | if ($id) { | ||
| 141 | $$doc{converted_pmml_cache}{$id} = $pmml; } | ||
| 142 | } | ||
| 143 | return; } | ||
| 144 | |||
| 145 | #================================================================================ | ||
| 146 | 1; |