Changeset 14174


Ignore:
Timestamp:
12/17/12 14:41:41 (12 years ago)
Author:
Mathieu Morlighem
Message:

CHG: updated export_fig

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

Legend:

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

    r12407 r14174  
    123123%   -bookmark - option to indicate that a bookmark with the name of the
    124124%               figure is to be created in the output file (pdf only).
    125 %   handle - The handle of the figure or axes (can be an array of handles
    126 %            of several axes, but these must be in the same figure) to be
     125%   handle - The handle of the figure, axes or uipanels (can be an array of
     126%            handles, but the objects must be in the same figure) to be
    127127%            saved. Default: gcf.
    128128%
     
    166166% 09/05/12: Incorporate patch of Arcelia Arrieta (many thanks), to keep
    167167%           tick marks fixed.
    168 
    169 function [im alpha] = export_fig(varargin)
     168% 12/12/12: Add support for isolating uipanels. Thanks to michael for
     169%           suggesting it.
     170
     171function [im, alpha] = export_fig(varargin)
    170172% Make sure the figure is rendered correctly _now_ so that properties like
    171173% axes limits are up-to-date.
    172174drawnow;
    173175% Parse the input arguments
    174 [fig options] = parse_args(nargout, varargin{:});
     176[fig, options] = parse_args(nargout, varargin{:});
    175177% Isolate the subplot, if it is one
    176 cls = strcmp(get(fig(1), 'Type'), 'axes');
     178cls = all(ismember(get(fig, 'Type'), {'axes', 'uipanel'}));
    177179if cls
    178180    % Given handles of one or more axes, so isolate them from the rest
    179181    fig = isolate_axes(fig);
    180182else
     183    % Check we have a figure
     184    if ~isequal(get(fig, 'Type'), 'figure');
     185        error('Handle must be that of a figure, axes or uipanel');
     186    end
     187    % Get the old InvertHardcopy mode
    181188    old_mode = get(fig, 'InvertHardcopy');
    182189end
     
    198205    end
    199206end
    200 % MATLAB "feature": axes limits can change when printing
     207% MATLAB "feature": axes limits and tick marks can change when printing
    201208Hlims = findall(fig, 'Type', 'axes');
    202209if ~cls
     
    282289        % Crop the background
    283290        if options.crop
    284             [alpha v] = crop_background(alpha, 0);
     291            [alpha, v] = crop_background(alpha, 0);
    285292            A = A(v(1):v(2),v(3):v(4),:);
    286293        end
     
    289296            res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
    290297            % Save the png
    291             imwrite(A, [options.name '.png'], 'Alpha', alpha, 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
     298            imwrite(A, [options.name '.png'], 'Alpha', double(alpha), 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
    292299            % Clear the png bit
    293300            options.png = false;
     
    325332            tcol = 255;
    326333        else
    327             [A tcol] = print2array(fig, magnify, renderer);
     334            [A, tcol] = print2array(fig, magnify, renderer);
    328335        end
    329336        % Crop the background
     
    460467return
    461468
    462 function [fig options] = parse_args(nout, varargin)
     469function [fig, options] = parse_args(nout, varargin)
    463470% Parse the input arguments
    464471% Set the defaults
     
    542549            end
    543550        else
    544             [p options.name ext] = fileparts(varargin{a});
     551            [p, options.name, ext] = fileparts(varargin{a});
    545552            if ~isempty(p)
    546553                options.name = [p filesep options.name];
     
    577584
    578585% Check whether transparent background is wanted (old way)
    579 if isequal(get(fig, 'Color'), 'none')
     586if isequal(get(ancestor(fig, 'figure'), 'Color'), 'none')
    580587    options.transparent = true;
    581588end
     
    667674return
    668675
    669 function [A v] = crop_background(A, bcol)
     676function [A, v] = crop_background(A, bcol)
    670677% Map the foreground pixels
    671 [h w c] = size(A);
     678[h, w, c] = size(A);
    672679if isscalar(bcol) && c > 1
    673680    bcol = bcol(ones(1, c));
  • issm/trunk-jpl/externalpackages/export_fig/ghostscript.m

    r10635 r14174  
    1 function varargout = ghostscript(cmd)
    21%GHOSTSCRIPT  Calls a local GhostScript executable with the input command
    32%
     
    2524% Thanks to Jonas Dorn for the fix for the title of the uigetdir window on
    2625% Mac OS.
    27 
    2826% Thanks to Nathan Childress for the fix to the default location on 64-bit
    2927% Windows systems.
    30 
    3128% 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
    3229% Shaun Kline for pointing out the issue
    33 
    3430% 4/5/11 - Thanks to David Chorlian for pointing out an alternative
    3531% location for gs on linux.
     32% 12/12/12 - Add extra executable name on Windows. Thanks to Ratish
     33% Punnoose for highlighting the issue.
    3634
     35function varargout = ghostscript(cmd)
    3736% Call ghostscript
    3837[varargout{1:nargout}] = system(sprintf('"%s" %s', gs_path, cmd));
    3938return
    4039
    41 function path = gs_path
     40function path_ = gs_path
    4241% Return a valid path
    4342% Start with the currently set path
    44 path = user_string('ghostscript');
     43path_ = user_string('ghostscript');
    4544% Check the path works
    46 if check_gs_path(path)
     45if check_gs_path(path_)
    4746    return
    4847end
    4948% Check whether the binary is on the path
    5049if ispc
    51     bin = {'gswin32c.exe', 'gswin64c.exe'};
     50    bin = {'gswin32c.exe', 'gswin64c.exe', 'gs'};
    5251else
    5352    bin = {'gs'};
    5453end
    5554for a = 1:numel(bin)
    56     path = bin{a};
    57     if check_store_gs_path(path)
     55    path_ = bin{a};
     56    if check_store_gs_path(path_)
    5857        return
    5958    end
     
    7675                path2 = [default_location dir_list(a).name executable{b}];
    7776                if exist(path2, 'file') == 2
    78                     path = path2;
     77                    path_ = path2;
    7978                    ver_num = ver_num2;
    8079                end
     
    8281        end
    8382    end
    84     if check_store_gs_path(path)
     83    if check_store_gs_path(path_)
    8584        return
    8685    end
     
    8887    bin = {'/usr/bin/gs', '/usr/local/bin/gs'};
    8988    for a = 1:numel(bin)
    90         path = bin{a};
    91         if check_store_gs_path(path)
     89        path_ = bin{a};
     90        if check_store_gs_path(path_)
    9291            return
    9392        end
     
    110109    for a = 1:numel(bin_dir)
    111110        for b = 1:numel(bin)
    112             path = [base bin_dir{a} bin{b}];
    113             if exist(path, 'file') == 2
    114                 if check_store_gs_path(path)
     111            path_ = [base bin_dir{a} bin{b}];
     112            if exist(path_, 'file') == 2
     113                if check_store_gs_path(path_)
    115114                    return
    116115                end
     
    121120error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
    122121
    123 function good = check_store_gs_path(path)
     122function good = check_store_gs_path(path_)
    124123% Check the path is valid
    125 good = check_gs_path(path);
     124good = check_gs_path(path_);
    126125if ~good
    127126    return
    128127end
    129128% Update the current default path to the path found
    130 if ~user_string('ghostscript', path)
     129if ~user_string('ghostscript', path_)
    131130    warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.txt.');
    132131    return
     
    134133return
    135134
    136 function good = check_gs_path(path)
     135function good = check_gs_path(path_)
    137136% Check the path is valid
    138 [good message] = system(sprintf('"%s" -h', path));
     137[good, message] = system(sprintf('"%s" -h', path_));
    139138good = good == 0;
    140139return
  • issm/trunk-jpl/externalpackages/export_fig/isolate_axes.m

    r11803 r14174  
    55%   fh = isolate_axes(ah, vis)
    66%
    7 % This function will create a new figure containing the axes specified, and
    8 % also their associated legends and colorbars. The axes specified must all
    9 % be in the same figure, but they will generally only be a subset of the
    10 % axes in the figure.
     7% This function will create a new figure containing the axes/uipanels
     8% specified, and also their associated legends and colorbars. The objects
     9% specified must all be in the same figure, but they will generally only be
     10% a subset of the objects in the figure.
    1111%
    1212% IN:
    13 %    ah - An array of axes handles, which must come from the same figure.
     13%    ah - An array of axes and uipanel handles, which must come from the
     14%         same figure.
    1415%    vis - A boolean indicating whether the new figure should be visible.
    1516%          Default: false.
     
    2324% 16/3/2012 Moved copyfig to its own function. Thanks to Bob Fratantonio
    2425% for pointing out that the function is also used in export_fig.m.
     26% 12/12/12 - Add support for isolating uipanels. Thanks to michael for
     27% suggesting it.
    2528
    2629function fh = isolate_axes(ah, vis)
     
    2932    error('ah must be an array of handles');
    3033end
    31 % Check that the handles are all for axes, and are all in the same figure
     34% Check that the handles are all for axes or uipanels, and are all in the same figure
    3235fh = ancestor(ah(1), 'figure');
    3336nAx = numel(ah);
    3437for a = 1:nAx
    35     if ~strcmp(get(ah(a), 'Type'), 'axes')
    36         error('All handles must be axes handles.');
     38    if ~ismember(get(ah(a), 'Type'), {'axes', 'uipanel'})
     39        error('All handles must be axes or uipanel handles.');
    3740    end
    3841    if ~isequal(ancestor(ah(a), 'figure'), fh)
     
    4043    end
    4144end
    42 % Tag the axes so we can find them in the copy
     45% Tag the objects so we can find them in the copy
    4346old_tag = get(ah, 'Tag');
    4447if nAx == 1
     
    5154    set(fh, 'Visible', 'off');
    5255end
    53 % Reset the axes tags
     56% Reset the object tags
    5457for a = 1:nAx
    5558    set(ah(a), 'Tag', old_tag{a});
     
    5962if numel(ah) ~= nAx
    6063    close(fh);
    61     error('Incorrect number of axes found.');
     64    error('Incorrect number of objects found.');
    6265end
    6366% Set the axes tags to what they should be
     
    8790% Get all the objects in the figure
    8891axs = findall(fh);
    89 % Delete everything except for the input axes and associated items
     92% Delete everything except for the input objects and associated items
    9093delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
    9194return
  • issm/trunk-jpl/externalpackages/export_fig/pdftops.m

    r10635 r14174  
    3333return
    3434
    35 function path = xpdf_path
     35function path_ = xpdf_path
    3636% Return a valid path
    3737% Start with the currently set path
    38 path = user_string('pdftops');
     38path_ = user_string('pdftops');
    3939% Check the path works
    40 if check_xpdf_path(path)
     40if check_xpdf_path(path_)
    4141    return
    4242end
     
    4848end
    4949if check_store_xpdf_path(bin)
    50     path = bin;
     50    path_ = bin;
    5151    return
    5252end
    5353% Search the obvious places
    5454if ispc
    55     path = 'C:\Program Files\xpdf\pdftops.exe';
     55    path_ = 'C:\Program Files\xpdf\pdftops.exe';
    5656else
    57     path = '/usr/local/bin/pdftops';
     57    path_ = '/usr/local/bin/pdftops';
    5858end
    59 if check_store_xpdf_path(path)
     59if check_store_xpdf_path(path_)
    6060    return
    6161end
     
    7575    bin_dir = {'', ['bin' filesep], ['lib' filesep]};
    7676    for a = 1:numel(bin_dir)
    77         path = [base bin_dir{a} bin];
    78         if exist(path, 'file') == 2
     77        path_ = [base bin_dir{a} bin];
     78        if exist(path_, 'file') == 2
    7979            break;
    8080        end
    8181    end
    82     if check_store_xpdf_path(path)
     82    if check_store_xpdf_path(path_)
    8383        return
    8484    end
     
    8686error('pdftops executable not found.');
    8787
    88 function good = check_store_xpdf_path(path)
     88function good = check_store_xpdf_path(path_)
    8989% Check the path is valid
    90 good = check_xpdf_path(path);
     90good = check_xpdf_path(path_);
    9191if ~good
    9292    return
    9393end
    9494% Update the current default path to the path found
    95 if ~user_string('pdftops', path)
     95if ~user_string('pdftops', path_)
    9696    warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.txt.');
    9797    return
     
    9999return
    100100
    101 function good = check_xpdf_path(path)
     101function good = check_xpdf_path(path_)
    102102% Check the path is valid
    103 [good message] = system(sprintf('"%s" -h', path));
     103[good message] = system(sprintf('"%s" -h', path_));
    104104% system returns good = 1 even when the command runs
    105105% Look for something distinct in the help text
  • issm/trunk-jpl/externalpackages/export_fig/print2array.m

    r12407 r14174  
    2525%   bcol - 1x3 uint8 vector of the background color
    2626
    27 % Copyright (C) Oliver Woodford 2008-2011
     27% Copyright (C) Oliver Woodford 2008-2012
    2828
    29 % 5/9/2011 Set EraseModes to normal when using opengl or zbuffer renderers.
    30 % Thanks to Pawel Kocieniewski for reporting the issue.
     29% 05/09/11: Set EraseModes to normal when using opengl or zbuffer
     30%           renderers. Thanks to Pawel Kocieniewski for reporting the
     31%           issue.
     32% 21/09/11: Bug fix: unit8 -> uint8! Thanks to Tobias Lamour for reporting
     33%           the issue.
     34% 14/11/11: Bug fix: stop using hardcopy(), as it interfered with figure
     35%           size and erasemode settings. Makes it a bit slower, but more
     36%           reliable. Thanks to Phil Trinh and Meelis Lootus for reporting
     37%           the issues.
     38% 09/12/11: Pass font path to ghostscript.
     39% 27/01/12: Bug fix affecting painters rendering tall figures. Thanks to
     40%           Ken Campbell for reporting it.
     41% 03/04/12: Bug fix to median input. Thanks to Andy Matthews for reporting
     42%           it.
     43% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
     44%           reporting the issue.
    3145
    32 % 21/9/2011 Bug fix: unit8 -> uint8!
    33 % Thanks to Tobias Lamour for reporting the issue.
    34 
    35 % 14/11/2011 Bug fix: stop using hardcopy(), as it interfered with figure
    36 % size and erasemode settings. Makes it a bit slower, but more reliable.
    37 % Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
    38 
    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 
    44 % 3/4/2012 Bug fix to median input. Thanks to Andy Matthews for reporting
    45 % it.
    46 
    47 function [A bcol] = print2array(fig, res, renderer)
     46function [A, bcol] = print2array(fig, res, renderer)
    4847% Generate default input arguments, if needed
    4948if nargin < 2
     
    8584        % Execute the ghostscript command
    8685        ghostscript(cmd_str);
    87     catch
     86    catch me
    8887        % Delete the intermediate file
    8988        delete(tmp_eps);
    90         rethrow(lasterror);
     89        rethrow(me);
    9190    end
    9291    % Delete the intermediate file
     
    134133    err = false;
    135134    % Set paper size
    136     old_mode = get(fig, 'PaperPositionMode');
    137     set(fig, 'PaperPositionMode', 'auto');
     135    old_pos_mode = get(fig, 'PaperPositionMode');
     136    old_orientation = get(fig, 'PaperOrientation');
     137    set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
    138138    try
    139139        % Print to tiff file
     
    147147    end
    148148    % Reset paper size
    149     set(fig, 'PaperPositionMode', old_mode);
     149    set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
    150150    % Throw any error that occurred
    151151    if err
  • issm/trunk-jpl/externalpackages/export_fig/print2eps.m

    r12407 r14174  
    2828% fex id: 5743, but the implementation is mine :)
    2929
    30 % 14/11/2011 Fix a MATLAB bug rendering black or white text incorrectly.
    31 % Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix
    32 % 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.
    46 
    47 % 10/4/2012 Make the font swapping case insensitive.
     30% 14/11/2011: Fix a MATLAB bug rendering black or white text incorrectly.
     31%             Thanks to Mathieu Morlighem for reporting the issue and
     32%             obtaining a fix from TMW.
     33% 08/12/11: Added ability to correct fonts. Several people have requested
     34%           this at one time or another, and also pointed me to printeps
     35%           (fex id: 7501), so thank you to them. My implementation (which
     36%           was not inspired by printeps - I'd already had the idea for my
     37%           approach) goes slightly further in that it allows multiple
     38%           fonts to be swapped.
     39% 14/12/11: Fix bug affecting font names containing spaces. Thanks to David
     40%           Szwer for reporting the issue.
     41% 25/01/12: Add a font not to be swapped. Thanks to Anna Rafferty and Adam
     42%           Jackson for reporting the issue. Also fix a bug whereby using a
     43%           font alias can lead to another font being swapped in.
     44% 10/04/12: Make the font swapping case insensitive.
     45% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
     46%           reporting the issue.
     47% 26/10/12: Fix issue to do with swapping fonts changing other fonts and
     48%           sizes we don't want, due to listeners. Thanks to Malcolm Hudson
     49%           for reporting the issue.
    4850
    4951function print2eps(name, fig, varargin)
     
    5961end
    6062% Find all the used fonts in the figure
    61 font_handles = findall(fig, '-property', 'FontName');
     63font_handles = findobj(fig, '-property', 'FontName');
    6264fonts = get(font_handles, 'FontName');
    6365if ~iscell(fonts)
     
    8688require_swap = find(~ismember(fontslu, matlab_fontsl));
    8789unused_fonts = find(~ismember(matlab_fontsl, fontslu));
    88 font_swap = cell(3, 0);
    89 for a = 1:min(numel(require_swap), numel(unused_fonts))
    90     ind = find(strcmp(fontslu{require_swap(a)}, fontsl));
    91     n = numel(ind);
    92     font_swap(1,end+1:end+n) = reshape(mat2cell(font_handles(ind), ones(n, 1)), 1, []);
    93     font_swap(2,end-n+1:end) = matlab_fonts(unused_fonts(a));
    94     font_swap(3,end-n+1:end) = reshape(fonts(ind), 1, []);
     90font_swap = cell(3, min(numel(require_swap), numel(unused_fonts)));
     91fonts_new = fonts;
     92for a = 1:size(font_swap, 2)
     93    font_swap{1,a} = find(strcmp(fontslu{require_swap(a)}, fontsl));
     94    font_swap{2,a} = matlab_fonts{unused_fonts(a)};
     95    font_swap{3,a} = fonts{font_swap{1,end}(1)};
     96    fonts_new(font_swap{1,a}) = {font_swap{2,a}};
    9597end
    9698% Swap the fonts
    97 for a = 1:size(font_swap, 2)
    98     set(font_swap{1,a}, 'FontName', font_swap{2,a});
     99if ~isempty(font_swap)
     100    fonts_size = get(font_handles, 'FontSize');
     101    if iscell(fonts_size)
     102        fonts_size = cell2mat(fonts_size);
     103    end
     104    M = false(size(font_handles));
     105    % Loop because some changes may not stick first time, due to listeners
     106    c = 0;
     107    update = zeros(1000, 1);
     108    for b = 1:10 % Limit number of loops to avoid infinite loop case
     109        for a = 1:numel(M)
     110            M(a) = ~isequal(get(font_handles(a), 'FontName'), fonts_new{a}) || ~isequal(get(font_handles(a), 'FontSize'), fonts_size(a));
     111            if M(a)
     112                set(font_handles(a), 'FontName', fonts_new{a}, 'FontSize', fonts_size(a));
     113                c = c + 1;
     114                update(c) = a;
     115            end
     116        end
     117        if ~any(M)
     118            break;
     119        end
     120    end
     121    % Compute the order to revert fonts later, without the need of a loop
     122    [update, M] = unique(update(1:c));
     123    [M, M] = sort(M);
     124    update = reshape(update(M), 1, []);
    99125end
    100126% Set paper size
    101 old_mode = get(fig, 'PaperPositionMode');
    102 set(fig, 'PaperPositionMode', 'auto');
     127old_pos_mode = get(fig, 'PaperPositionMode');
     128old_orientation = get(fig, 'PaperOrientation');
     129set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
    103130% MATLAB bug fix - black and white text can come out inverted sometimes
    104131% Find the white and black text
     
    120147set(white_text_handles, 'Color', [1 1 1]);
    121148% Reset paper size
    122 set(fig, 'PaperPositionMode', old_mode);
     149set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
    123150% Correct the fonts
    124151if ~isempty(font_swap)
    125152    % Reset the font names in the figure
    126     for a = 1:size(font_swap, 2)
    127         set(font_swap{1,a}, 'FontName', font_swap{3,a});
     153    for a = update
     154        set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a));
    128155    end
    129156    % Replace the font names in the eps file
Note: See TracChangeset for help on using the changeset viewer.