Skip to content

Commit

Permalink
(3.25) Fixed occasional empty files due to excessive cropping (issues #…
Browse files Browse the repository at this point in the history
  • Loading branch information
altmany committed Mar 16, 2022
1 parent f5f828d commit b78c71d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 6 additions & 4 deletions export_fig.m
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
% 02/03/22: (3.22) Fixed small potential memory leak during screen-capture; expanded exportgraphics message for vector exports; fixed rotated tick labels on R2021a+
% 02/03/22: (3.23) Added -toolbar and -menubar options to add figure toolbar/menubar items for interactive figure export (issue #73); fixed edge-case bug with GIF export
% 14/03/22: (3.24) Added support for specifying figure name in addition to handle; added warning when trying to export TIF/JPG/BMP with transparency; use current figure as default handle even when its HandleVisibility is not 'on'
% 16/03/22: (3.25) Fixed occasional empty files due to excessive cropping (issues #318, #350, #351)
%}

if nargout
Expand All @@ -352,7 +353,7 @@

% Ensure the figure is rendered correctly _now_ so that properties like axes limits are up-to-date
drawnow;
pause(0.02); % this solves timing issues with Java Swing's EDT (http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem)
pause(0.05); % this solves timing issues with Java Swing's EDT (http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem)

% Display promo (just once every 10 days!)
persistent promo_time
Expand Down Expand Up @@ -380,14 +381,14 @@
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});

% Check for newer version and exportgraphics/copygraphics compatibility
currentVersion = 3.24;
currentVersion = 3.25;
if options.version % export_fig's version requested - return it and bail out
imageData = currentVersion;
return
end
if ~options.silent
% Check for newer version (not too often)
checkForNewerVersion(3.24); % ...(currentVersion) is better but breaks in version 3.05- due to regexp limitation in checkForNewerVersion()
checkForNewerVersion(3.25); % ...(currentVersion) is better but breaks in version 3.05- due to regexp limitation in checkForNewerVersion()

% Hint to users to use exportgraphics/copygraphics in certain cases
alertForExportOrCopygraphics(options);
Expand Down Expand Up @@ -812,6 +813,7 @@
imwrite(img, [options.name '.tif'], 'Resolution',options.magnify*get(0,'ScreenPixelsPerInch'), 'WriteMode',append_mode{options.append+1}, format_options{:});
end
if options.gif
% TODO - merge contents with im2gif.m
% Convert to color-map image required by GIF specification
[img, map] = rgb2ind(A, 256);
% Handle the case of trying to append to non-existing GIF file
Expand Down Expand Up @@ -918,7 +920,7 @@
end
if ~options.crop
% Issue #56: due to internal bugs in Matlab's print() function, we can't use its internal cropping mechanism,
% therefore we always use '-loose' (in print2eps.m) and do our own cropping (in crop_borders)
% therefore we always use '-loose' (in print2eps.m) and do our own cropping (with crop_borders.m)
%printArgs{end+1} = '-loose';
end
if any(strcmpi(varargin,'-depsc'))
Expand Down
12 changes: 10 additions & 2 deletions print2eps.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function print2eps(name, fig, export_options, varargin)
% 11/03/21: Added documentation about export_options.regexprep; added sanity check (issue #324)
% 21/07/21: Fixed misleading warning message about regexprep field when it's empty (issue #338)
% 26/08/21: Added a short pause to avoid unintended image cropping (issue #318)
% 16/03/22: Fixed occasional empty files due to excessive cropping (issues #350, #351)
%}

options = {'-loose'};
Expand Down Expand Up @@ -325,7 +326,7 @@ function print2eps(name, fig, export_options, varargin)
end

% Ensure that everything is fully rendered, to avoid cropping (issue #318)
drawnow; pause(0.01);
drawnow; pause(0.02);

% Print to eps file
print(fig, options{:}, name);
Expand Down Expand Up @@ -540,9 +541,16 @@ function print2eps(name, fig, export_options, varargin)

% 2. Create a bitmap image and use crop_borders to create the relative
% bb with respect to the PageBoundingBox
drawnow; pause(0.02); % avoid unintended cropping (issue #318)
drawnow; pause(0.05); % avoid unintended cropping (issue #318)
[A, bcol] = print2array(fig, 1, renderer);
[aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts); %#ok<ASGLU>
if any(bb_rel>1) || any(bb_rel<=0) % invalid cropping - retry after prolonged pause
pause(0.15); % avoid unintended cropping (issues #350, #351)
[A, bcol] = print2array(fig, 1, renderer);
[aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts); %#ok<ASGLU>
end
bb_rel(bb_rel>1) = 1; % ignore invalid values
bb_rel(bb_rel<=0) = 1; % ignore invalid values

try set(hTitles,'Color','k'); catch, end

Expand Down

0 comments on commit b78c71d

Please sign in to comment.