jdk/src/share/classes/javax/swing/text/TextLayoutStrategy.java
author prr
Fri, 23 May 2014 09:05:24 -0700
changeset 24567 a0ebe5fd56ff
parent 5506 202f599c92aa
permissions -rw-r--r--
Merge
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: 3976
diff changeset
     2
 * Copyright (c) 1999, 2008, 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: 3976
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: 3976
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: 3976
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3976
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3976
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.font.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.AffineTransform;
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
    33
import javax.swing.JComponent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.font.BidiUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A flow strategy that uses java.awt.font.LineBreakMeasureer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * produce java.awt.font.TextLayout for i18n capable rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * If the child view being placed into the flow is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * GlyphView and can be rendered by TextLayout, a GlyphPainter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that uses TextLayout is plugged into the GlyphView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class TextLayoutStrategy extends FlowView.FlowStrategy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructs a layout strategy for paragraphs based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * upon java.awt.font.LineBreakMeasurer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public TextLayoutStrategy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        text = new AttributedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // --- FlowStrategy methods --------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * in a location that the given flow view is responsible for.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * strategy should update the appropriate changed region (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * depends upon the strategy used for repair).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param alloc the current allocation of the view inside of the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *   This value will be null if the view has not yet been displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void insertUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        sync(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super.insertUpdate(fv, e, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * in a location that the given flow view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param alloc the current allocation of the view inside of the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void removeUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        sync(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super.removeUpdate(fv, e, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void changedUpdate(FlowView fv, DocumentEvent e, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        sync(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super.changedUpdate(fv, e, alloc);
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
     * Does a a full layout on the given View.  This causes all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the rows (child views) to be rebuilt to match the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * constraints for each row.  This is called by a FlowView.layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * to update the child views in the flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   107
     * @param fv the view to reflow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void layout(FlowView fv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        super.layout(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a row of views that will fit within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * layout span of the row.  This is implemented to execute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * superclass functionality (which fills the row with child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * views or view fragments) and follow that with bidi reordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * of the unidirectional view fragments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param row the row to fill in with views.  This is assumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *   to be empty on entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param pos  The current position in the children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *   this views element from which to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return the position to start the next row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected int layoutRow(FlowView fv, int rowIndex, int p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int p1 = super.layoutRow(fv, rowIndex, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        View row = fv.getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Document doc = fv.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Object i18nFlag = doc.getProperty(AbstractDocument.I18NProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            int n = row.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (n > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                AbstractDocument d = (AbstractDocument)fv.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                Element bidiRoot = d.getBidiRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                byte[] levels = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                View[] reorder = new View[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                for( int i=0; i<n; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    View v = row.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    int bidiIndex =bidiRoot.getElementIndex(v.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    Element bidiElem = bidiRoot.getElement( bidiIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    levels[i] = (byte)StyleConstants.getBidiLevel(bidiElem.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    reorder[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                BidiUtils.reorderVisually( levels, reorder );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                row.replace(0, n, reorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Adjusts the given row if possible to fit within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * layout span.  Since all adjustments were already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * calculated by the LineBreakMeasurer, this is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * to do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param r the row to adjust to the current layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *  span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param desiredSpan the current layout span >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param x the location r starts at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected void adjustRow(FlowView fv, int rowIndex, int desiredSpan, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Creates a unidirectional view that can be used to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * current chunk.  This can be either an entire view from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * logical view, or a fragment of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param fv the view holding the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param startOffset the start location for the view being created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param spanLeft the about of span left to fill in the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param rowIndex the row the view will be placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected View createView(FlowView fv, int startOffset, int spanLeft, int rowIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Get the child view that contains the given starting position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        View lv = getLogicalView(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        View row = fv.getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        boolean requireNextWord = (viewBuffer.size() == 0) ? false : true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int childIndex = lv.getViewIndex(startOffset, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        View v = lv.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int endOffset = getLimitingOffset(v, startOffset, spanLeft, requireNextWord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (endOffset == startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        View frag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if ((startOffset==v.getStartOffset()) && (endOffset == v.getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // return the entire view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            frag = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // return a unidirectional fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            frag = v.createFragment(startOffset, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if ((frag instanceof GlyphView) && (measurer != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // install a TextLayout based renderer if the view is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // for glyphs.  If the view represents a tab, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // glyph painter is used (may want to handle tabs differently).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            boolean isTab = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            int p0 = frag.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            int p1 = frag.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if ((p1 - p0) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // check for tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                Segment s = ((GlyphView)frag).getText(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                char ch = s.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (ch == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    isTab = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            TextLayout tl = (isTab) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                measurer.nextLayout(spanLeft, text.toIteratorIndex(endOffset),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                    requireNextWord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (tl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                ((GlyphView)frag).setGlyphPainter(new GlyphPainter2(tl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return frag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Calculate the limiting offset for the next view fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * At most this would be the entire view (i.e. the limiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * offset would be the end offset in that case).  If the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * contains a tab or a direction change, that will limit the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * offset to something less.  This value is then fed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * LineBreakMeasurer as a limit to consider in addition to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * remaining span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param v the logical view representing the starting offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param startOffset the model location to start at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    int getLimitingOffset(View v, int startOffset, int spanLeft, boolean requireNextWord) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int endOffset = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // check for direction change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Document doc = v.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            AbstractDocument d = (AbstractDocument) doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Element bidiRoot = d.getBidiRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if( bidiRoot.getElementCount() > 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                int bidiIndex = bidiRoot.getElementIndex( startOffset );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                Element bidiElem = bidiRoot.getElement( bidiIndex );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                endOffset = Math.min( bidiElem.getEndOffset(), endOffset );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // check for tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (v instanceof GlyphView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            Segment s = ((GlyphView)v).getText(startOffset, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            char ch = s.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (ch == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // if the first character is a tab, create a dedicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // view for just the tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                endOffset = startOffset + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                for (ch = s.next(); ch != Segment.DONE; ch = s.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (ch == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        // found a tab, don't include it in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        endOffset = startOffset + s.getIndex() - s.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // determine limit from LineBreakMeasurer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int limitIndex = text.toIteratorIndex(endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (measurer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            int index = text.toIteratorIndex(startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (measurer.getPosition() != index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                measurer.setPosition(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            limitIndex = measurer.nextOffset(spanLeft, limitIndex, requireNextWord);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int pos = text.toModelPosition(limitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Synchronize the strategy with its FlowView.  Allows the strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * to update its state to account for changes in that portion of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * model represented by the FlowView.  Also allows the strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * to update the FlowView in response to these changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    void sync(FlowView fv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        View lv = getLogicalView(fv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        text.setView(lv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Container container = fv.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        FontRenderContext frc = sun.swing.SwingUtilities2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                    getFontRenderContext(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        BreakIterator iter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Container c = fv.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            iter = BreakIterator.getLineInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            iter = BreakIterator.getLineInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   305
        Object shaper = null;
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   306
        if (c instanceof JComponent) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   307
            shaper = ((JComponent) c).getClientProperty(
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   308
                                            TextAttribute.NUMERIC_SHAPING);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   309
        }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   310
        text.setShaper(shaper);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   311
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        measurer = new LineBreakMeasurer(text, iter, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // If the children of the FlowView's logical view are GlyphViews, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // need to have their painters updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int n = lv.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        for( int i=0; i<n; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            View child = lv.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if( child instanceof GlyphView ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                int p0 = child.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                int p1 = child.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                measurer.setPosition(text.toIteratorIndex(p0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                TextLayout layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    = measurer.nextLayout( Float.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                           text.toIteratorIndex(p1), false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                ((GlyphView)child).setGlyphPainter(new GlyphPainter2(layout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Reset measurer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        measurer.setPosition(text.getBeginIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    // --- variables -------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private LineBreakMeasurer measurer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private AttributedSegment text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Implementation of AttributedCharacterIterator that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * the GlyphView attributes for rendering the glyphs through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * TextLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    static class AttributedSegment extends Segment implements AttributedCharacterIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        AttributedSegment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        View getView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        void setView(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            this.v = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            Document doc = v.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            int p0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int p1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                doc.getText(p0, p1 - p0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw new IllegalArgumentException("Invalid view");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * Get a boundary position for the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * This is implemented to assume that two fonts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         * equal if their references are equal (i.e. that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * font came from a cache).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * @return the location in model coordinates.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         *  not the same as the Segment coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int getFontBoundary(int childIndex, int dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            Font f = getFont(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            for (childIndex += dir; (childIndex >= 0) && (childIndex < v.getViewCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                 childIndex += dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                Font next = getFont(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (next != f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    // this run is different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return (dir < 0) ? child.getStartOffset() : child.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * Get the font at the given child index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        Font getFont(int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (child instanceof GlyphView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return ((GlyphView)child).getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int toModelPosition(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return v.getStartOffset() + (index - getBeginIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int toIteratorIndex(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return pos - v.getStartOffset() + getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   410
        private void setShaper(Object shaper) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   411
            this.shaper = shaper;
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   412
        }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   413
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // --- AttributedCharacterIterator methods -------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * Returns the index of the first character of the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * with respect to all attributes containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public int getRunStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            int pos = toModelPosition(getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int i = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            View child = v.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return toIteratorIndex(child.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * Returns the index of the first character of the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * with respect to the given attribute containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        public int getRunStart(AttributedCharacterIterator.Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (attribute instanceof TextAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                int pos = toModelPosition(getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                int i = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                if (attribute == TextAttribute.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    return toIteratorIndex(getFontBoundary(i, -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * Returns the index of the first character of the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * with respect to the given attributes containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        public int getRunStart(Set<? extends Attribute> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            int index = getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Object[] a = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            for (int i = 0; i < a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                TextAttribute attr = (TextAttribute) a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                index = Math.max(getRunStart(attr), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return Math.min(getIndex(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * Returns the index of the first character following the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * with respect to all attributes containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        public int getRunLimit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            int pos = toModelPosition(getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            int i = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            View child = v.getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return toIteratorIndex(child.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * Returns the index of the first character following the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * with respect to the given attribute containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        public int getRunLimit(AttributedCharacterIterator.Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (attribute instanceof TextAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                int pos = toModelPosition(getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                int i = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (attribute == TextAttribute.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    return toIteratorIndex(getFontBoundary(i, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            return getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * Returns the index of the first character following the run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         * with respect to the given attributes containing the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public int getRunLimit(Set<? extends Attribute> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            int index = getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            Object[] a = attributes.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            for (int i = 0; i < a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                TextAttribute attr = (TextAttribute) a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                index = Math.min(getRunLimit(attr), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            return Math.max(getIndex(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * Returns a map with the attributes defined on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   500
        public Map<Attribute, Object> getAttributes() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            Object[] ka = keys.toArray();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   502
            Hashtable<Attribute, Object> h = new Hashtable<Attribute, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            for (int i = 0; i < ka.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                TextAttribute a = (TextAttribute) ka[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                Object value = getAttribute(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    h.put(a, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * Returns the value of the named attribute for the current character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * Returns null if the attribute is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * @param attribute the key of the attribute whose value is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        public Object getAttribute(AttributedCharacterIterator.Attribute attribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            int pos = toModelPosition(getIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            int childIndex = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (attribute == TextAttribute.FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                return getFont(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } else if( attribute == TextAttribute.RUN_DIRECTION ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    v.getDocument().getProperty(TextAttribute.RUN_DIRECTION);
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   526
            } else if (attribute == TextAttribute.NUMERIC_SHAPING) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   527
                return shaper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * Returns the keys of all attributes defined on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * iterator's text range. The set is empty if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * attributes are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   537
        public Set<Attribute> getAllAttributeKeys() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        View v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   543
        static Set<Attribute> keys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        static {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   546
            keys = new HashSet<Attribute>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            keys.add(TextAttribute.FONT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            keys.add(TextAttribute.RUN_DIRECTION);
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   549
            keys.add(TextAttribute.NUMERIC_SHAPING);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 1639
diff changeset
   552
        private Object shaper = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
}