source: MML/trunk/machine/SOLEIL/common/toolbox/export_fig/fix_lines.m @ 17

Last change on this file since 17 was 17, checked in by zhangj, 10 years ago

To have a stable version on the server.

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1function 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
32% Read in the file
33fh = fopen(fname, 'rt');
34try
35    fstrm = fread(fh, '*char')';
36catch
37    fclose(fh);
38    rethrow(lasterror);
39end
40fclose(fh);
41
42% Move any embedded fonts after the postscript header
43if strcmp(fstrm(1:15), '%!PS-AdobeFont-')
44    % Find the start and end of the header
45    ind = regexp(fstrm, '[\n\r]%!PS-Adobe-');
46    [ind2 ind2] = regexp(fstrm, '[\n\r]%%EndComments[\n\r]+');
47    % Put the header first
48    if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1)
49        fstrm = fstrm([ind(1)+1:ind2(1) 1:ind(1) ind2(1)+1:end]);
50    end
51end
52
53% Make sure all line width commands come before the line style definitions,
54% so that dash lengths can be based on the correct widths
55% Find all line style sections
56ind = [regexp(fstrm, '[\n\r]SO[\n\r]'),... % This needs to be here even though it doesn't have dots/dashes!
57       regexp(fstrm, '[\n\r]DO[\n\r]'),...
58       regexp(fstrm, '[\n\r]DA[\n\r]'),...
59       regexp(fstrm, '[\n\r]DD[\n\r]')];
60ind = sort(ind);
61% Find line width commands
62[ind2 ind3] = regexp(fstrm, '[\n\r]\d* w[\n\r]');
63% Go through each line style section and swap with any line width commands
64% near by
65b = 1;
66m = numel(ind);
67n = numel(ind2);
68for a = 1:m
69    % Go forwards width commands until we pass the current line style
70    while b <= n && ind2(b) < ind(a)
71        b = b + 1;
72    end
73    if b > n
74        % No more width commands
75        break;
76    end
77    % Check we haven't gone past another line style (including SO!)
78    if a < m && ind2(b) > ind(a+1)
79        continue;
80    end
81    % Are the commands close enough to be confident we can swap them?
82    if (ind2(b) - ind(a)) > 8
83        continue;
84    end
85    % Move the line style command below the line width command
86    fstrm(ind(a)+1:ind3(b)) = [fstrm(ind(a)+4:ind3(b)) fstrm(ind(a)+1:ind(a)+3)];
87    b = b + 1;
88end
89
90% Find any grid line definitions and change to GR format
91% Find the DO sections again as they may have moved
92ind = int32(regexp(fstrm, '[\n\r]DO[\n\r]'));
93if ~isempty(ind)
94    % Find all occurrences of what are believed to be axes and grid lines
95    ind2 = int32(regexp(fstrm, '[\n\r] *\d* *\d* *mt *\d* *\d* *L[\n\r]'));
96    if ~isempty(ind2)
97        % Now see which DO sections come just before axes and grid lines
98        ind2 = repmat(ind2', [1 numel(ind)]) - repmat(ind, [numel(ind2) 1]);
99        ind2 = any(ind2 > 0 & ind2 < 12); % 12 chars seems about right
100        ind = ind(ind2);
101        % Change any regions we believe to be grid lines to GR
102        fstrm(ind+1) = 'G';
103        fstrm(ind+2) = 'R';
104    end
105end
106
107% Isolate line style definition section
108first_sec = findstr(fstrm, '% line types:');
109[second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');
110[dummy remaining] = strtok(remaining, '%');
111
112% Define the new styles, including the new GR format
113% Dot and dash lengths have two parts: a constant amount plus a line width
114% variable amount. The constant amount comes after dpi2point, and the
115% variable amount comes after currentlinewidth. If you want to change
116% dot/dash lengths for a one particular line style only, edit the numbers
117% in the /DO (dotted lines), /DA (dashed lines), /DD (dot dash lines) and
118% /GR (grid lines) lines for the style you want to change.
119new_style = {'/dom { dpi2point 1 currentlinewidth 0.08 mul add mul mul } bdef',... % Dot length macro based on line width
120             '/dam { dpi2point 2 currentlinewidth 0.04 mul add mul mul } bdef',... % Dash length macro based on line width
121             '/SO { [] 0 setdash 0 setlinecap } bdef',... % Solid lines
122             '/DO { [1 dom 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dotted lines
123             '/DA { [4 dam 1.5 dam] 0 setdash 0 setlinecap } bdef',... % Dashed lines
124             '/DD { [1 dom 1.2 dom 4 dam 1.2 dom] 0 setdash 0 setlinecap } bdef',... % Dot dash lines
125             '/GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef'}; % Grid lines - dot spacing remains constant
126new_style = sprintf('%s\r', new_style{:});
127
128if nargin < 2
129    % Overwrite the input file
130    fname2 = fname;
131end
132
133% Save the file with the section replaced
134fh = fopen(fname2, 'wt');
135try
136    fprintf(fh, '%s%s%s%s', fstrm(1:first_sec), second_sec, new_style, remaining);
137catch
138    fclose(fh);
139    rethrow(lasterror);
140end
141fclose(fh);
142return
Note: See TracBrowser for help on using the repository browser.