1 /*
   2  * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d;
  27 
  28 import java.awt.Color;
  29 import java.awt.Font;
  30 import java.awt.Graphics2D;
  31 import java.awt.GraphicsConfiguration;
  32 import java.awt.GraphicsDevice;
  33 import java.awt.GraphicsEnvironment;
  34 import java.awt.Insets;
  35 import java.awt.Rectangle;
  36 import java.awt.Toolkit;
  37 import java.awt.font.TextAttribute;
  38 import java.awt.image.BufferedImage;
  39 import java.awt.peer.ComponentPeer;
  40 import java.io.BufferedReader;
  41 import java.io.File;
  42 import java.io.FileInputStream;
  43 import java.io.FilenameFilter;
  44 import java.io.InputStreamReader;
  45 import java.io.IOException;
  46 import java.text.AttributedCharacterIterator;
  47 import java.util.ArrayList;
  48 import java.util.HashSet;
  49 import java.util.Iterator;
  50 import java.util.Locale;
  51 import java.util.Map;
  52 import java.util.NoSuchElementException;
  53 import java.util.Set;
  54 import java.util.StringTokenizer;
  55 import java.util.TreeMap;
  56 import java.util.Vector;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import sun.awt.AppContext;
  59 import sun.awt.DisplayChangedListener;
  60 import sun.awt.FontConfiguration;
  61 import sun.awt.SunDisplayChanger;
  62 import sun.font.CompositeFontDescriptor;
  63 import sun.font.Font2D;
  64 import sun.font.FontManager;
  65 import sun.font.FontManagerFactory;
  66 import sun.font.FontManagerForSGE;
  67 import sun.font.NativeFont;
  68 
  69 /**
  70  * This is an implementation of a GraphicsEnvironment object for the
  71  * default local GraphicsEnvironment.
  72  *
  73  * @see GraphicsDevice
  74  * @see GraphicsConfiguration
  75  */
  76 public abstract class SunGraphicsEnvironment extends GraphicsEnvironment
  77     implements DisplayChangedListener {
  78 
  79     public static boolean isOpenSolaris;
  80     private static Font defaultFont;
  81 
  82     public SunGraphicsEnvironment() {
  83         java.security.AccessController.doPrivileged(
  84                                     new java.security.PrivilegedAction() {
  85             public Object run() {
  86                     String version = System.getProperty("os.version", "0.0");
  87                     try {
  88                         float ver = Float.parseFloat(version);
  89                         if (ver > 5.10f) {
  90                             File f = new File("/etc/release");
  91                             FileInputStream fis = new FileInputStream(f);
  92                             InputStreamReader isr
  93                                 = new InputStreamReader(fis, "ISO-8859-1");
  94                             BufferedReader br = new BufferedReader(isr);
  95                             String line = br.readLine();
  96                             if (line.indexOf("OpenSolaris") >= 0) {
  97                                 isOpenSolaris = true;
  98                             } else {
  99                                 /* We are using isOpenSolaris as meaning
 100                                  * we know the Solaris commercial fonts aren't
 101                                  * present. "Solaris Next" (03/10) did not
 102                                  * include these even though its was not
 103                                  * OpenSolaris. Need to revisit how this is
 104                                  * handled but for now as in 6ux, we'll use
 105                                  * the test for a standard font resource as
 106                                  * being an indicator as to whether we need
 107                                  * to treat this as OpenSolaris from a font
 108                                  * config perspective.
 109                                  */
 110                                 String courierNew =
 111                                     "/usr/openwin/lib/X11/fonts/TrueType/CourierNew.ttf";
 112                                 File courierFile = new File(courierNew);
 113                                 isOpenSolaris = !courierFile.exists();
 114                             }
 115                             fis.close();
 116                         }
 117                     } catch (Exception e) {
 118                     }
 119 
 120                 /* Establish the default font to be used by SG2D etc */
 121                 defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12);
 122 
 123                 return null;
 124             }
 125         });
 126     }
 127 
 128     protected GraphicsDevice[] screens;
 129 
 130     /**
 131      * Returns an array of all of the screen devices.
 132      */
 133     public synchronized GraphicsDevice[] getScreenDevices() {
 134         GraphicsDevice[] ret = screens;
 135         if (ret == null) {
 136             int num = getNumScreens();
 137             ret = new GraphicsDevice[num];
 138             for (int i = 0; i < num; i++) {
 139                 ret[i] = makeScreenDevice(i);
 140             }
 141             screens = ret;
 142         }
 143         return ret;
 144     }
 145 
 146     /**
 147      * Returns the number of screen devices of this graphics environment.
 148      *
 149      * @return the number of screen devices of this graphics environment
 150      */
 151     protected abstract int getNumScreens();
 152 
 153     /**
 154      * Create and return the screen device with the specified number. The
 155      * device with number <code>0</code> will be the default device (returned
 156      * by {@link #getDefaultScreenDevice()}.
 157      *
 158      * @param screennum the number of the screen to create
 159      *
 160      * @return the created screen device
 161      */
 162     protected abstract GraphicsDevice makeScreenDevice(int screennum);
 163 
 164     /**
 165      * Returns the default screen graphics device.
 166      */
 167     public GraphicsDevice getDefaultScreenDevice() {
 168         return getScreenDevices()[0];
 169     }
 170 
 171     /**
 172      * Returns a Graphics2D object for rendering into the
 173      * given BufferedImage.
 174      * @throws NullPointerException if BufferedImage argument is null
 175      */
 176     public Graphics2D createGraphics(BufferedImage img) {
 177         if (img == null) {
 178             throw new NullPointerException("BufferedImage cannot be null");
 179         }
 180         SurfaceData sd = SurfaceData.getPrimarySurfaceData(img);
 181         return new SunGraphics2D(sd, Color.white, Color.black, defaultFont);
 182     }
 183 
 184     public static FontManagerForSGE getFontManagerForSGE() {
 185         FontManager fm = FontManagerFactory.getInstance();
 186         return (FontManagerForSGE) fm;
 187     }
 188      /**
 189      * Returns all fonts available in this environment.
 190      */
 191     public Font[] getAllFonts() {
 192         FontManagerForSGE fm = getFontManagerForSGE();
 193         Font[] installedFonts = fm.getAllInstalledFonts();
 194         Font[] created = fm.getCreatedFonts();
 195         if (created == null || created.length == 0) {
 196             return installedFonts;
 197         } else {
 198             int newlen = installedFonts.length + created.length;
 199             Font [] fonts = java.util.Arrays.copyOf(installedFonts, newlen);
 200             System.arraycopy(created, 0, fonts,
 201                              installedFonts.length, created.length);
 202             return fonts;
 203         }
 204     }
 205 
 206     public String[] getAvailableFontFamilyNames(Locale requestedLocale) {
 207         FontManagerForSGE fm = getFontManagerForSGE();
 208         String[] installed = fm.getInstalledFontFamilyNames(requestedLocale);
 209         /* Use a new TreeMap as used in getInstalledFontFamilyNames
 210          * and insert all the keys in lower case, so that the sort order
 211          * is the same as the installed families. This preserves historical
 212          * behaviour and inserts new families in the right place.
 213          * It would have been marginally more efficient to directly obtain
 214          * the tree map and just insert new entries, but not so much as
 215          * to justify the extra internal interface.
 216          */
 217         TreeMap<String, String> map = fm.getCreatedFontFamilyNames();
 218         if (map == null || map.size() == 0) {
 219             return installed;
 220         } else {
 221             for (int i=0; i<installed.length; i++) {
 222                 map.put(installed[i].toLowerCase(requestedLocale),
 223                         installed[i]);
 224             }
 225             String[] retval =  new String[map.size()];
 226             Object [] keyNames = map.keySet().toArray();
 227             for (int i=0; i < keyNames.length; i++) {
 228                 retval[i] = (String)map.get(keyNames[i]);
 229             }
 230             return retval;
 231         }
 232     }
 233 
 234     public String[] getAvailableFontFamilyNames() {
 235         return getAvailableFontFamilyNames(Locale.getDefault());
 236     }
 237 
 238     /**
 239      * Return the bounds of a GraphicsDevice, less its screen insets.
 240      * See also java.awt.GraphicsEnvironment.getUsableBounds();
 241      */
 242     public static Rectangle getUsableBounds(GraphicsDevice gd) {
 243         GraphicsConfiguration gc = gd.getDefaultConfiguration();
 244         Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
 245         Rectangle usableBounds = gc.getBounds();
 246 
 247         usableBounds.x += insets.left;
 248         usableBounds.y += insets.top;
 249         usableBounds.width -= (insets.left + insets.right);
 250         usableBounds.height -= (insets.top + insets.bottom);
 251 
 252         return usableBounds;
 253     }
 254 
 255     /**
 256      * From the DisplayChangedListener interface; called
 257      * when the display mode has been changed.
 258      */
 259     public void displayChanged() {
 260         // notify screens in device array to do display update stuff
 261         for (GraphicsDevice gd : getScreenDevices()) {
 262             if (gd instanceof DisplayChangedListener) {
 263                 ((DisplayChangedListener) gd).displayChanged();
 264             }
 265         }
 266 
 267         // notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and
 268         // SurfaceDataProxies) about the display change event
 269         displayChanger.notifyListeners();
 270     }
 271 
 272     /**
 273      * Part of the DisplayChangedListener interface:
 274      * propagate this event to listeners
 275      */
 276     public void paletteChanged() {
 277         displayChanger.notifyPaletteChanged();
 278     }
 279 
 280     /**
 281      * Returns true when the display is local, false for remote displays.
 282      *
 283      * @return true when the display is local, false for remote displays
 284      */
 285     public abstract boolean isDisplayLocal();
 286 
 287     /*
 288      * ----DISPLAY CHANGE SUPPORT----
 289      */
 290 
 291     protected SunDisplayChanger displayChanger = new SunDisplayChanger();
 292 
 293     /**
 294      * Add a DisplayChangeListener to be notified when the display settings
 295      * are changed.
 296      */
 297     public void addDisplayChangedListener(DisplayChangedListener client) {
 298         displayChanger.add(client);
 299     }
 300 
 301     /**
 302      * Remove a DisplayChangeListener from Win32GraphicsEnvironment
 303      */
 304     public void removeDisplayChangedListener(DisplayChangedListener client) {
 305         displayChanger.remove(client);
 306     }
 307 
 308     /*
 309      * ----END DISPLAY CHANGE SUPPORT----
 310      */
 311 
 312     /**
 313      * Returns true if FlipBufferStrategy with COPIED buffer contents
 314      * is preferred for this peer's GraphicsConfiguration over
 315      * BlitBufferStrategy, false otherwise.
 316      *
 317      * The reason FlipBS could be preferred is that in some configurations
 318      * an accelerated copy to the screen is supported (like Direct3D 9)
 319      *
 320      * @return true if flip strategy should be used, false otherwise
 321      */
 322     public boolean isFlipStrategyPreferred(ComponentPeer peer) {
 323         return false;
 324     }
 325 }