Path: blob/master/src/java.desktop/unix/classes/sun/font/MFontConfiguration.java
66646 views
/*1* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.font;2627import sun.awt.FontConfiguration;28import sun.awt.X11FontManager;29import sun.font.FontUtilities;30import sun.font.SunFontManager;31import sun.util.logging.PlatformLogger;3233import java.io.File;34import java.io.FileInputStream;35import java.nio.charset.Charset;36import java.util.HashMap;37import java.util.HashSet;38import java.util.Properties;39import java.util.Scanner;4041public class MFontConfiguration extends FontConfiguration {4243private static FontConfiguration fontConfig = null;44private static PlatformLogger logger;4546public MFontConfiguration(SunFontManager fm) {47super(fm);48if (FontUtilities.debugFonts()) {49logger = PlatformLogger.getLogger("sun.awt.FontConfiguration");50}51initTables();52}535455public MFontConfiguration(SunFontManager fm,56boolean preferLocaleFonts,57boolean preferPropFonts) {58super(fm, preferLocaleFonts, preferPropFonts);59if (FontUtilities.debugFonts()) {60logger = PlatformLogger.getLogger("sun.awt.FontConfiguration");61}62initTables();63}6465/* Needs to be kept in sync with updates in the languages used in66* the fontconfig files.67*/68protected void initReorderMap() {69reorderMap = new HashMap<>();7071reorderMap.put("UTF-8.ja.JP", "japanese-iso10646");72reorderMap.put("UTF-8.ko.KR", "korean-iso10646");73reorderMap.put("UTF-8.zh.TW", "chinese-tw-iso10646");74reorderMap.put("UTF-8.zh.HK", "chinese-tw-iso10646");75reorderMap.put("UTF-8.zh.CN", "chinese-cn-iso10646");76reorderMap.put("x-euc-jp-linux",77split("japanese-x0201,japanese-x0208"));78reorderMap.put("GB2312", "chinese-gb18030");79reorderMap.put("Big5", "chinese-big5");80reorderMap.put("EUC-KR", "korean");81reorderMap.put("GB18030", "chinese-gb18030");82}8384/**85* Sets the OS name and version from environment information.86*/87protected void setOsNameAndVersion(){88super.setOsNameAndVersion();8990if (osName.equals("Linux")) {91try {92File f;93if ((f = new File("/etc/fedora-release")).canRead()) {94osName = "Fedora";95osVersion = getVersionString(f);96} else if ((f = new File("/etc/redhat-release")).canRead()) {97osName = "RedHat";98osVersion = getVersionString(f);99} else if ((f = new File("/etc/turbolinux-release")).canRead()) {100osName = "Turbo";101osVersion = getVersionString(f);102} else if ((f = new File("/etc/SuSE-release")).canRead()) {103osName = "SuSE";104osVersion = getVersionString(f);105} else if ((f = new File("/etc/lsb-release")).canRead()) {106/* Ubuntu and (perhaps others) use only lsb-release.107* Syntax and encoding is compatible with java properties.108* For Ubuntu the ID is "Ubuntu".109*/110Properties props = new Properties();111props.load(new FileInputStream(f));112osName = props.getProperty("DISTRIB_ID");113osVersion = props.getProperty("DISTRIB_RELEASE");114} else if ((f = new File("/etc/os-release")).canRead()) {115Properties props = new Properties();116try (FileInputStream fis = new FileInputStream(f)) {117props.load(fis);118}119osName = props.getProperty("NAME");120osVersion = props.getProperty("VERSION_ID");121osName = extractOsInfo(osName);122if (osName.equals("SLES")) osName = "SuSE";123osVersion = extractOsInfo(osVersion);124}125} catch (Exception e) {126}127}128return;129}130131/**132* Gets the OS version string from a Linux release-specific file.133*/134private String getVersionString(File f){135try {136Scanner sc = new Scanner(f);137return sc.findInLine("(\\d)+((\\.)(\\d)+)*");138}139catch (Exception e){140}141return null;142}143144private String extractOsInfo(String s) {145if (s.startsWith("\"")) s = s.substring(1);146if (s.endsWith("\"")) s = s.substring(0, s.length()-1);147return s;148}149150private static final String fontsDirPrefix = "$JRE_LIB_FONTS";151152protected String mapFileName(String fileName) {153if (fileName != null && fileName.startsWith(fontsDirPrefix)) {154return SunFontManager.jreFontDirName155+ fileName.substring(fontsDirPrefix.length());156}157return fileName;158}159160// overrides FontConfiguration.getFallbackFamilyName161public String getFallbackFamilyName(String fontName, String defaultFallback) {162// maintain compatibility with old font.properties files, which163// either had aliases for TimesRoman & Co. or defined mappings for them.164String compatibilityName = getCompatibilityFamilyName(fontName);165if (compatibilityName != null) {166return compatibilityName;167}168return defaultFallback;169}170171protected String getEncoding(String awtFontName,172String characterSubsetName) {173// extract encoding field from XLFD174int beginIndex = 0;175int fieldNum = 13; // charset registry field176while (fieldNum-- > 0 && beginIndex >= 0) {177beginIndex = awtFontName.indexOf("-", beginIndex) + 1;178}179if (beginIndex == -1) {180return "default";181}182String xlfdEncoding = awtFontName.substring(beginIndex);183if (xlfdEncoding.indexOf("fontspecific") > 0) {184if (awtFontName.indexOf("dingbats") > 0) {185return "sun.font.X11Dingbats";186} else if (awtFontName.indexOf("symbol") > 0) {187return "sun.awt.Symbol";188}189}190String encoding = encodingMap.get(xlfdEncoding);191if (encoding == null) {192encoding = "default";193}194return encoding;195}196197protected Charset getDefaultFontCharset(String fontName) {198return Charset.forName("ISO8859_1");199}200201protected String getFaceNameFromComponentFontName(String componentFontName) {202return null;203}204205protected String getFileNameFromComponentFontName(String componentFontName) {206// for X11, component font name is XLFD207// if we have a file name already, just use it; otherwise let's see208// what the graphics environment can provide209String fileName = getFileNameFromPlatformName(componentFontName);210if (fileName != null && fileName.charAt(0) == '/' &&211!needToSearchForFile(fileName)) {212return fileName;213}214return ((X11FontManager) fontManager).getFileNameFromXLFD(componentFontName);215}216217public HashSet<String> getAWTFontPathSet() {218HashSet<String> fontDirs = new HashSet<String>();219short[] scripts = getCoreScripts(0);220for (int i = 0; i< scripts.length; i++) {221String path = getString(table_awtfontpaths[scripts[i]]);222if (path != null) {223int start = 0;224int colon = path.indexOf(':');225while (colon >= 0) {226fontDirs.add(path.substring(start, colon));227start = colon + 1;228colon = path.indexOf(':', start);229}230fontDirs.add((start == 0) ? path : path.substring(start));231}232}233return fontDirs;234}235236/* methods for table setup ***********************************************/237238private static HashMap<String, String> encodingMap = new HashMap<>();239240private void initTables() {241// encodingMap maps XLFD encoding component to242// name of corresponding java.nio charset243encodingMap.put("iso8859-1", "ISO-8859-1");244encodingMap.put("iso8859-2", "ISO-8859-2");245encodingMap.put("iso8859-4", "ISO-8859-4");246encodingMap.put("iso8859-5", "ISO-8859-5");247encodingMap.put("iso8859-6", "ISO-8859-6");248encodingMap.put("iso8859-7", "ISO-8859-7");249encodingMap.put("iso8859-8", "ISO-8859-8");250encodingMap.put("iso8859-9", "ISO-8859-9");251encodingMap.put("iso8859-13", "ISO-8859-13");252encodingMap.put("iso8859-15", "ISO-8859-15");253encodingMap.put("gb2312.1980-0", "sun.font.X11GB2312");254if (osName == null) {255// use standard converter on Solaris256encodingMap.put("gbk-0", "GBK");257} else {258encodingMap.put("gbk-0", "sun.font.X11GBK");259}260encodingMap.put("gb18030.2000-0", "sun.font.X11GB18030_0");261encodingMap.put("gb18030.2000-1", "sun.font.X11GB18030_1");262encodingMap.put("cns11643-1", "sun.font.X11CNS11643P1");263encodingMap.put("cns11643-2", "sun.font.X11CNS11643P2");264encodingMap.put("cns11643-3", "sun.font.X11CNS11643P3");265encodingMap.put("big5-1", "Big5");266encodingMap.put("big5-0", "Big5");267encodingMap.put("hkscs-1", "Big5-HKSCS");268encodingMap.put("ansi-1251", "windows-1251");269encodingMap.put("koi8-r", "KOI8-R");270encodingMap.put("jisx0201.1976-0", "JIS0201");271encodingMap.put("jisx0208.1983-0", "JIS0208");272encodingMap.put("jisx0212.1990-0", "JIS0212");273encodingMap.put("ksc5601.1987-0", "sun.font.X11KSC5601");274encodingMap.put("ksc5601.1992-3", "sun.font.X11Johab");275encodingMap.put("tis620.2533-0", "TIS-620");276encodingMap.put("iso10646-1", "UTF-16BE");277}278279}280281282