[system] / trunk / webwork-modperl / lib / WeBWorK / PG / ImageGenerator.pm Repository:
ViewVC logotype

View of /trunk/webwork-modperl/lib/WeBWorK/PG/ImageGenerator.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1131 - (download) (as text) (annotate)
Wed Jun 11 20:40:11 2003 UTC (10 years ago) by sh002i
File size: 5769 byte(s)
Made many changes:

- turned off PG warning catching in conf/global.conf.dist
- added warning reporting to conf/templates/ur.template (but not to
  other templates!)
- modified a couple of error messages in WeBWorK.pm
- made failure to create course environment and failure to find course
  directory fatal errors in WeBWorK.pm
- added warning queueing and call stack storing to Apache::WeBWorK
- added "warnings" and "if_warnings" template escapes to
  WeBWorK::ContentGenerator
- removed warning handling from WeBWorK::ContentGenerator::Problem
- code tidying in WeBWorK::ContentGenerator::Problem
- code tidying in WeBWorK::PG::ImageGenerator

-sam

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9