1 /* 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package sun.font; 27 28 import java.util.Locale; 29 import java.util.logging.Level; 30 import java.util.logging.Logger; 31 32 import sun.awt.SunHints; 33 import sun.awt.SunToolkit; 34 35 /** 36 * Small utility class to manage FontConfig. 37 */ 38 public class FontConfigManager { 39 40 static boolean fontConfigFailed = false; 41 42 /* This is populated by native */ 43 private static final FontConfigInfo fcInfo = new FontConfigInfo(); 44 45 /* Begin support for GTK Look and Feel - query libfontconfig and 46 * return a composite Font to Swing that uses the desktop font(s). 47 */ 48 49 /* These next three classes are just data structures. 50 */ 51 public static class FontConfigFont { 52 public String familyName; // eg Bitstream Vera Sans 53 public String styleStr; // eg Bold 54 public String fullName; // eg Bitstream Vera Sans Bold 55 public String fontFile; // eg /usr/X11/lib/fonts/foo.ttf 56 } 57 58 public static class FcCompFont { 59 public String fcName; // eg sans 60 public String fcFamily; // eg sans 61 public String jdkName; // eg sansserif 62 public int style; // eg 0=PLAIN 63 public FontConfigFont firstFont; 64 public FontConfigFont[] allFonts; 65 //boolean preferBitmaps; // if embedded bitmaps preferred over AA 66 public CompositeFont compFont; // null if not yet created/known. 67 } 68 69 public static class FontConfigInfo { 70 public int fcVersion; 71 public String[] cacheDirs = new String[4]; 72 } 73 74 /* fontconfig recognises slants roman, italic, as well as oblique, 75 * and a slew of weights, where the ones that matter here are 76 * regular and bold. 77 * To fully qualify what we want, we can for example ask for (eg) 78 * Font.PLAIN : "serif:regular:roman" 79 * Font.BOLD : "serif:bold:roman" 80 * Font.ITALIC : "serif:regular:italic" 81 * Font.BOLD|Font.ITALIC : "serif:bold:italic" 82 */ 83 private static String[] fontConfigNames = { 84 "sans:regular:roman", 85 "sans:bold:roman", 86 "sans:regular:italic", 87 "sans:bold:italic", 88 89 "serif:regular:roman", 90 "serif:bold:roman", 91 "serif:regular:italic", 92 "serif:bold:italic", 93 94 "monospace:regular:roman", 95 "monospace:bold:roman", 96 "monospace:regular:italic", 97 "monospace:bold:italic", 98 }; 99 100 /* This array has the array elements created in Java code and is 101 * passed down to native to be filled in. 102 */ 103 private FcCompFont[] fontConfigFonts; 104 105 /** 106 * Instantiates a new FontConfigManager getting the default instance 107 * of FontManager from the FontManagerFactory. 108 */ 109 public FontConfigManager() { 110 } 111 112 public static String[] getFontConfigNames() { 113 return fontConfigNames; 114 } 115 116 /* Called from code that needs to know what are the AA settings 117 * that apps using FC would pick up for the default desktop font. 118 * Note apps can change the default desktop font. etc, so this 119 * isn't certain to be right but its going to correct for most cases. 120 * Native return values map to the text aa values in sun.awt.SunHints. 121 * which is used to look up the renderinghint value object. 122 */ 123 public static Object getFontConfigAAHint() { 124 return getFontConfigAAHint("sans"); 125 } 126 127 /* This is public solely so that for debugging purposes it can be called 128 * with other names, which might (eg) include a size, eg "sans-24" 129 * The return value is a text aa rendering hint value. 130 * Normally we should call the no-args version. 131 */ 132 public static Object getFontConfigAAHint(String fcFamily) { 133 if (FontUtilities.isWindows) { 134 return null; 135 } else { 136 int hint = getFontConfigAASettings(getFCLocaleStr(), fcFamily); 137 if (hint < 0) { 138 return null; 139 } else { 140 return SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING, 141 hint); 142 } 143 } 144 } 145 146 147 private static String getFCLocaleStr() { 148 Locale l = SunToolkit.getStartupLocale(); 149 String localeStr = l.getLanguage(); 150 String country = l.getCountry(); 151 if (!country.equals("")) { 152 localeStr = localeStr + "-" + country; 153 } 154 return localeStr; 155 } 156 157 /* This does cause the native libfontconfig to be loaded and unloaded, 158 * but it does not incur the overhead of initialisation of its 159 * data structures, so shouldn't have a measurable impact. 160 */ 161 public static native int getFontConfigVersion(); 162 163 /* This can be made public if it's needed to force a re-read 164 * rather than using the cached values. The re-read would be needed 165 * only if some event signalled that the fontconfig has changed. 166 * In that event this method would need to return directly the array 167 * to be used by the caller in case it subsequently changed. 168 */ 169 public synchronized void initFontConfigFonts(boolean includeFallbacks) { 170 171 if (fontConfigFonts != null) { 172 if (!includeFallbacks || (fontConfigFonts[0].allFonts != null)) { 173 return; 174 } 175 } 176 177 if (FontUtilities.isWindows || fontConfigFailed) { 178 return; 179 } 180 181 long t0 = 0; 182 if (FontUtilities.isLogging()) { 183 t0 = System.nanoTime(); 184 } 185 186 String[] fontConfigNames = FontConfigManager.getFontConfigNames(); 187 FcCompFont[] fontArr = new FcCompFont[fontConfigNames.length]; 188 189 for (int i = 0; i< fontArr.length; i++) { 190 fontArr[i] = new FcCompFont(); 191 fontArr[i].fcName = fontConfigNames[i]; 192 int colonPos = fontArr[i].fcName.indexOf(':'); 193 fontArr[i].fcFamily = fontArr[i].fcName.substring(0, colonPos); 194 fontArr[i].jdkName = FontConfigManager.mapFcName(fontArr[i].fcFamily); 195 fontArr[i].style = i % 4; // depends on array order. 196 } 197 getFontConfig(getFCLocaleStr(), fcInfo, fontArr, includeFallbacks); 198 /* If don't find anything (eg no libfontconfig), then just return */ 199 for (int i = 0; i< fontArr.length; i++) { 200 FcCompFont fci = fontArr[i]; 201 if (fci.firstFont == null) { 202 if (FontUtilities.isLogging()) { 203 Logger logger = FontUtilities.getLogger(); 204 logger.info("Fontconfig returned no fonts."); 205 } 206 fontConfigFailed = true; 207 return; 208 } 209 } 210 fontConfigFonts = fontArr; 211 212 if (FontUtilities.isLogging()) { 213 214 Logger logger = FontUtilities.getLogger(); 215 216 long t1 = System.nanoTime(); 217 logger.info("Time spent accessing fontconfig=" 218 + ((t1 - t0) / 1000000) + "ms."); 219 220 for (int i = 0; i< fontConfigFonts.length; i++) { 221 FcCompFont fci = fontConfigFonts[i]; 222 logger.info("FC font " + fci.fcName+" maps to family " + 223 fci.firstFont.familyName + 224 " in file " + fci.firstFont.fontFile); 225 if (fci.allFonts != null) { 226 for (int f=0;f<fci.allFonts.length;f++) { 227 FontConfigFont fcf = fci.allFonts[f]; 228 logger.info("Family=" + fcf.familyName + 229 " Style="+ fcf.styleStr + 230 " Fullname="+fcf.fullName + 231 " File="+fcf.fontFile); 232 } 233 } 234 } 235 } 236 } 237 238 public PhysicalFont registerFromFcInfo(FcCompFont fcInfo) { 239 240 SunFontManager fm = SunFontManager.getInstance(); 241 242 /* If it's a TTC file we need to know that as we will need to 243 * make sure we return the right font */ 244 String fontFile = fcInfo.firstFont.fontFile; 245 int offset = fontFile.length()-4; 246 if (offset <= 0) { 247 return null; 248 } 249 String ext = fontFile.substring(offset).toLowerCase(); 250 boolean isTTC = ext.equals(".ttc"); 251 252 /* If this file is already registered, can just return its font. 253 * However we do need to check in case it's a TTC as we need 254 * a specific font, so rather than directly returning it, let 255 * findFont2D resolve that. 256 */ 257 PhysicalFont physFont = fm.getRegisteredFontFile(fontFile); 258 if (physFont != null) { 259 if (isTTC) { 260 Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName, 261 fcInfo.style, 262 FontManager.NO_FALLBACK); 263 if (f2d instanceof PhysicalFont) { /* paranoia */ 264 return (PhysicalFont)f2d; 265 } else { 266 return null; 267 } 268 } else { 269 return physFont; 270 } 271 } 272 273 /* If the font may hide a JRE font (eg fontconfig says it is 274 * Lucida Sans), we want to use the JRE version, so make it 275 * point to the JRE font. 276 */ 277 physFont = fm.findJREDeferredFont(fcInfo.firstFont.familyName, 278 fcInfo.style); 279 280 /* It is also possible the font file is on the "deferred" list, 281 * in which case we can just initialise it now. 282 */ 283 if (physFont == null && 284 fm.isDeferredFont(fontFile) == true) { 285 physFont = fm.initialiseDeferredFont(fcInfo.firstFont.fontFile); 286 /* use findFont2D to get the right font from TTC's */ 287 if (physFont != null) { 288 if (isTTC) { 289 Font2D f2d = fm.findFont2D(fcInfo.firstFont.familyName, 290 fcInfo.style, 291 FontManager.NO_FALLBACK); 292 if (f2d instanceof PhysicalFont) { /* paranoia */ 293 return (PhysicalFont)f2d; 294 } else { 295 return null; 296 } 297 } else { 298 return physFont; 299 } 300 } 301 } 302 303 /* In the majority of cases we reach here, and need to determine 304 * the type and rank to register the font. 305 */ 306 if (physFont == null) { 307 int fontFormat = SunFontManager.FONTFORMAT_NONE; 308 int fontRank = Font2D.UNKNOWN_RANK; 309 310 if (ext.equals(".ttf") || isTTC) { 311 fontFormat = SunFontManager.FONTFORMAT_TRUETYPE; 312 fontRank = Font2D.TTF_RANK; 313 } else if (ext.equals(".pfa") || ext.equals(".pfb")) { 314 fontFormat = SunFontManager.FONTFORMAT_TYPE1; 315 fontRank = Font2D.TYPE1_RANK; 316 } 317 physFont = fm.registerFontFile(fcInfo.firstFont.fontFile, null, 318 fontFormat, true, fontRank); 319 } 320 return physFont; 321 } 322 323 /* 324 * We need to return a Composite font which has as the font in 325 * its first slot one obtained from fontconfig. 326 */ 327 public CompositeFont getFontConfigFont(String name, int style) { 328 329 name = name.toLowerCase(); 330 331 initFontConfigFonts(false); 332 333 FcCompFont fcInfo = null; 334 for (int i=0; i<fontConfigFonts.length; i++) { 335 if (name.equals(fontConfigFonts[i].fcFamily) && 336 style == fontConfigFonts[i].style) { 337 fcInfo = fontConfigFonts[i]; 338 break; 339 } 340 } 341 if (fcInfo == null) { 342 fcInfo = fontConfigFonts[0]; 343 } 344 345 if (FontUtilities.isLogging()) { 346 FontUtilities.getLogger() 347 .info("FC name=" + name + " style=" + style + 348 " uses " + fcInfo.firstFont.familyName + 349 " in file: " + fcInfo.firstFont.fontFile); 350 } 351 352 if (fcInfo.compFont != null) { 353 return fcInfo.compFont; 354 } 355 356 /* jdkFont is going to be used for slots 1..N and as a fallback. 357 * Slot 0 will be the physical font from fontconfig. 358 */ 359 CompositeFont jdkFont = (CompositeFont) 360 fm.findFont2D(fcInfo.jdkName, style, FontManager.LOGICAL_FALLBACK); 361 362 if (fcInfo.firstFont.familyName == null || 363 fcInfo.firstFont.fontFile == null) { 364 return (fcInfo.compFont = jdkFont); 365 } 366 367 /* First, see if the family and exact style is already registered. 368 * If it is, use it. If it's not, then try to register it. 369 * If that registration fails (signalled by null) just return the 370 * regular JDK composite. 371 * Algorithmically styled fonts won't match on exact style, so 372 * will fall through this code, but the regisration code will 373 * find that file already registered and return its font. 374 */ 375 FontFamily family = FontFamily.getFamily(fcInfo.firstFont.familyName); 376 PhysicalFont physFont = null; 377 if (family != null) { 378 Font2D f2D = family.getFontWithExactStyleMatch(fcInfo.style); 379 if (f2D instanceof PhysicalFont) { 380 physFont = (PhysicalFont)f2D; 381 } 382 } 383 384 if (physFont == null || 385 !fcInfo.firstFont.fontFile.equals(physFont.platName)) { 386 physFont = registerFromFcInfo(fcInfo); 387 if (physFont == null) { 388 return (fcInfo.compFont = jdkFont); 389 } 390 family = FontFamily.getFamily(physFont.getFamilyName(null)); 391 } 392 393 /* Now register the fonts in the family (the other styles) after 394 * checking that they aren't already registered and are actually in 395 * a different file. They may be the same file in CJK cases. 396 * For cases where they are different font files - eg as is common for 397 * Latin fonts, then we rely on fontconfig to report these correctly. 398 * Assume that all styles of this font are found by fontconfig, 399 * so we can find all the family members which must be registered 400 * together to prevent synthetic styling. 401 */ 402 for (int i=0; i<fontConfigFonts.length; i++) { 403 FcCompFont fc = fontConfigFonts[i]; 404 if (fc != fcInfo && 405 physFont.getFamilyName(null).equals(fc.firstFont.familyName) && 406 !fc.firstFont.fontFile.equals(physFont.platName) && 407 family.getFontWithExactStyleMatch(fc.style) == null) { 408 409 registerFromFcInfo(fontConfigFonts[i]); 410 } 411 } 412 413 /* Now we have a physical font. We will back this up with the JDK 414 * logical font (sansserif, serif, or monospaced) that corresponds 415 * to the Pango/GTK/FC logical font name. 416 */ 417 return (fcInfo.compFont = new CompositeFont(physFont, jdkFont)); 418 } 419 420 /** 421 * 422 * @param locale 423 * @param fcFamily 424 * @return 425 */ 426 public FcCompFont[] getFontConfigFonts() { 427 return fontConfigFonts; 428 } 429 430 /* Return an array of FcCompFont structs describing the primary 431 * font located for each of fontconfig/GTK/Pango's logical font names. 432 */ 433 private static native void getFontConfig(String locale, 434 FontConfigInfo fcInfo, 435 FcCompFont[] fonts, 436 boolean includeFallbacks); 437 438 void populateFontConfig(FcCompFont[] fcInfo) { 439 fontConfigFonts = fcInfo; 440 } 441 442 FcCompFont[] loadFontConfig() { 443 initFontConfigFonts(true); 444 return fontConfigFonts; 445 } 446 447 FontConfigInfo getFontConfigInfo() { 448 initFontConfigFonts(true); 449 return fcInfo; 450 } 451 452 private static native int 453 getFontConfigAASettings(String locale, String fcFamily); 454 }