lib/LaTeXML/Post/UnicodeMath.pm untested

filter: Statementstmt 0.0% 0 / 243 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 101 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 586 CC 64 SCAR 43.0
CC
64
Cov
0%
unimath_internal
240.0
stylizeContent
90.0
unimath_sub
90.0
CRAP 73.8
1 # /=====================================================================\ #
2 # | LaTeXML::Post::UnicodeMath | #
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::UnicodeMath;
14 use strict;
15 use warnings;
16 use LaTeXML::Common::XML;
17 use LaTeXML::Util::Unicode;
18 use LaTeXML::Post;
19 use List::Util qw(max);
20 use base qw(LaTeXML::Post::MathProcessor);
21 use base qw(Exporter);
22 our @EXPORT = qw( &unicodemath);
23  
24 # ================================================================================
25 # LaTeXML::Post::UnicodeMath; A Math Postprocessor
26 # What we'd like to support is UnicodeMath as
27 # (1) a single pure text format for math
28 # (2) a secondary format within MathML as a m:semantic/m:annotation
29 # (3) a utility for converting math to plain text within other post processors
30 # (eg. for title attributes)
31 # Attempts to be compliant with
32 # https://www.unicode.org/notes/tn28/UTN28-PlainTextMath-v3.1.pdf
33 # ================================================================================
34  
35 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 # Top level
37 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 our $lxMimeType = 'application/x-unicodemath'; # !?!?!?!?
39  
40 sub convertNode {
41 my ($self, $doc, $xmath, $style) = @_;
42 my $math = $xmath->parentNode;
43 my $uni = $math && isElementNode($math) && unicodemath($doc, $math);
44 return { processor => $self, encoding => $lxMimeType, mimetype => $lxMimeType,
45 string => $uni }; }
46  
47 sub rawIDSuffix {
48 return '.muni'; }
49  
50 sub getQName {
51 my ($node) = @_;
52 return $LaTeXML::Post::DOCUMENT->getQName($node); }
53  
54 # Separate interface to convert single Math element to Unicode
55 sub unicodemath {
56 my ($doc, $node) = @_;
57 local $LaTeXML::Post::DOCUMENT = $doc;
58 my ($uni, $prec) = unimath_internal($node);
59 return $uni; }
60  
61 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 # General translation utilities.
63 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64  
65 sub realize {
66 my ($node, $branch) = @_;
67 return (ref $node) ? $LaTeXML::Post::DOCUMENT->realizeXMNode($node, $branch) : $node; }
68  
69 # For a node that is a (possibly embellished) operator,
70 # find the underlying role.
71 my %EMBELLISHING_ROLE = ( # CONSTANT
72 SUPERSCRIPTOP => 1, SUBSCRIPTOP => 1,
73 OVERACCENT => 1, UNDERACCENT => 1, MODIFIER => 1, MODIFIEROP => 1);
74  
75 sub getOperatorRole {
76 my ($node) = @_;
77 if (!$node) {
78 return; }
79 elsif (my $role = $node->getAttribute('role')) {
80 return $role; }
81 elsif (getQName($node) eq 'ltx:XMApp') {
82 my ($op, $base) = element_nodes($node);
83 return ($EMBELLISHING_ROLE{ $op->getAttribute('role') || '' }
84 ? getOperatorRole($base)
85 : undef); } }
86  
87 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 # Table of Translators for presentation|content
89 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 # All translators take XMath XML::LibXML nodes as arguments,
91 # and return an unicode string & precedence
92  
93 our $unimath_converters = {};
94  
95 sub DefUnicodeMath {
96 my ($key, $converter) = @_;
97 $$unimath_converters{$key} = $converter if $converter;
98 return; }
99  
100 sub lookupConverter {
101 my ($mode, $role, $name) = @_;
102 $name = '?' unless $name;
103 $role = '?' unless $role;
104 return $$unimath_converters{"$mode:$role:$name"}
105 || $$unimath_converters{"$mode:?:$name"}
106 || $$unimath_converters{"$mode:$role:?"}
107 || $$unimath_converters{"$mode:?:?"}; }
108  
109 our $PREC_RELOP = 1;
110 our $PREC_ADDOP = 2;
111 our $PREC_MULOP = 3;
112 our $PREC_SCRIPTOP = 4;
113 our $PREC_SYMBOL = 10;
114 our $PREC_UNKNOWN = 10;
115  
116 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 # Support functions
118 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 # Convert to string, return string and precedence
120 sub unimath_internal {
121 my ($node) = @_;
122 return unimath_error("Missing Subexpression") unless $node;
123 my $tag = getQName($node);
124 my $role = $node->getAttribute('role');
125 #Debug("UnicodeMath tag=$tag, role=".($role||'<unknown>'));
126 if (($tag eq 'ltx:Math') || ($tag eq 'ltx:XMath')) {
127 return unimath_map(element_nodes($node)); } # Really multiple nodes???
128 elsif ($tag eq 'ltx:XMDual') {
129 my ($content, $presentation) = element_nodes($node);
130 return unimath_internal($presentation); }
131 elsif (($tag eq 'ltx:XMWrap') || ($tag eq 'ltx:XMArg')) { # Only present if parsing failed!
132 return unimath_map(element_nodes($node)); }
133 elsif ($tag eq 'ltx:XMApp') {
134 my ($op, @args) = element_nodes($node);
135 if (!$op) {
136 return unimath_error("Missing Operator"); }
137 elsif ($role && ($role =~ /^(FLOAT|POST)(SUB|SUPER)SCRIPT$/)) {
138 # (FLOAT|POST)(SUB|SUPER)SCRIPT's should NOT remain in successfully parsed math.
139 # This conversion creates something "presentable", though doubtfully correct (empty mi?)
140 # Really should mark & make a fake parsing pass to & group open/close pairs & attach scripts
141 return ($2 eq 'SUB' ? unimath_sub(undef, undef, $op) : unimath_sup(undef, undef, $op)); }
142 else {
143 my $rop = realize($op);
144 return &{ lookupConverter('Apply', getOperatorRole($rop), $rop->getAttribute('meaning'))
145 }($op, @args); } }
146 elsif ($tag eq 'ltx:XMTok') {
147 my $m = $node->getAttribute('meaning') || 'none';
148 return ($m eq 'absent' ? '' : stylizeContent($node), $PREC_SYMBOL); }
149 elsif ($tag eq 'ltx:XMHint') {
150 ## Presumably would output some space here, except that space is default end delimiter of expr.
151 return ('', 0); }
152 elsif ($tag eq 'ltx:XMArray') {
153 my @rows = ();
154 foreach my $row (element_nodes($node)) {
155 push(@rows, join('&', map { unimath_nested($_, 0); } element_nodes($row))); }
156 return ("\x{25A0}(" . join('@', @rows) . ')', 0); }
157 elsif ($tag eq 'ltx:XMText') {
158 return unimath_text($node); }
159 elsif ($tag eq 'ltx:ERROR') {
160 return unimath_error($node); }
161 else {
162 return unimath_text($node); } }
163  
164 # Convert $node, wrapping in braces if its precedence is lower than $prec
165 # Should this recognize already-fenced? (or XMWrap, above?)
166 sub unimath_nested {
167 my ($node, $prec) = @_;
168 my ($string, $iprec) = unimath_internal($node);
169 return ($iprec >= $prec ? $string : '{' . $string . '}'); }
170  
171 # Just combine the conversion of all the @args
172 sub unimath_map {
173 # my ($op, @args) = @_;
174 my (@args) = @_;
175 my $iprec = 0;
176 my $oprec = 0;
177 if ((scalar(@args) > 1)
178 && (($args[0]->getAttribute('role') || 'none') eq 'OPEN')
179 && (($args[-1]->getAttribute('role') || 'none') eq 'CLOSE')) {
180 $oprec = $PREC_SYMBOL; }
181 return (join('', map { unimath_nested($_, $iprec); } @args), $oprec); }
182  
183 sub unimath_args {
184 my ($op, @args) = @_;
185 my $iprec = 0;
186 my $oprec = 0;
187 if ((scalar(@args) > 1)
188 && (($args[0]->getAttribute('role') || 'none') eq 'OPEN')
189 && (($args[-1]->getAttribute('role') || 'none') eq 'CLOSE')) {
190 $oprec = $PREC_SYMBOL; }
191 return (join('', map { unimath_nested($_, $iprec); } @args), $oprec); }
192  
193 our %prefix_prec = ();
194  
195 sub unimath_prefix {
196 my ($op, @args) = @_;
197 $op = realize($op) if ref $op;
198 return ("", $PREC_SYMBOL) unless $op && @args;
199 my $role = (ref $op ? getOperatorRole($op) : 'none') || 'none';
200 my $prec = (defined $prefix_prec{$role} ? $prefix_prec{$role} : $PREC_SYMBOL);
201 return (join('', unimath_nested($op, 0),
202 map { unimath_nested($_, $prec); } @args), $prec); }
203  
204 # args are XMath nodes
205 # This is suitable for use as an Apply handler.
206 our %infix_prec = (
207 ADDOP => $PREC_ADDOP, BINOP => $PREC_ADDOP,
208 MULOP => $PREC_MULOP, MIDDLE => $PREC_MULOP,
209 COMPOSEOP => $PREC_MULOP, MODIFIEROP => $PREC_MULOP,
210 RELOP => $PREC_RELOP, METARELOP => $PREC_RELOP, ARROW => $PREC_RELOP,
211 );
212  
213 sub unimath_infix {
214 my ($op, @args) = @_;
215 $op = realize($op) if ref $op;
216 return ("", $PREC_SYMBOL) unless $op && @args;
217 my $role = (ref $op ? getOperatorRole($op) : 'none') || 'none';
218 my $prec = (defined $infix_prec{$role} ? $infix_prec{$role} : $PREC_SYMBOL);
219 my $opuni = unimath_nested($op, $prec);
220 my @items = (unimath_nested(shift(@args), $prec));
221 if (scalar(@args) == 0) { # Infix with 1 arg is presumably Prefix!
222 unshift(@items, $opuni); }
223 else {
224 while (@args) {
225 push(@items, $opuni, unimath_nested(shift(@args), $prec)); } }
226 return (join('', @items), $prec); }
227  
228 my %default_token_content = (
229 MULOP => "\x{2062}", ADDOP => "\x{2064}", PUNCT => "\x{2063}");
230  
231 # Given an item (string or token element w/attributes) and latexml attributes,
232 # convert the string to the appropriate unicode (possibly plane1)
233 # & MathML presentation attributes (mathvariant, mathsize, mathcolor, stretchy)
234 sub stylizeContent {
235 my ($item, %attr) = @_;
236 my $iselement = (ref $item) eq 'XML::LibXML::Element';
237 my $role = ($iselement ? $item->getAttribute('role') : 'ID');
238 my $font = ($iselement ? $item->getAttribute('font') : $attr{font})
239 || $LaTeXML::MathML::FONT;
240 my $text = (ref $item ? $item->textContent : $item);
241 my $variant = ($font ? unicode_mathvariant($font) : '');
242 if ((!defined $text) || ($text eq '')) { # Failsafe for empty tokens?
243 if (my $default = $role && $default_token_content{$role}) {
244 $text = $default; }
245 else {
246 $text = ($iselement ? $item->getAttribute('name') || $item->getAttribute('meaning') || $role : '?');
247 } }
248 my $u_text = $variant && unicode_convert($text, $variant);
249 if ((defined $u_text) && ($u_text ne '')) { # didn't remap the text ? Keep text & variant
250 $text = $u_text; }
251 return $text; }
252  
253 # Some of these equivalences may not be correct,
254 # in particular, ignoring the "semantics" associated with IPA or Phonetic symbols
255 # Here's an interesting question:
256 # How much do we prefer sub/superscipt glyphs over ^{...}, or _{...} ?
257 # We COULD unmap plane 1 math chars down to plain letters, and then remap them to sub/superscript.
258 # (maybe only math italic?)
259 sub unimath_sub {
260 my ($op, $base, $script) = @_;
261 my $pos = (ref $op && $op->getAttribute('scriptpos')) || 'post';
262 my $pre = $pos =~ /^pre/;
263 my $mid = $pos =~ /^mid/;
264 my $mapped = 0;
265 my $ubase = ($base ? unimath_nested($base, $PREC_SCRIPTOP) : '');
266 my ($uscript, $prec) = unimath_internal($script);
267 # Possibly convert $uscript to subscript codepoints ??
268 if ($mid) { } # Could try for combining accents?
269 else {
270 my $u_uscript = unicode_convert($uscript, 'subscript');
271 if (defined $u_uscript) {
272 $uscript = $u_uscript; $mapped = 1; } }
273 $uscript = '{' . $uscript . '}' if !$mapped && $prec < $PREC_SCRIPTOP; # Wrap if needed
274 return (($pre ? ($mapped ? $uscript : '{_' . $uscript . '}') . $ubase
275 : $ubase . ($mapped ? '' : ($mid ? "\x{252C}" : '_')) . $uscript), $PREC_SCRIPTOP); }
276  
277 sub unimath_sup {
278 my ($op, $base, $script) = @_;
279 my $pos = (ref $op && $op->getAttribute('scriptpos')) || 'post';
280 my $pre = $pos =~ /^pre/;
281 my $mid = $pos =~ /^mid/;
282 my $mapped = 0;
283 my $ubase = ($base ? unimath_nested($base, $PREC_SCRIPTOP) : '');
284 my ($uscript, $prec) = unimath_internal($script);
285 # Possibly convert $uscript to superscript codepoints ??
286 if ($mid) { } # Could try for combining accents?
287 else {
288 my $u_uscript = unicode_convert($uscript, 'superscript');
289 if (defined $u_uscript) {
290 $uscript = $u_uscript; $mapped = 1; } }
291 $uscript = '{' . $uscript . '}' if !$mapped && $prec < $PREC_SCRIPTOP; # Wrap if needed
292 return (($pre ? ($mapped ? $uscript : '{^' . $uscript . '}') . $ubase
293 : $ubase . ($mapped ? '' : ($mid ? "\x{2534}" : '^')) . $uscript), $PREC_SCRIPTOP); }
294  
295 # The combining chars assciated with the accent chars over/under
296 # Perhaps these tables should be moved to LaTeXML::Util::Unicode? (later...)
297 our %overaccents = ('^' => "\x{0302}", # \hat,
298 UTF(0x5E) => "\x{0302}", # \widehat
299 "\x{02C7}" => "\x{030C}", # \check
300 '~' => "\x{303}",
301 UTF(0x7E) => "\x{0303}", # \tilde, \widetilde
302 UTF(0x84) => "\x{0301}", # \acute
303 UTF(0x60) => "\x{0300}", # \grave
304 "\x{02D9}" => "\x{0307}", # \dot
305 UTF(0xAB) => "\x{0308}", # \ddot
306 UTF(0xAF) => "\x{0304}", # \bar, \overline
307 "\x{2192}" => "\x{20D7}", # \vec
308 "\x{02D8}" => "\x{0306}", # \breve
309 "o" => "\x{030A}", # \r
310 "\x{02DD}" => "\x{030B}", # \H
311 );
312 our %underaccents = (
313 UTF(0xB8) => "\x{0327}", # \c
314 '.' => "\x{0323}", # dot below
315 UTF(0xAF) => "\x{0331}", # macron below
316 "=" => "\x{0361}", # \t
317 "," => "\x{0361}", # lfhook
318 );
319 # \overbrace, \underbrace, \overleftarrow ??
320 sub unimath_overaccent {
321 my ($op, $base) = @_;
322 my $acc = $op->textContent;
323 my $cacc = $acc && $overaccents{$acc};
324 my ($ubase, $uprec) = unimath_internal($base);
325 $ubase = '(' . $ubase . ')' if length($ubase) > 1;
326 return ($ubase . ($cacc ? $cacc : "\x{252C}" . $acc), $PREC_SCRIPTOP); }
327  
328 sub unimath_underaccent {
329 my ($op, $base) = @_;
330 my $acc = $op->textContent;
331 my $cacc = $acc && $underaccents{$acc};
332 my ($ubase, $uprec) = unimath_internal($base);
333 $ubase = '(' . $ubase . ')' if length($ubase) > 1; # maybe NBSP, too???
334 return ($ubase . ($cacc ? $cacc : "\x{252C}" . $acc), $PREC_SCRIPTOP); }
335  
336 # Handle text contents.
337 # We probably should pass this back to the same code used in CrossRef.
338 # But also, there's the question of what to do with nested math?
339 # Is that even allowable in UnicodeMath?
340 sub unimath_text {
341 my (@nodes) = @_;
342 return ('"' . join('', map { (ref $_ ? $_->textContent : $_); } @nodes) . '"', $PREC_SYMBOL); }
343  
344 sub unimath_error {
345 return unimath_text('ERROR ', @_); }
346  
347 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 # Tranlators
349 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350  
351 DefUnicodeMath('Apply:?:?', \&unimath_prefix);
352 DefUnicodeMath('Apply:ADDOP:?', \&unimath_infix);
353 DefUnicodeMath('Apply:MULOP:?', \&unimath_infix);
354 DefUnicodeMath('Apply:BINOP:?', \&unimath_infix);
355 DefUnicodeMath('Apply:RELOP:?', \&unimath_infix);
356 DefUnicodeMath('Apply:METARELOP:?', \&unimath_infix);
357 DefUnicodeMath('Apply:ARROW:?', \&unimath_infix);
358 DefUnicodeMath('Apply:COMPOSEOP:?', \&unimath_infix);
359 DefUnicodeMath("Apply:DIFFOP:?", \&unimath_prefix);
360 DefUnicodeMath('Apply:BIGOP:?', \&unimath_prefix);
361 DefUnicodeMath('Apply:INTOP:?', \&unimath_prefix);
362 DefUnicodeMath('Apply:SUMOP:?', \&unimath_prefix);
363 DefUnicodeMath('Apply:?:formulae', \&unimath_map);
364 DefUnicodeMath('Apply:?:multirelation', \&unimath_args);
365 DefUnicodeMath('Apply:?:limit-from', \&unimath_prefix);
366 DefUnicodeMath('Apply:?:annotated', \&unimath_prefix);
367  
368 DefUnicodeMath('Apply:FRACOP:?', sub {
369 my ($op, $num, $den, @more) = @_;
370 my $thickness = $op->getAttribute('thickness');
371 if (defined $thickness) { # Hmm? maybe not even a fraction?
372 return ('(' . unimath_nested($num, 0) . UTF(0xA6) . unimath_nested($den, 0) . ')', $PREC_SYMBOL); }
373 else {
374 return (unimath_nested($num, $PREC_MULOP) . '/' . unimath_nested($den, $PREC_MULOP), 1); } });
375  
376 DefUnicodeMath('Apply:MODIFIEROP:?', \&unimath_infix);
377 DefUnicodeMath('Apply:MIDDLE:?', \&unimath_infix);
378 DefUnicodeMath('Apply:SUPERSCRIPTOP:?', \&unimath_sup);
379 DefUnicodeMath('Apply:SUBSCRIPTOP:?', \&unimath_sub);
380 # These could search for candidate combining chars?
381 DefUnicodeMath('Apply:OVERACCENT:?', \&unimath_overaccent);
382 DefUnicodeMath('Apply:UNDERACCENT:?', \&unimath_underaccent);
383  
384 DefUnicodeMath('Apply:POSTFIX:?', sub { # Reverse presentation, no @apply
385 my ($op) = unimath_internal($_[0]);
386 return (unimath_nested($_[1], $PREC_MULOP) . $op, $PREC_MULOP); });
387  
388 DefUnicodeMath('Apply:?:square-root', sub {
389 return ("\x{221A}" . unimath_nested($_[1], $PREC_MULOP), $PREC_MULOP); });
390 DefUnicodeMath('Apply:?:nth-root', sub {
391 # Could convert to \x{221B} for cube, \x{221C} for quartic
392 my ($n) = unimath_internal($_[2]);
393 my $op = ($n eq '2' ? "\x{221A}"
394 : ($n eq '3' ? "\x{221B}"
395 : ($n eq '4' ? "\x{221C}"
396 : "\\root " . $n . "\\of")));
397 return ($op . unimath_nested($_[1], $PREC_MULOP), $PREC_MULOP); });
398 DefUnicodeMath('Apply:ENCLOSE:?', sub {
399 my ($op, $base) = @_;
400 return (unimath_nested($base, $PREC_SYMBOL), $PREC_SYMBOL); });
401  
402 # ================================================================================
403 # cfrac! Ugh!
404 DefUnicodeMath('Apply:?:continued-fraction', sub {
405 return unimath_error("continued fraction"); });
406  
407 #================================================================================
408 1;
409  
410 __END__