001: /*
002: * @(#)QtGraphicsEnvironment.java 1.9 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.qt;
029:
030: import java.awt.Graphics2D;
031: import java.awt.GraphicsEnvironment;
032: import java.awt.GraphicsDevice;
033: import java.awt.image.BufferedImage;
034: import java.util.Locale;
035: import sun.io.FileIO;
036: import sun.io.FileIOFactory;
037: import sun.io.CharacterEncoding;
038: import java.io.InputStream;
039: import java.io.BufferedInputStream;
040: import java.util.Properties;
041: import java.util.List;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044:
045: public class QtGraphicsEnvironment extends GraphicsEnvironment {
046: private static final String DEFAULT_NATIVE_FONT_NAME = "-adobe-courier-medium-r-normal--0-%d-0-0-m-0-iso8859-1";
047:
048: // GTech-port (Bug #5078958)
049: private static final String[] hardwiredFontList = { "Dialog",
050: "SansSerif", "Serif", "Monospaced", "DialogInput" };
051:
052: private static String[] availableFontFamilyNames;
053:
054: // GTech-port (Bug #5078958)
055:
056: public GraphicsDevice getDefaultScreenDevice() {
057: return graphicsDevice;
058: }
059:
060: public GraphicsDevice[] getScreenDevices() {
061: return new GraphicsDevice[] { graphicsDevice };
062: }
063:
064: // GTech-port (Bug #5078958)
065: public String[] getAvailableFontFamilyNames() {
066: // Should we not create a copy here ??? gtech port
067: // does not seem to do and we follow the same lines.
068: return availableFontFamilyNames;
069: }
070:
071: private static String[] getAvailableFontFamilyNamesInit() {
072: List fontNames = new ArrayList();
073: Iterator fonts = defaultProperties.keySet().iterator();
074: int dotidx;
075: while (fonts.hasNext()) {
076: String fontName = (String) fonts.next();
077: if ((dotidx = fontName.indexOf('.')) == -1)
078: dotidx = fontName.length();
079: fontName = fontName.substring(0, dotidx);
080: for (int i = 0; i < hardwiredFontList.length; i++) {
081: if ((hardwiredFontList[i].toLowerCase())
082: .equals(fontName.toLowerCase())) {
083: if (!fontNames.contains(fontName))
084: fontNames.add(fontName);
085: }
086: }
087: }
088: if (!fontNames.contains("default"))
089: fontNames.add("default");
090: return (String[]) fontNames
091: .toArray(new String[fontNames.size()]);
092: }
093:
094: // GTech-port (Bug #5078958)
095:
096: public String[] getAvailableFontFamilyNames(Locale l) {
097: return getAvailableFontFamilyNames();
098: }
099:
100: /**
101: * Returns a <code>Graphics2D</code> object for rendering into the
102: * specified {@link BufferedImage}.
103: * @param img the specified <code>BufferedImage</code>
104: * @return a <code>Graphics2D</code> to be used for rendering into
105: * the specified <code>BufferedImage</code>.
106: */
107: public Graphics2D createGraphics(BufferedImage img) {
108: return img.createGraphics();
109: }
110:
111: /* Given the name and style of a font this method will determine
112: the fontset name that needs to be loaded by gdk_fontset_load. */
113:
114: static String getNativeFontName(String name, int style) {
115: String nativeName;
116:
117: name = name.toLowerCase();
118: nativeName = defaultProperties.getProperty(name + "." + style);
119:
120: if (nativeName == null) {
121: // Check if plain version exists
122:
123: nativeName = defaultProperties.getProperty(name + ".0");
124:
125: if (nativeName == null)
126: nativeName = DEFAULT_NATIVE_FONT_NAME;
127: }
128:
129: return nativeName;
130: }
131:
132: private QtGraphicsDevice graphicsDevice = new QtGraphicsDevice();
133:
134: /* The font propertiues used to map from font names to fontset names. */
135:
136: private static Properties defaultProperties;
137:
138: static {
139: defaultProperties = new Properties();
140:
141: // Ensure the lib is loaded.
142: java.awt.Toolkit.getDefaultToolkit();
143:
144: java.security.AccessController
145: .doPrivileged(new java.security.PrivilegedAction() {
146: public Object run() {
147: String jhome = System.getProperty("java.home");
148: String uhome = System.getProperty("user.home");
149:
150: if (jhome == null) {
151: throw new Error(
152: "java.home property not set");
153: }
154:
155: String language = System.getProperty(
156: "user.language", "en");
157: String region = System
158: .getProperty("user.region");
159:
160: // Translate the raw encoding name returned by the VM to the canonical
161: // name from the alias table in CharacterEncoding. Map unlisted raw
162: // encoding names to themselves. - bug 4163038
163:
164: String rawEncoding = System
165: .getProperty("file.encoding");
166: String encoding = CharacterEncoding
167: .aliasName(rawEncoding);
168:
169: if (encoding == null)
170: encoding = rawEncoding;
171:
172: try {
173: FileIO f = null;
174:
175: if (region != null) {
176: f = tryOpeningFontProp(f, uhome,
177: language, region + "_"
178: + encoding);
179: f = tryOpeningFontProp(f, jhome,
180: language, region + "_"
181: + encoding);
182: f = tryOpeningFontProp(f, uhome,
183: language, region);
184: f = tryOpeningFontProp(f, jhome,
185: language, region);
186: }
187:
188: f = tryOpeningFontProp(f, uhome, language,
189: encoding);
190: f = tryOpeningFontProp(f, jhome, language,
191: encoding);
192: f = tryOpeningFontProp(f, uhome, language,
193: null);
194: f = tryOpeningFontProp(f, jhome, language,
195: null);
196: f = tryOpeningFontProp(f, uhome, encoding,
197: null);
198: f = tryOpeningFontProp(f, jhome, encoding,
199: null);
200: f = tryOpeningFontProp(f, uhome, null, null);
201: f = tryOpeningFontProp(f, jhome, null, null);
202:
203: // Load property file
204: InputStream in = new BufferedInputStream(f
205: .getInputStream());
206:
207: defaultProperties.load(in);
208: in.close();
209: } // If anything goes wrong then resort to default properties.
210: catch (Exception e) {
211: defaultProperties
212: .put("serif.0",
213: "-URW-Times-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
214: defaultProperties
215: .put("serif.1",
216: "-URW-Times-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
217: defaultProperties
218: .put("serif.2",
219: "-URW-Times-medium-i-normal--0-%d-0-0-p-0-iso8859-1");
220: defaultProperties
221: .put("serif.3",
222: "-URW-Times-bold-i-normal--0-%d-0-0-p-0-iso8859-1");
223:
224: defaultProperties
225: .put("sansserif.0",
226: "-URW-Helvetica-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
227: defaultProperties
228: .put("sansserif.1",
229: "-URW-Helvetica-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
230: defaultProperties
231: .put("sansserif.2",
232: "-URW-Helvetica-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
233: defaultProperties
234: .put("sansserif.3",
235: "-URW-Helvetica-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
236:
237: defaultProperties
238: .put("monospaced.0",
239: "-URW-Courier-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
240: defaultProperties
241: .put("monospaced.1",
242: "-URW-Courier-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
243: defaultProperties
244: .put("monospaced.2",
245: "-URW-Courier-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
246: defaultProperties
247: .put("monospaced.3",
248: "-URW-Courier-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
249:
250: defaultProperties
251: .put("dialog.0",
252: "-URW-Helvetica-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
253: defaultProperties
254: .put("dialog.1",
255: "-URW-Helvetica-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
256: defaultProperties
257: .put("dialog.2",
258: "-URW-Helvetica-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
259: defaultProperties
260: .put("dialog.3",
261: "-URW-Helvetica-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
262:
263: defaultProperties
264: .put("dialoginput.0",
265: "-URW-Courier-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
266: defaultProperties
267: .put("dialoginput.1",
268: "-URW-Courier-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
269: defaultProperties
270: .put("dialoginput.2",
271: "-URW-Courier-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
272: defaultProperties
273: .put("dialoginput.3",
274: "-URW-Courier-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
275: }
276:
277: // GTech-port (Bug #5078958)
278: availableFontFamilyNames = getAvailableFontFamilyNamesInit();
279: // GTech-port (Bug #5078958)
280:
281: return null;
282: }
283: });
284: }
285:
286: private static FileIO tryOpeningFontProp(FileIO f, String homedir,
287: String language, String ext) {
288: if (f != null)
289: return f; // already validated
290:
291: String filename = homedir + FileIO.separator + "lib"
292: + FileIO.separator + "font.properties";
293:
294: if (language != null) {
295: filename += "." + language;
296:
297: if (ext != null)
298: filename += "_" + ext;
299: }
300:
301: FileIO propsFile = FileIOFactory.newInstance(filename);
302:
303: if ((propsFile != null) && propsFile.canRead()) {
304: return propsFile;
305: }
306:
307: return null;
308: }
309: }
|