~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/fonts/Fonts.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
31
package org.pushingpixels.substance.internal.fonts;
 
32
 
 
33
import java.awt.Font;
 
34
import java.awt.Toolkit;
 
35
import java.util.Locale;
 
36
 
 
37
import org.pushingpixels.lafwidget.utils.LookUtils;
 
38
import org.pushingpixels.substance.api.fonts.FontPolicy;
 
39
import org.pushingpixels.substance.api.fonts.FontSet;
 
40
 
 
41
/**
 
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
 
44
 * an inch.
 
45
 * <p>
 
46
 * 
 
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
 
51
 * Large).
 
52
 * 
 
53
 * @author Karsten Lentzsch
 
54
 * 
 
55
 * @see FontSet
 
56
 * @see FontSets
 
57
 * @see FontPolicy
 
58
 * @see FontPolicies
 
59
 * 
 
60
 * @since 2.0
 
61
 */
 
62
public final class Fonts {
 
63
 
 
64
        /**
 
65
         * The name of the default dialog font on western Windows XP.
 
66
         */
 
67
        public static final String TAHOMA_NAME = "Tahoma";
 
68
 
 
69
        /**
 
70
         * The name of the default dialog font on western Windows Vista.
 
71
         */
 
72
        public static final String SEGOE_UI_NAME = "Segoe UI";
 
73
 
 
74
        // Physical Fonts *********************************************************
 
75
 
 
76
        /**
 
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.
 
79
         */
 
80
        public static final Font TAHOMA_11PT = new Font(TAHOMA_NAME, Font.PLAIN, 11);
 
81
 
 
82
        /**
 
83
         * Ascent=13, descent=3, height=16, dbuX=8, dbuY=13, 14dluY=22.75px.
 
84
         */
 
85
        public static final Font TAHOMA_13PT = new Font(TAHOMA_NAME, Font.PLAIN, 13);
 
86
 
 
87
        /**
 
88
         * Ascent=14, descent=3, height=17, dbuX=8, dbuY=14, 14dluY=24.5px.
 
89
         */
 
90
        public static final Font TAHOMA_14PT = new Font(TAHOMA_NAME, Font.PLAIN, 14);
 
91
 
 
92
        /**
 
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.
 
95
         */
 
96
        public static final Font SEGOE_UI_12PT = new Font(SEGOE_UI_NAME,
 
97
                        Font.PLAIN, 12);
 
98
 
 
99
        /**
 
100
         * Ascent=14, descent=4, height=18, dbuX=8, dbuY=14, 13dluY=22.75px.
 
101
         */
 
102
        public static final Font SEGOE_UI_13PT = new Font(SEGOE_UI_NAME,
 
103
                        Font.PLAIN, 13);
 
104
 
 
105
        /**
 
106
         * Ascent=16, descent=5, height=21, dbuX=9, dbuY=16, 13dluY=26px.
 
107
         */
 
108
        public static final Font SEGOE_UI_15PT = new Font(SEGOE_UI_NAME,
 
109
                        Font.PLAIN, 15);
 
110
 
 
111
        // Default Windows Fonts **************************************************
 
112
 
 
113
        /**
 
114
         * The default icon font on western Windows XP with 96dpi and the dialog
 
115
         * font desktop setting "Normal".
 
116
         */
 
117
        public static final Font WINDOWS_XP_96DPI_NORMAL = TAHOMA_11PT;
 
118
 
 
119
        /**
 
120
         * The default GUI font on western Windows XP with 96dpi and the dialog font
 
121
         * desktop setting "Normal".
 
122
         */
 
123
        public static final Font WINDOWS_XP_96DPI_DEFAULT_GUI = TAHOMA_11PT;
 
124
 
 
125
        /**
 
126
         * The default icon font on western Windows XP with 96dpi and the dialog
 
127
         * font desktop setting "Large".
 
128
         */
 
129
        public static final Font WINDOWS_XP_96DPI_LARGE = TAHOMA_13PT;
 
130
 
 
131
        /**
 
132
         * The default icon font on western Windows XP with 120dpi and the dialog
 
133
         * font desktop setting "Normal".
 
134
         */
 
135
        public static final Font WINDOWS_XP_120DPI_NORMAL = TAHOMA_14PT;
 
136
 
 
137
        /**
 
138
         * The default GUI font on western Windows XP with 120dpi and the dialog
 
139
         * font desktop setting "Normal".
 
140
         */
 
141
        public static final Font WINDOWS_XP_120DPI_DEFAULT_GUI = TAHOMA_13PT;
 
142
 
 
143
        /**
 
144
         * The default icon font on western Windows Vista with 96dpi and the dialog
 
145
         * font desktop setting "Normal".
 
146
         */
 
147
        public static final Font WINDOWS_VISTA_96DPI_NORMAL = SEGOE_UI_12PT;
 
148
 
 
149
        /**
 
150
         * The default icon font on western Windows Vista with 96dpi and the dialog
 
151
         * font desktop setting "Large".
 
152
         */
 
153
        public static final Font WINDOWS_VISTA_96DPI_LARGE = SEGOE_UI_15PT;
 
154
 
 
155
        /**
 
156
         * The default icon font on western Windows Vista with 101dpi and the dialog
 
157
         * font desktop setting "Normal".
 
158
         * <P>
 
159
         * 
 
160
         * TODO: Check if this shall be removed or not.
 
161
         */
 
162
        static final Font WINDOWS_VISTA_101DPI_NORMAL = SEGOE_UI_13PT;
 
163
 
 
164
        /**
 
165
         * The default icon font on western Windows Vista with 120dpi and the dialog
 
166
         * font desktop setting "Normal".
 
167
         */
 
168
        public static final Font WINDOWS_VISTA_120DPI_NORMAL = SEGOE_UI_15PT;
 
169
 
 
170
        // Desktop Property Font Keys *********************************************
 
171
 
 
172
        /**
 
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.
 
176
         * 
 
177
         * @see #getWindowsControlFont()
 
178
         */
 
179
        static final String WINDOWS_DEFAULT_GUI_FONT_KEY = "win.defaultGUI.font";
 
180
 
 
181
        /**
 
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.
 
186
         * <p>
 
187
         * 
 
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.
 
191
         * 
 
192
         * @see #getWindowsControlFont()
 
193
         */
 
194
        static final String WINDOWS_ICON_FONT_KEY = "win.icon.font";
 
195
 
 
196
        // Instance Creation ******************************************************
 
197
 
 
198
        private Fonts() {
 
199
                // Override default constructor; prevents instantation.
 
200
        }
 
201
 
 
202
        // Font Lookup ************************************************************
 
203
 
 
204
        static Font getDefaultGUIFontWesternModernWindowsNormal() {
 
205
                return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_DEFAULT_GUI
 
206
                                : WINDOWS_XP_120DPI_DEFAULT_GUI;
 
207
        }
 
208
 
 
209
        static Font getDefaultIconFontWesternModernWindowsNormal() {
 
210
                return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_XP_96DPI_NORMAL
 
211
                                : WINDOWS_XP_120DPI_NORMAL;
 
212
        }
 
213
 
 
214
        static Font getDefaultIconFontWesternWindowsVistaNormal() {
 
215
                return LookUtils.IS_LOW_RESOLUTION ? WINDOWS_VISTA_96DPI_NORMAL
 
216
                                : WINDOWS_VISTA_120DPI_NORMAL;
 
217
        }
 
218
 
 
219
        /**
 
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
 
224
         * large).
 
225
         * <p>
 
226
         * 
 
227
         * On Windows Vista, the font may be completely wrong.
 
228
         * 
 
229
         * @return the Windows default GUI font that scales with the resolution, but
 
230
         *         not the desktop font size setting
 
231
         * 
 
232
         * @throws UnsupportedOperationException
 
233
         *             on non-Windows platforms
 
234
         */
 
235
        static Font getLooks1xWindowsControlFont() {
 
236
                if (!LookUtils.IS_OS_WINDOWS)
 
237
                        throw new UnsupportedOperationException();
 
238
 
 
239
                return getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
 
240
        }
 
241
 
 
242
        /**
 
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.
 
246
         * <p>
 
247
         * 
 
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.
 
253
         * <p>
 
254
         * 
 
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.
 
259
         * 
 
260
         * @return the Windows control font
 
261
         * 
 
262
         * @throws UnsupportedOperationException
 
263
         *             on non-Windows platforms
 
264
         */
 
265
        public static Font getWindowsControlFont() {
 
266
                if (!LookUtils.IS_OS_WINDOWS)
 
267
                        throw new UnsupportedOperationException();
 
268
 
 
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;
 
274
 
 
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;
 
282
                        }
 
283
                }
 
