From 73063c617436406a436bb367c8df6bc4c71ac357 Mon Sep 17 00:00:00 2001 From: Roman Kennke Date: Mon, 27 Nov 2006 13:11:16 +0000 Subject: 2006-11-27 Roman Kennke * java/awt/font/TextLayout.java (getCaretShape(TextHitInfo,Rectangle2D)): Implemented. (getCaretShape(TextHitInfo)): Use natural bounds. (getCaretShapes(int,Rectangle2D,CaretPolicy)): New API method. (getCaretShapes(int,Rectangle2D)): Delegate to new method above with DEFAULT_CARET_POLICY. (getCaretShapes(int)): Use natural bounds. --- ChangeLog | 10 +++++++ java/awt/font/TextLayout.java | 67 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index ded6bb442..450db4f2d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2006-11-27 Roman Kennke + + * java/awt/font/TextLayout.java + (getCaretShape(TextHitInfo,Rectangle2D)): Implemented. + (getCaretShape(TextHitInfo)): Use natural bounds. + (getCaretShapes(int,Rectangle2D,CaretPolicy)): New API method. + (getCaretShapes(int,Rectangle2D)): Delegate to new method + above with DEFAULT_CARET_POLICY. + (getCaretShapes(int)): Use natural bounds. + 2006-11-27 Roman Kennke * java/awt/font/TextLayout.java diff --git a/java/awt/font/TextLayout.java b/java/awt/font/TextLayout.java index 1e1cfa4cf..8bbf7e1fe 100644 --- a/java/awt/font/TextLayout.java +++ b/java/awt/font/TextLayout.java @@ -600,26 +600,73 @@ public final class TextLayout implements Cloneable return info; } - public Shape getCaretShape (TextHitInfo hit) + public Shape getCaretShape(TextHitInfo hit) { - return getCaretShape( hit, getBounds() ); + return getCaretShape(hit, getBounds()); } - public Shape getCaretShape (TextHitInfo hit, Rectangle2D bounds) - throws NotImplementedException + public Shape getCaretShape(TextHitInfo hit, Rectangle2D bounds) { - throw new Error ("not implemented"); + // TODO: Handle vertical shapes somehow. + float[] info = getCaretInfo(hit); + float x1 = info[0]; + float y1 = (float) bounds.getMinY(); + float x2 = info[0]; + float y2 = (float) bounds.getMaxY(); + if (info[1] != 0) + { + // Shift x1 and x2 according to the slope. + x1 -= y1 * info[1]; + x2 -= y2 * info[1]; + } + GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 2); + path.moveTo(x1, y1); + path.lineTo(x2, y2); + return path; } - public Shape[] getCaretShapes (int offset) + public Shape[] getCaretShapes(int offset) { - return getCaretShapes( offset, getBounds() ); + return getCaretShapes(offset, getNaturalBounds()); } - public Shape[] getCaretShapes (int offset, Rectangle2D bounds) - throws NotImplementedException + public Shape[] getCaretShapes(int offset, Rectangle2D bounds) { - throw new Error ("not implemented"); + return getCaretShapes(offset, bounds, DEFAULT_CARET_POLICY); + } + + public Shape[] getCaretShapes(int offset, Rectangle2D bounds, + CaretPolicy policy) + { + // The RI returns a 2-size array even when there's only one + // shape in it. + Shape[] carets = new Shape[2]; + TextHitInfo hit1 = TextHitInfo.afterOffset(offset); + int caretHit1 = hitToCaret(hit1); + TextHitInfo hit2 = hit1.getOtherHit(); + int caretHit2 = hitToCaret(hit2); + if (caretHit1 == caretHit2) + { + carets[0] = getCaretShape(hit1); + carets[1] = null; // The RI returns null in this seldom case. + } + else + { + Shape caret1 = getCaretShape(hit1); + Shape caret2 = getCaretShape(hit2); + TextHitInfo strong = policy.getStrongCaret(hit1, hit2, this); + if (strong == hit1) + { + carets[0] = caret1; + carets[1] = caret2; + } + else + { + carets[0] = caret2; + carets[1] = caret1; + } + } + return carets; } public int getCharacterCount () -- cgit v1.2.1