blob: 9a84a08c63f78d3381d9061fc93f70f2eb501799 (
plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* $Id$
* Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.render.ps;
// Java
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
// FOP
import org.apache.fop.datatypes.ColorType;
import org.apache.fop.fonts.Font;
import org.apache.fop.layout.FontInfo;
import org.apache.fop.render.AbstractRenderer;
/**
* Renderer that renders to PostScript.
* <br>
* This class currently generates PostScript Level 2 code. The only exception
* is the FlateEncode filter which is a Level 3 feature. The filters in use
* are hardcoded at the moment.
* <br>
* This class follows the Document Structuring Conventions (DSC) version 3.0.
* If anyone modifies this renderer please make
* sure to also follow the DSC to make it simpler to programmatically modify
* the generated Postscript files (ex. extract pages etc.).
* <br>
* @todo Rebuild the PostScript renderer
*/
public class PSRenderer extends AbstractRenderer {
/**
* the application producing the PostScript
*/
protected String producer;
private boolean enableComments = true;
/**
* the stream used to output the PostScript
*/
protected PSStream out;
private boolean ioTrouble = false;
private String currentFontName;
private int currentFontSize;
private int pageHeight;
private int pageWidth;
private float currRed;
private float currGreen;
private float currBlue;
private FontInfo fontInfo;
/**
* Set the document's producer
*
* @param producer string indicating application producing the PostScript
*/
public void setProducer(String producer) {
this.producer = producer;
}
/**
* Write out a command
* @param cmd PostScript command
*/
protected void write(String cmd) {
try {
out.write(cmd);
} catch (IOException e) {
if (!ioTrouble) {
e.printStackTrace();
}
ioTrouble = true;
}
}
/**
* Write out a comment
* @param comment Comment to write
*/
protected void comment(String comment) {
if (this.enableComments) {
write(comment);
}
}
/**
* Generates the PostScript code for the font dictionary.
* @param fontInfo available fonts
*/
protected void writeFontDict(FontInfo fontInfo) {
write("%%BeginResource: procset FOPFonts");
write("%%Title: Font setup (shortcuts) for this file");
write("/FOPFonts 100 dict dup begin");
write("/bd{bind def}bind def");
write("/ld{load def}bd");
write("/M/moveto ld");
write("/RM/rmoveto ld");
write("/t/show ld");
write("/ux 0.0 def");
write("/uy 0.0 def");
// write("/cf /Helvetica def");
// write("/cs 12000 def");
// <font> <size> F
write("/F {");
write(" /Tp exch def");
// write(" currentdict exch get");
write(" /Tf exch def");
write(" Tf findfont Tp scalefont setfont");
write(" /cf Tf def /cs Tp def /cw ( ) stringwidth pop def");
write("} bd");
write("/ULS {currentpoint /uy exch def /ux exch def} bd");
write("/ULE {");
write(" /Tcx currentpoint pop def");
write(" gsave");
write(" newpath");
write(" cf findfont cs scalefont dup");
write(" /FontMatrix get 0 get /Ts exch def /FontInfo get dup");
write(" /UnderlinePosition get Ts mul /To exch def");
write(" /UnderlineThickness get Ts mul /Tt exch def");
write(" ux uy To add moveto Tcx uy To add lineto");
write(" Tt setlinewidth stroke");
write(" grestore");
write("} bd");
write("/OLE {");
write(" /Tcx currentpoint pop def");
write(" gsave");
write(" newpath");
write(" cf findfont cs scalefont dup");
write(" /FontMatrix get 0 get /Ts exch def /FontInfo get dup");
write(" /UnderlinePosition get Ts mul /To exch def");
write(" /UnderlineThickness get Ts mul /Tt exch def");
write(" ux uy To add cs add moveto Tcx uy To add cs add lineto");
write(" Tt setlinewidth stroke");
write(" grestore");
write("} bd");
write("/SOE {");
write(" /Tcx currentpoint pop def");
write(" gsave");
write(" newpath");
write(" cf findfont cs scalefont dup");
write(" /FontMatrix get 0 get /Ts exch def /FontInfo get dup");
write(" /UnderlinePosition get Ts mul /To exch def");
write(" /UnderlineThickness get Ts mul /Tt exch def");
write(" ux uy To add cs 10 mul 26 idiv add moveto "
+ "Tcx uy To add cs 10 mul 26 idiv add lineto");
write(" Tt setlinewidth stroke");
write(" grestore");
write("} bd");
// write("/gfF1{/Helvetica findfont} bd");
// write("/gfF3{/Helvetica-Bold findfont} bd");
Map fonts = fontInfo.getFonts();
Iterator enum = fonts.keySet().iterator();
while (enum.hasNext()) {
String key = (String)enum.next();
Font fm = (Font)fonts.get(key);
write("/" + key + " /" + fm.getFontName() + " def");
}
write("end def");
write("%%EndResource");
enum = fonts.keySet().iterator();
while (enum.hasNext()) {
String key = (String)enum.next();
Font fm = (Font)fonts.get(key);
write("/" + fm.getFontName() + " findfont");
write("dup length dict begin");
write(" {1 index /FID ne {def} {pop pop} ifelse} forall");
write(" /Encoding ISOLatin1Encoding def");
write(" currentdict");
write("end");
write("/" + fm.getFontName() + " exch definefont pop");
}
}
/**
* Make sure the cursor is in the right place.
*/
protected void movetoCurrPosition() {
write(this.currentIPPosition + " " + this.currentBPPosition + " M");
}
/**
* Set up the font info
*
* @param fontInfo the font info object to set up
*/
public void setupFontInfo(FontInfo fontInfo) {
/* use PDF's font setup to get PDF metrics */
org.apache.fop.render.pdf.FontSetup.setup(fontInfo, null);
this.fontInfo = fontInfo;
}
/**
* Draws a filled rectangle.
* @param x x-coordinate
* @param y y-coordinate
* @param w width
* @param h height
* @param col color to fill with
*/
protected void addFilledRect(int x, int y, int w, int h,
ColorType col) {
write("newpath");
write(x + " " + y + " M");
write(w + " 0 rlineto");
write("0 " + (-h) + " rlineto");
write((-w) + " 0 rlineto");
write("0 " + h + " rlineto");
write("closepath");
useColor(col);
write("fill");
}
/**
* Changes the currently used font.
* @param name name of the font
* @param size font size
*/
public void useFont(String name, int size) {
if ((currentFontName != name) || (currentFontSize != size)) {
write(name + " " + size + " F");
currentFontName = name;
currentFontSize = size;
}
}
private void useColor(ColorType col) {
useColor(col.red(), col.green(), col.blue());
}
private void useColor(float red, float green, float blue) {
if ((red != currRed) || (green != currGreen) || (blue != currBlue)) {
write(red + " " + green + " " + blue + " setrgbcolor");
currRed = red;
currGreen = green;
currBlue = blue;
}
}
/**
* @see org.apache.fop.render.Renderer#startRenderer(OutputStream)
*/
public void startRenderer(OutputStream outputStream)
throws IOException {
getLogger().debug("rendering areas to PostScript");
this.out = new PSStream(outputStream);
write("%!PS-Adobe-3.0");
write("%%Creator: " + this.producer);
write("%%DocumentProcessColors: Black");
write("%%DocumentSuppliedResources: procset FOPFonts");
write("%%EndComments");
write("%%BeginDefaults");
write("%%EndDefaults");
write("%%BeginProlog");
write("%%EndProlog");
write("%%BeginSetup");
writeFontDict(fontInfo);
/* Write proc for including EPS */
write("%%BeginResource: procset EPSprocs");
write("%%Title: EPS encapsulation procs");
write("/BeginEPSF { %def");
write("/b4_Inc_state save def % Save state for cleanup");
write("/dict_count countdictstack def % Count objects on dict stack");
write("/op_count count 1 sub def % Count objects on operand stack");
write("userdict begin % Push userdict on dict stack");
write("/showpage { } def % Redefine showpage, { } = null proc");
write("0 setgray 0 setlinecap % Prepare graphics state");
write("1 setlinewidth 0 setlinejoin");
write("10 setmiterlimit [ ] 0 setdash newpath");
write("/languagelevel where % If level not equal to 1 then");
write("{pop languagelevel % set strokeadjust and");
write("1 ne % overprint to their defaults.");
write("{false setstrokeadjust false setoverprint");
write("} if");
write("} if");
write("} bind def");
write("/EndEPSF { %def");
write("count op_count sub {pop} repeat % Clean up stacks");
write("countdictstack dict_count sub {end} repeat");
write("b4_Inc_state restore");
write("} bind def");
write("%%EndResource");
write("%%EndSetup");
write("FOPFonts begin");
}
/**
* @see org.apache.fop.render.Renderer#stopRenderer()
*/
public void stopRenderer() throws IOException {
write("%%Trailer");
write("%%EOF");
this.out.flush();
}
}
|