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