284
 
 
285
                Font iconFont = getDesktopFont(WINDOWS_ICON_FONT_KEY);
 
286
                return Boolean.TRUE.equals(canDisplayLocalizedText(iconFont, Locale
 
287
                                .getDefault())) ? iconFont : defaultGUIFont;
 
288
        }
 
289
 
 
290
        /**
 
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.
 
295
         * 
 
296
         * @return the Windows defaultGUI font, or a dialog font as fallback.
 
297
         */
 
298
        private static Font getDefaultGUIFont() {
 
299
                Font font = getDesktopFont(WINDOWS_DEFAULT_GUI_FONT_KEY);
 
300
                if (font != null)
 
301
                        return font;
 
302
                return new Font("Dialog", Font.PLAIN, 12);
 
303
        }
 
304
 
 
305
        /**
 
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
 
308
         * test it.
 
309
         * <p>
 
310
         * 
 
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.
 
314
         * <p>
 
315
         * 
 
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.
 
320
         * 
 
321
         * @param font
 
322
         *            the font to be tested
 
323
         * @param locale
 
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
 
327
         *         don't know
 
328
         * 
 
329
         * @since 2.0.4
 
330
         */
 
331
        public static Boolean canDisplayLocalizedText(Font font, Locale locale) {
 
332
                if (localeHasLocalizedDisplayLanguage(locale)) {
 
333
                        return canDisplayLocalizedDisplayLanguage(font,
 
334
                    locale);
 
335
                }
 
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;
 
347
                }
 
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;
 
