KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)GlyphPainter2.java 1.22 04/09/01
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.text.*;
10 import java.awt.*;
11 import java.awt.font.*;
12 import java.awt.geom.Rectangle2D JavaDoc;
13
14 /**
15  * A class to perform rendering of the glyphs.
16  * This can be implemented to be stateless, or
17  * to hold some information as a cache to
18  * facilitate faster rendering and model/view
19  * translation. At a minimum, the GlyphPainter
20  * allows a View implementation to perform its
21  * duties independent of a particular version
22  * of JVM and selection of capabilities (i.e.
23  * shaping for i18n, etc).
24  * <p>
25  * This implementation is intended for operation
26  * under the JDK. It uses the
27  * java.awt.font.TextLayout class to do i18n capable
28  * rendering.
29  *
30  * @author Timothy Prinzing
31  * @version 1.22 09/01/04
32  * @see GlyphView
33  */

34 class GlyphPainter2 extends GlyphView.GlyphPainter JavaDoc {
35
36     public GlyphPainter2(TextLayout layout) {
37     this.layout = layout;
38     }
39
40     /**
41      * Create a painter to use for the given GlyphView.
42      */

43     public GlyphView.GlyphPainter JavaDoc getPainter(GlyphView JavaDoc v, int p0, int p1) {
44     return null;
45     }
46
47     /**
48      * Determine the span the glyphs given a start location
49      * (for tab expansion). This implementation assumes it
50      * has no tabs (i.e. TextLayout doesn't deal with tab
51      * expansion).
52      */

53     public float getSpan(GlyphView JavaDoc v, int p0, int p1,
54              TabExpander JavaDoc e, float x) {
55
56     if ((p0 == v.getStartOffset()) && (p1 == v.getEndOffset())) {
57         return layout.getAdvance();
58     }
59     int p = v.getStartOffset();
60     int index0 = p0 - p;
61     int index1 = p1 - p;
62
63     TextHitInfo hit0 = TextHitInfo.afterOffset(index0);
64     TextHitInfo hit1 = TextHitInfo.beforeOffset(index1);
65     float[] locs = layout.getCaretInfo(hit0);
66     float x0 = locs[0];
67     locs = layout.getCaretInfo(hit1);
68     float x1 = locs[0];
69     return (x1 > x0) ? x1 - x0 : x0 - x1;
70     }
71
72     public float getHeight(GlyphView JavaDoc v) {
73     return layout.getAscent() + layout.getDescent() + layout.getLeading();
74     }
75
76     /**
77      * Fetch the ascent above the baseline for the glyphs
78      * corresponding to the given range in the model.
79      */

80     public float getAscent(GlyphView JavaDoc v) {
81     return layout.getAscent();
82     }
83
84     /**
85      * Fetch the descent below the baseline for the glyphs
86      * corresponding to the given range in the model.
87      */

88     public float getDescent(GlyphView JavaDoc v) {
89     return layout.getDescent();
90     }
91
92     /**
93      * Paint the glyphs for the given view. This is implemented
94      * to only render if the Graphics is of type Graphics2D which
95      * is required by TextLayout (and this should be the case if
96      * running on the JDK).
97      */

98     public void paint(GlyphView JavaDoc v, Graphics g, Shape a, int p0, int p1) {
99     if (g instanceof Graphics2D) {
100         Rectangle2D JavaDoc alloc = a.getBounds2D();
101         Graphics2D g2d = (Graphics2D)g;
102         float y = (float) alloc.getY() + layout.getAscent() + layout.getLeading();
103         float x = (float) alloc.getX();
104             if( p0 > v.getStartOffset() || p1 < v.getEndOffset() ) {
105                 try {
106                     //TextLayout can't render only part of it's range, so if a
107
//partial range is required, add a clip region.
108
Shape s = v.modelToView(p0, Position.Bias.Forward,
109                                             p1, Position.Bias.Backward, a);
110                     Shape savedClip = g.getClip();
111                     g2d.clip(s);
112                     layout.draw(g2d, x, y);
113                     g.setClip(savedClip);
114                 } catch (BadLocationException JavaDoc e) {}
115             } else {
116                 layout.draw(g2d, x, y);
117             }
118     }
119     }
120
121     public Shape modelToView(GlyphView JavaDoc v, int pos, Position.Bias JavaDoc bias,
122                  Shape a) throws BadLocationException JavaDoc {
123     int offs = pos - v.getStartOffset();
124     Rectangle2D JavaDoc alloc = a.getBounds2D();
125     TextHitInfo hit = (bias == Position.Bias.Forward) ?
126         TextHitInfo.afterOffset(offs) : TextHitInfo.beforeOffset(offs);
127     float[] locs = layout.getCaretInfo(hit);
128
129     // vertical at the baseline, should use slope and check if glyphs
130
// are being rendered vertically.
131
alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight());
132     return alloc;
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 either <code>Position.Bias.Forward</code>
144      * or <code>Position.Bias.Backward</code> is returned as the
145      * zero-th element of this array
146      * @return the location within the model that best represents the
147      * given point of view
148      * @see View#viewToModel
149      */

150     public int viewToModel(GlyphView JavaDoc v, float x, float y, Shape a,
151                Position.Bias JavaDoc[] biasReturn) {
152
153     Rectangle2D JavaDoc alloc = (a instanceof Rectangle2D JavaDoc) ? (Rectangle2D JavaDoc)a : a.getBounds2D();
154         //Move the y co-ord of the hit onto the baseline. This is because TextLayout supports
155
//italic carets and we do not.
156
TextHitInfo hit = layout.hitTestChar(x - (float)alloc.getX(), 0);
157     int pos = hit.getInsertionIndex();
158     biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
159     return pos + v.getStartOffset();
160     }
161
162     /**
163      * Determines the model location that represents the
164      * maximum advance that fits within the given span.
165      * This could be used to break the given view. The result
166      * should be a location just shy of the given advance. This
167      * differs from viewToModel which returns the closest
168      * position which might be proud of the maximum advance.
169      *
170      * @param v the view to find the model location to break at.
171      * @param p0 the location in the model where the
172      * fragment should start it's representation >= 0.
173      * @param pos the graphic location along the axis that the
174      * broken view would occupy >= 0. This may be useful for
175      * things like tab calculations.
176      * @param len specifies the distance into the view
177      * where a potential break is desired >= 0.
178      * @return the maximum model location possible for a break.
179      * @see View#breakView
180      */

181     public int getBoundedPosition(GlyphView JavaDoc v, int p0, float x, float len) {
182         if( len < 0 )
183             throw new IllegalArgumentException JavaDoc("Length must be >= 0.");
184         // note: this only works because swing uses TextLayouts that are
185
// only pure rtl or pure ltr
186
TextHitInfo hit;
187         if (layout.isLeftToRight()) {
188             hit = layout.hitTestChar(len, 0);
189         } else {
190             hit = layout.hitTestChar(layout.getAdvance() - len, 0);
191         }
192         return v.getStartOffset() + hit.getCharIndex();
193     }
194     
195     /**
196      * Provides a way to determine the next visually represented model
197      * location that one might place a caret. Some views may not be
198      * visible, they might not be in the same order found in the model, or
199      * they just might not allow access to some of the locations in the
200      * model.
201      *
202      * @param v the view to use
203      * @param pos the position to convert >= 0
204      * @param a the allocated region to render into
205      * @param direction the direction from the current position that can
206      * be thought of as the arrow keys typically found on a keyboard.
207      * This may be SwingConstants.WEST, SwingConstants.EAST,
208      * SwingConstants.NORTH, or SwingConstants.SOUTH.
209      * @return the location within the model that best represents the next
210      * location visual position.
211      * @exception BadLocationException
212      * @exception IllegalArgumentException for an invalid direction
213      */

214         public int getNextVisualPositionFrom(GlyphView JavaDoc v, int pos,
215                                              Position.Bias JavaDoc b, Shape a,
216                          int direction,
217                          Position.Bias JavaDoc[] biasRet)
218         throws BadLocationException JavaDoc {
219
220         int startOffset = v.getStartOffset();
221         int endOffset = v.getEndOffset();
222         Segment JavaDoc text;
223             AbstractDocument JavaDoc doc;
224             boolean viewIsLeftToRight;
225             TextHitInfo currentHit, nextHit;
226         
227         switch (direction) {
228         case View.NORTH:
229         break;
230         case View.SOUTH:
231         break;
232         case View.EAST:
233                 doc = (AbstractDocument JavaDoc)v.getDocument();
234                 viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
235                 
236         if(startOffset == doc.getLength()) {
237             if(pos == -1) {
238             biasRet[0] = Position.Bias.Forward;
239             return startOffset;
240             }
241             // End case for bidi text where newline is at beginning
242
// of line.
243
return -1;
244         }
245         if(pos == -1) {
246                     // Entering view from the left.
247
if( viewIsLeftToRight ) {
248                         biasRet[0] = Position.Bias.Forward;
249                         return startOffset;
250                     } else {
251                         text = v.getText(endOffset - 1, endOffset);
252                         char c = text.array[text.offset];
253                         SegmentCache.releaseSharedSegment(text);
254                         if(c == '\n') {
255                             biasRet[0] = Position.Bias.Forward;
256                             return endOffset-1;
257                         }
258                         biasRet[0] = Position.Bias.Backward;
259                         return endOffset;
260                     }
261         }
262                 if( b==Position.Bias.Forward )
263                     currentHit = TextHitInfo.afterOffset(pos-startOffset);
264                 else
265                     currentHit = TextHitInfo.beforeOffset(pos-startOffset);
266                 nextHit = layout.getNextRightHit(currentHit);
267                 if( nextHit == null ) {
268                     return -1;
269                 }
270                 if( viewIsLeftToRight != layout.isLeftToRight() ) {
271                     // If the layout's base direction is different from
272
// this view's run direction, we need to use the weak
273
// carrat.
274
nextHit = layout.getVisualOtherHit(nextHit);
275                 }
276                 pos = nextHit.getInsertionIndex() + startOffset;
277                 
278         if(pos == endOffset) {
279                     // A move to the right from an internal position will
280
// only take us to the endOffset in a left to right run.
281
text = v.getText(endOffset - 1, endOffset);
282                     char c = text.array[text.offset];
283                     SegmentCache.releaseSharedSegment(text);
284             if(c == '\n') {
285             return -1;
286             }
287             biasRet[0] = Position.Bias.Backward;
288         }
289         else {
290             biasRet[0] = Position.Bias.Forward;
291         }
292         return pos;
293         case View.WEST:
294                 doc = (AbstractDocument JavaDoc)v.getDocument();
295                 viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
296                 
297         if(startOffset == doc.getLength()) {
298             if(pos == -1) {
299             biasRet[0] = Position.Bias.Forward;
300             return startOffset;
301             }
302             // End case for bidi text where newline is at beginning
303
// of line.
304
return -1;
305         }
306         if(pos == -1) {
307                     // Entering view from the right
308
if( viewIsLeftToRight ) {
309                         text = v.getText(endOffset - 1, endOffset);
310                         char c = text.array[text.offset];
311                         SegmentCache.releaseSharedSegment(text);
312                         if(c == '\n') {
313                             biasRet[0] = Position.Bias.Forward;
314                             return endOffset - 1;
315                         }
316                         biasRet[0] = Position.Bias.Backward;
317                         return endOffset;
318                     } else {
319                         biasRet[0] = Position.Bias.Forward;
320                         return startOffset;
321                    }
322         }
323                 if( b==Position.Bias.Forward )
324                     currentHit = TextHitInfo.afterOffset(pos-startOffset);
325                 else
326                     currentHit = TextHitInfo.beforeOffset(pos-startOffset);
327                 nextHit = layout.getNextLeftHit(currentHit);
328                 if( nextHit == null ) {
329                     return -1;
330                 }
331                 if( viewIsLeftToRight != layout.isLeftToRight() ) {
332                     // If the layout's base direction is different from
333
// this view's run direction, we need to use the weak
334
// carrat.
335
nextHit = layout.getVisualOtherHit(nextHit);
336                 }
337                 pos = nextHit.getInsertionIndex() + startOffset;
338
339         if(pos == endOffset) {
340                     // A move to the left from an internal position will
341
// only take us to the endOffset in a right to left run.
342
text = v.getText(endOffset - 1, endOffset);
343                     char c = text.array[text.offset];
344                     SegmentCache.releaseSharedSegment(text);
345             if(c == '\n') {
346             return -1;
347             }
348             biasRet[0] = Position.Bias.Backward;
349         }
350         else {
351             biasRet[0] = Position.Bias.Forward;
352         }
353         return pos;
354         default:
355         throw new IllegalArgumentException JavaDoc("Bad direction: " + direction);
356         }
357         return pos;
358
359     }
360     // --- variables ---------------------------------------------
361

362     TextLayout layout;
363
364 }
365
Popular Tags