| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::PictureImages | # | ||
| 3 | # | Postprocessor to create images for picture | # | ||
| 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::PictureImages; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use LaTeXML::Post; | ||
| 17 | use base qw(LaTeXML::Post::LaTeXImages); | ||
| 18 | |||
| 19 | # Options: | ||
| 20 | # resource_directory | ||
| 21 | # resource_prefix | ||
| 22 | # use_dvipng | ||
| 23 | # empty_only : process only ltx:picture w/o children (but with tex attribute) | ||
| 24 | sub new { | ||
| 25 | my ($class, %options) = @_; | ||
| 26 | $options{resource_directory} = 'pic' unless defined $options{resource_directory}; | ||
| 27 | $options{resource_prefix} = 'pic' unless defined $options{resource_prefix}; | ||
| 28 | $options{use_dvipng} = 0 unless defined $options{use_dvipng}; | ||
| 29 | return $class->SUPER::new(%options); } | ||
| 30 | |||
| 31 | #====================================================================== | ||
| 32 | |||
| 33 | sub find_documentclass_and_packages { | ||
| 34 | my ($self, $doc) = @_; | ||
| 35 | my ($classdata, @packages) = $self->SUPER::find_documentclass_and_packages($doc); | ||
| 36 | # If we haven't already included graphics or graphicx | ||
| 37 | if (!grep { /graphic(:?s|x)/ } @packages) { | ||
| 38 | # and if a picture has @scale | ||
| 39 | if ($doc->findnodes('//ltx:picture[@scale]')) { | ||
| 40 | # Then load graphicx as well | ||
| 41 | push(@packages, ['graphicx', '']); } } | ||
| 42 | return ($classdata, @packages); } | ||
| 43 | |||
| 44 | # Return the list of Picture nodes. | ||
| 45 | sub toProcess { | ||
| 46 | my ($self, $doc) = @_; | ||
| 47 | my @nodes = $doc->findnodes('//ltx:picture'); | ||
| 48 | if ($$self{empty_only}) { | ||
| 49 | @nodes = grep { !$_->hasChildNodes; } @nodes; } | ||
| 50 | return @nodes; } | ||
| 51 | |||
| 52 | # Return the TeX string to format the image for this node. | ||
| 53 | sub extractTeX { | ||
| 54 | my ($self, $doc, $node) = @_; | ||
| 55 | my $tex = $self->cleanTeX($node->getAttribute('tex') || ''); | ||
| 56 | $tex =~ s/\n//gs; # trim stray CR's | ||
| 57 | if (my $u = $node->getAttribute('unitlength')) { | ||
| 58 | $tex = "\\setlength{\\unitlength}{$u}" . $tex; } | ||
| 59 | # xunitlength, yunitlength for pstricks??? | ||
| 60 | if (my $s = $node->getAttribute('scale')) { | ||
| 61 | $tex = "\\scalebox{$s}{$tex}"; } | ||
| 62 | return "\\beginPICTURE $tex\\endPICTURE"; } | ||
| 63 | |||
| 64 | sub process { | ||
| 65 | my ($self, $doc, @nodes) = @_; | ||
| 66 | return $self->generateImages($doc, @nodes); } | ||
| 67 | |||
| 68 | sub setTeXImage { | ||
| 69 | my ($self, $doc, $node, $path, $width, $height, $depth) = @_; | ||
| 70 | $self->SUPER::setTeXImage($doc, $node, $path, $width, $height, $depth); | ||
| 71 | # Since the width & height attributes can get exposed in the XSLT (CSS) | ||
| 72 | # adjust them to match the size actually computed | ||
| 73 | # $node->setAttribute(width => $width . "pt"); | ||
| 74 | # $node->setAttribute(height => $height . "pt"); | ||
| 75 | # But, since the inner image (of whatever format) will already get a size, | ||
| 76 | # it's probably safer to just remove those attributes? | ||
| 77 | # $node->removeAttribute('width'); | ||
| 78 | # $node->removeAttribute('height'); | ||
| 79 | return; } | ||
| 80 | |||
| 81 | # Definitions needed for processing inline & display picture images | ||
| 82 | sub preamble { | ||
| 83 | my ($self, $doc) = @_; | ||
| 84 | return <<'EOPreamble'; | ||
| 85 | \def\scalePicture#1{\setlength{\unitlength}{#1 \unitlength} | ||
| 86 | \@tempdimb\f@size\p@ \@tempdimb#1\@tempdimb | ||
| 87 | \fontsize{\strip@pt\@tempdimb}{\strip@pt\@tempdimb}\selectfont} | ||
| 88 | \def\beginPICTURE{\lxBeginImage} | ||
| 89 | \def\endPICTURE{\lxEndImage\lxShowImage} | ||
| 90 | EOPreamble | ||
| 91 | } | ||
| 92 | #====================================================================== | ||
| 93 | 1; |