/*
* Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
* (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
* (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
*
* The original version of this source code and documentation is
* copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
* of IBM. These materials are provided under terms of a License
* Agreement between Taligent and Sun. This technology is protected
* by multiple US and International patents.
*
* This notice and attribution to Taligent may not be removed.
* Taligent is a registered trademark of Taligent, Inc.
*
*/
package java.awt.font;
import java.lang.String;
/**
* The TextHitInfo
class represents a character position in a
* text model, and a bias, or "side," of the character. Biases are
* either leading (the left edge, for a left-to-right character)
* or trailing (the right edge, for a left-to-right character).
* Instances of TextHitInfo
are used to specify caret and
* insertion positions within text.
*
* For example, consider the text "abc". TextHitInfo.trailing(1) * corresponds to the right side of the 'b' in the text. *
* TextHitInfo
is used primarily by {@link TextLayout} and
* clients of TextLayout
. Clients of TextLayout
* query TextHitInfo
instances for an insertion offset, where
* new text is inserted into the text model. The insertion offset is equal
* to the character position in the TextHitInfo
if the bias
* is leading, and one character after if the bias is trailing. The
* insertion offset for TextHitInfo.trailing(1) is 2.
*
* Sometimes it is convenient to construct a TextHitInfo
with
* the same insertion offset as an existing one, but on the opposite
* character. The getOtherHit
method constructs a new
* TextHitInfo
with the same insertion offset as an existing
* one, with a hit on the character on the other side of the insertion offset.
* Calling getOtherHit
on trailing(1) would return leading(2).
* In general, getOtherHit
for trailing(n) returns
* leading(n+1) and getOtherHit
for leading(n)
* returns trailing(n-1).
*
* Example:
* Converting a graphical point to an insertion point within a text * model *
* * @see TextLayout */ public final class TextHitInfo { private int charIndex; private boolean isLeadingEdge; /** * Constructs a new* TextLayout layout = ...; * Point2D.Float hitPoint = ...; * TextHitInfo hitInfo = layout.hitTestChar(hitPoint.x, hitPoint.y); * int insPoint = hitInfo.getInsertionIndex(); * // insPoint is relative to layout; may need to adjust for use * // in a text model *
TextHitInfo
.
* @param charIndex the index of the character hit
* @param isLeadingEdge true
if the leading edge of the
* character was hit
*/
private TextHitInfo(int charIndex, boolean isLeadingEdge) {
this.charIndex = charIndex;
this.isLeadingEdge = isLeadingEdge;
}
/**
* Returns the index of the character hit.
* @return the index of the character hit.
*/
public int getCharIndex() {
return charIndex;
}
/**
* Returns true
if the leading edge of the character was
* hit.
* @return true
if the leading edge of the character was
* hit; false
otherwise.
*/
public boolean isLeadingEdge() {
return isLeadingEdge;
}
/**
* Returns the insertion index. This is the character index if
* the leading edge of the character was hit, and one greater
* than the character index if the trailing edge was hit.
* @return the insertion index.
*/
public int getInsertionIndex() {
return isLeadingEdge ? charIndex : charIndex + 1;
}
/**
* Returns the hash code.
* @return the hash code of this TextHitInfo
, which is
* also the charIndex
of this TextHitInfo
.
*/
public int hashCode() {
return charIndex;
}
/**
* Returns true
if the specified Object
is a
* TextHitInfo
and equals this TextHitInfo
.
* @param obj the Object
to test for equality
* @return true
if the specified Object
* equals this TextHitInfo
; false
otherwise.
*/
public boolean equals(Object obj) {
return (obj instanceof TextHitInfo) && equals((TextHitInfo)obj);
}
/**
* Returns true
if the specified TextHitInfo
* has the same charIndex
and isLeadingEdge
* as this TextHitInfo
. This is not the same as having
* the same insertion offset.
* @param hitInfo a specified TextHitInfo
* @return true
if the specified TextHitInfo
* has the same charIndex
and isLeadingEdge
* as this TextHitInfo
.
*/
public boolean equals(TextHitInfo hitInfo) {
return hitInfo != null && charIndex == hitInfo.charIndex &&
isLeadingEdge == hitInfo.isLeadingEdge;
}
/**
* Returns a String
representing the hit for debugging
* use only.
* @return a String
representing this
* TextHitInfo
.
*/
public String toString() {
return "TextHitInfo[" + charIndex + (isLeadingEdge ? "L" : "T")+"]";
}
/**
* Creates a TextHitInfo
on the leading edge of the
* character at the specified charIndex
.
* @param charIndex the index of the character hit
* @return a TextHitInfo
on the leading edge of the
* character at the specified charIndex
.
*/
public static TextHitInfo leading(int charIndex) {
return new TextHitInfo(charIndex, true);
}
/**
* Creates a hit on the trailing edge of the character at
* the specified charIndex
.
* @param charIndex the index of the character hit
* @return a TextHitInfo
on the trailing edge of the
* character at the specified charIndex
.
*/
public static TextHitInfo trailing(int charIndex) {
return new TextHitInfo(charIndex, false);
}
/**
* Creates a TextHitInfo
at the specified offset,
* associated with the character before the offset.
* @param offset an offset associated with the character before
* the offset
* @return a TextHitInfo
at the specified offset.
*/
public static TextHitInfo beforeOffset(int offset) {
return new TextHitInfo(offset-1, false);
}
/**
* Creates a TextHitInfo
at the specified offset,
* associated with the character after the offset.
* @param offset an offset associated with the character after
* the offset
* @return a TextHitInfo
at the specified offset.
*/
public static TextHitInfo afterOffset(int offset) {
return new TextHitInfo(offset, true);
}
/**
* Creates a TextHitInfo
on the other side of the
* insertion point. This TextHitInfo
remains unchanged.
* @return a TextHitInfo
on the other side of the
* insertion point.
*/
public TextHitInfo getOtherHit() {
if (isLeadingEdge) {
return trailing(charIndex - 1);
} else {
return leading(charIndex + 1);
}
}
/**
* Creates a TextHitInfo
whose character index is offset
* by delta
from the charIndex
of this
* TextHitInfo
. This TextHitInfo
remains
* unchanged.
* @param delta the value to offset this charIndex
* @return a TextHitInfo
whose charIndex
is
* offset by delta
from the charIndex
of
* this TextHitInfo
.
*/
public TextHitInfo getOffsetHit(int delta) {
return new TextHitInfo(charIndex + delta, isLeadingEdge);
}
}