KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfGraphics2D


1 /*
2  * $Id: PdfGraphics2D.java 2780 2007-05-24 09:19:25Z blowagie $
3  *
4  * Copyright 2002 by Jim Moore <jim@scolamoore.com>.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf;
51
52 import java.awt.AlphaComposite JavaDoc;
53 import java.awt.BasicStroke JavaDoc;
54 import java.awt.Color JavaDoc;
55 import java.awt.Component JavaDoc;
56 import java.awt.Composite JavaDoc;
57 import java.awt.Font JavaDoc;
58 import java.awt.FontMetrics JavaDoc;
59 import java.awt.GradientPaint JavaDoc;
60 import java.awt.Graphics JavaDoc;
61 import java.awt.Graphics2D JavaDoc;
62 import java.awt.GraphicsConfiguration JavaDoc;
63 import java.awt.Image JavaDoc;
64 import java.awt.MediaTracker JavaDoc;
65 import java.awt.Paint JavaDoc;
66 import java.awt.Polygon JavaDoc;
67 import java.awt.Rectangle JavaDoc;
68 import java.awt.RenderingHints JavaDoc;
69 import java.awt.Shape JavaDoc;
70 import java.awt.Stroke JavaDoc;
71 import java.awt.TexturePaint JavaDoc;
72 import java.awt.Transparency JavaDoc;
73 import java.awt.RenderingHints.Key;
74 import java.awt.font.FontRenderContext JavaDoc;
75 import java.awt.font.GlyphVector JavaDoc;
76 import java.awt.font.TextAttribute JavaDoc;
77 import java.awt.geom.AffineTransform JavaDoc;
78 import java.awt.geom.Arc2D JavaDoc;
79 import java.awt.geom.Area JavaDoc;
80 import java.awt.geom.Ellipse2D JavaDoc;
81 import java.awt.geom.Line2D JavaDoc;
82 import java.awt.geom.NoninvertibleTransformException JavaDoc;
83 import java.awt.geom.PathIterator JavaDoc;
84 import java.awt.geom.Point2D JavaDoc;
85 import java.awt.geom.Rectangle2D JavaDoc;
86 import java.awt.geom.RoundRectangle2D JavaDoc;
87 import java.awt.image.BufferedImage JavaDoc;
88 import java.awt.image.BufferedImageOp JavaDoc;
89 import java.awt.image.ColorModel JavaDoc;
90 import java.awt.image.ImageObserver JavaDoc;
91 import java.awt.image.RenderedImage JavaDoc;
92 import java.awt.image.WritableRaster JavaDoc;
93 import java.awt.image.renderable.RenderableImage JavaDoc;
94 import java.io.ByteArrayOutputStream JavaDoc;
95 import java.text.AttributedCharacterIterator JavaDoc;
96 import java.util.ArrayList JavaDoc;
97 import java.util.HashMap JavaDoc;
98 import java.util.Hashtable JavaDoc;
99 import java.util.Iterator JavaDoc;
100 import java.util.Map JavaDoc;
101 import java.util.Set JavaDoc;
102
103 import com.lowagie.text.pdf.internal.PolylineShape;
104
105 public class PdfGraphics2D extends Graphics2D JavaDoc {
106     
107     private static final int FILL = 1;
108     private static final int STROKE = 2;
109     private static final int CLIP = 3;
110     private BasicStroke JavaDoc strokeOne = new BasicStroke JavaDoc(1);
111     
112     private static final AffineTransform JavaDoc IDENTITY = new AffineTransform JavaDoc();
113     
114     private Font JavaDoc font;
115     private BaseFont baseFont;
116     private float fontSize;
117     private AffineTransform JavaDoc transform;
118     private Paint JavaDoc paint;
119     private Color JavaDoc background;
120     private float width;
121     private float height;
122     
123     private Area JavaDoc clip;
124     
125     private RenderingHints JavaDoc rhints = new RenderingHints JavaDoc(null);
126     
127     private Stroke JavaDoc stroke;
128     private Stroke JavaDoc originalStroke;
129     
130     private PdfContentByte cb;
131     
132     /** Storage for BaseFont objects created. */
133     private HashMap JavaDoc baseFonts;
134     
135     private boolean disposeCalled = false;
136     
137     private FontMapper fontMapper;
138     
139     private ArrayList JavaDoc kids;
140     
141     private boolean kid = false;
142     
143     private Graphics2D JavaDoc dg2 = new BufferedImage JavaDoc(2, 2, BufferedImage.TYPE_INT_RGB).createGraphics();
144     
145     private boolean onlyShapes = false;
146     
147     private Stroke JavaDoc oldStroke;
148     private Paint JavaDoc paintFill;
149     private Paint JavaDoc paintStroke;
150     
151     private MediaTracker JavaDoc mediaTracker;
152
153     // Added by Jurij Bilas
154
protected boolean underline; // indicates if the font style is underlined
155

