Path: blob/master/src/java.desktop/unix/classes/sun/font/MFontConfiguration.java
41153 views
/*1* Copyright (c) 2000, 2020, 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}115} catch (Exception e) {116}117}118return;119}120121/**122* Gets the OS version string from a Linux release-specific file.123*/124private String getVersionString(File f){125try {126Scanner sc = new Scanner(f);127return sc.findInLine("(\\d)+((\\.)(\\d)+)*");128}129catch (Exception e){130}131return null;132}133134private static final String fontsDirPrefix = "$JRE_LIB_FONTS";135136protected String mapFileName(String fileName) {137if (fileName != null && fileName.startsWith(fontsDirPrefix)) {138return SunFontManager.jreFontDirName139+ fileName.substring(fontsDirPrefix.length());140}141return fileName;142}143144// overrides FontConfiguration.getFallbackFamilyName145public String getFallbackFamilyName(String fontName, String defaultFallback) {146// maintain compatibility with old font.properties files, which147// either had aliases for TimesRoman & Co. or defined mappings for them.148String compatibilityName = getCompatibilityFamilyName(fontName);149if (compatibilityName != null) {150return compatibilityName;151}152return defaultFallback;153}154155protected String getEncoding(String awtFontName,156String characterSubsetName) {157// extract encoding field from XLFD158int beginIndex = 0;159int fieldNum = 13; // charset registry field160while (fieldNum-- > 0 && beginIndex >= 0) {161beginIndex = awtFontName.indexOf("-", beginIndex) + 1;162}163if (beginIndex == -1) {164return "default";165}166String xlfdEncoding = awtFontName.substring(beginIndex);167if (xlfdEncoding.indexOf("fontspecific") > 0) {168if (awtFontName.indexOf("dingbats") > 0) {169return "sun.font.X11Dingbats";170} else if (awtFontName.indexOf("symbol") > 0) {171return "sun.awt.Symbol";172}173}174String encoding = encodingMap.get(xlfdEncoding);175if (encoding == null) {176encoding = "default";177}178return encoding;179}180181protected Charset getDefaultFontCharset(String fontName) {182return Charset.forName("ISO8859_1");183}184185protected String getFaceNameFromComponentFontName(String componentFontName) {186return null;187}188189protected String getFileNameFromComponentFontName(String componentFontName) {190// for X11, component font name is XLFD191// if we have a file name already, just use it; otherwise let's see192// what the graphics environment can provide193String fileName = getFileNameFromPlatformName(componentFontName);194if (fileName != null && fileName.charAt(0) == '/' &&195!needToSearchForFile(fileName)) {196return fileName;197}198return ((X11FontManager) fontManager).getFileNameFromXLFD(componentFontName);199}200201public HashSet<String> getAWTFontPathSet() {202HashSet<String> fontDirs = new HashSet<String>();203short[] scripts = getCoreScripts(0);204for (int i = 0; i< scripts.length; i++) {205String path = getString(table_awtfontpaths[scripts[i]]);206if (path != null) {207int start = 0;208int colon = path.indexOf(':');209while (colon >= 0) {210fontDirs.add(path.substring(start, colon));211start = colon + 1;212colon = path.indexOf(':', start);213}214fontDirs.add((start == 0) ? path : path.substring(start));215}216}217return fontDirs;218}219220/* methods for table setup ***********************************************/221222private static HashMap<String, String> encodingMap = new HashMap<>();223224private void initTables() {225// encodingMap maps XLFD encoding component to226// name of corresponding java.nio charset227encodingMap.put("iso8859-1", "ISO-8859-1");228encodingMap.put("iso8859-2", "ISO-8859-2");229encodingMap.put("iso8859-4", "ISO-8859-4");230encodingMap.put("iso8859-5", "ISO-8859-5");231encodingMap.put("iso8859-6", "ISO-8859-6");232encodingMap.put("iso8859-7", "ISO-8859-7");233encodingMap.put("iso8859-8", "ISO-8859-8");234encodingMap.put("iso8859-9", "ISO-8859-9");235encodingMap.put("iso8859-13", "ISO-8859-13");236encodingMap.put("iso8859-15", "ISO-8859-15");237encodingMap.put("gb2312.1980-0", "sun.font.X11GB2312");238if (osName == null) {239// use standard converter on Solaris240encodingMap.put("gbk-0", "GBK");241} else {242encodingMap.put("gbk-0", "sun.font.X11GBK");243}244encodingMap.put("gb18030.2000-0", "sun.font.X11GB18030_0");245encodingMap.put("gb18030.2000-1", "sun.font.X11GB18030_1");246encodingMap.put("cns11643-1", "sun.font.X11CNS11643P1");247encodingMap.put("cns11643-2", "sun.font.X11CNS11643P2");248encodingMap.put("cns11643-3", "sun.font.X11CNS11643P3");249encodingMap.put("big5-1", "Big5");250encodingMap.put("big5-0", "Big5");251encodingMap.put("hkscs-1", "Big5-HKSCS");252encodingMap.put("ansi-1251", "windows-1251");253encodingMap.put("koi8-r", "KOI8-R");254encodingMap.put("jisx0201.1976-0", "JIS0201");255encodingMap.put("jisx0208.1983-0", "JIS0208");256encodingMap.put("jisx0212.1990-0", "JIS0212");257encodingMap.put("ksc5601.1987-0", "sun.font.X11KSC5601");258encodingMap.put("ksc5601.1992-3", "sun.font.X11Johab");259encodingMap.put("tis620.2533-0", "TIS-620");260encodingMap.put("iso10646-1", "UTF-16BE");261}262263}264265266