Created
March 9, 2016 16:04
-
-
Save goerz/d2678065e570e9712584 to your computer and use it in GitHub Desktop.
Creating Combined tikz/png Plots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
# (c) 2010 Michael Goerz <[email protected]> | |
# This code is in the public domain | |
# Usage: | |
# combine_pm3dmap.pl infile.tikz infile.png | |
# replace infile.tikz with "combined" version | |
my $tikzfile = ''; | |
my $pngfile = ''; | |
if (@ARGV == 2){ | |
$tikzfile = $ARGV[0]; | |
$pngfile = $ARGV[1]; | |
} else { | |
print "Usage: \n"; | |
print " combine_pm3dmap.pl infile.tikz infile.png\n"; | |
} | |
my $nw = ''; | |
my $sw = ''; | |
my $se = ''; | |
my $ne = ''; | |
my $bottom_color = ''; | |
my $top_color = ''; | |
my $white_color = 'color=\gprgb{1000}{1000}{1000}'; | |
my $bottom2white_box_sw = ''; | |
my $bottom2white_box_ne = ''; | |
my $white2top_box_sw = ''; | |
my $white2top_box_ne = ''; | |
# copy file, get size of drawing area + position/colors of legend (first pass) | |
open(INFILE, $tikzfile) or die ("Coudn't open $tikzfile for reading\n"); | |
open(OUTFILE, ">$tikzfile~") or die ("Coudn't open $tikzfile~ for writing\n"); | |
#$| = 1; # switch off buffering | |
my $reached_border = 0; | |
while(my $line = <INFILE>) { | |
print OUTFILE $line; | |
chomp $line; | |
if ($line eq '\gpsetlinetype{gp_lt_border}'){ | |
$reached_border = 1; | |
} | |
if ($reached_border){ | |
if ($line =~ m'\\draw\[gp path\] \(([0-9.,]+)\)--\(([0-9.,]+)\)--\(([0-9.,]+)\)--\(([0-9.,]+)\)--cycle;'){ | |
$nw = $1 if ($nw eq ''); | |
$sw = $2 if ($sw eq ''); | |
$se = $3 if ($se eq ''); | |
$ne = $4 if ($ne eq ''); | |
} | |
if ($line =~ m'\\gpfill\{(color=\\gprgb\{1000\}\{1000\}\{1000\})\} (\([0-9.,]+\))--(\([0-9.,]+\))--(\([0-9.,]+\))--(\([0-9.,]+\))--cycle;'){ | |
# the white box in the middle defines top of bottom2white_box and | |
# bottom of white2top_box | |
$white2top_box_sw = $2; | |
$bottom2white_box_ne = $4; | |
} elsif ($line =~ m'\\gpfill\{(color=\\gprgb\{[0-9]+\}\{[0-9]+\}\{[0-9]+\})\} (\([0-9.,]+\))--(\([0-9.,]+\))--(\([0-9.,]+\))--(\([0-9.,]+\))--cycle;'){ | |
$bottom_color = $1 if ($bottom_color eq ''); | |
$top_color = $1; | |
$bottom2white_box_sw = $2 if ($bottom2white_box_sw eq ''); | |
$white2top_box_ne = $4; # overwrite each time, final result from last gpfill line | |
} | |
} | |
} | |
close OUTFILE; | |
close INFILE; | |
# replace plot commands with png graphic + legend with gradient (second pass) | |
open(INFILE, "$tikzfile~") or die ("Coudn't open $tikzfile~ for reading\n"); | |
open(OUTFILE, ">$tikzfile") or die ("Coudn't open $tikzfile for writing\n"); | |
my $started_plot = 0; | |
my $started_legend = 0; | |
my $finished_plot = 0; | |
my $finished_legend = 0; | |
while(my $line = <INFILE>) { | |
if ($line =~ m'^\\gpfill'){ | |
if (not $started_plot){ | |
$started_plot = 1; | |
print OUTFILE "\\node[anchor=north west,inner sep=0pt,outer sep=0pt] at ($nw)\n"; | |
my $width = 0; | |
if ($ne =~ m'([0-9.]+),([0-9.]+)') { | |
$width = $1; | |
} else { | |
die ("ne point '$ne' has unexpected format\n"); | |
} | |
if ($nw =~ m'([0-9.]+),([0-9.]+)') { | |
$width = $width - $1; | |
} else { | |
die ("nw point '$nw' has unexpected format\n"); | |
} | |
my $height = 0; | |
if ($nw =~ m'([0-9.]+),([0-9.]+)') { | |
$height = $2; | |
} else { | |
die ("nw point '$nw' has unexpected format\n"); | |
} | |
if ($sw =~ m'([0-9.]+),([0-9.]+)') { | |
$height = $height - $2; | |
} else { | |
die ("sw point '$sw' has unexpected format\n"); | |
} | |
print OUTFILE " {\\includegraphics[width=${width}cm,height=${height}cm]{$pngfile}};\n"; | |
} | |
if ($finished_plot and not $started_legend){ | |
$started_legend = 1; | |
print OUTFILE "\\shade[bottom $bottom_color,top $white_color] $bottom2white_box_sw rectangle $bottom2white_box_ne;\n"; | |
print OUTFILE "\\shade[bottom $white_color, top $top_color] $white2top_box_sw rectangle $white2top_box_ne;\n"; | |
} | |
} else { | |
$finished_plot = 1 if ($started_plot); | |
$finished_legend = 1 if ($started_legend); | |
} | |
unless ( ($started_plot and not $finished_plot) | |
or ($started_legend and not $finished_legend) ) { | |
print OUTFILE $line; | |
} | |
} | |
close OUTFILE; | |
close INFILE; | |
unlink "$tikzfile~"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment