Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/SunGraphicsEnvironment.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d;2627import java.awt.AWTError;28import java.awt.Color;29import java.awt.Font;30import java.awt.Graphics2D;31import java.awt.GraphicsConfiguration;32import java.awt.GraphicsDevice;33import java.awt.GraphicsEnvironment;34import java.awt.Insets;35import java.awt.Rectangle;36import java.awt.Toolkit;37import java.awt.font.TextAttribute;38import java.awt.image.BufferedImage;39import java.awt.peer.ComponentPeer;40import java.io.BufferedReader;41import java.io.File;42import java.io.FileInputStream;43import java.io.FilenameFilter;44import java.io.InputStreamReader;45import java.io.IOException;46import java.text.AttributedCharacterIterator;47import java.util.ArrayList;48import java.util.HashSet;49import java.util.Iterator;50import java.util.Locale;51import java.util.Map;52import java.util.NoSuchElementException;53import java.util.Set;54import java.util.StringTokenizer;55import java.util.TreeMap;56import java.util.Vector;57import java.util.concurrent.ConcurrentHashMap;58import sun.awt.AppContext;59import sun.awt.DisplayChangedListener;60import sun.awt.FontConfiguration;61import sun.awt.SunDisplayChanger;62import sun.font.CompositeFontDescriptor;63import sun.font.Font2D;64import sun.font.FontManager;65import sun.font.FontManagerFactory;66import sun.font.FontManagerForSGE;67import sun.font.NativeFont;6869/**70* This is an implementation of a GraphicsEnvironment object for the71* default local GraphicsEnvironment.72*73* @see GraphicsDevice74* @see GraphicsConfiguration75*/76public abstract class SunGraphicsEnvironment extends GraphicsEnvironment77implements DisplayChangedListener {7879public static boolean isOpenSolaris;80private static Font defaultFont;8182public SunGraphicsEnvironment() {83java.security.AccessController.doPrivileged(84new java.security.PrivilegedAction() {85public Object run() {86String version = System.getProperty("os.version", "0.0");87try {88float ver = Float.parseFloat(version);89if (ver > 5.10f) {90File f = new File("/etc/release");91FileInputStream fis = new FileInputStream(f);92InputStreamReader isr93= new InputStreamReader(fis, "ISO-8859-1");94BufferedReader br = new BufferedReader(isr);95String line = br.readLine();96if (line.indexOf("OpenSolaris") >= 0) {97isOpenSolaris = true;98} else {99/* We are using isOpenSolaris as meaning100* we know the Solaris commercial fonts aren't101* present. "Solaris Next" (03/10) did not102* include these even though its was not103* OpenSolaris. Need to revisit how this is104* handled but for now as in 6ux, we'll use105* the test for a standard font resource as106* being an indicator as to whether we need107* to treat this as OpenSolaris from a font108* config perspective.109*/110String courierNew =111"/usr/openwin/lib/X11/fonts/TrueType/CourierNew.ttf";112File courierFile = new File(courierNew);113isOpenSolaris = !courierFile.exists();114}115fis.close();116}117} catch (Exception e) {118}119120/* Establish the default font to be used by SG2D etc */121defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12);122123return null;124}125});126}127128protected GraphicsDevice[] screens;129130/**131* Returns an array of all of the screen devices.132*/133public synchronized GraphicsDevice[] getScreenDevices() {134GraphicsDevice[] ret = screens;135if (ret == null) {136int num = getNumScreens();137ret = new GraphicsDevice[num];138for (int i = 0; i < num; i++) {139ret[i] = makeScreenDevice(i);140}141screens = ret;142}143return ret;144}145146/**147* Returns the number of screen devices of this graphics environment.148*149* @return the number of screen devices of this graphics environment150*/151protected abstract int getNumScreens();152153/**154* Create and return the screen device with the specified number. The155* device with number <code>0</code> will be the default device (returned156* by {@link #getDefaultScreenDevice()}.157*158* @param screennum the number of the screen to create159*160* @return the created screen device161*/162protected abstract GraphicsDevice makeScreenDevice(int screennum);163164/**165* Returns the default screen graphics device.166*/167public GraphicsDevice getDefaultScreenDevice() {168GraphicsDevice[] screens = getScreenDevices();169if (screens.length == 0) {170throw new AWTError("no screen devices");171}172return screens[0];173}174175/**176* Returns a Graphics2D object for rendering into the177* given BufferedImage.178* @throws NullPointerException if BufferedImage argument is null179*/180public Graphics2D createGraphics(BufferedImage img) {181if (img == null) {182throw new NullPointerException("BufferedImage cannot be null");183}184SurfaceData sd = SurfaceData.getPrimarySurfaceData(img);185return new SunGraphics2D(sd, Color.white, Color.black, defaultFont);186}187188public static FontManagerForSGE getFontManagerForSGE() {189FontManager fm = FontManagerFactory.getInstance();190return (FontManagerForSGE) fm;191}192193/* Modifies the behaviour of a subsequent call to preferLocaleFonts()194* to use Mincho instead of Gothic for dialoginput in JA locales195* on windows. Not needed on other platforms.196*197* DO NOT MOVE OR RENAME OR OTHERWISE ALTER THIS METHOD.198* ITS USED BY SOME NON-JRE INTERNAL CODE.199*/200public static void useAlternateFontforJALocales() {201getFontManagerForSGE().useAlternateFontforJALocales();202}203204/**205* Returns all fonts available in this environment.206*/207public Font[] getAllFonts() {208FontManagerForSGE fm = getFontManagerForSGE();209Font[] installedFonts = fm.getAllInstalledFonts();210Font[] created = fm.getCreatedFonts();211if (created == null || created.length == 0) {212return installedFonts;213} else {214int newlen = installedFonts.length + created.length;215Font [] fonts = java.util.Arrays.copyOf(installedFonts, newlen);216System.arraycopy(created, 0, fonts,217installedFonts.length, created.length);218return fonts;219}220}221222public String[] getAvailableFontFamilyNames(Locale requestedLocale) {223FontManagerForSGE fm = getFontManagerForSGE();224String[] installed = fm.getInstalledFontFamilyNames(requestedLocale);225/* Use a new TreeMap as used in getInstalledFontFamilyNames226* and insert all the keys in lower case, so that the sort order227* is the same as the installed families. This preserves historical228* behaviour and inserts new families in the right place.229* It would have been marginally more efficient to directly obtain230* the tree map and just insert new entries, but not so much as231* to justify the extra internal interface.232*/233TreeMap<String, String> map = fm.getCreatedFontFamilyNames();234if (map == null || map.size() == 0) {235return installed;236} else {237for (int i=0; i<installed.length; i++) {238map.put(installed[i].toLowerCase(requestedLocale),239installed[i]);240}241String[] retval = new String[map.size()];242Object [] keyNames = map.keySet().toArray();243for (int i=0; i < keyNames.length; i++) {244retval[i] = (String)map.get(keyNames[i]);245}246return retval;247}248}249250public String[] getAvailableFontFamilyNames() {251return getAvailableFontFamilyNames(Locale.getDefault());252}253254/**255* Return the bounds of a GraphicsDevice, less its screen insets.256* See also java.awt.GraphicsEnvironment.getUsableBounds();257*/258public static Rectangle getUsableBounds(GraphicsDevice gd) {259GraphicsConfiguration gc = gd.getDefaultConfiguration();260Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);261Rectangle usableBounds = gc.getBounds();262263usableBounds.x += insets.left;264usableBounds.y += insets.top;265usableBounds.width -= (insets.left + insets.right);266usableBounds.height -= (insets.top + insets.bottom);267268return usableBounds;269}270271/**272* From the DisplayChangedListener interface; called273* when the display mode has been changed.274*/275public void displayChanged() {276// notify screens in device array to do display update stuff277for (GraphicsDevice gd : getScreenDevices()) {278if (gd instanceof DisplayChangedListener) {279((DisplayChangedListener) gd).displayChanged();280}281}282283// notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and284// SurfaceDataProxies) about the display change event285displayChanger.notifyListeners();286}287288/**289* Part of the DisplayChangedListener interface:290* propagate this event to listeners291*/292public void paletteChanged() {293displayChanger.notifyPaletteChanged();294}295296/**297* Returns true when the display is local, false for remote displays.298*299* @return true when the display is local, false for remote displays300*/301public abstract boolean isDisplayLocal();302303/*304* ----DISPLAY CHANGE SUPPORT----305*/306307protected SunDisplayChanger displayChanger = new SunDisplayChanger();308309/**310* Add a DisplayChangeListener to be notified when the display settings311* are changed.312*/313public void addDisplayChangedListener(DisplayChangedListener client) {314displayChanger.add(client);315}316317/**318* Remove a DisplayChangeListener from Win32GraphicsEnvironment319*/320public void removeDisplayChangedListener(DisplayChangedListener client) {321displayChanger.remove(client);322}323324/*325* ----END DISPLAY CHANGE SUPPORT----326*/327328/**329* Returns true if FlipBufferStrategy with COPIED buffer contents330* is preferred for this peer's GraphicsConfiguration over331* BlitBufferStrategy, false otherwise.332*333* The reason FlipBS could be preferred is that in some configurations334* an accelerated copy to the screen is supported (like Direct3D 9)335*336* @return true if flip strategy should be used, false otherwise337*/338public boolean isFlipStrategyPreferred(ComponentPeer peer) {339return false;340}341}342343344