jdk/src/share/classes/javax/swing/text/GlyphPainter2.java
author rupashka
Wed, 06 Apr 2011 20:36:22 +0400
changeset 9216 d102bd3f513c
parent 5506 202f599c92aa
child 20103 f640c22218a3
permissions -rw-r--r--
6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5 Reviewed-by: peterz
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) 1999, 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
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.font.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A class to perform rendering of the glyphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This can be implemented to be stateless, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to hold some information as a cache to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * facilitate faster rendering and model/view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * translation.  At a minimum, the GlyphPainter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * allows a View implementation to perform its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * duties independent of a particular version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of JVM and selection of capabilities (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * shaping for i18n, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This implementation is intended for operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * under the JDK.  It uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * java.awt.font.TextLayout class to do i18n capable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see GlyphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class GlyphPainter2 extends GlyphView.GlyphPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public GlyphPainter2(TextLayout layout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.layout = layout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Create a painter to use for the given GlyphView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public GlyphView.GlyphPainter getPainter(GlyphView v, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Determine the span the glyphs given a start location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * (for tab expansion).  This implementation assumes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * has no tabs (i.e. TextLayout doesn't deal with tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * expansion).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public float getSpan(GlyphView v, int p0, int p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                         TabExpander e, float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if ((p0 == v.getStartOffset()) && (p1 == v.getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return layout.getAdvance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int p = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int index0 = p0 - p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int index1 = p1 - p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        TextHitInfo hit0 = TextHitInfo.afterOffset(index0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        TextHitInfo hit1 = TextHitInfo.beforeOffset(index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        float[] locs = layout.getCaretInfo(hit0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        float x0 = locs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        locs = layout.getCaretInfo(hit1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        float x1 = locs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return (x1 > x0) ? x1 - x0 : x0 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public float getHeight(GlyphView v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return layout.getAscent() + layout.getDescent() + layout.getLeading();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Fetch the ascent above the baseline for the glyphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * corresponding to the given range in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public float getAscent(GlyphView v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return layout.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Fetch the descent below the baseline for the glyphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * corresponding to the given range in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public float getDescent(GlyphView v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return layout.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Paint the glyphs for the given view.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * to only render if the Graphics is of type Graphics2D which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * is required by TextLayout (and this should be the case if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * running on the JDK).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void paint(GlyphView v, Graphics g, Shape a, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (g instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            Rectangle2D alloc = a.getBounds2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            float y = (float) alloc.getY() + layout.getAscent() + layout.getLeading();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            float x = (float) alloc.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if( p0 > v.getStartOffset() || p1 < v.getEndOffset() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    //TextLayout can't render only part of it's range, so if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    //partial range is required, add a clip region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    Shape s = v.modelToView(p0, Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                            p1, Position.Bias.Backward, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    Shape savedClip = g.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    g2d.clip(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    layout.draw(g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    g.setClip(savedClip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                } catch (BadLocationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                layout.draw(g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public Shape modelToView(GlyphView v, int pos, Position.Bias bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                             Shape a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int offs = pos - v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Rectangle2D alloc = a.getBounds2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        TextHitInfo hit = (bias == Position.Bias.Forward) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            TextHitInfo.afterOffset(offs) : TextHitInfo.beforeOffset(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        float[] locs = layout.getCaretInfo(hit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // vertical at the baseline, should use slope and check if glyphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // are being rendered vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        alloc.setRect(alloc.getX() + locs[0], alloc.getY(), 1, alloc.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param v the view containing the view coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param x the X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param y the Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param biasReturn either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *  or <code>Position.Bias.Backward</code> is returned as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  zero-th element of this array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  given point of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int viewToModel(GlyphView v, float x, float y, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                           Position.Bias[] biasReturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Rectangle2D alloc = (a instanceof Rectangle2D) ? (Rectangle2D)a : a.getBounds2D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        //Move the y co-ord of the hit onto the baseline.  This is because TextLayout supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        //italic carets and we do not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        TextHitInfo hit = layout.hitTestChar(x - (float)alloc.getX(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int pos = hit.getInsertionIndex();
9216
d102bd3f513c 6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5
rupashka
parents: 5506
diff changeset
   175
d102bd3f513c 6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5
rupashka
parents: 5506
diff changeset
   176
        if (pos == v.getEndOffset()) {
d102bd3f513c 6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5
rupashka
parents: 5506
diff changeset
   177
            pos--;
d102bd3f513c 6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5
rupashka
parents: 5506
diff changeset
   178
        }
d102bd3f513c 6849232: closed/javax/swing/text/GlyphPainter2/6427244/bug6427244.java fails on RHEL5
rupashka
parents: 5506
diff changeset
   179
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        biasReturn[0] = hit.isLeadingEdge() ? Position.Bias.Forward : Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return pos + v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Determines the model location that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * maximum advance that fits within the given span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This could be used to break the given view.  The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * should be a location just shy of the given advance.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * differs from viewToModel which returns the closest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * position which might be proud of the maximum advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param v the view to find the model location to break at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param p0 the location in the model where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *  fragment should start it's representation >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param pos the graphic location along the axis that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *  broken view would occupy >= 0.  This may be useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *  things like tab calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param len specifies the distance into the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *  where a potential break is desired >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the maximum model location possible for a break.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @see View#breakView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public int getBoundedPosition(GlyphView v, int p0, float x, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if( len < 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            throw new IllegalArgumentException("Length must be >= 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // note: this only works because swing uses TextLayouts that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // only pure rtl or pure ltr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        TextHitInfo hit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (layout.isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            hit = layout.hitTestChar(len, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            hit = layout.hitTestChar(layout.getAdvance() - len, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return v.getStartOffset() + hit.getCharIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * location that one might place a caret.  Some views may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * visible, they might not be in the same order found in the model, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * they just might not allow access to some of the locations in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * @param v the view to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         *  be thought of as the arrow keys typically found on a keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         *  This may be SwingConstants.WEST, SwingConstants.EAST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         *  SwingConstants.NORTH, or SwingConstants.SOUTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         *  location visual position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * @exception IllegalArgumentException for an invalid direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public int getNextVisualPositionFrom(GlyphView v, int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                             Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                             int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                             Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int startOffset = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int endOffset = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Segment text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            AbstractDocument doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            boolean viewIsLeftToRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            TextHitInfo currentHit, nextHit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            case View.NORTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            case View.SOUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            case View.EAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                doc = (AbstractDocument)v.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if(startOffset == doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    // End case for bidi text where newline is at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    // of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    // Entering view from the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    if( viewIsLeftToRight ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        text = v.getText(endOffset - 1, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        char c = text.array[text.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        SegmentCache.releaseSharedSegment(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        if(c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                            return endOffset-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        biasRet[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        return endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if( b==Position.Bias.Forward )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    currentHit = TextHitInfo.afterOffset(pos-startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    currentHit = TextHitInfo.beforeOffset(pos-startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                nextHit = layout.getNextRightHit(currentHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if( nextHit == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if( viewIsLeftToRight != layout.isLeftToRight() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // If the layout's base direction is different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // this view's run direction, we need to use the weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    // carrat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    nextHit = layout.getVisualOtherHit(nextHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                pos = nextHit.getInsertionIndex() + startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if(pos == endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    // A move to the right from an internal position will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    // only take us to the endOffset in a left to right run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    text = v.getText(endOffset - 1, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    char c = text.array[text.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    SegmentCache.releaseSharedSegment(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    if(c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    biasRet[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            case View.WEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                doc = (AbstractDocument)v.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                viewIsLeftToRight = doc.isLeftToRight(startOffset, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if(startOffset == doc.getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    // End case for bidi text where newline is at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    // of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    // Entering view from the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    if( viewIsLeftToRight ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        text = v.getText(endOffset - 1, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        char c = text.array[text.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        SegmentCache.releaseSharedSegment(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        if ((c == '\n') || Character.isSpaceChar(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                            biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            return endOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        biasRet[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        return endOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if( b==Position.Bias.Forward )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    currentHit = TextHitInfo.afterOffset(pos-startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    currentHit = TextHitInfo.beforeOffset(pos-startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                nextHit = layout.getNextLeftHit(currentHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if( nextHit == null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if( viewIsLeftToRight != layout.isLeftToRight() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // If the layout's base direction is different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    // this view's run direction, we need to use the weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    // carrat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    nextHit = layout.getVisualOtherHit(nextHit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                pos = nextHit.getInsertionIndex() + startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if(pos == endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    // A move to the left from an internal position will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    // only take us to the endOffset in a right to left run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    text = v.getText(endOffset - 1, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    char c = text.array[text.offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    SegmentCache.releaseSharedSegment(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    if(c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    biasRet[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new IllegalArgumentException("Bad direction: " + direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // --- variables ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    TextLayout layout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}