Skip to content

Commit

Permalink
Make it easier to change the image extension without concern of breaking
Browse files Browse the repository at this point in the history
hardcopy by subclassing the TikZImage in the PGtikz.pl macro and
overriding the `ext` method.

Also set the default for image generation to `svg` instead of `png`.  A
problem author should determine if `svg` images will work well with the
tikz code utilized.  If not, the author should override the extension to
`png`.
  • Loading branch information
drgrice1 committed Apr 4, 2021
1 parent 24da98b commit 46ddcc6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/TikZImage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sub new {
tikzLibraries => '',
texPackages => {},
addToPreamble => '',
ext => 'png',
ext => 'svg',
imageName => ''
};
my $self = sub {
Expand All @@ -46,7 +46,7 @@ sub new {
if ($field eq 'ext') {
my $ext = shift;
$data->{ext} = $ext
if ($ext eq 'png' || $ext eq 'gif' || $ext eq 'svg' || $ext eq 'pdf');
if ($ext && ($ext eq 'png' || $ext eq 'gif' || $ext eq 'svg' || $ext eq 'pdf'));
}
else {
$data->{$field} = shift;
Expand Down
38 changes: 29 additions & 9 deletions macros/PGtikz.pl
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,41 @@ =head1 DETAILED USAGE
$image->ext() Set the file type to be used for the image.
The valid image types are 'png', 'gif', 'svg',
and 'pdf'. The default is a 'png' image. This
macro sets this to 'pdf' when a hardcopy is
generated.
and 'pdf'. The default is an 'svg' image. You
should determine if an 'svg' image works well with
the TikZ code that you utilize. If not, then use
this method to change the exension to 'png' or
'gif'.
This macro sets the extension to 'pdf' when a
hardcopy is generated.
=cut

sub _PGtikz_init {}
sub _PGtikz_init {
main::PG_restricted_eval('sub createTikZImage { PGtikz->new(@_); }');
}

package PGtikz;
our @ISA = qw(TikZImage);

# Not much needs to be done here. The real work is done in TikZImage.pm.
sub createTikZImage
{
my $image = new TikZImage;
$image->ext('pdf') if $main::displayMode eq 'TeX';
sub new {
my $self = shift;
my $class = ref($self) || $self;

my $image = $class->SUPER::new(@_);
$image->SUPER::ext('pdf') if $main::displayMode eq 'TeX';
$image->imageName($main::PG->getUniqueName($image->ext));
return $image;

return bless $image, $class;
}

sub ext {
my $self = shift;
my $ext = shift;
return $self->SUPER::ext($ext) if $ext && $main::displayMode ne 'TeX';
return $self->SUPER::ext;
}

1;

0 comments on commit 46ddcc6

Please sign in to comment.