2
* Copyright (c) 2005-2010 Substance Kirill Grouchnikov. 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 Substance Kirill Grouchnikov 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.
30
package org.pushingpixels.substance.api.fonts;
33
import java.security.AccessController;
34
import java.security.PrivilegedAction;
36
import javax.swing.UIDefaults;
37
import javax.swing.plaf.FontUIResource;
39
import org.pushingpixels.lafwidget.utils.LookUtils;
40
import org.pushingpixels.substance.internal.fonts.*;
41
import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
44
* Font-related utilities.
46
* @author Kirill Grouchnikov
48
public class SubstanceFontUtilities {
50
* Font set implementation for Substance. This is used to make the window
53
* @author Kirill Grouchnikov
55
private static class SubstanceFontSet implements FontSet {
57
* The default system font set.
59
private FontSet systemFontSet;
62
* Creates a new font set for Substance.
64
* @param systemFontSet
65
* The default system font set.
67
public SubstanceFontSet(FontSet systemFontSet) {
68
this.systemFontSet = systemFontSet;
72
* Returns Substance-specific font resource.
75
* The default system font.
76
* @return Substance-specific font resource.
78
private FontUIResource getSubstanceFont(FontUIResource systemFont) {
83
* Returns Substance-specific font resource.
86
* The default system font.
88
* If <code>true</code>, the original font (the first
89
* parameter) is boldified.
90
* @param extraFontSize
91
* Extra font size in pixels.
92
* @return Substance-specific font resource.
94
private FontUIResource getSubstanceFont(FontUIResource systemFont,
95
boolean toBoldify, int extraFontSize) {
96
boolean isOrigItalic = systemFont.isItalic();
97
int newStyle = systemFont.getStyle();
100
newStyle = Font.ITALIC + Font.BOLD;
102
newStyle = Font.BOLD;
104
return new FontUIResource(systemFont.deriveFont(
105
(float) (systemFont.getSize() + extraFontSize)).deriveFont(
107
// return new FontUIResource(systemFont.getFontName(), newStyle,
108
// systemFont.getSize() + extraFontSize);
112
public FontUIResource getControlFont() {
113
return this.getSubstanceFont(this.systemFontSet.getControlFont());
117
public FontUIResource getMenuFont() {
118
return this.getSubstanceFont(this.systemFontSet.getMenuFont());
122
public FontUIResource getMessageFont() {
123
return this.getSubstanceFont(this.systemFontSet.getMessageFont());
127
public FontUIResource getSmallFont() {
128
return this.getSubstanceFont(this.systemFontSet.getSmallFont(),
133
public FontUIResource getTitleFont() {
134
return this.getSubstanceFont(this.systemFontSet.getTitleFont());
138
public FontUIResource getWindowTitleFont() {
139
return this.getSubstanceFont(this.systemFontSet
140
.getWindowTitleFont(), true, 1);
145
* Returns the default platform-specific font policy.
147
* @return Default platform-specific font policy.
149
public static FontPolicy getDefaultFontPolicy() {
150
// boolean toWrapPolicy = !LookUtils.IS_OS_MAC;
151
FontPolicy defaultPolicy = FontPolicies.getDefaultPlasticPolicy();
152
boolean isKDE = false;
153
// boolean isGnome = false;
155
isKDE = DefaultKDEFontPolicy.isKDERunning();
156
} catch (Throwable t) {
157
// security access - too bad for KDE desktops.
159
if (LookUtils.IS_OS_WINDOWS) {
160
defaultPolicy = FontPolicies.getDefaultWindowsPolicy();
162
if (LookUtils.IS_OS_MAC) {
163
defaultPolicy = new DefaultMacFontPolicy();
166
// new in version 4.2
167
defaultPolicy = new DefaultKDEFontPolicy();
170
String desktop = AccessController
171
.doPrivileged(new PrivilegedAction<String>() {
173
public String run() {
175
.getProperty("sun.desktop");
178
if ("gnome".equals(desktop)) {
179
// new in version 4.1
180
defaultPolicy = new DefaultGnomeFontPolicy();
183
} catch (Throwable t) {
184
// security access - too bad for Gnome desktops.
190
// System.out.println("System " + System.getProperty("os.name")
191
// + ", policy " + defaultPolicy.getClass().getName());
193
SubstanceSizeUtils.resetPointsToPixelsRatio(defaultPolicy);
194
final FontPolicy fontPolicy = FontPolicies
195
.customSettingsPolicy(defaultPolicy);
196
if (LookUtils.IS_OS_MAC || isKDE)// || isGnome)
198
return new FontPolicy() {
200
public FontSet getFontSet(String lafName, UIDefaults table) {
201
FontSet baseResult = fontPolicy.getFontSet(lafName, table);
202
FontSet substanceFontSet = new SubstanceFontSet(baseResult);
203
return substanceFontSet;
209
* Returns scaled platform-specific font policy.
212
* Scale factor. Should be positive.
213
* @return Scaled platform-specific font policy.
215
public static FontPolicy getScaledFontPolicy(final float scaleFactor) {
216
final FontSet substanceCoreFontSet = SubstanceFontUtilities
217
.getDefaultFontPolicy().getFontSet("Substance", null);
218
// Create the scaled font set
219
FontPolicy newFontPolicy = new FontPolicy() {
221
public FontSet getFontSet(String lafName, UIDefaults table) {
222
return new ScaledFontSet(substanceCoreFontSet, scaleFactor);
225
return newFontPolicy;