ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
print2eps.m
Go to the documentation of this file.
1function print2eps(name, fig, export_options, varargin)
2%PRINT2EPS Prints figures to eps with improved line styles
3%
4% Examples:
5% print2eps filename
6% print2eps(filename, fig_handle)
7% print2eps(filename, fig_handle, export_options)
8% print2eps(filename, fig_handle, export_options, print_options)
9%
10% This function saves a figure as an eps file, with two improvements over
11% MATLAB's print command. First, it improves the line style, making dashed
12% lines more like those on screen and giving grid lines a dotted line style.
13% Secondly, it substitutes original font names back into the eps file,
14% where these have been changed by MATLAB, for up to 11 different fonts.
15%
16% Inputs:
17% filename - string containing the name (optionally including full or
18% relative path) of the file the figure is to be saved as. A
19% ".eps" extension is added if not there already. If a path is
20% not specified, the figure is saved in the current directory.
21% fig_handle - The handle of the figure to be saved. Default: gcf().
22% export_options - array or struct of optional values:
23% bb_padding - Scalar value of amount of padding to add to border around
24% the cropped image, in points (if >1) or percent (if <1).
25% Can be negative as well as positive; Default: 0
26% crop - Cropping flag. Deafult: 0
27% fontswap - Whether to swap non-default fonts in figure. Default: true
28% preserve_size - Whether to preserve the figure's PaperSize. Default: false
29% font_space - Character used to separate font-name terms in the EPS output
30% e.g. "Courier New" => "Courier-New". Default: ''
31% (available only via the struct alternative)
32% renderer - Renderer used to generate bounding-box. Default: 'opengl'
33% (available only via the struct alternative)
34% crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
35% (available only via the struct alternative)
36% regexprep - 2-element cell-array of regular-expression replacement in the
37% generated EPS. 1st element is the replaced string(s), 2nd is
38% the replacement(s) (available only via the struct alternative)
39% print_options - Additional parameter strings to be passed to the print command
40
41%{
42% Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
43
44% The idea of editing the EPS file to change line styles comes from Jiro
45% Doke's FIXPSLINESTYLE (fex id: 17928)
46% The idea of changing dash length with line width came from comments on
47% fex id: 5743, but the implementation is mine :)
48%}
49%{
50% 14/11/11: Fix a MATLAB bug rendering black or white text incorrectly.
51% Thanks to Mathieu Morlighem for reporting the issue and
52% obtaining a fix from TMW.
53% 08/12/11: Added ability to correct fonts. Several people have requested
54% this at one time or another, and also pointed me to printeps
55% (fex id: 7501), so thank you to them. My implementation (which
56% was not inspired by printeps - I'd already had the idea for my
57% approach) goes slightly further in that it allows multiple
58% fonts to be swapped.
59% 14/12/11: Fix bug affecting font names containing spaces. Thanks to David
60% Szwer for reporting the issue.
61% 25/01/12: Add a font not to be swapped. Thanks to Anna Rafferty and Adam
62% Jackson for reporting the issue. Also fix a bug whereby using a
63% font alias can lead to another font being swapped in.
64% 10/04/12: Make the font swapping case insensitive.
65% 26/10/12: Set PaperOrientation to portrait. Thanks to Michael Watts for
66% reporting the issue.
67% 26/10/12: Fix issue to do with swapping fonts changing other fonts and
68% sizes we don't want, due to listeners. Thanks to Malcolm Hudson
69% for reporting the issue.
70% 22/03/13: Extend font swapping to axes labels. Thanks to Rasmus Ischebeck
71% for reporting the issue.
72% 23/07/13: Bug fix to font swapping. Thanks to George for reporting the
73% issue.
74% 13/08/13: Fix MATLAB feature of not exporting white lines correctly.
75% Thanks to Sebastian Hesslinger for reporting it.
76% 24/02/15: Fix for Matlab R2014b bug (issue #31): LineWidths<0.75 are not
77% set in the EPS (default line width is used)
78% 25/02/15: Fixed issue #32: BoundingBox problem caused uncropped EPS/PDF files
79% 05/03/15: Fixed issue #43: Inability to perform EPS file post-processing
80% 06/03/15: Improved image padding & cropping thanks to Oscar Hartogensis
81% 21/03/15: Fixed edge-case of missing handles having a 'FontName' property
82% 26/03/15: Attempt to fix issue #45: white lines in subplots do not print correctly
83% 27/03/15: Attempt to fix issue #44: white artifact lines appearing in patch exports
84% 30/03/15: Fixed issue #52: improved performance on HG2 (R2014b+)
85% 09/04/15: Comment blocks consolidation and minor code cleanup (no real code change)
86% 12/04/15: Fixed issue #56: bad cropping
87% 14/04/15: Workaround for issue #45: lines in image subplots are exported in invalid color
88% 07/07/15: Added option to avoid font-swapping in EPS/PDF
89% 07/07/15: Fixed issue #83: use numeric handles in HG1
90% 22/07/15: Fixed issue #91 (thanks to Carlos Moffat)
91% 28/09/15: Fixed issue #108 (thanks to JacobD10)
92% 01/11/15: Fixed issue #112: optional renderer for bounding-box computation (thanks to Jesús Pestana Puerta)
93% 21/02/16: Enabled specifying non-automated crop amounts
94% 22/02/16: Better support + backward compatibility for transparency (issue #108)
95% 10/06/16: Fixed issue #159: text handles get cleared by Matlab in the print() command
96% 12/06/16: Improved the fix for issue #159 (in the previous commit)
97% 12/06/16: Fixed issue #158: transparent patch color in PDF/EPS
98% 18/09/17: Fixed issue #194: incorrect fonts in EPS/PDF output
99% 18/09/17: Fixed issue #195: relaxed too-tight cropping in EPS/PDF
100% 14/11/17: Workaround for issue #211: dashed/dotted lines in 3D axes appear solid
101% 15/11/17: Updated issue #211: only set SortMethod='ChildOrder' in HG2, and when it looks the same onscreen; support multiple figure axes
102% 18/11/17: Fixed issue #225: transparent/translucent dashed/dotted lines appear solid in EPS/PDF
103% 24/03/18: Fixed issue #239: black title meshes with temporary black background figure bgcolor, causing bad cropping
104% 21/03/19: Improvement for issue #258: missing fonts in output EPS/PDF (still *NOT* fully solved)
105% 21/03/19: Fixed issues #166,#251: Arial font is no longer replaced with Helvetica but rather treated as a non-standard user font
106% 14/05/19: Made Helvetica the top default font-swap, replacing Courier
107% 12/06/19: Issue #277: Enabled preservation of figure's PaperSize in output PDF/EPS file
108% 06/08/19: Issue #281: only fix patch/textbox color if it's not opaque
109% 15/01/20: Added warning ID for easier suppression by users
110% 20/01/20: Added comment about unsupported patch transparency in some Ghostscript versions (issue #285)
111% 10/12/20: Enabled user-specified regexp replacements in the generated EPS file (issue #324)
112% 11/03/21: Added documentation about export_options.regexprep; added sanity check (issue #324)
113% 21/07/21: Fixed misleading warning message about regexprep field when it's empty (issue #338)
114% 26/08/21: Added a short pause to avoid unintended image cropping (issue #318)
115% 16/03/22: Fixed occasional empty files due to excessive cropping (issues #350, #351)
116% 15/05/22: Fixed EPS bounding box (issue #356)
117% 13/04/23: Reduced (hopefully fixed) unintended EPS/PDF image cropping (issues #97, #318)
118%}
119
120 options = {'-loose'};
121 if nargin > 3
122 options = [options varargin];
123 elseif nargin < 3
124 export_options = 0;
125 if nargin < 2
126 fig = gcf();
127 end
128 end
129
130 % Retrieve padding, crop & font-swap values
131 crop_amounts = nan(1,4); % auto-crop all 4 sides by default
132 if isstruct(export_options)
133 try preserve_size = export_options.preserve_size; catch, preserve_size = false; end
134 try fontswap = export_options.fontswap; catch, fontswap = true; end
135 try font_space = export_options.font_space; catch, font_space = ''; end
136 font_space(2:end) = '';
137 try bb_crop = export_options.crop; catch, bb_crop = 0; end
138 try crop_amounts = export_options.crop_amounts; catch, end
139 try bb_padding = export_options.bb_padding; catch, bb_padding = 0; end
140 try renderer = export_options.rendererStr; catch, renderer = 'opengl'; end % fix for issue #110
141 if renderer(1)~='-', renderer = ['-' renderer]; end
142 else
143 if numel(export_options) > 3 % preserve_size
144 preserve_size = export_options(4);
145 else
146 preserve_size = false;
147 end
148 if numel(export_options) > 2 % font-swapping
149 fontswap = export_options(3);
150 else
151 fontswap = true;
152 end
153 if numel(export_options) > 1 % cropping
154 bb_crop = export_options(2);
155 else
156 bb_crop = 0; % scalar value, so use default bb_crop value of 0
157 end
158 if numel(export_options) > 0 % padding
159 bb_padding = export_options(1);
160 else
161 bb_padding = 0;
162 end
163 renderer = '-opengl';
164 font_space = '';
165 end
166
167 % Construct the filename
168 if numel(name) < 5 || ~strcmpi(name(end-3:end), '.eps')
169 name = [name '.eps']; % Add the missing extension
170 end
171
172 % Set paper size
173 old_pos_mode = get(fig, 'PaperPositionMode');
174 old_orientation = get(fig, 'PaperOrientation');
175 old_paper_units = get(fig, 'PaperUnits');
176 set(fig, 'PaperPositionMode','auto', 'PaperOrientation','portrait', 'PaperUnits','points');
177
178 % Find all the used fonts in the figure
179 font_handles = findall(fig, '-property', 'FontName');
180 fonts = get(font_handles, 'FontName');
181 if isempty(fonts)
182 fonts = {};
183 elseif ~iscell(fonts)
184 fonts = {fonts};
185 end
186
187 % Map supported font aliases onto the correct name
188 fontsl = lower(fonts);
189 for a = 1:numel(fonts)
190 f = fontsl{a};
191 f(f==' ') = [];
192 switch f
193 case {'times', 'timesnewroman', 'times-roman'}
194 fontsl{a} = 'times';
195 %case {'arial', 'helvetica'} % issues #166, #251
196 % fontsl{a} = 'helvetica';
197 case {'newcenturyschoolbook', 'newcenturyschlbk'}
198 fontsl{a} = 'newcenturyschlbk';
199 otherwise
200 end
201 end
202 fontslu = unique(fontsl);
203
204 % Determine the font swap table
205 if fontswap
206 % Issue #258: Rearrange standard fonts list based on decending "problematicness"
207 % The issue is still *NOT* fully solved because I cannot figure out how to force
208 % the EPS postscript engine to look for the user's font on disk
209 % Also see: https://stat.ethz.ch/pipermail/r-help/2005-January/064374.html
210 matlab_fonts = {'Helvetica', 'Times', 'Courier', 'Symbol', 'ZapfDingbats', ...
211 'Palatino', 'Bookman', 'ZapfChancery', 'AvantGarde', ...
212 'NewCenturySchlbk', 'Helvetica-Narrow'};
213 matlab_fontsl = lower(matlab_fonts);
214 require_swap = find(~ismember(fontslu, matlab_fontsl));
215 unused_fonts = find(~ismember(matlab_fontsl, fontslu));
216 font_swap = cell(3, min(numel(require_swap), numel(unused_fonts)));
217 fonts_new = fonts;
218 for a = 1:size(font_swap, 2)
219 font_swap{1,a} = find(strcmp(fontslu{require_swap(a)}, fontsl));
220 font_swap{2,a} = matlab_fonts{unused_fonts(a)};
221 font_swap{3,a} = fonts{font_swap{1,a}(1)};
222 fonts_new(font_swap{1,a}) = font_swap(2,a);
223 end
224 else
225 font_swap = [];
226 end
227
228 % Swap the fonts
229 if ~isempty(font_swap)
230 fonts_size = get(font_handles, 'FontSize');
231 if iscell(fonts_size)
232 fonts_size = cell2mat(fonts_size);
233 end
234 M = false(size(font_handles));
235
236 % Loop because some changes may not stick first time, due to listeners
237 c = 0;
238 update = zeros(1000, 1);
239 for b = 1:10 % Limit number of loops to avoid infinite loop case
240 for a = 1:numel(M)
241 M(a) = ~isequal(get(font_handles(a), 'FontName'), fonts_new{a}) || ~isequal(get(font_handles(a), 'FontSize'), fonts_size(a));
242 if M(a)
243 set(font_handles(a), 'FontName', fonts_new{a}, 'FontSize', fonts_size(a));
244 c = c + 1;
245 update(c) = a;
246 end
247 end
248 if ~any(M)
249 break;
250 end
251 end
252
253 % Compute the order to revert fonts later, without the need of a loop
254 [update, M] = unique(update(1:c));
255 [dummy, M] = sort(M); %#ok<ASGLU>
256 update = reshape(update(M), 1, []);
257 end
258
259 % MATLAB bug fix - black and white text can come out inverted sometimes
260 % Find the white and black text
261 black_text_handles = findall(fig, 'Type', 'text', 'Color', [0 0 0]);
262 white_text_handles = findall(fig, 'Type', 'text', 'Color', [1 1 1]);
263 % Set the font colors slightly off their correct values
264 set(black_text_handles, 'Color', [0 0 0] + eps);
265 set(white_text_handles, 'Color', [1 1 1] - eps);
266
267 % MATLAB bug fix - white lines can come out funny sometimes
268 % Find the white lines
269 white_line_handles = findall(fig, 'Type', 'line', 'Color', [1 1 1]);
270 % Set the line color slightly off white
271 set(white_line_handles, 'Color', [1 1 1] - 0.00001);
272
273 % MATLAB bug fix (issue #211): dashed/dotted lines in 3D axes appear solid
274 % Note: this "may limit other functionality in plotting such as hidden line/surface removal"
275 % reference: Technical Support Case #02838114, https://mail.google.com/mail/u/0/#inbox/15fb7659f70e7bd8
276 hAxes = findall(fig, 'Type', 'axes');
277 if using_hg2 && ~isempty(hAxes) % issue #211 presumably happens only in HG2, not HG1
278 try
279 % If there are any axes using SortMethod~='ChildOrder'
280 oldSortMethods = get(hAxes,{'SortMethod'}); % use {'SortMethod'} to ensure we get a cell array, even for single axes
281 if any(~strcmpi('ChildOrder',oldSortMethods)) % i.e., any oldSortMethods=='depth'
282 % Check if the axes look visually different onscreen when SortMethod='ChildOrder'
283 imgBefore = print2array(fig);
284 set(hAxes,'SortMethod','ChildOrder');
285 imgAfter = print2array(fig);
286 if isequal(imgBefore, imgAfter)
287 % They look the same, so use SortMethod='ChildOrder' when generating the EPS
288 else
289 % They look different, so revert SortMethod and issue a warning message
290 warning('YMA:export_fig:issue211', ...
291 ['You seem to be using axes that have overlapping/hidden graphic elements. ' 10 ...
292 'Setting axes.SortMethod=''ChildOrder'' may solve potential problems in EPS/PDF export. ' 10 ...
293 'Additional info: https://github.com/altmany/export_fig/issues/211'])
294 set(hAxes,{'SortMethod'},oldSortMethods);
295 end
296 end
297 catch err
298 % ignore
299 a=err; %#ok<NASGU> % debug breakpoint
300 end
301 end
302
303 % Workaround for issue #45: lines in image subplots are exported in invalid color
304 % In this case the -depsc driver solves the problem, but then all the other workarounds
305 % below (for all the other issues) will fail, so it's better to let the user decide by
306 % just issuing a warning and accepting the '-depsc' input parameter
307 epsLevel2 = ~any(strcmpi(options,'-depsc'));
308 if epsLevel2
309 % Use -depsc2 (EPS color level-2) if -depsc (EPS color level-3) was not specifically requested
310 options{end+1} = '-depsc2';
311 % Issue a warning if multiple images & lines were found in the figure, and HG1 with painters renderer is used
312 isPainters = any(strcmpi(options,'-painters'));
313 if isPainters && ~using_hg2 && numel(findall(fig,'Type','image'))>1 && ~isempty(findall(fig,'Type','line'))
314 warning('YMA:export_fig:issue45', ...
315 ['Multiple images & lines detected. In such cases, the lines might \n' ...
316 'appear with an invalid color due to an internal MATLAB bug (fixed in R2014b). \n' ...
317 'Possible workaround: add a ''-depsc'' or ''-opengl'' parameter to the export_fig command.']);
318 end
319 end
320
321 % Fix issue #83: use numeric handles in HG1
322 if ~using_hg2(fig), fig = double(fig); end
323
324 % Workaround for when transparency is lost through conversion fig>EPS>PDF (issue #108)
325 % Replace transparent patch RGB values with an ID value (rare chance that ID color is being used already)
326 if using_hg2
327 origAlphaColors = eps_maintainAlpha(fig);
328 end
329
330 % Ensure that everything is fully rendered, to avoid cropping (issue #318)
331 drawnow; pause(0.05);
332
333 % Print to eps file
334 print(fig, options{:}, name);
335
336 % Restore the original axes SortMethods (if updated)
337 try set(hAxes,{'SortMethod'},oldSortMethods); catch, end
338
339 % Do post-processing on the eps file
340 try
341 % Read the EPS file into memory
342 fstrm = read_write_entire_textfile(name);
343 catch
344 fstrm = '';
345 end
346
347 % Restore colors for transparent patches/lines and apply the
348 % setopacityalpha setting in the EPS file (issue #108)
349 if using_hg2
350 [~,fstrm,foundFlags] = eps_maintainAlpha(fig, fstrm, origAlphaColors);
351
352 % If some of the transparencies were not found in the EPS file, then rerun the
353 % export with only the found transparencies modified (backward compatibility)
354 if ~isempty(fstrm) && ~all(foundFlags)
355 foundIdx = find(foundFlags);
356 for objIdx = 1 : sum(foundFlags)
357 colorsIdx = foundIdx(objIdx);
358 colorsData = origAlphaColors{colorsIdx};
359 hObj = colorsData{1};
360 propName = colorsData{2};
361 newColor = colorsData{4};
362 hObj.(propName).ColorData = newColor;
363 end
364 delete(name);
365 print(fig, options{:}, name);
366 fstrm = read_write_entire_textfile(name);
367 [~,fstrm] = eps_maintainAlpha(fig, fstrm, origAlphaColors(foundFlags));
368 end
369 end
370
371 % Bail out if EPS post-processing is not possible
372 if isempty(fstrm)
373 warning('YMA:export_fig:EPS','Loading EPS file failed, so unable to perform post-processing. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
374 return
375 end
376
377 % Fix for Matlab R2014b bug (issue #31): LineWidths<0.75 are not set in the EPS (default line width is used)
378 try
379 if using_hg2(fig)
380 % Convert miter joins to line joins
381 %fstrm = regexprep(fstrm, '\n10.0 ML\n', '\n1 LJ\n');
382 % This is faster (the original regexprep could take many seconds when the axes contains many lines):
383 fstrm = strrep(fstrm, sprintf('\n10.0 ML\n'), sprintf('\n1 LJ\n'));
384
385 % In HG2, grid lines and axes Ruler Axles have a default LineWidth of 0.5 => replace en-bulk (assume that 1.0 LineWidth = 1.333 LW)
386 % hAxes=gca; hAxes.YGridHandle.LineWidth, hAxes.YRuler.Axle.LineWidth
387 %fstrm = regexprep(fstrm, '(GC\n2 setlinecap\n1 LJ)\nN', '$1\n0.667 LW\nN');
388 % This is faster:
389 fstrm = strrep(fstrm, sprintf('GC\n2 setlinecap\n1 LJ\nN'), sprintf('GC\n2 setlinecap\n1 LJ\n0.667 LW\nN'));
390
391 % This is more accurate but *MUCH* slower (issue #52)
392 %{
393 % Modify all thin lines in the figure to have 10x LineWidths
394 hLines = findall(fig,'Type','line');
395 hThinLines = [];
396 for lineIdx = 1 : numel(hLines)
397 thisLine = hLines(lineIdx);
398 if thisLine.LineWidth < 0.75 && strcmpi(thisLine.Visible,'on')
399 hThinLines(end+1) = thisLine; %#ok<AGROW>
400 thisLine.LineWidth = thisLine.LineWidth * 10;
401 end
402 end
403
404 % If any thin lines were found
405 if ~isempty(hThinLines)
406 % Prepare an EPS with large-enough line widths
407 print(fig, options{:}, name);
408 % Restore the original LineWidths in the figure
409 for lineIdx = 1 : numel(hThinLines)
410 thisLine = handle(hThinLines(lineIdx));
411 thisLine.LineWidth = thisLine.LineWidth / 10;
412 end
413
414 % Compare the original and the new EPS files and correct the original stream's LineWidths
415 fstrm_new = read_write_entire_textfile(name);
416 idx = 500; % skip heading with its possibly-different timestamp
417 markerStr = sprintf('10.0 ML\nN');
418 markerLen = length(markerStr);
419 while ~isempty(idx) && idx < length(fstrm)
420 lastIdx = min(length(fstrm), length(fstrm_new));
421 delta = fstrm(idx+1:lastIdx) - fstrm_new(idx+1:lastIdx);
422 idx = idx + find(delta,1);
423 if ~isempty(idx) && ...
424 isequal(fstrm(idx-markerLen+1:idx), markerStr) && ...
425 ~isempty(regexp(fstrm_new(idx-markerLen+1:idx+12),'10.0 ML\n[\d\.]+ LW\nN')) %#ok<RGXP1>
426 value = str2double(regexprep(fstrm_new(idx:idx+12),' .*',''));
427 if isnan(value), break; end % something's wrong... - bail out
428 newStr = sprintf('%0.3f LW\n',value/10);
429 fstrm = [fstrm(1:idx-1) newStr fstrm(idx:end)];
430 idx = idx + 12;
431 else
432 break;
433 end
434 end
435 end
436 %}
437
438 % This is much faster although less accurate: fix all non-gray lines to have a LineWidth of 0.75 (=1 LW)
439 % Note: This will give incorrect LineWidth of 0.75 for lines having LineWidth<0.75, as well as for non-gray grid-lines (if present)
440 % However, in practice these edge-cases are very rare indeed, and the difference in LineWidth should not be noticeable
441 %fstrm = regexprep(fstrm, '([CR]C\n2 setlinecap\n1 LJ)\nN', '$1\n1 LW\nN');
442 % This is faster (the original regexprep could take many seconds when the axes contains many lines):
443 fstrm = strrep(fstrm, sprintf('\n2 setlinecap\n1 LJ\nN'), sprintf('\n2 setlinecap\n1 LJ\n1 LW\nN'));
444 end
445 catch err
446 fprintf(2, 'Error fixing LineWidths in EPS file: %s\n at %s:%d\n', err.message, err.stack(1).file, err.stack(1).line);
447 end
448
449 % Reset the font and line colors
450 try
451 set(black_text_handles, 'Color', [0 0 0]);
452 set(white_text_handles, 'Color', [1 1 1]);
453 catch
454 % Fix issue #159: redo findall() '*text_handles'
455 black_text_handles = findall(fig, 'Type', 'text', 'Color', [0 0 0]+eps);
456 white_text_handles = findall(fig, 'Type', 'text', 'Color', [1 1 1]-eps);
457 set(black_text_handles, 'Color', [0 0 0]);
458 set(white_text_handles, 'Color', [1 1 1]);
459 end
460 set(white_line_handles, 'Color', [1 1 1]);
461
462 % Preserve the figure's PaperSize in the output file, if requested (issue #277)
463 if preserve_size
464 % https://stackoverflow.com/questions/19646329/postscript-document-size
465 paper_size = get(fig, 'PaperSize'); % in [points]
466 fstrm = sprintf('<< /PageSize [%d %d] >> setpagedevice\n%s', paper_size, fstrm);
467 end
468
469 % Reset paper size
470 set(fig, 'PaperPositionMode',old_pos_mode, 'PaperOrientation',old_orientation, 'PaperUnits',old_paper_units);
471
472 % Reset the font names in the figure
473 if ~isempty(font_swap)
474 for a = update
475 set(font_handles(a), 'FontName', fonts{a}, 'FontSize', fonts_size(a));
476 end
477
478 for a = 1:size(font_swap, 2)
479 fontName = font_swap{3,a};
480 %fontName = fontName(~isspace(font_swap{3,a}));
481 if length(fontName) > 29
482 warning('YMA:export_fig:font_name','Font name ''%s'' is longer than 29 characters. This might cause problems in some EPS/PDF readers. Consider using a different font.',fontName);
483 end
484 if isempty(font_space)
485 fontName(fontName==' ') = '';
486 else
487 fontName(fontName==' ') = char(font_space);
488 end
489
490 % Replace all instances of the standard Matlab fonts with the original user's font names
491 %fstrm = regexprep(fstrm, [font_swap{1,a} '-?[a-zA-Z]*\>'], fontName);
492 %fstrm = regexprep(fstrm, [font_swap{2,a} '([ \n])'], [fontName '$1']);
493 %fstrm = regexprep(fstrm, font_swap{2,a}, fontName); % also replace -Bold, -Italic, -BoldItalic
494
495 % Times-Roman's Bold/Italic fontnames don't include '-Roman'
496 fstrm = regexprep(fstrm, [font_swap{2,a} '(\-Roman)?'], fontName);
497 end
498 end
499
500 % Move the bounding box to the top of the file (HG2 only), or fix the line styles (HG1 only)
501 if using_hg2(fig)
502 % Move the bounding box to the top of the file (HG2 only)
503 [s, e] = regexp(fstrm, '%%BoundingBox: [^%]*%%');
504 if numel(s) == 2
505 fstrm = fstrm([1:s(1)-1 s(2):e(2)-2 e(1)-1:s(2)-1 e(2)-1:end]);
506 end
507 else
508 % Fix the line styles (HG1 only)
509 fstrm = fix_lines(fstrm);
510 end
511
512 % Apply the bounding box padding & cropping, replacing Matlab's print()'s bounding box
513 if bb_crop
514 % Calculate a new bounding box based on a bitmap print using crop_border.m
515 % 1. Determine the Matlab BoundingBox and PageBoundingBox
516 [s,e] = regexp(fstrm, '%%BoundingBox: [^%]*%%'); % location BB in eps file
517 if numel(s)==2, s=s(2); e=e(2); end
518 aa = fstrm(s+15:e-3); % dimensions bb - STEP1
519 bb_matlab = cell2mat(textscan(aa,'%f32%f32%f32%f32')); % dimensions bb - STEP2
520
521 [s,e] = regexp(fstrm, '%%PageBoundingBox: [^%]*%%'); % location bb in eps file
522 if numel(s)==2, s=s(2); e=e(2); end
523 aa = fstrm(s+19:e-3); % dimensions bb - STEP1
524 pagebb_matlab = cell2mat(textscan(aa,'%f32%f32%f32%f32')); % dimensions bb - STEP2
525
526 % 1b. Fix issue #239: black title meshes with temporary black background figure bgcolor, causing bad cropping
527 hTitles = [];
528 if isequal(get(fig,'Color'),'none')
529 hAxes = findall(fig,'type','axes');
530 for idx = 1 : numel(hAxes)
531 hAx = hAxes(idx);
532 try
533 hTitle = hAx.Title;
534 oldColor = hTitle.Color;
535 if all(oldColor < 5*eps) || (ischar(oldColor) && lower(oldColor(1))=='k')
536 hTitles(end+1) = hTitle; %#ok<AGROW>
537 hTitle.Color = [0,0,.01];
538 end
539 catch
540 end
541 end
542 end
543
544 % 2. Create a bitmap image and use crop_borders to create the relative
545 % bb with respect to the PageBoundingBox
546 drawnow; pause(0.05); % avoid unintended cropping (issue #318)
547 [A, bcol] = print2array(fig, 1, renderer);
548 [aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts); %#ok<ASGLU>
549 if any(bb_rel>1) || any(bb_rel<=0) || bb_rel(2)>0.15 % invalid cropping - retry after prolonged pause
550 pause(0.15); % avoid unintended cropping (issues #350, #351)
551 [A, bcol] = print2array(fig, 1, renderer);
552 [aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts); %#ok<ASGLU>
553 end
554 bb_rel(bb_rel>1) = 1; % ignore invalid values
555 bb_rel(bb_rel<0) = 1; % ignore invalid values (fix issue #356)
556
557 try set(hTitles,'Color','k'); catch, end
558
559 % 3. Calculate the new Bounding Box
560 pagew = pagebb_matlab(3)-pagebb_matlab(1);
561 pageh = pagebb_matlab(4)-pagebb_matlab(2);
562 %bb_new = [pagebb_matlab(1)+pagew*bb_rel(1) pagebb_matlab(2)+pageh*bb_rel(2) ...
563 % pagebb_matlab(1)+pagew*bb_rel(3) pagebb_matlab(2)+pageh*bb_rel(4)];
564 bb_new = pagebb_matlab([1,2,1,2]) + [pagew,pageh,pagew,pageh].*bb_rel; % clearer
565 bb_offset = (bb_new-bb_matlab) + [-2,-2,2,2]; % 2px margin so that cropping is not TOO tight (issue #195)
566
567 % Apply the bounding box padding
568 if bb_padding
569 if abs(bb_padding)<1
570 bb_padding = round((mean([bb_new(3)-bb_new(1) bb_new(4)-bb_new(2)])*bb_padding)/0.5)*0.5; % ADJUST BB_PADDING
571 end
572 add_padding = @(n1, n2, n3, n4) sprintf(' %.0f', str2double({n1, n2, n3, n4}) + bb_offset + bb_padding*[-1,-1,1,1]); %#ok<NASGU>
573 else
574 add_padding = @(n1, n2, n3, n4) sprintf(' %.0f', str2double({n1, n2, n3, n4}) + bb_offset); %#ok<NASGU> % fix small but noticeable bounding box shift
575 end
576 fstrm = regexprep(fstrm, '%%BoundingBox:[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)[ ]+([-]?\d+)', '%%BoundingBox:${add_padding($1, $2, $3, $4)}');
577 end
578
579 % Fix issue #44: white artifact lines appearing in patch exports
580 % Note: the problem is due to the fact that Matlab's print() function exports patches
581 % as a combination of filled triangles, and a white line appears where the triangles touch
582 % In the workaround below, we will modify such dual-triangles into a filled rectangle.
583 % We are careful to only modify regexps that exactly match specific patterns - it's better to not
584 % correct some white-line artifacts than to change the geometry of a patch, or to corrupt the EPS.
585 % e.g.: '0 -450 937 0 0 450 3 MP PP 937 0 0 -450 0 450 3 MP PP' => '0 -450 937 0 0 450 0 0 4 MP'
586 fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\2 \1 \3 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
587 fstrm = regexprep(fstrm, '\n([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) ([-\d.]+ [-\d.]+) 3 MP\nPP\n\2 \3 \1 3 MP\nPP\n','\n$1 $2 $3 0 0 4 MP\nPP\n');
588 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');
589 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');
590
591 % If user requested a regexprep replacement of string(s), do this now (issue #324)
592 if isstruct(export_options) && isfield(export_options,'regexprep') && ~isempty(export_options.regexprep) %issue #338
593 try
594 oldStrOrRegexp = export_options.regexprep{1};
595 newStrOrRegexp = export_options.regexprep{2};
596 fstrm = regexprep(fstrm, oldStrOrRegexp, newStrOrRegexp);
597 catch err
598 warning('YMA:export_fig:regexprep', 'Error parsing regexprep: %s', err.message);
599 end
600 end
601
602 % Write out the fixed eps file
604
605 drawnow; pause(0.01);
606end
607
608function [StoredColors, fstrm, foundFlags] = eps_maintainAlpha(fig, fstrm, StoredColors)
609 if nargin == 1 % in: convert transparency in Matlab figure into unique RGB colors
610 hObjs = findall(fig); %findobj(fig,'Type','Area');
611 StoredColors = {};
612 propNames = {'Face','Edge'};
613 for objIdx = 1:length(hObjs)
614 hObj = hObjs(objIdx);
615 for propIdx = 1 : numel(propNames)
616 try
617 propName = propNames{propIdx};
618 if strcmp(hObj.(propName).ColorType, 'truecoloralpha')
619 oldColor = hObj.(propName).ColorData;
620 if numel(oldColor)>3 && oldColor(4)~=255 % issue #281: only fix patch/textbox color if it's not opaque
621 nColors = length(StoredColors);
622 newColor = uint8([101; 102+floor(nColors/255); mod(nColors,255); 255]);
623 StoredColors{end+1} = {hObj, propName, oldColor, newColor}; %#ok<AGROW>
624 hObj.(propName).ColorData = newColor;
625 end
626 end
627 catch
628 % Never mind - ignore (either doesn't have the property or cannot change it)
629 end
630 end
631 end
632 else % restore transparency in Matlab figure by converting back from the unique RGBs
633 %Find the transparent patches
634 wasError = false;
635 nColors = length(StoredColors);
636 foundFlags = false(1,nColors);
637 for objIdx = 1 : nColors
638 colorsData = StoredColors{objIdx};
639 hObj = colorsData{1};
640 propName = colorsData{2};
641 origColor = colorsData{3};
642 newColor = colorsData{4};
643 try
644 %Restore the EPS files patch color
645 colorID = num2str(round(double(newColor(1:3)') /255,3),'%.3g %.3g %.3g'); %ID for searching
646 origRGB = num2str(round(double(origColor(1:3)')/255,3),'%.3g %.3g %.3g'); %Replace with original color
647 origAlpha = num2str(round(double(origColor(end)) /255,3),'%.3g'); %Convert alpha value for EPS
648
649 %Find and replace the RGBA values within the EPS text fstrm
650 %Note: .setopacityalpha is an unsupported PS extension that croaks in some GS versions (issue #285, https://bugzilla.redhat.com/show_bug.cgi?id=1632030)
651 % (such cases are caught in eps2pdf.m and corrected by adding the -dNOSAFER Ghosscript option or by removing the .setopacityalpha line)
652 if strcmpi(propName,'Face')
653 oldStr = sprintf(['\n' colorID ' RC\n']); % ...N\n (removed to fix issue #225)
654 newStr = sprintf(['\n' origRGB ' RC\n' origAlpha ' .setopacityalpha true\n']); % ...N\n
655 else %'Edge'
656 oldStr = sprintf(['\n' colorID ' RC\n']); % ...1 LJ\n (removed to fix issue #225)
657 newStr = sprintf(['\n' origRGB ' RC\n' origAlpha ' .setopacityalpha true\n']);
658 end
659 foundFlags(objIdx) = ~isempty(strfind(fstrm, oldStr)); %#ok<STREMP>
660 fstrm = strrep(fstrm, oldStr, newStr);
661
662 %Restore the figure object's original color
663 hObj.(propName).ColorData = origColor;
664 catch err
665 % something is wrong - cannot restore transparent color...
666 if ~wasError
667 fprintf(2, 'Error maintaining transparency in EPS file: %s\n at %s:%d\n', err.message, err.stack(1).file, err.stack(1).line);
668 wasError = true;
669 end
670 end
671 end
672 end
673end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function list()
Return a list of MATLAB strings containing the names of OS platforms supported by the ParaMonte MATLA...
function eps2pdf(in source, in dest, in crop, in append, in gray, in quality, in gs_options)
function find(in vendor)
Return a list of scalar MATLAB strings containing the paths to all detected mpiexec binaries installe...
function info()
Return a MATLAB string containing the current system information.
function eps_maintainAlpha(in fig, in fstrm, in StoredColors)
function print2eps(in name, in fig, in export_options, in varargin)
function read_write_entire_textfile(in fname, in fstrm)
function touch()
Return the contents of the .bash_profile file in the system home folder as a scalar MATLAB string.