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.io.*;
034:
035: import javax.swing.UIDefaults;
036:
037: /**
038: * The default font policy for KDE desktops.
039: *
040: * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
041: */
042: public class DefaultKDEFontPolicy implements FontPolicy {
043:
044: /**
045: * method to check if current user session is KDE
046: *
047: * @return {@Code true} if KDE session is currently running.
048: */
049: public static boolean isKDERunning() {
050: // KDE_FULL_SESSION=true
051: return "true".equals(System.getenv("KDE_FULL_SESSION"));
052: }
053:
054: private static final String SANS_SERIF = "sans";
055:
056: private static FontSet fontSet = null;
057:
058: public synchronized FontSet getFontSet(String lafName,
059: UIDefaults table) {
060: if (fontSet == null) {
061: // size is the most important, then family and then style
062: int commonSize = 10;
063: int menuSize = 10;
064: int titleSize = 10;
065: int commonStyle = Font.PLAIN;
066: int menuStyle = Font.PLAIN;
067: int titleStyle = Font.BOLD;
068: String commonFamily = SANS_SERIF;
069: String menuFamily = SANS_SERIF;
070: String titleFamily = SANS_SERIF;
071:
072: /*
073: * have some font params in this file.
074: *
075: * [General] font menuFont [WM] activeFont
076: *
077: * plain: null bold: Sans Serif,10,-1,5,75,0,0,0,0,0 italic: Sans
078: * Serif,10,-1,5,50,1,0,0,0,0 italic-bold: Sans
079: * Serif,10,-1,5,75,1,0,0,0,0
080: */
081: // no use to do checks, they all will go back to default font, so
082: // just catching exceptions...
083: try {
084:
085: String kdeglobals = getFileContent(".kde/share/config/kdeglobals");
086:
087: // <editor-fold defaultstate="collapsed" desc=" reading common
088: // font ">
089: try {
090: String font = getIniParam(kdeglobals, "[General]",
091: "font");
092: String[] params = font.split(",");
093: commonSize = Integer.parseInt(params[1]);
094: boolean bold = "75".equals(params[4]);
095: boolean italic = "1".equals(params[5]);
096: if (bold && italic) {
097: commonStyle = Font.BOLD + Font.ITALIC;
098: } else if (italic) {
099: commonStyle = Font.ITALIC;
100: } else if (bold) {
101: commonStyle = Font.BOLD;
102: } else {
103: commonStyle = Font.PLAIN;
104: }
105: } catch (Exception fontReadingException) {
106: commonStyle = Font.PLAIN;
107: commonSize = 10;
108: commonFamily = SANS_SERIF;
109: }
110: // </editor-fold>
111:
112: // <editor-fold defaultstate="collapsed" desc=" reading menu
113: // font ">
114: try {
115: String font = getIniParam(kdeglobals, "[General]",
116: "menuFont");
117: String[] params = font.split(",");
118: menuSize = Integer.parseInt(params[1]);
119: boolean bold = "75".equals(params[4]);
120: boolean italic = "1".equals(params[5]);
121: if (bold && italic) {
122: menuStyle = Font.BOLD + Font.ITALIC;
123: } else if (italic) {
124: menuStyle = Font.ITALIC;
125: } else if (bold) {
126: menuStyle = Font.BOLD;
127: } else {
128: menuStyle = Font.PLAIN;
129: }
130: } catch (Exception menuFontException) {
131: menuSize = 10;
132: menuStyle = Font.PLAIN;
133: menuFamily = SANS_SERIF;
134: }
135: // </editor-fold>
136:
137: // <editor-fold defaultstate="collapsed" desc=" reading title
138: // font ">
139: try {
140: String font = getIniParam(kdeglobals, "[WM]",
141: "activeFont");
142: String[] params = font.split(",");
143: titleSize = Integer.parseInt(params[1]);
144: boolean bold = "75".equals(params[4]);
145: boolean italic = "1".equals(params[5]);
146: if (bold && italic) {
147: titleStyle = Font.BOLD + Font.ITALIC;
148: } else if (italic) {
149: // System.out.println("italic");
150: titleStyle = Font.ITALIC;
151: } else if (bold) {
152: titleStyle = Font.BOLD;
153: } else {
154: titleStyle = Font.PLAIN;
155: }
156: } catch (Exception titleFontException) {
157: titleSize = 10;
158: titleStyle = Font.BOLD;
159: titleFamily = SANS_SERIF;
160: }
161: // </editor-fold>
162:
163: } catch (Exception kdeglobalsReadingException) {
164: commonSize = 10;
165: menuSize = 10;
166: titleSize = 10;
167:
168: commonStyle = Font.PLAIN;
169: menuStyle = Font.PLAIN;
170: titleStyle = Font.BOLD;
171:
172: commonFamily = SANS_SERIF;
173: menuFamily = SANS_SERIF;
174: titleFamily = SANS_SERIF;
175: }
176:
177: double dcommonSize = commonSize;
178: double dmenuSize = menuSize;
179: double dtitleSize = titleSize;
180:
181: int dpi = 96;
182: try {
183: String dpiParam = getIniParam(
184: getFileContent(".kde/share/config/kcmfonts"),
185: "[General]", "forceFontDPI");
186: dpi = Integer.parseInt(dpiParam);
187: } catch (Exception ex) {
188: }
189:
190: if (dpi < 0) {
191: dpi = 96;
192: }
193: if (dpi < 50) {
194: dpi = 50;
195: }
196: dcommonSize = ((commonSize * dpi) / 72.0);
197: dmenuSize = ((menuSize * dpi) / 72.0);
198: dtitleSize = ((titleSize * dpi) / 72.0);
199:
200: commonSize = (int) (dcommonSize + 0.5);
201: if (commonSize < 1) {
202: commonSize = 1;
203: }
204:
205: menuSize = (int) (dmenuSize + 0.5);
206: if (menuSize < 1) {
207: menuSize = 1;
208: }
209:
210: titleSize = (int) (dtitleSize + 0.5);
211: if (titleSize < 1) {
212: titleSize = 1;
213: }
214:
215: Font commonFont = new Font(commonFamily, commonStyle,
216: commonSize);
217: Font menuFont = new Font(menuFamily, menuStyle, menuSize);
218: Font titleFont = new Font(titleFamily, titleStyle,
219: titleSize);
220:
221: fontSet = FontSets.createDefaultFontSet(commonFont,
222: menuFont, commonFont, commonFont, commonFont,
223: titleFont);
224: }
225: return fontSet;
226: }
227:
228: private String getIniParam(String content, String category,
229: String param) throws Exception {
230: // checking if our param in our category - we don't need same params
231: // from other categories
232: int categoryIndex = content.indexOf(category);
233: int nextCategoryIndex = content.indexOf('[', categoryIndex + 1);
234: if (nextCategoryIndex < 0) {
235: nextCategoryIndex = content.length();
236: }
237: int paramIndex = content.indexOf(param, categoryIndex);
238: if (paramIndex >= 0 && paramIndex < nextCategoryIndex) {
239:
240: // now paramString contains full string of our parameter
241: String paramString = content
242: .substring(
243: paramIndex,
244: content.indexOf(System
245: .getProperty("line.separator"),
246: paramIndex)).trim();
247:
248: // getting just our value, we don't need other symbols
249: int equalityIndex = paramString.indexOf('=');
250: /*
251: * paramString.indexOf('#'); paramString.indexOf(';');
252: *
253: * don't know do we need to remove trailing comments after this
254: * symbols? have nothing similar in my system...
255: */
256: return paramString.substring(equalityIndex + 1);
257: }
258: throw new Exception("No such param in current category");
259: }
260:
261: private String getFileContent(String filePath) throws IOException {
262: String fileSeparator = System.getProperty("file.separator");
263: if (!"/".equals(fileSeparator)) {
264: filePath = filePath.replace("/", fileSeparator);
265: }
266:
267: // creating file from user home, using system's file separator:
268: BufferedReader in = new BufferedReader(new FileReader(System
269: .getProperty("user.home")
270: + fileSeparator + filePath));
271: StringBuilder sb = new StringBuilder();
272:
273: // size same as inside BufferedReader code
274: char[] buffer = new char[8192];
275: int read = 0;
276: while ((read = in.read(buffer)) >= 0) {
277: sb.append(buffer, 0, read);
278: }
279: in.close();
280:
281: return sb.toString();
282: }
283:
284: }
|