355
                }
 
356
                return null;
 
357
        }
 
358
 
 
359
        /**
 
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.
 
365
         * 
 
366
         * @param font
 
367
         *            the font to be tested
 
368
         * @param locale
 
369
         *            the locale to be used
 
370
         * @return true if the font can display the locale's localized display
 
371
         *         language, false otherwise
 
372
         */
 
373
        private static boolean canDisplayLocalizedDisplayLanguage(Font font,
 
374
                        Locale locale) {
 
375
                String testString = locale.getDisplayLanguage(locale);
 
376
                int index = font.canDisplayUpTo(testString);
 
377
                return index == -1;
 
378
        }
 
379
 
 
380
        /**
 
381
         * Checks and answers whether the locale's display language is available in
 
382
         * a localized form, for example "Deutsch" for the German locale.
 
383
         * 
 
384
         * @param locale
 
385
         *            the Locale to test
 
386
         * @return true if the display language is localized, false if not
 
387
         */
 
388
        private static boolean localeHasLocalizedDisplayLanguage(Locale locale) {
 
389
                if (locale.getLanguage().equals(Locale.ENGLISH.getLanguage()))
 
390
                        return true;
 
391
                String englishDisplayLanguage = locale
 
392
                                .getDisplayLanguage(Locale.ENGLISH);
 
393
                String localizedDisplayLanguage = locale.getDisplayLanguage(locale);
 
394
                return !(englishDisplayLanguage.equals(localizedDisplayLanguage));
 
395
        }
 
396
 
 
397
        /**
 
398
         * Looks up and returns a font using the default toolkit's desktop
 
399
         * properties.
 
400
         * 
 
401
         * @param fontName
 
402
         *            the name of the font to return
 
403
         * @return the font
 
404
         */
 
405
        private static Font getDesktopFont(String fontName) {
 
406
                Toolkit toolkit = Toolkit.getDefaultToolkit();
 
407
                return (Font) toolkit.getDesktopProperty(fontName);
 
408
        }
 
409
 
 
410
}