jdk/src/share/classes/sun/awt/PlatformFont.java
author denis
Wed, 27 Mar 2013 16:19:51 +0400
changeset 16705 1caaa379eded
parent 7668 d4a77089c587
child 22567 5816a47fa4dd
permissions -rw-r--r--
7075105: WIN: Provide a way to format HTML on drop Reviewed-by: uta, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5935
diff changeset
     2
 * Copyright (c) 1996, 2010, 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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Vector mcs = null;
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    mcs = new Vector(3);
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,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                            len-lastIndex, currentFont);
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            result = new CharsetString[mcs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            for (int i = 0; i < mcs.size(); i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                result[i] = (CharsetString)mcs.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Is it possible that this font's metrics require the multi-font calls?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * This might be true, for example, if the font supports kerning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public boolean mightHaveMultiFontMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return fontConfig != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Specialized fast path string conversion for AWT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public Object[] makeConvertedMultiFontString(String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return makeConvertedMultiFontChars(str.toCharArray(),0,str.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public Object[] makeConvertedMultiFontChars(char[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                                int start, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Object[] result = new Object[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Object[] workingCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        byte[] convertedData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int stringIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int convertedDataIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int resultIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int cacheIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        FontDescriptor currentFontDescriptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        FontDescriptor lastFontDescriptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        char currentDefaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        PlatformFontCache theChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // Simple bounds check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int end = start + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (start < 0 || end > data.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if(stringIndex >= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // coversion loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        while(stringIndex < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            currentDefaultChar = data[stringIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // Note that cache sizes must be a power of two!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            cacheIndex = (int)(currentDefaultChar & this.FONTCACHEMASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            theChar = (PlatformFontCache)getFontCache()[cacheIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // Is the unicode char we want cached?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if(theChar == null || theChar.uniChar != currentDefaultChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                /* find a converter that can convert the current character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                currentFontDescriptor = defaultFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                currentDefaultChar = defaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                char ch = (char)data[stringIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                int componentCount = componentFonts.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                for (int j = 0; j < componentCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    FontDescriptor fontDescriptor = componentFonts[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    fontDescriptor.encoder.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    //fontDescriptor.encoder.onUnmappleCharacterAction(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    if (fontDescriptor.isExcluded(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    if (fontDescriptor.encoder.canEncode(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        currentFontDescriptor = fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        currentDefaultChar = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    char[] input = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    input[0] = currentDefaultChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    theChar = new PlatformFontCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    if (currentFontDescriptor.useUnicode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        currentFontDescriptor.unicodeEncoder.encode(CharBuffer.wrap(input),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                                                    theChar.bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                                                    true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        if (currentFontDescriptor.isLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            theChar.bb.put((byte)(input[0] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            theChar.bb.put((byte)(input[0] >>8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                            theChar.bb.put((byte)(input[0] >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                            theChar.bb.put((byte)(input[0] & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        currentFontDescriptor.encoder.encode(CharBuffer.wrap(input),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                                             theChar.bb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                                             true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    theChar.fontDescriptor = currentFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    theChar.uniChar = data[stringIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    getFontCache()[cacheIndex] = theChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                } catch(Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    // Should never happen!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // Check to see if we've changed fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if(lastFontDescriptor != theChar.fontDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if(lastFontDescriptor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    result[resultIndex++] = lastFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    result[resultIndex++] = convertedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    //  Add the size to the converted data field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    if(convertedData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        convertedDataIndex -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        convertedData[0] = (byte)(convertedDataIndex >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        convertedData[1] = (byte)(convertedDataIndex >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        convertedData[2] = (byte)(convertedDataIndex >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        convertedData[3] = (byte)convertedDataIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    if(resultIndex >= result.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        Object[] newResult = new Object[result.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        System.arraycopy(result, 0, newResult, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                         result.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        result = newResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (theChar.fontDescriptor.useUnicode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    convertedData = new byte[(end - stringIndex + 1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                        (int)theChar.fontDescriptor.unicodeEncoder.maxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                        + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    convertedData = new byte[(end - stringIndex + 1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                        (int)theChar.fontDescriptor.encoder.maxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                        + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                convertedDataIndex = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                lastFontDescriptor = theChar.fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            byte[] ba = theChar.bb.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            int size = theChar.bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if(size == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            else if(size == 2) {
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
            } else if(size == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                convertedData[convertedDataIndex++] = ba[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                convertedData[convertedDataIndex++] = ba[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else if(size == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                convertedData[convertedDataIndex++] = ba[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                convertedData[convertedDataIndex++] = ba[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                convertedData[convertedDataIndex++] = ba[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                convertedData[convertedDataIndex++] = ba[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            stringIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        result[resultIndex++] = lastFontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        result[resultIndex] = convertedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        //  Add the size to the converted data field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if(convertedData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            convertedDataIndex -= 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            convertedData[0] = (byte)(convertedDataIndex >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            convertedData[1] = (byte)(convertedDataIndex >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            convertedData[2] = (byte)(convertedDataIndex >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            convertedData[3] = (byte)convertedDataIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Create fontCache on demand instead of during construction to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * reduce overall memory consumption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * This method is declared final so that its code can be inlined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * by the compiler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    protected final Object[] getFontCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // This method is not MT-safe by design. Since this is just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // cache anyways, it's okay if we occasionally allocate the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // twice or return an array which will be dereferenced and gced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (fontCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            fontCache = new Object[this.FONTCACHESIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return fontCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    class PlatformFontCache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        char uniChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        FontDescriptor fontDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        ByteBuffer bb = ByteBuffer.allocate(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}