jdk/src/share/classes/java/awt/font/TextLayout.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 11081 4a18889223e4
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996-2003, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of IBM. These materials are provided under terms of a License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Agreement between Taligent and Sun. This technology is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.awt.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.font.NumericShaper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.font.TextLine.TextLineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.geom.GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.text.AttributedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.text.AttributedCharacterIterator.Attribute;
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
    58
import java.text.CharacterIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.font.AttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.font.CoreMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.font.Decoration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import sun.font.FontLineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import sun.font.FontResolver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.font.GraphicComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.font.LayoutPathImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.text.CodePointIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <code>TextLayout</code> is an immutable graphical representation of styled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * character data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * It provides the following capabilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <li>implicit bidirectional analysis and reordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <li>cursor positioning and movement, including split cursors for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * mixed directional text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <li>highlighting, including both logical and visual highlighting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * for mixed directional text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <li>multiple baselines (roman, hanging, and centered),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <li>hit testing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <li>justification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li>default font substitution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <li>metric information such as ascent, descent, and advance, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * A <code>TextLayout</code> object can be rendered using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * its <code>draw</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <code>TextLayout</code> can be constructed either directly or through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the use of a {@link LineBreakMeasurer}.  When constructed directly, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * source text represents a single paragraph.  <code>LineBreakMeasurer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * allows styled text to be broken into lines that fit within a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * width.  See the <code>LineBreakMeasurer</code> documentation for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <code>TextLayout</code> construction logically proceeds as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <li>paragraph attributes are extracted and examined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <li>text is analyzed for bidirectional reordering, and reordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * information is computed if needed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <li>text is segmented into style runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <li>fonts are chosen for style runs, first by using a font if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * attribute {@link TextAttribute#FONT} is present, otherwise by computing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * a default font using the attributes that have been defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <li>if text is on multiple baselines, the runs or subruns are further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * broken into subruns sharing a common baseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <li>glyphvectors are generated for each run using the chosen font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <li>final bidirectional reordering is performed on the glyphvectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * All graphical information returned from a <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * object's methods is relative to the origin of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <code>TextLayout</code>, which is the intersection of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <code>TextLayout</code> object's baseline with its left edge.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * coordinates passed into a <code>TextLayout</code> object's methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * are assumed to be relative to the <code>TextLayout</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * origin.  Clients usually need to translate between a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <code>TextLayout</code> object's coordinate system and the coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * system in another object (such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * {@link java.awt.Graphics Graphics} object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <code>TextLayout</code> objects are constructed from styled text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * but they do not retain a reference to their source text.  Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * changes in the text previously used to generate a <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * do not affect the <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Three methods on a <code>TextLayout</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * (<code>getNextRightHit</code>, <code>getNextLeftHit</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <code>hitTestChar</code>) return instances of {@link TextHitInfo}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * The offsets contained in these <code>TextHitInfo</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * are relative to the start of the <code>TextLayout</code>, <b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * to the text used to create the <code>TextLayout</code>.  Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <code>TextLayout</code> methods that accept <code>TextHitInfo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * instances as parameters expect the <code>TextHitInfo</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * offsets to be relative to the <code>TextLayout</code>, not to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * underlying text storage model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <strong>Examples</strong>:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * Constructing and drawing a <code>TextLayout</code> and its bounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * rectangle:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *   Graphics2D g = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *   Point2D loc = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *   Font font = Font.getFont("Helvetica-bold-italic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   FontRenderContext frc = g.getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *   TextLayout layout = new TextLayout("This is a string", font, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *   layout.draw(g, (float)loc.getX(), (float)loc.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *   Rectangle2D bounds = layout.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *   bounds.setRect(bounds.getX()+loc.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *                  bounds.getY()+loc.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *                  bounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *                  bounds.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *   g.draw(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * Hit-testing a <code>TextLayout</code> (determining which character is at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * a particular graphical location):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *   Point2D click = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *   TextHitInfo hit = layout.hitTestChar(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *                         (float) (click.getX() - loc.getX()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *                         (float) (click.getY() - loc.getY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * Responding to a right-arrow key press:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *   int insertionIndex = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *   TextHitInfo next = layout.getNextRightHit(insertionIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *   if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *       // translate graphics to origin of layout on screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *       g.translate(loc.getX(), loc.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *       Shape[] carets = layout.getCaretShapes(next.getInsertionIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *       g.draw(carets[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *       if (carets[1] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *           g.draw(carets[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * Drawing a selection range corresponding to a substring in the source text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * The selected area may not be visually contiguous:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *   // selStart, selLimit should be relative to the layout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *   // not to the source text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *   int selStart = ..., selLimit = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *   Color selectionColor = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *   Shape selection = layout.getLogicalHighlightShape(selStart, selLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *   // selection may consist of disjoint areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *   // graphics is assumed to be tranlated to origin of layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *   g.setColor(selectionColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *   g.fill(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Drawing a visually contiguous selection range.  The selection range may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * correspond to more than one substring in the source text.  The ranges of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * the corresponding source text substrings can be obtained with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <code>getLogicalRangesForVisualSelection()</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *   TextHitInfo selStart = ..., selLimit = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *   Shape selection = layout.getVisualHighlightShape(selStart, selLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *   g.setColor(selectionColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *   g.fill(selection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *   int[] ranges = getLogicalRangesForVisualSelection(selStart, selLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *   // ranges[0], ranges[1] is the first selection range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *   // ranges[2], ranges[3] is the second selection range, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * Note: Font rotations can cause text baselines to be rotated, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * multiple runs with different rotations can cause the baseline to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * bend or zig-zag.  In order to account for this (rare) possibility,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * some APIs are specified to return metrics and take parameters 'in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * baseline-relative coordinates' (e.g. ascent, advance), and others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * are in 'in standard coordinates' (e.g. getBounds).  Values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * baseline-relative coordinates map the 'x' coordinate to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * distance along the baseline, (positive x is forward along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * baseline), and the 'y' coordinate to a distance along the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 11081
diff changeset
   226
 * perpendicular to the baseline at 'x' (positive y is 90 degrees
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * clockwise from the baseline vector).  Values in standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * coordinates are measured along the x and y axes, with 0,0 at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * origin of the TextLayout.  Documentation for each relevant API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * indicates what values are in what coordinate system.  In general,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * measurement-related APIs are in baseline-relative coordinates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * while display-related APIs are in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * @see LineBreakMeasurer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * @see TextAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * @see TextHitInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * @see LayoutPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
public final class TextLayout implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private int characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private boolean isVerticalLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private byte baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private float[] baselineOffsets;  // why have these ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private TextLine textLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // cached values computed from GlyphSets and set info:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    // all are recomputed from scratch in buildCache()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private TextLine.TextLineMetrics lineMetrics = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private float visibleAdvance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private int hashCodeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * TextLayouts are supposedly immutable.  If you mutate a TextLayout under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the covers (like the justification code does) you'll need to set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * back to false.  Could be replaced with textLine != null <--> cacheIsValid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private boolean cacheIsValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // This value is obtained from an attribute, and constrained to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // interval [0,1].  If 0, the layout cannot be justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private float justifyRatio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // If a layout is produced by justification, then that layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // cannot be justified.  To enforce this constraint the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // justifyRatio of the justified layout is set to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static final float ALREADY_JUSTIFIED = -53.9f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // dx and dy specify the distance between the TextLayout's origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // and the origin of the leftmost GlyphSet (TextLayoutComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // actually).  They were used for hanging punctuation support,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    // which is no longer implemented.  Currently they are both always 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // and TextLayout is not guaranteed to work with non-zero dx, dy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // values right now.  They were left in as an aide and reminder to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // anyone who implements hanging punctuation or other similar stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // They are static now so they don't take up space in TextLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static float dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static float dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Natural bounds is used internally.  It is built on demand in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * getNaturalBounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private Rectangle2D naturalBounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * boundsRect encloses all of the bits this TextLayout can draw.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * is build on demand in getBounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private Rectangle2D boundsRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * flag to supress/allow carets inside of ligatures when hit testing or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * arrow-keying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private boolean caretsInLigaturesAreAllowed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Defines a policy for determining the strong caret location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * This class contains one method, <code>getStrongCaret</code>, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * is used to specify the policy that determines the strong caret in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * dual-caret text.  The strong caret is used to move the caret to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * left or right. Instances of this class can be passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>getCaretShapes</code>, <code>getNextLeftHit</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <code>getNextRightHit</code> to customize strong caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * To specify alternate caret policies, subclass <code>CaretPolicy</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * and override <code>getStrongCaret</code>.  <code>getStrongCaret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * should inspect the two <code>TextHitInfo</code> arguments and choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * one of them as the strong caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Most clients do not need to use this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public static class CaretPolicy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * Constructs a <code>CaretPolicy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         public CaretPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * Chooses one of the specified <code>TextHitInfo</code> instances as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * a strong caret in the specified <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * @param hit1 a valid hit in <code>layout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * @param hit2 a valid hit in <code>layout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * @param layout the <code>TextLayout</code> in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         *        <code>hit1</code> and <code>hit2</code> are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * @return <code>hit1</code> or <code>hit2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         *        (or an equivalent <code>TextHitInfo</code>), indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         *        strong caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public TextHitInfo getStrongCaret(TextHitInfo hit1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                          TextHitInfo hit2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                          TextLayout layout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 11081
diff changeset
   340
            // default implementation just calls private method on layout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return layout.getStrongHit(hit1, hit2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This <code>CaretPolicy</code> is used when a policy is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * by the client.  With this policy, a hit on a character whose direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * is the same as the line direction is stronger than a hit on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * counterdirectional character.  If the characters' directions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * the same, a hit on the leading edge of a character is stronger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * than a hit on the trailing edge of a character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public static final CaretPolicy DEFAULT_CARET_POLICY = new CaretPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Constructs a <code>TextLayout</code> from a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * and a {@link Font}.  All the text is styled using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <code>Font</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * The <code>String</code> must specify a single paragraph of text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * because an entire paragraph is required for the bidirectional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param string the text to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param font a <code>Font</code> used to style the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param frc contains information about a graphics device which is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *       to measure the text correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *       Text measurements can vary slightly depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *       device resolution, and attributes such as antialiasing.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *       parameter does not specify a translation between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *       <code>TextLayout</code> and user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public TextLayout(String string, Font font, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (string.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   386
        Map<? extends Attribute, ?> attributes = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (font.hasLayoutAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            attributes = font.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        char[] text = string.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            fastInit(text, font, attributes, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            AttributedString as = attributes == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                ? new AttributedString(string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                : new AttributedString(string, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            as.addAttribute(TextAttribute.FONT, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            standardInit(as.getIterator(), text, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Constructs a <code>TextLayout</code> from a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * and an attribute set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * All the text is styled using the provided attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>string</code> must specify a single paragraph of text because an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * entire paragraph is required for the bidirectional algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param string the text to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param attributes the attributes used to style the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param frc contains information about a graphics device which is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *       to measure the text correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *       Text measurements can vary slightly depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *       device resolution, and attributes such as antialiasing.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *       parameter does not specify a translation between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *       <code>TextLayout</code> and user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public TextLayout(String string, Map<? extends Attribute,?> attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                      FontRenderContext frc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new IllegalArgumentException("Null map passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (string.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        char[] text = string.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        Font font = singleFont(text, 0, text.length, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            fastInit(text, font, attributes, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            AttributedString as = new AttributedString(string, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            standardInit(as.getIterator(), text, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Determines a font for the attributes, and if a single font can render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * all the text on one baseline, return it, otherwise null.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * attributes specify a font, assume it can display all the text without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * If the AttributeSet contains an embedded graphic, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    private static Font singleFont(char[] text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                   int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                   int limit,
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   455
                                   Map<? extends Attribute, ?> attributes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (attributes.get(TextAttribute.CHAR_REPLACEMENT) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Font font = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            font = (Font)attributes.get(TextAttribute.FONT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (attributes.get(TextAttribute.FAMILY) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                font = Font.getFont(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (font.canDisplayUpTo(text, start, limit) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                FontResolver resolver = FontResolver.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                CodePointIterator iter = CodePointIterator.create(text, start, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                int fontIndex = resolver.nextFontRunIndex(iter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                if (iter.charIndex() == limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    font = resolver.getFont(fontIndex, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (sameBaselineUpTo(font, text, start, limit) != limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Constructs a <code>TextLayout</code> from an iterator over styled text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * The iterator must specify a single paragraph of text because an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * entire paragraph is required for the bidirectional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param text the styled text to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param frc contains information about a graphics device which is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *       to measure the text correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *       Text measurements can vary slightly depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *       device resolution, and attributes such as antialiasing.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *       parameter does not specify a translation between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *       <code>TextLayout</code> and user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public TextLayout(AttributedCharacterIterator text, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            throw new IllegalArgumentException("Null iterator passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        int start = text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        int limit = text.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (start == limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw new IllegalArgumentException("Zero length iterator passed to TextLayout constructor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int len = limit - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        text.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        char[] chars = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int n = 0;
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   520
        for (char c = text.first();
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   521
             c != CharacterIterator.DONE;
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   522
             c = text.next())
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   523
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            chars[n++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        text.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (text.getRunLimit() == limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   530
            Map<? extends Attribute, ?> attributes = text.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            Font font = singleFont(chars, 0, len, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                fastInit(chars, font, attributes, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        standardInit(text, chars, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Creates a <code>TextLayout</code> from a {@link TextLine} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * some paragraph data.  This method is used by {@link TextMeasurer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param textLine the line measurement attributes to apply to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *       the resulting <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param baseline the baseline of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param baselineOffsets the baseline offsets for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <code>TextLayout</code>.  This should already be normalized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <code>baseline</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param justifyRatio <code>0</code> if the <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *     cannot be justified; <code>1</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    TextLayout(TextLine textLine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
               byte baseline,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
               float[] baselineOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
               float justifyRatio) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        this.characterCount = textLine.characterCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        this.baseline = baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        this.baselineOffsets = baselineOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        this.textLine = textLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        this.justifyRatio = justifyRatio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Initialize the paragraph-specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   568
    private void paragraphInit(byte aBaseline, CoreMetrics lm,
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   569
                               Map<? extends Attribute, ?> paragraphAttrs,
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   570
                               char[] text) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        baseline = aBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // normalize to current baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        baselineOffsets = TextLine.getNormalizedOffsets(lm.baselineOffsets, baseline);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        justifyRatio = AttributeValues.getJustification(paragraphAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        NumericShaper shaper = AttributeValues.getNumericShaping(paragraphAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (shaper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            shaper.shape(text, 0, text.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * the fast init generates a single glyph set.  This requires:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * all one style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * all renderable by one font (ie no embedded graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * all on one baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   590
    private void fastInit(char[] chars, Font font,
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   591
                          Map<? extends Attribute, ?> attrs,
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   592
                          FontRenderContext frc) {
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   593
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // Object vf = attrs.get(TextAttribute.ORIENTATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // isVerticalLine = TextAttribute.ORIENTATION_VERTICAL.equals(vf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        isVerticalLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        LineMetrics lm = font.getLineMetrics(chars, 0, chars.length, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        CoreMetrics cm = CoreMetrics.get(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        byte glyphBaseline = (byte) cm.baselineIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (attrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            baseline = glyphBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            baselineOffsets = cm.baselineOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            justifyRatio = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            paragraphInit(glyphBaseline, cm, attrs, chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        characterCount = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        textLine = TextLine.fastCreateTextLine(frc, chars, font, cm, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * the standard init generates multiple glyph sets based on style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * renderable, and baseline runs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param chars the text in the iterator, extracted into a char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private void standardInit(AttributedCharacterIterator text, char[] chars, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        characterCount = chars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // set paragraph attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // If there's an embedded graphic at the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // paragraph, look for the first non-graphic character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // and use it and its font to initialize the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // If not, use the first graphic to initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
11081
4a18889223e4 7117199: Fix javac warnings in src/share/classes/java/awt/font
prr
parents: 10419
diff changeset
   631
            Map<? extends Attribute, ?> paragraphAttrs = text.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            boolean haveFont = TextLine.advanceToFirstFont(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (haveFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                Font defaultFont = TextLine.getFontAtCurrentPos(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                int charsStart = text.getIndex() - text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                LineMetrics lm = defaultFont.getLineMetrics(chars, charsStart, charsStart+1, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                CoreMetrics cm = CoreMetrics.get(lm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                paragraphInit((byte)cm.baselineIndex, cm, paragraphAttrs, chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                // hmmm what to do here?  Just try to supply reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                // values I guess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                GraphicAttribute graphic = (GraphicAttribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                paragraphAttrs.get(TextAttribute.CHAR_REPLACEMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                byte defaultBaseline = getBaselineFromGraphic(graphic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                CoreMetrics cm = GraphicComponent.createCoreMetrics(graphic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                paragraphInit(defaultBaseline, cm, paragraphAttrs, chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        textLine = TextLine.standardCreateTextLine(frc, text, chars, baselineOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * A utility to rebuild the ascent/descent/leading/advance cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * You'll need to call this if you clone and mutate (like justification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * editing methods do)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    private void ensureCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (!cacheIsValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            buildCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private void buildCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        lineMetrics = textLine.getMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // compute visibleAdvance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (textLine.isDirectionLTR()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            int lastNonSpace = characterCount-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            while (lastNonSpace != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                int logIndex = textLine.visualToLogical(lastNonSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (!textLine.isCharSpace(logIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    --lastNonSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (lastNonSpace == characterCount-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                visibleAdvance = lineMetrics.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            else if (lastNonSpace == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                visibleAdvance = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                int logIndex = textLine.visualToLogical(lastNonSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                visibleAdvance = textLine.getCharLinePosition(logIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                        + textLine.getCharAdvance(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            int leftmostNonSpace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            while (leftmostNonSpace != characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                int logIndex = textLine.visualToLogical(leftmostNonSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                if (!textLine.isCharSpace(logIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    ++leftmostNonSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            if (leftmostNonSpace == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                visibleAdvance = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            else if (leftmostNonSpace == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                visibleAdvance = lineMetrics.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                int logIndex = textLine.visualToLogical(leftmostNonSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                float pos = textLine.getCharLinePosition(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                visibleAdvance = lineMetrics.advance - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // naturalBounds, boundsRect will be generated on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        naturalBounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        boundsRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // hashCode will be regenerated on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        hashCodeCache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        cacheIsValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * The 'natural bounds' encloses all the carets the layout can draw.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private Rectangle2D getNaturalBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (naturalBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            naturalBounds = textLine.getItalicBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return naturalBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Creates a copy of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    protected Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * !!! I think this is safe.  Once created, nothing mutates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * glyphvectors or arrays.  But we need to make sure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * {jbr} actually, that's not quite true.  The justification code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * mutates after cloning.  It doesn't actually change the glyphvectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * (that's impossible) but it replaces them with justified sets.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * is a problem for GlyphIterator creation, since new GlyphIterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * are created by cloning a prototype.  If the prototype has outdated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         * glyphvectors, so will the new ones.  A partial solution is to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * prototypical GlyphIterator to null when the glyphvectors change.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * you forget this one time, you're hosed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   765
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Utility to throw an expection if an invalid TextHitInfo is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * as a parameter.  Avoids code duplication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private void checkTextHit(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (hit == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            throw new IllegalArgumentException("TextHitInfo is null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (hit.getInsertionIndex() < 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            hit.getInsertionIndex() > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            throw new IllegalArgumentException("TextHitInfo is out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * Creates a copy of this <code>TextLayout</code> justified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * specified width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * If this <code>TextLayout</code> has already been justified, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * exception is thrown.  If this <code>TextLayout</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * justification ratio is zero, a <code>TextLayout</code> identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * to this <code>TextLayout</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param justificationWidth the width to use when justifying the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * For best results, it should not be too different from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * advance of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @return a <code>TextLayout</code> justified to the specified width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @exception Error if this layout has already been justified, an Error is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    public TextLayout getJustifiedLayout(float justificationWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (justificationWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            throw new IllegalArgumentException("justificationWidth <= 0 passed to TextLayout.getJustifiedLayout()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        if (justifyRatio == ALREADY_JUSTIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            throw new Error("Can't justify again.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        ensureCache(); // make sure textLine is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        // default justification range to exclude trailing logical whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int limit = characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        while (limit > 0 && textLine.isCharWhitespace(limit-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            --limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        TextLine newLine = textLine.getJustifiedLine(justificationWidth, justifyRatio, 0, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (newLine != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return new TextLayout(newLine, baseline, baselineOffsets, ALREADY_JUSTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Justify this layout.  Overridden by subclassers to control justification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * (if there were subclassers, that is...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * The layout will only justify if the paragraph attributes (from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * source text, possibly defaulted by the layout attributes) indicate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * non-zero justification ratio.  The text will be justified to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * indicated width.  The current implementation also adjusts hanging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * punctuation and trailing whitespace to overhang the justification width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Once justified, the layout may not be rejustified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Some code may rely on immutablity of layouts.  Subclassers should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * call this directly, but instead should call getJustifiedLayout, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * will call this method on a clone of this layout, preserving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param justificationWidth the width to use when justifying the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * For best results, it should not be too different from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * advance of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @see #getJustifiedLayout(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    protected void handleJustify(float justificationWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
      // never called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Returns the baseline for this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * The baseline is one of the values defined in <code>Font</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * which are roman, centered and hanging.  Ascent and descent are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * relative to this baseline.  The <code>baselineOffsets</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * are also relative to this baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @return the baseline of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @see #getBaselineOffsets()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @see Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public byte getBaseline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Returns the offsets array for the baselines used for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * The array is indexed by one of the values defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <code>Font</code>, which are roman, centered and hanging.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * values are relative to this <code>TextLayout</code> object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * baseline, so that <code>getBaselineOffsets[getBaseline()] == 0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Offsets are added to the position of the <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * object's baseline to get the position for the new baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @return the offsets array containing the baselines used for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *    <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @see #getBaseline()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @see Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public float[] getBaselineOffsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        float[] offsets = new float[baselineOffsets.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        System.arraycopy(baselineOffsets, 0, offsets, 0, offsets.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * Returns the advance of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * The advance is the distance from the origin to the advance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * rightmost (bottommost) character.  This is in baseline-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @return the advance of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    public float getAdvance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return lineMetrics.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Returns the advance of this <code>TextLayout</code>, minus trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * whitespace.  This is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @return the advance of this <code>TextLayout</code> without the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *      trailing whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @see #getAdvance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    public float getVisibleAdvance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        return visibleAdvance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * Returns the ascent of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * The ascent is the distance from the top (right) of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * <code>TextLayout</code> to the baseline.  It is always either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * positive or zero.  The ascent is sufficient to
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 11081
diff changeset
   915
     * accommodate superscripted text and is the maximum of the sum of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * ascent, offset, and baseline of each glyph.  The ascent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * the maximum ascent from the baseline of all the text in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * TextLayout.  It is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return the ascent of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public float getAscent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return lineMetrics.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Returns the descent of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * The descent is the distance from the baseline to the bottom (left) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * the <code>TextLayout</code>.  It is always either positive or zero.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 11081
diff changeset
   930
     * The descent is sufficient to accommodate subscripted text and is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * maximum of the sum of the descent, offset, and baseline of each glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * This is the maximum descent from the baseline of all the text in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * the TextLayout.  It is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @return the descent of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public float getDescent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        return lineMetrics.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Returns the leading of the <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * The leading is the suggested interline spacing for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * <code>TextLayout</code>.  This is in baseline-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * The leading is computed from the leading, descent, and baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * of all glyphvectors in the <code>TextLayout</code>.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * is roughly as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * maxD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * maxDL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * for (GlyphVector g in all glyphvectors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *    maxD = max(maxD, g.getDescent() + offsets[g.getBaseline()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *    maxDL = max(maxDL, g.getDescent() + g.getLeading() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *                       offsets[g.getBaseline()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * return maxDL - maxD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @return the leading of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public float getLeading() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        return lineMetrics.leading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Returns the bounds of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * The bounds are in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <p>Due to rasterization effects, this bounds might not enclose all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * pixels rendered by the TextLayout.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * It might not coincide exactly with the ascent, descent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * origin or advance of the <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @return a {@link Rectangle2D} that is the bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *        <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    public Rectangle2D getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if (boundsRect == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            Rectangle2D vb = textLine.getVisualBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if (dx != 0 || dy != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                vb.setRect(vb.getX() - dx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                           vb.getY() - dy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                           vb.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                           vb.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            boundsRect = vb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        Rectangle2D bounds = new Rectangle2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        bounds.setRect(boundsRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Returns the pixel bounds of this <code>TextLayout</code> when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * rendered in a graphics with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * <code>FontRenderContext</code> at the given location.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * graphics render context need not be the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * <code>FontRenderContext</code> used to create this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * <code>TextLayout</code>, and can be null.  If it is null, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * <code>FontRenderContext</code> of this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param frc the <code>FontRenderContext</code> of the <code>Graphics</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param x the x-coordinate at which to render this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @param y the y-coordinate at which to render this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @return a <code>Rectangle</code> bounding the pixels that would be affected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @see GlyphVector#getPixelBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        return textLine.getPixelBounds(frc, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Returns <code>true</code> if this <code>TextLayout</code> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * a left-to-right base direction or <code>false</code> if it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * a right-to-left base direction.  The <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * has a base direction of either left-to-right (LTR) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * right-to-left (RTL).  The base direction is independent of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * actual direction of text on the line, which may be either LTR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * RTL, or mixed. Left-to-right layouts by default should position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * flush left.  If the layout is on a tabbed line, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * tabs run left to right, so that logically successive layouts position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * left to right.  The opposite is true for RTL layouts. By default they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * should position flush left, and tabs run right-to-left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @return <code>true</code> if the base direction of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *         <code>TextLayout</code> is left-to-right; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *         otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    public boolean isLeftToRight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return textLine.isDirectionLTR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * Returns <code>true</code> if this <code>TextLayout</code> is vertical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @return <code>true</code> if this <code>TextLayout</code> is vertical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *      <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public boolean isVertical() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        return isVerticalLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * Returns the number of characters represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @return the number of characters in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    public int getCharacterCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * carets and hit testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Positions on a text line are represented by instances of TextHitInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Any TextHitInfo with characterOffset between 0 and characterCount-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * inclusive, represents a valid position on the line.  Additionally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * [-1, trailing] and [characterCount, leading] are valid positions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * represent positions at the logical start and end of the line,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * The characterOffsets in TextHitInfo's used and returned by TextLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * are relative to the beginning of the text layout, not necessarily to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * the beginning of the text storage the client is using.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Every valid TextHitInfo has either one or two carets associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * A caret is a visual location in the TextLayout indicating where text at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * the TextHitInfo will be displayed on screen.  If a TextHitInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * represents a location on a directional boundary, then there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * possible visible positions for newly inserted text.  Consider the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * following example, in which capital letters indicate right-to-left text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * and the overall line direction is left-to-right:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Text Storage: [ a, b, C, D, E, f ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Display:        a b E D C f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * The text hit info (1, t) represents the trailing side of 'b'.  If 'q',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * a left-to-right character is inserted into the text storage at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * location, it will be displayed between the 'b' and the 'E':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * Text Storage: [ a, b, q, C, D, E, f ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Display:        a b q E D C f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * However, if a 'W', which is right-to-left, is inserted into the storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * after 'b', the storage and display will be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Text Storage: [ a, b, W, C, D, E, f ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Display:        a b E D C W f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * So, for the original text storage, two carets should be displayed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * location (1, t): one visually between 'b' and 'E' and one visually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * between 'C' and 'f'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * When two carets are displayed for a TextHitInfo, one caret is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * 'strong' caret and the other is the 'weak' caret.  The strong caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * indicates where an inserted character will be displayed when that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * character's direction is the same as the direction of the TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * The weak caret shows where an character inserted character will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * displayed when the character's direction is opposite that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Clients should not be overly concerned with the details of correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * caret display. TextLayout.getCaretShapes(TextHitInfo) will return an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * array of two paths representing where carets should be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * The first path in the array is the strong caret; the second element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * if non-null, is the weak caret.  If the second element is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * then there is no weak caret for the given TextHitInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * Since text can be visually reordered, logically consecutive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * TextHitInfo's may not be visually consecutive.  One implication of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * is that a client cannot tell from inspecting a TextHitInfo whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * hit represents the first (or last) caret in the layout.  Clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * can call getVisualOtherHit();  if the visual companion is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * (-1, TRAILING) or (characterCount, LEADING), then the hit is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * first (last) caret position in the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    private float[] getCaretInfo(int caret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                                 Rectangle2D bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                                 float[] info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        float top1X, top2X;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        float bottom1X, bottom2X;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        if (caret == 0 || caret == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            float pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            int logIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            if (caret == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                logIndex = textLine.visualToLogical(characterCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                pos = textLine.getCharLinePosition(logIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                                        + textLine.getCharAdvance(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                logIndex = textLine.visualToLogical(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                pos = textLine.getCharLinePosition(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            float angle = textLine.getCharAngle(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            float shift = textLine.getCharShift(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            pos += angle * shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            top1X = top2X = pos + angle*textLine.getCharAscent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            bottom1X = bottom2X = pos - angle*textLine.getCharDescent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                int logIndex = textLine.visualToLogical(caret-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                float angle1 = textLine.getCharAngle(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                float pos1 = textLine.getCharLinePosition(logIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                                    + textLine.getCharAdvance(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                if (angle1 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    pos1 += angle1 * textLine.getCharShift(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    top1X = pos1 + angle1*textLine.getCharAscent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    bottom1X = pos1 - angle1*textLine.getCharDescent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    top1X = bottom1X = pos1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                int logIndex = textLine.visualToLogical(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                float angle2 = textLine.getCharAngle(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                float pos2 = textLine.getCharLinePosition(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                if (angle2 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                    pos2 += angle2*textLine.getCharShift(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    top2X = pos2 + angle2*textLine.getCharAscent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    bottom2X = pos2 - angle2*textLine.getCharDescent(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    top2X = bottom2X = pos2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        float topX = (top1X + top2X) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        float bottomX = (bottom1X + bottom2X) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        if (info == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            info = new float[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (isVerticalLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            info[1] = (float) ((topX - bottomX) / bounds.getWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            info[0] = (float) (topX + (info[1]*bounds.getX()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            info[1] = (float) ((topX - bottomX) / bounds.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            info[0] = (float) (bottomX + (info[1]*bounds.getMaxY()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * Returns information about the caret corresponding to <code>hit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * The first element of the array is the intersection of the caret with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * the baseline, as a distance along the baseline. The second element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * of the array is the inverse slope (run/rise) of the caret, measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * with respect to the baseline at that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * This method is meant for informational use.  To display carets, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * is better to use <code>getCaretShapes</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @param hit a hit on a character in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @param bounds the bounds to which the caret info is constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *     The bounds is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @return a two-element array containing the position and slope of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * the caret.  The returned caret info is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @see #getCaretShapes(int, Rectangle2D, TextLayout.CaretPolicy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @see Font#getItalicAngle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    public float[] getCaretInfo(TextHitInfo hit, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        return getCaretInfoTestInternal(hit, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    // this version provides extra info in the float array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    // the first two values are as above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    // the next four values are the endpoints of the caret, as computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    // using the hit character's offset (baseline + ssoffset) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    // natural ascent and descent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    // these  values are trimmed to the bounds where required to fit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    // but otherwise independent of it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    private float[] getCaretInfoTestInternal(TextHitInfo hit, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        float[] info = new float[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        // get old data first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        getCaretInfo(hitToCaret(hit), bounds, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        // then add our new data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        double iangle, ixbase, p1x, p1y, p2x, p2y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        int charix = hit.getCharIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        boolean lead = hit.isLeadingEdge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        boolean ltr = textLine.isDirectionLTR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        boolean horiz = !isVertical();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        if (charix == -1 || charix == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            // !!! note: want non-shifted, baseline ascent and descent here!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            // TextLine should return appropriate line metrics object for these values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            TextLineMetrics m = textLine.getMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            boolean low = ltr == (charix == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            iangle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (horiz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                p1x = p2x = low ? 0 : m.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                p1y = -m.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                p2y = m.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                p1y = p2y = low ? 0 : m.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                p1x = m.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                p2x = m.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            CoreMetrics thiscm = textLine.getCoreMetricsAt(charix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            iangle = thiscm.italicAngle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            ixbase = textLine.getCharLinePosition(charix, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            if (thiscm.baselineIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                // this is a graphic, no italics, use entire line height for caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                TextLineMetrics m = textLine.getMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                if (horiz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    p1x = p2x = ixbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    if (thiscm.baselineIndex == GraphicAttribute.TOP_ALIGNMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                        p1y = -m.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                        p2y = p1y + thiscm.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                        p2y = m.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                        p1y = p2y - thiscm.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                    p1y = p2y = ixbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    p1x = m.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    p2x = m.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    // !!! top/bottom adjustment not implemented for vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                float bo = baselineOffsets[thiscm.baselineIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                if (horiz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    ixbase += iangle * thiscm.ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    p1x = ixbase + iangle * thiscm.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    p2x = ixbase - iangle * thiscm.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    p1y = bo - thiscm.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                    p2y = bo + thiscm.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    ixbase -= iangle * thiscm.ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    p1y = ixbase + iangle * thiscm.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    p2y = ixbase - iangle * thiscm.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                    p1x = bo + thiscm.ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    p2x = bo + thiscm.descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        info[2] = (float)p1x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        info[3] = (float)p1y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        info[4] = (float)p2x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        info[5] = (float)p2y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * Returns information about the caret corresponding to <code>hit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * This method is a convenience overload of <code>getCaretInfo</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * uses the natural bounds of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * @param hit a hit on a character in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * @return the information about a caret corresponding to a hit.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *     returned caret info is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    public float[] getCaretInfo(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        return getCaretInfo(hit, getNaturalBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Returns a caret index corresponding to <code>hit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * Carets are numbered from left to right (top to bottom) starting from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * zero. This always places carets next to the character hit, on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * indicated side of the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @param hit a hit on a character in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * @return a caret index corresponding to the specified hit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    private int hitToCaret(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        int hitIndex = hit.getCharIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        if (hitIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            return textLine.isDirectionLTR() ? 0 : characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        } else if (hitIndex >= characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            return textLine.isDirectionLTR() ? characterCount : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        int visIndex = textLine.logicalToVisual(hitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        if (hit.isLeadingEdge() != textLine.isCharLTR(hitIndex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            ++visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        return visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * Given a caret index, return a hit whose caret is at the index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * The hit is NOT guaranteed to be strong!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @param caret a caret index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @return a hit on this layout whose strong caret is at the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    private TextHitInfo caretToHit(int caret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        if (caret == 0 || caret == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            if ((caret == characterCount) == textLine.isDirectionLTR()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                return TextHitInfo.leading(characterCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                return TextHitInfo.trailing(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            int charIndex = textLine.visualToLogical(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            boolean leading = textLine.isCharLTR(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            return leading? TextHitInfo.leading(charIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                            : TextHitInfo.trailing(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    private boolean caretIsValid(int caret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        if (caret == characterCount || caret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        int offset = textLine.visualToLogical(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        if (!textLine.isCharLTR(offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            offset = textLine.visualToLogical(caret-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            if (textLine.isCharLTR(offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        // At this point, the leading edge of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        // at offset is at the given caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        return textLine.caretAtOffsetIsValid(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Returns the hit for the next caret to the right (bottom); if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * is no such hit, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * If the hit character index is out of bounds, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * {@link IllegalArgumentException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @param hit a hit on a character in this layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * right (bottom) of the caret of the provided hit or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    public TextHitInfo getNextRightHit(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        int caret = hitToCaret(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (caret == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            ++caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        } while (!caretIsValid(caret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        return caretToHit(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Returns the hit for the next caret to the right (bottom); if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * such hit, returns <code>null</code>.  The hit is to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * the strong caret at the specified offset, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * specified policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * The returned hit is the stronger of the two possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * hits, as determined by the specified policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @param offset an insertion offset in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * Cannot be less than 0 or greater than this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * object's character count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @param policy the policy used to select the strong caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * right (bottom) of the caret of the provided hit, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    public TextHitInfo getNextRightHit(int offset, CaretPolicy policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        if (offset < 0 || offset > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            throw new IllegalArgumentException("Offset out of bounds in TextLayout.getNextRightHit()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            throw new IllegalArgumentException("Null CaretPolicy passed to TextLayout.getNextRightHit()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        TextHitInfo hit1 = TextHitInfo.afterOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        TextHitInfo hit2 = hit1.getOtherHit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        TextHitInfo nextHit = getNextRightHit(policy.getStrongCaret(hit1, hit2, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        if (nextHit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            TextHitInfo otherHit = getVisualOtherHit(nextHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            return policy.getStrongCaret(otherHit, nextHit, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * Returns the hit for the next caret to the right (bottom); if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * such hit, returns <code>null</code>.  The hit is to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * the strong caret at the specified offset, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * The returned hit is the stronger of the two possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * hits, as determined by the default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @param offset an insertion offset in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * Cannot be less than 0 or greater than the <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * object's character count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * right (bottom) of the caret of the provided hit, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    public TextHitInfo getNextRightHit(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        return getNextRightHit(offset, DEFAULT_CARET_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * Returns the hit for the next caret to the left (top); if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * hit, returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * If the hit character index is out of bounds, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * @param hit a hit on a character in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * left (top) of the caret of the provided hit, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    public TextHitInfo getNextLeftHit(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        int caret = hitToCaret(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (caret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            --caret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        } while(!caretIsValid(caret));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        return caretToHit(caret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * Returns the hit for the next caret to the left (top); if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * such hit, returns <code>null</code>.  The hit is to the left of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * the strong caret at the specified offset, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * specified policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * The returned hit is the stronger of the two possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * hits, as determined by the specified policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @param offset an insertion offset in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * Cannot be less than 0 or greater than this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * object's character count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @param policy the policy used to select the strong caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * left (top) of the caret of the provided hit, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    public TextHitInfo getNextLeftHit(int offset, CaretPolicy policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            throw new IllegalArgumentException("Null CaretPolicy passed to TextLayout.getNextLeftHit()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        if (offset < 0 || offset > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            throw new IllegalArgumentException("Offset out of bounds in TextLayout.getNextLeftHit()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        TextHitInfo hit1 = TextHitInfo.afterOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        TextHitInfo hit2 = hit1.getOtherHit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        TextHitInfo nextHit = getNextLeftHit(policy.getStrongCaret(hit1, hit2, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        if (nextHit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            TextHitInfo otherHit = getVisualOtherHit(nextHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            return policy.getStrongCaret(otherHit, nextHit, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Returns the hit for the next caret to the left (top); if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * such hit, returns <code>null</code>.  The hit is to the left of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * the strong caret at the specified offset, as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * The returned hit is the stronger of the two possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * hits, as determined by the default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * @param offset an insertion offset in this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * Cannot be less than 0 or greater than this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * object's character count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * @return a hit whose caret appears at the next position to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * left (top) of the caret of the provided hit, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    public TextHitInfo getNextLeftHit(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        return getNextLeftHit(offset, DEFAULT_CARET_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * Returns the hit on the opposite side of the specified hit's caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @param hit the specified hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * @return a hit that is on the opposite side of the specified hit's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     *    caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    public TextHitInfo getVisualOtherHit(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        int hitCharIndex = hit.getCharIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        int charIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        boolean leading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        if (hitCharIndex == -1 || hitCharIndex == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            int visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            if (textLine.isDirectionLTR() == (hitCharIndex == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                visIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                visIndex = characterCount-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            charIndex = textLine.visualToLogical(visIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            if (textLine.isDirectionLTR() == (hitCharIndex == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                // at left end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                leading = textLine.isCharLTR(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                // at right end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                leading = !textLine.isCharLTR(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            int visIndex = textLine.logicalToVisual(hitCharIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            boolean movedToRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            if (textLine.isCharLTR(hitCharIndex) == hit.isLeadingEdge()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                --visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                movedToRight = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                ++visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                movedToRight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            if (visIndex > -1 && visIndex < characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                charIndex = textLine.visualToLogical(visIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                leading = movedToRight == textLine.isCharLTR(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                charIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                    (movedToRight == textLine.isDirectionLTR())? characterCount : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                leading = charIndex == characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        return leading? TextHitInfo.leading(charIndex) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                                TextHitInfo.trailing(charIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    private double[] getCaretPath(TextHitInfo hit, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        float[] info = getCaretInfo(hit, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        return new double[] { info[2], info[3], info[4], info[5] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * Return an array of four floats corresponding the endpoints of the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * x0, y0, x1, y1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * This creates a line along the slope of the caret intersecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * baseline at the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * position, and extending from ascent above the baseline to descent below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    private double[] getCaretPath(int caret, Rectangle2D bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                                  boolean clipToBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        float[] info = getCaretInfo(caret, bounds, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        double pos = info[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        double slope = info[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        double x0, y0, x1, y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        double x2 = -3141.59, y2 = -2.7; // values are there to make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        double left = bounds.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        double right = left + bounds.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        double top = bounds.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        double bottom = top + bounds.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        boolean threePoints = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        if (isVerticalLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            if (slope >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                x0 = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                x1 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                x1 = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                x0 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            y0 = pos + x0 * slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            y1 = pos + x1 * slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            // y0 <= y1, always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            if (clipToBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                if (y0 < top) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    if (slope <= 0 || y1 <= top) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                        y0 = y1 = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                        threePoints = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                        y0 = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                        y2 = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                        x2 = x1 + (top-y1)/slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                        if (y1 > bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                            y1 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                else if (y1 > bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    if (slope >= 0 || y0 >= bottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                        y0 = y1 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                        threePoints = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                        y1 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                        y2 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                        x2 = x0 + (bottom-x1)/slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            if (slope >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                y0 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                y1 = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                y1 = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                y0 = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            x0 = pos - y0 * slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            x1 = pos - y1 * slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            // x0 <= x1, always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            if (clipToBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                if (x0 < left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    if (slope <= 0 || x1 <= left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                        x0 = x1 = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                        threePoints = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                        x0 = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                        x2 = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                        y2 = y1 - (left-x1)/slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                        if (x1 > right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                            x1 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                else if (x1 > right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                    if (slope >= 0 || x0 >= right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                        x0 = x1 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                        threePoints = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                        x1 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                        x2 = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                        y2 = y0 - (right-x0)/slope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        return threePoints?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    new double[] { x0, y0, x2, y2, x1, y1 } :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                    new double[] { x0, y0, x1, y1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    private static GeneralPath pathToShape(double[] path, boolean close, LayoutPathImpl lp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        GeneralPath result = new GeneralPath(GeneralPath.WIND_EVEN_ODD, path.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        result.moveTo((float)path[0], (float)path[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        for (int i = 2; i < path.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            result.lineTo((float)path[i], (float)path[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        if (close) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            result.closePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            result = (GeneralPath)lp.mapShape(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * Returns a {@link Shape} representing the caret at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * hit inside the specified bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * @param hit the hit at which to generate the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * @param bounds the bounds of the <code>TextLayout</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *    in generating the caret.  The bounds is in baseline-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *    coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @return a <code>Shape</code> representing the caret.  The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *    shape is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    public Shape getCaretShape(TextHitInfo hit, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        if (bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            throw new IllegalArgumentException("Null Rectangle2D passed to TextLayout.getCaret()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return pathToShape(getCaretPath(hit, bounds), false, textLine.getLayoutPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * Returns a <code>Shape</code> representing the caret at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * hit inside the natural bounds of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * @param hit the hit at which to generate the caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * @return a <code>Shape</code> representing the caret.  The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *     shape is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    public Shape getCaretShape(TextHitInfo hit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        return getCaretShape(hit, getNaturalBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * Return the "stronger" of the TextHitInfos.  The TextHitInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * should be logical or visual counterparts.  They are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * checked for validity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    private final TextHitInfo getStrongHit(TextHitInfo hit1, TextHitInfo hit2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        // right now we're using the following rule for strong hits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        // A hit on a character with a lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        // is stronger than one on a character with a higher level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        // If this rule ties, the hit on the leading edge of a character wins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        // If THIS rule ties, hit1 wins.  Both rules shouldn't tie, unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        // infos aren't counterparts of some sort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        byte hit1Level = getCharacterLevel(hit1.getCharIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        byte hit2Level = getCharacterLevel(hit2.getCharIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        if (hit1Level == hit2Level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            if (hit2.isLeadingEdge() && !hit1.isLeadingEdge()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                return hit2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                return hit1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            return (hit1Level < hit2Level)? hit1 : hit2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * Returns the level of the character at <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * Indices -1 and <code>characterCount</code> are assigned the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * level of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * @param index the index of the character from which to get the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * @return the level of the character at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    public byte getCharacterLevel(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        // hmm, allow indices at endpoints?  For now, yes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        if (index < -1 || index > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            throw new IllegalArgumentException("Index is out of range in getCharacterLevel.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        if (index == -1 || index == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
             return (byte) (textLine.isDirectionLTR()? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        return textLine.getCharLevel(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * Returns two paths corresponding to the strong and weak caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * @param offset an offset in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * @param bounds the bounds to which to extend the carets.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * bounds is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * @param policy the specified <code>CaretPolicy</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * @return an array of two paths.  Element zero is the strong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * caret.  If there are two carets, element one is the weak caret,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * otherwise it is <code>null</code>. The returned shapes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * are in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    public Shape[] getCaretShapes(int offset, Rectangle2D bounds, CaretPolicy policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        if (offset < 0 || offset > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            throw new IllegalArgumentException("Offset out of bounds in TextLayout.getCaretShapes()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        if (bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            throw new IllegalArgumentException("Null Rectangle2D passed to TextLayout.getCaretShapes()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            throw new IllegalArgumentException("Null CaretPolicy passed to TextLayout.getCaretShapes()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        Shape[] result = new Shape[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        TextHitInfo hit = TextHitInfo.afterOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        int hitCaret = hitToCaret(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        Shape hitShape = pathToShape(getCaretPath(hit, bounds), false, lp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        TextHitInfo otherHit = hit.getOtherHit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        int otherCaret = hitToCaret(otherHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        if (hitCaret == otherCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
            result[0] = hitShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        else { // more than one caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            Shape otherShape = pathToShape(getCaretPath(otherHit, bounds), false, lp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            TextHitInfo strongHit = policy.getStrongCaret(hit, otherHit, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            boolean hitIsStrong = strongHit.equals(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            if (hitIsStrong) {// then other is weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                result[0] = hitShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                result[1] = otherShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                result[0] = otherShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                result[1] = hitShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * Returns two paths corresponding to the strong and weak caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * This method is a convenience overload of <code>getCaretShapes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * that uses the default caret policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @param offset an offset in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * @param bounds the bounds to which to extend the carets.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     *     in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * @return two paths corresponding to the strong and weak caret as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *    defined by the <code>DEFAULT_CARET_POLICY</code>.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     *    in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    public Shape[] getCaretShapes(int offset, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        // {sfb} parameter checking is done in overloaded version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        return getCaretShapes(offset, bounds, DEFAULT_CARET_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * Returns two paths corresponding to the strong and weak caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * This method is a convenience overload of <code>getCaretShapes</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * that uses the default caret policy and this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * object's natural bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * @param offset an offset in this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * @return two paths corresponding to the strong and weak caret as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *    defined by the <code>DEFAULT_CARET_POLICY</code>.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     *    in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    public Shape[] getCaretShapes(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        // {sfb} parameter checking is done in overloaded version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        return getCaretShapes(offset, getNaturalBounds(), DEFAULT_CARET_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    // A utility to return a path enclosing the given path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    // Path0 must be left or top of path1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    // {jbr} no assumptions about size of path0, path1 anymore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    private GeneralPath boundingShape(double[] path0, double[] path1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        // Really, we want the path to be a convex hull around all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        // points in path0 and path1.  But we can get by with less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        // that.  We do need to prevent the two segments which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        // join path0 to path1 from crossing each other.  So, if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        // traverse path0 from top to bottom, we'll traverse path1 from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        // bottom to top (and vice versa).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        GeneralPath result = pathToShape(path0, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        boolean sameDirection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        if (isVerticalLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            sameDirection = (path0[1] > path0[path0.length-1]) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                            (path1[1] > path1[path1.length-1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            sameDirection = (path0[0] > path0[path0.length-2]) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                            (path1[0] > path1[path1.length-2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        int start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        int increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        if (sameDirection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            start = path1.length-2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            limit = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            increment = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            limit = path1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            increment = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        for (int i = start; i != limit; i += increment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            result.lineTo((float)path1[i], (float)path1[i+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        result.closePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    // A utility to convert a pair of carets into a bounding path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    // {jbr} Shape is never outside of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    private GeneralPath caretBoundingShape(int caret0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                                           int caret1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                                           Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        if (caret0 > caret1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
            int temp = caret0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            caret0 = caret1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
            caret1 = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        return boundingShape(getCaretPath(caret0, bounds, true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                             getCaretPath(caret1, bounds, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * A utility to return the path bounding the area to the left (top) of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * Shape is never outside of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    private GeneralPath leftShape(Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        double[] path0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        if (isVerticalLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            path0 = new double[] { bounds.getX(), bounds.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                                       bounds.getX() + bounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                                       bounds.getY() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            path0 = new double[] { bounds.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                                       bounds.getY() + bounds.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                                       bounds.getX(), bounds.getY() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        double[] path1 = getCaretPath(0, bounds, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        return boundingShape(path0, path1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * A utility to return the path bounding the area to the right (bottom) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * the layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    private GeneralPath rightShape(Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        double[] path1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        if (isVerticalLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            path1 = new double[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                bounds.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                bounds.getY() + bounds.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                bounds.getX() + bounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                bounds.getY() + bounds.getHeight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            path1 = new double[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                bounds.getX() + bounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                bounds.getY() + bounds.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                bounds.getX() + bounds.getWidth(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                bounds.getY()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        double[] path0 = getCaretPath(characterCount, bounds, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        return boundingShape(path0, path1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * Returns the logical ranges of text corresponding to a visual selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * @param firstEndpoint an endpoint of the visual range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * @param secondEndpoint the other endpoint of the visual range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * This endpoint can be less than <code>firstEndpoint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * @return an array of integers representing start/limit pairs for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * selected ranges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * @see #getVisualHighlightShape(TextHitInfo, TextHitInfo, Rectangle2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    public int[] getLogicalRangesForVisualSelection(TextHitInfo firstEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                                                    TextHitInfo secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        checkTextHit(firstEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        checkTextHit(secondEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        // !!! probably want to optimize for all LTR text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        boolean[] included = new boolean[characterCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        int startIndex = hitToCaret(firstEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        int limitIndex = hitToCaret(secondEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        if (startIndex > limitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            int t = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            startIndex = limitIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            limitIndex = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
         * now we have the visual indexes of the glyphs at the start and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
         * of the selection range walk through runs marking characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
         * were included in the visual range there is probably a more efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
         * way to do this, but this ought to work, so hey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        if (startIndex < limitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            int visIndex = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            while (visIndex < limitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                included[textLine.visualToLogical(visIndex)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                ++visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
         * count how many runs we have, ought to be one or two, but perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
         * things are especially weird
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        boolean inrun = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        for (int i = 0; i < characterCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            if (included[i] != inrun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                inrun = !inrun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                if (inrun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        int[] ranges = new int[count * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        inrun = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        for (int i = 0; i < characterCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            if (included[i] != inrun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                ranges[count++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                inrun = !inrun;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        if (inrun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            ranges[count++] = characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        return ranges;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * Returns a path enclosing the visual selection in the specified range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * extended to <code>bounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * If the selection includes the leftmost (topmost) position, the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * is extended to the left (top) of <code>bounds</code>.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * selection includes the rightmost (bottommost) position, the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * is extended to the right (bottom) of the bounds.  The height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * (width on vertical lines) of the selection is always extended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * <code>bounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * Although the selection is always contiguous, the logically selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * text can be discontiguous on lines with mixed-direction text.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * logical ranges of text selected can be retrieved using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * <code>getLogicalRangesForVisualSelection</code>.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * consider the text 'ABCdef' where capital letters indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * right-to-left text, rendered on a right-to-left line, with a visual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * selection from 0L (the leading edge of 'A') to 3T (the trailing edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * of 'd').  The text appears as follows, with bold underlined areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * representing the selection:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * <br><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     *    d<u><b>efCBA  </b></u>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * The logical selection ranges are 0-3, 4-6 (ABC, ef) because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * visually contiguous text is logically discontiguous.  Also note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * since the rightmost position on the layout (to the right of 'A') is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * selected, the selection is extended to the right of the bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * @param firstEndpoint one end of the visual selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * @param secondEndpoint the other end of the visual selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @param bounds the bounding rectangle to which to extend the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     *     This is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * @return a <code>Shape</code> enclosing the selection.  This is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *     standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * @see #getLogicalRangesForVisualSelection(TextHitInfo, TextHitInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @see #getLogicalHighlightShape(int, int, Rectangle2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    public Shape getVisualHighlightShape(TextHitInfo firstEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                                        TextHitInfo secondEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                                        Rectangle2D bounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        checkTextHit(firstEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        checkTextHit(secondEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        if(bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                throw new IllegalArgumentException("Null Rectangle2D passed to TextLayout.getVisualHighlightShape()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        GeneralPath result = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        int firstCaret = hitToCaret(firstEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        int secondCaret = hitToCaret(secondEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        result.append(caretBoundingShape(firstCaret, secondCaret, bounds),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                      false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        if (firstCaret == 0 || secondCaret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
            GeneralPath ls = leftShape(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            if (!ls.getBounds().isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                result.append(ls, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        if (firstCaret == characterCount || secondCaret == characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            GeneralPath rs = rightShape(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            if (!rs.getBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                result.append(rs, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            result = (GeneralPath)lp.mapShape(result); // dlf cast safe?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        return  result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * Returns a <code>Shape</code> enclosing the visual selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * specified range, extended to the bounds.  This method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * convenience overload of <code>getVisualHighlightShape</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     * uses the natural bounds of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * @param firstEndpoint one end of the visual selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * @param secondEndpoint the other end of the visual selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * @return a <code>Shape</code> enclosing the selection.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     *     in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    public Shape getVisualHighlightShape(TextHitInfo firstEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                                             TextHitInfo secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        return getVisualHighlightShape(firstEndpoint, secondEndpoint, getNaturalBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * Returns a <code>Shape</code> enclosing the logical selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * specified range, extended to the specified <code>bounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * If the selection range includes the first logical character, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * selection is extended to the portion of <code>bounds</code> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * the start of this <code>TextLayout</code>.  If the range includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * the last logical character, the selection is extended to the portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * of <code>bounds</code> after the end of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * The height (width on vertical lines) of the selection is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * extended to <code>bounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * The selection can be discontiguous on lines with mixed-direction text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * Only those characters in the logical range between start and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * appear selected.  For example, consider the text 'ABCdef' where capital
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * letters indicate right-to-left text, rendered on a right-to-left line,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * with a logical selection from 0 to 4 ('ABCd').  The text appears as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * follows, with bold standing in for the selection, and underlining for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * the extension:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * <br><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     *    <u><b>d</b></u>ef<u><b>CBA  </b></u>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * The selection is discontiguous because the selected characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * visually discontiguous. Also note that since the range includes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * first logical character (A), the selection is extended to the portion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * of the <code>bounds</code> before the start of the layout, which in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     * this case (a right-to-left line) is the right portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * <code>bounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * @param firstEndpoint an endpoint in the range of characters to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * @param secondEndpoint the other endpoint of the range of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * to select. Can be less than <code>firstEndpoint</code>.  The range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * includes the character at min(firstEndpoint, secondEndpoint), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * excludes max(firstEndpoint, secondEndpoint).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * @param bounds the bounding rectangle to which to extend the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     *     This is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * @return an area enclosing the selection.  This is in standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     *     coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * @see #getVisualHighlightShape(TextHitInfo, TextHitInfo, Rectangle2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    public Shape getLogicalHighlightShape(int firstEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                                         int secondEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                                         Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        if (bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
            throw new IllegalArgumentException("Null Rectangle2D passed to TextLayout.getLogicalHighlightShape()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        if (firstEndpoint > secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            int t = firstEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            firstEndpoint = secondEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            secondEndpoint = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        if(firstEndpoint < 0 || secondEndpoint > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            throw new IllegalArgumentException("Range is invalid in TextLayout.getLogicalHighlightShape()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        GeneralPath result = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        int[] carets = new int[10]; // would this ever not handle all cases?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        if (firstEndpoint < secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            int logIndex = firstEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                carets[count++] = hitToCaret(TextHitInfo.leading(logIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                boolean ltr = textLine.isCharLTR(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    logIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                } while (logIndex < secondEndpoint && textLine.isCharLTR(logIndex) == ltr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                int hitCh = logIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                carets[count++] = hitToCaret(TextHitInfo.trailing(hitCh - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                if (count == carets.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                    int[] temp = new int[carets.length + 10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    System.arraycopy(carets, 0, temp, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    carets = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            } while (logIndex < secondEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            count = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
            carets[0] = carets[1] = hitToCaret(TextHitInfo.leading(firstEndpoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        // now create paths for pairs of carets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        for (int i = 0; i < count; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            result.append(caretBoundingShape(carets[i], carets[i+1], bounds),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                          false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        if (firstEndpoint != secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            if ((textLine.isDirectionLTR() && firstEndpoint == 0) || (!textLine.isDirectionLTR() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                                                                      secondEndpoint == characterCount)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                GeneralPath ls = leftShape(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                if (!ls.getBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                    result.append(ls, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            if ((textLine.isDirectionLTR() && secondEndpoint == characterCount) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                (!textLine.isDirectionLTR() && firstEndpoint == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                GeneralPath rs = rightShape(bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                if (!rs.getBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    result.append(rs, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            result = (GeneralPath)lp.mapShape(result); // dlf cast safe?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * Returns a <code>Shape</code> enclosing the logical selection in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * specified range, extended to the natural bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * <code>TextLayout</code>.  This method is a convenience overload of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * <code>getLogicalHighlightShape</code> that uses the natural bounds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * @param firstEndpoint an endpoint in the range of characters to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * @param secondEndpoint the other endpoint of the range of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * to select. Can be less than <code>firstEndpoint</code>.  The range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * includes the character at min(firstEndpoint, secondEndpoint), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * excludes max(firstEndpoint, secondEndpoint).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * @return a <code>Shape</code> enclosing the selection.  This is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     *     standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    public Shape getLogicalHighlightShape(int firstEndpoint, int secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        return getLogicalHighlightShape(firstEndpoint, secondEndpoint, getNaturalBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * Returns the black box bounds of the characters in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * The black box bounds is an area consisting of the union of the bounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * boxes of all the glyphs corresponding to the characters between start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * and limit.  This area can be disjoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * @param firstEndpoint one end of the character range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * @param secondEndpoint the other end of the character range.  Can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * less than <code>firstEndpoint</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * @return a <code>Shape</code> enclosing the black box bounds.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     *     in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    public Shape getBlackBoxBounds(int firstEndpoint, int secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        if (firstEndpoint > secondEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            int t = firstEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            firstEndpoint = secondEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
            secondEndpoint = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        if (firstEndpoint < 0 || secondEndpoint > characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            throw new IllegalArgumentException("Invalid range passed to TextLayout.getBlackBoxBounds()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
         * return an area that consists of the bounding boxes of all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
         * characters from firstEndpoint to limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        GeneralPath result = new GeneralPath(GeneralPath.WIND_NON_ZERO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        if (firstEndpoint < characterCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            for (int logIndex = firstEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                        logIndex < secondEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                        logIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                Rectangle2D r = textLine.getCharBounds(logIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                if (!r.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                    result.append(r, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        if (dx != 0 || dy != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
            AffineTransform tx = AffineTransform.getTranslateInstance(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            result = (GeneralPath)tx.createTransformedShape(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            result = (GeneralPath)lp.mapShape(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        //return new Highlight(result, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * Returns the distance from the point (x,&nbsp;y) to the caret along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * the line direction defined in <code>caretInfo</code>.  Distance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * negative if the point is to the left of the caret on a horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * line, or above the caret on a vertical line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * Utility for use by hitTestChar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    private float caretToPointDistance(float[] caretInfo, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        // distanceOffBaseline is negative if you're 'above' baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        float lineDistance = isVerticalLine? y : x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        float distanceOffBaseline = isVerticalLine? -x : y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        return lineDistance - caretInfo[0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            (distanceOffBaseline*caretInfo[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * Returns a <code>TextHitInfo</code> corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * specified point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * Coordinates outside the bounds of the <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * map to hits on the leading edge of the first logical character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * or the trailing edge of the last logical character, as appropriate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * regardless of the position of that character in the line.  Only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * direction along the baseline is used to make this evaluation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * @param x the x offset from the origin of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     *     <code>TextLayout</code>.  This is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * @param y the y offset from the origin of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     *     <code>TextLayout</code>.  This is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     * @param bounds the bounds of the <code>TextLayout</code>.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     *     is in baseline-relative coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * @return a hit describing the character and edge (leading or trailing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     *     under the specified point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    public TextHitInfo hitTestChar(float x, float y, Rectangle2D bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        // check boundary conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        boolean prev = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
            Point2D.Float pt = new Point2D.Float(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            prev = lp.pointToPath(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            x = pt.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            y = pt.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        if (isVertical()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            if (y < bounds.getMinY()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                return TextHitInfo.leading(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            } else if (y >= bounds.getMaxY()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                return TextHitInfo.trailing(characterCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            if (x < bounds.getMinX()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                return isLeftToRight() ? TextHitInfo.leading(0) : TextHitInfo.trailing(characterCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            } else if (x >= bounds.getMaxX()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                return isLeftToRight() ? TextHitInfo.trailing(characterCount-1) : TextHitInfo.leading(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        // revised hit test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        // the original seems too complex and fails miserably with italic offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        // the natural tendency is to move towards the character you want to hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        // so we'll just measure distance to the center of each character's visual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        // bounds, pick the closest one, then see which side of the character's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        // center line (italic) the point is on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        // this tends to make it easier to hit narrow characters, which can be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
        // bit odd if you're visually over an adjacent wide character. this makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        // a difference with bidi, so perhaps i need to revisit this yet again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        double distance = Double.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        int trail = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        CoreMetrics lcm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        float icx = 0, icy = 0, ia = 0, cy = 0, dya = 0, ydsq = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        for (int i = 0; i < characterCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
            if (!textLine.caretAtOffsetIsValid(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            if (trail == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                trail = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            CoreMetrics cm = textLine.getCoreMetricsAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            if (cm != lcm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                lcm = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                // just work around baseline mess for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                if (cm.baselineIndex == GraphicAttribute.TOP_ALIGNMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                    cy = -(textLine.getMetrics().ascent - cm.ascent) + cm.ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                } else if (cm.baselineIndex == GraphicAttribute.BOTTOM_ALIGNMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                    cy = textLine.getMetrics().descent - cm.descent + cm.ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                    cy = cm.effectiveBaselineOffset(baselineOffsets) + cm.ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                float dy = (cm.descent - cm.ascent) / 2 - cy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                dya = dy * cm.italicAngle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                cy += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                ydsq = (cy - y)*(cy - y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            float cx = textLine.getCharXPosition(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
            float ca = textLine.getCharAdvance(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            float dx = ca / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            cx += dx - dya;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            // proximity in x (along baseline) is two times as important as proximity in y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            double nd = Math.sqrt(4*(cx - x)*(cx - x) + ydsq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
            if (nd < distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                distance = nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                trail = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                icx = cx; icy = cy; ia = cm.italicAngle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        boolean left = x < icx - (y - icy) * ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        boolean leading = textLine.isCharLTR(index) == left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        if (trail == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            trail = characterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        TextHitInfo result = leading ? TextHitInfo.leading(index) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            TextHitInfo.trailing(trail-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * Returns a <code>TextHitInfo</code> corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * specified point.  This method is a convenience overload of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * <code>hitTestChar</code> that uses the natural bounds of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * @param x the x offset from the origin of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     *     <code>TextLayout</code>.  This is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * @param y the y offset from the origin of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     *     <code>TextLayout</code>.  This is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * @return a hit describing the character and edge (leading or trailing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * under the specified point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    public TextHitInfo hitTestChar(float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        return hitTestChar(x, y, getNaturalBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     * Returns the hash code of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     * @return the hash code of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        if (hashCodeCache == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            hashCodeCache = textLine.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        return hashCodeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
     * Returns <code>true</code> if the specified <code>Object</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
     * <code>TextLayout</code> object and if the specified <code>Object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     * equals this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
     * @param obj an <code>Object</code> to test for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
     * @return <code>true</code> if the specified <code>Object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
     *      equals this <code>TextLayout</code>; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
     *      otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        return (obj instanceof TextLayout) && equals((TextLayout)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * Returns <code>true</code> if the two layouts are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * Two layouts are equal if they contain equal glyphvectors in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     * @param rhs the <code>TextLayout</code> to compare to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     *       <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     * @return <code>true</code> if the specified <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     *      equals this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
    public boolean equals(TextLayout rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        if (rhs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        if (rhs == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        return textLine.equals(rhs.textLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     * Returns debugging information for this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * @return the <code>textLine</code> of this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     *        as a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        return textLine.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * Renders this <code>TextLayout</code> at the specified location in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     * the specified {@link java.awt.Graphics2D Graphics2D} context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     * The origin of the layout is placed at x,&nbsp;y.  Rendering may touch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     * any point within <code>getBounds()</code> of this position.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
     * leaves the <code>g2</code> unchanged.  Text is rendered along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
     * baseline path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * @param g2 the <code>Graphics2D</code> context into which to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     *         the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * @param x the X coordinate of the origin of this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     * @param y the Y coordinate of the origin of this <code>TextLayout</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     * @see #getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
    public void draw(Graphics2D g2, float x, float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
        if (g2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            throw new IllegalArgumentException("Null Graphics2D passed to TextLayout.draw()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        textLine.draw(g2, x - dx, y - dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     * Package-only method for testing ONLY.  Please don't abuse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    TextLine getTextLineForTesting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        return textLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * Return the index of the first character with a different baseline from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     * character at start, or limit if all characters between start and limit have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * the same baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
    private static int sameBaselineUpTo(Font font, char[] text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                                        int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        // current implementation doesn't support multiple baselines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        return limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        byte bl = font.getBaselineFor(text[start++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        while (start < limit && font.getBaselineFor(text[start]) == bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            ++start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        return start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
    static byte getBaselineFromGraphic(GraphicAttribute graphic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        byte alignment = (byte) graphic.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        if (alignment == GraphicAttribute.BOTTOM_ALIGNMENT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                alignment == GraphicAttribute.TOP_ALIGNMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            return (byte)GraphicAttribute.ROMAN_BASELINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
            return alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
     * Returns a <code>Shape</code> representing the outline of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
     * <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
     * @param tx an optional {@link AffineTransform} to apply to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
     *     outline of this <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
     * @return a <code>Shape</code> that is the outline of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
     *     <code>TextLayout</code>.  This is in standard coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
    public Shape getOutline(AffineTransform tx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        Shape result = textLine.getOutline(tx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        LayoutPathImpl lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            result = lp.mapShape(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * Return the LayoutPath, or null if the layout path is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * default path (x maps to advance, y maps to offset).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * @return the layout path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
    public LayoutPath getLayoutPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        return textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
     * Convert a hit to a point in standard coordinates.  The point is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     * on the baseline of the character at the leading or trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
     * edge of the character, as appropriate.  If the path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * broken at the side of the character represented by the hit, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
     * point will be adjacent to the character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     * @param hit the hit to check.  This must be a valid hit on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
     * the TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
     * @param point the returned point. The point is in standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
     *     coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     * @throws IllegalArgumentException if the hit is not valid for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
     * TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
     * @throws NullPointerException if hit or point is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
    public void hitToPoint(TextHitInfo hit, Point2D point) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        if (hit == null || point == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
            throw new NullPointerException((hit == null ? "hit" : "point") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                                           " can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        ensureCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        checkTextHit(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        float adv = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
        float off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        int ix = hit.getCharIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        boolean leading = hit.isLeadingEdge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        boolean ltr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        if (ix == -1 || ix == textLine.characterCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            ltr = textLine.isDirectionLTR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
            adv = (ltr == (ix == -1)) ? 0 : lineMetrics.advance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            ltr = textLine.isCharLTR(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
            adv = textLine.getCharLinePosition(ix, leading);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            off = textLine.getCharYPosition(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
        point.setLocation(adv, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        LayoutPath lp = textLine.getLayoutPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        if (lp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            lp.pathToPoint(point, ltr != leading, point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
}