LaTeXML
view release on metacpan or search on metacpan
lib/LaTeXML/Post/LaTeXImages.pm view on Meta::CPAN
if (($$self{imagetype} eq 'svg') && (!defined $options{use_dvisvgm}) && which('dvisvgm')) {
$$self{use_dvisvgm} = 1; }
elsif ($$self{use_dvisvgm} && (($$self{imagetype} ne 'svg') || !which('dvisvgm'))) {
$$self{use_dvisvgm} = 0; } # but disable if inappropriate or unavailable
# We'll use dvipng (MUCH faster) if requested or not forbidden & available.
# But be careful: it can't handle much postscript, so better NOT for graphics!
if (($$self{imagetype} eq 'png') && (!defined $options{use_dvipng}) && which('dvipng')) {
$$self{use_dvipng} = 1; }
elsif ($$self{use_dvipng} && (($$self{imagetype} ne 'png') || !which('dvipng'))) {
$$self{use_dvipng} = 0; } # but disable if inappropriate or unavailable.
# Parameterize according to the selected dvi-to-whatever processor.
my $mag = int($$self{magnification} * 1000);
my $dpi = int($$self{DPI} * $$self{magnification});
# Unfortunately, each command has incompatible -o option to name the output file.
# Note that the formatting char used, '%', has to be doubled on Windows!!
# Maybe that's only true when it looks like an environment variable?
# Ie. %variable% ?
# my $fmt = ($^O eq 'MSWin32' ? '%%' : '%');
my $fmt = '%';
if ($$self{use_dvisvgm}) {
## $$self{dvicmd} = "dvisvgm --page=1- --bbox=min --mag=$mag -o imgx-${fmt}p";
# dvisvgm currently creates glyph descriptions w/ unicode attribute having the wrong codepoint
# firefox, chromium use this codepoint instead of the glyph "drawing"
# a later version of dvisvgm should do better at synthesizing the unicode?
# but for now, we'll use --no-fonts, which creates glyph drawings rather than "glyphs"
# Also, increase the bounding box from min by 1pt
$$self{dvicmd} = "dvisvgm --page=1- --bbox=1pt --scale=$$self{magnification} --no-fonts -o imgx-${fmt}3p";
$$self{dvicmd_output_name} = 'imgx-%03d.svg';
$$self{dvicmd_output_type} = 'svg';
$$self{frame_output} = 0; }
elsif ($$self{use_dvipng}) {
$$self{dvicmd} = "dvipng -bg Transparent -T tight -q -D$dpi -o imgx-${fmt}03d.png";
$$self{dvicmd_output_name} = 'imgx-%03d.png';
$$self{dvicmd_output_type} = 'png32';
$$self{frame_output} = 1; }
else {
# Useful DVIPS options:
# -q : run quietly
# -x# : magnification * 1000
# -S1 -i : make a separate file for each `section' consisting of a single page.
# QUESTION: dvips' naming scheme allows for 999 pages... what happens at 1000?
# -E : crop each page close to the `ink'.
# -j0 : don't subset fonts; silly really, but some font tests are making problems!
$$self{dvicmd} = "dvips -q -S1 -i -E -j0 -x$mag -o imgx";
$$self{dvicmd_output_name} = 'imgx%03d';
$$self{dvicmd_output_type} = 'eps';
$$self{frame_output} = 1; }
return $self; }
#**********************************************************************
# Check whether this processor actually has the resources it needs to process.
# I don't know if there's a general API implied here;
# It could, conceivably be use in planning post processing?
# But generally, before signalling an error, you'd want to know that the processor
# is even needed.
# This test is called once we know that, from within
#
# At any rate: To process LaTeX snippets into images, we will need
# * latex (or related) from a TeX installation
# * Image::Magick (or similar) [see LaTeXML::Util::Image]
sub canProcess {
my ($self) = @_;
# Check if we have Image::Magick (or similar)
if (!image_can_image()) {
Error('expected', 'Image::Magick', undef,
"No available image processing module found; Skipping.",
"Please install one of: " . join(',', image_classes()));
return; }
# AND check if we have an approriprate latex!!!
if (($LATEXCMD =~ /^(\S+)/) && !which($1)) { # does the command seem to be available?
Error('expected', $LATEXCMD, undef,
"No latex command ($LATEXCMD) found; Skipping.",
"Please install TeX to generate images from LaTeX");
return; }
return 1; }
#**********************************************************************
# Methods that must be defined;
# This is an abstract class; concrete classes must select the nodes to process.
# We'll still need to use $proc->extractTeX($doc,$node)
# to extract the actual TeX string!
sub toProcess {
return (); }
# $self->extractTeX($doc,$node)=>$texstring;
sub extractTeX {
return ""; }
# $self->format_tex($texstring)
sub format_tex {
return ""; }
# $self->setTeXImage($doc,$node,$imagepath,$width,$height);
# This is the default
sub setTeXImage {
my ($self, $doc, $node, $path, $width, $height, $depth) = @_;
$node->setAttribute('imagesrc', pathname_to_url($path));
$node->setAttribute('imagewidth', $width);
$node->setAttribute('imageheight', $height);
$node->setAttribute('imagedepth', $depth) if defined $depth;
return; }
#======================================================================
# Methods that could be wrapped, overridden.
# Should be called by extractTeX
sub cleanTeX {
my ($self, $tex) = @_;
return unless defined $tex;
my $style = '';
# Save any leading style
if ($tex =~ s/^\s*(\\displaystyle|\\textstyle|\\scriptstyle|\\scriptscriptstyle)\s*//) {
$style = $1; }
$tex =~ s/^(?:\\\s*,|\\!\s*|\\>\s*|\\;\s*|\\:\s*|\\ \s*|\\\/\s*)*//; # Trim leading spacing (especially negative!)
$tex =~ s/(?:\\\s*,|\\!\s*|\\>\s*|\\;\s*|\\:\s*|\\ \s*|\\\/\s*)*$//; # and trailing spacing
# Strip comments, but watchout for \% (or more exactly, an ODD number of \)
$tex =~ s/(?<!\\)((?:\\\\)*)\%[^\n]*\n/$1/gs;
$tex = $style . ' ' . $tex if $style; # Put back the style, if any
return $tex; }
#**********************************************************************
#**********************************************************************
# Generating & Processing the LaTeX source.
( run in 1.856 second using v1.01-cache-2.11-cpan-39bf76dae61 )