1   /**
2    * JHylaFax - A java client for HylaFAX.
3    *
4    * Copyright (C) 2005 by Steffen Pingel <steffenp@gmx.de>
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19   */
20  /**
21   * This file has been adopted for JHylaFAX. Original copyright notice below.
22   */
23  /*
24   * $Id: FaxCover.java,v 1.3 2009/04/19 02:10:08 squig Exp $
25   *
26   * Die Klasse FaxCovergen
27   * (c) 1997 SuSE GmbH
28   * Autor: Carsten Hoeger
29   *
30   * Diese Klasse erzeugt ein Faxcover aus dem Faxcover-Template
31   *
32   * Konstruktor:
33   * public FaxCovergen(String cover, String doc, boolean debug)
34   *
35   * String  cover : Pfad auf das Covertemplate
36   * String  doc   : Pfad auf das zu sendende Dokument
37   * boolean debug : Debug Flag
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  	// EPS Bounding Box
77  	private int urx; // upper right
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; // North American Letter
97  			height = 13200;
98  			break;
99  		}
100 		urx = (int)((width / 1200.0) * 72);
101 		ury = (int)((height / 1200.0) * 72);
102 		/* Pagesizes from hylafax-database 
103 		 #						Guaranteed Reproducible Area
104 		 # Name			Abbrev	Width	Height	Width	Height	Top	Left
105 		 ISO A3			A3	14030	19840	13200	 18480	472	345
106 		 ISO A4			A4	 9920	14030	 9240	 13200	472	345
107 		 ISO A5			A5	 7133	 9921	 6455	  9236	472	345
108 		 ISO A6			A6	 5055	 6991	 4575	  6508	472	345
109 		 ISO B4			B4	12048	17196	11325	 16010	472	345
110 		 North American Letter	NA-LET	10200	13200	 9240	 12400	472	345
111 		 American Legal		US-LEG	10200	16800	 9240	 15775	472	345
112 		 American Ledger		US-LED	13200	20400	11946	 19162	472	345
113 		 American Executive	US-EXE	 8700	12600	 7874	 11835	472	345
114 		 Japanese Letter		JP-LET	 8598	12141	 7600	 10200	900	400
115 		 Japanese Legal		JP-LEG	12141	17196	11200	 15300	900	400
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 		// read in cover template
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 		// count pages of attached documents
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 		// prepend generated header to cover template that defines the 
160 		// template variables
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 }