[system] / trunk / xmlrpc / lib / ImageGenerator.pm Repository:
ViewVC logotype

View of /trunk/xmlrpc/lib/ImageGenerator.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 594 - (download) (as text) (annotate)
Fri Oct 18 13:50:48 2002 UTC (10 years, 7 months ago) by gage
File size: 5564 byte(s)
Added two new files

    1 #!/usr/local/bin/perl
    2 
    3 ################################################################################
    4 # WeBWorK
    5 #
    6 # Copyright (c) 1995-2002 University of Rochester
    7 # All rights reserved
    8 #
    9 ################################################################################
   10 
   11 use lib '.'; use webworkInit; # WeBWorKInitLine
   12 use WeBWorK::PG::IO;
   13 use strict;
   14 
   15 =head1 NAME
   16 
   17   ImageGenerator.pm
   18 
   19 =head1 SYNPOSIS
   20 
   21 Create an object for holding bits of math for LaTeX, and then to process
   22 them later.
   23 
   24     my $imgen = new ImageGenerator;      #create a image generator
   25     $imgen->initialize(\%envir); # provide basic data in preparation of image collection
   26     $imgen->add(string);  # add a new LaTeX string to be processed.
   27                           # Should be in math mode with \( or \[
   28                           # It returns the html tag
   29 
   30     $imgen->getCount();   # Returns the number of images which have been added
   31     $imgen->tmpurl();     # Returns the beginning of the html path
   32     $imgen->render();     # Generates the images.  By default, we reuse old
   33                           # images when reasonable, unless passed a flag
   34                           # refresh=>'yes' (or 1)
   35 
   36 =head1 DESCRIPTION
   37 
   38 This module defines an object which holds bits of LaTeX math for
   39 generating images.
   40 
   41 =cut
   42 
   43 
   44 package ImageGenerator;
   45 
   46 
   47 =head2  new
   48   Creates the ImageGenerator object.
   49 
   50 =cut
   51 
   52 
   53 sub new {
   54     my $class = shift;
   55     my $self = {
   56                 latexlines => [],
   57                 count => 0,
   58                 tmppath => "",
   59                 tmpURLstart=>"",
   60                 filenamestart=> ""
   61     };
   62 
   63     bless $self, $class;
   64 }
   65 
   66 
   67 sub initialize {
   68     my $self = shift;
   69     my $envir = shift; # pointer to problem envirment hash
   70     my $problemnum = $envir->{'probNum'};
   71     my $studname = $envir->{'studentLogin'};
   72     my $psvn = $envir->{'psvn'};
   73     my $setname = $envir->{'setNumber'};
   74     my $tmpURLstart = $envir->{'tempURL'};
   75     my $path=WeBWorK::PG::IO::surePathToTmpFile(WeBWorK::PG::IO::convertPath("png/foo"));
   76     $path =~ s/foo$//; # remove final foo
   77     $self->{sourceFile} = $envir->{templateDirectory} . "/" .
   78       $envir->{fileName};
   79     $self->{tmpURLstart} = $tmpURLstart;
   80     $self->{tmppath}=$path;
   81     $self->{filenamestart}="$studname-$psvn-set${setname}prob${problemnum}image";
   82 }
   83 
   84 # Add another string to list to be LaTeX'ed
   85 # return the tag
   86 sub add {
   87   my $self = shift;
   88   my $newstr = shift;
   89   my $tag = $newstr;
   90   $self->{count}++;
   91   my $tempURL= $self->tmpurl()."$self->{count}.png";
   92 
   93   if ($tag =~ /^\\\(/) {
   94     $tag =~ s|^\\\( *||;
   95     $tag =~ s|\\\)$||;
   96     $tag = qq!<img src="$tempURL" align="middle" alt="$tag">!;
   97   } else {
   98     # Displayed math comes in with \[ stuff \].  To get a good
   99     # bounding box through preview, we change that to \( \displaystyle{
  100     # stuff } \), and then center the resulting image
  101     $tag =~ s|^\\\[ *||;
  102     $tag =~ s|\\\]$||;
  103     $newstr = '\(\displaystyle{'.$tag.'}\)';
  104     $tag = qq!<div align="center"><img src="$tempURL" align="middle" alt="$tag"></div>!;
  105   }
  106   push @{$self->{latexlines}}, $newstr;
  107 
  108   return $tag;
  109 }
  110 
  111 sub getCount {
  112   my $self = shift;
  113   return($self->{count});
  114 }
  115 
  116 sub tmpurl {
  117   my $self = shift;
  118   return("$self->{tmpURLstart}png/$self->{filenamestart}");
  119 }
  120 
  121 sub render {
  122   my $self = shift;
  123   my %opts = @_;
  124   my $refreshMe = 0;
  125   if (defined($opts{refresh}) and
  126       (($opts{refresh} eq "yes") or ($opts{refresh} == 1))) {
  127     $refreshMe = 1;
  128   }
  129 
  130 # $refreshMe = 1;  # Uncomment for testing
  131   my $latexfilenamebase = $self->{tmppath}.$self->{filenamestart};
  132 
  133   my $sourcePath = $self->{sourceFile};
  134   my $tempFile = "${latexfilenamebase}".$self->{count}.".png"; # last image
  135 
  136   if ($refreshMe or not -e $tempFile
  137             or (stat $sourcePath)[9] > (stat $tempFile)[9]) {
  138     # image file doesn't exist, or source file is newer then image file
  139     # or we just want new images produced
  140 
  141 #   my $old_cdir = `pwd`; # cd for running latex
  142 #   chomp($old_cdir);
  143     chdir($self->{tmppath}) ||
  144       warn "Could not move into temporary directory $self->{tmppath}";
  145 
  146     if (-e "$latexfilenamebase.tex") {
  147       unlink("$latexfilenamebase.tex") ||
  148         warn "Could not delete old LaTeX file";
  149     }
  150 
  151     local *LATEXME;
  152     open(LATEXME,">$latexfilenamebase.tex") || warn "Cannot create temporary tex file";
  153     print LATEXME <<'EOT';
  154 \documentclass[12pt]{article}
  155 \nonstopmode
  156 \usepackage{amsmath,amsfonts,amssymb}
  157 \def\gt{>}
  158 \def\lt{<}
  159 
  160 \usepackage[active,textmath,displaymath]{preview}
  161 \begin{document}
  162 EOT
  163 
  164     my $j;
  165     for $j (@{$self->{latexlines}}) {
  166       print LATEXME "\n$j\n";
  167     }
  168 
  169     print LATEXME '\end{document}'."\n";
  170     close(LATEXME);
  171 
  172     chmod(0666, "$latexfilenamebase.tex");
  173 
  174     $ENV{PATH} .= ":$Global::externalLaTeX2HTMLSupportPath";
  175     my $dvipng_res = int($Global::dvipngScaling * 1000+0.5);
  176     my $cmdout="";
  177 
  178     unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log",
  179            "$self->{filenamestart}.aux","missfont.log");
  180   print  "latex path == $Global::externalLatexPath \n";
  181   print " dvipng path == $Global::externalDvipngPath \n";
  182   print "divpngres= $dvipng_res  dvipngScaling= $Global::dvipngScaling shrinkFactor $Global::dvipngShrinkFactor  mode $Global::dvipngMode  DPI $Global::dvipngDPI\n";
  183     $cmdout=system("$Global::externalLatexPath $self->{filenamestart}.tex > /dev/null 2>/dev/null");
  184 
  185     $cmdout=system("$Global::externalDvipngPath -x$dvipng_res -bgTransparent -Q$Global::dvipngShrinkFactor -mode $Global::dvipngMode -D$Global::dvipngDPI $self->{filenamestart}.dvi > /dev/null 2>/dev/null");
  186 #   unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log",
  187 #          "$self->{filenamestart}.tex",
  188 #          "$self->{filenamestart}.aux" );
  189 #   chdir($old_cdir);
  190   }
  191 }
  192 
  193 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9