@@ -813,9 +813,13 @@ def save(figure=None):
813813 # any required modifications.
814814 pp_funcs = []
815815
816+ # Local cache of postprocessing options.
817+ fix_raster_paths = _config ['postprocessing' ].getboolean ('fix_raster_paths' )
818+ tikzpicture = _config ['postprocessing' ].getboolean ('tikzpicture' )
819+
816820 # Add the appropriate directory prefix to all raster images
817821 # included via \pgfimage.
818- if _config [ 'postprocessing' ]. getboolean ( ' fix_raster_paths' ) :
822+ if fix_raster_paths :
819823 figdir = os .path .dirname (figname ) or '.'
820824
821825 # Only apply this if the figure is not in the top-level directory.
@@ -826,60 +830,59 @@ def save(figure=None):
826830 pp_funcs .append (lambda s : re .sub (expr , repl , s ))
827831
828832 # Use the tikzpicture environment rather than pgfpicture.
829- if _config [ 'postprocessing' ]. getboolean ( ' tikzpicture' ) :
833+ if tikzpicture :
830834 expr = re .compile (r"\\(begin|end){pgfpicture}" )
831835 repl = r"\\\1{tikzpicture}"
832836 pp_funcs .append (lambda s : re .sub (expr , repl , s ))
833837
834838 # Postprocess the figure, moving it into the final destination.
835839 with open (mpname , 'r' ) as infile , open (figname , 'w' ) as outfile :
836- # Update the creator line at the start of the header.
837- line = infile .readline ()[:- 1 ]
838- outfile .write (line )
839- outfile .write (" v" )
840- outfile .write (mpl_version )
841- outfile .write (", matplotlib-pgfutils v" )
842- outfile .write (__version__ )
843- outfile .write ('\n %% Script: ' )
844- outfile .write (os .path .abspath (script ))
845- outfile .write ('\n ' )
846-
847- # Update the \input instructions.
848- outfile .write (infile .readline ())
849- outfile .write (infile .readline ())
850- _ = infile .readline ()
851- outfile .write ("%% \\ input{" )
852- outfile .write (figname )
853- outfile .write ("}\n " )
854-
855- # Copy the preamble instructions.
856- outfile .write (infile .readline ())
857- outfile .write (infile .readline ())
858- line = infile .readline ()
859- if _config ['postprocessing' ].getboolean ('tikzpicture' ):
860- outfile .write ("%% \\ usepackage{tikz}\n " )
861- else :
862- outfile .write (line )
863- outfile .write (infile .readline ())
864840
865- # If we've updated the raster image directory, we can delete the
866- # instructions about getting them to appear.
867- if _config ['postprocessing' ].getboolean ('fix_raster_paths' ):
868- for n in range (7 ):
869- _ = infile .readline ()
870-
871- # Otherwise we need to copy them over.
872- else :
873- for n in range (7 ):
874- outfile .write (infile .readline ())
875-
876- # Ignore the rest of the header (which includes the preamble which was
877- # used). The first non-header line is \begingroup which we don't want
878- # to change anyway.
841+ # Make some modifications to the header.
879842 line = infile .readline ()
880843 while line [0 ] == '%' :
881- outfile .write (line )
844+ # Update the creator line to include pgfutils version, and add a
845+ # line with the path of the script that created the figure.
846+ if "Creator:" in line :
847+ outfile .write (line [:- 1 ])
848+ outfile .write (" v" )
849+ outfile .write (mpl_version )
850+ outfile .write (", matplotlib-pgfutils v" )
851+ outfile .write (__version__ )
852+ outfile .write ('\n %% Script: ' )
853+ outfile .write (os .path .abspath (script ))
854+ outfile .write ('\n ' )
855+
856+ # Update the \input instructions.
857+ elif r"\input{<filename>.pgf}" in line :
858+ outfile .write ("%% \\ input{" )
859+ outfile .write (figname )
860+ outfile .write ("}\n " )
861+
862+ # If we're changing the figure to use the tikzpicture environment, we also
863+ # need to update the required package.
864+ elif tikzpicture and r"\usepackage{pgf}" in line :
865+ outfile .write ("%% \\ usepackage{tikz}\n " )
866+
867+ # If we're fixing the paths to rasterised images, we can remove the
868+ # instructions about using the import package.
869+ elif fix_raster_paths and line .startswith ("%% Figures using additional raster" ):
870+ while r"\import{<path to file>}" not in line :
871+ line = infile .readline ()
872+
873+ # Discard blank line after the statement too.
874+ infile .readline ()
875+
876+ # Copy the original line.
877+ else :
878+ outfile .write (line )
879+
880+ # Next line of header.
882881 line = infile .readline ()
882+
883+ # Post-process and write the first line after the header.
884+ for func in pp_funcs :
885+ line = func (line )
883886 outfile .write (line )
884887
885888 # Apply the postprocessing to the remainder of the file.
0 commit comments