<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * @(#)TextHitInfo.java	1.30 00/02/02
 *
 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * 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 &lt;code&gt;TextHitInfo&lt;/code&gt; class represents a character position in a
 * text model, and a &lt;b&gt;bias&lt;/b&gt;, or "side," of the character.  Biases are
 * either &lt;EM&gt;leading&lt;/EM&gt; (the left edge, for a left-to-right character)
 * or &lt;EM&gt;trailing&lt;/EM&gt; (the right edge, for a left-to-right character).
 * Instances of &lt;code&gt;TextHitInfo&lt;/code&gt; are used to specify caret and
 * insertion positions within text.
 * &lt;p&gt;
 * For example, consider the text "abc".  TextHitInfo.trailing(1)
 * corresponds to the right side of the 'b' in the text.
 * &lt;p&gt;
 * &lt;code&gt;TextHitInfo&lt;/code&gt; is used primarily by {@link TextLayout} and
 * clients of &lt;code&gt;TextLayout&lt;/code&gt;.  Clients of &lt;code&gt;TextLayout&lt;/code&gt; 
 * query &lt;code&gt;TextHitInfo&lt;/code&gt; 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 &lt;code&gt;TextHitInfo&lt;/code&gt; if the bias
 * is leading, and one character after if the bias is trailing.  The 
 * insertion offset for TextHitInfo.trailing(1) is 2.
 * &lt;p&gt;
 * Sometimes it is convenient to construct a &lt;code&gt;TextHitInfo&lt;/code&gt; with
 * the same insertion offset as an existing one, but on the opposite
 * character.  The &lt;code&gt;getOtherHit&lt;/code&gt; method constructs a new
 * &lt;code&gt;TextHitInfo&lt;/code&gt; with the same insertion offset as an existing 
 * one, with a hit on the character on the other side of the insertion offset. 
 * Calling &lt;code&gt;getOtherHit&lt;/code&gt; on trailing(1) would return leading(2).
 * In general, &lt;code&gt;getOtherHit&lt;/code&gt; for trailing(n) returns 
 * leading(n+1) and &lt;code&gt;getOtherHit&lt;/code&gt; for leading(n) 
 * returns trailing(n-1).
 * &lt;p&gt;
 * &lt;strong&gt;Example&lt;/strong&gt;:&lt;p&gt;
 * Converting a graphical point to an insertion point within a text 
 * model
 * &lt;blockquote&gt;&lt;pre&gt;
 * 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
 * &lt;/pre&gt;&lt;/blockquote&gt;
 *
 * @see TextLayout
 */

public final class TextHitInfo {
    private int charIndex;
    private boolean isLeadingEdge;

    /**
     * Constructs a new &lt;code&gt;TextHitInfo&lt;/code&gt;.
     * @param charIndex the index of the character hit
     * @param isLeadingEdge &lt;code&gt;true&lt;/code&gt; 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 &lt;code&gt;true&lt;/code&gt; if the leading edge of the character was
     * hit.
     * @return &lt;code&gt;true&lt;/code&gt; if the leading edge of the character was
     * hit; &lt;code&gt;false&lt;/code&gt; 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 &lt;code&gt;TextHitInfo&lt;/code&gt;, which is
     * also the &lt;code&gt;charIndex&lt;/code&gt; of this &lt;code&gt;TextHitInfo&lt;/code&gt;.
     */
    public int hashCode() {
        return charIndex;
    }

    /**
     * Returns &lt;code&gt;true&lt;/code&gt; if the specified &lt;code&gt;Object&lt;/code&gt; is a
     * &lt;code&gt;TextHitInfo&lt;/code&gt; and equals this &lt;code&gt;TextHitInfo&lt;/code&gt;. 
     * @param obj the &lt;code&gt;Object&lt;/code&gt; to test for equality
     * @return &lt;code&gt;true&lt;/code&gt; if the specified &lt;code&gt;Object&lt;/code&gt;
     * equals this &lt;code&gt;TextHitInfo&lt;/code&gt;; &lt;code&gt;false&lt;/code&gt; otherwise.
     */
    public boolean equals(Object obj) {
        return (obj instanceof TextHitInfo) &amp;&amp; equals((TextHitInfo)obj);
    }

    /**
     * Returns &lt;code&gt;true&lt;/code&gt; if the specified &lt;code&gt;TextHitInfo&lt;/code&gt;
     * has the same &lt;code&gt;charIndex&lt;/code&gt; and &lt;code&gt;isLeadingEdge&lt;/code&gt;
     * as this &lt;code&gt;TextHitInfo&lt;/code&gt;.  This is not the same as having
     * the same insertion offset.
     * @param hitInfo a specified &lt;code&gt;TextHitInfo&lt;/code&gt;
     * @return &lt;code&gt;true&lt;/code&gt; if the specified &lt;code&gt;TextHitInfo&lt;/code&gt;
     * has the same &lt;code&gt;charIndex&lt;/code&gt; and &lt;code&gt;isLeadingEdge&lt;/code&gt;
     * as this &lt;code&gt;TextHitInfo&lt;/code&gt;.
     */
    public boolean equals(TextHitInfo hitInfo) {
        return hitInfo != null &amp;&amp; charIndex == hitInfo.charIndex &amp;&amp;
            isLeadingEdge == hitInfo.isLeadingEdge;
    }

    /**
     * Returns a &lt;code&gt;String&lt;/code&gt; representing the hit for debugging
     * use only.
     * @return a &lt;code&gt;String&lt;/code&gt; representing this
     * &lt;code&gt;TextHitInfo&lt;/code&gt;.
     */
    public String toString() {
        return "TextHitInfo[" + charIndex + (isLeadingEdge ? "L" : "T")+"]";
    }
    
    /**
     * Creates a &lt;code&gt;TextHitInfo&lt;/code&gt; on the leading edge of the
     * character at the specified &lt;code&gt;charIndex&lt;/code&gt;.
     * @param charIndex the index of the character hit
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; on the leading edge of the
     * character at the specified &lt;code&gt;charIndex&lt;/code&gt;.
     */
    public static TextHitInfo leading(int charIndex) {
        return new TextHitInfo(charIndex, true);
    }

    /**
     * Creates a hit on the trailing edge of the character at 
     * the specified &lt;code&gt;charIndex&lt;/code&gt;.
     * @param charIndex the index of the character hit
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; on the trailing edge of the
     * character at the specified &lt;code&gt;charIndex&lt;/code&gt;.
     */
    public static TextHitInfo trailing(int charIndex) {
        return new TextHitInfo(charIndex, false);
    }

    /**
     * Creates a &lt;code&gt;TextHitInfo&lt;/code&gt; at the specified offset,
     * associated with the character before the offset.
     * @param offset an offset associated with the character before
     * the offset
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; at the specified offset.
     */
    public static TextHitInfo beforeOffset(int offset) {
        return new TextHitInfo(offset-1, false);
    }

    /**
     * Creates a &lt;code&gt;TextHitInfo&lt;/code&gt; at the specified offset,
     * associated with the character after the offset.
     * @param offset an offset associated with the character after
     * the offset
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; at the specified offset.
     */
    public static TextHitInfo afterOffset(int offset) {
        return new TextHitInfo(offset, true);
    }

    /**
     * Creates a &lt;code&gt;TextHitInfo&lt;/code&gt; on the other side of the
     * insertion point.  This &lt;code&gt;TextHitInfo&lt;/code&gt; remains unchanged.
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; on the other side of the 
     * insertion point.
     */
    public TextHitInfo getOtherHit() {
        if (isLeadingEdge) {
            return trailing(charIndex - 1);
        } else {
            return leading(charIndex + 1);
        }
    }

    /**
     * Creates a &lt;code&gt;TextHitInfo&lt;/code&gt; whose character index is offset
     * by &lt;code&gt;delta&lt;/code&gt; from the &lt;code&gt;charIndex&lt;/code&gt; of this
     * &lt;code&gt;TextHitInfo&lt;/code&gt;. This &lt;code&gt;TextHitInfo&lt;/code&gt; remains
     * unchanged.
     * @param delta the value to offset this &lt;code&gt;charIndex&lt;/code&gt;
     * @return a &lt;code&gt;TextHitInfo&lt;/code&gt; whose &lt;code&gt;charIndex&lt;/code&gt; is
     * offset by &lt;code&gt;delta&lt;/code&gt; from the &lt;code&gt;charIndex&lt;/code&gt; of 
     * this &lt;code&gt;TextHitInfo&lt;/code&gt;.
     */
    public TextHitInfo getOffsetHit(int delta) {
        return new TextHitInfo(charIndex + delta, isLeadingEdge);
    }
}
</pre></body></html>