Index: /issm/trunk-jpl/externalpackages/export_fig/export_fig.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 12406)
+++ /issm/trunk-jpl/externalpackages/export_fig/export_fig.m	(revision 12407)
@@ -16,4 +16,5 @@
 %   export_fig ... -<colorspace>
 %   export_fig ... -append
+%   export_fig ... -bookmark
 %   export_fig(..., handle)
 %
@@ -120,4 +121,6 @@
 %             exists, the figure is to be appended as a new page, instead
 %             of being overwritten (default).
+%   -bookmark - option to indicate that a bookmark with the name of the
+%               figure is to be created in the output file (pdf only).
 %   handle - The handle of the figure or axes (can be an array of handles
 %            of several axes, but these must be in the same figure) to be
@@ -159,4 +162,8 @@
 % 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for
 %           reporting it).
+% 02/05/12: Incorporate patch of Petr Nechaev (many thanks), enabling
+%           bookmarking of figures in pdf files.
+% 09/05/12: Incorporate patch of Arcelia Arrieta (many thanks), to keep
+%           tick marks fixed.
 
 function [im alpha] = export_fig(varargin)
@@ -194,11 +201,14 @@
 Hlims = findall(fig, 'Type', 'axes');
 if ~cls
-    % Record the old axes limit modes
+    % Record the old axes limit and tick 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');
+    Xtick = make_cell(get(Hlims, 'XTickMode'));
+    Ytick = make_cell(get(Hlims, 'YTickMode'));
+    Ztick = make_cell(get(Hlims, 'ZTickMode'));
+end
+% Set all axes limit and tick modes to manual, so the limits and ticks can't change
+set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', 'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual');
 % Set to print exactly what is there
 set(fig, 'InvertHardcopy', 'off');
@@ -403,4 +413,12 @@
             eps_remove_background(tmp_nam);
         end
+        % Add a bookmark to the PDF if desired
+        if options.bookmark
+            fig_nam = get(fig, 'Name');
+            if isempty(fig_nam)
+                warning('export_fig:EmptyBookmark', 'Bookmark requested for figure with no name. Bookmark will be empty.');
+            end
+            add_bookmark(tmp_nam, fig_nam);
+        end
         % Generate a pdf
         eps2pdf(tmp_nam, pdf_nam, 1, options.append, options.colourspace==2, options.quality);
@@ -435,7 +453,7 @@
     % Reset the hardcopy mode
     set(fig, 'InvertHardcopy', old_mode);
-    % Reset the axes limit modes
+    % Reset the axes limit and tick modes
     for a = 1:numel(Hlims)
-        set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a});
+        set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a}, 'XTickMode', Xtick{a}, 'YTickMode', Ytick{a}, 'ZTickMode', Ztick{a});
     end
 end
@@ -462,4 +480,5 @@
                  'aa_factor', 3, ...
                  'magnify', 1, ...
+                 'bookmark', false, ...
                  'quality', []);
 native = false; % Set resolution to native of an image
@@ -504,4 +523,6 @@
                 case 'append'
                     options.append = true;
+                case 'bookmark'
+                    options.bookmark = true;
                 case 'native'
                     native = true;
@@ -743,6 +764,40 @@
 % Helper function
 function A = make_cell(A)
- if ~iscell(A)
-     A = {A};
- end
- 
+if ~iscell(A)
+    A = {A};
+end
+return
+
+function add_bookmark(fname, bookmark_text)
+% Adds a bookmark to the temporary EPS file after %%EndPageSetup
+% 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);
+
+% Include standard pdfmark prolog to maximize compatibility
+fstrm = strrep(fstrm, '%%BeginProlog', sprintf('%%%%BeginProlog\n/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse'));
+% Add page bookmark
+fstrm = strrep(fstrm, '%%EndPageSetup', sprintf('%%%%EndPageSetup\n[ /Title (%s) /OUT pdfmark',bookmark_text));
+
+% Write out the updated file
+fh = fopen(fname, 'w');
+if fh == -1
+    error('Unable to open %s for writing.', fname);
+end
+try
+    fwrite(fh, fstrm, 'char*1');
+catch ex
+    fclose(fh);
+    rethrow(ex);
+end
+fclose(fh);
+return
Index: /issm/trunk-jpl/externalpackages/export_fig/print2array.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 12406)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2array.m	(revision 12407)
@@ -41,4 +41,7 @@
 % 27/1/2012 Bug fix affecting painters rendering tall figures. Thanks to
 % Ken Campbell for reporting it.
+
+% 3/4/2012 Bug fix to median input. Thanks to Andy Matthews for reporting
+% it.
 
 function [A bcol] = print2array(fig, res, renderer)
@@ -119,5 +122,5 @@
             end
         end
-        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))], 1);
+        bcol = uint8(median(single([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);
Index: /issm/trunk-jpl/externalpackages/export_fig/print2eps.m
===================================================================
--- /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 12406)
+++ /issm/trunk-jpl/externalpackages/export_fig/print2eps.m	(revision 12407)
@@ -45,4 +45,6 @@
 % alias can lead to another font being swapped in.
 
+% 10/4/2012 Make the font swapping case insensitive.
+
 function print2eps(name, fig, varargin)
 options = {'-depsc2'};
@@ -57,33 +59,42 @@
 end
 % Find all the used fonts in the figure
-fonts = get(findall(fig, '-property', 'FontName'), 'FontName');
+font_handles = findall(fig, '-property', 'FontName');
+fonts = get(font_handles, 'FontName');
 if ~iscell(fonts)
     fonts = {fonts};
 end
-fonts = unique(fonts);
 % Map supported font aliases onto the correct name
+fontsl = lower(fonts);
 for a = 1:numel(fonts)
-    f = lower(fonts{a});
+    f = fontsl{a};
     f(f==' ') = [];
     switch f
         case {'times', 'timesnewroman', 'times-roman'}
-            fonts{a} = 'Times-Roman';
+            fontsl{a} = 'times-roman';
         case {'arial', 'helvetica'}
-            fonts{a} = 'Helvetica';
+            fontsl{a} = 'helvetica';
         case {'newcenturyschoolbook', 'newcenturyschlbk'}
-            fonts{a} = 'NewCenturySchlbk';
+            fontsl{a} = 'newcenturyschlbk';
         otherwise
     end
 end
+fontslu = unique(fontsl);
 % 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)];
+matlab_fontsl = lower(matlab_fonts);
+require_swap = find(~ismember(fontslu, matlab_fontsl));
+unused_fonts = find(~ismember(matlab_fontsl, fontslu));
+font_swap = cell(3, 0);
+for a = 1:min(numel(require_swap), numel(unused_fonts))
+    ind = find(strcmp(fontslu{require_swap(a)}, fontsl));
+    n = numel(ind);
+    font_swap(1,end+1:end+n) = reshape(mat2cell(font_handles(ind), ones(n, 1)), 1, []);
+    font_swap(2,end-n+1:end) = matlab_fonts(unused_fonts(a));
+    font_swap(3,end-n+1:end) = reshape(fonts(ind), 1, []);
+end
 % Swap the fonts
 for a = 1:size(font_swap, 2)
-    set(findall(fig, 'FontName', font_swap{2,a}), 'FontName', font_swap{1,a});
+    set(font_swap{1,a}, 'FontName', font_swap{2,a});
 end
 % Set paper size
@@ -114,7 +125,8 @@
     % 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});
+        set(font_swap{1,a}, 'FontName', font_swap{3,a});
     end
     % Replace the font names in the eps file
+    font_swap = font_swap(2:3,:);
     try
         swap_fonts(name, font_swap{:});
