001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.fonts;
031:
032: import java.awt.Font;
033: import java.security.AccessController;
034: import java.security.PrivilegedAction;
035:
036: import javax.swing.UIDefaults;
037: import javax.swing.plaf.FontUIResource;
038:
039: import org.jvnet.lafwidget.utils.LookUtils;
040: import org.jvnet.substance.utils.SubstanceSizeUtils;
041:
042: /**
043: * Font-related utilities.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class SubstanceFontUtilities {
048: /**
049: * Font set implementation for Substance. This is used to make the window
050: * title font bold.
051: *
052: * @author Kirill Grouchnikov
053: */
054: private static class SubstanceFontSet implements FontSet {
055: /**
056: * The default system font set.
057: */
058: private FontSet systemFontSet;
059:
060: /**
061: * Creates a new font set for Substance.
062: *
063: * @param systemFontSet
064: * The default system font set.
065: */
066: public SubstanceFontSet(FontSet systemFontSet) {
067: this .systemFontSet = systemFontSet;
068: }
069:
070: /**
071: * Returns Substance-specific font resource.
072: *
073: * @param systemFont
074: * The default system font.
075: * @return Substance-specific font resource.
076: */
077: private FontUIResource getSubstanceFont(
078: FontUIResource systemFont) {
079: return this .getSubstanceFont(systemFont, false, 0);
080: }
081:
082: /**
083: * Returns Substance-specific font resource.
084: *
085: * @param systemFont
086: * The default system font.
087: * @param toBoldify
088: * If <code>true</code>, the original font (the first
089: * parameter) is boldified.
090: * @param extraFontSize
091: * Extra font size in pixels.
092: * @return Substance-specific font resource.
093: */
094: private FontUIResource getSubstanceFont(
095: FontUIResource systemFont, boolean toBoldify,
096: int extraFontSize) {
097: boolean isOrigItalic = systemFont.isItalic();
098: int newStyle = systemFont.getStyle();
099: if (toBoldify) {
100: if (isOrigItalic)
101: newStyle = Font.ITALIC + Font.BOLD;
102: else
103: newStyle = Font.BOLD;
104: }
105: return new FontUIResource(systemFont.getFontName(),
106: newStyle, systemFont.getSize() + extraFontSize);
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see contrib.com.jgoodies.looks.common.FontSet#getControlFont()
113: */
114: public FontUIResource getControlFont() {
115: return this .getSubstanceFont(this .systemFontSet
116: .getControlFont());
117: }
118:
119: /*
120: * (non-Javadoc)
121: *
122: * @see contrib.com.jgoodies.looks.common.FontSet#getMenuFont()
123: */
124: public FontUIResource getMenuFont() {
125: return this .getSubstanceFont(this .systemFontSet
126: .getMenuFont());
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see contrib.com.jgoodies.looks.common.FontSet#getMessageFont()
133: */
134: public FontUIResource getMessageFont() {
135: return this .getSubstanceFont(this .systemFontSet
136: .getMessageFont());
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see contrib.com.jgoodies.looks.common.FontSet#getSmallFont()
143: */
144: public FontUIResource getSmallFont() {
145: return this .getSubstanceFont(this .systemFontSet
146: .getSmallFont(), false, 1);
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see contrib.com.jgoodies.looks.common.FontSet#getTitleFont()
153: */
154: public FontUIResource getTitleFont() {
155: return this .getSubstanceFont(this .systemFontSet
156: .getTitleFont());
157: }
158:
159: /*
160: * (non-Javadoc)
161: *
162: * @see contrib.com.jgoodies.looks.common.FontSet#getWindowTitleFont()
163: */
164: public FontUIResource getWindowTitleFont() {
165: return this .getSubstanceFont(this .systemFontSet
166: .getWindowTitleFont(), true, 1);
167: }
168: }
169:
170: /**
171: * Returns the default platform-specific font policy.
172: *
173: * @return Default platform-specific font policy.
174: */
175: public static FontPolicy getDefaultFontPolicy() {
176: // boolean toWrapPolicy = !LookUtils.IS_OS_MAC;
177: FontPolicy defaultPolicy = FontPolicies
178: .getDefaultPlasticPolicy();
179: boolean isKDE = false;
180: try {
181: isKDE = DefaultKDEFontPolicy.isKDERunning();
182: } catch (Throwable t) {
183: // security access - too bad for KDE desktops.
184: }
185: if (LookUtils.IS_OS_WINDOWS) {
186: defaultPolicy = FontPolicies.getDefaultWindowsPolicy();
187: } else {
188: if (LookUtils.IS_OS_MAC) {
189: defaultPolicy = new DefaultMacFontPolicy();
190: } else {
191: if (isKDE) {
192: // new in version 4.2
193: defaultPolicy = new DefaultKDEFontPolicy();
194: } else {
195: try {
196: String desktop = AccessController
197: .doPrivileged(new PrivilegedAction<String>() {
198: public String run() {
199: return System
200: .getProperty("sun.desktop");
201: }
202: });
203: if ("gnome".equals(desktop)) {
204: // new in version 4.1
205: defaultPolicy = new DefaultGnomeFontPolicy();
206: }
207: } catch (Throwable t) {
208: // security access - too bad for Gnome desktops.
209: }
210: }
211: }
212: }
213:
214: // System.out.println("System " + System.getProperty("os.name")
215: // + ", policy " + defaultPolicy.getClass().getName());
216: //
217: SubstanceSizeUtils.resetPointsToPixelsRatio(defaultPolicy);
218: final FontPolicy fontPolicy = FontPolicies
219: .customSettingsPolicy(defaultPolicy);
220: if (LookUtils.IS_OS_MAC || isKDE)
221: return fontPolicy;
222: return new FontPolicy() {
223: public FontSet getFontSet(String lafName, UIDefaults table) {
224: FontSet baseResult = fontPolicy.getFontSet(lafName,
225: table);
226: FontSet substanceFontSet = new SubstanceFontSet(
227: baseResult);
228: return substanceFontSet;
229: }
230: };
231: }
232:
233: /**
234: * Returns scaled platform-specific font policy.
235: *
236: * @param scaleFactor
237: * Scale factor. Should be positive.
238: * @return Scaled platform-specific font policy.
239: */
240: public static FontPolicy getScaledFontPolicy(final float scaleFactor) {
241: final FontSet substanceCoreFontSet = SubstanceFontUtilities
242: .getDefaultFontPolicy().getFontSet("Substance", null);
243: // Create the scaled font set
244: FontPolicy newFontPolicy = new FontPolicy() {
245: public FontSet getFontSet(String lafName, UIDefaults table) {
246: return new ScaledFontSet(substanceCoreFontSet,
247: scaleFactor);
248: }
249: };
250: return newFontPolicy;
251: }
252: }
|