412 boolean renamed = tempFile.renameTo(fcInfoFile);
413 if (!renamed && FontUtilities.debugFonts()) {
414 System.out.println("rename failed");
415 warning("Failed renaming file to "+ getFcInfoFile());
416 }
417 } catch (Exception e) {
418 if (FontUtilities.debugFonts()) {
419 warning("IOException writing to "+ getFcInfoFile());
420 }
421 }
422 }
423
424 /* We want to be able to use this cache instead of invoking
425 * fontconfig except when we can detect the system cache has changed.
426 * But there doesn't seem to be a way to find the location of
427 * the system cache.
428 */
429 private void readFcInfo() {
430 File fcFile = getFcInfoFile();
431 if (!fcFile.exists()) {
432 return;
433 }
434 Properties props = new Properties();
435 FcFontManager fm = (FcFontManager) fontManager;
436 FontConfigManager fcm = fm.getFontConfigManager();
437 try {
438 FileInputStream fis = new FileInputStream(fcFile);
439 props.load(fis);
440 fis.close();
441 } catch (IOException e) {
442 if (FontUtilities.debugFonts()) {
443 warning("IOException reading from "+fcFile.toString());
444 }
445 return;
446 }
447 String version = (String)props.get("version");
448 if (version == null || !version.equals(fileVersion)) {
449 return;
450 }
451
452 // If there's a new, different fontconfig installed on the
453 // system, we invalidate our fontconfig file.
454 String fcVersionStr = (String)props.get("fcversion");
455 if (fcVersionStr != null) {
456 int fcVersion;
457 try {
458 fcVersion = Integer.parseInt(fcVersionStr);
459 if (fcVersion != 0 &&
460 fcVersion != FontConfigManager.getFontConfigVersion()) {
461 return;
462 }
463 } catch (Exception e) {
464 if (FontUtilities.debugFonts()) {
465 warning("Exception parsing version " + fcVersionStr);
466 }
467 return;
468 }
469 }
470
471 // If we can locate the fontconfig cache dirs, then compare the
472 // time stamp of those with our properties file. If we are out
473 // of date then re-generate.
474 long lastModified = fcFile.lastModified();
475 int cacheDirIndex = 0;
476 while (cacheDirIndex<4) { // should never be more than 2 anyway.
477 String dir = (String)props.get("cachedir."+cacheDirIndex);
478 if (dir == null) {
479 break;
480 }
481 File dirFile = new File(dir);
482 if (dirFile.exists() && dirFile.lastModified() > lastModified) {
483 return;
484 }
485 cacheDirIndex++;
486 }
487
488 String[] names = { "sansserif", "serif", "monospaced" };
489 String[] fcnames = { "sans", "serif", "monospace" };
490 int namesLen = names.length;
491 int numStyles = 4;
492 FcCompFont[] fci = new FcCompFont[namesLen*numStyles];
493
494 try {
495 for (int i=0; i<namesLen; i++) {
496 for (int s=0; s<numStyles; s++) {
497 int index = i*numStyles+s;
498 fci[index] = new FcCompFont();
499 String key = names[i]+"."+s;
500 fci[index].jdkName = names[i];
501 fci[index].fcFamily = fcnames[i];
502 fci[index].style = s;
503 String lenStr = (String)props.get(key+".length");
504 int nfonts = Integer.parseInt(lenStr);
505 if (nfonts <= 0) {
506 return; // bad file
507 }
508 fci[index].allFonts = new FontConfigFont[nfonts];
509 for (int f=0; f<nfonts; f++) {
510 fci[index].allFonts[f] = new FontConfigFont();
511 String fkey = key+"."+f+".fullName";
512 String fullName = (String)props.get(fkey);
513 fci[index].allFonts[f].fullName = fullName;
514 fkey = key+"."+f+".file";
515 String file = (String)props.get(fkey);
516 if (file == null) {
517 return; // bad file
518 }
519 fci[index].allFonts[f].fontFile = file;
520 }
521 fci[index].firstFont = fci[index].allFonts[0];
522
523 }
524 }
525 fcCompFonts = fci;
526 } catch (Throwable t) {
527 if (FontUtilities.debugFonts()) {
528 warning(t.toString());
529 }
530 }
531 }
532
533 private static void warning(String msg) {
534 PlatformLogger logger = PlatformLogger.getLogger("sun.awt.FontConfiguration");
535 logger.warning(msg);
536 }
537 }
|
412 boolean renamed = tempFile.renameTo(fcInfoFile);
413 if (!renamed && FontUtilities.debugFonts()) {
414 System.out.println("rename failed");
415 warning("Failed renaming file to "+ getFcInfoFile());
416 }
417 } catch (Exception e) {
418 if (FontUtilities.debugFonts()) {
419 warning("IOException writing to "+ getFcInfoFile());
420 }
421 }
422 }
423
424 /* We want to be able to use this cache instead of invoking
425 * fontconfig except when we can detect the system cache has changed.
426 * But there doesn't seem to be a way to find the location of
427 * the system cache.
428 */
429 private void readFcInfo() {
430 File fcFile = getFcInfoFile();
431 if (!fcFile.exists()) {
432 if (FontUtilities.debugFonts()) {
433 warning("fontconfig info file " + fcFile.toString() + " does not exist");
434 }
435 return;
436 }
437 Properties props = new Properties();
438 FcFontManager fm = (FcFontManager) fontManager;
439 FontConfigManager fcm = fm.getFontConfigManager();
440 try {
441 FileInputStream fis = new FileInputStream(fcFile);
442 props.load(fis);
443 fis.close();
444 } catch (IOException e) {
445 if (FontUtilities.debugFonts()) {
446 warning("IOException reading from "+fcFile.toString());
447 }
448 return;
449 }
450 String version = (String)props.get("version");
451 if (version == null || !version.equals(fileVersion)) {
452 if (FontUtilities.debugFonts()) {
453 warning("fontconfig info file caused a version mismatch");
454 }
455 return;
456 }
457
458 // If there's a new, different fontconfig installed on the
459 // system, we invalidate our fontconfig file.
460 String fcVersionStr = (String)props.get("fcversion");
461 if (fcVersionStr != null) {
462 int fcVersion;
463 try {
464 fcVersion = Integer.parseInt(fcVersionStr);
465 if (fcVersion != 0 &&
466 fcVersion != FontConfigManager.getFontConfigVersion()) {
467 if (FontUtilities.debugFonts()) {
468 warning("new, different fontconfig detected");
469 }
470 return;
471 }
472 } catch (Exception e) {
473 if (FontUtilities.debugFonts()) {
474 warning("Exception parsing version " + fcVersionStr);
475 }
476 return;
477 }
478 }
479
480 // If we can locate the fontconfig cache dirs, then compare the
481 // time stamp of those with our properties file. If we are out
482 // of date then re-generate.
483 long lastModified = fcFile.lastModified();
484 int cacheDirIndex = 0;
485 while (cacheDirIndex<4) { // should never be more than 2 anyway.
486 String dir = (String)props.get("cachedir."+cacheDirIndex);
487 if (dir == null) {
488 break;
489 }
490 File dirFile = new File(dir);
491 if (dirFile.exists() && dirFile.lastModified() > lastModified) {
492 if (FontUtilities.debugFonts()) {
493 warning("Out of date cache directories detected");
494 }
495 return;
496 }
497 cacheDirIndex++;
498 }
499
500 String[] names = { "sansserif", "serif", "monospaced" };
501 String[] fcnames = { "sans", "serif", "monospace" };
502 int namesLen = names.length;
503 int numStyles = 4;
504 FcCompFont[] fci = new FcCompFont[namesLen*numStyles];
505
506 try {
507 for (int i=0; i<namesLen; i++) {
508 for (int s=0; s<numStyles; s++) {
509 int index = i*numStyles+s;
510 fci[index] = new FcCompFont();
511 String key = names[i]+"."+s;
512 fci[index].jdkName = names[i];
513 fci[index].fcFamily = fcnames[i];
514 fci[index].style = s;
515 String lenStr = (String)props.get(key+".length");
516 int nfonts = Integer.parseInt(lenStr);
517 if (nfonts <= 0) {
518 if (FontUtilities.debugFonts()) {
519 warning("Bad non-positive .length entry in fontconfig file " + fcFile.toString());
520 }
521 return; // bad file
522 }
523 fci[index].allFonts = new FontConfigFont[nfonts];
524 for (int f=0; f<nfonts; f++) {
525 fci[index].allFonts[f] = new FontConfigFont();
526 String fkey = key+"."+f+".fullName";
527 String fullName = (String)props.get(fkey);
528 fci[index].allFonts[f].fullName = fullName;
529 fkey = key+"."+f+".file";
530 String file = (String)props.get(fkey);
531 if (file == null) {
532 if (FontUtilities.debugFonts()) {
533 warning("Missing file value for key " + fkey + " in fontconfig file " + fcFile.toString());
534 }
535 return; // bad file
536 }
537 fci[index].allFonts[f].fontFile = file;
538 }
539 fci[index].firstFont = fci[index].allFonts[0];
540
541 }
542 }
543 fcCompFonts = fci;
544 } catch (Throwable t) {
545 if (FontUtilities.debugFonts()) {
546 warning(t.toString());
547 }
548 }
549
550 if (FontUtilities.debugFonts()) {
551 PlatformLogger logger = FontUtilities.getLogger();
552 logger.info("successfully parsed the fontconfig file at " + fcFile.toString());
553 }
554 }
555
556 private static void warning(String msg) {
557 PlatformLogger logger = PlatformLogger.getLogger("sun.awt.FontConfiguration");
558 logger.warning(msg);
559 }
560 }
|