KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > output > java2d > Java2DCanvasProvider


1 /*
2  * $Id: Java2DCanvasProvider.java,v 1.3 2003/07/14 08:25:52 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.output.java2d;
59
60 import java.awt.BasicStroke JavaDoc;
61 import java.awt.Font JavaDoc;
62 import java.awt.Graphics2D JavaDoc;
63 import java.awt.font.FontRenderContext JavaDoc;
64 import java.awt.font.GlyphVector JavaDoc;
65 import java.awt.geom.Point2D JavaDoc;
66 import java.awt.geom.Rectangle2D JavaDoc;
67
68 import org.krysalis.barcode.output.AbstractCanvasProvider;
69 import org.krysalis.barcode.tools.UnitConv;
70
71 /**
72  * CanvasProvider implementation that renders to Java2D (AWT).
73  *
74  * @author Jeremias Maerki
75  */

76 public class Java2DCanvasProvider extends AbstractCanvasProvider {
77
78     private static final boolean DEBUG = false;
79
80     private Graphics2D JavaDoc g2d;
81
82     /**
83      * Creates a new Java2DCanvasProvider.
84      * <p>
85      * This class internally operates with millimeters (mm) as units. This
86      * means you have to apply the necessary transformation before rendering
87      * a barcode to obtain the expected size. See the source code for
88      * BitmapBuilder.java for an example.
89      * <p>
90      * To improve the quality of text output it is recommended that fractional
91      * font metrics be enabled on the Graphics2D object passed in:
92      * <br>
93      * <code>
94      * g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
95      * RenderingHints.VALUE_FRACTIONALMETRICS_ON);
96      * </code>
97      * @param g2d Graphics2D object to paint on
98      */

99     public Java2DCanvasProvider(Graphics2D JavaDoc g2d) {
100         this.g2d = g2d;
101     }
102
103     /** @see org.krysalis.barcode.output.CanvasProvider */
104     public void deviceFillRect(double x, double y, double w, double h) {
105         g2d.fill(new Rectangle2D.Double JavaDoc(x, y, w, h));
106     }
107
108     /** @see org.krysalis.barcode.output.CanvasProvider */
109     public void deviceDrawRect(double x, double y, double w, double h) {
110         g2d.draw(new Rectangle2D.Double JavaDoc(x, y, w, h));
111     }
112
113     /** @see org.krysalis.barcode.output.CanvasProvider */
114     public void deviceJustifiedText(String JavaDoc text, double x1, double x2, double y1,
115                             String JavaDoc fontName, double fontSize) {
116         deviceCenteredText(text, x1, x2, y1, fontName, fontSize, true);
117     }
118                             
119     /** @see org.krysalis.barcode.output.CanvasProvider */
120     public void deviceCenteredText(String JavaDoc text, double x1, double x2, double y1,
121                             String JavaDoc fontName, double fontSize) {
122         deviceCenteredText(text, x1, x2, y1, fontName, fontSize, false);
123     }
124                             
125     /**
126      * Draws centered text.
127      * @param text the text to draw
128      * @param x1 the left boundary
129      * @param x2 the right boundary
130      * @param y1 the y coordinate
131      * @param fontName the name of the font
132      * @param fontSize the size of the font
133      * @param justify true if the text should be justified instead of centered
134      */

135     public void deviceCenteredText(
136             String JavaDoc text,
137             double x1,
138             double x2,
139             double y1,
140             String JavaDoc fontName,
141             double fontSize,
142             boolean justify) {
143         if (DEBUG) {
144             System.out.println("deviceText " + x1 + " " + x2 + " "
145                     + (x2 - x1) + " " + y1 + " " + text);
146             System.out.println("fontSize: "
147                     + fontSize + "pt (" + UnitConv.pt2mm(fontSize) + "mm)");
148         }
149         Font JavaDoc font = new Font JavaDoc(fontName, Font.PLAIN,
150             (int)Math.round(UnitConv.pt2mm(fontSize)));
151         FontRenderContext JavaDoc frc = g2d.getFontRenderContext();
152         GlyphVector JavaDoc gv = font.createGlyphVector(frc, text);
153         
154         final float textwidth = (float)gv.getLogicalBounds().getWidth();
155         final float distributableSpace = (float)((x2 - x1) - textwidth);
156         final float intercharSpace;
157         if (gv.getNumGlyphs() > 1) {
158             intercharSpace = distributableSpace / (gv.getNumGlyphs() - 1);
159         } else {
160             intercharSpace = 0.0f;
161         }
162         if (DEBUG) {
163             System.out.println(gv.getLogicalBounds()
164                     + " " + gv.getVisualBounds());
165             System.out.println("textwidth=" + textwidth);
166             System.out.println("distributableSpace=" + distributableSpace);
167             System.out.println("intercharSpace=" + intercharSpace);
168         }
169         final float indent;
170         if (justify && text.length() > 1) {
171             indent = 0.0f;
172         } else {
173             indent = distributableSpace / 2;
174         }
175         Font JavaDoc oldFont = g2d.getFont();
176         g2d.setFont(font);
177         if (justify) {
178             //move the individual glyphs
179
float rx = 0.0f;
180             for (int i = 0; i < gv.getNumGlyphs(); i++) {
181                 Point2D JavaDoc point = gv.getGlyphPosition(i);
182                 point.setLocation(point.getX() + i * intercharSpace, point.getY());
183                 gv.setGlyphPosition(i, point);
184                 if (DEBUG) {
185                     System.out.println(i + " " + point
186                             + " " + gv.getGlyphLogicalBounds(i).getBounds2D());
187                     System.out.println(i + " " + text.substring(i, i + 1)
188                         + " " + gv.getGlyphMetrics(i).getBounds2D());
189                 }
190             }
191         }
192         g2d.drawGlyphVector(gv, (float)x1 + indent, (float)y1);
193         g2d.setFont(oldFont);
194         if (DEBUG) {
195             g2d.setStroke(new BasicStroke JavaDoc(0.01f));
196             g2d.draw(new Rectangle2D.Double JavaDoc(x1, y1 - UnitConv.pt2mm(fontSize),
197                 x2 - x1, UnitConv.pt2mm(fontSize)));
198             g2d.draw(new Rectangle2D.Double JavaDoc(x1 + indent,
199                 y1 - UnitConv.pt2mm(fontSize),
200                 textwidth, UnitConv.pt2mm(fontSize)));
201         }
202     }
203
204 }
205
Popular Tags