2
|
1 |
/*
|
|
2 |
* Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.text;
|
|
26 |
|
|
27 |
import java.text.*;
|
|
28 |
import java.awt.*;
|
|
29 |
import java.awt.font.*;
|
|
30 |
import java.awt.geom.Rectangle2D;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A class to perform rendering of the glyphs.
|
|
34 |
* This can be implemented to be stateless, or
|
|
35 |
* to hold some information as a cache to
|
|
36 |
* facilitate faster rendering and model/view
|
|
37 |
* translation. At a minimum, the GlyphPainter
|
|
38 |
* allows a View implementation to perform its
|
|
39 |
* duties independent of a particular version
|
|
40 |
* of JVM and selection of capabilities (i.e.
|
|
41 |
* shaping for i18n, etc).
|
|
42 |
* <p>
|
|
43 |
* This implementation is intended for operation
|
|
44 |
* under the JDK. It uses the
|
|
45 |
* java.awt.font.TextLayout class to do i18n capable
|
|
46 |
* rendering.
|
|
47 |
*
|
|
48 |
* @author Timothy Prinzing
|
|
49 |
* @see GlyphView
|
|
50 |
*/
|
|
51 |
class GlyphPainter2 extends GlyphView.GlyphPainter {
|
|
52 |
|
|
53 |
public GlyphPainter2(TextLayout layout) {
|
|
54 |
this.layout = layout;
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Create a painter to use for the given GlyphView.
|
|
59 |
*/
|
|
60 |
public GlyphView.GlyphPainter getPainter(GlyphView v, int p0, int p1) {
|
|
61 |
return null;
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Determine the span the glyphs given a start location
|
|
66 |
* (for tab expansion). This implementation assumes it
|
|
67 |
* has no tabs (i.e. TextLayout doesn't deal with tab
|
|
68 |
* expansion).
|
|
69 |
*/
|
|
70 |
public float getSpan(GlyphView v, int p0, int p1,
|
|
71 |
TabExpander e, float x) {
|
|
72 |
|
|
73 |
if ((p0 == v.getStartOffset()) && (p1 == v.getEndOffset())) {
|
|
74 |
return layout.getAdvance();
|
|
75 |
}
|
|
76 |
int p = v.getStartOffset();
|
|
77 |
int index0 = p0 - p;
|
|
78 |
int index1 = p1 - p;
|
|
79 |
|
|
80 |
TextHitInfo hit0 = TextHitInfo.afterOffset(index0);
|
|
81 |
TextHitInfo hit1 = TextHitInfo.beforeOffset(index1);
|
|
82 |
float[] locs = layout.getCaretInfo(hit0);
|
|
83 |
float x0 = locs[0];
|
|
84 |
locs = layout.getCaretInfo(hit1);
|
|
85 |
float x1 = locs[0];
|
|
86 |
return (x1 > x0) ? x1 - x0 : x0 - x1;
|
|
87 |
}
|
|
88 |
|
|
89 |
public float getHeight(GlyphView v) {
|
|
90 |
return layout.getAscent() + layout.getDescent() + layout.getLeading();
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Fetch the ascent above the baseline for the glyphs
|
|
95 |
* corresponding to the given range in the model.
|
|
96 |
*/
|
|
97 |
public float getAscent(GlyphView v) {
|
|
98 |
return layout.getAscent();
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Fetch the descent below the baseline for the glyphs
|
|
103 |
* corresponding to the given range in the model.
|
|
104 |
*/
|
|
105 |
public float getDescent(GlyphView v) {
|
|
106 |
return layout.getDescent();
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Paint the glyphs for the given view. This is implemented
|
|
111 |
* to only render if the Graphics is of type Graphics2D which
|
|
112 |
* is required by TextLayout (and this should be the case if
|
|
113 |
* running on the JDK).
|
|
114 |
*/
|
|
115 |
public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) {
|
|
116 |
if (g instanceof Graphics2D) {
|
|
117 |
Rectangle2D alloc = a.getBounds2D();
|
|
118 |
Graphics2D g2d = (Graphics2D)g;
|
|
119 |
float y = (float) alloc.getY() + layout.getAscent() + layout.getLeading();
|
|
120 |
float x = (float) alloc.getX();
|
|
121 |
if( p0 > v.getStartOffset() || p1 < v.getEndOffset() ) {
|
|
122 |
try {
|
|
123 |
//TextLayout can't render only part of it's range, so if a
|
|
124 |
//partial range is required, add a clip region.
|
|
125 |
Shape s = v.modelToView(p0, Position.Bias.Forward,
|
|
126 |
p1, Position.Bias.Backward, a);
|
|
127 |
Shape savedClip = g.getClip();
|
|
128 |
g2d.clip(s);
|
|
129 |
layout.draw(g2d, x, y);
|
|
130 |
g.setClip(savedClip);
|
|
131 |
} catch (BadLocationException e) {}
|
|
132 |
} else {
|
|
133 |
layout.draw(g2d, x, y);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
public Shape modelToView(GlyphView v, int pos, Position.Bias bias,
|
|
139 |
Shape a) throws BadLocationException {
|
|
140 |
int offs = pos - v.getStartOffset();
|
|
141 |
Rectangle2D alloc = a.getBounds2D();
|
|
142 |
TextHitInfo hit = (bias == Position.Bias.Forward) ?
|
|
143 |
TextHitInfo.afterOffset(offs) : TextHitInfo.beforeOffset(offs);
|
|
144 |
float[] locs = layout.getCaretInfo(hit);
|
|
145 |
|
|
146 |
// vertical at the baseline, should use slope and check if glyphs
|
|
147 |
// are being rendered vertically.
|
|
148 |
alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight());
|
|
149 |
return alloc;
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Provides a mapping from the view coordinate space to the logical
|
|
154 |
* coordinate space of the model.
|
|
155 |
*
|
|
156 |
* @param v the view containing the view coordinates
|
|
157 |
* @param x the X coordinate
|
|
158 |
* @param y the Y coordinate
|
|
159 |
* @param a the allocated region to render into
|
|
160 |
* @param biasReturn either <code>Position.Bias.Forward</code>
|
|
161 |
* or <code>Position.Bias.Backward</code> is returned as the
|
|
162 |
* zero-th element of this array
|
|
163 |
* @return the location within the model that best represents the
|
|
164 |
* given point of view
|
|
165 |
* @see View#viewToModel
|
|
166 |
*/
|
|
167 |
public int viewToModel(GlyphView v, float x, float y, Shape a,
|
|
168 |
Position.Bias[] biasReturn) {
|
|
169 |
|
|
170 |
Rectangle2D alloc = (a instanceof Rectangle2D) ? (Rectangle2D)a : a.getBounds2D();
|
|
171 |
//Move the y co-ord of the hit onto the baseline. This is because TextLayout supports
|
|
172 |
//italic carets and we do not.
|
|
173 |
TextHitInfo hit = layout.hitTestChar(x - (float)alloc.getX(), 0);
|
|
174 |
int pos = hit.getInsertionIndex();
|
|
175 |
biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
|
|
176 |
return pos + v.getStartOffset();
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Determines the model location that represents the
|
|
181 |
* maximum advance that fits within the given span.
|
|
182 |
* This could be used to break the given view. The result
|
|
183 |
* should be a location just shy of the given advance. This
|
|
184 |
* differs from viewToModel which returns the closest
|
|
185 |
* position which might be proud of the maximum advance.
|
|
186 |
*
|
|
187 |
* @param v the view to find the model location to break at.
|
|
188 |
* @param p0 the location in the model where the
|
|
189 |
* fragment should start it's representation >= 0.
|
|
190 |
* @param pos the graphic location along the axis that the
|
|
191 |
* broken view would occupy >= 0. This may be useful for
|
|
192 |
* things like tab calculations.
|
|
193 |
* @param len specifies the distance into the view
|
|
194 |
* where a potential break is desired >= 0.
|
|
195 |
* @return the maximum model location possible for a break.
|
|
196 |
* @see View#breakView
|
|
197 |
*/
|
|
198 |
public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
|
|
199 |
if( len < 0 )
|
|
200 |
throw new IllegalArgumentException("Length must be >= 0.");
|
|
201 |
// note: this only works because swing uses TextLayouts that are
|
|
202 |
// only pure rtl or pure ltr
|
|
203 |
TextHitInfo hit;
|
|
204 |
if (layout.isLeftToRight()) {
|
|
205 |
hit = layout.hitTestChar(len, 0);
|
|
206 |
} else {
|
|
207 |
hit = layout.hitTestChar(layout.getAdvance() - len, 0);
|
|
208 |
}
|
|
209 |
return v.getStartOffset() + hit.getCharIndex();
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* Provides a way to determine the next visually represented model
|
|
214 |
* location that one might place a caret. Some views may not be
|
|
215 |
* visible, they might not be in the same order found in the model, or
|
|
216 |
* they just might not allow access to some of the locations in the
|
|
217 |
* model.
|
|
218 |
*
|
|
219 |
* @param v the view to use
|
|
220 |
* @param pos the position to convert >= 0
|
|
221 |
* @param a the allocated region to render into
|
|
222 |
* @param direction the direction from the current position that can
|
|
223 |
* be thought of as the arrow keys typically found on a keyboard.
|
|
224 |
* This may be SwingConstants.WEST, SwingConstants.EAST,
|
|
225 |
* SwingConstants.NORTH, or SwingConstants.SOUTH.
|
|
226 |
* @return the location within the model that best represents the next
|
|
227 |
* location visual position.
|
|
228 |
* @exception BadLocationException
|
|
229 |
* @exception IllegalArgumentException for an invalid direction
|
|
230 |
*/
|
|
231 |
public int getNextVisualPositionFrom(GlyphView v, int pos,
|
|
232 |
Position.Bias b, Shape a,
|
|
233 |
int direction,
|
|
234 |
Position.Bias[] biasRet)
|
|
235 |
throws BadLocationException {
|
|
236 |
|
|
237 |
int startOffset = v.getStartOffset();
|
|
238 |
int endOffset = v.getEndOffset();
|
|
239 |
Segment text;
|
|
240 |
AbstractDocument doc;
|
|
241 |
boolean viewIsLeftToRight;
|
|
242 |
TextHitInfo currentHit, nextHit;
|
|
243 |
|
|
244 |
switch (direction) {
|
|
245 |
case View.NORTH:
|
|
246 |
break;
|
|
247 |
case View.SOUTH:
|
|
248 |
break;
|
|
249 |
case View.EAST:
|
|
250 |
doc = (AbstractDocument)v.getDocument();
|
|
251 |
viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
|
|
252 |
|
|
253 |
if(startOffset == doc.getLength()) {
|
|
254 |
if(pos == -1) {
|
|
255 |
biasRet[0] = Position.Bias.Forward;
|
|
256 |
return startOffset;
|
|
257 |
}
|
|
258 |
// End case for bidi text where newline is at beginning
|
|
259 |
// of line.
|
|
260 |
return -1;
|
|
261 |
}
|
|
262 |
if(pos == -1) {
|
|
263 |
// Entering view from the left.
|
|
264 |
if( viewIsLeftToRight ) {
|
|
265 |
biasRet[0] = Position.Bias.Forward;
|
|
266 |
return startOffset;
|
|
267 |
} else {
|
|
268 |
text = v.getText(endOffset - 1, endOffset);
|
|
269 |
char c = text.array[text.offset];
|
|
270 |
SegmentCache.releaseSharedSegment(text);
|
|
271 |
if(c == '\n') {
|
|
272 |
biasRet[0] = Position.Bias.Forward;
|
|
273 |
return endOffset-1;
|
|
274 |
}
|
|
275 |
biasRet[0] = Position.Bias.Backward;
|
|
276 |
return endOffset;
|
|
277 |
}
|
|
278 |
}
|
|
279 |
if( b==Position.Bias.Forward )
|
|
280 |
currentHit = TextHitInfo.afterOffset(pos-startOffset);
|
|
281 |
else
|
|
282 |
currentHit = TextHitInfo.beforeOffset(pos-startOffset);
|
|
283 |
nextHit = layout.getNextRightHit(currentHit);
|
|
284 |
if( nextHit == null ) {
|
|
285 |
return -1;
|
|
286 |
}
|
|
287 |
if( viewIsLeftToRight != layout.isLeftToRight() ) {
|
|
288 |
// If the layout's base direction is different from
|
|
289 |
// this view's run direction, we need to use the weak
|
|
290 |
// carrat.
|
|
291 |
nextHit = layout.getVisualOtherHit(nextHit);
|
|
292 |
}
|
|
293 |
pos = nextHit.getInsertionIndex() + startOffset;
|
|
294 |
|
|
295 |
if(pos == endOffset) {
|
|
296 |
// A move to the right from an internal position will
|
|
297 |
// only take us to the endOffset in a left to right run.
|
|
298 |
text = v.getText(endOffset - 1, endOffset);
|
|
299 |
char c = text.array[text.offset];
|
|
300 |
SegmentCache.releaseSharedSegment(text);
|
|
301 |
if(c == '\n') {
|
|
302 |
return -1;
|
|
303 |
}
|
|
304 |
biasRet[0] = Position.Bias.Backward;
|
|
305 |
}
|
|
306 |
else {
|
|
307 |
biasRet[0] = Position.Bias.Forward;
|
|
308 |
}
|
|
309 |
return pos;
|
|
310 |
case View.WEST:
|
|
311 |
doc = (AbstractDocument)v.getDocument();
|
|
312 |
viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
|
|
313 |
|
|
314 |
if(startOffset == doc.getLength()) {
|
|
315 |
if(pos == -1) {
|
|
316 |
biasRet[0] = Position.Bias.Forward;
|
|
317 |
return startOffset;
|
|
318 |
}
|
|
319 |
// End case for bidi text where newline is at beginning
|
|
320 |
// of line.
|
|
321 |
return -1;
|
|
322 |
}
|
|
323 |
if(pos == -1) {
|
|
324 |
// Entering view from the right
|
|
325 |
if( viewIsLeftToRight ) {
|
|
326 |
text = v.getText(endOffset - 1, endOffset);
|
|
327 |
char c = text.array[text.offset];
|
|
328 |
SegmentCache.releaseSharedSegment(text);
|
|
329 |
if ((c == '\n') || Character.isSpaceChar(c)) {
|
|
330 |
biasRet[0] = Position.Bias.Forward;
|
|
331 |
return endOffset - 1;
|
|
332 |
}
|
|
333 |
biasRet[0] = Position.Bias.Backward;
|
|
334 |
return endOffset;
|
|
335 |
} else {
|
|
336 |
biasRet[0] = Position.Bias.Forward;
|
|
337 |
return startOffset;
|
|
338 |
}
|
|
339 |
}
|
|
340 |
if( b==Position.Bias.Forward )
|
|
341 |
currentHit = TextHitInfo.afterOffset(pos-startOffset);
|
|
342 |
else
|
|
343 |
currentHit = TextHitInfo.beforeOffset(pos-startOffset);
|
|
344 |
nextHit = layout.getNextLeftHit(currentHit);
|
|
345 |
if( nextHit == null ) {
|
|
346 |
return -1;
|
|
347 |
}
|
|
348 |
if( viewIsLeftToRight != layout.isLeftToRight() ) {
|
|
349 |
// If the layout's base direction is different from
|
|
350 |
// this view's run direction, we need to use the weak
|
|
351 |
// carrat.
|
|
352 |
nextHit = layout.getVisualOtherHit(nextHit);
|
|
353 |
}
|
|
354 |
pos = nextHit.getInsertionIndex() + startOffset;
|
|
355 |
|
|
356 |
if(pos == endOffset) {
|
|
357 |
// A move to the left from an internal position will
|
|
358 |
// only take us to the endOffset in a right to left run.
|
|
359 |
text = v.getText(endOffset - 1, endOffset);
|
|
360 |
char c = text.array[text.offset];
|
|
361 |
SegmentCache.releaseSharedSegment(text);
|
|
362 |
if(c == '\n') {
|
|
363 |
return -1;
|
|
364 |
}
|
|
365 |
biasRet[0] = Position.Bias.Backward;
|
|
366 |
}
|
|
367 |
else {
|
|
368 |
biasRet[0] = Position.Bias.Forward;
|
|
369 |
}
|
|
370 |
return pos;
|
|
371 |
default:
|
|
372 |
throw new IllegalArgumentException("Bad direction: " + direction);
|
|
373 |
}
|
|
374 |
return pos;
|
|
375 |
|
|
376 |
}
|
|
377 |
// --- variables ---------------------------------------------
|
|
378 |
|
|
379 |
TextLayout layout;
|
|
380 |
|
|
381 |
}
|