jdk/src/java.desktop/share/classes/sun/font/FontRunIterator.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26004 jdk/src/share/classes/sun/font/FontRunIterator.java@7507a1b93f67
parent 25859 jdk/src/share/classes/sun/font/FontRunIterator.java@3317bb8137f4
permissions -rw-r--r--
Merge
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
 * (C) Copyright IBM Corp. 2003 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Iterates over runs of fonts in a CompositeFont, optionally taking script runs into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public final class FontRunIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    CompositeFont font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    char[] text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    int start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    CompositeGlyphMapper mapper; // handy cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int slot = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public void init(CompositeFont font, char[] text, int start, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (font == null || text == null || start < 0 || limit < start || limit > text.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.limit = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.mapper = (CompositeGlyphMapper)font.getMapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.slot = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.pos = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public PhysicalFont getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return slot == -1 ? null : font.getSlotFont(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public int getGlyphMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return slot << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public int getPos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return pos;
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
     * characters that are in the 'common' script become part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * surrounding script run.  we want to fetch these from the same font
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * used to get surrounding characters, where possible.  but we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * want to force non-common characters to come from other than their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * standard font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * what we really want to do is this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * 1) fetch a code point from the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * 2) get its 'native' script code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * 3) determine its 'resolved' script code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * 4) if its native script is COMMON, and its resolved script is the same as the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *    code point's, then see if the previous font supports this code point.  if so, use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * 5) otherwise resolve the font as usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * 6) break the run when either the physical font or the resolved script changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * problems: we optimize latin-1 and cjk text assuming a fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * width for each character.  since latin-1 digits and punctuation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * are common, following this algorithm they will change to match
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    93
     * the fonts used for the preceding text, and potentially change metrics.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * this also seems to have the potential for changing arbitrary runs of text, e.g.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    96
     * any number of digits and spaces can change depending on the preceding (or following!)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * non-COMMON character's font assignment.  this is not good.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * since the goal is to enable layout to be performed using as few physical fonts as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * possible, and the primary cause of switching fonts is to handle spaces, perhaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * we should just special-case spaces and assign them from the current font, whatever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * it may be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * One could also argue that the job of the composite font is to assign physical fonts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * to text runs, however it wishes.  we don't necessarily have to provide script info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to let it do this.  it can determine based on whatever.  so having a special 'next'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * function that takes script (and limit) is redundant.  It can fetch the script again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * if need be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * both this and the script iterator are turning char sequences into code point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * sequences.  maybe it would be better to feed a single code point into each iterator-- push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the data instead of pull it?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public boolean next(int script, int lim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (pos == lim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int ch = nextCodePoint(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int sl = mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        slot = sl >>> 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while ((ch = nextCodePoint(lim)) != DONE && (mapper.charToGlyph(ch) & CompositeGlyphMapper.SLOTMASK) == sl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        pushback(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return next(Script.COMMON, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static final int SURROGATE_START = 0x10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static final int LEAD_START = 0xd800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static final int LEAD_LIMIT = 0xdc00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static final int TAIL_START = 0xdc00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static final int TAIL_LIMIT = 0xe000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static final int LEAD_SURROGATE_SHIFT = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static final int SURROGATE_OFFSET = SURROGATE_START - (LEAD_START << LEAD_SURROGATE_SHIFT) - TAIL_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static final int DONE = -1;
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
    int nextCodePoint() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return nextCodePoint(limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   147
    int nextCodePoint(int lim) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (pos >= lim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int ch = text[pos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (ch >= LEAD_START && ch < LEAD_LIMIT && pos < lim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int nch = text[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (nch >= TAIL_START && nch < TAIL_LIMIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                ++pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                ch = (ch << LEAD_SURROGATE_SHIFT) + nch + SURROGATE_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 21278
diff changeset
   162
    void pushback(int ch) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (ch >= 0x10000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                pos -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                pos -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}