156     protected PdfGState fillGState[] = new PdfGState[256];
157     protected PdfGState strokeGState[] = new PdfGState[256];
158     protected int currentFillGState = 255;
159     protected int currentStrokeGState = 255;
160     
161     public static final int AFM_DIVISOR = 1000; // used to calculate coordinates
162

163     private boolean convertImagesToJPEG = false;
164     private float jpegQuality = .95f;
165
166     // Added by Alexej Suchov
167
private float alpha;
168
169     // Added by Alexej Suchov
170
private Composite JavaDoc composite;
171
172     // Added by Alexej Suchov
173
private Paint JavaDoc realPaint;
174
175     private PdfGraphics2D() {
176         dg2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
177         setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
178     }
179     
180     /**
181      * Constructor for PDFGraphics2D.
182      *
183      */

184     PdfGraphics2D(PdfContentByte cb, float width, float height, FontMapper fontMapper, boolean onlyShapes, boolean convertImagesToJPEG, float quality) {
185         super();
186         dg2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
187         setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
188         try {
189             Class.forName("com.sun.image.codec.jpeg.JPEGCodec");
190         }
191         catch (Throwable JavaDoc t) {
192             convertImagesToJPEG = false;
193         }
194         this.convertImagesToJPEG = convertImagesToJPEG;
195         this.jpegQuality = quality;
196         this.onlyShapes = onlyShapes;
197         this.transform = new AffineTransform JavaDoc();
198         this.baseFonts = new HashMap JavaDoc();
199         if (!onlyShapes) {
200             this.fontMapper = fontMapper;
201             if (this.fontMapper == null)
202                 this.fontMapper = new DefaultFontMapper();
203         }
204         paint = Color.black;
205         background = Color.white;
206         setFont(new Font JavaDoc("sanserif", Font.PLAIN, 12));
207         this.cb = cb;
208         cb.saveState();
209         this.width = width;
210         this.height = height;
211         clip = new Area JavaDoc(new Rectangle2D.Float JavaDoc(0, 0, width, height));
212         clip(clip);
213         originalStroke = stroke = oldStroke = strokeOne;
214         setStrokeDiff(stroke, null);
215         cb.saveState();
216     }
217     
218     /**
219      * @see Graphics2D#draw(Shape)
220      */

221     public void draw(Shape JavaDoc s) {
222         followPath(s, STROKE);
223     }
224     
225     /**
226      * @see Graphics2D#drawImage(Image, AffineTransform, ImageObserver)
227      */

228     public boolean drawImage(Image JavaDoc img, AffineTransform JavaDoc xform, ImageObserver JavaDoc obs) {
229         return drawImage(img, null, xform, null, obs);
230     }
231     
232     /**
233      * @see Graphics2D#drawImage(BufferedImage, BufferedImageOp, int, int)
234      */

235     public void drawImage(BufferedImage JavaDoc img, BufferedImageOp JavaDoc op, int x, int y) {
236         BufferedImage JavaDoc result = img;
237         if (op != null) {
238             result = op.createCompatibleDestImage(img, img.getColorModel());
239             result = op.filter(img, result);
240         }
241         drawImage(result, x, y, null);
242     }
243     
244     /**
245      * @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform)
246      */

247     public void drawRenderedImage(RenderedImage JavaDoc img, AffineTransform JavaDoc xform) {
248         BufferedImage JavaDoc image = null;
249         if (img instanceof BufferedImage JavaDoc) {
250             image = (BufferedImage JavaDoc)img;
251         } else {
252             ColorModel JavaDoc cm = img.getColorModel();
253             int width = img.getWidth();
254             int height = img.getHeight();
255             WritableRaster JavaDoc raster = cm.createCompatibleWritableRaster(width, height);
256             boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
257             Hashtable JavaDoc properties = new Hashtable JavaDoc();
258             String JavaDoc[] keys = img.getPropertyNames();
259             if (keys!=null) {
260                 for (int i = 0; i < keys.length; i++) {
261                     properties.put(keys[i], img.getProperty(keys[i]));
262                 }
263             }
264             BufferedImage JavaDoc result = new BufferedImage JavaDoc(cm, raster, isAlphaPremultiplied, properties);
265             img.copyData(raster);
266             image=result;
267         }
268         drawImage(image, xform, null);
269     }
270     
271     /**
272      * @see Graphics2D#drawRenderableImage(RenderableImage, AffineTransform)
273      */

