Ignore:
Timestamp:
03/28/12 13:43:15 (13 years ago)
Author:
Mathieu Morlighem
Message:

updated export_fig

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/externalpackages/export_fig/print2eps.m

    r10635 r11803  
    66%   print2eps(filename, fig_handle, options)
    77%
    8 % This function saves a figure as an eps file, and improves the line style,
    9 % making dashed lines more like those on screen and giving grid lines their
    10 % own dotted style.
     8% This function saves a figure as an eps file, with two improvements over
     9% MATLAB's print command. First, it improves the line style, making dashed
     10% lines more like those on screen and giving grid lines their own dotted
     11% style. Secondly, it substitutes original font names back into the eps
     12% file, where these have been changed by MATLAB, for up to 11 different
     13% fonts.
    1114%
    1215%IN:
     
    1821%   options - Additional parameter strings to be passed to print.
    1922
    20 % Copyright (C) Oliver Woodford 2008-2011
     23% Copyright (C) Oliver Woodford 2008-2012
    2124
    2225% The idea of editing the EPS file to change line styles comes from Jiro
     
    2831% Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix
    2932% from TMW.
     33
     34% 8/12/2011 Added ability to correct fonts. Several people have requested
     35% this at one time or another, and also pointed me to printeps (fex id:
     36% 7501), so thank you to them. My implementation (which was not inspired by
     37% printeps - I'd already had the idea for my approach) goes
     38% slightly further in that it allows multiple fonts to be swapped.
     39
     40% 14/12/2011 Fix bug affecting font names containing spaces. Many thanks to
     41% David Szwer for reporting the issue.
     42
     43% 25/1/2012 Add a font not to be swapped. Thanks to Anna Rafferty and Adam
     44% Jackson for reporting the issue. Also fix a bug whereby using a font
     45% alias can lead to another font being swapped in.
    3046
    3147function print2eps(name, fig, varargin)
     
    3955if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps')
    4056    name = [name '.eps']; % Add the missing extension
     57end
     58% Find all the used fonts in the figure
     59fonts = get(findall(fig, '-property', 'FontName'), 'FontName');
     60if ~iscell(fonts)
     61    fonts = {fonts};
     62end
     63fonts = unique(fonts);
     64% Map supported font aliases onto the correct name
     65for a = 1:numel(fonts)
     66    f = lower(fonts{a});
     67    f(f==' ') = [];
     68    switch f
     69        case {'times', 'timesnewroman', 'times-roman'}
     70            fonts{a} = 'Times-Roman';
     71        case {'arial', 'helvetica'}
     72            fonts{a} = 'Helvetica';
     73        case {'newcenturyschoolbook', 'newcenturyschlbk'}
     74            fonts{a} = 'NewCenturySchlbk';
     75        otherwise
     76    end
     77end
     78% Determine the font swap table
     79matlab_fonts = {'Helvetica', 'Times-Roman', 'Palatino', 'Bookman', 'Helvetica-Narrow', 'Symbol', ...
     80                'AvantGarde', 'NewCenturySchlbk', 'Courier', 'ZapfChancery', 'ZapfDingbats'};
     81require_swap = find(~ismember(fonts, matlab_fonts));
     82unused_fonts = find(~ismember(matlab_fonts, fonts));
     83font_swap = min(numel(require_swap), numel(unused_fonts));
     84font_swap = [reshape(matlab_fonts(unused_fonts(1:font_swap)), 1, font_swap); reshape(fonts(require_swap(1:font_swap)), 1, font_swap)];
     85% Swap the fonts
     86for a = 1:size(font_swap, 2)
     87    set(findall(fig, 'FontName', font_swap{2,a}), 'FontName', font_swap{1,a});
    4188end
    4289% Set paper size
     
    63110% Reset paper size
    64111set(fig, 'PaperPositionMode', old_mode);
     112% Correct the fonts
     113if ~isempty(font_swap)
     114    % Reset the font names in the figure
     115    for a = 1:size(font_swap, 2)
     116        set(findall(fig, 'FontName', font_swap{1,a}), 'FontName', font_swap{2,a});
     117    end
     118    % Replace the font names in the eps file
     119    try
     120        swap_fonts(name, font_swap{:});
     121    catch
     122        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.');
     123        return
     124    end
     125end
    65126% Fix the line styles
    66127try
     
    70131end
    71132return
     133
     134function swap_fonts(fname, varargin)
     135% Read in the file
     136fh = fopen(fname, 'r');
     137if fh == -1
     138    error('File %s not found.', fname);
     139end
     140try
     141    fstrm = fread(fh, '*char')';
     142catch ex
     143    fclose(fh);
     144    rethrow(ex);
     145end
     146fclose(fh);
     147
     148% Replace the font names
     149for a = 1:2:numel(varargin)
     150    fstrm = regexprep(fstrm, [varargin{a} '-?[a-zA-Z]*\>'], varargin{a+1}(~isspace(varargin{a+1})));
     151end
     152
     153% Write out the updated file
     154fh = fopen(fname, 'w');
     155if fh == -1
     156    error('Unable to open %s for writing.', fname2);
     157end
     158try
     159    fwrite(fh, fstrm, 'char*1');
     160catch ex
     161    fclose(fh);
     162    rethrow(ex);
     163end
     164fclose(fh);
     165return
Note: See TracChangeset for help on using the changeset viewer.