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