274     public void drawRenderableImage(RenderableImage JavaDoc img, AffineTransform JavaDoc xform) {
275         drawRenderedImage(img.createDefaultRendering(), xform);
276     }
277     
278     /**
279      * @see Graphics#drawString(String, int, int)
280      */

281     public void drawString(String JavaDoc s, int x, int y) {
282         drawString(s, (float)x, (float)y);
283     }
284     
285     /**
286      * Calculates position and/or stroke thickness depending on the font size
287      * @param d value to be converted
288      * @param i font size
289      * @return position and/or stroke thickness depending on the font size
290      */

291     public static double asPoints(double d, int i) {
292         return (d * (double)i) / (double)AFM_DIVISOR;
293     }
294     /**
295      * This routine goes through the attributes and sets the font
296      * before calling the actual string drawing routine
297      * @param iter
298      */

299     protected void doAttributes(AttributedCharacterIterator JavaDoc iter) {
300         underline = false;
301         Set JavaDoc set = iter.getAttributes().keySet();
302         for(Iterator JavaDoc iterator = set.iterator(); iterator.hasNext();) {
303             AttributedCharacterIterator.Attribute JavaDoc attribute = (AttributedCharacterIterator.Attribute JavaDoc)iterator.next();
304             if (!(attribute instanceof TextAttribute JavaDoc))
305                 continue;
306             TextAttribute JavaDoc textattribute = (TextAttribute JavaDoc)attribute;
307             if(textattribute.equals(TextAttribute.FONT)) {
308                 Font JavaDoc font = (Font JavaDoc)iter.getAttributes().get(textattribute);
309                 setFont(font);
310             }
311             else if(textattribute.equals(TextAttribute.UNDERLINE)) {
312                 if(iter.getAttributes().get(textattribute) == TextAttribute.UNDERLINE_ON)
313                     underline = true;
314             }
315             else if(textattribute.equals(TextAttribute.SIZE)) {
316                 Object JavaDoc obj = iter.getAttributes().get(textattribute);
317                 if(obj instanceof Integer JavaDoc) {
318                     int i = ((Integer JavaDoc)obj).intValue();
319                     setFont(getFont().deriveFont(getFont().getStyle(), i));
320                 }
321                 else if(obj instanceof Float JavaDoc) {
322                     float f = ((Float JavaDoc)obj).floatValue();
323                     setFont(getFont().deriveFont(getFont().getStyle(), f));
324                 }
325             }
326             else if(textattribute.equals(TextAttribute.FOREGROUND)) {
327                 setColor((Color JavaDoc) iter.getAttributes().get(textattribute));
328             }
329             else if(textattribute.equals(TextAttribute.FAMILY)) {
330               Font JavaDoc font = getFont();
331               Map JavaDoc fontAttributes = font.getAttributes();
332               fontAttributes.put(TextAttribute.FAMILY, iter.getAttributes().get(textattribute));
333               setFont(font.deriveFont(fontAttributes));
334             }
335             else if(textattribute.equals(TextAttribute.POSTURE)) {
336               Font JavaDoc font = getFont();
337               Map JavaDoc fontAttributes = font.getAttributes();
338               fontAttributes.put(TextAttribute.POSTURE, iter.getAttributes().get(textattribute));
339               setFont(font.deriveFont(fontAttributes));
340             }
341             else if(textattribute.equals(TextAttribute.WEIGHT)) {
342               Font JavaDoc font = getFont();
343               Map JavaDoc fontAttributes = font.getAttributes();
344               fontAttributes.put(TextAttribute.WEIGHT, iter.getAttributes().get(textattribute));
345               setFont(font.deriveFont(fontAttributes));
346             }
347         }
348     }
349
350     /**
351      * @see Graphics2D#drawString(String, float, float)
352      */

