001: /*
002: * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.windows;
027:
028: import java.util.HashMap;
029: import java.util.Hashtable;
030: import sun.awt.FontDescriptor;
031: import sun.awt.FontConfiguration;
032: import sun.java2d.SunGraphicsEnvironment;
033: import java.nio.charset.*;
034:
035: public class WFontConfiguration extends FontConfiguration {
036:
037: // whether compatibility fallbacks for TimesRoman and Co. are used
038: private boolean useCompatibilityFallbacks;
039:
040: public WFontConfiguration(SunGraphicsEnvironment environment) {
041: super (environment);
042: useCompatibilityFallbacks = "windows-1252".equals(encoding);
043: initTables(encoding);
044: }
045:
046: public WFontConfiguration(SunGraphicsEnvironment environment,
047: boolean preferLocaleFonts, boolean preferPropFonts) {
048: super (environment, preferLocaleFonts, preferPropFonts);
049: useCompatibilityFallbacks = "windows-1252".equals(encoding);
050: }
051:
052: protected void initReorderMap() {
053: if (encoding.equalsIgnoreCase("windows-31j")) {
054: localeMap = new Hashtable();
055: /* Substitute Mincho for Gothic in this one case.
056: * Note the windows fontconfig files already contain the mapping:
057: * filename.MS_Mincho=MSMINCHO.TTC
058: * which isn't essential to this usage but avoids a call
059: * to loadfonts in the event MSMINCHO.TTC has not otherwise
060: * been opened and its fonts loaded.
061: * Also note this usage is only enabled if a private flag is set.
062: */
063: if ("98".equals(osName) || "Me".equals(osName)) {
064: localeMap.put("dialoginput.plain.japanese",
065: "\uff2d\uff33 \u660e\u671d");
066: localeMap.put("dialoginput.bold.japanese",
067: "\uff2d\uff33 \u660e\u671d");
068: localeMap.put("dialoginput.italic.japanese",
069: "\uff2d\uff33 \u660e\u671d");
070: localeMap.put("dialoginput.bolditalic.japanese",
071: "\uff2d\uff33 \u660e\u671d");
072: } else {
073:
074: localeMap
075: .put("dialoginput.plain.japanese", "MS Mincho");
076: localeMap.put("dialoginput.bold.japanese", "MS Mincho");
077: localeMap.put("dialoginput.italic.japanese",
078: "MS Mincho");
079: localeMap.put("dialoginput.bolditalic.japanese",
080: "MS Mincho");
081: }
082: }
083: reorderMap = new HashMap();
084: reorderMap.put("UTF-8.hi", "devanagari");
085: reorderMap.put("windows-1255", "hebrew");
086: reorderMap.put("x-windows-874", "thai");
087: reorderMap.put("windows-31j", "japanese");
088: reorderMap.put("x-windows-949", "korean");
089: reorderMap.put("GBK", "chinese-ms936");
090: reorderMap.put("GB18030", "chinese-gb18030");
091: reorderMap.put("x-windows-950", "chinese-ms950");
092: reorderMap.put("x-MS950-HKSCS",
093: split("chinese-ms950,chinese-hkscs"));
094: // reorderMap.put("windows-1252", "alphabetic");
095: }
096:
097: protected void setOsNameAndVersion() {
098: super .setOsNameAndVersion();
099: if (osName.startsWith("Windows")) {
100: int p, q;
101: p = osName.indexOf(' ');
102: if (p == -1) {
103: osName = null;
104: } else {
105: q = osName.indexOf(' ', p + 1);
106: if (q == -1) {
107: osName = osName.substring(p + 1);
108: } else {
109: osName = osName.substring(p + 1, q);
110: }
111: }
112: osVersion = null;
113: }
114: }
115:
116: // overrides FontConfiguration.getFallbackFamilyName
117: public String getFallbackFamilyName(String fontName,
118: String defaultFallback) {
119: // maintain compatibility with old font.properties files, where
120: // default file had aliases for timesroman & Co, while others didn't.
121: if (useCompatibilityFallbacks) {
122: String compatibilityName = getCompatibilityFamilyName(fontName);
123: if (compatibilityName != null) {
124: return compatibilityName;
125: }
126: }
127: return defaultFallback;
128: }
129:
130: protected String makeAWTFontName(String platformFontName,
131: String characterSubsetName) {
132: String windowsCharset = (String) subsetCharsetMap
133: .get(characterSubsetName);
134: if (windowsCharset == null) {
135: windowsCharset = "DEFAULT_CHARSET";
136: }
137: return platformFontName + "," + windowsCharset;
138: }
139:
140: protected String getEncoding(String awtFontName,
141: String characterSubsetName) {
142: String encoding = (String) subsetEncodingMap
143: .get(characterSubsetName);
144: if (encoding == null) {
145: encoding = "default";
146: }
147: return encoding;
148: }
149:
150: protected Charset getDefaultFontCharset(String fontName) {
151: return new WDefaultFontCharset(fontName);
152: }
153:
154: public String getFaceNameFromComponentFontName(
155: String componentFontName) {
156: // for Windows, the platform name is the face name
157: return componentFontName;
158: }
159:
160: protected String getFileNameFromComponentFontName(
161: String componentFontName) {
162: return getFileNameFromPlatformName(componentFontName);
163: }
164:
165: /**
166: * Returns the component font name (face name plus charset) of the
167: * font that should be used for AWT text components. May return null.
168: */
169: public String getTextComponentFontName(String familyName, int style) {
170: FontDescriptor[] fontDescriptors = getFontDescriptors(
171: familyName, style);
172: String fontName = findFontWithCharset(fontDescriptors,
173: textInputCharset);
174: if (fontName == null) {
175: fontName = findFontWithCharset(fontDescriptors,
176: "DEFAULT_CHARSET");
177: }
178: return fontName;
179: }
180:
181: private String findFontWithCharset(
182: FontDescriptor[] fontDescriptors, String charset) {
183: String fontName = null;
184: for (int i = 0; i < fontDescriptors.length; i++) {
185: String componentFontName = fontDescriptors[i]
186: .getNativeName();
187: if (componentFontName.endsWith(charset)) {
188: fontName = componentFontName;
189: }
190: }
191: return fontName;
192: }
193:
194: private static HashMap subsetCharsetMap = new HashMap();
195: private static HashMap subsetEncodingMap = new HashMap();
196: private static String textInputCharset;
197:
198: private void initTables(String defaultEncoding) {
199: subsetCharsetMap.put("alphabetic", "ANSI_CHARSET");
200: subsetCharsetMap.put("alphabetic/1252", "ANSI_CHARSET");
201: subsetCharsetMap.put("alphabetic/default", "DEFAULT_CHARSET");
202: subsetCharsetMap.put("arabic", "ARABIC_CHARSET");
203: subsetCharsetMap.put("chinese-ms936", "GB2312_CHARSET");
204: subsetCharsetMap.put("chinese-gb18030", "GB2312_CHARSET");
205: subsetCharsetMap.put("chinese-ms950", "CHINESEBIG5_CHARSET");
206: subsetCharsetMap.put("chinese-hkscs", "CHINESEBIG5_CHARSET");
207: subsetCharsetMap.put("cyrillic", "RUSSIAN_CHARSET");
208: subsetCharsetMap.put("devanagari", "DEFAULT_CHARSET");
209: subsetCharsetMap.put("dingbats", "SYMBOL_CHARSET");
210: subsetCharsetMap.put("greek", "GREEK_CHARSET");
211: subsetCharsetMap.put("hebrew", "HEBREW_CHARSET");
212: subsetCharsetMap.put("japanese", "SHIFTJIS_CHARSET");
213: subsetCharsetMap.put("korean", "HANGEUL_CHARSET");
214: subsetCharsetMap.put("latin", "ANSI_CHARSET");
215: subsetCharsetMap.put("symbol", "SYMBOL_CHARSET");
216: subsetCharsetMap.put("thai", "THAI_CHARSET");
217:
218: subsetEncodingMap.put("alphabetic", "default");
219: subsetEncodingMap.put("alphabetic/1252", "windows-1252");
220: subsetEncodingMap.put("alphabetic/default", defaultEncoding);
221: subsetEncodingMap.put("arabic", "windows-1256");
222: subsetEncodingMap.put("chinese-ms936", "GBK");
223: subsetEncodingMap.put("chinese-gb18030", "GB18030");
224: if ("x-MS950-HKSCS".equals(defaultEncoding)) {
225: subsetEncodingMap.put("chinese-ms950", "x-MS950-HKSCS");
226: } else {
227: subsetEncodingMap.put("chinese-ms950", "x-windows-950"); //MS950
228: }
229: subsetEncodingMap.put("chinese-hkscs", "sun.awt.HKSCS");
230: subsetEncodingMap.put("cyrillic", "windows-1251");
231: subsetEncodingMap.put("devanagari", "UTF-16LE");
232: subsetEncodingMap.put("dingbats", "sun.awt.windows.WingDings");
233: subsetEncodingMap.put("greek", "windows-1253");
234: subsetEncodingMap.put("hebrew", "windows-1255");
235: subsetEncodingMap.put("japanese", "windows-31j");
236: subsetEncodingMap.put("korean", "x-windows-949");
237: subsetEncodingMap.put("latin", "windows-1252");
238: subsetEncodingMap.put("symbol", "sun.awt.Symbol");
239: subsetEncodingMap.put("thai", "x-windows-874");
240:
241: if ("windows-1256".equals(defaultEncoding)) {
242: textInputCharset = "ARABIC_CHARSET";
243: } else if ("GBK".equals(defaultEncoding)) {
244: textInputCharset = "GB2312_CHARSET";
245: } else if ("GB18030".equals(defaultEncoding)) {
246: textInputCharset = "GB2312_CHARSET";
247: } else if ("x-windows-950".equals(defaultEncoding)) {
248: textInputCharset = "CHINESEBIG5_CHARSET";
249: } else if ("x-MS950-HKSCS".equals(defaultEncoding)) {
250: textInputCharset = "CHINESEBIG5_CHARSET";
251: } else if ("windows-1251".equals(defaultEncoding)) {
252: textInputCharset = "RUSSIAN_CHARSET";
253: } else if ("UTF-8".equals(defaultEncoding)) {
254: textInputCharset = "DEFAULT_CHARSET";
255: } else if ("windows-1253".equals(defaultEncoding)) {
256: textInputCharset = "GREEK_CHARSET";
257: } else if ("windows-1255".equals(defaultEncoding)) {
258: textInputCharset = "HEBREW_CHARSET";
259: } else if ("windows-31j".equals(defaultEncoding)) {
260: textInputCharset = "SHIFTJIS_CHARSET";
261: } else if ("x-windows-949".equals(defaultEncoding)) {
262: textInputCharset = "HANGEUL_CHARSET";
263: } else if ("x-windows-874".equals(defaultEncoding)) {
264: textInputCharset = "THAI_CHARSET";
265: } else {
266: textInputCharset = "DEFAULT_CHARSET";
267: }
268: }
269: }
|