1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 package net.sf.jhylafax.fax;
40
41 import java.io.BufferedReader;
42 import java.io.FileInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import net.sf.jhylafax.JHylaFAX;
50
51 import org.xnap.commons.io.ProgressMonitor;
52
53 public class FaxCover {
54
55 public static final String CHARSET = "ISO-8859-1";
56
57 public String to = "";
58 public String to_adress = "";
59 public String to_company = "";
60 public String to_location = "";
61 public String to_voice_number = "";
62 public String to_fax_number = "";
63 public float pagewidth;
64 public float pagelength;
65 public String from = "";
66 public String from_fax_number = "";
67 public String from_voice_number = "";
68 public String from_company = "";
69 public String from_location = "";
70 public String todays_date = "";
71 public String regarding = "";
72 public String comments = "";
73 private boolean debug;
74 private int page_count;
75 private int npages;
76
77 private int urx;
78 private int ury;
79 private List<String> docs = new ArrayList<String>();
80
81 public FaxCover(Paper paper)
82 {
83 float width = 9920;
84 float height = 14030;
85
86 switch (paper) {
87 case A4:
88 width = 9920;
89 height = 14030;
90 break;
91 case A3:
92 width = 14030;
93 height = 19840;
94 break;
95 case LETTER:
96 width = 10200;
97 height = 13200;
98 break;
99 }
100 urx = (int)((width / 1200.0) * 72);
101 ury = (int)((height / 1200.0) * 72);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 }
118
119 public void addDocument(String filename) {
120 docs.add(filename);
121 }
122
123 public StringBuffer generate(InputStream in, ProgressMonitor monitor) throws IOException
124 {
125 StringBuffer coverBuffer = new StringBuffer();
126
127
128 String line;
129 BufferedReader reader = new BufferedReader(new InputStreamReader(in, FaxCover.CHARSET));
130 try {
131 while ((line = reader.readLine()) != null) {
132 if (line.startsWith("%%Page: ")) {
133 page_count++;
134 }
135 coverBuffer.append(line);
136 coverBuffer.append("\n");
137 }
138 }
139 finally {
140 reader.close();
141 }
142
143
144 for (String filename : docs) {
145 reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
146 try {
147 while ((line = reader.readLine()) != null) {
148 if (line.startsWith("%%Page: ")) {
149 page_count++;
150 npages++;
151 }
152 }
153 }
154 finally {
155 reader.close();
156 }
157 }
158
159
160
161 StringBuffer sb = createHeader();
162 sb.append(coverBuffer);
163 return sb;
164 }
165
166 private StringBuffer createHeader()
167 {
168 StringBuffer sb = new StringBuffer();
169 sb.append("%!PS-Adobe-2.0 EPSF-2.0\n"
170 + "%%Creator: JHylaFAX Version " + JHylaFAX.getVersion() + "\n"
171 + "%%Title: HylaFAX Cover Sheet\n"
172 + "%%CreationDate: "
173 + todays_date
174 + "\n"
175 + "%%Origin: 0 0\n"
176 + "%%BoundingBox: "
177 + 0
178 + " "
179 + 0
180 + " "
181 + urx
182 + " "
183 + ury
184 + "\n"
185 + "%%Pages: "
186 + page_count
187 + "\n"
188 + "%%EndComments\n"
189 + "%%BeginProlog\n"
190 + "100 dict begin\n"
191 + "/wordbreak ( ) def\n"
192 + "/linebreak (\\n) def\n"
193 + "/doLine {\n"
194 + "% <line> <width> <height> <x> <y> doLine <width> <height> <x> <y>\n"
195 + "2 copy moveto 5 -1 roll\n"
196 + "wordbreak\n"
197 + "{\n"
198 + " search {\n"
199 + " dup stringwidth pop currentpoint pop add 7 index 6 index add gt {\n"
200 + " 6 3 roll 2 index sub 2 copy moveto 6 3 roll\n"
201 + " } if\n"
202 + " show wordbreak show\n"
203 + " }{\n"
204 + " dup stringwidth pop currentpoint pop add 5 index 4 index add gt {\n"
205 + " 3 1 roll 3 index sub 2 copy moveto 3 -1 roll\n"
206 + " } if\n" + " show exit\n" + " } ifelse\n"
207 + "} loop\n" + "2 index sub 2 copy moveto\n" + "} def\n"
208 + "/BreakIntoLines{\n"
209 + "% <width> <height> <x> <y> <text> BreakIntoLines\n"
210 + "linebreak\n" + "{\n" + " search {\n"
211 + " 7 3 roll doLine 6 -2 roll\n" + " }{\n"
212 + " 5 1 roll doLine exit\n" + " } ifelse\n"
213 + " } loop\n" + "pop pop pop pop\n" + "} def\n" + "/to (" + to
214 + ") def\n" + "/to-company (" + to_company + ") def\n"
215 + "/to-adress (" + to_adress + ") def\n" + "/to-Location ("
216 + to_location + ") def\n" + "/to-voice-number ("
217 + to_voice_number + ") def\n" + "/to-fax-number ("
218 + to_fax_number + ") def\n" + "/pageWidth " + pagewidth
219 + " def\n" + "/pageLength " + pagelength + " def\n" + "/from ("
220 + from + ") def\n" + "/from-fax-number (" + from_fax_number
221 + ") def\n" + "/from-voice-number (" + from_voice_number
222 + ") def\n" + "/from-company (" + from_company + ") def\n"
223 + "/from-Location (" + from_location + ") def\n"
224 + "/page-count (" + npages + ") def\n" + "/todays-date ("
225 + todays_date + ") def\n" + "/regarding (" + regarding
226 + ") def\n" + "/comments (" + emitCommentDefs(comments)
227 + ") def\n");
228 sb.append("%%EndProlog\n");
229 return sb;
230 }
231
232 private String emitCommentDefs(String comments)
233 {
234 int len = comments.length();
235 StringBuffer buffer = new StringBuffer();
236 buffer.setLength(2 * len);
237 int n = 0;
238 for (int i = 0; i < len; i++) {
239 char cur = comments.charAt(i);
240 if (cur == '\n') {
241 buffer.setCharAt(n++, '\\');
242 buffer.insert(n++, "\\n");
243 }
244 else {
245 if (cur == '(' || cur == ')' || cur == '\\')
246 buffer.insert(n++, '\\');
247 buffer.setCharAt(n, cur);
248 }
249 n++;
250 }
251 buffer.setLength(n);
252 return buffer.toString();
253 }
254
255 }