PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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