Ignore:
Timestamp:
12/02/14 11:40:53 (10 years ago)
Author:
Mathieu Morlighem
Message:

CHG: upgrading export_fig for hg2 support and matlab R2014b

File:
1 edited

Legend:

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

    r14174 r18904  
    1313%   export_fig ... -a<val>
    1414%   export_fig ... -q<val>
     15%   export_fig ... -p<val>
    1516%   export_fig ... -<renderer>
    1617%   export_fig ... -<colorspace>
     
    8586%                  made transparent (png, pdf and eps output only).
    8687%   -m<val> - option where val indicates the factor to magnify the
    87 %             on-screen figure dimensions by when generating bitmap
     88%             on-screen figure pixel dimensions by when generating bitmap
    8889%             outputs. Default: '-m1'.
    8990%   -r<val> - option val indicates the resolution (in pixels per inch) to
    90 %             export bitmap outputs at, keeping the dimensions of the
    91 %             on-screen figure. Default: sprintf('-r%g', get(0,
    92 %             'ScreenPixelsPerInch')). Note that the -m and -r options
    93 %             change the same property.
     91%             export bitmap and vector outputs at, keeping the dimensions
     92%             of the on-screen figure. Default: '-r864' (for vector output
     93%             only). Note that the -m option overides the -r option for
     94%             bitmap outputs only.
    9495%   -native - option indicating that the output resolution (when outputting
    9596%             a bitmap format) should be such that the vertical resolution
     
    118119%             sometimes give a smaller file size than the default lossy
    119120%             compression, depending on the type of images.
     121%   -p<val> - option to add a border of width val to eps and pdf files,
     122%             where val is in units of the intermediate eps file. Default:
     123%             0 (i.e. no padding).
    120124%   -append - option indicating that if the file (pdfs only) already
    121125%             exists, the figure is to be appended as a new page, instead
     
    133137%
    134138%   Some helpful examples and tips can be found at:
    135 %      http://sites.google.com/site/oliverwoodford/software/export_fig
     139%      https://github.com/ojwoodford/export_fig
    136140%
    137141%   See also PRINT, SAVEAS.
    138142
    139 % Copyright (C) Oliver Woodford 2008-2012
     143% Copyright (C) Oliver Woodford 2008-2014
    140144
    141145% The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
     
    168172% 12/12/12: Add support for isolating uipanels. Thanks to michael for
    169173%           suggesting it.
     174% 25/09/13: Add support for changing resolution in vector formats. Thanks
     175%           to Jan Jaap Meijer for suggesting it.
     176% 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner
     177%           for suggesting it.
    170178
    171179function [im, alpha] = export_fig(varargin)
     
    217225end
    218226% Set all axes limit and tick modes to manual, so the limits and ticks can't change
    219 set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual', 'XTickMode', 'manual', 'YTickMode', 'manual', 'ZTickMode', 'manual');
     227set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual', 'ZLimMode', 'manual');
     228set_tick_mode(Hlims, 'X');
     229set_tick_mode(Hlims, 'Y');
     230set_tick_mode(Hlims, 'Z');
    220231% Set to print exactly what is there
    221232set(fig, 'InvertHardcopy', 'off');
     
    289300        % Crop the background
    290301        if options.crop
    291             [alpha, v] = crop_background(alpha, 0);
     302            [alpha, v] = crop_borders(alpha, 0, 1);
    292303            A = A(v(1):v(2),v(3):v(4),:);
    293304        end
     
    336347        % Crop the background
    337348        if options.crop
    338             A = crop_background(A, tcol);
     349            A = crop_borders(A, tcol, 1);
    339350        end
    340351        % Downscale the image
     
    406417    end
    407418    % Generate the options for print
    408     p2eArgs = {renderer};
     419    p2eArgs = {renderer, sprintf('-r%d', options.resolution)};
    409420    if options.colourspace == 1
    410421        p2eArgs = [p2eArgs {'-cmyk'}];
     
    415426    try
    416427        % Generate an eps
    417         print2eps(tmp_nam, fig, p2eArgs{:});
     428        print2eps(tmp_nam, fig, options.bb_padding, p2eArgs{:});
    418429        % Remove the background, if desired
    419430        if options.transparent && ~isequal(get(fig, 'Color'), 'none')
    420             eps_remove_background(tmp_nam);
     431            eps_remove_background(tmp_nam, 1 + using_hg2(fig));
    421432        end
    422433        % Add a bookmark to the PDF if desired
     
    465476    end
    466477end
    467 return
     478end
    468479
    469480function [fig, options] = parse_args(nout, varargin)
     
    485496                 'im', nout == 1, ...
    486497                 'alpha', nout == 2, ...
    487                  'aa_factor', 3, ...
    488                  'magnify', 1, ...
     498                 'aa_factor', 0, ...
     499                 'bb_padding', 0, ...
     500                 'magnify', [], ...
     501                 'resolution', [], ...
    489502                 'bookmark', false, ...
    490503                 'quality', []);
     
    535548                    native = true;
    536549                otherwise
    537                     val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q))(\d*\.)?\d+(e-?\d+)?', 'match'));
     550                    val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match'));
    538551                    if ~isscalar(val)
    539552                        error('option %s not recognised', varargin{a});
     
    543556                            options.magnify = val;
    544557                        case 'r'
    545                             options.magnify = val ./ get(0, 'ScreenPixelsPerInch');
     558                            options.resolution = val;
    546559                        case 'q'
    547560                            options.quality = max(val, 0);
     561                        case 'p'
     562                            options.bb_padding = val;
    548563                    end
    549564            end
     
    573588end
    574589
     590% Set default anti-aliasing now we know the renderer
     591if options.aa_factor == 0
     592    options.aa_factor = 1 + 2 * (~(using_hg2(fig) && strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on')) | (options.renderer == 3));
     593end
     594
     595% Convert user dir '~' to full path
     596if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\')
     597    options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
     598end
     599
     600% Compute the magnification and resolution
     601if isempty(options.magnify)
     602    if isempty(options.resolution)
     603        options.magnify = 1;
     604        options.resolution = 864;
     605    else
     606        options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch');
     607    end
     608elseif isempty(options.resolution)
     609    options.resolution = 864;
     610end 
     611
    575612% Check we have a figure handle
    576613if isempty(fig)
     
    584621
    585622% Check whether transparent background is wanted (old way)
    586 if isequal(get(ancestor(fig, 'figure'), 'Color'), 'none')
     623if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none')
    587624    options.transparent = true;
    588625end
     
    632669    end
    633670end
    634 return
     671end
    635672
    636673function A = downsize(A, factor)
     
    659696    A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
    660697end
    661 return
     698end
    662699
    663700function A = rgb2grey(A)
    664701A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A));
    665 return
     702end
    666703
    667704function A = check_greyscale(A)
     
    672709    A = A(:,:,1); % Save only one channel for 8-bit output
    673710end
    674 return
    675 
    676 function [A, v] = crop_background(A, bcol)
    677 % Map the foreground pixels
    678 [h, w, c] = size(A);
    679 if isscalar(bcol) && c > 1
    680     bcol = bcol(ones(1, c));
    681 end
    682 bail = false;
    683 for l = 1:w
    684     for a = 1:c
    685         if ~all(A(:,l,a) == bcol(a))
    686             bail = true;
    687             break;
    688         end
    689     end
    690     if bail
    691         break;
    692     end
    693 end
    694 bail = false;
    695 for r = w:-1:l
    696     for a = 1:c
    697         if ~all(A(:,r,a) == bcol(a))
    698             bail = true;
    699             break;
    700         end
    701     end
    702     if bail
    703         break;
    704     end
    705 end
    706 bail = false;
    707 for t = 1:h
    708     for a = 1:c
    709         if ~all(A(t,:,a) == bcol(a))
    710             bail = true;
    711             break;
    712         end
    713     end
    714     if bail
    715         break;
    716     end
    717 end
    718 bail = false;
    719 for b = h:-1:t
    720     for a = 1:c
    721         if ~all(A(b,:,a) == bcol(a))
    722             bail = true;
    723             break;
    724         end
    725     end
    726     if bail
    727         break;
    728     end
    729 end
    730 % Crop the background, leaving one boundary pixel to avoid bleeding on
    731 % resize
    732 v = [max(t-1, 1) min(b+1, h) max(l-1, 1) min(r+1, w)];
    733 A = A(v(1):v(2),v(3):v(4),:);
    734 return
    735 
    736 function eps_remove_background(fname)
     711end
     712
     713function eps_remove_background(fname, count)
    737714% Remove the background of an eps file
    738715% Open the file
     
    742719end
    743720% Read the file line by line
    744 while true
     721while count
    745722    % Get the next line
    746723    l = fgets(fh);
     
    749726    end
    750727    % Check if the line contains the background rectangle
    751     if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +rf *[\n\r]+', 'start'), 1)
     728    if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1)
    752729        % Set the line to whitespace and quit
    753730        l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
    754731        fseek(fh, -numel(l), 0);
    755732        fprintf(fh, l);
    756         break;
     733        % Reduce the count
     734        count = count - 1;
    757735    end
    758736end
    759737% Close the file
    760738fclose(fh);
    761 return
     739end
    762740
    763741function b = isvector(options)
    764742b = options.pdf || options.eps;
    765 return
     743end
    766744
    767745function b = isbitmap(options)
    768746b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
    769 return
     747end
    770748
    771749% Helper function
     
    774752    A = {A};
    775753end
    776 return
     754end
    777755
    778756function add_bookmark(fname, bookmark_text)
     
    808786end
    809787fclose(fh);
    810 return
     788end
     789
     790function set_tick_mode(Hlims, ax)
     791% Set the tick mode of linear axes to manual
     792% Leave log axes alone as these are tricky
     793M = get(Hlims, [ax 'Scale']);
     794if ~iscell(M)
     795    M = {M};
     796end
     797M = cellfun(@(c) strcmp(c, 'linear'), M);
     798set(Hlims(M), [ax 'TickMode'], 'manual');
     799end
Note: See TracChangeset for help on using the changeset viewer.