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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/api/fonts/SubstanceFontUtilities.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) 2005-2010 Substance Kirill Grouchnikov. 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 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.
 
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.api.fonts;
 
31
 
 
32
import java.awt.Font;
 
33
import java.security.AccessController;
 
34
import java.security.PrivilegedAction;
 
35
 
 
36
import javax.swing.UIDefaults;
 
37
import javax.swing.plaf.FontUIResource;
 
38
 
 
39
import org.pushingpixels.lafwidget.utils.LookUtils;
 
40
import org.pushingpixels.substance.internal.fonts.*;
 
41
import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
 
42
 
 
43
/**
 
44
 * Font-related utilities.
 
45
 * 
 
46
 * @author Kirill Grouchnikov
 
47
 */
 
48
public class SubstanceFontUtilities {
 
49
        /**
 
50
         * Font set implementation for Substance. This is used to make the window
 
51
         * title font bold.
 
52
         * 
 
53
         * @author Kirill Grouchnikov
 
54
         */
 
55
        private static class SubstanceFontSet implements FontSet {
 
56
                /**
 
57
                 * The default system font set.
 
58
                 */
 
59
                private FontSet systemFontSet;
 
60
 
 
61
                /**
 
62
                 * Creates a new font set for Substance.
 
63
                 * 
 
64
                 * @param systemFontSet
 
65
                 *            The default system font set.
 
66
                 */
 
67
                public SubstanceFontSet(FontSet systemFontSet) {
 
68
                        this.systemFontSet = systemFontSet;
 
69
                }
 
70
 
 
71
                /**
 
72
                 * Returns Substance-specific font resource.
 
73
                 * 
 
74
                 * @param systemFont
 
75
                 *            The default system font.
 
76
                 * @return Substance-specific font resource.
 
77
                 */
 
78
                private FontUIResource getSubstanceFont(FontUIResource systemFont) {
 
79
                        return systemFont;
 
80
                }
 
81
 
 
82
                /**
 
83
                 * Returns Substance-specific font resource.
 
84
                 * 
 
85
                 * @param systemFont
 
86
                 *            The default system font.
 
87
                 * @param toBoldify
 
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.
 
93
                 */
 
94
                private FontUIResource getSubstanceFont(FontUIResource systemFont,
 
95
                                boolean toBoldify, int extraFontSize) {
 
96
                        boolean isOrigItalic = systemFont.isItalic();
 
97
                        int newStyle = systemFont.getStyle();
 
98
                        if (toBoldify) {
 
99
                                if (isOrigItalic)
 
100
                                        newStyle = Font.ITALIC + Font.BOLD;
 
101
                                else
 
102
                                        newStyle = Font.BOLD;
 
103
                        }
 
104
                        return new FontUIResource(systemFont.deriveFont(
 
105
                                        (float) (systemFont.getSize() + extraFontSize)).deriveFont(
 
106
                                        newStyle));
 
107
                        // return new FontUIResource(systemFont.getFontName(), newStyle,
 
108
                        // systemFont.getSize() + extraFontSize);
 
109
                }
 
110
 
 
111
                @Override
 
112
        public FontUIResource getControlFont() {
 
113
                        return this.getSubstanceFont(this.systemFontSet.getControlFont());
 
114
                }
 
115
 
 
116
                @Override
 
117
        public FontUIResource getMenuFont() {
 
118
                        return this.getSubstanceFont(this.systemFontSet.getMenuFont());
 
119
                }
 
120
 
 
121
                @Override
 
122
        public FontUIResource getMessageFont() {
 
123
                        return this.getSubstanceFont(this.systemFontSet.getMessageFont());
 
124
                }
 
125
 
 
126
                @Override
 
127
        public FontUIResource getSmallFont() {
 
128
                        return this.getSubstanceFont(this.systemFontSet.getSmallFont(),
 
129
                                        false, 1);
 
130
                }
 
131
 
 
132
                @Override
 
133
        public FontUIResource getTitleFont() {
 
134
                        return this.getSubstanceFont(this.systemFontSet.getTitleFont());
 
135
                }
 
136
 
 
137
                @Override
 
138
        public FontUIResource getWindowTitleFont() {
 
139
                        return this.getSubstanceFont(this.systemFontSet
 
140
                                        .getWindowTitleFont(), true, 1);
 
141
                }
 
142
        }
 
143
 
 
144
        /**
 
145
         * Returns the default platform-specific font policy.
 
146
         * 
 
147
         * @return Default platform-specific font policy.
 
148
         */
 
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;
 
154
                try {
 
155
                        isKDE = DefaultKDEFontPolicy.isKDERunning();
 
156
                } catch (Throwable t) {
 
157
                        // security access - too bad for KDE desktops.
 
158
                }
 
159
                if (LookUtils.IS_OS_WINDOWS) {
 
160
                        defaultPolicy = FontPolicies.getDefaultWindowsPolicy();
 
161
                } else {
 
162
                        if (LookUtils.IS_OS_MAC) {
 
163
                                defaultPolicy = new DefaultMacFontPolicy();
 
164
                        } else {
 
165
                                if (isKDE) {
 
166
                                        // new in version 4.2
 
167
                                        defaultPolicy = new DefaultKDEFontPolicy();
 
168
                                } else {
 
169
                                        try {
 
170
                                                String desktop = AccessController
 
171
                                                                .doPrivileged(new PrivilegedAction<String>() {
 
172
                                                                        @Override
 
173
                                    public String run() {
 
174
                                                                                return System
 
175
                                                                                                .getProperty("sun.desktop");
 
176
                                                                        }
 
177
                                                                });
 
178
                                                if ("gnome".equals(desktop)) {
 
179
                                                        // new in version 4.1
 
180
                                                        defaultPolicy = new DefaultGnomeFontPolicy();
 
181
                                                        // isGnome = true;
 
182
                                                }
 
183
                                        } catch (Throwable t) {
 
184
                                                // security access - too bad for Gnome desktops.
 
185
                                        }
 
186
                                }
 
187
                        }
 
188
                }
 
189
 
 
190
                // System.out.println("System " + System.getProperty("os.name")
 
191
                // + ", policy " + defaultPolicy.getClass().getName());
 
192
                //
 
193
                SubstanceSizeUtils.resetPointsToPixelsRatio(defaultPolicy);
 
194
                final FontPolicy fontPolicy = FontPolicies
 
195
                                .customSettingsPolicy(defaultPolicy);
 
196
                if (LookUtils.IS_OS_MAC || isKDE)// || isGnome)
 
197
                        return fontPolicy;
 
198
                return new FontPolicy() {
 
199
                        @Override
 
200
            public FontSet getFontSet(String lafName, UIDefaults table) {
 
201
                                FontSet baseResult = fontPolicy.getFontSet(lafName, table);
 
202
                                FontSet substanceFontSet = new SubstanceFontSet(baseResult);
 
203
                                return substanceFontSet;
 
204
                        }
 
205
                };
 
206
        }
 
207
 
 
208
        /**
 
209
         * Returns scaled platform-specific font policy.
 
210
         * 
 
211
         * @param scaleFactor
 
212
         *            Scale factor. Should be positive.
 
213
         * @return Scaled platform-specific font policy.
 
214
         */
 
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() {
 
220
                        @Override
 
221
            public FontSet getFontSet(String lafName, UIDefaults table) {
 
222
                                return new ScaledFontSet(substanceCoreFontSet, scaleFactor);
 
223
                        }
 
224
                };
 
225
                return newFontPolicy;
 
226
        }
 
227
}