SunGraphicsEnvironment.java revision 3441
893N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
893N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
893N/A * or visit www.oracle.com if you need additional information or have any
893N/A * questions.
893N/A */
893N/A
893N/Apackage sun.java2d;
893N/A
893N/Aimport java.awt.Color;
893N/Aimport java.awt.Font;
893N/Aimport java.awt.Graphics2D;
893N/Aimport java.awt.GraphicsConfiguration;
893N/Aimport java.awt.GraphicsDevice;
893N/Aimport java.awt.GraphicsEnvironment;
893N/Aimport java.awt.Insets;
893N/Aimport java.awt.Rectangle;
893N/Aimport java.awt.Toolkit;
893N/Aimport java.awt.font.TextAttribute;
893N/Aimport java.awt.image.BufferedImage;
893N/Aimport java.awt.peer.ComponentPeer;
893N/Aimport java.io.BufferedReader;
893N/Aimport java.io.File;
893N/Aimport java.io.FileInputStream;
893N/Aimport java.io.FilenameFilter;
893N/Aimport java.io.InputStreamReader;
893N/Aimport java.io.IOException;
893N/Aimport java.text.AttributedCharacterIterator;
893N/Aimport java.util.ArrayList;
893N/Aimport java.util.HashSet;
893N/Aimport java.util.Iterator;
893N/Aimport java.util.Locale;
893N/Aimport java.util.Map;
893N/Aimport java.util.NoSuchElementException;
893N/Aimport java.util.Set;
893N/Aimport java.util.StringTokenizer;
893N/Aimport java.util.TreeMap;
893N/Aimport java.util.Vector;
893N/Aimport java.util.concurrent.ConcurrentHashMap;
893N/Aimport sun.awt.AppContext;
893N/Aimport sun.awt.DisplayChangedListener;
893N/Aimport sun.awt.FontConfiguration;
893N/Aimport sun.awt.SunDisplayChanger;
893N/Aimport sun.font.CompositeFontDescriptor;
893N/Aimport sun.font.Font2D;
893N/Aimport sun.font.FontManager;
893N/Aimport sun.font.FontManagerFactory;
893N/Aimport sun.font.FontManagerForSGE;
893N/Aimport sun.font.NativeFont;
893N/A
893N/A/**
893N/A * This is an implementation of a GraphicsEnvironment object for the
893N/A * default local GraphicsEnvironment.
893N/A *
893N/A * @see GraphicsDevice
893N/A * @see GraphicsConfiguration
893N/A */
893N/Apublic abstract class SunGraphicsEnvironment extends GraphicsEnvironment
893N/A implements DisplayChangedListener {
893N/A
893N/A public static boolean isOpenSolaris;
893N/A private static Font defaultFont;
893N/A
893N/A public SunGraphicsEnvironment() {
893N/A java.security.AccessController.doPrivileged(
893N/A new java.security.PrivilegedAction() {
893N/A public Object run() {
893N/A String version = System.getProperty("os.version", "0.0");
893N/A try {
893N/A float ver = Float.parseFloat(version);
893N/A if (ver > 5.10f) {
893N/A File f = new File("/etc/release");
893N/A FileInputStream fis = new FileInputStream(f);
893N/A InputStreamReader isr
893N/A = new InputStreamReader(fis, "ISO-8859-1");
893N/A BufferedReader br = new BufferedReader(isr);
893N/A String line = br.readLine();
893N/A if (line.indexOf("OpenSolaris") >= 0) {
893N/A isOpenSolaris = true;
893N/A } else {
893N/A /* We are using isOpenSolaris as meaning
893N/A * we know the Solaris commercial fonts aren't
893N/A * present. "Solaris Next" (03/10) did not
893N/A * include these even though its was not
893N/A * OpenSolaris. Need to revisit how this is
893N/A * handled but for now as in 6ux, we'll use
893N/A * the test for a standard font resource as
893N/A * being an indicator as to whether we need
893N/A * to treat this as OpenSolaris from a font
893N/A * config perspective.
893N/A */
893N/A String courierNew =
893N/A "/usr/openwin/lib/X11/fonts/TrueType/CourierNew.ttf";
893N/A File courierFile = new File(courierNew);
893N/A isOpenSolaris = !courierFile.exists();
893N/A }
893N/A fis.close();
893N/A }
893N/A } catch (Exception e) {
893N/A }
893N/A
893N/A /* Establish the default font to be used by SG2D etc */
893N/A defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12);
893N/A
893N/A return null;
893N/A }
893N/A });
893N/A }
893N/A
893N/A protected GraphicsDevice[] screens;
893N/A
893N/A /**
893N/A * Returns an array of all of the screen devices.
893N/A */
893N/A public synchronized GraphicsDevice[] getScreenDevices() {
893N/A GraphicsDevice[] ret = screens;
893N/A if (ret == null) {
893N/A int num = getNumScreens();
893N/A ret = new GraphicsDevice[num];
893N/A for (int i = 0; i < num; i++) {
893N/A ret[i] = makeScreenDevice(i);
893N/A }
893N/A screens = ret;
893N/A }
893N/A return ret;
893N/A }
893N/A
893N/A /**
893N/A * Returns the number of screen devices of this graphics environment.
893N/A *
893N/A * @return the number of screen devices of this graphics environment
893N/A */
893N/A protected abstract int getNumScreens();
893N/A
893N/A /**
893N/A * Create and return the screen device with the specified number. The
893N/A * device with number <code>0</code> will be the default device (returned
893N/A * by {@link #getDefaultScreenDevice()}.
893N/A *
893N/A * @param screennum the number of the screen to create
893N/A *
893N/A * @return the created screen device
893N/A */
893N/A protected abstract GraphicsDevice makeScreenDevice(int screennum);
893N/A
893N/A /**
893N/A * Returns the default screen graphics device.
893N/A */
893N/A public GraphicsDevice getDefaultScreenDevice() {
893N/A return getScreenDevices()[0];
893N/A }
893N/A
893N/A /**
893N/A * Returns a Graphics2D object for rendering into the
893N/A * given BufferedImage.
893N/A * @throws NullPointerException if BufferedImage argument is null
893N/A */
893N/A public Graphics2D createGraphics(BufferedImage img) {
893N/A if (img == null) {
893N/A throw new NullPointerException("BufferedImage cannot be null");
893N/A }
893N/A SurfaceData sd = SurfaceData.getPrimarySurfaceData(img);
893N/A return new SunGraphics2D(sd, Color.white, Color.black, defaultFont);
893N/A }
893N/A
893N/A public static FontManagerForSGE getFontManagerForSGE() {
893N/A FontManager fm = FontManagerFactory.getInstance();
893N/A return (FontManagerForSGE) fm;
893N/A }
893N/A /**
893N/A * Returns all fonts available in this environment.
893N/A */
893N/A public Font[] getAllFonts() {
893N/A FontManagerForSGE fm = getFontManagerForSGE();
893N/A Font[] installedFonts = fm.getAllInstalledFonts();
893N/A Font[] created = fm.getCreatedFonts();
893N/A if (created == null || created.length == 0) {
893N/A return installedFonts;
893N/A } else {
893N/A int newlen = installedFonts.length + created.length;
893N/A Font [] fonts = java.util.Arrays.copyOf(installedFonts, newlen);
893N/A System.arraycopy(created, 0, fonts,
893N/A installedFonts.length, created.length);
893N/A return fonts;
893N/A }
893N/A }
893N/A
893N/A public String[] getAvailableFontFamilyNames(Locale requestedLocale) {
893N/A FontManagerForSGE fm = getFontManagerForSGE();
893N/A String[] installed = fm.getInstalledFontFamilyNames(requestedLocale);
893N/A /* Use a new TreeMap as used in getInstalledFontFamilyNames
893N/A * and insert all the keys in lower case, so that the sort order
893N/A * is the same as the installed families. This preserves historical
893N/A * behaviour and inserts new families in the right place.
893N/A * It would have been marginally more efficient to directly obtain
893N/A * the tree map and just insert new entries, but not so much as
893N/A * to justify the extra internal interface.
893N/A */
893N/A TreeMap<String, String> map = fm.getCreatedFontFamilyNames();
893N/A if (map == null || map.size() == 0) {
893N/A return installed;
893N/A } else {
893N/A for (int i=0; i<installed.length; i++) {
893N/A map.put(installed[i].toLowerCase(requestedLocale),
893N/A installed[i]);
893N/A }
893N/A String[] retval = new String[map.size()];
893N/A Object [] keyNames = map.keySet().toArray();
893N/A for (int i=0; i < keyNames.length; i++) {
893N/A retval[i] = (String)map.get(keyNames[i]);
893N/A }
893N/A return retval;
893N/A }
893N/A }
893N/A
893N/A public String[] getAvailableFontFamilyNames() {
893N/A return getAvailableFontFamilyNames(Locale.getDefault());
893N/A }
893N/A
893N/A /**
893N/A * Return the bounds of a GraphicsDevice, less its screen insets.
893N/A * See also java.awt.GraphicsEnvironment.getUsableBounds();
893N/A */
893N/A public static Rectangle getUsableBounds(GraphicsDevice gd) {
893N/A GraphicsConfiguration gc = gd.getDefaultConfiguration();
893N/A Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
893N/A Rectangle usableBounds = gc.getBounds();
893N/A
893N/A usableBounds.x += insets.left;
893N/A usableBounds.y += insets.top;
893N/A usableBounds.width -= (insets.left + insets.right);
893N/A usableBounds.height -= (insets.top + insets.bottom);
893N/A
893N/A return usableBounds;
893N/A }
893N/A
893N/A /**
893N/A * From the DisplayChangedListener interface; called
893N/A * when the display mode has been changed.
893N/A */
893N/A public void displayChanged() {
// notify screens in device array to do display update stuff
for (GraphicsDevice gd : getScreenDevices()) {
if (gd instanceof DisplayChangedListener) {
((DisplayChangedListener) gd).displayChanged();
}
}
// notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and
// SurfaceDataProxies) about the display change event
displayChanger.notifyListeners();
}
/**
* Part of the DisplayChangedListener interface:
* propagate this event to listeners
*/
public void paletteChanged() {
displayChanger.notifyPaletteChanged();
}
/**
* Returns true when the display is local, false for remote displays.
*
* @return true when the display is local, false for remote displays
*/
public abstract boolean isDisplayLocal();
/*
* ----DISPLAY CHANGE SUPPORT----
*/
protected SunDisplayChanger displayChanger = new SunDisplayChanger();
/**
* Add a DisplayChangeListener to be notified when the display settings
* are changed.
*/
public void addDisplayChangedListener(DisplayChangedListener client) {
displayChanger.add(client);
}
/**
* Remove a DisplayChangeListener from Win32GraphicsEnvironment
*/
public void removeDisplayChangedListener(DisplayChangedListener client) {
displayChanger.remove(client);
}
/*
* ----END DISPLAY CHANGE SUPPORT----
*/
/**
* Returns true if FlipBufferStrategy with COPIED buffer contents
* is preferred for this peer's GraphicsConfiguration over
* BlitBufferStrategy, false otherwise.
*
* The reason FlipBS could be preferred is that in some configurations
* an accelerated copy to the screen is supported (like Direct3D 9)
*
* @return true if flip strategy should be used, false otherwise
*/
public boolean isFlipStrategyPreferred(ComponentPeer peer) {
return false;
}
}