#!/usr/local/bin/perl
################################################################################
# WeBWorK
#
# Copyright (c) 1995-2002 University of Rochester
# All rights reserved
#
################################################################################
use lib '.'; use webworkInit; # WeBWorKInitLine
use strict;
=head1 NAME
ImageGenerator.pm
=head1 SYNPOSIS
Create an object for holding bits of math for LaTeX, and then to process
them later.
my $imgen = new ImageGenerator; #create a image generator
$imgen->initialize(\%envir); # provide basic data in preparation of image collection
$imgen->add(string); # add a new LaTeX string to be processed.
# Should be in math mode with \( or \[
# It returns the html tag
$imgen->getCount(); # Returns the number of images which have been added
$imgen->tmpurl(); # Returns the beginning of the html path
$imgen->render(); # Generates the images. By default, we reuse old
# images when reasonable, unless passed a flag
# refresh=>'yes' (or 1)
=head1 DESCRIPTION
This module defines an object which holds bits of LaTeX math for
generating images.
=cut
package ImageGenerator;
=head2 new
Creates the ImageGenerator object.
=cut
sub new {
my $class = shift;
my $self = {
latexlines => [],
count => 0,
tmppath => "",
tmpURLstart=>"",
filenamestart=> ""
};
bless $self, $class;
}
sub initialize {
my $self = shift;
my $envir = shift; # pointer to problem envirment hash
my $problemnum = $envir->{'probNum'};
my $studname = $envir->{'studentLogin'};
my $psvn = $envir->{'psvn'};
my $setname = $envir->{'setNumber'};
my $tmpURLstart = $envir->{'tempURL'};
my $path=main::surePathToTmpFile(main::convertPath("png/$setname/$psvn/foo"));
$path =~ s/foo$//; # remove final foo
$self->{sourceFile} = $envir->{templateDirectory} . "/" .
$envir->{fileName};
$self->{tmpURLstart} = $tmpURLstart."png/$setname/$psvn";
$self->{tmppath}=$path;
$self->{filenamestart}="$studname-prob${problemnum}image";
}
# Add another string to list to be LaTeX'ed
# return the tag
sub add {
my $self = shift;
my $newstr = shift;
my $tag = $newstr;
$self->{count}++;
my $tempURL= $self->tmpurl()."$self->{count}.png";
if ($tag =~ /^\\\(/) {
$tag =~ s|^\\\( *||;
$tag =~ s|\\\)$||;
$tag = qq!!;
} else {
# Displayed math comes in with \[ stuff \]. To get a good
# bounding box through preview, we change that to \( \displaystyle{
# stuff } \), and then center the resulting image
$tag =~ s|^\\\[ *||;
$tag =~ s|\\\]$||;
$newstr = '\(\displaystyle{'.$tag.'}\)';
$tag = qq!