1687N/A/*
4944N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
1687N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1687N/A *
1687N/A * This code is free software; you can redistribute it and/or modify it
1687N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1687N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1687N/A *
1687N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1687N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1687N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1687N/A * version 2 for more details (a copy is included in the LICENSE file that
1687N/A * accompanied this code).
1687N/A *
1687N/A * You should have received a copy of the GNU General Public License version
1687N/A * 2 along with this work; if not, write to the Free Software Foundation,
1687N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1687N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1687N/A */
1687N/A
1687N/Apackage sun.font;
1687N/A
1687N/Aimport java.awt.Font;
1687N/Aimport java.io.BufferedReader;
1687N/Aimport java.io.File;
1687N/Aimport java.io.FileInputStream;
1687N/Aimport java.io.InputStreamReader;
3540N/Aimport java.lang.ref.SoftReference;
3540N/Aimport java.util.concurrent.ConcurrentHashMap;
1687N/Aimport java.security.AccessController;
1687N/A
2124N/Aimport java.security.PrivilegedAction;
1687N/Aimport javax.swing.plaf.FontUIResource;
1687N/A
1696N/Aimport sun.util.logging.PlatformLogger;
1687N/A
1687N/A/**
1687N/A * A collection of utility methods.
1687N/A */
1687N/Apublic final class FontUtilities {
1687N/A
2124N/A public static boolean isSolaris;
1687N/A
2124N/A public static boolean isLinux;
1687N/A
4632N/A public static boolean isMacOSX;
4632N/A
2124N/A public static boolean isSolaris8;
1687N/A
2124N/A public static boolean isSolaris9;
1687N/A
2124N/A public static boolean isOpenSolaris;
1687N/A
2124N/A public static boolean useT2K;
1687N/A
2124N/A public static boolean isWindows;
1687N/A
2124N/A public static boolean isOpenJDK;
1687N/A
1687N/A static final String LUCIDA_FILE_NAME = "LucidaSansRegular.ttf";
1687N/A
2515N/A private static boolean debugFonts = false;
2515N/A private static PlatformLogger logger = null;
2515N/A private static boolean logging;
2515N/A
1687N/A // This static initializer block figures out the OS constants.
1687N/A static {
1687N/A
2124N/A AccessController.doPrivileged(new PrivilegedAction () {
2124N/A public Object run() {
2124N/A String osName = System.getProperty("os.name", "unknownOS");
2124N/A isSolaris = osName.startsWith("SunOS");
1687N/A
2124N/A isLinux = osName.startsWith("Linux");
1687N/A
4944N/A isMacOSX = osName.contains("OS X"); // TODO: MacOSX
4632N/A
2124N/A String t2kStr = System.getProperty("sun.java2d.font.scaler");
2124N/A if (t2kStr != null) {
2124N/A useT2K = "t2k".equals(t2kStr);
2124N/A } else {
2124N/A useT2K = false;
2124N/A }
2124N/A if (isSolaris) {
2124N/A String version = System.getProperty("os.version", "0.0");
2124N/A isSolaris8 = version.startsWith("5.8");
2124N/A isSolaris9 = version.startsWith("5.9");
2124N/A float ver = Float.parseFloat(version);
2124N/A if (ver > 5.10f) {
2124N/A File f = new File("/etc/release");
2124N/A String line = null;
2124N/A try {
2124N/A FileInputStream fis = new FileInputStream(f);
2124N/A InputStreamReader isr = new InputStreamReader(
1687N/A fis, "ISO-8859-1");
2124N/A BufferedReader br = new BufferedReader(isr);
2124N/A line = br.readLine();
2124N/A fis.close();
2124N/A } catch (Exception ex) {
2124N/A // Nothing to do here.
2124N/A }
2124N/A if (line != null && line.indexOf("OpenSolaris") >= 0) {
2124N/A isOpenSolaris = true;
2124N/A } else {
2124N/A isOpenSolaris = false;
2124N/A }
2124N/A } else {
2124N/A isOpenSolaris = false;
2124N/A }
1687N/A } else {
2124N/A isSolaris8 = false;
2124N/A isSolaris9 = false;
1687N/A isOpenSolaris = false;
1687N/A }
2124N/A isWindows = osName.startsWith("Windows");
2124N/A String jreLibDirName = System.getProperty("java.home", "")
2124N/A + File.separator + "lib";
2124N/A String jreFontDirName =
2124N/A jreLibDirName + File.separator + "fonts";
2124N/A File lucidaFile = new File(jreFontDirName + File.separator
2124N/A + LUCIDA_FILE_NAME);
2124N/A isOpenJDK = !lucidaFile.exists();
2515N/A
2515N/A String debugLevel =
2515N/A System.getProperty("sun.java2d.debugfonts");
2515N/A
2515N/A if (debugLevel != null && !debugLevel.equals("false")) {
2515N/A debugFonts = true;
2515N/A logger = PlatformLogger.getLogger("sun.java2d");
2515N/A if (debugLevel.equals("warning")) {
2515N/A logger.setLevel(PlatformLogger.WARNING);
2515N/A } else if (debugLevel.equals("severe")) {
2515N/A logger.setLevel(PlatformLogger.SEVERE);
2515N/A }
2515N/A }
2515N/A
2515N/A if (debugFonts) {
2515N/A logger = PlatformLogger.getLogger("sun.java2d");
2515N/A logging = logger.isEnabled();
2515N/A }
2515N/A
2124N/A return null;
1687N/A }
2124N/A });
1687N/A }
1687N/A
1687N/A /**
1687N/A * Referenced by code in the JDK which wants to test for the
1687N/A * minimum char code for which layout may be required.
1687N/A * Note that even basic latin text can benefit from ligatures,
1687N/A * eg "ffi" but we presently apply those only if explicitly
1687N/A * requested with TextAttribute.LIGATURES_ON.
1687N/A * The value here indicates the lowest char code for which failing
1687N/A * to invoke layout would prevent acceptable rendering.
1687N/A */
1687N/A public static final int MIN_LAYOUT_CHARCODE = 0x0300;
1687N/A
1687N/A /**
1687N/A * Referenced by code in the JDK which wants to test for the
1687N/A * maximum char code for which layout may be required.
1687N/A * Note this does not account for supplementary characters
1687N/A * where the caller interprets 'layout' to mean any case where
1687N/A * one 'char' (ie the java type char) does not map to one glyph
1687N/A */
1687N/A public static final int MAX_LAYOUT_CHARCODE = 0x206F;
1687N/A
1687N/A /**
1687N/A * Calls the private getFont2D() method in java.awt.Font objects.
1687N/A *
1687N/A * @param font the font object to call
1687N/A *
1687N/A * @return the Font2D object returned by Font.getFont2D()
1687N/A */
1687N/A public static Font2D getFont2D(Font font) {
1687N/A return FontAccess.getFontAccess().getFont2D(font);
1687N/A }
1687N/A
1687N/A /**
1687N/A * If there is anything in the text which triggers a case
1687N/A * where char->glyph does not map 1:1 in straightforward
1687N/A * left->right ordering, then this method returns true.
1687N/A * Scripts which might require it but are not treated as such
1687N/A * due to JDK implementations will not return true.
1687N/A * ie a 'true' return is an indication of the treatment by
1687N/A * the implementation.
1687N/A * Whether supplementary characters should be considered is dependent
1687N/A * on the needs of the caller. Since this method accepts the 'char' type
1687N/A * then such chars are always represented by a pair. From a rendering
1687N/A * perspective these will all (in the cases I know of) still be one
1687N/A * unicode character -> one glyph. But if a caller is using this to
1687N/A * discover any case where it cannot make naive assumptions about
1687N/A * the number of chars, and how to index through them, then it may
1687N/A * need the option to have a 'true' return in such a case.
1687N/A */
1687N/A public static boolean isComplexText(char [] chs, int start, int limit) {
1687N/A
1687N/A for (int i = start; i < limit; i++) {
1687N/A if (chs[i] < MIN_LAYOUT_CHARCODE) {
1687N/A continue;
1687N/A }
1687N/A else if (isNonSimpleChar(chs[i])) {
1687N/A return true;
1687N/A }
1687N/A }
1687N/A return false;
1687N/A }
1687N/A
1687N/A /* This is almost the same as the method above, except it takes a
1687N/A * char which means it may include undecoded surrogate pairs.
1687N/A * The distinction is made so that code which needs to identify all
1687N/A * cases in which we do not have a simple mapping from
1687N/A * char->unicode character->glyph can be be identified.
1687N/A * For example measurement cannot simply sum advances of 'chars',
1687N/A * the caret in editable text cannot advance one 'char' at a time, etc.
1687N/A * These callers really are asking for more than whether 'layout'
1687N/A * needs to be run, they need to know if they can assume 1->1
1687N/A * char->glyph mapping.
1687N/A */
1687N/A public static boolean isNonSimpleChar(char ch) {
1687N/A return
1687N/A isComplexCharCode(ch) ||
1687N/A (ch >= CharToGlyphMapper.HI_SURROGATE_START &&
1687N/A ch <= CharToGlyphMapper.LO_SURROGATE_END);
1687N/A }
1687N/A
1687N/A /* If the character code falls into any of a number of unicode ranges
1687N/A * where we know that simple left->right layout mapping chars to glyphs
1687N/A * 1:1 and accumulating advances is going to produce incorrect results,
1687N/A * we want to know this so the caller can use a more intelligent layout
1687N/A * approach. A caller who cares about optimum performance may want to
1687N/A * check the first case and skip the method call if its in that range.
1687N/A * Although there's a lot of tests in here, knowing you can skip
1687N/A * CTL saves a great deal more. The rest of the checks are ordered
1687N/A * so that rather than checking explicitly if (>= start & <= end)
1687N/A * which would mean all ranges would need to be checked so be sure
1687N/A * CTL is not needed, the method returns as soon as it recognises
1687N/A * the code point is outside of a CTL ranges.
1687N/A * NOTE: Since this method accepts an 'int' it is asssumed to properly
1687N/A * represent a CHARACTER. ie it assumes the caller has already
1687N/A * converted surrogate pairs into supplementary characters, and so
1687N/A * can handle this case and doesn't need to be told such a case is
1687N/A * 'complex'.
1687N/A */
1687N/A public static boolean isComplexCharCode(int code) {
1687N/A
1687N/A if (code < MIN_LAYOUT_CHARCODE || code > MAX_LAYOUT_CHARCODE) {
1687N/A return false;
1687N/A }
1687N/A else if (code <= 0x036f) {
1687N/A // Trigger layout for combining diacriticals 0x0300->0x036f
1687N/A return true;
1687N/A }
1687N/A else if (code < 0x0590) {
1687N/A // No automatic layout for Greek, Cyrillic, Armenian.
1687N/A return false;
1687N/A }
1687N/A else if (code <= 0x06ff) {
1687N/A // Hebrew 0590 - 05ff
1687N/A // Arabic 0600 - 06ff
1687N/A return true;
1687N/A }
1687N/A else if (code < 0x0900) {
1687N/A return false; // Syriac and Thaana
1687N/A }
1687N/A else if (code <= 0x0e7f) {
1687N/A // if Indic, assume shaping for conjuncts, reordering:
1687N/A // 0900 - 097F Devanagari
1687N/A // 0980 - 09FF Bengali
1687N/A // 0A00 - 0A7F Gurmukhi
1687N/A // 0A80 - 0AFF Gujarati
1687N/A // 0B00 - 0B7F Oriya
1687N/A // 0B80 - 0BFF Tamil
1687N/A // 0C00 - 0C7F Telugu
1687N/A // 0C80 - 0CFF Kannada
1687N/A // 0D00 - 0D7F Malayalam
1687N/A // 0D80 - 0DFF Sinhala
1687N/A // 0E00 - 0E7F if Thai, assume shaping for vowel, tone marks
1687N/A return true;
1687N/A }
3171N/A else if (code < 0x0f00) {
3171N/A return false;
3171N/A }
3171N/A else if (code <= 0x0fff) { // U+0F00 - U+0FFF Tibetan
3171N/A return true;
3171N/A }
3171N/A else if (code < 0x1100) {
3171N/A return false;
3171N/A }
3171N/A else if (code < 0x11ff) { // U+1100 - U+11FF Old Hangul
3171N/A return true;
3171N/A }
1687N/A else if (code < 0x1780) {
1687N/A return false;
1687N/A }
1687N/A else if (code <= 0x17ff) { // 1780 - 17FF Khmer
1687N/A return true;
1687N/A }
1687N/A else if (code < 0x200c) {
1687N/A return false;
1687N/A }
1687N/A else if (code <= 0x200d) { // zwj or zwnj
1687N/A return true;
1687N/A }
1687N/A else if (code >= 0x202a && code <= 0x202e) { // directional control
1687N/A return true;
1687N/A }
1687N/A else if (code >= 0x206a && code <= 0x206f) { // directional control
1687N/A return true;
1687N/A }
1687N/A return false;
1687N/A }
1687N/A
1696N/A public static PlatformLogger getLogger() {
1687N/A return logger;
1687N/A }
1687N/A
1687N/A public static boolean isLogging() {
1687N/A return logging;
1687N/A }
1687N/A
1687N/A public static boolean debugFonts() {
1687N/A return debugFonts;
1687N/A }
1687N/A
1687N/A
1687N/A // The following methods are used by Swing.
1687N/A
1687N/A /* Revise the implementation to in fact mean "font is a composite font.
1687N/A * This ensures that Swing components will always benefit from the
1687N/A * fall back fonts
1687N/A */
1687N/A public static boolean fontSupportsDefaultEncoding(Font font) {
1687N/A return getFont2D(font) instanceof CompositeFont;
1687N/A }
1687N/A
1687N/A /**
1687N/A * This method is provided for internal and exclusive use by Swing.
1687N/A *
1687N/A * It may be used in conjunction with fontSupportsDefaultEncoding(Font)
1687N/A * In the event that a desktop properties font doesn't directly
1687N/A * support the default encoding, (ie because the host OS supports
1687N/A * adding support for the current locale automatically for native apps),
1687N/A * then Swing calls this method to get a font which uses the specified
1687N/A * font for the code points it covers, but also supports this locale
1687N/A * just as the standard composite fonts do.
1687N/A * Note: this will over-ride any setting where an application
1687N/A * specifies it prefers locale specific composite fonts.
1687N/A * The logic for this, is that this method is used only where the user or
1687N/A * application has specified that the native L&F be used, and that
1687N/A * we should honour that request to use the same font as native apps use.
1687N/A *
1687N/A * The behaviour of this method is to construct a new composite
1687N/A * Font object that uses the specified physical font as its first
1687N/A * component, and adds all the components of "dialog" as fall back
1687N/A * components.
1687N/A * The method currently assumes that only the size and style attributes
1687N/A * are set on the specified font. It doesn't copy the font transform or
1687N/A * other attributes because they aren't set on a font created from
1687N/A * the desktop. This will need to be fixed if use is broadened.
1687N/A *
1687N/A * Operations such as Font.deriveFont will work properly on the
1687N/A * font returned by this method for deriving a different point size.
1687N/A * Additionally it tries to support a different style by calling
1687N/A * getNewComposite() below. That also supports replacing slot zero
1687N/A * with a different physical font but that is expected to be "rare".
1687N/A * Deriving with a different style is needed because its been shown
1687N/A * that some applications try to do this for Swing FontUIResources.
1687N/A * Also operations such as new Font(font.getFontName(..), Font.PLAIN, 14);
1687N/A * will NOT yield the same result, as the new underlying CompositeFont
1687N/A * cannot be "looked up" in the font registry.
1687N/A * This returns a FontUIResource as that is the Font sub-class needed
1687N/A * by Swing.
1687N/A * Suggested usage is something like :
1687N/A * FontUIResource fuir;
1687N/A * Font desktopFont = getDesktopFont(..);
1687N/A * // NOTE even if fontSupportsDefaultEncoding returns true because
1687N/A * // you get Tahoma and are running in an English locale, you may
1687N/A * // still want to just call getCompositeFontUIResource() anyway
1687N/A * // as only then will you get fallback fonts - eg for CJK.
1687N/A * if (FontManager.fontSupportsDefaultEncoding(desktopFont)) {
1687N/A * fuir = new FontUIResource(..);
1687N/A * } else {
1687N/A * fuir = FontManager.getCompositeFontUIResource(desktopFont);
1687N/A * }
1687N/A * return fuir;
1687N/A */
3540N/A private static volatile
3540N/A SoftReference<ConcurrentHashMap<PhysicalFont, CompositeFont>>
3540N/A compMapRef = new SoftReference(null);
3540N/A
1687N/A public static FontUIResource getCompositeFontUIResource(Font font) {
1687N/A
2720N/A FontUIResource fuir = new FontUIResource(font);
1687N/A Font2D font2D = FontUtilities.getFont2D(font);
1687N/A
1687N/A if (!(font2D instanceof PhysicalFont)) {
1687N/A /* Swing should only be calling this when a font is obtained
1687N/A * from desktop properties, so should generally be a physical font,
1687N/A * an exception might be for names like "MS Serif" which are
1687N/A * automatically mapped to "Serif", so there's no need to do
1687N/A * anything special in that case. But note that suggested usage
1687N/A * is first to call fontSupportsDefaultEncoding(Font) and this
1687N/A * method should not be called if that were to return true.
1687N/A */
1687N/A return fuir;
1687N/A }
1687N/A
1687N/A FontManager fm = FontManagerFactory.getInstance();
4632N/A Font2D dialog = fm.findFont2D("dialog", font.getStyle(), FontManager.NO_FALLBACK);
4632N/A // Should never be null, but MACOSX fonts are not CompositeFonts
4632N/A if (dialog == null || !(dialog instanceof CompositeFont)) {
1687N/A return fuir;
1687N/A }
4632N/A CompositeFont dialog2D = (CompositeFont)dialog;
1687N/A PhysicalFont physicalFont = (PhysicalFont)font2D;
3540N/A ConcurrentHashMap<PhysicalFont, CompositeFont> compMap = compMapRef.get();
3540N/A if (compMap == null) { // Its been collected.
3540N/A compMap = new ConcurrentHashMap<PhysicalFont, CompositeFont>();
3540N/A compMapRef = new SoftReference(compMap);
3540N/A }
3540N/A CompositeFont compFont = compMap.get(physicalFont);
3540N/A if (compFont == null) {
3540N/A compFont = new CompositeFont(physicalFont, dialog2D);
3540N/A compMap.put(physicalFont, compFont);
3540N/A }
1687N/A FontAccess.getFontAccess().setFont2D(fuir, compFont.handle);
1687N/A /* marking this as a created font is needed as only created fonts
1687N/A * copy their creator's handles.
1687N/A */
1687N/A FontAccess.getFontAccess().setCreatedFont(fuir);
1687N/A return fuir;
1687N/A }
1687N/A
1687N/A /* A small "map" from GTK/fontconfig names to the equivalent JDK
1687N/A * logical font name.
1687N/A */
1687N/A private static final String[][] nameMap = {
1687N/A {"sans", "sansserif"},
1687N/A {"sans-serif", "sansserif"},
1687N/A {"serif", "serif"},
1687N/A {"monospace", "monospaced"}
1687N/A };
1687N/A
1687N/A public static String mapFcName(String name) {
1687N/A for (int i = 0; i < nameMap.length; i++) {
1687N/A if (name.equals(nameMap[i][0])) {
1687N/A return nameMap[i][1];
1687N/A }
1687N/A }
1687N/A return null;
1687N/A }
1687N/A
1687N/A
1687N/A /* This is called by Swing passing in a fontconfig family name
1687N/A * such as "sans". In return Swing gets a FontUIResource instance
1687N/A * that has queried fontconfig to resolve the font(s) used for this.
1687N/A * Fontconfig will if asked return a list of fonts to give the largest
1687N/A * possible code point coverage.
1687N/A * For now we use only the first font returned by fontconfig, and
1687N/A * back it up with the most closely matching JDK logical font.
1687N/A * Essentially this means pre-pending what we return now with fontconfig's
1687N/A * preferred physical font. This could lead to some duplication in cases,
1687N/A * if we already included that font later. We probably should remove such
1687N/A * duplicates, but it is not a significant problem. It can be addressed
1687N/A * later as part of creating a Composite which uses more of the
1687N/A * same fonts as fontconfig. At that time we also should pay more
1687N/A * attention to the special rendering instructions fontconfig returns,
1687N/A * such as whether we should prefer embedded bitmaps over antialiasing.
1687N/A * There's no way to express that via a Font at present.
1687N/A */
1687N/A public static FontUIResource getFontConfigFUIR(String fcFamily,
1687N/A int style, int size) {
1687N/A
1687N/A String mapped = mapFcName(fcFamily);
1687N/A if (mapped == null) {
1687N/A mapped = "sansserif";
1687N/A }
1687N/A
1687N/A FontUIResource fuir;
1687N/A FontManager fm = FontManagerFactory.getInstance();
1687N/A if (fm instanceof SunFontManager) {
1687N/A SunFontManager sfm = (SunFontManager) fm;
1687N/A fuir = sfm.getFontConfigFUIR(mapped, style, size);
1687N/A } else {
1687N/A fuir = new FontUIResource(mapped, style, size);
1687N/A }
1687N/A return fuir;
1687N/A }
1687N/A
1687N/A
1687N/A /**
1687N/A * Used by windows printing to assess if a font is likely to
1687N/A * be layout compatible with JDK
1687N/A * TrueType fonts should be, but if they have no GPOS table,
1687N/A * but do have a GSUB table, then they are probably older
1687N/A * fonts GDI handles differently.
1687N/A */
1687N/A public static boolean textLayoutIsCompatible(Font font) {
1687N/A
1687N/A Font2D font2D = getFont2D(font);
1687N/A if (font2D instanceof TrueTypeFont) {
1687N/A TrueTypeFont ttf = (TrueTypeFont) font2D;
1687N/A return
1687N/A ttf.getDirectoryEntry(TrueTypeFont.GSUBTag) == null ||
1687N/A ttf.getDirectoryEntry(TrueTypeFont.GPOSTag) != null;
1687N/A } else {
1687N/A return false;
1687N/A }
1687N/A }
1687N/A
1687N/A}