jdk/src/share/classes/sun/awt/PlatformFont.java
author anthony
Tue, 12 Aug 2014 14:22:05 +0400
changeset 26342 3637212ae8f2
parent 24538 25bf8153fbfe
permissions -rw-r--r--
8049065: [JLightweightFrame] Support DnD for SwingNode Summary: Delegate DnD operations to LightweightContent when appropriate Reviewed-by: ant, pchelko
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 7668
diff changeset
     2
 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.peer.FontPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
5935
f247e59ab3ca 6943487: NPE in makeMultiCharsetString while printing on linux
prr
parents: 5506
diff changeset
    31
import sun.font.SunFontManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.java2d.FontSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public abstract class PlatformFont implements FontPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        NativeLibLoader.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected FontDescriptor[] componentFonts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected char defaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected FontConfiguration fontConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected FontDescriptor defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected String familyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Object[] fontCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Maybe this should be a property that is set based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // on the locale?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected static int FONTCACHESIZE = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected static int FONTCACHEMASK = PlatformFont.FONTCACHESIZE - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected static String osVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public PlatformFont(String name, int style){
5935
f247e59ab3ca 6943487: NPE in makeMultiCharsetString while printing on linux
prr
parents: 5506
diff changeset
    60
        SunFontManager sfm = SunFontManager.getInstance();
