Index: /issm/trunk-jpl/externalpackages/export_fig/copyfig.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/copyfig.m	(revision 11803)
+++ /issm/trunk-jpl/externalpackages/export_fig/copyfig.m	(revision 11803)
@@ -0,0 +1,33 @@
+%COPYFIG Create a copy of a figure, without changing the figure
+%
+% Examples:
+%   fh_new = copyfig(fh_old)
+%
+% This function will create a copy of a figure, but not change the figure,
+% as copyobj sometimes does, e.g. by changing legends.
+%
+% IN:
+%    fh_old - The handle of the figure to be copied. Default: gcf.
+%
+% OUT:
+%    fh_new - The handle of the created figure.
+
+% Copyright (C) Oliver Woodford 2012
+
+function fh = copyfig(fh)
+% Set the default
+if nargin == 0
+    fh = gcf;
+end
+% Is there a legend?
+if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
+    % Safe to copy using copyobj
+    fh = copyobj(fh, 0);
+else
+    % copyobj will change the figure, so save and then load it instead
+    tmp_nam = [tempname '.fig'];
+    hgsave(fh, tmp_nam);
+    fh = hgload(tmp_nam);
+    delete(tmp_nam);
+end
+return
Index: /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/eps2pdf.m	(revision 11803)
@@ -33,5 +33,5 @@
 %             gives lossless output. Default: ghostscript prepress default.
 
-% Copyright (C) Oliver Woodford 2009-2010
+% Copyright (C) Oliver Woodford 2009-2011
 
 % Suggestion of appending pdf files provided by Matt C at:
@@ -43,4 +43,6 @@
 % which was fixed for lossless compression settings.
 
+% 9/12/2011 Pass font path to ghostscript.
+
 function eps2pdf(source, dest, crop, append, gray, quality)
 % Intialise the options string for ghostscript
@@ -49,4 +51,9 @@
 if nargin < 3 || crop
     options = [options ' -dEPSCrop'];
+end
+% Set the font path
+fp = font_path();
+if ~isempty(fp)
+    options = [options ' -sFONTPATH="' fp '"'];
 end
 % Set the grayscale option
@@ -103,2 +110,25 @@
 return
 
+% Function to return (and create, where necessary) the font path
+function fp = font_path()
+fp = user_string('gs_font_path');
+if ~isempty(fp)
+    return
+end
+% Create the path
+% Start with the default path
+fp = getenv('GS_FONTPATH');
+% Add on the typical directories for a given OS
+if ispc
+    if ~isempty(fp)
+        fp = [fp ';'];
+    end
+    fp = [fp getenv('WINDIR') filesep 'Fonts'];
+else
+    if ~isempty(fp)
+        fp = [fp ':'];
+    end
+    fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
+end
+user_string('gs_font_path', fp);
+return
Index: /issm/trunk-jpl/externalpackages/export_fig/export_fig.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 11803)
@@ -134,5 +134,5 @@
 %   See also PRINT, SAVEAS.
 
-% Copyright (C) Oliver Woodford 2008-2011
+% Copyright (C) Oliver Woodford 2008-2012
 
 % The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
@@ -156,5 +156,12 @@
 % isolated from gui objects.
 
+% 23/02/12: Ensure that axes limits don't change during printing
+% 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for
+%           reporting it).
+
 function [im alpha] = export_fig(varargin)
+% Make sure the figure is rendered correctly _now_ so that properties like
+% axes limits are up-to-date.
+drawnow;
 % Parse the input arguments
 [fig options] = parse_args(nargout, varargin{:});
@@ -184,4 +191,14 @@
     end
 end
+% MATLAB "feature": axes limits can change when printing
+Hlims = findall(fig, 'Type', 'axes');
+if ~cls
+    % Record the old axes limit modes
+    Xlims = make_cell(get(Hlims, 'XLimMode'));
+    Ylims = make_cell(get(Hlims, 'YLimMode'));
+    Zlims = make_cell(get(Hlims, 'ZLimMode'));
+end
+% Set all axes limit modes to manual, so the limits can't change
+set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual');
 % Set to print exactly what is there
 set(fig, 'InvertHardcopy', 'off');
@@ -418,4 +435,8 @@
     % Reset the hardcopy mode
     set(fig, 'InvertHardcopy', old_mode);
