jdk/src/solaris/classes/sun/font/NativeFont.java
author rkennke
Fri, 07 Aug 2009 18:31:11 +0200
changeset 3928 be186a33df9b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6795908: Refactor FontManager Reviewed-by: prr, igor
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
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.FontFormatException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.GeneralPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Ideally there would be no native fonts used, and this class would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * unneeded and removed. Presently it is still needed until such time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * as font configuration files (or the implementation equivalent) can have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * all references to fonts that are not handled via Java 2D removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Currently there are two cases where this class is needed, both on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Unix, primarily Solaris, but useful on Linux too if fonts have moved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 1. Some legacy F3 fonts are still referenced so that AWT "X/Motif"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * can get dingbats and symbols from them. This can be dispensed with when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * either AWT is based on 2D, or when the X font path is known to always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * contain a Type1 or TrueType font that can be used in font configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * files to replace the F3 fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 2. When location of font files by 2D fails, because of some system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * configuration problem, it is desirable to have a fall back to some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * functionality that lessens the immediate impact on users. Being able
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * to perform limited operations by using bitmaps from X11 helps here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class NativeFont extends PhysicalFont {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    String encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int numGlyphs = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    boolean isBitmapDelegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    PhysicalFont delegateFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Verifies native font is accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @throws FontFormatException - if the font can't be located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public NativeFont(String platName, boolean bitmapDelegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws FontFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super(platName, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        /* This is set true if this is an instance of a NativeFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * created by some other font, to get native bitmaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * The delegating font will call this font only for "basic"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * cases - ie non-rotated, uniform scale, monochrome bitmaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * If this is false, then this instance may need to itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * delegate to another font for non-basic cases. Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * NativeFonts are used in that way only for symbol and dingbats
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * we know its safe to delegate these to the JRE's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * physical font (Lucida Sans Regular).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        isBitmapDelegate = bitmapDelegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new FontFormatException("Native font in headless toolkit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fontRank = Font2D.NATIVE_RANK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        initNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (getNumGlyphs() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
          throw new FontFormatException("Couldn't locate font" + platName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void initNames() throws FontFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* Valid XLFD has exactly 14 "-" chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * First run over the string to verify have at least this many
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * At the same time record the locations of the hyphens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * so we can just pick the right substring later on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int[] hPos = new int[14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int hyphenCnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int pos = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String xlfd = platName.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (xlfd.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            while (pos != -1 && hyphenCnt < 14) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                pos = xlfd.indexOf('-', pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (pos != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    hPos[hyphenCnt++] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (hyphenCnt == 14 && pos != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            /* Capitalise words in the Family name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            String tmpFamily = xlfd.substring(hPos[1]+1, hPos[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            StringBuilder sBuffer = new StringBuilder(tmpFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            char ch = Character.toUpperCase(sBuffer.charAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            sBuffer.replace(0, 1, String.valueOf(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            for (int i=1;i<sBuffer.length()-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (sBuffer.charAt(i) == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    ch = Character.toUpperCase(sBuffer.charAt(i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    sBuffer.replace(i+1, i+2, String.valueOf(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            familyName = sBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            String tmpWeight = xlfd.substring(hPos[2]+1, hPos[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            String tmpSlant = xlfd.substring(hPos[3]+1, hPos[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            String styleStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (tmpWeight.indexOf("bold") >= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                tmpWeight.indexOf("demi") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                style |= Font.BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                styleStr = "Bold";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (tmpSlant.equals("i") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                tmpSlant.indexOf("italic") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                style |= Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (styleStr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    styleStr = "Italic";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    styleStr = styleStr + " Italic";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            else if (tmpSlant.equals("o") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                tmpSlant.indexOf("oblique") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                style |= Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (styleStr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    styleStr = "Oblique";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    styleStr = styleStr + " Oblique";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (styleStr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                fullName = familyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                fullName = familyName + " " + styleStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            encoding = xlfd.substring(hPos[12]+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (encoding.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                encoding = xlfd.substring(hPos[13]+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (encoding.indexOf("fontspecific") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (tmpFamily.indexOf("dingbats") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    encoding = "dingbats";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else if (tmpFamily.indexOf("symbol") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    encoding = "symbol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    encoding = "iso8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new FontFormatException("Bad native name " + platName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
//             familyName = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
//             fullName = "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
//             style = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
//             encoding = "iso8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /* Wildcard all the size fields in the XLFD and retrieve a list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * XLFD's that match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * We only look for scaleable fonts, so we can just replace the 0's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * with *'s and see what we get back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * No matches means even the scaleable version wasn't found. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * means the X font path isn't set up for this font at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * One match means only the scaleable version we started with was found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * -monotype-arial-bold-i-normal--0-0-0-0-p-0-iso8859-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Two matches apparently means as well as the above, a scaleable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * specified for 72 dpi is found, not that there are bitmaps : eg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * -monotype-arial-bold-i-normal--0-0-72-72-p-0-iso8859-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * So require at least 3 matches (no need to parse) to determine that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * there are external bitmaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static boolean hasExternalBitmaps(String platName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /* Turn -monotype-arial-bold-i-normal--0-0-0-0-p-0-iso8859-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * into -monotype-arial-bold-i-normal--*-*-*-*-p-*-iso8859-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * by replacing all -0- substrings with -*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        StringBuilder sb = new StringBuilder(platName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int pos = sb.indexOf("-0-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        while (pos >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            sb.replace(pos+1, pos+2, "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            pos = sb.indexOf("-0-", pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        String xlfd = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            bytes = xlfd.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            bytes = xlfd.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return haveBitmapFonts(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static boolean fontExists(String xlfd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            bytes = xlfd.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            bytes = xlfd.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return fontExists(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private static native boolean haveBitmapFonts(byte[] xlfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private static native boolean fontExists(byte[] xlfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public CharToGlyphMapper getMapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (mapper == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (isBitmapDelegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                /* we are a delegate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                mapper = new NativeGlyphMapper(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                /* we need to delegate */
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   245
                SunFontManager fm = SunFontManager.getInstance();
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   246
                delegateFont = fm.getDefaultPhysicalFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                mapper = delegateFont.getMapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return mapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    FontStrike createStrike(FontStrikeDesc desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (isBitmapDelegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return new NativeStrike(this, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (delegateFont == null) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   258
                SunFontManager fm = SunFontManager.getInstance();
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   259
                delegateFont = fm.getDefaultPhysicalFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            /* If no FileFont's are found, delegate font may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
             * a NativeFont, so we need to avoid recursing here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (delegateFont instanceof NativeFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return new NativeStrike((NativeFont)delegateFont, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            FontStrike delegate = delegateFont.createStrike(desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return new DelegateStrike(this, desc, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public Rectangle2D getMaxCharBounds(FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    native StrikeMetrics getFontMetrics(long pScalerContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    native float getGlyphAdvance(long pContext, int glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                            int glyphCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return new Rectangle2D.Float(0f, 0f, 0f, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public GeneralPath getGlyphOutline(long pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                       int glyphCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                       float x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                       float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    native long getGlyphImage(long pScalerContext, int glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    native long getGlyphImageNoDefault(long pScalerContext, int glyphCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void getGlyphMetrics(long pScalerContext, int glyphCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        Point2D.Float metrics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        throw new RuntimeException("this should be called on the strike");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public  GeneralPath getGlyphVectorOutline(long pScalerContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                              int[] glyphs, int numGlyphs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                              float x,  float y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private native int countGlyphs(byte[] platformNameBytes, int ptSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public int getNumGlyphs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (numGlyphs == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            byte[] bytes = getPlatformNameBytes(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            numGlyphs = countGlyphs(bytes, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return numGlyphs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    PhysicalFont getDelegateFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (delegateFont == null) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   319
            SunFontManager fm = SunFontManager.getInstance();
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2
diff changeset
   320
            delegateFont = fm.getDefaultPhysicalFont();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return delegateFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /* Specify that the dpi is 72x72, as this corresponds to JDK's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * default user space. These are the 10th and 11th fields in the XLFD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * ptSize in XLFD is in 10th's of a point so multiply by 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Replace the 9th field in the XLFD (ie after the 8th hyphen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * with this pt size (this corresponds to the field that's "%d" in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * font configuration files). Wild card the other numeric fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * ie to request 12 pt Times New Roman italic font, use an XLFD like :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * -monotype-times new roman-regular-i---*-120-72-72-p-*-iso8859-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    byte[] getPlatformNameBytes(int ptSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int[] hPos = new int[14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int hyphenCnt = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int pos = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        while (pos != -1 && hyphenCnt < 14) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            pos = platName.indexOf('-', pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (pos != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                hPos[hyphenCnt++] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        String sizeStr = Integer.toString((int)Math.abs(ptSize)*10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        StringBuilder sb = new StringBuilder(platName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        /* work backwards so as to not invalidate the positions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        sb.replace(hPos[11]+1, hPos[12], "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        sb.replace(hPos[9]+1, hPos[10], "72");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        sb.replace(hPos[8]+1, hPos[9], "72");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /* replace the 3 lines above with the next 3 lines to get the 1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
//      sb.replace(hPos[11]+1, hPos[12], "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
//      sb.replace(hPos[9]+1, hPos[10], "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
//      sb.replace(hPos[8]+1, hPos[9], "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        sb.replace(hPos[7]+1, hPos[8], sizeStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        sb.replace(hPos[6]+1, hPos[7], "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /* replace the 1 line above with the next line to get the 1.4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
//      sb.replace(hPos[6]+1, hPos[7], "0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        /* comment out this block to the the 1.4.2 behaviour */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (hPos[0] == 0 && hPos[1] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            /* null foundry name : some linux font configuration files have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
             * symbol font entries like this and its just plain wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
             * Replace with a wild card. (Although those fonts should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
             * located via disk access rather than X11).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
           sb.replace(hPos[0]+1, hPos[1], "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        String xlfd = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        byte[] bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            bytes = xlfd.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            bytes = xlfd.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return " ** Native Font: Family="+familyName+ " Name="+fullName+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            " style="+style+" nativeName="+platName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}