1 function fix_lines(fname, fname2)
2 %FIX_LINES Improves the line style of eps files generated by print
6 % fix_lines fname fname2
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. 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 19 % fname - Name or path of source eps file. 20 % fname2 - Name or path of destination eps file. Default: same as fname. 22 % Copyright: (C) Oliver Woodford, 2008-2010 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 :)
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
33 % Thank you to Laurence K
for suggesting the check to see
if the file was
37 fh = fopen(fname,
'r');
39 error(
'File %s not found.', fname);
42 fstrm = fread(fh,
'*char')
'; 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]); 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]')];
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
76 % Go forwards width commands until we pass the current line style
77 while b <= n && ind2(b) < ind(a)
81 % No more width commands
84 % Check we haven't gone past another line style (including SO!)
85 if a < m && ind2(b) > ind(a+1)
88 % Are the commands close enough to be confident we can swap them?
89 if (ind2(b) - ind(a)) > 8
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)];
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]'));
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]'));
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
108 % Change any regions we believe to be grid lines to GR
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, '%');
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
135 % Overwrite the input file
139 % Save the file with the section replaced
140 fh = fopen(fname2,
'w');
142 error(
'Unable to open %s for writing.', fname2);
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');