[system] / trunk / pg / lib / WeBWorK / PG / ImageGenerator.pm Repository:
ViewVC logotype

Diff of /trunk/pg/lib/WeBWorK/PG/ImageGenerator.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 6850 Revision 6851
163 my ($invocant, %options) = @_; 163 my ($invocant, %options) = @_;
164 my $class = ref $invocant || $invocant; 164 my $class = ref $invocant || $invocant;
165 my $self = { 165 my $self = {
166 names => [], 166 names => [],
167 strings => [], 167 strings => [],
168 texPreambleAdditions => undef,
168 depths => {}, 169 depths => {},
169 %options, 170 %options,
170 }; 171 };
171 172
172 # set some values 173 # set some values
180 $self->{basename} = ""; 181 $self->{basename} = "";
181 $self->{equationCache} = WeBWorK::EquationCache->new(cacheDB => $self->{cacheDB}); 182 $self->{equationCache} = WeBWorK::EquationCache->new(cacheDB => $self->{cacheDB});
182 } 183 }
183 184
184 bless $self, $class; 185 bless $self, $class;
186}
187
188=item addToTeXPreamble($string)
189
190Adds the string as part of the TeX preamble for all equations in the problem.
191For example
192 $rh_envir->{imagegen}->addToTeXPreamble("\newcommand{\myVec}[#1]{\vec{#1}} ");
193
194Will define a question wide style for interpreting
195 \( \myVec{v} \)
196
197If this statement is placed in PGcourse.pl then the backslashes must be doubled since it is a .pl file
198not a .pg file
199
200=cut
201
202sub addToTeXPreamble {
203 my $self = shift;
204 my $str = shift;
205 $self->{texPreambleAdditions} = $str if defined $str;
206 $self->{texPreambleAdditions};
207}
208
209=item refresh(1)
210
211Forces every equation picture to be recalculated. Useful for debugging.
212 $rh_envir->{imagegen}->refresh(1);
213
214=cut
215
216sub refresh {
217 my $self = shift;
218 my $in = shift;
219 $self->{refresh} = $in if defined($in);
220 $self->{refresh};
185} 221}
186 222
187=item add($string, $mode) 223=item add($string, $mode)
188 224
189Adds the equation in C<$string> to the object. C<$mode> can be "display" or 225Adds the equation in C<$string> to the object. C<$mode> can be "display" or
281source file from which the equations originate be used for this value. If the 317source file from which the equations originate be used for this value. If the
282key "refresh" in C<%options> is true, images will be regenerated regardless of 318key "refresh" in C<%options> is true, images will be regenerated regardless of
283when they were last modified. If neither option is supplied, "refresh" is 319when they were last modified. If neither option is supplied, "refresh" is
284assumed. 320assumed.
285 321
322NOTE: It's not clear to me that mtime has been implemented -- MEG - 2011/06
323
286=cut 324=cut
287 325
288sub render { 326sub render {
289 my ($self, %options) = @_; 327 my ($self, %options) = @_;
290 328
295 my $dvipng = $self->{dvipng}; 333 my $dvipng = $self->{dvipng};
296 my $names = $self->{names}; 334 my $names = $self->{names};
297 my $strings = $self->{strings}; 335 my $strings = $self->{strings};
298 my $depths = $self->{depths}; 336 my $depths = $self->{depths};
299 $self->{body_text} = $options{body_text}; 337 $self->{body_text} = $options{body_text};
338 my $forceRefresh = $self->{refresh} || 0; # recreate every equation image -- default is do not refresh
300 339
301 ############################################### 340 ###############################################
302 # check that the equations directory exists and create if it doesn't 341 # check that the equations directory exists and create if it doesn't
303 ############################################### 342 ###############################################
304 unless (-e "$dir") { 343 unless (-e "$dir") {
311 ############################################### 350 ###############################################
312 my (@newStrings, @newNames); 351 my (@newStrings, @newNames);
313 for (my $i = 0; $i < @$strings; $i++) { 352 for (my $i = 0; $i < @$strings; $i++) {
314 my $string = $strings->[$i]; 353 my $string = $strings->[$i];
315 my $name = $names->[$i]; 354 my $name = $names->[$i];
316 if (-e "$dir/$name") { 355 if (!$forceRefresh and -e "$dir/$name") {
317 #warn "ImageGenerator: found a file named $name, skipping string $string\n"; 356 #warn "ImageGenerator: found a file named $name, skipping string $string\n";
318 } else { 357 } else {
319 #warn "ImageGenerator: didn't find a file named $name, including string $string\n"; 358 #warn "ImageGenerator: didn't find a file named $name, including string $string\n";
320 push @newStrings, $string; 359 push @newStrings, $string;
321 push @newNames, $name; 360 push @newNames, $name;
330 # store equations in a tex file 369 # store equations in a tex file
331 my $texFile = "$wd/equation.tex"; 370 my $texFile = "$wd/equation.tex";
332 open my $tex, ">", $texFile 371 open my $tex, ">", $texFile
333 or die "failed to open file $texFile for writing: $!"; 372 or die "failed to open file $texFile for writing: $!";
334 print $tex $TexPreamble; 373 print $tex $TexPreamble;
374 print $tex $self->{texPreambleAdditions} if defined($self->{texPreambleAdditions});
335 print $tex "$_\n" foreach @newStrings; 375 print $tex "$_\n" foreach @newStrings;
336 print $tex $TexPostamble; 376 print $tex $TexPostamble;
337 close $tex; 377 close $tex;
338 warn "tex file $texFile was not written" unless -e $texFile; 378 warn "tex file $texFile was not written" unless -e $texFile;
339 379

Legend:
Removed from v.6850  
changed lines
  Added in v.6851

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9