001: /*
002: * @(#)GdkGraphicsEnvironment.java 1.20 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: package sun.awt.gtk;
028:
029: import java.awt.Graphics2D;
030: import java.awt.GraphicsEnvironment;
031: import java.awt.GraphicsDevice;
032: import java.awt.image.BufferedImage;
033: import java.util.Locale;
034: import sun.io.FileIO;
035: import sun.io.FileIOFactory;
036: import sun.io.CharacterEncoding;
037: import java.io.InputStream;
038: import java.io.BufferedInputStream;
039: import java.util.Properties;
040: import java.util.List;
041: import java.util.ArrayList;
042: import java.util.Iterator;
043:
044: public class GdkGraphicsEnvironment extends GraphicsEnvironment {
045:
046: private static final String DEFAULT_NATIVE_FONT_NAME = "-adobe-courier-medium-r-normal--0-%d-0-0-m-0-iso8859-1";
047: private static String defaultNativeFontName = null;
048:
049: public GraphicsDevice getDefaultScreenDevice() {
050: return graphicsDevice;
051: }
052:
053: public GraphicsDevice[] getScreenDevices() {
054: return new GraphicsDevice[] { graphicsDevice };
055: }
056:
057: public String[] getAvailableFontFamilyNames() {
058:
059: /* Call the static version in GPlatformFont */
060:
061: return GPlatformFont.getAvailableFontFamilyNames();
062:
063: /*
064: List fontNames = new ArrayList();
065: Iterator fonts = defaultProperties.keySet().iterator();
066: int dotidx;
067: while (fonts.hasNext()) {
068: String fontName = (String) fonts.next();
069: if ((dotidx = fontName.indexOf('.')) == -1)
070: dotidx = fontName.length();
071: fontName = fontName.substring(0, dotidx);
072: if (!fontNames.contains(fontName))
073: fontNames.add(fontName);
074: }
075: if (!fontNames.contains("default"))
076: fontNames.add("default");
077: return (String[]) fontNames.toArray(new String[fontNames.size()]);
078: */
079: }
080:
081: public String[] getAvailableFontFamilyNames(Locale l) {
082:
083: /* FIXME... add the locale version to GPlatformFont */
084:
085: return getAvailableFontFamilyNames();
086: }
087:
088: /**
089: * Returns a <code>Graphics2D</code> object for rendering into the
090: * specified {@link BufferedImage}.
091: * @param img the specified <code>BufferedImage</code>
092: * @return a <code>Graphics2D</code> to be used for rendering into
093: * the specified <code>BufferedImage</code>.
094: */
095: public Graphics2D createGraphics(BufferedImage img) {
096: return img.createGraphics();
097: }
098:
099: /* Given the name and style of a font this method will detrmine the fontset name
100: that needs to be loaded by gdk_fontset_load. */
101:
102: static String getNativeFontName(String name, int style) {
103:
104: /* Call the static version in GPlatformFont */
105:
106: return GPlatformFont.getNativeFontName(name, style);
107:
108: /*
109: String nativeName;
110: name = name.toLowerCase();
111: nativeName = defaultProperties.getProperty(name + "." + style);
112: if (nativeName == null) {
113: // Check if plain version exists
114:
115: nativeName = defaultProperties.getProperty(name + ".0");
116:
117: // Get the default native font name from a system property if
118: // exists as a system property
119: if (nativeName == null) {
120: if (defaultNativeFontName == null) {
121: String fontNameTmp =
122: System.getProperty("j2me.pp.gtk.default.font");
123: if (fontNameTmp == null) {
124: defaultNativeFontName = DEFAULT_NATIVE_FONT_NAME;
125: }
126: else {
127: defaultNativeFontName = fontNameTmp;
128: }
129: }
130: nativeName = defaultNativeFontName;
131: }
132: }
133: return nativeName;
134:
135: */
136:
137: }
138:
139: private GdkGraphicsDevice graphicsDevice = new GdkGraphicsDevice();
140: /* The font propertiues used to map from font names to fonset names. */
141:
142: // private static Properties defaultProperties;
143: static {
144: // defaultProperties = new Properties();
145: // Ensure the lib is loaded.
146: java.awt.Toolkit.getDefaultToolkit();
147: /*
148: java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
149: public Object run() {
150: String jhome = System.getProperty("java.home");
151: String uhome = System.getProperty("user.home");
152: if (jhome == null) {
153: throw new Error("java.home property not set");
154: }
155: String language = System.getProperty("user.language", "en");
156: String region = System.getProperty("user.region");
157: // Translate the raw encoding name returned by the VM to the canonical
158: // name from the alias table in CharacterEncoding. Map unlisted raw
159: // encoding names to themselves. -bug 4163038
160:
161: String rawEncoding = System.getProperty("file.encoding");
162: String encoding = CharacterEncoding.aliasName(rawEncoding);
163: if (encoding == null)
164: encoding = rawEncoding;
165: try {
166: FileIO f = null;
167: if (region != null) {
168: f = tryOpeningFontProp(
169: f, uhome, language, region + "_" + encoding);
170: f = tryOpeningFontProp(
171: f, jhome, language, region + "_" + encoding);
172: f = tryOpeningFontProp(f, uhome, language, region);
173: f = tryOpeningFontProp(f, jhome, language, region);
174: }
175: f = tryOpeningFontProp(f, uhome, language, encoding);
176: f = tryOpeningFontProp(f, jhome, language, encoding);
177: f = tryOpeningFontProp(f, uhome, language, null);
178: f = tryOpeningFontProp(f, jhome, language, null);
179: f = tryOpeningFontProp(f, uhome, encoding, null);
180: f = tryOpeningFontProp(f, jhome, encoding, null);
181: f = tryOpeningFontProp(f, uhome, null, null);
182: f = tryOpeningFontProp(f, jhome, null, null);
183: // Load property file
184: InputStream in = new BufferedInputStream(f.getInputStream());
185: defaultProperties.load(in);
186: in.close();
187: } // If anything goes wrong then resort to default properties.
188: catch (Exception e) {
189: defaultProperties.put("serif.0", "-URW-Times-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
190: defaultProperties.put("serif.1", "-URW-Times-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
191: defaultProperties.put("serif.2", "-URW-Times-medium-i-normal--0-%d-0-0-p-0-iso8859-1");
192: defaultProperties.put("serif.3", "-URW-Times-bold-i-normal--0-%d-0-0-p-0-iso8859-1");
193: defaultProperties.put("sansserif.0", "-URW-Helvetica-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
194: defaultProperties.put("sansserif.1", "-URW-Helvetica-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
195: defaultProperties.put("sansserif.2", "-URW-Helvetica-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
196: defaultProperties.put("sansserif.3", "-URW-Helvetica-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
197: defaultProperties.put("monospaced.0", "-URW-Courier-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
198: defaultProperties.put("monospaced.1", "-URW-Courier-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
199: defaultProperties.put("monospaced.2", "-URW-Courier-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
200: defaultProperties.put("monospaced.3", "-URW-Courier-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
201: defaultProperties.put("dialog.0", "-URW-Helvetica-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
202: defaultProperties.put("dialog.1", "-URW-Helvetica-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
203: defaultProperties.put("dialog.2", "-URW-Helvetica-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
204: defaultProperties.put("dialog.3", "-URW-Helvetica-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
205: defaultProperties.put("dialoginput.0", "-URW-Courier-medium-r-normal--0-%d-0-0-p-0-iso8859-1");
206: defaultProperties.put("dialoginput.1", "-URW-Courier-bold-r-normal--0-%d-0-0-p-0-iso8859-1");
207: defaultProperties.put("dialoginput.2", "-URW-Courier-medium-o-normal--0-%d-0-0-p-0-iso8859-1");
208: defaultProperties.put("dialoginput.3", "-URW-Courier-bold-o-normal--0-%d-0-0-p-0-iso8859-1");
209: }
210: return null;
211: }
212: }
213: );
214: */
215:
216: }
217:
218: /*
219: private static FileIO tryOpeningFontProp(FileIO f, String homedir, String language, String ext) {
220: if (f != null)
221: return f; // already validated
222: String filename = homedir + FileIO.separator
223: + "lib" + FileIO.separator
224: + "font.properties";
225: if (language != null) {
226: filename += "." + language;
227: if (ext != null)
228: filename += "_" + ext;
229: }
230: FileIO propsFile = FileIOFactory.newInstance(filename);
231: if ((propsFile != null) && propsFile.canRead()) {
232: return propsFile;
233: }
234: return null;
235: }
236: */
237: }
|