PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jdk/src/solaris/classes/sun/awt/motif/MFontConfiguration.java

https://bitbucket.org/nkabir/jdk-6
Java | 408 lines | 300 code | 32 blank | 76 comment | 65 complexity | 0f45a0f3f7b2254ad0b1775b10307a71 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2000, 2007, 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.motif;
  26. import java.awt.Font;
  27. import java.io.BufferedReader;
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.InputStreamReader;
  31. import java.util.HashMap;
  32. import java.util.HashSet;
  33. import java.util.Locale;
  34. import java.util.logging.Logger;
  35. import java.util.Properties;
  36. import java.util.Scanner;
  37. import sun.awt.FontConfiguration;
  38. import sun.awt.X11GraphicsEnvironment;
  39. import sun.java2d.SunGraphicsEnvironment;
  40. import java.nio.charset.Charset;
  41. public class MFontConfiguration extends FontConfiguration {
  42. private static FontConfiguration fontConfig = null;
  43. private static Logger logger;
  44. public MFontConfiguration(SunGraphicsEnvironment environment) {
  45. super(environment);
  46. if (SunGraphicsEnvironment.debugFonts) {
  47. logger = Logger.getLogger("sun.awt.FontConfiguration");
  48. }
  49. initTables();
  50. }
  51. public MFontConfiguration(SunGraphicsEnvironment environment,
  52. boolean preferLocaleFonts,
  53. boolean preferPropFonts) {
  54. super(environment, preferLocaleFonts, preferPropFonts);
  55. if (SunGraphicsEnvironment.debugFonts) {
  56. logger = Logger.getLogger("sun.awt.FontConfiguration");
  57. }
  58. initTables();
  59. }
  60. /* Needs to be kept in sync with updates in the languages used in
  61. * the fontconfig files.
  62. */
  63. protected void initReorderMap() {
  64. reorderMap = new HashMap();
  65. if (osName == null) { /* null means SunOS */
  66. initReorderMapForSolaris();
  67. } else {
  68. initReorderMapForLinux();
  69. }
  70. }
  71. private void initReorderMapForSolaris() {
  72. /* Don't create a no-op entry, so we can optimize this case
  73. * i.e. we don't need to do anything so can avoid slower paths in
  74. * the code.
  75. */
  76. // reorderMap.put("UTF-8", "latin-1");
  77. reorderMap.put("UTF-8.hi", "devanagari"); // NB is in Lucida.
  78. reorderMap.put("UTF-8.ja",
  79. split("japanese-x0201,japanese-x0208,japanese-x0212"));
  80. reorderMap.put("UTF-8.ko", "korean-johab");
  81. reorderMap.put("UTF-8.th", "thai");
  82. reorderMap.put("UTF-8.zh.TW", "chinese-big5");
  83. reorderMap.put("UTF-8.zh.HK", split("chinese-big5,chinese-hkscs"));
  84. if (sun.font.FontManager.isSolaris8) {
  85. reorderMap.put("UTF-8.zh.CN", split("chinese-gb2312,chinese-big5"));
  86. } else {
  87. reorderMap.put("UTF-8.zh.CN",
  88. split("chinese-gb18030-0,chinese-gb18030-1"));
  89. }
  90. reorderMap.put("UTF-8.zh",
  91. split("chinese-big5,chinese-hkscs,chinese-gb18030-0,chinese-gb18030-1"));
  92. reorderMap.put("Big5", "chinese-big5");
  93. reorderMap.put("Big5-HKSCS", split("chinese-big5,chinese-hkscs"));
  94. if (! sun.font.FontManager.isSolaris8 && ! sun.font.FontManager.isSolaris9) {
  95. reorderMap.put("GB2312", split("chinese-gbk,chinese-gb2312"));
  96. } else {
  97. reorderMap.put("GB2312","chinese-gb2312");
  98. }
  99. reorderMap.put("x-EUC-TW",
  100. split("chinese-cns11643-1,chinese-cns11643-2,chinese-cns11643-3"));
  101. reorderMap.put("GBK", "chinese-gbk");
  102. reorderMap.put("GB18030",split("chinese-gb18030-0,chinese-gb18030-1"));
  103. reorderMap.put("TIS-620", "thai");
  104. reorderMap.put("x-PCK",
  105. split("japanese-x0201,japanese-x0208,japanese-x0212"));
  106. reorderMap.put("x-eucJP-Open",
  107. split("japanese-x0201,japanese-x0208,japanese-x0212"));
  108. reorderMap.put("EUC-KR", "korean");
  109. /* Don't create a no-op entry, so we can optimize this case */
  110. // reorderMap.put("ISO-8859-1", "latin-1");
  111. reorderMap.put("ISO-8859-2", "latin-2");
  112. reorderMap.put("ISO-8859-5", "cyrillic-iso8859-5");
  113. reorderMap.put("windows-1251", "cyrillic-cp1251");
  114. reorderMap.put("KOI8-R", "cyrillic-koi8-r");
  115. reorderMap.put("ISO-8859-6", "arabic");
  116. reorderMap.put("ISO-8859-7", "greek");
  117. reorderMap.put("ISO-8859-8", "hebrew");
  118. reorderMap.put("ISO-8859-9", "latin-5");
  119. reorderMap.put("ISO-8859-13", "latin-7");
  120. reorderMap.put("ISO-8859-15", "latin-9");
  121. }
  122. private void initReorderMapForLinux() {
  123. reorderMap.put("UTF-8.ja.JP", "japanese-iso10646");
  124. reorderMap.put("UTF-8.ko.KR", "korean-iso10646");
  125. reorderMap.put("UTF-8.zh.TW", "chinese-tw-iso10646");
  126. reorderMap.put("UTF-8.zh.HK", "chinese-tw-iso10646");
  127. reorderMap.put("UTF-8.zh.CN", "chinese-cn-iso10646");
  128. reorderMap.put("x-euc-jp-linux",
  129. split("japanese-x0201,japanese-x0208"));
  130. reorderMap.put("GB2312", "chinese-gb18030");
  131. reorderMap.put("Big5", "chinese-big5");
  132. reorderMap.put("EUC-KR", "korean");
  133. if (osName.equals("Sun")){
  134. reorderMap.put("GB18030", "chinese-cn-iso10646");
  135. }
  136. else {
  137. reorderMap.put("GB18030", "chinese-gb18030");
  138. }
  139. }
  140. /**
  141. * Sets the OS name and version from environment information.
  142. */
  143. protected void setOsNameAndVersion(){
  144. super.setOsNameAndVersion();
  145. if (osName.equals("SunOS")) {
  146. //don't care os name on Solaris
  147. osName = null;
  148. } else if (osName.equals("Linux")) {
  149. try {
  150. File f;
  151. if ((f = new File("/etc/sun-release")).canRead()) {
  152. osName = "Sun";
  153. osVersion = getVersionString(f);
  154. } else if ((f = new File("/etc/fedora-release")).canRead()) {
  155. osName = "Fedora";
  156. osVersion = getVersionString(f);
  157. } else if ((f = new File("/etc/redhat-release")).canRead()) {
  158. osName = "RedHat";
  159. osVersion = getVersionString(f);
  160. } else if ((f = new File("/etc/turbolinux-release")).canRead()) {
  161. osName = "Turbo";
  162. osVersion = getVersionString(f);
  163. } else if ((f = new File("/etc/SuSE-release")).canRead()) {
  164. osName = "SuSE";
  165. osVersion = getVersionString(f);
  166. } else if ((f = new File("/etc/lsb-release")).canRead()) {
  167. /* Ubuntu and (perhaps others) use only lsb-release.
  168. * Syntax and encoding is compatible with java properties.
  169. * For Ubuntu the ID is "Ubuntu".
  170. */
  171. Properties props = new Properties();
  172. props.load(new FileInputStream(f));
  173. osName = props.getProperty("DISTRIB_ID");
  174. osVersion = props.getProperty("DISTRIB_RELEASE");
  175. }
  176. } catch (Exception e) {
  177. }
  178. }
  179. return;
  180. }
  181. /**
  182. * Gets the OS version string from a Linux release-specific file.
  183. */
  184. private String getVersionString(File f){
  185. try {
  186. Scanner sc = new Scanner(f);
  187. return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
  188. }
  189. catch (Exception e){
  190. }
  191. return null;
  192. }
  193. private static final String fontsDirPrefix = "$JRE_LIB_FONTS";
  194. protected String mapFileName(String fileName) {
  195. if (fileName != null && fileName.startsWith(fontsDirPrefix)) {
  196. return SunGraphicsEnvironment.jreFontDirName
  197. + fileName.substring(fontsDirPrefix.length());
  198. }
  199. return fileName;
  200. }
  201. // overrides FontConfiguration.getFallbackFamilyName
  202. public String getFallbackFamilyName(String fontName, String defaultFallback) {
  203. // maintain compatibility with old font.properties files, which
  204. // either had aliases for TimesRoman & Co. or defined mappings for them.
  205. String compatibilityName = getCompatibilityFamilyName(fontName);
  206. if (compatibilityName != null) {
  207. return compatibilityName;
  208. }
  209. return defaultFallback;
  210. }
  211. protected String getEncoding(String awtFontName,
  212. String characterSubsetName) {
  213. // extract encoding field from XLFD
  214. int beginIndex = 0;
  215. int fieldNum = 13; // charset registry field
  216. while (fieldNum-- > 0 && beginIndex >= 0) {
  217. beginIndex = awtFontName.indexOf("-", beginIndex) + 1;
  218. }
  219. if (beginIndex == -1) {
  220. return "default";
  221. }
  222. String xlfdEncoding = awtFontName.substring(beginIndex);
  223. if (xlfdEncoding.indexOf("fontspecific") > 0) {
  224. if (awtFontName.indexOf("dingbats") > 0) {
  225. return "sun.awt.motif.X11Dingbats";
  226. } else if (awtFontName.indexOf("symbol") > 0) {
  227. return "sun.awt.Symbol";
  228. }
  229. }
  230. String encoding = (String) encodingMap.get(xlfdEncoding);
  231. if (encoding == null) {
  232. encoding = "default";
  233. }
  234. return encoding;
  235. }
  236. protected Charset getDefaultFontCharset(String fontName) {
  237. return Charset.forName("ISO8859_1");
  238. }
  239. /* methods for Motif support *********************************************/
  240. private String[][] motifFontSets = new String[NUM_FONTS][NUM_STYLES];
  241. public String getMotifFontSet(String fontName, int style) {
  242. assert isLogicalFontFamilyName(fontName);
  243. fontName = fontName.toLowerCase(Locale.ENGLISH);
  244. int fontIndex = getFontIndex(fontName);
  245. int styleIndex = getStyleIndex(style);
  246. return getMotifFontSet(fontIndex, styleIndex);
  247. }
  248. private String getMotifFontSet(int fontIndex, int styleIndex) {
  249. String fontSet = motifFontSets[fontIndex][styleIndex];
  250. if (fontSet == null) {
  251. fontSet = buildMotifFontSet(fontIndex, styleIndex);
  252. motifFontSets[fontIndex][styleIndex] = fontSet;
  253. }
  254. return fontSet;
  255. }
  256. private String buildMotifFontSet(int fontIndex, int styleIndex) {
  257. StringBuilder buffer = new StringBuilder();
  258. short[] scripts = getCoreScripts(fontIndex);
  259. for (int i = 0; i < scripts.length; i++) {
  260. short nameID = getComponentFontIDMotif(scripts[i], fontIndex, styleIndex);
  261. if (nameID == 0) {
  262. nameID = getComponentFontID(scripts[i], fontIndex, styleIndex);
  263. }
  264. String name = getComponentFontName(nameID);
  265. if (name == null || name.endsWith("fontspecific")) {
  266. continue;
  267. }
  268. if (buffer.length() > 0) {
  269. buffer.append(',');
  270. }
  271. buffer.append(name);
  272. }
  273. return buffer.toString();
  274. }
  275. protected String getFaceNameFromComponentFontName(String componentFontName) {
  276. return null;
  277. }
  278. protected String getFileNameFromComponentFontName(String componentFontName) {
  279. // for X11, component font name is XLFD
  280. // if we have a file name already, just use it; otherwise let's see
  281. // what the graphics environment can provide
  282. String fileName = getFileNameFromPlatformName(componentFontName);
  283. if (fileName != null && fileName.charAt(0) == '/' &&
  284. !needToSearchForFile(fileName)) {
  285. return fileName;
  286. }
  287. return ((X11GraphicsEnvironment) environment).getFileNameFromXLFD(componentFontName);
  288. }
  289. /**
  290. * Get default font for Motif widgets to use, preventing them from
  291. * wasting time accessing inappropriate X resources. This is called
  292. * only from native code.
  293. *
  294. * This is part of a Motif specific performance enhancement. By
  295. * default, when Motif widgets are created and initialized, Motif will
  296. * set up default fonts for the widgets, which we ALWAYS override.
  297. * This set up includes finding the default font in the widget's X
  298. * resources and fairly expensive requests of the X server to identify
  299. * the specific font or fontset. We avoid all of this overhead by
  300. * providing a well known font to use at the creation of widgets, where
  301. * possible.
  302. *
  303. * The X11 fonts are specified by XLFD strings which have %d as a
  304. * marker to indicate where the fontsize should be substituted. [The
  305. * libc function sprintf() is used to replace it.] The value 140
  306. * specifies a font size of 14 points.
  307. */
  308. private static String getDefaultMotifFontSet() {
  309. String font = ((MFontConfiguration) getFontConfiguration()).getMotifFontSet("sansserif", Font.PLAIN);
  310. if (font != null) {
  311. int i;
  312. while ((i = font.indexOf("%d")) >= 0) {
  313. font = font.substring(0, i) + "140" + font.substring(i+2);
  314. }
  315. }
  316. return font;
  317. }
  318. public HashSet<String> getAWTFontPathSet() {
  319. HashSet<String> fontDirs = new HashSet<String>();
  320. short[] scripts = getCoreScripts(0);
  321. for (int i = 0; i< scripts.length; i++) {
  322. String path = getString(table_awtfontpaths[scripts[i]]);
  323. if (path != null) {
  324. int start = 0;
  325. int colon = path.indexOf(':');
  326. while (colon >= 0) {
  327. fontDirs.add(path.substring(start, colon));
  328. start = colon + 1;
  329. colon = path.indexOf(':', start);
  330. }
  331. fontDirs.add((start == 0) ? path : path.substring(start));
  332. }
  333. }
  334. return fontDirs;
  335. }
  336. /* methods for table setup ***********************************************/
  337. private static HashMap encodingMap = new HashMap();
  338. private void initTables() {
  339. // encodingMap maps XLFD encoding component to
  340. // name of corresponding java.nio charset
  341. encodingMap.put("iso8859-1", "ISO-8859-1");
  342. encodingMap.put("iso8859-2", "ISO-8859-2");
  343. encodingMap.put("iso8859-4", "ISO-8859-4");
  344. encodingMap.put("iso8859-5", "ISO-8859-5");
  345. encodingMap.put("iso8859-6", "ISO-8859-6");
  346. encodingMap.put("iso8859-7", "ISO-8859-7");
  347. encodingMap.put("iso8859-8", "ISO-8859-8");
  348. encodingMap.put("iso8859-9", "ISO-8859-9");
  349. encodingMap.put("iso8859-13", "ISO-8859-13");
  350. encodingMap.put("iso8859-15", "ISO-8859-15");
  351. encodingMap.put("gb2312.1980-0", "sun.awt.motif.X11GB2312");
  352. if (osName == null) {
  353. // use standard converter on Solaris
  354. encodingMap.put("gbk-0", "GBK");
  355. } else {
  356. encodingMap.put("gbk-0", "sun.awt.motif.X11GBK");
  357. }
  358. encodingMap.put("gb18030.2000-0", "sun.awt.motif.X11GB18030_0");
  359. encodingMap.put("gb18030.2000-1", "sun.awt.motif.X11GB18030_1");
  360. encodingMap.put("cns11643-1", "sun.awt.motif.X11CNS11643P1");
  361. encodingMap.put("cns11643-2", "sun.awt.motif.X11CNS11643P2");
  362. encodingMap.put("cns11643-3", "sun.awt.motif.X11CNS11643P3");
  363. encodingMap.put("big5-1", "Big5");
  364. encodingMap.put("big5-0", "Big5");
  365. encodingMap.put("hkscs-1", "Big5-HKSCS");
  366. encodingMap.put("ansi-1251", "windows-1251");
  367. encodingMap.put("koi8-r", "KOI8-R");
  368. encodingMap.put("jisx0201.1976-0", "sun.awt.motif.X11JIS0201");
  369. encodingMap.put("jisx0208.1983-0", "sun.awt.motif.X11JIS0208");
  370. encodingMap.put("jisx0212.1990-0", "sun.awt.motif.X11JIS0212");
  371. encodingMap.put("ksc5601.1987-0", "sun.awt.motif.X11KSC5601");
  372. encodingMap.put("ksc5601.1992-3", "sun.awt.motif.X11Johab");
  373. encodingMap.put("tis620.2533-0", "TIS-620");
  374. encodingMap.put("iso10646-1", "UTF-16BE");
  375. }
  376. }