PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

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