+    % Reset the axes limit modes
+    for a = 1:numel(Hlims)
+        set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a});
+    end
 end
 return
@@ -719,2 +740,9 @@
 b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
 return
+
+% Helper function
+function A = make_cell(A)
+ if ~iscell(A)
+     A = {A};
+ end
+ 
Index: /issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m	(revision 11803)
@@ -18,7 +18,9 @@
 %    fh - The handle of the created figure.
 
-% Copyright (C) Oliver Woodford 2011
+% Copyright (C) Oliver Woodford 2011-2012
 
 % Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
+% 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
+% for pointing out that the function is also used in export_fig.m.
 
 function fh = isolate_axes(ah, vis)
@@ -107,16 +109,2 @@
 end
 return
-
-function fh = copyfig(fh)
-% Is there a legend?
-if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
-    % Safe to copy using copyobj
-    fh = copyobj(fh, 0);
-else
-    % copyobj will change the figure, so save and then load it instead
-    tmp_nam = [tempname '.fig'];
-    hgsave(fh, tmp_nam);
-    fh = hgload(tmp_nam);
-    delete(tmp_nam);
-end
-return
Index: /issm/trunk-jpl/externalpackages/export_fig/license.txt
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/license.txt	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/license.txt	(revision 11803)
@@ -1,3 +1,3 @@
-Copyright (c) 2011, Oliver Woodford
+Copyright (c) 2012, Oliver Woodford
 All rights reserved.
 
Index: /issm/trunk-jpl/externalpackages/export_fig/print2array.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 11803)
@@ -37,4 +37,9 @@
 % Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
 
+% 9/12/2011 Pass font path to ghostscript.
+
+% 27/1/2012 Bug fix affecting painters rendering tall figures. Thanks to
+% Ken Campbell for reporting it.
+
 function [A bcol] = print2array(fig, res, renderer)
 % Generate default input arguments, if needed
@@ -66,6 +71,15 @@
     print2eps(tmp_eps, fig, renderer, '-loose');
     try
-        % Export to tiff using ghostscript
-        ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
+        % Initialize the command to export to tiff using ghostscript
+        cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
+        % Set the font path
+        fp = font_path();
+        if ~isempty(fp)
+            cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
+        end
+        % Add the filenames
+        cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
+        % Execute the ghostscript command
+        ghostscript(cmd_str);
     catch
         % Delete the intermediate file
@@ -105,5 +119,5 @@
             end
         end
-        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
+        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))], 1);
         for c = 1:size(A, 3)
             A(:,[1:l-1, r+1:end],c) = bcol(c);
@@ -156,2 +170,26 @@
 end
 return
+
+% Function to return (and create, where necessary) the font path
+function fp = font_path()
+fp = user_string('gs_font_path');
+if ~isempty(fp)
+    return
+end
+% Create the path
+% Start with the default path
+fp = getenv('GS_FONTPATH');
+% Add on the typical directories for a given OS
+if ispc
+    if ~isempty(fp)
+        fp = [fp ';'];
+    end
+    fp = [fp getenv('WINDIR') filesep 'Fonts'];
+else
+    if ~isempty(fp)
+        fp = [fp ':'];
+    end
+    fp = [fp '/usr/share/fonts:/usr/local/share/fonts:/usr/share/fonts/X11:/usr/local/share/fonts/X11:/usr/share/fonts/truetype:/usr/local/share/fonts/truetype'];
+end
+user_string('gs_font_path', fp);
+return
Index: /issm/trunk-jpl/externalpackages/export_fig/print2eps.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 11802)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 11803)
@@ -6,7 +6,10 @@
 %   print2eps(filename, fig_handle, options)
 %
-% This function saves a figure as an eps file, and improves the line style,
-% making dashed lines more like those on screen and giving grid lines their
-% own dotted style.
+% This function saves a figure as an eps file, with two improvements over
+% MATLAB's print command. First, it improves the line style, making dashed
+% lines more like those on screen and giving grid lines their own dotted
+% style. Secondly, it substitutes original font names back into the eps
+% file, where these have been changed by MATLAB, for up to 11 different
+% fonts.
 %
 %IN:
@@ -18,5 +21,5 @@
 %   options - Additional parameter strings to be passed to print.
 
