Changeset 11803


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

updated export_fig

Location:
issm/trunk-jpl/externalpackages/export_fig
Files:
1 added
6 edited

Legend:

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

    r7182 r11803  
    3333%             gives lossless output. Default: ghostscript prepress default.
    3434
    35 % Copyright (C) Oliver Woodford 2009-2010
     35% Copyright (C) Oliver Woodford 2009-2011
    3636
    3737% Suggestion of appending pdf files provided by Matt C at:
     
    4343% which was fixed for lossless compression settings.
    4444
     45% 9/12/2011 Pass font path to ghostscript.
     46
    4547function eps2pdf(source, dest, crop, append, gray, quality)
    4648% Intialise the options string for ghostscript
     
    4951if nargin < 3 || crop
    5052    options = [options ' -dEPSCrop'];
     53end
     54% Set the font path
     55fp = font_path();
     56if ~isempty(fp)
     57    options = [options ' -sFONTPATH="' fp '"'];
    5158end
    5259% Set the grayscale option
     
    103110return
    104111
     112% Function to return (and create, where necessary) the font path
     113function fp = font_path()
     114fp = user_string('gs_font_path');
     115if ~isempty(fp)
     116    return
     117end
     118% Create the path
     119% Start with the default path
     120fp = getenv('GS_FONTPATH');
     121% Add on the typical directories for a given OS
     122if ispc
     123    if ~isempty(fp)
     124        fp = [fp ';'];
     125    end
     126    fp = [fp getenv('WINDIR') filesep 'Fonts'];
     127else
     128    if ~isempty(fp)
     129        fp = [fp ':'];
     130    end
     131    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'];
     132end
     133user_string('gs_font_path', fp);
     134return
  • issm/trunk-jpl/externalpackages/export_fig/export_fig.m

    r10635 r11803  
    134134%   See also PRINT, SAVEAS.
    135135
    136 % Copyright (C) Oliver Woodford 2008-2011
     136% Copyright (C) Oliver Woodford 2008-2012
    137137
    138138% The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
     
    156156% isolated from gui objects.
    157157
     158% 23/02/12: Ensure that axes limits don't change during printing
     159% 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for
     160%           reporting it).
     161
    158162function [im alpha] = export_fig(varargin)
     163% Make sure the figure is rendered correctly _now_ so that properties like
     164% axes limits are up-to-date.
     165drawnow;
    159166% Parse the input arguments
    160167[fig options] = parse_args(nargout, varargin{:});
     
    184191    end
    185192end
     193% MATLAB "feature": axes limits can change when printing
     194Hlims = findall(fig, 'Type', 'axes');
     195if ~cls
     196    % Record the old axes limit modes
     197    Xlims = make_cell(get(Hlims, 'XLimMode'));
     198    Ylims = make_cell(get(Hlims, 'YLimMode'));
     199    Zlims = make_cell(get(Hlims, 'ZLimMode'));
     200end
     201% Set all axes limit modes to manual, so the limits can't change
     202set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual');
    186203% Set to print exactly what is there
    187204set(fig, 'InvertHardcopy', 'off');
     
    418435    % Reset the hardcopy mode
    419436    set(fig, 'InvertHardcopy', old_mode);
     437    % Reset the axes limit modes
     438    for a = 1:numel(Hlims)
     439        set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a});
     440    end
    420441end
    421442return
     
    719740b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
    720741return
     742
     743% Helper function
     744function A = make_cell(A)
     745 if ~iscell(A)
     746     A = {A};
     747 end
     748 
  • issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m

    r10635 r11803  
    1818%    fh - The handle of the created figure.
    1919
    20 % Copyright (C) Oliver Woodford 2011
     20% Copyright (C) Oliver Woodford 2011-2012
    2121
    2222% Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
     23% 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
     24% for pointing out that the function is also used in export_fig.m.
    2325
    2426function fh = isolate_axes(ah, vis)
     
    107109end
    108110return
    109 
    110 function fh = copyfig(fh)
    111 % Is there a legend?
    112 if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
    113     % Safe to copy using copyobj
    114     fh = copyobj(fh, 0);
    115 else
    116     % copyobj will change the figure, so save and then load it instead
    117     tmp_nam = [tempname '.fig'];
    118     hgsave(fh, tmp_nam);
    119     fh = hgload(tmp_nam);
    120     delete(tmp_nam);
    121 end
    122 return
  • issm/trunk-jpl/externalpackages/export_fig/license.txt

    r10635 r11803  
    1 Copyright (c) 2011, Oliver Woodford
     1Copyright (c) 2012, Oliver Woodford
    22All rights reserved.
    33
  • issm/trunk-jpl/externalpackages/export_fig/print2array.m

    r10635 r11803  
    3737% Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
    3838
     39% 9/12/2011 Pass font path to ghostscript.
     40
     41% 27/1/2012 Bug fix affecting painters rendering tall figures. Thanks to
     42% Ken Campbell for reporting it.
     43
    3944function [A bcol] = print2array(fig, res, renderer)
    4045% Generate default input arguments, if needed
     
    6671    print2eps(tmp_eps, fig, renderer, '-loose');
    6772    try
    68         % Export to tiff using ghostscript
    69         ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
     73        % Initialize the command to export to tiff using ghostscript
     74        cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
     75        % Set the font path
     76        fp = font_path();
     77        if ~isempty(fp)
     78            cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
     79        end
     80        % Add the filenames
     81        cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"'];
     82        % Execute the ghostscript command
     83        ghostscript(cmd_str);
    7084    catch
    7185        % Delete the intermediate file
     
    105119            end
    106120        end
    107         bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
     121        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A([t b],:,:), [], size(A, 3))], 1);
    108122        for c = 1:size(A, 3)
    109123            A(:,[1:l-1, r+1:end],c) = bcol(c);
     
    156170end
    157171return
     172
     173% Function to return (and create, where necessary) the font path
     174function fp = font_path()
     175fp = user_string('gs_font_path');
     176if ~isempty(fp)
     177    return
     178end
     179% Create the path
     180% Start with the default path
     181fp = getenv('GS_FONTPATH');
     182% Add on the typical directories for a given OS
     183if ispc
     184    if ~isempty(fp)
     185        fp = [fp ';'];
     186    end
     187    fp = [fp getenv('WINDIR') filesep 'Fonts'];
     188else
     189    if ~isempty(fp)
     190        fp = [fp ':'];
     191    end
     192    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'];
     193end
     194user_string('gs_font_path', fp);
     195return
  • 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.