PageRenderTime 78ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/jdk/src/windows/classes/sun/awt/windows/WFontConfiguration.java

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