KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > GlyphPainter1


1 /*
2  * @(#)GlyphPainter1.java 1.20 06/04/10
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.awt.*;
10
11 /**
12  * A class to perform rendering of the glyphs.
13  * This can be implemented to be stateless, or
14  * to hold some information as a cache to
15  * facilitate faster rendering and model/view
16  * translation. At a minimum, the GlyphPainter
17  * allows a View implementation to perform its
18  * duties independent of a particular version
19  * of JVM and selection of capabilities (i.e.
20  * shaping for i18n, etc).
21  * <p>
22  * This implementation is intended for operation
23  * under the JDK1.1 API of the Java Platform.
24  * Since the JDK is backward compatible with
25  * JDK1.1 API, this class will also function on
26  * Java 2. The JDK introduces improved
27  * API for rendering text however, so the GlyphPainter2
28  * is recommended for the DK.
29  *
30  * @author Timothy Prinzing
31  * @version 1.20 04/10/06
32  * @see GlyphView
33  */

34 class GlyphPainter1 extends GlyphView.GlyphPainter JavaDoc {
35
36     /**
37      * Determine the span the glyphs given a start location
38      * (for tab expansion).
39      */

40     public float getSpan(GlyphView JavaDoc v, int p0, int p1,
41              TabExpander JavaDoc e, float x) {
42     sync(v);
43     Segment JavaDoc text = v.getText(p0, p1);
44         int[] justificationData = getJustificationData(v);
45         int width = Utilities.getTabbedTextWidth(v, text, metrics, (int) x, e, p0,
46                                                  justificationData);
47         SegmentCache.releaseSharedSegment(text);
48     return width;
49     }
50
51     public float getHeight(GlyphView JavaDoc v) {
52     sync(v);
53     return metrics.getHeight();
54     }
55
56     /**
57      * Fetches the ascent above the baseline for the glyphs
58      * corresponding to the given range in the model.
59      */

60     public float getAscent(GlyphView JavaDoc v) {
61     sync(v);
62     return metrics.getAscent();
63     }
64
65     /**
66      * Fetches the descent below the baseline for the glyphs
67      * corresponding to the given range in the model.
68      */

69     public float getDescent(GlyphView JavaDoc v) {
70     sync(v);
71     return metrics.getDescent();
72     }
73
74     /**
75      * Paints the glyphs representing the given range.
76      */

77     public void paint(GlyphView JavaDoc v, Graphics g, Shape a, int p0, int p1) {
78     sync(v);
79     Segment JavaDoc text;
80     TabExpander JavaDoc expander = v.getTabExpander();
81     Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
82
83     // determine the x coordinate to render the glyphs
84
int x = alloc.x;
85     int p = v.getStartOffset();
86         int[] justificationData = getJustificationData(v);
87     if (p != p0) {
88         text = v.getText(p, p0);
89             int width = Utilities.getTabbedTextWidth(v, text, metrics, x, expander, p,
90                                                      justificationData);
91         x += width;
92             SegmentCache.releaseSharedSegment(text);
93     }
94
95     // determine the y coordinate to render the glyphs
96
int y = alloc.y + metrics.getHeight() - metrics.getDescent();
97
98     // render the glyphs
99
text = v.getText(p0, p1);
100     g.setFont(metrics.getFont());
101
102         Utilities.drawTabbedText(v, text, x, y, g, expander,p0,
103                                  justificationData);
104         SegmentCache.releaseSharedSegment(text);
105     }
106
107     public Shape modelToView(GlyphView JavaDoc v, int pos, Position.Bias JavaDoc bias,
108                  Shape a) throws BadLocationException JavaDoc {
109
110         sync(v);
111     Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
112     int p0 = v.getStartOffset();
113     int p1 = v.getEndOffset();
114     TabExpander JavaDoc expander = v.getTabExpander();
115     Segment JavaDoc text;
116
117     if(pos == p1) {
118         // The caller of this is left to right and borders a right to
119
// left view, return our end location.
120
return new Rectangle(alloc.x + alloc.width, alloc.y, 0,
121                  metrics.getHeight());
122     }
123     if ((pos >= p0) && (pos <= p1)) {
124         // determine range to the left of the position
125
text = v.getText(p0, pos);
126             int[] justificationData = getJustificationData(v);
127             int width = Utilities.getTabbedTextWidth(v, text, metrics, alloc.x, expander, p0,
128                                                      justificationData);
129             SegmentCache.releaseSharedSegment(text);
130         return new Rectangle(alloc.x + width, alloc.y, 0, metrics.getHeight());
131     }
132     throw new BadLocationException JavaDoc("modelToView - can't convert", p1);
133     }
134
135     /**
136      * Provides a mapping from the view coordinate space to the logical
137      * coordinate space of the model.
138      *
139      * @param v the view containing the view coordinates
140      * @param x the X coordinate
141      * @param y the Y coordinate
142      * @param a the allocated region to render into
143      * @param biasReturn always returns <code>Position.Bias.Forward</code>
144      * as the zero-th element of this array
145      * @return the location within the model that best represents the
146      * given point in the view
147      * @see View#viewToModel
148      */

149     public int viewToModel(GlyphView JavaDoc v, float x, float y, Shape a,
150                Position.Bias JavaDoc[] biasReturn) {
151
152         sync(v);
153     Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
154     int p0 = v.getStartOffset();
155     int p1 = v.getEndOffset();
156     TabExpander JavaDoc expander = v.getTabExpander();
157     Segment JavaDoc text = v.getText(p0, p1);
158         int[] justificationData = getJustificationData(v);
159         int offs = Utilities.getTabbedTextOffset(v, text, metrics,
160                                                  alloc.x, (int) x, expander, p0,
161                                                  justificationData);
162         SegmentCache.releaseSharedSegment(text);
163     int retValue = p0 + offs;
164     if(retValue == p1) {
165             // No need to return backward bias as GlyphPainter1 is used for
166
// ltr text only.
167
retValue--;
168         }
169         biasReturn[0] = Position.Bias.Forward;
170     return retValue;
171     }
172
173     /**
174      * Determines the best location (in the model) to break
175      * the given view.
176      * This method attempts to break on a whitespace
177      * location. If a whitespace location can't be found, the
178      * nearest character location is returned.
179      *
180      * @param v the view
181      * @param p0 the location in the model where the
182      * fragment should start its representation >= 0
183      * @param pos the graphic location along the axis that the
184      * broken view would occupy >= 0; this may be useful for
185      * things like tab calculations
186      * @param len specifies the distance into the view
187      * where a potential break is desired >= 0
188      * @return the model location desired for a break
189      * @see View#breakView
190      */

191     public int getBoundedPosition(GlyphView JavaDoc v, int p0, float x, float len) {
192     sync(v);
193     TabExpander JavaDoc expander = v.getTabExpander();
194     Segment JavaDoc s = v.getText(p0, v.getEndOffset());
195         int[] justificationData = getJustificationData(v);
196         int index = Utilities.getTabbedTextOffset(v, s, metrics, (int)x, (int)(x+len),
197                                                   expander, p0, false,
198                                                   justificationData);
199         SegmentCache.releaseSharedSegment(s);
200     int p1 = p0 + index;
201     return p1;
202     }
203
204     void sync(GlyphView JavaDoc v) {
205     Font f = v.getFont();
206     if ((metrics == null) || (! f.equals(metrics.getFont()))) {
207         // fetch a new FontMetrics
208
Container c = v.getContainer();
209             metrics = (c != null) ? c.getFontMetrics(f) :
210                 Toolkit.getDefaultToolkit().getFontMetrics(f);
211     }
212     }
213
214
215
216     /**
217      * @return justificationData from the ParagraphRow this GlyphView
218      * is in or {@code null} if no justification is needed
219      */

220     private int[] getJustificationData(GlyphView JavaDoc v) {
221         View JavaDoc parent = v.getParent();
222         int [] ret = null;
223         if (parent instanceof ParagraphView.Row JavaDoc) {
224             ParagraphView.Row JavaDoc row = ((ParagraphView.Row JavaDoc) parent);
225             ret = row.justificationData;
226         }
227         return ret;
228     }
229     
230     // --- variables ---------------------------------------------
231

232     FontMetrics metrics;
233 }
234
Popular Tags