f247e59ab3ca 6943487: NPE in makeMultiCharsetString while printing on linux
prr
parents: 5506
diff changeset
    61
        if (sfm instanceof FontSupport) {
f247e59ab3ca 6943487: NPE in makeMultiCharsetString while printing on linux
prr
parents: 5506
diff changeset
    62
            fontConfig = ((FontSupport)sfm).getFontConfiguration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (fontConfig == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // map given font name to a valid logical font family name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        familyName = name.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (!FontConfiguration.isLogicalFontFamilyName(familyName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            familyName = fontConfig.getFallbackFamilyName(familyName, "sansserif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        componentFonts = fontConfig.getFontDescriptors(familyName, style);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // search default character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        char missingGlyphCharacter = getMissingGlyphCharacter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        defaultChar = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (componentFonts.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            defaultFont = componentFonts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int i = 0; i < componentFonts.length; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (componentFonts[i].isExcluded(missingGlyphCharacter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (componentFonts[i].encoder.canEncode(missingGlyphCharacter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                defaultFont = componentFonts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                defaultChar = missingGlyphCharacter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Returns the character that should be rendered when a glyph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * is missing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected abstract char getMissingGlyphCharacter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * make a array of CharsetString with given String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public CharsetString[] makeMultiCharsetString(String str){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return makeMultiCharsetString(str.toCharArray(), 0, str.length(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * make a array of CharsetString with given String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public CharsetString[] makeMultiCharsetString(String str, boolean allowdefault){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return makeMultiCharsetString(str.toCharArray(), 0, str.length(), allowdefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * make a array of CharsetString with given char array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param str The char array to convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param offset offset of first character of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param len number of characters to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public CharsetString[] makeMultiCharsetString(char str[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return makeMultiCharsetString(str, offset, len, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * make a array of CharsetString with given char array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param str The char array to convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param offset offset of first character of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param len number of characters to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param allowDefault whether to allow the default char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Setting this to true overloads the meaning of this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * return non-null only if all chars can be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return array of CharsetString or if allowDefault is false and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * of the returned chars would have been converted to a default char,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * then return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * This is used to choose alternative means of displaying the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public CharsetString[] makeMultiCharsetString(char str[], int offset, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                  boolean allowDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (len < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return new CharsetString[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 22584
diff changeset
   146
        Vector<CharsetString> mcs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        char[] tmpStr = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        char tmpChar = defaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        boolean encoded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        FontDescriptor currentFont = defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i = 0; i < componentFonts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (componentFonts[i].isExcluded(str[offset])){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            /* Need "encoded" variable to distinguish the case when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             * the default char is the same as the encoded char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             * The defaultChar on Linux is '?' so it is needed there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (componentFonts[i].encoder.canEncode(str[offset])){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                currentFont = componentFonts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                tmpChar = str[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                encoded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!allowDefault && !encoded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            tmpStr[0] = tmpChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int lastIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        for (int i = 1; i < len; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            char ch = str[offset + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            FontDescriptor fd = defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            tmpChar = defaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            encoded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            for (int j = 0; j < componentFonts.length; j++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (componentFonts[j].isExcluded(ch)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (componentFonts[j].encoder.canEncode(ch)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    fd = componentFonts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    tmpChar = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    encoded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (!allowDefault && !encoded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                tmpStr[i] = tmpChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (currentFont != fd){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (mcs == null) {
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 22584
diff changeset
   201
                    mcs = new Vector<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                mcs.addElement(new CharsetString(tmpStr, lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                 i-lastIndex, currentFont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                currentFont = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                fd = defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                lastIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        CharsetString[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        CharsetString cs = new CharsetString(tmpStr, lastIndex,
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 22584
diff changeset
   212
                                             len-lastIndex, currentFont);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (mcs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            result = new CharsetString[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            result[0] = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            mcs.addElement(cs);
24538
25bf8153fbfe 8039642: Fix raw and unchecked warnings in sun.awt.*
henryjen
parents: 22584
diff changeset
   218
            result = mcs.toArray(new CharsetString[mcs.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Is it possible that this font's metrics require the multi-font calls?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * This might be true, for example, if the font supports kerning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public boolean mightHaveMultiFontMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return fontConfig != null;
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
     * Specialized fast path string conversion for AWT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public Object[] makeConvertedMultiFontString(String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return makeConvertedMultiFontChars(str.toCharArray(),0,str.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public Object[] makeConvertedMultiFontChars(char[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                                int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Object[] result = new Object[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        Object[] workingCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] convertedData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int stringIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int convertedDataIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int resultIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int cacheIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        FontDescriptor currentFontDescriptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        FontDescriptor lastFontDescriptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        char currentDefaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        PlatformFontCache theChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // Simple bounds check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int end = start + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (start < 0 || end > data.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if(stringIndex >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // coversion loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        while(stringIndex < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            currentDefaultChar = data[stringIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // Note that cache sizes must be a power of two!
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 22567
diff changeset
   270
            cacheIndex = (currentDefaultChar & PlatformFont.FONTCACHEMASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            theChar = (PlatformFontCache)getFontCache()[cacheIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // Is the unicode char we want cached?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if(theChar == null || theChar.uniChar != currentDefaultChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                /* find a converter that can convert the current character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                currentFontDescriptor = defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                currentDefaultChar = defaultChar;
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 22567
diff changeset
   280
                char ch = data[stringIndex];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                int componentCount = componentFonts.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                for (int j = 0; j < componentCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    FontDescriptor fontDescriptor = componentFonts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    fontDescriptor.encoder.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    //fontDescriptor.encoder.onUnmappleCharacterAction(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    if (fontDescriptor.isExcluded(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    if (fontDescriptor.encoder.canEncode(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        currentFontDescriptor = fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        currentDefaultChar = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    char[] input = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    input[0] = currentDefaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    theChar = new PlatformFontCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (currentFontDescriptor.useUnicode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        currentFontDescriptor.unicodeEncoder.encode(CharBuffer.wrap(input),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                                                    theChar.bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                                    true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        */
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 7668
diff changeset
   309
                        if (FontDescriptor.isLE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            theChar.bb.put((byte)(input[0] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            theChar.bb.put((byte)(input[0] >>8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            theChar.bb.put((byte)(input[0] >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            theChar.bb.put((byte)(input[0] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        currentFontDescriptor.encoder.encode(CharBuffer.wrap(input),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                                             theChar.bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                                             true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    theChar.fontDescriptor = currentFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    theChar.uniChar = data[stringIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    getFontCache()[cacheIndex] = theChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                } catch(Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    // Should never happen!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // Check to see if we've changed fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if(lastFontDescriptor != theChar.fontDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if(lastFontDescriptor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    result[resultIndex++] = lastFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    result[resultIndex++] = convertedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    //  Add the size to the converted data field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if(convertedData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        convertedDataIndex -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        convertedData[0] = (byte)(convertedDataIndex >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        convertedData[1] = (byte)(convertedDataIndex >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        convertedData[2] = (byte)(convertedDataIndex >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        convertedData[3] = (byte)convertedDataIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if(resultIndex >= result.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        Object[] newResult = new Object[result.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        System.arraycopy(result, 0, newResult, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                         result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        result = newResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (theChar.fontDescriptor.useUnicode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    convertedData = new byte[(end - stringIndex + 1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                        (int)theChar.fontDescriptor.unicodeEncoder.maxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                        + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    convertedData = new byte[(end - stringIndex + 1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                        (int)theChar.fontDescriptor.encoder.maxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                        + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                convertedDataIndex = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                lastFontDescriptor = theChar.fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            byte[] ba = theChar.bb.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int size = theChar.bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if(size == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            else if(size == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                convertedData[convertedDataIndex++] = ba[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else if(size == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                convertedData[convertedDataIndex++] = ba[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                convertedData[convertedDataIndex++] = ba[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } else if(size == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                convertedData[convertedDataIndex++] = ba[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                convertedData[convertedDataIndex++] = ba[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                convertedData[convertedDataIndex++] = ba[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            stringIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        result[resultIndex++] = lastFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        result[resultIndex] = convertedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        //  Add the size to the converted data field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if(convertedData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            convertedDataIndex -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            convertedData[0] = (byte)(convertedDataIndex >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            convertedData[1] = (byte)(convertedDataIndex >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            convertedData[2] = (byte)(convertedDataIndex >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            convertedData[3] = (byte)convertedDataIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Create fontCache on demand instead of during construction to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * reduce overall memory consumption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This method is declared final so that its code can be inlined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * by the compiler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected final Object[] getFontCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // This method is not MT-safe by design. Since this is just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // cache anyways, it's okay if we occasionally allocate the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // twice or return an array which will be dereferenced and gced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (fontCache == null) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 7668
diff changeset
   420
            fontCache = new Object[PlatformFont.FONTCACHESIZE];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return fontCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    class PlatformFontCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        char uniChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        FontDescriptor fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        ByteBuffer bb = ByteBuffer.allocate(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
}