KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > renderer > BasicTextPainter


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.gvt.renderer;
19
20 import java.awt.font.FontRenderContext JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23
24 import org.apache.batik.gvt.TextNode;
25 import org.apache.batik.gvt.TextPainter;
26 import org.apache.batik.gvt.text.ConcreteTextLayoutFactory;
27 import org.apache.batik.gvt.text.Mark;
28 import org.apache.batik.gvt.text.TextHit;
29 import org.apache.batik.gvt.text.TextLayoutFactory;
30
31 /**
32  * Basic implementation of TextPainter which
33  * renders the attributed character iterator of a <tt>TextNode</tt>.
34  * Suitable for use with "standard" java.awt.font.TextAttributes only.
35  * @see java.awt.font.TextAttribute
36  *
37  * @author <a HREF="mailto:bill.haneman@ireland.sun.com">Bill Haneman</a>
38  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
39  * @version $Id: BasicTextPainter.java,v 1.19 2005/03/27 08:58:34 cam Exp $
40  */

41 public abstract class BasicTextPainter implements TextPainter {
42
43     private static TextLayoutFactory textLayoutFactory =
44     new ConcreteTextLayoutFactory();
45
46     /**
47      * The font render context to use.
48      */

49     protected FontRenderContext JavaDoc fontRenderContext =
50     new FontRenderContext JavaDoc(new AffineTransform JavaDoc(), true, true);
51
52     protected FontRenderContext JavaDoc aaOffFontRenderContext =
53     new FontRenderContext JavaDoc(new AffineTransform JavaDoc(), false, true);
54
55     protected TextLayoutFactory getTextLayoutFactory() {
56         return textLayoutFactory;
57     }
58
59     /**
60      * Given an X, y coordinate, AttributedCharacterIterator,
61      * return a Mark which encapsulates a "selection start" action.
62      * The standard order of method calls for selection is:
63      * selectAt(); [selectTo(),...], selectTo(); getSelection().
64      */

65     public Mark selectAt(double x, double y, TextNode node) {
66         return hitTest(x, y, node);
67     }
68
69     /**
70      * Given an X, y coordinate, starting Mark, AttributedCharacterIterator,
71      * return a Mark which encapsulates a "selection continued" action.
72      * The standard order of method calls for selection is:
73      * selectAt(); [selectTo(),...], selectTo(); getSelection().
74      */

75     public Mark selectTo(double x, double y, Mark beginMark) {
76     if (beginMark == null) {
77         return null;
78     } else {
79         return hitTest(x, y, beginMark.getTextNode());
80     }
81     }
82
83
84     /**
85      * Get a Rectangle2D in userspace coords which encloses the textnode
86      * glyphs just including the geometry info.
87      * @param node the TextNode to measure
88      */

89     public Rectangle2D JavaDoc getGeometryBounds(TextNode node) {
90         return getOutline(node).getBounds2D();
91     }
92
93     /**
94      * Returns the mark for the specified parameters.
95      */

96     protected abstract Mark hitTest(double x, double y, TextNode node);
97
98     // ------------------------------------------------------------------------
99
// Inner class - implementation of the Mark interface
100
// ------------------------------------------------------------------------
101

102     /**
103      * This TextPainter's implementation of the Mark interface.
104      */

105     protected static class BasicMark implements Mark {
106     
107         private TextNode node;
108         private TextHit hit;
109
110     /**
111      * Constructs a new Mark with the specified parameters.
112      */

113         protected BasicMark(TextNode node,
114                             TextHit hit) {
115             this.hit = hit;
116             this.node = node;
117         }
118
119         public TextHit getHit() {
120             return hit;
121         }
122
123         public TextNode getTextNode() {
124             return node;
125         }
126
127     /**
128      * Returns the index of the character that has been hit.
129      *
130      * @return The character index.
131      */

132         public int getCharIndex() {
133             return hit.getCharIndex();
134         }
135     }
136 }
137
138
139
Popular Tags