353     public void drawString(String JavaDoc s, float x, float y) {
354         if (s.length() == 0)
355             return;
356         setFillPaint();
357         if (onlyShapes) {
358             drawGlyphVector(this.font.layoutGlyphVector(getFontRenderContext(), s.toCharArray(), 0, s.length(), java.awt.Font.LAYOUT_LEFT_TO_RIGHT), x, y);
359 // Use the following line to compile in JDK 1.3
360
// drawGlyphVector(this.font.createGlyphVector(getFontRenderContext(), s), x, y);
361
}
362         else {
363             AffineTransform JavaDoc at = getTransform();
364             AffineTransform JavaDoc at2 = getTransform();
365             at2.translate(x, y);
366             at2.concatenate(font.getTransform());
367             setTransform(at2);
368             AffineTransform JavaDoc inverse = this.normalizeMatrix();
369             AffineTransform JavaDoc flipper = AffineTransform.getScaleInstance(1,-1);
370             inverse.concatenate(flipper);
371             double[] mx = new double[6];
372             inverse.getMatrix(mx);
373             cb.beginText();
374             cb.setFontAndSize(baseFont, fontSize);
375             cb.setTextMatrix((float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]);
376             Float JavaDoc fontTextAttributeWidth = (Float JavaDoc)font.getAttributes().get(TextAttribute.WIDTH);
377             fontTextAttributeWidth = (fontTextAttributeWidth == null)
378                                      ? TextAttribute.WIDTH_REGULAR
379                                      : fontTextAttributeWidth;
380             if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth))
381                 cb.setHorizontalScaling(100.0f / fontTextAttributeWidth.floatValue());
382             double width = 0;
383             if (font.getSize2D() > 0) {
384                 float scale = 1000 / font.getSize2D();
385                 width = font.deriveFont(AffineTransform.getScaleInstance(scale, scale)).getStringBounds(s, getFontRenderContext()).getWidth() / scale;
386             }
387             if (s.length() > 1) {
388                 float adv = ((float)width - baseFont.getWidthPoint(s, fontSize)) / (s.length() - 1);
389                 cb.setCharacterSpacing(adv);
390             }
391             cb.showText(s);
392             if (s.length() > 1) {
393                 cb.setCharacterSpacing(0);
394             }
395             if (!TextAttribute.WIDTH_REGULAR.equals(fontTextAttributeWidth))
396                 cb.setHorizontalScaling(100);
397             cb.endText();
398             setTransform(at);
399             if(underline)
400             {
401                 // These two are supposed to be taken from the .AFM file
402
//int UnderlinePosition = -100;
403
int UnderlineThickness = 50;
404                 //
405
double d = asPoints((double)UnderlineThickness, (int)fontSize);
406                 setStroke(new BasicStroke JavaDoc((float)d));
407                 y = (float)((double)(y) + asPoints((double)(UnderlineThickness), (int)fontSize));
408                 Line2D JavaDoc line = new Line2D.Double JavaDoc((double)x, (double)y, (double)(width+x), (double)y);
409                 draw(line);
410             }
411         }
412     }
413
414     /**
415      * @see Graphics#drawString(AttributedCharacterIterator, int, int)
416      */

417     public void drawString(AttributedCharacterIterator JavaDoc iterator, int x, int y) {
418         drawString(iterator, (float)x, (float)y);
419     }
420     
421     /**
422      * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)
423      */

