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