1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.text;
  27 
  28 import java.text.BreakIterator;
  29 
  30 import com.sun.javafx.scene.text.TextLayout;
  31 
  32 /**
  33  * Represents the hit information in a Text node.
  34  *
  35  * @since 9
  36  */
  37 public class HitInfo {
  38 
  39     private int charIndex;
  40     private boolean leading;
  41     private int insertionIndex;
  42     private String text;
  43 
  44     /**
  45      * Create a HitInfo object representing a text index and forward bias.
  46      *
  47      * @param charIndex the character index.
  48      * @param leading whether the hit is on the leading edge of the character. If it is false, it represents the trailing edge.
  49      */
  50     HitInfo(int charIndex, int insertionIndex, boolean leading, String text) {
  51         this.charIndex = charIndex;
  52         this.leading = leading;
  53         this.insertionIndex = insertionIndex;
  54         this.text = text;
  55     }
  56 
  57     /**
  58      * The index of the character which this hit information refers to.
  59      */
  60     public int getCharIndex() { return charIndex; }
  61 
  62     /**
  63      * Indicates whether the hit is on the leading edge of the character.
  64      * If it is false, it represents the trailing edge.
  65      */
  66     public boolean isLeading() { return leading; }
  67 
  68     private static BreakIterator charIterator = null;
  69     /**
  70      * Returns the index of the insertion position.
  71      */
  72     public int getInsertionIndex() {
  73         if (insertionIndex == -1) {
  74             insertionIndex = charIndex;
  75             if (!leading) {
  76                 if (text != null) {
  77                     // Skip complex character clusters / ligatures.
  78                     if (charIterator == null) {
  79                         charIterator = BreakIterator.getCharacterInstance();
  80                     }
  81                     charIterator.setText(text);
  82                     int next = charIterator.following(insertionIndex);
  83                     if (next == BreakIterator.DONE) {
  84                         insertionIndex += 1;
  85                     } else {
  86                         insertionIndex = next;
  87                     }
  88                 } else {
  89                     insertionIndex += 1;
  90                 }
  91             }
  92         }
  93         return insertionIndex;
  94     }
  95 
  96     @Override public String toString() {
  97         return "charIndex: " + charIndex + ", isLeading: " + leading + ", insertionIndex: " + getInsertionIndex();
  98     }
  99 }