ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
fix_lines.m
Go to the documentation of this file.
1%FIX_LINES Improves the line style of eps files generated by print
2%
3% Examples:
4% fix_lines fname
5% fix_lines fname fname2
6% fstrm_out = fixlines(fstrm_in)
7%
8% This function improves the style of lines in eps files generated by
9% MATLAB's print function, making them more similar to those seen on
10% screen. Grid lines are also changed from a dashed style to a dotted
11% style, for greater differentiation from dashed lines.
12%
13% The function also places embedded fonts after the postscript header, in
14% versions of MATLAB which place the fonts first (R2006b and earlier), in
15% order to allow programs such as Ghostscript to find the bounding box
16% information.
17%
18%IN:
19% fname - Name or path of source eps file.
20% fname2 - Name or path of destination eps file. Default: same as fname.
21% fstrm_in - File contents of a MATLAB-generated eps file.
22%
23%OUT:
24% fstrm_out - Contents of the eps file with line styles fixed.
25
26% Copyright: (C) Oliver Woodford, 2008-2014
27
28% The idea of editing the EPS file to change line styles comes from Jiro
29% Doke's FIXPSLINESTYLE (fex id: 17928)
30% The idea of changing dash length with line width came from comments on
31% fex id: 5743, but the implementation is mine :)
32
33% Thank you to Sylvain Favrot for bringing the embedded font/bounding box
34% interaction in older versions of MATLAB to my attention.
35% Thank you to D Ko for bringing an error with eps files with tiff previews
36% to my attention.
37% Thank you to Laurence K for suggesting the check to see if the file was
38% opened.
39
40% 01/03/15: Issue #20: warn users if using this function in HG2 (R2014b+)
41% 27/03/15: Fixed out of memory issue with enormous EPS files (generated by print() with OpenGL renderer), related to issue #39
42
43function fstrm = fix_lines(fstrm, fname2)
44
45% Issue #20: warn users if using this function in HG2 (R2014b+)
46if using_hg2
47 warning('export_fig:hg2','The fix_lines function should not be used in this Matlab version.');
48end
49
50if nargout == 0 || nargin > 1
51 if nargin < 2
52 % Overwrite the input file
53 fname2 = fstrm;
54 end
55 % Read in the file
56 fstrm = read_write_entire_textfile(fstrm);
57end
58
59% Move any embedded fonts after the postscript header
60if strcmp(fstrm(1:15), '%!PS-AdobeFont-')
61 % Find the start and end of the header
62 ind = regexp(fstrm, '[\n\r]%!PS-Adobe-');
63 [ind2, ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');
64 % Put the header first
65 if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1)
66 fstrm = fstrm([ind(1)+1:ind2(1) 1:ind(1) ind2(1)+1:end]);
67 end
68end
69
70% Make sure all line width commands come before the line style definitions,
71% so that dash lengths can be based on the correct widths
72% Find all line style sections
73ind = [regexp(fstrm, '[\n\r]SO[\n\r]'),... % This needs to be here even though it doesn't have dots/dashes!
74 regexp(fstrm, '[\n\r]DO[\n\r]'),...
75 regexp(fstrm, '[\n\r]DA[\n\r]'),...
76 regexp(fstrm, '[\n\r]DD[\n\r]')];
77ind = sort(ind);
78% Find line width commands
79[ind2, ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');
80% Go through each line style section and swap with any line width commands
81% near by
82b = 1;
83m = numel(ind);
84n = numel(ind2);
85for a = 1:m
86 % Go forwards width commands until we pass the current line style
87 while b <= n && ind2(b) < ind(a)
88 b = b + 1;
89 end
90 if b > n
91 % No more width commands
92 break;
93 end
94 % Check we haven't gone past another line style (including SO!)
95 if a < m && ind2(b) > ind(a+1)
96 continue;
97 end
98 % Are the commands close enough to be confident we can swap them?
99 if (ind2(b) - ind(a)) > 8
100 continue;
101 end
102 % Move the line style command below the line width command
103 fstrm(ind(a)+1:ind3(b)) = [fstrm(ind(a)+4:ind3(b)) fstrm(ind(a)+1:ind(a)+3)];
104 b = b + 1;
105end
106
107% Find any grid line definitions and change to GR format
108% Find the DO sections again as they may have moved
109ind = int32(regexp(fstrm, '[\n\r]DO[\n\r]'));
110if ~isempty(ind)
111 % Find all occurrences of what are believed to be axes and grid lines
112 ind2 = int32(regexp(fstrm, '[\n\r] *\d* *\d* *mt *\d* *\d* *L[\n\r]'));
113 if ~isempty(ind2)
114 % Now see which DO sections come just before axes and grid lines
115 ind2 = repmat(ind2', [1 numel(ind)]) - repmat(ind, [numel(ind2) 1]);
116 ind2 = any(ind2 > 0 & ind2 < 12); % 12 chars seems about right
117 ind = ind(ind2);
118 % Change any regions we believe to be grid lines to GR
119 fstrm(ind+1) = 'G';
120 fstrm(ind+2) = 'R';
121 end
122end
123
124% Define the new styles, including the new GR format
125% Dot and dash lengths have two parts: a constant amount plus a line width
126% variable amount. The constant amount comes after dpi2point, and the
127% variable amount comes after currentlinewidth. If you want to change
128% dot/dash lengths for a one particular line style only, edit the numbers
129% in the /DO (dotted lines), /DA (dashed lines), /DD (dot dash lines) and
130% /GR (grid lines) lines for the style you want to change.
131new_style = {'/dom { dpi2point 1 currentlinewidth 0.08 mul add mul mul } bdef',... % Dot length macro based on line width
132 '/dam { dpi2point 2 currentlinewidth 0.04 mul add mul mul } bdef',... % Dash length macro based on line width
133 '/SO { [] 0 setdash 0 setlinecap } bdef',... % Solid lines
134 '/DO { [1 dom 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dotted lines
135 '/DA { [4 dam 1.5 dam] 0 setdash 0 setlinecap } bdef',... % Dashed lines
136 '/DD { [1 dom 1.2 dom 4 dam 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dot dash lines
137 '/GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef'}; % Grid lines - dot spacing remains constant
138
139% Construct the output
140% This is the original (memory-intensive) code:
141%first_sec = strfind(fstrm, '% line types:'); % Isolate line style definition section
142%[second_sec, remaining] = strtok(fstrm(first_sec+1:end), '/');
143%[remaining, remaining] = strtok(remaining, '%');
144%fstrm = [fstrm(1:first_sec) second_sec sprintf('%s\r', new_style{:}) remaining];
145fstrm = regexprep(fstrm,'(% line types:.+?)/.+?%',['$1',sprintf('%s\r',new_style{:}),'%']);
146
147% Write the output file
148if nargout == 0 || nargin > 1
149 read_write_entire_textfile(fname2, fstrm);
150end
151end
function version(in silent)
Return a scalar MATLAB string containing the latest available ParaMonte MATLAB version newer than the...
function fix_lines(in fstrm, in fname2)
function read_write_entire_textfile(in fname, in fstrm)
function using_hg2(in fig)
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...