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