/plugins/Substance/trunk/substance-6.1/substance/src/org/pushingpixels/substance/internal/fonts/Fonts.java

# · Java · 410 lines · 119 code · 44 blank · 247 comment · 27 complexity · b4263b221b6f816e59b8ad582616cf28 MD5 · raw file

  1. /*
  2. * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * o Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * o Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * o Neither the name of JGoodies Karsten Lentzsch nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package org.pushingpixels.substance.internal.fonts;
  31. import java.awt.Font;
  32. import java.awt.Toolkit;
  33. import java.util.Locale;
  34. import org.pushingpixels.lafwidget.utils.LookUtils;
  35. import org.pushingpixels.substance.api.fonts.FontPolicy;
  36. import org.pushingpixels.substance.api.fonts.FontSet;
  37. /**
  38. * Provides static access to popular Windows fonts. The sizes of the font
  39. * constants are specified in <em>typographic points</em>, approximately 1/72 of
  40. * an inch.
  41. * <p>
  42. *
  43. * TODO: Consider changing the visibility of the package private methods to
  44. * public. As an alternative we may provide a FontPolicy that can emulate the
  45. * font choice on Windows XP/2000 and Windows Vista for different software
  46. * resolutions (96dpi/120dpi) and desktop font size settings (Normal/Large/Extra
  47. * Large).
  48. *
  49. * @author Karsten Lentzsch
  50. *
  51. * @see FontSet
  52. * @see FontSets
  53. * @see FontPolicy
  54. * @see FontPolicies
  55. *
  56. * @since 2.0
  57. */
  58. public final class Fonts {
  59. /**
  60. * The name of the default dialog font on western Windows XP.
  61. */
  62. public static final String TAHOMA_NAME = "Tahoma";
  63. /**
  64. * The name of the default dialog font on western Windows Vista.
  65. */
  66. public static final String SEGOE_UI_NAME = "Segoe UI";
  67. // Physical Fonts *********************************************************
  68. /**
  69. * This is the default font on western XP with 96dpi and normal fonts.
  70. * Ascent=11, descent=3, height=14, dbuX=6, dbuY=12, 14dluY=21px.
  71. */
  72. public static final Font TAHOMA_11PT = new Font(TAHOMA_NAME, Font.PLAIN, 11);
  73. /**
  74. * Ascent=13, descent=3, height=16, dbuX=8, dbuY=13, 14dluY=22.75px.
  75. */
  76. public static final Font TAHOMA_13PT = new Font(TAHOMA_NAME, Font.PLAIN, 13);
  77. /**
  78. * Ascent=14, descent=3, height=17, dbuX=8, dbuY=14, 14dluY=24.5px.
  79. */
  80. public static final Font TAHOMA_14PT = new Font(TAHOMA_NAME, Font.PLAIN, 14);
  81. /**
  82. * This is Segoe UI 9pt, the default font on western Vista with 96dpi.
  83. * Ascent=13, descent=4, height=17, dbuX=7, dbuY=13, 13dluY=21.125px.
  84. */
  85. public static final Font SEGOE_UI_12PT = new Font(SEGOE_UI_NAME,
  86. Font.PLAIN, 12);
  87. /**
  88. * Ascent=14, descent=4, height=18, dbuX=8, dbuY=14, 13dluY=22.75px.
  89. */
  90. public static final Font SEGOE_UI_13PT = new Font(SEGOE_UI_NAME,
  91. Font.PLAIN, 13);
  92. /**
  93. * Ascent=16, descent=5, height=21, dbuX=9, dbuY=16, 13dluY=26px.
  94. */
  95. public static final Font SEGOE_UI_15PT = new Font(SEGOE_UI_NAME,
  96. Font.PLAIN, 15);
  97. // Default Windows Fonts **************************************************
  98. /**
  99. * The default icon font on western Windows XP with 96dpi and the dialog
  100. * font desktop setting "Normal".
  101. */
  102. public static final Font WINDOWS_XP_96DPI_NORMAL = TAHOMA_11PT;
  103. /**
  104. * The default GUI font on western Windows XP with 96dpi and the dialog font
  105. * desktop setting "Normal".
  106. */
  107. public static final Font WINDOWS_XP_96DPI_DEFAULT_GUI = TAHOMA_11PT;
  108. /**
  109. * The default icon font on western Windows XP with 96dpi and the dialog
  110. * font desktop setting "Large".
  111. */
  112. public static final Font WINDOWS_XP_96DPI_LARGE = TAHOMA_13PT;
  113. /**
  114. * The default icon font on western Windows XP with 120dpi and the dialog
  115. * font desktop setting "Normal".
  116. */
  117. public static final Font WINDOWS_XP_120DPI_NORMAL = TAHOMA_14PT;
  118. /**
  119. * The default GUI font on western Windows XP with 120dpi and the dialog
  120. * font desktop setting "Normal".
  121. */
  122. public static final Font WINDOWS_XP_120DPI_DEFAULT_GUI = TAHOMA_13PT;
  123. /**
  124. * The default icon font on western Windows Vista with 96dpi and the dialog
  125. * font desktop setting "Normal".
  126. */
  127. public static final Font WINDOWS_VISTA_96DPI_NORMAL = SEGOE_UI_12PT;
  128. /**
  129. * The default icon font on western Windows Vista with 96dpi and the dialog
  130. * font desktop setting "Large".
  131. */
  132. public static final Font WINDOWS_VISTA_96DPI_LARGE = SEGOE_UI_15PT;
  133. /**
  134. * The default icon font on western Windows Vista with 101dpi and the dialog
  135. * font desktop setting "Normal".
  136. * <P>
  137. *
  138. * TODO: Check if this shall be removed or not.
  139. */
  140. static final Font WINDOWS_VISTA_101DPI_NORMAL = SEGOE_UI_13PT;
  141. /**
  142. * The default icon font on western Windows Vista with 120dpi and the dialog
  143. * font desktop setting "Normal".
  144. */
  145. public static final Font WINDOWS_VISTA_120DPI_NORMAL = SEGOE_UI_15PT;
  146. // Desktop Property Font Keys *********************************************
  147. /**
  148. * The desktop property key used to lookup the DEFAULTGUI font. This font
  149. * scales with the software resolution only but works in western and
  150. * non-western Windows environments.
  151. *
  152. * @see #getWindowsControlFont()
  153. */
  154. static final String WINDOWS_DEFAULT_GUI_FONT_KEY = "win.defaultGUI.font";
  155. /**
  156. * The desktop property key used to lookup Windows' icon font. This font
  157. * scales with the software resolution and the desktop font size setting
  158. * (Normal/Large/Extra Large). However, in some non-western Windows
  159. * environments this font cannot display the locale's glyphs.
  160. * <p>
  161. *
  162. * Implementation Note: Windows uses the icon font to label icons in the
  163. * Windows Explorer and other places. It seems to me that this works in
  164. * non-western environments due to font chaining.
  165. *
  166. * @see #getWindowsControlFont()
  167. */
  168. static final String WINDOWS_ICON_FONT_KEY = "win.icon.font";
  169. // Instance Creation ******************************************************
  170. private Fonts() {
  171. // Override default constructor; prevents instantation.
  172. }
  173. // Font Lookup ************************************************************
  174. static Font getDefaultGUIFontWesternModernWindowsNormal() {
  175. return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_DEFAULT_GUI
  176. : WINDOWS_XP_120DPI_DEFAULT_GUI;
  177. }
  178. static Font getDefaultIconFontWesternModernWindowsNormal() {
  179. return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_NORMAL
  180. : WINDOWS_XP_120DPI_NORMAL;
  181. }
  182. static Font getDefaultIconFontWesternWindowsVistaNormal() {
  183. return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_VISTA_96DPI_NORMAL
  184. : WINDOWS_VISTA_120DPI_NORMAL;
  185. }
  186. /**
  187. * Returns the Windows control font used by the JGoodies Looks version 1.x.
  188. * It is intended for visual backward compatibility only. The font returned
  189. * is the default GUI font that scales with the resolution (96dpi, 120dpi,
  190. * etc) but not with the desktop font size settings (normal, large, extra
  191. * large).
  192. * <p>
  193. *
  194. * On Windows Vista, the font may be completely wrong.
  195. *
  196. * @return the Windows default GUI font that scales with the resolution, but
  197. * not the desktop font size setting
  198. *
  199. * @throws UnsupportedOperationException
  200. * on non-Windows platforms
  201. */
  202. static Font getLooks1xWindowsControlFont() {
  203. if (!LookUtils.IS_OS_WINDOWS)
  204. throw new UnsupportedOperationException();
  205. return getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
  206. }
  207. /**
  208. * Looks up and returns the Windows control font. Returns the Windows icon
  209. * title font unless it is inappropriate for the Windows version, Java
  210. * renderer, or locale.
  211. * <p>
  212. *
  213. * The icon title font scales with the resolution (96dpi, 101dpi, 120dpi,
  214. * etc) and the desktop font size settings (normal, large, extra large).
  215. * Older versions may return a poor font. Also, since Java 1.4 and Java 5
  216. * render the Windows Vista icon font Segoe UI poorly, we return the default
  217. * GUI font in these environments.
  218. * <p>
  219. *
  220. * The last check is, if the icon font can display text in the default
  221. * locale. Therefore we test if the locale's localized display name can be
  222. * displayed by the icon font. For example, Tahoma can display "English",
  223. * "Deutsch", but not the display name for "Chinese" in Chinese.
  224. *
  225. * @return the Windows control font
  226. *
  227. * @throws UnsupportedOperationException
  228. * on non-Windows platforms
  229. */
  230. public static Font getWindowsControlFont() {
  231. if (!LookUtils.IS_OS_WINDOWS)
  232. throw new UnsupportedOperationException();
  233. Font defaultGUIFont = getDefaultGUIFont();
  234. // Return the default GUI font on older Windows versions.
  235. if (LookUtils.IS_OS_WINDOWS_95 || LookUtils.IS_OS_WINDOWS_98
  236. || LookUtils.IS_OS_WINDOWS_NT || LookUtils.IS_OS_WINDOWS_ME)
  237. return defaultGUIFont;
  238. // Java 1.4 and Java 5 raster the Segoe UI poorly,
  239. // so we use the older Tahoma, if it can display the localized text.
  240. if (LookUtils.IS_OS_WINDOWS_VISTA) {
  241. if (LookUtils.IS_JAVA_1_4_OR_5) {
  242. Font tahoma = getDefaultGUIFontWesternModernWindowsNormal();
  243. return Boolean.TRUE.equals(canDisplayLocalizedText(tahoma,
  244. Locale.getDefault())) ? tahoma : defaultGUIFont;
  245. }
  246. }
  247. Font iconFont = getDesktopFont(WINDOWS_ICON_FONT_KEY);
  248. return Boolean.TRUE.equals(canDisplayLocalizedText(iconFont, Locale
  249. .getDefault())) ? iconFont : defaultGUIFont;
  250. }
  251. /**
  252. * Looks up and returns the Windows defaultGUI font. Works around a bug with
  253. * Java 1.4.2_11, 1.5.0_07, and 1.6 b89 in the Vista Beta2, where the
  254. * win.defaultGUI.font desktop property returns null. In this case a logical
  255. * "Dialog" font is used as fallback.
  256. *
  257. * @return the Windows defaultGUI font, or a dialog font as fallback.
  258. */
  259. private static Font getDefaultGUIFont() {
  260. Font font = getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
  261. if (font != null)
  262. return font;
  263. return new Font("Dialog", Font.PLAIN, 12);
  264. }
  265. /**
  266. * Checks and answers whether the given font can display text that is
  267. * localized for the specified locale. Returns <code>null</code> if we can't
  268. * test it.
  269. * <p>
  270. *
  271. * First checks, if the locale's display language is available in localized
  272. * form, for example "Deutsch" for the German locale. If so, we check if the
  273. * given font can display the localized display language.
  274. * <p>
  275. *
  276. * Otherwise we check some known combinations of fonts and locales and
  277. * return the associated results. For all other combinations,
  278. * <code>null</code> is returned to indicate that we don't know whether the
  279. * font can display text in the given locale.
  280. *
  281. * @param font
  282. * the font to be tested
  283. * @param locale
  284. * the locale to be used
  285. * @return <code>Boolean.TRUE</code> if the font can display the locale's
  286. * text, <code>Boolean.FALSE</code> if not, <code>null</code> if we
  287. * don't know
  288. *
  289. * @since 2.0.4
  290. */
  291. public static Boolean canDisplayLocalizedText(Font font, Locale locale) {
  292. if (localeHasLocalizedDisplayLanguage(locale)) {
  293. return Boolean.valueOf(canDisplayLocalizedDisplayLanguage(font,
  294. locale));
  295. }
  296. String fontName = font.getName();
  297. String language = locale.getLanguage();
  298. if ("Tahoma".equals(fontName)) {
  299. if ("hi".equals(language))
  300. return Boolean.FALSE;
  301. else if ("ja".equals(language))
  302. return Boolean.FALSE;
  303. else if ("ko".equals(language))
  304. return Boolean.FALSE;
  305. else if ("zh".equals(language))
  306. return Boolean.FALSE;
  307. }
  308. if ("Microsoft Sans Serif".equals(fontName)) {
  309. if ("ja".equals(language))
  310. return Boolean.FALSE;
  311. else if ("ko".equals(language))
  312. return Boolean.FALSE;
  313. else if ("zh".equals(language))
  314. return Boolean.FALSE;
  315. }
  316. return null;
  317. }
  318. /**
  319. * Checks and answers if the given font can display the locale's localized
  320. * display language, for example "English" for English, "Deutsch" for
  321. * German, etc. The test invokes <code>Font#canDisplayUpTo</code> on the
  322. * localized display language. In a Chinese locale this test will check if
  323. * the font can display Chinese glyphs.
  324. *
  325. * @param font
  326. * the font to be tested
  327. * @param locale
  328. * the locale to be used
  329. * @return true if the font can display the locale's localized display
  330. * language, false otherwise
  331. */
  332. private static boolean canDisplayLocalizedDisplayLanguage(Font font,
  333. Locale locale) {
  334. String testString = locale.getDisplayLanguage(locale);
  335. int index = font.canDisplayUpTo(testString);
  336. return index == -1;
  337. }
  338. /**
  339. * Checks and answers whether the locale's display language is available in
  340. * a localized form, for example "Deutsch" for the German locale.
  341. *
  342. * @param locale
  343. * the Locale to test
  344. * @return true if the display language is localized, false if not
  345. */
  346. private static boolean localeHasLocalizedDisplayLanguage(Locale locale) {
  347. if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage()))
  348. return true;
  349. String englishDisplayLanguage = locale
  350. .getDisplayLanguage(Locale.ENGLISH);
  351. String localizedDisplayLanguage = locale.getDisplayLanguage(locale);
  352. return !(englishDisplayLanguage.equals(localizedDisplayLanguage));
  353. }
  354. /**
  355. * Looks up and returns a font using the default toolkit's desktop
  356. * properties.
  357. *
  358. * @param fontName
  359. * the name of the font to return
  360. * @return the font
  361. */
  362. private static Font getDesktopFont(String fontName) {
  363. Toolkit toolkit = Toolkit.getDefaultToolkit();
  364. return (Font) toolkit.getDesktopProperty(fontName);
  365. }
  366. }