1 /*
   2  * Copyright 2001-2005 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.awt.windows;
  27 
  28 import java.util.HashMap;
  29 import java.util.Hashtable;
  30 import sun.awt.FontDescriptor;
  31 import sun.awt.FontConfiguration;
  32 import sun.java2d.SunGraphicsEnvironment;
  33 import java.nio.charset.*;
  34 
  35 public class WFontConfiguration extends FontConfiguration {
  36 
  37     // whether compatibility fallbacks for TimesRoman and Co. are used
  38     private boolean useCompatibilityFallbacks;
  39 
  40     public WFontConfiguration(SunGraphicsEnvironment environment) {
  41         super(environment);
  42         useCompatibilityFallbacks = "windows-1252".equals(encoding);
  43         initTables(encoding);
  44     }
  45 
  46     public WFontConfiguration(SunGraphicsEnvironment environment,
  47                               boolean preferLocaleFonts,
  48                               boolean preferPropFonts) {
  49         super(environment, preferLocaleFonts, preferPropFonts);
  50         useCompatibilityFallbacks = "windows-1252".equals(encoding);
  51     }
  52 
  53     protected void initReorderMap() {
  54         if (encoding.equalsIgnoreCase("windows-31j")) {
  55             localeMap = new Hashtable();
  56             /* Substitute Mincho for Gothic in this one case.
  57              * Note the windows fontconfig files already contain the mapping:
  58              * filename.MS_Mincho=MSMINCHO.TTC
  59              * which isn't essential to this usage but avoids a call
  60              * to loadfonts in the event MSMINCHO.TTC has not otherwise
  61              * been opened and its fonts loaded.
  62              * Also note this usage is only enabled if a private flag is set.
  63              */
  64             localeMap.put("dialoginput.plain.japanese", "MS Mincho");
  65             localeMap.put("dialoginput.bold.japanese", "MS Mincho");
  66             localeMap.put("dialoginput.italic.japanese", "MS Mincho");
  67             localeMap.put("dialoginput.bolditalic.japanese", "MS Mincho");
  68         }
  69         reorderMap = new HashMap();
  70         reorderMap.put("UTF-8.hi", "devanagari");
  71         reorderMap.put("windows-1255", "hebrew");
  72         reorderMap.put("x-windows-874", "thai");
  73         reorderMap.put("windows-31j", "japanese");
  74         reorderMap.put("x-windows-949", "korean");
  75         reorderMap.put("GBK", "chinese-ms936");
  76         reorderMap.put("GB18030", "chinese-gb18030");
  77         reorderMap.put("x-windows-950", "chinese-ms950");
  78         reorderMap.put("x-MS950-HKSCS", split("chinese-ms950,chinese-hkscs"));
  79 //      reorderMap.put("windows-1252", "alphabetic");
  80     }
  81 
  82     protected void setOsNameAndVersion(){
  83         super.setOsNameAndVersion();
  84         if (osName.startsWith("Windows")){
  85             int p, q;
  86             p = osName.indexOf(' ');
  87             if (p == -1){
  88                 osName = null;
  89             }
  90             else{
  91                 q = osName.indexOf(' ', p + 1);
  92                 if (q == -1){
  93                     osName = osName.substring(p + 1);
  94                 }
  95                 else{
  96                     osName = osName.substring(p + 1, q);
  97                 }
  98             }
  99             osVersion = null;
 100         }
 101     }
 102 
 103     // overrides FontConfiguration.getFallbackFamilyName
 104     public String getFallbackFamilyName(String fontName, String defaultFallback) {
 105         // maintain compatibility with old font.properties files, where
 106         // default file had aliases for timesroman & Co, while others didn't.
 107         if (useCompatibilityFallbacks) {
 108             String compatibilityName = getCompatibilityFamilyName(fontName);
 109             if (compatibilityName != null) {
 110                 return compatibilityName;
 111             }
 112         }
 113         return defaultFallback;
 114     }
 115 
 116     protected String makeAWTFontName(String platformFontName, String characterSubsetName) {
 117         String windowsCharset = (String) subsetCharsetMap.get(characterSubsetName);
 118         if (windowsCharset == null) {
 119             windowsCharset = "DEFAULT_CHARSET";
 120         }
 121         return platformFontName + "," + windowsCharset;
 122     }
 123 
 124     protected String getEncoding(String awtFontName, String characterSubsetName) {
 125         String encoding = (String) subsetEncodingMap.get(characterSubsetName);
 126         if (encoding == null) {
 127             encoding = "default";
 128         }
 129         return encoding;
 130     }
 131 
 132     protected Charset getDefaultFontCharset(String fontName) {
 133         return new WDefaultFontCharset(fontName);
 134     }
 135 
 136     public String getFaceNameFromComponentFontName(String componentFontName) {
 137         // for Windows, the platform name is the face name
 138         return componentFontName;
 139     }
 140 
 141     protected String getFileNameFromComponentFontName(String componentFontName) {
 142         return getFileNameFromPlatformName(componentFontName);
 143     }
 144 
 145     /**
 146      * Returns the component font name (face name plus charset) of the
 147      * font that should be used for AWT text components. May return null.
 148      */
 149     public String getTextComponentFontName(String familyName, int style) {
 150         FontDescriptor[] fontDescriptors = getFontDescriptors(familyName, style);
 151         String fontName = findFontWithCharset(fontDescriptors, textInputCharset);
 152         if (fontName == null) {
 153             fontName = findFontWithCharset(fontDescriptors, "DEFAULT_CHARSET");
 154         }
 155         return fontName;
 156     }
 157 
 158     private String findFontWithCharset(FontDescriptor[] fontDescriptors, String charset) {
 159         String fontName = null;
 160         for (int i = 0; i < fontDescriptors.length; i++) {
 161             String componentFontName = fontDescriptors[i].getNativeName();
 162             if (componentFontName.endsWith(charset)) {
 163                 fontName = componentFontName;
 164             }
 165         }
 166         return fontName;
 167     }
 168 
 169     private static HashMap subsetCharsetMap = new HashMap();
 170     private static HashMap subsetEncodingMap = new HashMap();
 171     private static String textInputCharset;
 172 
 173     private void initTables(String defaultEncoding) {
 174         subsetCharsetMap.put("alphabetic", "ANSI_CHARSET");
 175         subsetCharsetMap.put("alphabetic/1252", "ANSI_CHARSET");
 176         subsetCharsetMap.put("alphabetic/default", "DEFAULT_CHARSET");
 177         subsetCharsetMap.put("arabic", "ARABIC_CHARSET");
 178         subsetCharsetMap.put("chinese-ms936", "GB2312_CHARSET");
 179         subsetCharsetMap.put("chinese-gb18030", "GB2312_CHARSET");
 180         subsetCharsetMap.put("chinese-ms950", "CHINESEBIG5_CHARSET");
 181         subsetCharsetMap.put("chinese-hkscs", "CHINESEBIG5_CHARSET");
 182         subsetCharsetMap.put("cyrillic", "RUSSIAN_CHARSET");
 183         subsetCharsetMap.put("devanagari", "DEFAULT_CHARSET");
 184         subsetCharsetMap.put("dingbats", "SYMBOL_CHARSET");
 185         subsetCharsetMap.put("greek", "GREEK_CHARSET");
 186         subsetCharsetMap.put("hebrew", "HEBREW_CHARSET");
 187         subsetCharsetMap.put("japanese", "SHIFTJIS_CHARSET");
 188         subsetCharsetMap.put("korean", "HANGEUL_CHARSET");
 189         subsetCharsetMap.put("latin", "ANSI_CHARSET");
 190         subsetCharsetMap.put("symbol", "SYMBOL_CHARSET");
 191         subsetCharsetMap.put("thai", "THAI_CHARSET");
 192 
 193         subsetEncodingMap.put("alphabetic", "default");
 194         subsetEncodingMap.put("alphabetic/1252", "windows-1252");
 195         subsetEncodingMap.put("alphabetic/default", defaultEncoding);
 196         subsetEncodingMap.put("arabic", "windows-1256");
 197         subsetEncodingMap.put("chinese-ms936", "GBK");
 198         subsetEncodingMap.put("chinese-gb18030", "GB18030");
 199         if ("x-MS950-HKSCS".equals(defaultEncoding)) {
 200             subsetEncodingMap.put("chinese-ms950", "x-MS950-HKSCS");
 201         } else {
 202             subsetEncodingMap.put("chinese-ms950", "x-windows-950"); //MS950
 203         }
 204         subsetEncodingMap.put("chinese-hkscs", "sun.awt.HKSCS");
 205         subsetEncodingMap.put("cyrillic", "windows-1251");
 206         subsetEncodingMap.put("devanagari", "UTF-16LE");
 207         subsetEncodingMap.put("dingbats", "sun.awt.windows.WingDings");
 208         subsetEncodingMap.put("greek", "windows-1253");
 209         subsetEncodingMap.put("hebrew", "windows-1255");
 210         subsetEncodingMap.put("japanese", "windows-31j");
 211         subsetEncodingMap.put("korean", "x-windows-949");
 212         subsetEncodingMap.put("latin", "windows-1252");
 213         subsetEncodingMap.put("symbol", "sun.awt.Symbol");
 214         subsetEncodingMap.put("thai", "x-windows-874");
 215 
 216         if ("windows-1256".equals(defaultEncoding)) {
 217             textInputCharset = "ARABIC_CHARSET";
 218         } else if ("GBK".equals(defaultEncoding)) {
 219             textInputCharset = "GB2312_CHARSET";
 220         } else if ("GB18030".equals(defaultEncoding)) {
 221             textInputCharset = "GB2312_CHARSET";
 222         } else if ("x-windows-950".equals(defaultEncoding)) {
 223             textInputCharset = "CHINESEBIG5_CHARSET";
 224         } else if ("x-MS950-HKSCS".equals(defaultEncoding)) {
 225             textInputCharset = "CHINESEBIG5_CHARSET";
 226         } else if ("windows-1251".equals(defaultEncoding)) {
 227             textInputCharset = "RUSSIAN_CHARSET";
 228         } else if ("UTF-8".equals(defaultEncoding)) {
 229             textInputCharset = "DEFAULT_CHARSET";
 230         } else if ("windows-1253".equals(defaultEncoding)) {
 231             textInputCharset = "GREEK_CHARSET";
 232         } else if ("windows-1255".equals(defaultEncoding)) {
 233             textInputCharset = "HEBREW_CHARSET";
 234         } else if ("windows-31j".equals(defaultEncoding)) {
 235             textInputCharset = "SHIFTJIS_CHARSET";
 236         } else if ("x-windows-949".equals(defaultEncoding)) {
 237             textInputCharset = "HANGEUL_CHARSET";
 238         } else if ("x-windows-874".equals(defaultEncoding)) {
 239             textInputCharset = "THAI_CHARSET";
 240         } else {
 241             textInputCharset = "DEFAULT_CHARSET";
 242         }
 243     }
 244 }