lib/LaTeXML/Post/MathML/Presentation.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 113 CC 13 SCAR 31.9
CC
13
Cov
0%
preprocess_linebreaking
56.0
convertNode
20.0
convertNode_linebreak
12.0
CRAP 24.2
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 #================================================================================
72 # Presentation MathML with Line breaking
73 # Not at all sure how this will integrate with Parallel markup...
74  
75 # Any displayed formula is a candidate for line-breaking.
76 # If it is not already in a MathFork, and needs line-breaking,
77 # then we ought to wrap in a MathFork, so as to preserve the
78 # slightly "semantically meaningful" form.
79 # If we're mangling the document structure in this way,
80 # it needs to be done before the main scan-all-math's loop,
81 # since it moves the maths around.
82 # However, since we also have to check whether it NEEDS line breaking beforehand,
83 # we might as well linebreak & store that line-broken result alongside.
84 # [it will get stored WITHOUT an XMath expression, though, so we won't be asked to redo it]
85 # convertNode will be called later on the main fork (unbroken).
86 # Also, other subexpressions inside MathFork/MathBranch that were created by
87 # the usual means (bindings for eqnarray, or whatever) will still need to
88 # be converted (convertNode).
89 # And in fact they also should be line-broken -- we just don't know the width!!
90 sub preprocess_linebreaking {
91 my ($self, $doc, @maths) = @_;
92  
93 # Rewrap every displayed ltx:Math in an ltx:MathFork (if it isn't ALREADY in a MathFork).
94 # This is so that we can preserve the "more semantic" non-linebroken form as the main branch.
95 foreach my $math (@maths) {
96 my $mode = $math->getAttribute('mode') || 'inline';
97 next unless $mode eq 'display'; # SKIP if not in display mode?
98 my $style = ($mode eq 'display' ? 'display' : 'text');
99 # If already has in a MathBranch, we can't really know if, or how wide, to line break!?!?!
100 next if $doc->findnodes('ancestor::ltx:MathFork', $math); # SKIP if already in a branch?
101 # Now let's do the layout & see if it actually needs line breaks!
102 # next if $math isn't really so wide ..
103 my $id = $math->getAttribute('xml:id');
104 my $xmath = $doc->findnode('ltx:XMath', $math);
105 my ($pmml, $broke) = $self->convertNode_linebreak($doc, $xmath, $style);
106 if ($broke) { # YES it did linebreak!
107 # Replace the Math node with a MathFork that contains the Math node.
108 # And a MathBranch that ONLY contains the line-broken pmml.
109 # That branch won't get other parallel markup,
110 # but the main, more semantic(?) one, will and will get the unbroken pmml (?), as well.
111 my $p = $math->parentNode;
112 $id = $id . ".mbr" if $id;
113 $doc->replaceNode($math, ['ltx:MathFork', {}, $math,
114 ['ltx:MathBranch', {},
115 ['ltx:Math', { 'xml:id' => $id },
116 $self->outerWrapper($doc, $xmath, $pmml)]]]);
117 # Now,RE-mark, since the insertion removed internal attributes!
118 $doc->markXMNodeVisibility; }
119 # cache the converted pmml?
120 # But note that applyLayout MAY have MODIFIED the orignal $pmml, so it may have linebreaks!
121 # but then, it will have a modified id, as well!!! (.mbr appended)
122 if ($id) {
123 $$doc{converted_pmml_cache}{$id} = $pmml; }
124 }
125 return; }
126  
127 #================================================================================
128 1;