KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fix_lines.m
Go to the documentation of this file.
1 
2 
3 /* (Autoinserted by mtoc++)
4  * This source code has been filtered by the mtoc++ executable,
5  * which generates code that can be processed by the doxygen documentation tool.
6  *
7  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
8  * Except for the comments, the function bodies of your M-file functions are untouched.
9  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
10  * attached source files that are highly readable by humans.
11  *
12  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
13  * the correct locations in the source code browser.
14  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
15  */
16 
17 function fix_lines(fname,fname2) {
18 
19 
20 /* Copyright: (C) Oliver Woodford, 2008-2010 */
21 
22 /* The idea of editing the EPS file to change line styles comes from Jiro
23  * Doke's FIXPSLINESTYLE (fex id: 17928)
24  * The idea of changing dash length with line width came from comments on
25  * fex id: 5743, but the implementation is mine :) */
26 
27 /* Thank you to Sylvain Favrot for bringing the embedded font/bounding box
28  * interaction in older versions of MATLAB to my attention.
29  * Thank you to D Ko for bringing an error with eps files with tiff previews
30  * to my attention.
31  * Thank you to Laurence K for suggesting the check to see if the file was
32  * opened. */
33 
34 /* Read in the file */
35 fh = fopen(fname, " r ");
36 if fh == -1
37  error(" File %s not found. ", fname);
38 end
39 try
40  fstrm = fread(fh, " *char ")^t;
41 catch ex
42  fclose(fh);
43  rethrow(ex);
44 end
45 fclose(fh);
46 
47 /* Move any embedded fonts after the postscript header */
48 if strcmp(fstrm(1:15), " %!PS-AdobeFont- ")
49  /* Find the start and end of the header */
50  ind = regexp(fstrm, " [\n\r]%!PS-Adobe- ");
51  [ind2 ind2] = regexp(fstrm, " [\n\r]%%EndComments[\n\r]+ ");
52  /* Put the header first */
53  if ~isempty(ind) && ~isempty(ind2) && ind(1) < ind2(1)
54  fstrm = fstrm([ind(1)+1:ind2(1) 1:ind(1) ind2(1)+1:end]);
55  end
56 end
57 
58 /* Make sure all line width commands come before the line style definitions,
59  * so that dash lengths can be based on the correct widths
60  * Find all line style sections */
61 ind = [regexp(fstrm, " [\n\r]SO[\n\r] "),... /* This needs to be here even though it doesn't have dots/dashes! */
62 
63  regexp(fstrm, " [\n\r]DO[\n\r] "),...
64  regexp(fstrm, " [\n\r]DA[\n\r] "),...
65  regexp(fstrm, " [\n\r]DD[\n\r] ")];
66 ind = sort(ind);
67 /* Find line width commands */
68 [ind2 ind3] = regexp(fstrm, " [\n\r]\d* w[\n\r] ");
69 /* Go through each line style section and swap with any line width commands
70  * near by */
71 b = 1;
72 m = numel(ind);
73 n = numel(ind2);
74 for a = 1:m
75  /* Go forwards width commands until we pass the current line style */
76  while b <= n && ind2(b) < ind(a)
77  b = b + 1;
78  end
79  if b > n
80  /* No more width commands */
81  break;
82  end
83  /* Check we haven't gone past another line style (including SO!) */
84  if a < m && ind2(b) > ind(a+1)
85  continue;
86  end
87  /* Are the commands close enough to be confident we can swap them? */
88  if (ind2(b) - ind(a)) > 8
89  continue;
90  end
91  /* Move the line style command below the line width command */
92  fstrm(ind(a)+1:ind3(b)) = [fstrm(ind(a)+4:ind3(b)) fstrm(ind(a)+1:ind(a)+3)];
93  b = b + 1;
94 end
95 
96 /* Find any grid line definitions and change to GR format
97  * Find the DO sections again as they may have moved */
98 ind = int32(regexp(fstrm, " [\n\r]DO[\n\r] "));
99 if ~isempty(ind)
100  /* Find all occurrences of what are believed to be axes and grid lines */
101  ind2 = int32(regexp(fstrm, " [\n\r] *\d* *\d* *mt *\d* *\d* *L[\n\r] "));
102  if ~isempty(ind2)
103  /* Now see which DO sections come just before axes and grid lines */
104  ind2 = repmat(ind2^t, [1 numel(ind)]) - repmat(ind, [numel(ind2) 1]);
105  ind2 = any(ind2 > 0 & ind2 < 12); /* 12 chars seems about right */
106 
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 
128  " /dam { dpi2point 2 currentlinewidth 0.04 mul add mul mul } bdef ",... /* Dash length macro based on line width */
129 
130  " /SO { [] 0 setdash 0 setlinecap } bdef ",... /* Solid lines */
131 
132  " /DO { [1 dom 1.2 dom] 0 setdash 0 setlinecap } bdef ",... /* Dotted lines */
133 
134  " /DA { [4 dam 1.5 dam] 0 setdash 0 setlinecap } bdef ",... /* Dashed lines */
135 
136  " /DD { [1 dom 1.2 dom 4 dam 1.2 dom] 0 setdash 0 setlinecap } bdef ",... /* Dot dash lines */
137 
138  " /GR { [0 dpi2point mul 4 dpi2point mul] 0 setdash 1 setlinecap } bdef "]; /* Grid lines - dot spacing remains constant */
139 
140 
141 if nargin < 2
142  /* Overwrite the input file */
143  fname2 = fname;
144 end
145 
146 /* Save the file with the section replaced */
147 fh = fopen(fname2, " w ");
148 if fh == -1
149  error(" Unable to open %s for writing. ", fname2);
150 end
151 try
152  fwrite(fh, fstrm(1:first_sec), " char*1 ");
153  fwrite(fh, second_sec, " char*1 ");
154  fprintf(fh, " %s\r ", new_style[:]);
155  fwrite(fh, remaining, " char*1 ");
156 catch ex
157  fclose(fh);
158  rethrow(ex);
159 end
160 fclose(fh);
161 return
162 }
function fix_lines(fname, fname2)
FIX_LINES Improves the line style of eps files generated by print.
Definition: fix_lines.m:17