424     public void drawString(AttributedCharacterIterator JavaDoc iter, float x, float y) {
425 /*
426         StringBuffer sb = new StringBuffer();
427         for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
428             sb.append(c);
429         }
430         drawString(sb.toString(),x,y);
431 */

432         StringBuffer JavaDoc stringbuffer = new StringBuffer JavaDoc(iter.getEndIndex());
433         for(char c = iter.first(); c != '\uFFFF'; c = iter.next())
434         {
435             if(iter.getIndex() == iter.getRunStart())
436             {
437                 if(stringbuffer.length() > 0)
438                 {
439                     drawString(stringbuffer.toString(), x, y);
440                     FontMetrics JavaDoc fontmetrics = getFontMetrics();
441                     x = (float)((double)x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
442                     stringbuffer.delete(0, stringbuffer.length());
443                 }
444                 doAttributes(iter);
445             }
446             stringbuffer.append(c);
447         }
448         
449         drawString(stringbuffer.toString(), x, y);
450         underline = false;
451     }
452     
453     /**
454      * @see Graphics2D#drawGlyphVector(GlyphVector, float, float)
455      */

456     public void drawGlyphVector(GlyphVector JavaDoc g, float x, float y) {
457         Shape JavaDoc s = g.getOutline(x, y);
458         fill(s);
459     }
460     
461     /**
462      * @see Graphics2D#fill(Shape)
463      */

464     public void fill(Shape JavaDoc s) {
465         followPath(s, FILL);
466     }
467     
468     /**
469      * @see Graphics2D#hit(Rectangle, Shape, boolean)
470      */

471     public boolean hit(Rectangle JavaDoc rect, Shape JavaDoc s, boolean onStroke) {
472         if (onStroke) {
473             s = stroke.createStrokedShape(s);
474         }
475         s = transform.createTransformedShape(s);
476         Area JavaDoc area = new Area JavaDoc(s);
477         if (clip != null)
478             area.intersect(clip);
479         return area.intersects(rect.x, rect.y, rect.width, rect.height);
480     }
481     
482     /**
483      * @see Graphics2D#getDeviceConfiguration()
484      */

485     public GraphicsConfiguration JavaDoc getDeviceConfiguration() {
486         return dg2.getDeviceConfiguration();
487     }
488     
489     /**
490      * Method contributed by Alexej Suchov
491      * @see Graphics2D#setComposite(Composite)
492      */

493     public void setComposite(Composite JavaDoc comp) {
494         
495         if (comp instanceof AlphaComposite JavaDoc) {
496
497             AlphaComposite JavaDoc composite = (AlphaComposite JavaDoc) comp;
498
499             if (composite.getRule() == 3) {
500
501                 alpha = composite.getAlpha();
502                 this.composite = composite;
503
504                 if (realPaint != null && (realPaint instanceof Color JavaDoc)) {
505
506                     Color JavaDoc c = (Color JavaDoc) realPaint;
507                     paint = new Color JavaDoc(c.getRed(), c.getGreen(), c.getBlue(),
508                             (int) ((float) c.getAlpha() * alpha));
509                 }
510                 return;
511             }
512         }
513
514         this.composite = comp;
515         alpha = 1.0F;
516
517     }
518     
519     /**
520      * Method contributed by Alexej Suchov
521      * @see Graphics2D#setPaint(Paint)
522      */

523     public void setPaint(Paint JavaDoc paint) {
524         if (paint == null)
525             return;
526         this.paint = paint;
527         realPaint = paint;
528
529         if ((composite instanceof AlphaComposite JavaDoc) && (paint instanceof Color JavaDoc)) {
530             
531             AlphaComposite JavaDoc co = (AlphaComposite JavaDoc) composite;
532             
533             if (co.getRule() == 3) {
534                 Color JavaDoc c = (Color JavaDoc) paint;
535                 this.paint = new Color JavaDoc(c.getRed(), c.getGreen(), c.getBlue(), (int) ((float) c.getAlpha() * alpha));
536                 realPaint = paint;
537             }
538         }
539
540     }
541
542     private Stroke JavaDoc transformStroke(Stroke JavaDoc stroke) {
543         if (!(stroke instanceof BasicStroke JavaDoc))
544             return stroke;
545         BasicStroke JavaDoc st = (BasicStroke JavaDoc)stroke;
546         float scale = (float)Math.sqrt(Math.abs(transform.getDeterminant()));
547         float dash[] = st.getDashArray();
548         if (dash != null) {
549             for (int k = 0; k < dash.length; ++k)
550                 dash[k] *= scale;
551         }
552         return new BasicStroke JavaDoc(st.getLineWidth() * scale, st.getEndCap(), st.getLineJoin(), st.getMiterLimit(), dash, st.getDashPhase() * scale);
553     }
554     
555     private void setStrokeDiff(Stroke JavaDoc newStroke, Stroke JavaDoc oldStroke) {
556         if (newStroke == oldStroke)
557             return;
558         if (!(newStroke instanceof BasicStroke JavaDoc))
559             return;
560         BasicStroke JavaDoc nStroke = (BasicStroke JavaDoc)newStroke;
561         boolean oldOk = (oldStroke instanceof BasicStroke JavaDoc);
562         BasicStroke JavaDoc oStroke = null;
563         if (oldOk)
564             oStroke = (BasicStroke JavaDoc)oldStroke;
565         if (!oldOk || nStroke.getLineWidth() != oStroke.getLineWidth())
566             cb.setLineWidth(nStroke.getLineWidth());
567         if (!oldOk || nStroke.getEndCap() != oStroke.getEndCap()) {
568             switch (nStroke.getEndCap()) {
569             case BasicStroke.CAP_BUTT:
570                 cb.setLineCap(0);
571                 break;
572             case BasicStroke.CAP_SQUARE:
573                 cb.setLineCap(2);
574                 break;
575             default:
576                 cb.setLineCap(1);
577             }
578         }
579         if (!oldOk || nStroke.getLineJoin() != oStroke.getLineJoin()) {
580             switch (nStroke.getLineJoin()) {
581             case BasicStroke.JOIN_MITER:
582                 cb.setLineJoin(0);
583                 break;
584             case BasicStroke.JOIN_BEVEL:
585                 cb.setLineJoin(2);
586                 break;
587             default:
588                 cb.setLineJoin(1);
589             }
590         }
591         if (!oldOk || nStroke.getMiterLimit() != oStroke.getMiterLimit())
592             cb.setMiterLimit(nStroke.getMiterLimit());
593         boolean makeDash;
594         if (oldOk) {
595             if (nStroke.getDashArray() != null) {
596                 if (nStroke.getDashPhase() != oStroke.getDashPhase()) {
597                     makeDash = true;
598                 }
599                 else if (!java.util.Arrays.equals(nStroke.getDashArray(), oStroke.getDashArray())) {
600                     makeDash = true;
601                 }
602                 else
603                     makeDash = false;
604             }
605             else if (oStroke.getDashArray() != null) {
606                 makeDash = true;
607             }
608             else
609                 makeDash = false;
610         }
611         else {
612             makeDash = true;
613         }
614         if (makeDash) {
615             float dash[] = nStroke.getDashArray();
616             if (dash == null)
617                 cb.setLiteral("[]0 d\n");
618             else {
619                 cb.setLiteral('[');
620                 int lim = dash.length;
621                 for (int k = 0; k < lim; ++k) {
622                     cb.setLiteral(dash[k]);
623                     cb.setLiteral(' ');
624                 }
625                 cb.setLiteral(']');
626                 cb.setLiteral(nStroke.getDashPhase());
627                 cb.setLiteral(" d\n");
628             }
629         }
630     }
631     
632     /**
633      * @see Graphics2D#setStroke(Stroke)
634      */

635     public void setStroke(Stroke JavaDoc s) {
636         originalStroke = s;
637         this.stroke = transformStroke(s);
638     }
639     
640     
641     /**
642      * Sets a rendering hint
643      * @param arg0
644      * @param arg1
645      */

646     public void setRenderingHint(Key arg0, Object JavaDoc arg1) {
647         if (arg1 != null) {
648             rhints.put(arg0, arg1);
649         } else {
650             rhints.remove(arg0);
651         }
652     }
653     
654     /**
655      * @param arg0 a key
656      * @return the rendering hint
657      */

658     public Object JavaDoc getRenderingHint(Key arg0) {
659         return rhints.get(arg0);
660     }
661     
662     /**
663      * @see Graphics2D#setRenderingHints(Map)
664      */

665     public void setRenderingHints(Map JavaDoc hints) {
666         rhints.clear();
667         rhints.putAll(hints);
668     }
669     
670     /**
671      * @see Graphics2D#addRenderingHints(Map)
672      */

673     public void addRenderingHints(Map JavaDoc hints) {
674         rhints.putAll(hints);
675     }
676     
677     /**
678      * @see Graphics2D#getRenderingHints()
679      */

680     public RenderingHints JavaDoc getRenderingHints() {
681         return rhints;
682     }
683     
684     /**
685      * @see Graphics#translate(int, int)
686      */

687     public void translate(int x, int y) {
688         translate((double)x, (double)y);
689     }
690     
691     /**
692      * @see Graphics2D#translate(double, double)
693      */

694     public void translate(double tx, double ty) {
695         transform.translate(tx,ty);
696     }
697     
698     /**
699      * @see Graphics2D#rotate(double)
700      */

701     public void rotate(double theta) {
702         transform.rotate(theta);
703     }
704     
705     /**
706      * @see Graphics2D#rotate(double, double, double)
707      */

708     public void rotate(double theta, double x, double y) {
709         transform.rotate(theta, x, y);
710     }
711     
712     /**
713      * @see Graphics2D#scale(double, double)
714      */

715     public void scale(double sx, double sy) {
716         transform.scale(sx, sy);
717         this.stroke = transformStroke(originalStroke);
718     }
719