Changeset 21671


Ignore:
Timestamp:
04/12/17 16:35:34 (8 years ago)
Author:
Mathieu Morlighem
Message:

CHG: updated export_fig

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

Legend:

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

    r21315 r21671  
    5656% 04/10/15: Suggest a workaround for issue #41 (missing font path; thanks Mariia Fedotenkova)
    5757% 22/02/16: Bug fix from latest release of this file (workaround for issue #41)
     58% 20/03/17: Added informational message in case of GS croak (issue #186)
    5859
    5960    % Intialise the options string for ghostscript
     
    159160                fprintf(2, '  or maybe the%s option(s) are not accepted by your GS version\n', gs_options);
    160161            end
     162            fprintf(2, '  or maybe you have another gs executable in your system''s path\n');
    161163            fprintf(2, 'Ghostscript options: %s\n\n', orig_options);
    162164            error(message);
  • issm/trunk-jpl/externalpackages/export_fig/export_fig.m

    r21315 r21671  
    2525%   export_fig ... -update
    2626%   export_fig ... -nofontswap
     27%   export_fig ... -linecaps
    2728%   export_fig(..., handle)
    2829%
     
    4344%   - Vector formats: pdf, eps
    4445%   - Bitmap formats: png, tiff, jpg, bmp, export to workspace
     46%   - Rounded line-caps (optional; pdf & eps only)
    4547%
    4648% This function is especially suited to exporting figures for use in
     
    153155%             done in vector formats (only): 11 standard Matlab fonts are
    154156%             replaced by the original figure fonts. This option prevents this.
     157%   -linecaps - option to create rounded line-caps (vector formats only).
    155158%   handle -  The handle of the figure, axes or uipanels (can be an array of
    156159%             handles, but the objects must be in the same figure) to be
     
    241244% 17/05/16: Fixed case of image YData containing more than 2 elements (issue #151)
    242245% 08/08/16: Enabled exporting transparency to TIF, in addition to PNG/PDF (issue #168)
     246% 11/12/16: Added alert in case of error creating output PDF/EPS file (issue #179)
     247% 13/12/16: Minor fix to the commit for issue #179 from 2 days ago
     248% 22/03/17: Fixed issue #187: only set manual ticks when no exponent is present
     249% 09/04/17: Added -linecaps option (idea by Baron Finer, issue #192)
    243250%}
    244251
     
    271278    else
    272279        % Check we have a figure
    273         if ~isequal(get(fig, 'Type'), 'figure');
     280        if ~isequal(get(fig, 'Type'), 'figure')
    274281            error('Handle must be that of a figure, axes or uipanel');
    275282        end
     
    583590                if hasTransparency || hasPatches
    584591                    % This is *MUCH* slower, but more accurate for patches and transparent annotations (issue #39)
    585                     renderer = '-painters';
     592                    renderer = '-painters'; %ISSM fix
    586593                else
    587594                    renderer = '-painters';
     
    655662                eps2pdf(tmp_nam, pdf_nam_tmp, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
    656663                % Ghostscript croaks on % chars in the output PDF file, so use tempname and then rename the file
    657                 try movefile(pdf_nam_tmp, pdf_nam, 'f'); catch, end
     664                try
     665                    % Rename the file (except if it is already the same)
     666                    % Abbie K's comment on the commit for issue #179 (#commitcomment-20173476)
     667                    if ~isequal(pdf_nam_tmp, pdf_nam)
     668                        movefile(pdf_nam_tmp, pdf_nam, 'f');
     669                    end
     670                catch
     671                    % Alert in case of error creating output PDF/EPS file (issue #179)
     672                    if exist(pdf_nam_tmp, 'file')
     673                        error(['Could not create ' pdf_nam ' - perhaps the folder does not exist, or you do not have write permissions']);
     674                    else
     675                        error('Could not generate the intermediary EPS file.');
     676                    end
     677                end
    658678            catch ex
    659679                % Delete the eps
     
    663683            % Delete the eps
    664684            delete(tmp_nam);
    665             if options.eps
     685            if options.eps || options.linecaps
    666686                try
    667687                    % Generate an eps from the pdf
     
    669689                    eps_nam_tmp = strrep(pdf_nam_tmp,'.pdf','.eps');
    670690                    pdf2eps(pdf_nam, eps_nam_tmp);
    671                     movefile(eps_nam_tmp,  [options.name '.eps'], 'f');
     691
     692                    % Issue #192: enable rounded line-caps
     693                    if options.linecaps
     694                        fstrm = read_write_entire_textfile(eps_nam_tmp);
     695                        fstrm = regexprep(fstrm, '[02] J', '1 J');
     696                        read_write_entire_textfile(eps_nam_tmp, fstrm);
     697                        if options.pdf
     698                            eps2pdf(eps_nam_tmp, pdf_nam, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
     699                        end
     700                    end
     701
     702                    if options.eps
     703                        movefile(eps_nam_tmp, [options.name '.eps'], 'f');
     704                    else  % if options.pdf
     705                        try delete(eps_nam_tmp); catch, end
     706                    end
    672707                catch ex
    673708                    if ~options.pdf
     
    856891        'update',       false, ...
    857892        'fontswap',     true, ...
     893        'linecaps',     false, ...
    858894        'gs_options',   {{}});
    859895end
     
    946982                    case 'nofontswap'
    947983                        options.fontswap = false;
     984                    case 'linecaps'
     985                        options.linecaps = true;
    948986                    otherwise
    949987                        try
     
    11611199
    11621200function A = rgb2grey(A)
    1163     A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); %#ok<ZEROLIKE>
     1201    A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); % #ok<ZEROLIKE>
    11641202end
    11651203
     
    12571295        M = {M};
    12581296    end
    1259     M = cellfun(@(c) strcmp(c, 'linear'), M);
    1260     set(Hlims(M), [ax 'TickMode'], 'manual');
    1261     %set(Hlims(M), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
     1297    %idx = cellfun(@(c) strcmp(c, 'linear'), M);
     1298    idx = find(strcmp(M,'linear'));
     1299    %set(Hlims(idx), [ax 'TickMode'], 'manual');  % issue #187
     1300    %set(Hlims(idx), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
     1301    for idx2 = 1 : numel(idx)
     1302        try
     1303            % Fix for issue #187 - only set manual ticks when no exponent is present
     1304            hAxes = Hlims(idx(idx2));
     1305            props = {[ax 'TickMode'],'manual', [ax 'TickLabelMode'],'manual'};
     1306            if isempty(strtrim(hAxes.([ax 'Ruler']).SecondaryLabel.String))
     1307                set(hAxes, props{:});  % no exponent, so update moth ticks and tick labels to manual
     1308            end
     1309        catch  % probably HG1
     1310            set(hAxes, props{:});  % revert back to old behavior
     1311        end
     1312    end
    12621313end
    12631314
  • issm/trunk-jpl/externalpackages/export_fig/pdftops.m

    r21315 r21671  
    3333
    3434    % Call pdftops
    35     [varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd));
     35    [varargout{1:nargout}] = system([xpdf_command(xpdf_path()) cmd]);
    3636end
    3737
     
    138138function good = check_xpdf_path(path_)
    139139    % Check the path is valid
    140     [good, message] = system(sprintf('"%s" -h', path_)); %#ok<ASGLU>
     140    [good, message] = system([xpdf_command(path_) '-h']); %#ok<ASGLU>
    141141    % system returns good = 1 even when the command runs
    142142    % Look for something distinct in the help text
     
    149149    end
    150150end
     151
     152function cmd = xpdf_command(path_)
     153    % Initialize any required system calls before calling ghostscript
     154    % TODO: in Unix/Mac, find a way to determine whether to use "export" (bash) or "setenv" (csh/tcsh)
     155    shell_cmd = '';
     156    if isunix
     157        % Avoids an error on Linux with outdated MATLAB lib files
     158        % R20XXa/bin/glnxa64/libtiff.so.X
     159        % R20XXa/sys/os/glnxa64/libstdc++.so.X
     160        shell_cmd = 'export LD_LIBRARY_PATH=""; ';
     161    end
     162    if ismac
     163        shell_cmd = 'export DYLD_LIBRARY_PATH=""; ';
     164    end
     165    % Construct the command string
     166    cmd = sprintf('%s"%s" ', shell_cmd, path_);
     167end
  • issm/trunk-jpl/externalpackages/export_fig/print2array.m

    r21315 r21671  
    5050% 28/05/15: Fixed issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() func)
    5151% 07/07/15: Fixed issue #83: use numeric handles in HG1
     52% 11/12/16: Fixed cropping issue reported by Harry D.
    5253%}
    5354
     
    102103    end
    103104    if nargin > 2 && strcmp(renderer, '-painters')
    104         % Print to eps file
    105         if isTempDirOk
    106             tmp_eps = [tempname '.eps'];
    107         else
    108             tmp_eps = fullfile(fpath,[fname '.eps']);
    109         end
    110         print2eps(tmp_eps, fig, 0, renderer, '-loose');
     105        % First try to print directly to tif file
    111106        try
    112             % Initialize the command to export to tiff using ghostscript
    113             cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
    114             % Set the font path
    115             fp = font_path();
    116             if ~isempty(fp)
    117                 cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
    118             end
    119             % Add the filenames
    120             cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
    121             % Execute the ghostscript command
    122             ghostscript(cmd_str);
    123         catch me
     107            % Print the file into a temporary TIF file and read it into array A
     108            [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
     109            if err, rethrow(ex); end
     110        catch  % error - try to print to EPS and then using Ghostscript to TIF
     111            % Print to eps file
     112            if isTempDirOk
     113                tmp_eps = [tempname '.eps'];
     114            else
     115                tmp_eps = fullfile(fpath,[fname '.eps']);
     116            end
     117            print2eps(tmp_eps, fig, 0, renderer, '-loose');
     118            try
     119                % Initialize the command to export to tiff using ghostscript
     120                cmd_str = ['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc'];
     121                % Set the font path
     122                fp = font_path();
     123                if ~isempty(fp)
     124                    cmd_str = [cmd_str ' -sFONTPATH="' fp '"'];
     125                end
     126                % Add the filenames
     127                cmd_str = [cmd_str ' -sOutputFile="' tmp_nam '" "' tmp_eps '"' gs_options];
     128                % Execute the ghostscript command
     129                ghostscript(cmd_str);
     130            catch me
     131                % Delete the intermediate file
     132                delete(tmp_eps);
     133                rethrow(me);
     134            end
    124135            % Delete the intermediate file
    125136            delete(tmp_eps);
    126             rethrow(me);
    127         end
    128         % Delete the intermediate file
    129         delete(tmp_eps);
    130         % Read in the generated bitmap
    131         A = imread(tmp_nam);
    132         % Delete the temporary bitmap file
    133         delete(tmp_nam);
     137            % Read in the generated bitmap
     138            A = imread(tmp_nam);
     139            % Delete the temporary bitmap file
     140            delete(tmp_nam);
     141        end
    134142        % Set border pixels to the correct colour
    135143        if isequal(bcol, 'none')
     
    168176            renderer = '-opengl';
    169177        end
    170         err = false;
    171         % Set paper size
    172         old_pos_mode = get(fig, 'PaperPositionMode');
    173         old_orientation = get(fig, 'PaperOrientation');
    174         set(fig, 'PaperPositionMode', 'auto', 'PaperOrientation', 'portrait');
    175         try
    176             % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
    177             fp = [];  % in case we get an error below
    178             fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
    179             set(fp, 'LineWidth',0.5);
    180             % Fix issue #83: use numeric handles in HG1
    181             if ~using_hg2(fig),  fig = double(fig);  end
    182             % Print to tiff file
    183             print(fig, renderer, res_str, '-dtiff', tmp_nam);
    184             % Read in the printed file
    185             A = imread(tmp_nam);
    186             % Delete the temporary file
    187             delete(tmp_nam);
    188         catch ex
    189             err = true;
    190         end
    191         set(fp, 'LineWidth',0.75);  % restore original figure appearance
    192         % Reset paper size
    193         set(fig, 'PaperPositionMode', old_pos_mode, 'PaperOrientation', old_orientation);
     178        % Print the file into a temporary TIF file and read it into array A
     179        [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam);
    194180        % Throw any error that occurred
    195181        if err
     
    220206end
    221207
     208% Function to create a TIF image of the figure and read it into an array
     209function [A, err, ex] = read_tif_img(fig, res_str, renderer, tmp_nam)
     210    err = false;
     211    ex = [];
     212    % Temporarily set the paper size
     213    old_pos_mode    = get(fig, 'PaperPositionMode');
     214    old_orientation = get(fig, 'PaperOrientation');
     215    set(fig, 'PaperPositionMode','auto', 'PaperOrientation','portrait');
     216    try
     217        % Workaround for issue #69: patches with LineWidth==0.75 appear wide (internal bug in Matlab's print() function)
     218        fp = [];  % in case we get an error below
     219        fp = findall(fig, 'Type','patch', 'LineWidth',0.75);
     220        set(fp, 'LineWidth',0.5);
     221        % Fix issue #83: use numeric handles in HG1
     222        if ~using_hg2(fig),  fig = double(fig);  end
     223        % Print to tiff file
     224        print(fig, renderer, res_str, '-dtiff', tmp_nam);
     225        % Read in the printed file
     226        A = imread(tmp_nam);
     227        % Delete the temporary file
     228        delete(tmp_nam);
     229    catch ex
     230        err = true;
     231    end
     232    set(fp, 'LineWidth',0.75);  % restore original figure appearance
     233    % Reset the paper size
     234    set(fig, 'PaperPositionMode',old_pos_mode, 'PaperOrientation',old_orientation);
     235end
     236
    222237% Function to return (and create, where necessary) the font path
    223238function fp = font_path()
  • issm/trunk-jpl/externalpackages/export_fig/print2eps.m

    r21315 r21671  
    2727%       fontswap   - Whether to swap non-default fonts in figure. Default: true
    2828%       renderer   - Renderer used to generate bounding-box. Default: 'opengl'
     29%                    (available only via the struct alternative)
    2930%       crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
    3031%                    (available only via the struct alternative)
Note: See TracChangeset for help on using the changeset viewer.