| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::MathImages | # | ||
| 3 | # | Postprocessor to create images for math | # | ||
| 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::MathImages; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use LaTeXML::Post; | ||
| 17 | use LaTeXML::Util::Pathname; | ||
| 18 | use base qw(LaTeXML::Post::MathProcessor LaTeXML::Post::LaTeXImages); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ($class, %options) = @_; | ||
| 22 | $options{resource_directory} = 'mi' unless defined $options{resource_directory}; | ||
| 23 | $options{resource_prefix} = 'mi' unless defined $options{resource_prefix}; | ||
| 24 | # return $class->SUPER::new(%options); } | ||
| 25 | # Dangerous, since multiple inheritance! | ||
| 26 | return $class->LaTeXML::Post::LaTeXImages::new(%options); } | ||
| 27 | |||
| 28 | #====================================================================== | ||
| 29 | |||
| 30 | # Return the list of Math nodes. | ||
| 31 | sub toProcess { | ||
| 32 | my ($self, $doc) = @_; | ||
| 33 | return $doc->findnodes('//ltx:Math'); } | ||
| 34 | |||
| 35 | # Return the TeX string to format the image for this node. | ||
| 36 | sub extractTeX { | ||
| 37 | my ($self, $doc, $node) = @_; | ||
| 38 | my $mode = uc($node->getAttribute('mode') || 'INLINE'); | ||
| 39 | my $tex = $self->cleanTeX($node->getAttribute('tex')); | ||
| 40 | return unless defined $tex; | ||
| 41 | $mode = 'DISPLAY' if $tex =~ s/^\s*\\displaystyle\s+//; # Strip leading displaystyle | ||
| 42 | return ($tex =~ /^\s*$/ ? undef : "\\begin$mode $tex\\end$mode"); } | ||
| 43 | |||
| 44 | my $MML_NAMESPACE = "http://www.w3.org/1998/Math/MathML"; # [CONSTANT] | ||
| 45 | |||
| 46 | # Don't set the image attributes, will be handled by math postprocessing. | ||
| 47 | sub setTeXImage { | ||
| 48 | return; } | ||
| 49 | |||
| 50 | our %MIMETYPES = (gif => 'image/gif', jpeg => 'image/jpeg', png => 'image/png', svg => 'image/svg+xml'); | ||
| 51 | |||
| 52 | sub preprocess { | ||
| 53 | my ($self, $doc, @nodes) = @_; | ||
| 54 | return $self->generateImages($doc, @nodes); } | ||
| 55 | |||
| 56 | # RIDICULOUS amount of work to look up the image that was previously generated!!! | ||
| 57 | sub convertNode { | ||
| 58 | my ($self, $doc, $xmath, $style) = @_; | ||
| 59 | my $math = $xmath->parentNode; | ||
| 60 | my $tex = $self->extractTeX($doc, $math); | ||
| 61 | my $type = $$self{imagetype}; | ||
| 62 | # next if !(defined $tex) || ($tex =~ /^\s*$/); | ||
| 63 | my $key = (ref $self) . ':' . $type . ':' . $tex; | ||
| 64 | if (($doc->cacheLookup($key) || '') =~ /^(.*);(\d+);(\d+);(\d+)$/) { | ||
| 65 | my ($image, $width, $height, $depth) = ($1, $2, $3, $4); | ||
| 66 | # Ideally, $image is already relative, but if not, make relative to document | ||
| 67 | my $reldest = pathname_relative($image, $doc->getDestinationDirectory); | ||
| 68 | return { processor => $self, mimetype => $MIMETYPES{$type}, | ||
| 69 | src => $reldest, width => $width, height => $height, depth => $depth }; } | ||
| 70 | else { | ||
| 71 | print STDERR "Couldn't find image for '$key'\n"; | ||
| 72 | return {}; } } | ||
| 73 | |||
| 74 | # Definitions needed for processing inline & display math images | ||
| 75 | sub preamble { | ||
| 76 | my ($self, $doc) = @_; | ||
| 77 | return <<'EOPreamble'; | ||
| 78 | \def\beginINLINE{\lxBeginImage\(} | ||
| 79 | \def\endINLINE{\)\lxEndImage\lxShowImage} | ||
| 80 | % For Display, same as inline, but set displaystyle. | ||
| 81 | \def\beginDISPLAY{\lxBeginImage\(\displaystyle\the\everydisplay} | ||
| 82 | \def\endDISPLAY{\)\lxEndImage\lxShowImage} | ||
| 83 | EOPreamble | ||
| 84 | } | ||
| 85 | #====================================================================== | ||
| 86 | 1; |