< prev index next >

src/java.desktop/unix/classes/sun/font/FcFontConfiguration.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2019, 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.awt.Font;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.net.InetAddress;
  34 import java.net.UnknownHostException;
  35 import java.nio.charset.Charset;
  36 import java.nio.charset.StandardCharsets;
  37 import java.nio.file.Files;
  38 import java.util.HashMap;
  39 import java.util.HashSet;
  40 import java.util.Locale;
  41 import java.util.Properties;
  42 import java.util.Scanner;
  43 import sun.awt.FcFontManager;
  44 import sun.awt.FontConfiguration;
  45 import sun.awt.FontDescriptor;
  46 import sun.awt.SunToolkit;
  47 import sun.font.CompositeFontDescriptor;
  48 import sun.font.FontManager;
  49 import sun.font.FontConfigManager.FontConfigInfo;
  50 import sun.font.FontConfigManager.FcCompFont;
  51 import sun.font.FontConfigManager.FontConfigFont;
  52 import sun.java2d.SunGraphicsEnvironment;
  53 import sun.util.logging.PlatformLogger;
  54 
  55 public class FcFontConfiguration extends FontConfiguration {
  56 
  57     /** Version of the cache file format understood by this code.
  58      * Its part of the file name so that we can rev this at
  59      * any time, even in a minor JDK update.
  60      * It is stored as the value of the "version" property.


 248 
 249             for (int styleIndex = 0; styleIndex < NUM_STYLES; styleIndex++) {
 250 
 251                 String faceName = fontName + "." + styleNames[styleIndex];
 252                 FontConfigFont[] fcFonts =
 253                     getFcFontList(fcCompFonts,
 254                                   fontNames[fontIndex], styleIndex);
 255 
 256                 int numFonts = fcFonts.length;
 257                 // fall back fonts listed in the lib/fonts/fallback directory
 258                 if (installedFallbackFontFiles != null) {
 259                     numFonts += installedFallbackFontFiles.length;
 260                 }
 261 
 262                 String[] fileNames = new String[numFonts];
 263                 String[] faceNames = new String[numFonts];
 264 
 265                 int index;
 266                 for (index = 0; index < fcFonts.length; index++) {
 267                     fileNames[index] = fcFonts[index].fontFile;
 268                     faceNames[index] = fcFonts[index].fullName;
 269                 }
 270 
 271                 if (installedFallbackFontFiles != null) {
 272                     System.arraycopy(installedFallbackFontFiles, 0,
 273                                      fileNames, fcFonts.length,
 274                                      installedFallbackFontFiles.length);
 275                 }
 276 
 277                 result[fontIndex * NUM_STYLES + styleIndex]
 278                         = new CompositeFontDescriptor(
 279                             faceName,
 280                             1,
 281                             faceNames,
 282                             fileNames,
 283                             null, null);
 284             }
 285         }
 286         return result;
 287     }
 288 


 341         }
 342     }
 343 
 344     private File getFcInfoFile() {
 345         if (fcInfoFileName == null) {
 346             // NB need security permissions to get true IP address, and
 347             // we should have those as the whole initialisation is in a
 348             // doPrivileged block. But in this case no exception is thrown,
 349             // and it returns the loop back address, and so we end up with
 350             // "localhost"
 351             String hostname;
 352             try {
 353                 hostname = InetAddress.getLocalHost().getHostName();
 354             } catch (UnknownHostException e) {
 355                 hostname = "localhost";
 356             }
 357             String userDir = System.getProperty("user.home");
 358             String version = System.getProperty("java.version");
 359             String fs = File.separator;
 360             String dir = userDir+fs+".java"+fs+"fonts"+fs+version;
 361             Locale locale = SunToolkit.getStartupLocale();
 362             String lang = locale.getLanguage();
 363             String country = locale.getCountry();
 364             String name = "fcinfo-"+fileVersion+"-"+hostname+"-"+
 365                 osName+"-"+osVersion+"-"+lang+"-"+country+".properties";
 366             fcInfoFileName = dir+fs+name;
 367         }
 368         return new File(fcInfoFileName);
 369     }
 370 
 371     private void writeFcInfo() {
 372         Properties props = new Properties();
 373         props.setProperty("version", fileVersion);
 374         FcFontManager fm = (FcFontManager) fontManager;
 375         FontConfigManager fcm = fm.getFontConfigManager();
 376         FontConfigInfo fcInfo = fcm.getFontConfigInfo();
 377         props.setProperty("fcversion", Integer.toString(fcInfo.fcVersion));
 378         if (fcInfo.cacheDirs != null) {
 379             for (int i=0;i<fcInfo.cacheDirs.length;i++) {
 380                 if (fcInfo.cacheDirs[i] != null) {
 381                    props.setProperty("cachedir."+i,  fcInfo.cacheDirs[i]);
 382                 }
 383             }
 384         }
 385         for (int i=0; i<fcCompFonts.length; i++) {
 386             FcCompFont fci = fcCompFonts[i];
 387             String styleKey = fci.jdkName+"."+fci.style;
 388             props.setProperty(styleKey+".length",
 389                               Integer.toString(fci.allFonts.length));
 390             for (int j=0; j<fci.allFonts.length; j++) {


 391                 props.setProperty(styleKey+"."+j+".file",
 392                                   fci.allFonts[j].fontFile);
 393                 if (fci.allFonts[j].fullName != null) {
 394                     props.setProperty(styleKey+"."+j+".fullName",
 395                                       fci.allFonts[j].fullName);
 396                 }
 397             }
 398         }
 399         try {
 400             /* This writes into a temp file then renames when done.
 401              * Since the rename is an atomic action within the same
 402              * directory no client will ever see a partially written file.
 403              */
 404             File fcInfoFile = getFcInfoFile();
 405             File dir = fcInfoFile.getParentFile();
 406             dir.mkdirs();
 407             File tempFile = Files.createTempFile(dir.toPath(), "fcinfo", null).toFile();
 408             FileOutputStream fos = new FileOutputStream(tempFile);
 409             props.store(fos,
 410                       "JDK Font Configuration Generated File: *Do Not Edit*");
 411             fos.close();
 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             }


 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) {
   1 /*
   2  * Copyright (c) 2008, 2014, 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.awt.Font;
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.net.InetAddress;
  34 import java.net.UnknownHostException;
  35 import java.nio.charset.Charset;
  36 import java.nio.charset.StandardCharsets;
  37 import java.nio.file.Files;
  38 import java.util.HashMap;
  39 import java.util.HashSet;

  40 import java.util.Properties;
  41 import java.util.Scanner;
  42 import sun.awt.FcFontManager;
  43 import sun.awt.FontConfiguration;
  44 import sun.awt.FontDescriptor;
  45 import sun.awt.SunToolkit;
  46 import sun.font.CompositeFontDescriptor;
  47 import sun.font.FontManager;
  48 import sun.font.FontConfigManager.FontConfigInfo;
  49 import sun.font.FontConfigManager.FcCompFont;
  50 import sun.font.FontConfigManager.FontConfigFont;
  51 import sun.java2d.SunGraphicsEnvironment;
  52 import sun.util.logging.PlatformLogger;
  53 
  54 public class FcFontConfiguration extends FontConfiguration {
  55 
  56     /** Version of the cache file format understood by this code.
  57      * Its part of the file name so that we can rev this at
  58      * any time, even in a minor JDK update.
  59      * It is stored as the value of the "version" property.


 247 
 248             for (int styleIndex = 0; styleIndex < NUM_STYLES; styleIndex++) {
 249 
 250                 String faceName = fontName + "." + styleNames[styleIndex];
 251                 FontConfigFont[] fcFonts =
 252                     getFcFontList(fcCompFonts,
 253                                   fontNames[fontIndex], styleIndex);
 254 
 255                 int numFonts = fcFonts.length;
 256                 // fall back fonts listed in the lib/fonts/fallback directory
 257                 if (installedFallbackFontFiles != null) {
 258                     numFonts += installedFallbackFontFiles.length;
 259                 }
 260 
 261                 String[] fileNames = new String[numFonts];
 262                 String[] faceNames = new String[numFonts];
 263 
 264                 int index;
 265                 for (index = 0; index < fcFonts.length; index++) {
 266                     fileNames[index] = fcFonts[index].fontFile;
 267                     faceNames[index] = fcFonts[index].familyName;
 268                 }
 269 
 270                 if (installedFallbackFontFiles != null) {
 271                     System.arraycopy(installedFallbackFontFiles, 0,
 272                                      fileNames, fcFonts.length,
 273                                      installedFallbackFontFiles.length);
 274                 }
 275 
 276                 result[fontIndex * NUM_STYLES + styleIndex]
 277                         = new CompositeFontDescriptor(
 278                             faceName,
 279                             1,
 280                             faceNames,
 281                             fileNames,
 282                             null, null);
 283             }
 284         }
 285         return result;
 286     }
 287 


 340         }
 341     }
 342 
 343     private File getFcInfoFile() {
 344         if (fcInfoFileName == null) {
 345             // NB need security permissions to get true IP address, and
 346             // we should have those as the whole initialisation is in a
 347             // doPrivileged block. But in this case no exception is thrown,
 348             // and it returns the loop back address, and so we end up with
 349             // "localhost"
 350             String hostname;
 351             try {
 352                 hostname = InetAddress.getLocalHost().getHostName();
 353             } catch (UnknownHostException e) {
 354                 hostname = "localhost";
 355             }
 356             String userDir = System.getProperty("user.home");
 357             String version = System.getProperty("java.version");
 358             String fs = File.separator;
 359             String dir = userDir+fs+".java"+fs+"fonts"+fs+version;
 360             String lang = SunToolkit.getStartupLocale().getLanguage();


 361             String name = "fcinfo-"+fileVersion+"-"+hostname+"-"+
 362                 osName+"-"+osVersion+"-"+lang+".properties";
 363             fcInfoFileName = dir+fs+name;
 364         }
 365         return new File(fcInfoFileName);
 366     }
 367 
 368     private void writeFcInfo() {
 369         Properties props = new Properties();
 370         props.setProperty("version", fileVersion);
 371         FcFontManager fm = (FcFontManager) fontManager;
 372         FontConfigManager fcm = fm.getFontConfigManager();
 373         FontConfigInfo fcInfo = fcm.getFontConfigInfo();
 374         props.setProperty("fcversion", Integer.toString(fcInfo.fcVersion));
 375         if (fcInfo.cacheDirs != null) {
 376             for (int i=0;i<fcInfo.cacheDirs.length;i++) {
 377                 if (fcInfo.cacheDirs[i] != null) {
 378                    props.setProperty("cachedir."+i,  fcInfo.cacheDirs[i]);
 379                 }
 380             }
 381         }
 382         for (int i=0; i<fcCompFonts.length; i++) {
 383             FcCompFont fci = fcCompFonts[i];
 384             String styleKey = fci.jdkName+"."+fci.style;
 385             props.setProperty(styleKey+".length",
 386                               Integer.toString(fci.allFonts.length));
 387             for (int j=0; j<fci.allFonts.length; j++) {
 388                 props.setProperty(styleKey+"."+j+".family",
 389                                   fci.allFonts[j].familyName);
 390                 props.setProperty(styleKey+"."+j+".file",
 391                                   fci.allFonts[j].fontFile);




 392             }
 393         }
 394         try {
 395             /* This writes into a temp file then renames when done.
 396              * Since the rename is an atomic action within the same
 397              * directory no client will ever see a partially written file.
 398              */
 399             File fcInfoFile = getFcInfoFile();
 400             File dir = fcInfoFile.getParentFile();
 401             dir.mkdirs();
 402             File tempFile = Files.createTempFile(dir.toPath(), "fcinfo", null).toFile();
 403             FileOutputStream fos = new FileOutputStream(tempFile);
 404             props.store(fos,
 405                       "JDK Font Configuration Generated File: *Do Not Edit*");
 406             fos.close();
 407             boolean renamed = tempFile.renameTo(fcInfoFile);
 408             if (!renamed && FontUtilities.debugFonts()) {
 409                 System.out.println("rename failed");
 410                 warning("Failed renaming file to "+ getFcInfoFile());
 411             }


 486         int numStyles = 4;
 487         FcCompFont[] fci = new FcCompFont[namesLen*numStyles];
 488 
 489         try {
 490             for (int i=0; i<namesLen; i++) {
 491                 for (int s=0; s<numStyles; s++) {
 492                     int index = i*numStyles+s;
 493                     fci[index] = new FcCompFont();
 494                     String key = names[i]+"."+s;
 495                     fci[index].jdkName = names[i];
 496                     fci[index].fcFamily = fcnames[i];
 497                     fci[index].style = s;
 498                     String lenStr = (String)props.get(key+".length");
 499                     int nfonts = Integer.parseInt(lenStr);
 500                     if (nfonts <= 0) {
 501                         return; // bad file
 502                     }
 503                     fci[index].allFonts = new FontConfigFont[nfonts];
 504                     for (int f=0; f<nfonts; f++) {
 505                         fci[index].allFonts[f] = new FontConfigFont();
 506                         String fkey = key+"."+f+".family";
 507                         String family = (String)props.get(fkey);
 508                         fci[index].allFonts[f].familyName = family;
 509                         fkey = key+"."+f+".file";
 510                         String file = (String)props.get(fkey);
 511                         if (file == null) {
 512                             return; // bad file
 513                         }
 514                         fci[index].allFonts[f].fontFile = file;
 515                     }
 516                     fci[index].firstFont =  fci[index].allFonts[0];
 517 
 518                 }
 519             }
 520             fcCompFonts = fci;
 521         } catch (Throwable t) {
 522             if (FontUtilities.debugFonts()) {
 523                 warning(t.toString());
 524             }
 525         }
 526     }
 527 
 528     private static void warning(String msg) {
< prev index next >