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