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