KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > postscript > PAPencil


1 /*
2  * Copyright 1998 by Sun Microsystems, Inc.,
3  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
4  * All rights reserved.
5  *
6  * This software is the confidential and proprietary information
7  * of Sun Microsystems, Inc. ("Confidential Information"). You
8  * shall not disclose such Confidential Information and shall use
9  * it only in accordance with the terms of the license agreement
10  * you entered into with Sun.
11  */

12
13 package com.lowagie.text.pdf.codec.postscript;
14
15 import java.util.*;
16 import java.awt.*;
17 import java.awt.font.*;
18 import java.awt.geom.*;
19
20
21 public class PAPencil extends Object {
22
23     static protected class State extends Object implements Cloneable {
24         public Stroke stroke;
25         public Paint paint;
26         public AffineTransform at;
27         public Shape clipShape;
28         public Font font;
29         public Composite composite;
30         public GeneralPath path;
31
32         public State(){
33             this(null);
34         }
35
36         public State(Graphics2D g){
37             if(g == null){
38                 this.stroke = new BasicStroke();
39                 this.paint = Color.black;
40                 this.at = new AffineTransform();
41                 this.font = new Font("SansSerif", Font.PLAIN, 12);
42                 this.composite = AlphaComposite.getInstance(AlphaComposite.DST_OVER, 1.0f);
43                 this.clipShape = null;
44             } else {
45                 this.recordState(g);
46             }
47             this.path = new GeneralPath();
48         }
49
50         public void recordState(Graphics2D g){
51             this.stroke = g.getStroke();
52             this.paint = g.getPaint();
53             this.at = g.getTransform();
54             this.font = g.getFont();
55             this.composite = g.getComposite();
56             this.clipShape = g.getClip();
57         }
58
59         public void stampState(Graphics2D g, Dimension size){
60             g.setTransform(new AffineTransform());
61             g.setClip(new Rectangle(0, 0, size.width, size.height));
62             g.setStroke(this.stroke);
63             g.setPaint(this.paint);
64             g.setTransform(this.at);
65             g.setFont(this.font);
66             g.setComposite(this.composite);
67             if(this.clipShape != null){
68                 g.clip(this.clipShape);
69             }
70         }
71
72         public Object clone(){
73             try {
74                 State n = (State)super.clone();
75
76                 n.at = (AffineTransform) this.at.clone();
77                 n.path = new GeneralPath();
78                 n.path.append(this.path, false);
79                 return n;
80             } catch(CloneNotSupportedException e){
81                 throw new InternalError();
82             }
83         }
84
85     }
86
87     //
88
// Class Variables
89
//
90

91     //
92
// Instance Variables
93
//
94

95     /**
96      * The canvas size.
97      */

98     protected Dimension size;
99
100     /**
101      * The current graphics state.
102      */

103     protected State state;
104
105     /**
106      * The stack of graphic states.
107      */

108     protected Stack gStack;
109
110     /**
111      * The font hashtable with postscript names as keys
112      */

113     protected HashMap fonts;
114
115     /**
116      * The current graphics device
117      */

118     public Graphics2D graphics;
119
120     //
121
// Constructors
122
//
123

124     public PAPencil(Component component){
125     this.graphics = (Graphics2D) component.getGraphics();
126     this.size = component.getSize();
127     this.initgraphics();
128     }
129
130     public PAPencil(Graphics graphics, Dimension size){
131     this.graphics = (Graphics2D) graphics;
132     this.size = size;
133     this.initgraphics();
134     }
135
136     //
137
// Graphics state management
138
//
139

140     public void gsave(){
141         this.state.recordState(this.graphics);
142     State next = (State) this.state.clone();
143
144     this.gStack.push(this.state);
145     this.state = next;
146     }
147
148     public void grestore(){
149         if(this.gStack.empty()){
150         this.initgraphics();
151     } else {
152         this.state = (State) this.gStack.pop();
153             this.state.stampState(this.graphics, this.size);
154     }
155     }
156
157     public void grestoreall(){
158     this.initgraphics();
159     }
160
161     public void initgraphics(){
162        // AffineTransform at = new AffineTransform();
163

164         // turn anti-aliasing and high-quality rendering on
165
this.graphics.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
166     this.graphics.setRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
167
168         // initialize to a postscript coordinate system
169
// at.translate(0, this.size.getHeight());
170
// at.scale(1, -1);
171
// this.graphics.setTransform(at);
172
this.graphics.translate(0, this.size.getHeight());
173         this.graphics.scale(1, -1);
174
175         // state, stack and page
176
this.state = new State(this.graphics);
177     this.gStack = new Stack();
178     this.erasepage();
179     }
180
181     //
182
// Path definition
183
//
184

185     public void newpath(){
186     this.state.path.reset();
187     }
188
189     public void moveto(double x, double y){
190     this.state.path.moveTo((float) x, (float) y);
191     }
192
193     public void moveto(Point2D p) {
194     this.moveto(p.getX(), p.getY());
195     }
196
197     public void rmoveto(double dx, double dy) throws PainterException {
198         Point2D currentPoint = this.state.path.getCurrentPoint();
199
200     if(currentPoint == null){
201         throw new PainterException("no current point");
202     }
203     this.state.path.moveTo((float) (currentPoint.getX() + dx) , (float) (currentPoint.getY() + dy));
204     }
205
206     public void lineto(double x, double y) throws PainterException {
207     Point2D currentPoint = this.state.path.getCurrentPoint();
208
209     if(currentPoint == null){
210         throw new PainterException("no current point");
211     }
212     this.state.path.lineTo((float) x, (float) y);
213     }
214
215     public void lineto(Point2D p) throws PainterException {
216     this.lineto(p.getX(), p.getY());
217     }
218
219     public void rlineto(double dx, double dy) throws PainterException {
220         Point2D currentPoint = this.state.path.getCurrentPoint();
221
222     if(currentPoint == null){
223         throw new PainterException("no current point");
224     }
225         this.state.path.lineTo((float) (currentPoint.getX() + dx) , (float) (currentPoint.getY() + dy));
226     }
227
228     public void arc(double cx, double cy, double r, double ang1, double ang2){
229         Arc2D.Float arc = new Arc2D.Float((float) cx, (float) cy, (float) r, (float) r,
230                                           (float) ang1, (float) (ang2 - ang1), Arc2D.OPEN);
231         Point2D currentPoint = this.state.path.getCurrentPoint();
232
233     if(currentPoint == null){
234             this.state.path.append(arc, false);
235         } else {
236             this.state.path.append(arc, true);
237         }
238     }
239
240     public void arcn(double cx, double cy, double r,
241                   double ang1, double ang2) {
242     Arc2D.Float arc = new Arc2D.Float((float) cx, (float) cy, (float) r, (float) r,
243                                           (float) ang1, (float) (ang1 - ang2), Arc2D.OPEN);
244        Point2D currentPoint = this.state.path.getCurrentPoint();
245
246     if(currentPoint == null){
247             this.state.path.append(arc, false);
248         } else {
249             this.state.path.append(arc, true);
250         }
251     }
252
253     public void curveto(double x1, double y1, double x2, double y2,
254                  double x3, double y3) throws PainterException {
255         Point2D currentPoint = this.state.path.getCurrentPoint();
256
257     if(currentPoint == null){
258         throw new PainterException("no current point");
259     }
260         this.state.path.curveTo((float) x1, (float) y1, (float) x2, (float) y2,
261                                   (float) x3, (float) y3);
262     }
263
264     public void rcurveto(double dx1, double dy1, double dx2, double dy2,
265               double dx3, double dy3) throws PainterException {
266         Point2D currentPoint = this.state.path.getCurrentPoint();
267
268     if(currentPoint == null){
269         throw new PainterException("no current point");
270     }
271     double x0 = currentPoint.getX();
272     double y0 = currentPoint.getY();
273     this.curveto(x0 + dx1, y0 + dy1, x0 + dx2, y0 + dy2, x0 + dx3,y0 + dy3);
274     }
275
276     public void closepath(){
277         this.state.path.closePath();
278     }
279
280     // PENDING(uweh): just a placeholder for now
281
public void clippath(){
282         this.rectpath(0.0d, 0.0d, 800.0d, 800.0d);
283     }
284
285     public void erasepage(){
286     this.graphics.clearRect(0, 0, 800, 800);
287     }
288
289     public void charpath(String aString, boolean adjustForStroking){
290     GlyphVector glyphVector = this.state.font.createGlyphVector(this.graphics.getFontRenderContext(), aString);
291
292         Shape glyphShape = glyphVector.getOutline();
293         this.state.path.append(glyphShape, false);
294     }
295
296     public void showpage(){
297
298     }
299
300     public void show(String string) throws PainterException {
301         Point2D currentPoint = this.state.path.getCurrentPoint();
302         AffineTransform currentTransform = this.graphics.getTransform();
303         Point2D tranformedPoint = currentTransform.transform(currentPoint, null);
304
305
306     if(currentPoint == null){
307         throw new PainterException("no current point");
308     }
309         this.graphics.setTransform(new AffineTransform());
310         this.graphics.drawString(string, (float) tranformedPoint.getX(), (float) tranformedPoint.getY());
311         this.graphics.setTransform(currentTransform);
312     }
313
314     public void fill(){
315         this.graphics.fill(this.state.path);
316         this.newpath();
317     }
318
319     public void eofill(){
320         this.state.path.setWindingRule(GeneralPath.WIND_EVEN_ODD);
321         this.graphics.fill(this.state.path);
322         this.state.path.setWindingRule(GeneralPath.WIND_NON_ZERO);
323         this.newpath();
324     }
325
326     public void stroke(){
327         this.graphics.draw(this.state.path);
328         this.newpath();
329     }
330
331     public void rectfill(double x, double y, double width, double height){
332     this.gsave();
333     this.rectpath(x, y, width, height);
334     this.fill();
335     this.grestore();
336     }
337
338     public void rectfill(Rectangle2D rect){
339         this.rectfill(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
340     }
341
342     public void rectstroke(double x, double y, double width, double height){
343     this.gsave();
344     this.rectpath(x, y, width, height);
345     this.stroke();
346     this.grestore();
347     }
348
349     public void rectstroke(Rectangle2D rect){
350     this.rectstroke(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
351     }
352
353     public void rectpath(double x, double y, double width, double height){
354     this.newpath();
355     this.moveto(x, y);
356     try {
357         this.rlineto(width, 0);
358         this.rlineto(0, height);
359         this.rlineto(-width, 0);
360     } catch(PainterException e){
361     }
362     this.closepath();
363     }
364
365     // convenience
366

367     // this guy tries to find an appropiate font
368
// if he fails returns whatever font he wants
369
public Font findFont(String fontname){
370         Font result;
371         StringBuffer buffer = new StringBuffer(fontname);
372         int i, n;
373         n = buffer.length();
374
375         for(i = 0; i < n; i++){
376             if(buffer.charAt(i) == '-'){
377                 buffer.setCharAt(i,' ');
378             }
379         }
380
381         fontname = buffer.toString();
382
383         if(this.fonts == null){
384             // construct the fonts dictionary
385
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
386             Font[] fontArray = genv.getAllFonts();
387             this.fonts = new HashMap();
388             for(i = 0; i < fontArray.length; i++){
389                 String postscriptName = fontArray[i].getPSName();
390                 this.fonts.put(postscriptName, fontArray[i]);
391             }
392         }
393         result = (Font) this.fonts.get(fontname);
394         if(result == null){
395             result = new Font("SansSerif", Font.PLAIN, 12);
396         }
397         return result;
398     }
399 }
400
401
Popular Tags