Skip to content

Commit

Permalink
(3.14) Enabled user-specified regexp replacements in generated EPS/PD…
Browse files Browse the repository at this point in the history
…F files (issue #324)
  • Loading branch information
altmany committed Dec 9, 2020
1 parent d7671fe commit a6de274
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
30 changes: 20 additions & 10 deletions export_fig.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
% export_fig ... -preserve_size
% export_fig ... -options <optionsStruct>
% export_fig ... -silent
% export_fig ... -regexprep <pattern> <replace>
% export_fig(..., handle)
%
% This function saves a figure or single axes to one or more vector and/or
Expand Down Expand Up @@ -185,6 +186,10 @@
% options.PNG.BitDepth=4. Valid only for PNG,TIF,JPG output formats.
% -silent - option to avoid various warning and informational messages, such
% as version update checks, transparency or renderer issues, etc.
% -regexprep <old> <new> - replaces all occurances of <old> (a regular expression
% string or array of strings; case-sensitive), with the corresponding
% <new> string(s), in EPS/PDF files (only). See regexp function's doc.
% Warning: invalid replacement can make your EPS/PDF file unreadable!
% handle - The handle of the figure, axes or uipanels (can be an array of
% handles, but the objects must be in the same figure) which is
% to be saved. Default: gcf (handle of current figure).
Expand Down Expand Up @@ -317,6 +322,7 @@
% 30/07/20: (3.11) Fixed issue #317 (bug when exporting figure with non-pixels units); Potential solve also of issue #303 (size change upon export)
% 14/08/20: (3.12) Fixed some exportgraphics/copygraphics compatibility messages; Added -silent option to suppress non-critical messages; Reduced promo message display rate to once a week; Added progress messages during export_fig('-update')
% 07/10/20: (3.13) Added version info and change-log links to update message (issue #322); Added -version option to return the current export_fig version; Avoid JavaFrame warning message; Improved exportgraphics/copygraphics infomercial message inc. support of upcoming Matlab R2021a
% 10/12/20: (3.14) Enabled user-specified regexp replacements in generated EPS/PDF files (issue #324)
%}

if nargout
Expand Down Expand Up @@ -345,14 +351,14 @@
[fig, options] = parse_args(nargout, fig, argNames, varargin{:});

% Check for newer version and exportgraphics/copygraphics compatibility
currentVersion = 3.13;
currentVersion = 3.14;
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.13); % ...(currentVersion) is better but breaks in version 3.05- due to regexp limitation in checkForNewerVersion()
checkForNewerVersion(3.14); % ...(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 @@ -1321,6 +1327,7 @@
'format_options', struct, ...
'preserve_size', false, ...
'silent', false, ...
'regexprep', [], ...
'gs_options', {{}});
end

Expand All @@ -1338,10 +1345,10 @@
options.handleName = ''; % default handle name

% Go through the other arguments
skipNext = false;
for a = 1:nargin-3
if skipNext
skipNext = false;
skipNext = 0;
for a = 1:nargin-3 % only process varargin, no other parse_args() arguments
if skipNext > 0
skipNext = skipNext-1;
continue;
end
if all(ishandle(varargin{a}))
Expand Down Expand Up @@ -1415,7 +1422,7 @@
options.fontswap = false;
case 'font_space'
options.font_space = varargin{a+1};
skipNext = true;
skipNext = 1;
case 'linecaps'
options.linecaps = true;
case 'noinvert'
Expand All @@ -1434,9 +1441,12 @@
formatName = regexprep(lower(formats{idx}),{'tiff','jpeg'},{'tif','jpg'});
options.format_options.(formatName) = optionsStruct; %=optionsCells(:)';
end
skipNext = true;
skipNext = 1;
case 'silent'
options.silent = true;
case 'regexprep'
options.regexprep = varargin(a+1:a+2);
skipNext = 2;
otherwise
try
wasError = false;
Expand All @@ -1449,7 +1459,7 @@
error('export_fig:BadOptionValue','option ''%s'' cannot be parsed: only image, bitmap, emf and pdf formats are supported',varargin{a});
end
if numel(varargin{a})==2
skipNext = true;
skipNext = 1;
vals = str2num(varargin{a+1}); %#ok<ST2NM>
else
vals = str2num(varargin{a}(3:end)); %#ok<ST2NM>
Expand All @@ -1466,7 +1476,7 @@
% Issue #51: improved processing of input args (accept space between param name & value)
val = str2double(varargin{a+1});
if isscalar(val) && ~isnan(val)
skipNext = true;
skipNext = 1;
end
end
if ~isscalar(val) || isnan(val)
Expand Down
15 changes: 12 additions & 3 deletions print2eps.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function print2eps(name, fig, export_options, varargin)
% 06/08/19: Issue #281: only fix patch/textbox color if it's not opaque
% 15/01/20: Added warning ID for easier suppression by users
% 20/01/20: Added comment about unsupported patch transparency in some Ghostscript versions (issue #285)
% 10/12/20: Enabled user-specified regexp replacements in the generated EPS file (issue #324)
%}

options = {'-loose'};
Expand Down Expand Up @@ -461,10 +462,7 @@ function print2eps(name, fig, export_options, varargin)
for a = update
set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a));
end
end

% Replace the font names
if ~isempty(font_swap)
for a = 1:size(font_swap, 2)
fontName = font_swap{3,a};
%fontName = fontName(~isspace(font_swap{3,a}));
Expand Down Expand Up @@ -570,6 +568,17 @@ function print2eps(name, fig, export_options, varargin)
fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\3 \1 \2 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\3 \2 \1 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');

% If user requested a regexprep replacement of string(s), do this now (issue #324)
if ~isempty(export_options.regexprep)
try
oldStrOrRegexp = export_options.regexprep{1};
newStrOrRegexp = export_options.regexprep{2};
fstrm = regexprep(fstrm, oldStrOrRegexp, newStrOrRegexp);
catch err
warning('YMA:export_fig:regexprep', 'Error parsing regexprep: %s', err.message);
end
end

% Write out the fixed eps file
read_write_entire_textfile(name, fstrm);
end
Expand Down

0 comments on commit a6de274

Please sign in to comment.