Robochameleon  v1.0
fix_lines.m
1 function fix_lines(fname, fname2)
2 %FIX_LINES Improves the line style of eps files generated by print
3 %
4 % Examples:
5 % fix_lines fname
6 % fix_lines fname fname2
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 
22 % Copyright: (C) Oliver Woodford, 2008-2010
23 
24 % The idea of editing the EPS file to change line styles comes from Jiro
25 % Doke's FIXPSLINESTYLE (fex id: 17928)
26 % The idea of changing dash length with line width came from comments on
27 % fex id: 5743, but the implementation is mine :)
28 
29 % Thank you to Sylvain Favrot for bringing the embedded font/bounding box
30 % interaction in older versions of MATLAB to my attention.
31 % Thank you to D Ko for bringing an error with eps files with tiff previews
32 % to my attention.
33 % Thank you to Laurence K for suggesting the check to see if the file was
34 % opened.
35 
36 % Read in the file
37 fh = fopen(fname, 'r');
38 if fh == -1
39  error('File %s not found.', fname);
40 end
41 try
42  fstrm = fread(fh, '*char')';
43 catch ex
44  fclose(fh);
45  rethrow(ex);
46 end
47 fclose(fh);
48 
49 % Move any embedded fonts after the postscript header
50 if strcmp(fstrm(1:15), '%!PS-AdobeFont-')
51  % Find the start and end of the header
52  ind = regexp(fstrm, '[\n\r]%!PS-Adobe-');
53  [ind2 ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');
54  % Put the header first
55  if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1)
56  fstrm = fstrm([ind(1)+1:ind2(1) 1:ind(1) ind2(1)+1:end]);
57  end
58 end
59 
60 % Make sure all line width commands come before the line style definitions,
61 % so that dash lengths can be based on the correct widths
62 % Find all line style sections
63 ind = [regexp(fstrm, '[\n\r]SO[\n\r]'),... % This needs to be here even though it doesn't have dots/dashes!
64  regexp(fstrm, '[\n\r]DO[\n\r]'),...
65  regexp(fstrm, '[\n\r]DA[\n\r]'),...
66  regexp(fstrm, '[\n\r]DD[\n\r]')];
67 ind = sort(ind);
68 % Find line width commands
69 [ind2 ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');
70 % Go through each line style section and swap with any line width commands
71 % near by
72 b = 1;
73 m = numel(ind);
74 n = numel(ind2);
75 for a = 1:m
76  % Go forwards width commands until we pass the current line style
77  while b <= n && ind2(b) < ind(a)
78  b = b + 1;
79  end
80  if b > n
81  % No more width commands
82  break;
83  end
84  % Check we haven't gone past another line style (including SO!)
85  if a < m && ind2(b) > ind(a+1)
86  continue;
87  end
88  % Are the commands close enough to be confident we can swap them?
89  if (ind2(b) - ind(a)) > 8
90  continue;
91  end
92  % Move the line style command below the line width command
93  fstrm(ind(a)+1:ind3(b)) = [fstrm(ind(a)+4:ind3(b)) fstrm(ind(a)+1:ind(a)+3)];
94  b = b + 1;
95 end
96 
97 % Find any grid line definitions and change to GR format
98 % Find the DO sections again as they may have moved
99 ind = int32(regexp(fstrm, '[\n\r]DO[\n\r]'));
100 if ~isempty(ind)
101  % Find all occurrences of what are believed to be axes and grid lines
102  ind2 = int32(regexp(fstrm, '[\n\r] *\d* *\d* *mt *\d* *\d* *L[\n\r]'));
103  if ~isempty(ind2)
104  % Now see which DO sections come just before axes and grid lines
105  ind2 = repmat(ind2', [1 numel(ind)]) - repmat(ind, [numel(ind2) 1]);
106  ind2 = any(ind2 > 0 & ind2 < 12); % 12 chars seems about right
107  ind = ind(ind2);
108  % Change any regions we believe to be grid lines to GR
109  fstrm(ind+1) = 'G';
110  fstrm(ind+2) = 'R';
111  end
112 end
113 
114 % Isolate line style definition section
115 first_sec = strfind(fstrm, '% line types:');
116 [second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');
117 [remaining remaining] = strtok(remaining, '%');
118 
119 % Define the new styles, including the new GR format
120 % Dot and dash lengths have two parts: a constant amount plus a line width
121 % variable amount. The constant amount comes after dpi2point, and the
122 % variable amount comes after currentlinewidth. If you want to change
123 % dot/dash lengths for a one particular line style only, edit the numbers
124 % in the /DO (dotted lines), /DA (dashed lines), /DD (dot dash lines) and
125 % /GR (grid lines) lines for the style you want to change.
126 new_style = {'/dom { dpi2point 1 currentlinewidth 0.08 mul add mul mul } bdef',... % Dot length macro based on line width
127  '/dam { dpi2point 2 currentlinewidth 0.04 mul add mul mul } bdef',... % Dash length macro based on line width
128  '/SO { [] 0 setdash 0 setlinecap } bdef',... % Solid lines
129  '/DO { [1 dom 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dotted lines
130  '/DA { [4 dam 1.5 dam] 0 setdash 0 setlinecap } bdef',... % Dashed lines
131  '/DD { [1 dom 1.2 dom 4 dam 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dot dash lines
132  '/GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef'}; % Grid lines - dot spacing remains constant
133 
134 if nargin < 2
135  % Overwrite the input file
136  fname2 = fname;
137 end
138 
139 % Save the file with the section replaced
140 fh = fopen(fname2, 'w');
141 if fh == -1
142  error('Unable to open %s for writing.', fname2);
143 end
144 try
145  fwrite(fh, fstrm(1:first_sec), 'char*1');
146  fwrite(fh, second_sec, 'char*1');
147  fprintf(fh, '%s\r', new_style{:});
148  fwrite(fh, remaining, 'char*1');
149 catch ex
150  fclose(fh);
151  rethrow(ex);
152 end
153 fclose(fh);
154 return