jdk/src/java.desktop/share/classes/sun/font/ScriptRun.java
author prr
Wed, 09 Nov 2016 11:28:13 -0800
changeset 42208 7c1017f0ade5
parent 35667 ed476aba94de
permissions -rw-r--r--
8155874: Fix java.desktop deprecation warnings about Class.newInstance Reviewed-by: serb, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * 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
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *******************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *   Copyright (C) 1999-2003, International Business Machines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   Corporation and others.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *******************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
    38
 * {@code ScriptRun} is used to find runs of characters in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
    39
 * the same script, as defined in the {@code Script} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * It implements a simple iterator over an array of characters.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
    41
 * The iterator will assign {@code COMMON} and {@code INHERITED}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    42
 * characters to the same script as the preceding characters. If the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * COMMON and INHERITED characters are first, they will be assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the same script as the following characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The iterator will try to match paired punctuation. If it sees an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * opening punctuation character, it will remember the script that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * was assigned to that character, and assign the same script to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * matching closing punctuation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * No attempt is made to combine related scripts into a single run. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * particular, Hiragana, Katakana, and Han characters will appear in seperate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * runs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Here is an example of how to iterate over script runs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * void printScriptRuns(char[] text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     ScriptRun scriptRun = new ScriptRun(text, 0, text.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     while (scriptRun.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         int start  = scriptRun.getScriptStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *         int limit  = scriptRun.getScriptLimit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         int script = scriptRun.getScriptCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         System.out.println("Script \"" + Script.getName(script) + "\" from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *                            start + " to " + limit + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public final class ScriptRun
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private char[] text;   // fixed once set by constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int textStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private int textLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private int scriptStart;     // change during iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int scriptLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private int scriptCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private int stack[];         // stack used to handle paired punctuation if encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int parenSP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public ScriptRun() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // must call init later or we die.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
    91
     * Construct a {@code ScriptRun} object which iterates over a subrange
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * of the given characetrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param chars the array of characters over which to iterate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param start the index of the first character over which to iterate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param count the number of characters over which to iterate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public ScriptRun(char[] chars, int start, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        init(chars, start, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public void init(char[] chars, int start, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (chars == null || start < 0 || count < 0 || count > chars.length - start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        text = chars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        textStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        textLimit = start + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        scriptStart = textStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        scriptLimit = textStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        scriptCode = Script.INVALID_CODE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        parenSP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Get the starting index of the current script run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the index of the first character in the current script run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   124
    public int getScriptStart() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return scriptStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Get the index of the first character after the current script run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the index of the first character after the current script run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   133
    public int getScriptLimit() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return scriptLimit;
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
     * Get the script code for the script of the current script run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the script code for the script of the current script run.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 26037
diff changeset
   141
     * @see Script
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   143
    public int getScriptCode() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return scriptCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
   148
     * Find the next script run. Returns {@code false} if there
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
   149
     * isn't another run, returns {@code true} if there is.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
   151
     * @return {@code false} if there isn't another run, {@code true} if there is.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   153
    public boolean next() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int startSP  = parenSP;  // used to find the first new open character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // if we've fallen off the end of the text, we're done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (scriptLimit >= textLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        scriptCode  = Script.COMMON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        scriptStart = scriptLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        while ((ch = nextCodePoint()) != DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            int sc = ScriptRunData.getScript(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int pairIndex = sc == Script.COMMON ? getPairIndex(ch) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Paired character handling:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // if it's an open character, push it onto the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // if it's a close character, find the matching open on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // stack, and use that script code. Any non-matching open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // characters above it on the stack will be popped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (pairIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if ((pairIndex & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    if (stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        stack = new int[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    } else if (parenSP == stack.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        int[] newstack = new int[stack.length + 32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        System.arraycopy(stack, 0, newstack, 0, stack.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        stack = newstack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    stack[parenSP++] = pairIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    stack[parenSP++] = scriptCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                } else if (parenSP > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    int pi = pairIndex & ~1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    while ((parenSP -= 2) >= 0 && stack[parenSP] != pi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    if (parenSP >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        sc = stack[parenSP+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                      parenSP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    if (parenSP < startSP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        startSP = parenSP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (sameScript(scriptCode, sc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (scriptCode <= Script.INHERITED && sc > Script.INHERITED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    scriptCode = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    // now that we have a final script code, fix any open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    // characters we pushed before we knew the script code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    while (startSP < parenSP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        stack[startSP+1] = scriptCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        startSP += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                // if this character is a close paired character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // pop it from the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (pairIndex > 0 && (pairIndex & 1) != 0 && parenSP > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    parenSP -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // We've just seen the first character of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // the next run. Back over it so we'll see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                // it again the next time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                pushback(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // we're outta here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static final int SURROGATE_START = 0x10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static final int LEAD_START = 0xd800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static final int LEAD_LIMIT = 0xdc00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    static final int TAIL_START = 0xdc00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static final int TAIL_LIMIT = 0xe000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    static final int LEAD_SURROGATE_SHIFT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static final int DONE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   245
    private int nextCodePoint() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (scriptLimit >= textLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int ch = text[scriptLimit++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (ch >= LEAD_START && ch < LEAD_LIMIT && scriptLimit < textLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int nch = text[scriptLimit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (nch >= TAIL_START && nch < TAIL_LIMIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                ++scriptLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   260
    private void pushback(int ch) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (ch >= 0x10000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                scriptLimit -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                scriptLimit -= 1;
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
     * Compare two script codes to see if they are in the same script. If one script is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * a strong script, and the other is INHERITED or COMMON, it will compare equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param scriptOne one of the script codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param scriptTwo the other script code.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30948
diff changeset
   276
     * @return {@code true} if the two scripts are the same.
30948
0a0972d3b58d 6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents: 26037
diff changeset
   277
     * @see Script
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static boolean sameScript(int scriptOne, int scriptTwo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return scriptOne == scriptTwo || scriptOne <= Script.INHERITED || scriptTwo <= Script.INHERITED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Find the highest bit that's set in a word. Uses a binary search through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * the bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param n the word in which to find the highest bit that's set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @return the bit number (counting from the low order bit) of the highest bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   290
    private static byte highBit(int n)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return -32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        byte bit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (n >= 1 << 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            n >>= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            bit += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (n >= 1 << 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            n >>= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            bit += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (n >= 1 << 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            n >>= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            bit += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (n >= 1 << 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            n >>= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            bit += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (n >= 1 << 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            n >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            bit += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Search the pairedChars array for the given character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param ch the character for which to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return the index of the character in the table, or -1 if it's not there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static int getPairIndex(int ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int probe = pairedCharPower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (ch >= pairedChars[pairedCharExtra]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            index = pairedCharExtra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        while (probe > (1 << 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            probe >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (ch >= pairedChars[index + probe]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                index += probe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (pairedChars[index] != ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    // all common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static int pairedChars[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        0x0028, 0x0029, // ascii paired punctuation  // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        0x003c, 0x003e, // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        0x005b, 0x005d, // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        0x007b, 0x007d, // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        0x00ab, 0x00bb, // guillemets // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        0x2018, 0x2019, // general punctuation // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        0x201c, 0x201d, // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        0x2039, 0x203a, // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        0x3008, 0x3009, // chinese paired punctuation // common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        0x300a, 0x300b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        0x300c, 0x300d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        0x300e, 0x300f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        0x3010, 0x3011,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        0x3014, 0x3015,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        0x3016, 0x3017,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        0x3018, 0x3019,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        0x301a, 0x301b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static final int pairedCharPower = 1 << highBit(pairedChars.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static final int pairedCharExtra = pairedChars.length - pairedCharPower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}