2
* Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package org.pushingpixels.substance.internal.fonts;
34
import java.awt.Toolkit;
35
import java.util.Locale;
37
import org.pushingpixels.lafwidget.utils.LookUtils;
38
import org.pushingpixels.substance.api.fonts.FontPolicy;
39
import org.pushingpixels.substance.api.fonts.FontSet;
42
* Provides static access to popular Windows fonts. The sizes of the font
43
* constants are specified in <em>typographic points</em>, approximately 1/72 of
47
* TODO: Consider changing the visibility of the package private methods to
48
* public. As an alternative we may provide a FontPolicy that can emulate the
49
* font choice on Windows XP/2000 and Windows Vista for different software
50
* resolutions (96dpi/120dpi) and desktop font size settings (Normal/Large/Extra
53
* @author Karsten Lentzsch
62
public final class Fonts {
65
* The name of the default dialog font on western Windows XP.
67
public static final String TAHOMA_NAME = "Tahoma";
70
* The name of the default dialog font on western Windows Vista.
72
public static final String SEGOE_UI_NAME = "Segoe UI";
74
// Physical Fonts *********************************************************
77
* This is the default font on western XP with 96dpi and normal fonts.
78
* Ascent=11, descent=3, height=14, dbuX=6, dbuY=12, 14dluY=21px.
80
public static final Font TAHOMA_11PT = new Font(TAHOMA_NAME, Font.PLAIN, 11);
83
* Ascent=13, descent=3, height=16, dbuX=8, dbuY=13, 14dluY=22.75px.
85
public static final Font TAHOMA_13PT = new Font(TAHOMA_NAME, Font.PLAIN, 13);
88
* Ascent=14, descent=3, height=17, dbuX=8, dbuY=14, 14dluY=24.5px.
90
public static final Font TAHOMA_14PT = new Font(TAHOMA_NAME, Font.PLAIN, 14);
93
* This is Segoe UI 9pt, the default font on western Vista with 96dpi.
94
* Ascent=13, descent=4, height=17, dbuX=7, dbuY=13, 13dluY=21.125px.
96
public static final Font SEGOE_UI_12PT = new Font(SEGOE_UI_NAME,
100
* Ascent=14, descent=4, height=18, dbuX=8, dbuY=14, 13dluY=22.75px.
102
public static final Font SEGOE_UI_13PT = new Font(SEGOE_UI_NAME,
106
* Ascent=16, descent=5, height=21, dbuX=9, dbuY=16, 13dluY=26px.
108
public static final Font SEGOE_UI_15PT = new Font(SEGOE_UI_NAME,
111
// Default Windows Fonts **************************************************
114
* The default icon font on western Windows XP with 96dpi and the dialog
115
* font desktop setting "Normal".
117
public static final Font WINDOWS_XP_96DPI_NORMAL = TAHOMA_11PT;
120
* The default GUI font on western Windows XP with 96dpi and the dialog font
121
* desktop setting "Normal".
123
public static final Font WINDOWS_XP_96DPI_DEFAULT_GUI = TAHOMA_11PT;
126
* The default icon font on western Windows XP with 96dpi and the dialog
127
* font desktop setting "Large".
129
public static final Font WINDOWS_XP_96DPI_LARGE = TAHOMA_13PT;
132
* The default icon font on western Windows XP with 120dpi and the dialog
133
* font desktop setting "Normal".
135
public static final Font WINDOWS_XP_120DPI_NORMAL = TAHOMA_14PT;
138
* The default GUI font on western Windows XP with 120dpi and the dialog
139
* font desktop setting "Normal".
141
public static final Font WINDOWS_XP_120DPI_DEFAULT_GUI = TAHOMA_13PT;
144
* The default icon font on western Windows Vista with 96dpi and the dialog
145
* font desktop setting "Normal".
147
public static final Font WINDOWS_VISTA_96DPI_NORMAL = SEGOE_UI_12PT;
150
* The default icon font on western Windows Vista with 96dpi and the dialog
151
* font desktop setting "Large".
153
public static final Font WINDOWS_VISTA_96DPI_LARGE = SEGOE_UI_15PT;
156
* The default icon font on western Windows Vista with 101dpi and the dialog
157
* font desktop setting "Normal".
160
* TODO: Check if this shall be removed or not.
162
static final Font WINDOWS_VISTA_101DPI_NORMAL = SEGOE_UI_13PT;
165
* The default icon font on western Windows Vista with 120dpi and the dialog
166
* font desktop setting "Normal".
168
public static final Font WINDOWS_VISTA_120DPI_NORMAL = SEGOE_UI_15PT;
170
// Desktop Property Font Keys *********************************************
173
* The desktop property key used to lookup the DEFAULTGUI font. This font
174
* scales with the software resolution only but works in western and
175
* non-western Windows environments.
177
* @see #getWindowsControlFont()
179
static final String WINDOWS_DEFAULT_GUI_FONT_KEY = "win.defaultGUI.font";
182
* The desktop property key used to lookup Windows' icon font. This font
183
* scales with the software resolution and the desktop font size setting
184
* (Normal/Large/Extra Large). However, in some non-western Windows
185
* environments this font cannot display the locale's glyphs.
188
* Implementation Note: Windows uses the icon font to label icons in the
189
* Windows Explorer and other places. It seems to me that this works in
190
* non-western environments due to font chaining.
192
* @see #getWindowsControlFont()
194
static final String WINDOWS_ICON_FONT_KEY = "win.icon.font";
196
// Instance Creation ******************************************************
199
// Override default constructor; prevents instantation.
202
// Font Lookup ************************************************************
204
static Font getDefaultGUIFontWesternModernWindowsNormal() {
205
return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_DEFAULT_GUI
206
: WINDOWS_XP_120DPI_DEFAULT_GUI;
209
static Font getDefaultIconFontWesternModernWindowsNormal() {
210
return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_NORMAL
211
: WINDOWS_XP_120DPI_NORMAL;
214
static Font getDefaultIconFontWesternWindowsVistaNormal() {
215
return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_VISTA_96DPI_NORMAL
216
: WINDOWS_VISTA_120DPI_NORMAL;
220
* Returns the Windows control font used by the JGoodies Looks version 1.x.
221
* It is intended for visual backward compatibility only. The font returned
222
* is the default GUI font that scales with the resolution (96dpi, 120dpi,
223
* etc) but not with the desktop font size settings (normal, large, extra
227
* On Windows Vista, the font may be completely wrong.
229
* @return the Windows default GUI font that scales with the resolution, but
230
* not the desktop font size setting
232
* @throws UnsupportedOperationException
233
* on non-Windows platforms
235
static Font getLooks1xWindowsControlFont() {
236
if (!LookUtils.IS_OS_WINDOWS)
237
throw new UnsupportedOperationException();
239
return getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
243
* Looks up and returns the Windows control font. Returns the Windows icon
244
* title font unless it is inappropriate for the Windows version, Java
245
* renderer, or locale.
248
* The icon title font scales with the resolution (96dpi, 101dpi, 120dpi,
249
* etc) and the desktop font size settings (normal, large, extra large).
250
* Older versions may return a poor font. Also, since Java 1.4 and Java 5
251
* render the Windows Vista icon font Segoe UI poorly, we return the default
252
* GUI font in these environments.
255
* The last check is, if the icon font can display text in the default
256
* locale. Therefore we test if the locale's localized display name can be
257
* displayed by the icon font. For example, Tahoma can display "English",
258
* "Deutsch", but not the display name for "Chinese" in Chinese.
260
* @return the Windows control font
262
* @throws UnsupportedOperationException
263
* on non-Windows platforms
265
public static Font getWindowsControlFont() {
266
if (!LookUtils.IS_OS_WINDOWS)
267
throw new UnsupportedOperationException();
269
Font defaultGUIFont = getDefaultGUIFont();
270
// Return the default GUI font on older Windows versions.
271
if (LookUtils.IS_OS_WINDOWS_95 || LookUtils.IS_OS_WINDOWS_98
272
|| LookUtils.IS_OS_WINDOWS_NT || LookUtils.IS_OS_WINDOWS_ME)
273
return defaultGUIFont;
275
// Java 1.4 and Java 5 raster the Segoe UI poorly,
276
// so we use the older Tahoma, if it can display the localized text.
277
if (LookUtils.IS_OS_WINDOWS_VISTA) {
278
if (LookUtils.IS_JAVA_1_4_OR_5) {
279
Font tahoma = getDefaultGUIFontWesternModernWindowsNormal();
280
return Boolean.TRUE.equals(canDisplayLocalizedText(tahoma,
281
Locale.getDefault())) ? tahoma : defaultGUIFont;
285
Font iconFont = getDesktopFont(WINDOWS_ICON_FONT_KEY);
286
return Boolean.TRUE.equals(canDisplayLocalizedText(iconFont, Locale
287
.getDefault())) ? iconFont : defaultGUIFont;
291
* Looks up and returns the Windows defaultGUI font. Works around a bug with
292
* Java 1.4.2_11, 1.5.0_07, and 1.6 b89 in the Vista Beta2, where the
293
* win.defaultGUI.font desktop property returns null. In this case a logical
294
* "Dialog" font is used as fallback.
296
* @return the Windows defaultGUI font, or a dialog font as fallback.
298
private static Font getDefaultGUIFont() {
299
Font font = getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
302
return new Font("Dialog", Font.PLAIN, 12);
306
* Checks and answers whether the given font can display text that is
307
* localized for the specified locale. Returns <code>null</code> if we can't
311
* First checks, if the locale's display language is available in localized
312
* form, for example "Deutsch" for the German locale. If so, we check if the
313
* given font can display the localized display language.
316
* Otherwise we check some known combinations of fonts and locales and
317
* return the associated results. For all other combinations,
318
* <code>null</code> is returned to indicate that we don't know whether the
319
* font can display text in the given locale.
322
* the font to be tested
324
* the locale to be used
325
* @return <code>Boolean.TRUE</code> if the font can display the locale's
326
* text, <code>Boolean.FALSE</code> if not, <code>null</code> if we
331
public static Boolean canDisplayLocalizedText(Font font, Locale locale) {
332
if (localeHasLocalizedDisplayLanguage(locale)) {
333
return canDisplayLocalizedDisplayLanguage(font,
336
String fontName = font.getName();
337
String language = locale.getLanguage();
338
if ("Tahoma".equals(fontName)) {
339
if ("hi".equals(language))
340
return Boolean.FALSE;
341
else if ("ja".equals(language))
342
return Boolean.FALSE;
343
else if ("ko".equals(language))
344
return Boolean.FALSE;
345
else if ("zh".equals(language))
346
return Boolean.FALSE;
348
if ("Microsoft Sans Serif".equals(fontName)) {
349
if ("ja".equals(language))
350
return Boolean.FALSE;
351
else if ("ko".equals(language))
352
return Boolean.FALSE;
353
else if ("zh".equals(language))
354
return Boolean.FALSE;
360
* Checks and answers if the given font can display the locale's localized
361
* display language, for example "English" for English, "Deutsch" for
362
* German, etc. The test invokes <code>Font#canDisplayUpTo</code> on the
363
* localized display language. In a Chinese locale this test will check if
364
* the font can display Chinese glyphs.
367
* the font to be tested
369
* the locale to be used
370
* @return true if the font can display the locale's localized display
371
* language, false otherwise
373
private static boolean canDisplayLocalizedDisplayLanguage(Font font,
375
String testString = locale.getDisplayLanguage(locale);
376
int index = font.canDisplayUpTo(testString);
381
* Checks and answers whether the locale's display language is available in
382
* a localized form, for example "Deutsch" for the German locale.
386
* @return true if the display language is localized, false if not
388
private static boolean localeHasLocalizedDisplayLanguage(Locale locale) {
389
if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage()))
391
String englishDisplayLanguage = locale
392
.getDisplayLanguage(Locale.ENGLISH);
393
String localizedDisplayLanguage = locale.getDisplayLanguage(locale);
394
return !(englishDisplayLanguage.equals(localizedDisplayLanguage));
398
* Looks up and returns a font using the default toolkit's desktop
402
* the name of the font to return
405
private static Font getDesktopFont(String fontName) {
406
Toolkit toolkit = Toolkit.getDefaultToolkit();
407
return (Font) toolkit.getDesktopProperty(fontName);