1 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 ; 13 14 34 class GlyphPainter2 extends GlyphView.GlyphPainter { 35 36 public GlyphPainter2(TextLayout layout) { 37 this.layout = layout; 38 } 39 40 43 public GlyphView.GlyphPainter getPainter(GlyphView v, int p0, int p1) { 44 return null; 45 } 46 47 53 public float getSpan(GlyphView v, int p0, int p1, 54 TabExpander 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 v) { 73 return layout.getAscent() + layout.getDescent() + layout.getLeading(); 74 } 75 76 80 public float getAscent(GlyphView v) { 81 return layout.getAscent(); 82 } 83 84 88 public float getDescent(GlyphView v) { 89 return layout.getDescent(); 90 } 91 92 98 public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) { 99 if (g instanceof Graphics2D) { 100 Rectangle2D 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 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 e) {} 115 } else { 116 layout.draw(g2d, x, y); 117 } 118 } 119 } 120 121 public Shape modelToView(GlyphView v, int pos, Position.Bias bias, 122 Shape a) throws BadLocationException { 123 int offs = pos - v.getStartOffset(); 124 Rectangle2D 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 alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight()); 132 return alloc; 133 } 134 135 150 public int viewToModel(GlyphView v, float x, float y, Shape a, 151 Position.Bias [] biasReturn) { 152 153 Rectangle2D alloc = (a instanceof Rectangle2D ) ? (Rectangle2D )a : a.getBounds2D(); 154 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 181 public int getBoundedPosition(GlyphView v, int p0, float x, float len) { 182 if( len < 0 ) 183 throw new IllegalArgumentException ("Length must be >= 0."); 184 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 214 public int getNextVisualPositionFrom(GlyphView v, int pos, 215 Position.Bias b, Shape a, 216 int direction, 217 Position.Bias [] biasRet) 218 throws BadLocationException { 219 220 int startOffset = v.getStartOffset(); 221 int endOffset = v.getEndOffset(); 222 Segment text; 223 AbstractDocument 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 )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 return -1; 244 } 245 if(pos == -1) { 246 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 nextHit = layout.getVisualOtherHit(nextHit); 275 } 276 pos = nextHit.getInsertionIndex() + startOffset; 277 278 if(pos == endOffset) { 279 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 )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 return -1; 305 } 306 if(pos == -1) { 307 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 nextHit = layout.getVisualOtherHit(nextHit); 336 } 337 pos = nextHit.getInsertionIndex() + startOffset; 338 339 if(pos == endOffset) { 340 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 ("Bad direction: " + direction); 356 } 357 return pos; 358 359 } 360 362 TextLayout layout; 363 364 } 365 | Popular Tags |