Frames | No Frames |
1: /* ClasspathToolkit.java -- Abstract superclass for Classpath toolkits. 2: Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.java.awt; 40: 41: import gnu.java.awt.EmbeddedWindow; 42: import gnu.java.awt.peer.ClasspathFontPeer; 43: import gnu.java.awt.peer.EmbeddedWindowPeer; 44: import gnu.java.awt.peer.ClasspathTextLayoutPeer; 45: import gnu.java.security.action.SetAccessibleAction; 46: 47: import java.awt.AWTException; 48: import java.awt.Component; 49: import java.awt.Dimension; 50: import java.awt.DisplayMode; 51: import java.awt.Font; 52: import java.awt.FontMetrics; 53: import java.awt.GraphicsDevice; 54: import java.awt.GraphicsEnvironment; 55: import java.awt.Image; 56: import java.awt.Toolkit; 57: import java.awt.font.FontRenderContext; 58: import java.awt.image.ColorModel; 59: import java.awt.image.ImageProducer; 60: import java.awt.peer.RobotPeer; 61: import java.io.File; 62: import java.io.InputStream; 63: import java.lang.reflect.Constructor; 64: import java.lang.reflect.InvocationTargetException; 65: import java.net.MalformedURLException; 66: import java.net.URL; 67: import java.text.AttributedString; 68: import java.util.HashMap; 69: import java.util.Map; 70: import java.security.AccessController; 71: 72: import javax.imageio.spi.IIORegistry; 73: 74: /** 75: * An abstract superclass for Classpath toolkits. 76: * 77: * <p>There exist some parts of AWT and Java2D that are specific to 78: * the underlying platform, but for which the {@link Toolkit} class 79: * does not provide suitable abstractions. Examples include some 80: * methods of {@link Font} or {@link GraphicsEnvironment}. Those 81: * methods use ClasspathToolkit as a central place for obtaining 82: * platform-specific functionality. 83: * 84: * <p>In addition, ClasspathToolkit implements some abstract methods 85: * of {@link java.awt.Toolkit} that are not really platform-specific, 86: * such as the maintenance of a cache of loaded images. 87: * 88: * <p><b>Thread Safety:</b> The methods of this class may safely be 89: * called without external synchronization. This also hold for any 90: * inherited {@link Toolkit} methods. Subclasses are responsible for 91: * the necessary synchronization. 92: * 93: * @author Sascha Brawer (brawer@dandelis.ch) 94: */ 95: public abstract class ClasspathToolkit 96: extends Toolkit 97: { 98: /** 99: * Returns a shared instance of the local, platform-specific 100: * graphics environment. 101: * 102: * <p>This method is specific to GNU Classpath. It gets called by 103: * the Classpath implementation of {@link 104: * GraphicsEnvironment.getLocalGraphcisEnvironment()}. 105: */ 106: public abstract GraphicsEnvironment getLocalGraphicsEnvironment(); 107: 108: /** 109: * Acquires an appropriate {@link ClasspathFontPeer}, for use in 110: * classpath's implementation of {@link java.awt.Font}. 111: * 112: * @param name The logical name of the font. This may be either a face 113: * name or a logical font name, or may even be null. A default 114: * implementation of name decoding is provided in 115: * {@link ClasspathFontPeer}, but may be overridden in other toolkits. 116: * 117: * @param attrs Any extra {@link java.awt.font.TextAttribute} attributes 118: * this font peer should have, such as size, weight, family name, or 119: * transformation. 120: */ 121: public abstract ClasspathFontPeer getClasspathFontPeer (String name, Map attrs); 122: 123: public abstract ClasspathTextLayoutPeer 124: getClasspathTextLayoutPeer (AttributedString str, FontRenderContext frc); 125: 126: 127: /** 128: * Creates a {@link Font}, in a platform-specific manner. 129: * 130: * The default implementation simply constructs a {@link Font}, but some 131: * toolkits may wish to override this, to return {@link Font} subclasses 132: * which implement {@link java.awt.font.OpenType} or 133: * {@link java.awt.font.MultipleMaster}. 134: */ 135: public Font getFont (String name, Map attrs) 136: { 137: Font f = null; 138: 139: // Circumvent the package-privateness of the 140: // java.awt.Font.Font(String,Map) constructor. 141: try 142: { 143: Constructor fontConstructor = Font.class.getDeclaredConstructor 144: (new Class[] { String.class, Map.class }); 145: AccessController.doPrivileged 146: (new SetAccessibleAction(fontConstructor)); 147: f = (Font) fontConstructor.newInstance(new Object[] { name, attrs }); 148: } 149: catch (IllegalAccessException e) 150: { 151: throw new AssertionError(e); 152: } 153: catch (NoSuchMethodException e) 154: { 155: throw new AssertionError(e); 156: } 157: catch (InstantiationException e) 158: { 159: throw new AssertionError(e); 160: } 161: catch (InvocationTargetException e) 162: { 163: throw new AssertionError(e); 164: } 165: return f; 166: } 167: 168: /** 169: * Creates a font, reading the glyph definitions from a stream. 170: * 171: * <p>This method provides the platform-specific implementation for 172: * the static factory method {@link Font#createFont(int, 173: * java.io.InputStream)}. 174: * 175: * @param format the format of the font data, such as {@link 176: * Font#TRUETYPE_FONT}. An implementation may ignore this argument 177: * if it is able to automatically recognize the font format from the 178: * provided data. 179: * 180: * @param stream an input stream from where the font data is read 181: * in. The stream will be advanced to the position after the font 182: * data, but not closed. 183: * 184: * @throws IllegalArgumentException if <code>format</code> is 185: * not supported. 186: * 187: * @throws FontFormatException if <code>stream</code> does not 188: * contain data in the expected format, or if required tables are 189: * missing from a font. 190: * 191: * @throws IOException if a problem occurs while reading in the 192: * contents of <code>stream</code>. 193: */ 194: public abstract Font createFont(int format, InputStream stream); 195: 196: public abstract RobotPeer createRobot (GraphicsDevice screen) 197: throws AWTException; 198: 199: /** 200: * Creates an embedded window peer, and associates it with an 201: * EmbeddedWindow object. 202: * 203: * @param w The embedded window with which to associate a peer. 204: */ 205: public abstract EmbeddedWindowPeer createEmbeddedWindow (EmbeddedWindow w); 206: 207: /** 208: * Used to register ImageIO SPIs provided by the toolkit. 209: */ 210: public void registerImageIOSpis(IIORegistry reg) 211: { 212: } 213: }