-% Copyright (C) Oliver Woodford 2008-2011
+% Copyright (C) Oliver Woodford 2008-2012
 
 % The idea of editing the EPS file to change line styles comes from Jiro
@@ -28,4 +31,17 @@
 % Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix
 % from TMW.
+
+% 8/12/2011 Added ability to correct fonts. Several people have requested
+% this at one time or another, and also pointed me to printeps (fex id:
+% 7501), so thank you to them. My implementation (which was not inspired by
+% printeps - I'd already had the idea for my approach) goes
+% slightly further in that it allows multiple fonts to be swapped.
+
+% 14/12/2011 Fix bug affecting font names containing spaces. Many thanks to
+% David Szwer for reporting the issue.
+
+% 25/1/2012 Add a font not to be swapped. Thanks to Anna Rafferty and Adam
+% Jackson for reporting the issue. Also fix a bug whereby using a font
+% alias can lead to another font being swapped in.
 
 function print2eps(name, fig, varargin)
@@ -39,4 +55,35 @@
 if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps')
     name = [name '.eps']; % Add the missing extension
+end
+% Find all the used fonts in the figure
+fonts = get(findall(fig, '-property', 'FontName'), 'FontName');
+if ~iscell(fonts)
+    fonts = {fonts};
+end
+fonts = unique(fonts);
+% Map supported font aliases onto the correct name
+for a = 1:numel(fonts)
+    f = lower(fonts{a});
+    f(f==' ') = [];
+    switch f
+        case {'times', 'timesnewroman', 'times-roman'}
+            fonts{a} = 'Times-Roman';
+        case {'arial', 'helvetica'}
+            fonts{a} = 'Helvetica';
+        case {'newcenturyschoolbook', 'newcenturyschlbk'}
+            fonts{a} = 'NewCenturySchlbk';
+        otherwise
+    end
+end
+% Determine the font swap table
+matlab_fonts = {'Helvetica', 'Times-Roman', 'Palatino', 'Bookman', 'Helvetica-Narrow', 'Symbol', ...
+                'AvantGarde', 'NewCenturySchlbk', 'Courier', 'ZapfChancery', 'ZapfDingbats'};
+require_swap = find(~ismember(fonts, matlab_fonts));
+unused_fonts = find(~ismember(matlab_fonts, fonts));
+font_swap = min(numel(require_swap), numel(unused_fonts));
+font_swap = [reshape(matlab_fonts(unused_fonts(1:font_swap)), 1, font_swap); reshape(fonts(require_swap(1:font_swap)), 1, font_swap)];
+% Swap the fonts
+for a = 1:size(font_swap, 2)
+    set(findall(fig, 'FontName', font_swap{2,a}), 'FontName', font_swap{1,a});
 end
 % Set paper size
@@ -63,4 +110,18 @@
 % Reset paper size
 set(fig, 'PaperPositionMode', old_mode);
+% Correct the fonts
+if ~isempty(font_swap)
+    % Reset the font names in the figure
+    for a = 1:size(font_swap, 2)
+        set(findall(fig, 'FontName', font_swap{1,a}), 'FontName', font_swap{2,a});
+    end
+    % Replace the font names in the eps file
+    try
+        swap_fonts(name, font_swap{:});
+    catch
+        warning('swap_fonts() failed. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
+        return
+    end
+end
 % Fix the line styles
 try
@@ -70,2 +131,35 @@
 end
 return
+
+function swap_fonts(fname, varargin)
+% Read in the file
+fh = fopen(fname, 'r');
+if fh == -1
+    error('File %s not found.', fname);
+end
+try
+    fstrm = fread(fh, '*char')';
+catch ex
+    fclose(fh);
+    rethrow(ex);
+end
+fclose(fh);
+
+% Replace the font names
+for a = 1:2:numel(varargin)
+    fstrm = regexprep(fstrm, [varargin{a} '-?[a-zA-Z]*\>'], varargin{a+1}(~isspace(varargin{a+1})));
+end
+
+% Write out the updated file
+fh = fopen(fname, 'w');
+if fh == -1
+    error('Unable to open %s for writing.', fname2);
+end
+try
+    fwrite(fh, fstrm, 'char*1');
+catch ex
+    fclose(fh);
+    rethrow(ex);
+end
+fclose(fh);
+return
