Home » openjdk-7 » sun » java2d » [javadoc | source]

    1   /*
    2    * Copyright (c) 1997, 2011, 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       /* Modifies the behaviour of a subsequent call to preferLocaleFonts()
  190        * to use Mincho instead of Gothic for dialoginput in JA locales
  191        * on windows. Not needed on other platforms.
  192        *
  193        * DO NOT MOVE OR RENAME OR OTHERWISE ALTER THIS METHOD.
  194        * ITS USED BY SOME NON-JRE INTERNAL CODE.
  195        */
  196       public static void useAlternateFontforJALocales() {
  197           getFontManagerForSGE().useAlternateFontforJALocales();
  198       }
  199   
  200        /**
  201        * Returns all fonts available in this environment.
  202        */
  203       public Font[] getAllFonts() {
  204           FontManagerForSGE fm = getFontManagerForSGE();
  205           Font[] installedFonts = fm.getAllInstalledFonts();
  206           Font[] created = fm.getCreatedFonts();
  207           if (created == null || created.length == 0) {
  208               return installedFonts;
  209           } else {
  210               int newlen = installedFonts.length + created.length;
  211               Font [] fonts = java.util.Arrays.copyOf(installedFonts, newlen);
  212               System.arraycopy(created, 0, fonts,
  213                                installedFonts.length, created.length);
  214               return fonts;
  215           }
  216       }
  217   
  218       public String[] getAvailableFontFamilyNames(Locale requestedLocale) {
  219           FontManagerForSGE fm = getFontManagerForSGE();
  220           String[] installed = fm.getInstalledFontFamilyNames(requestedLocale);
  221           /* Use a new TreeMap as used in getInstalledFontFamilyNames
  222            * and insert all the keys in lower case, so that the sort order
  223            * is the same as the installed families. This preserves historical
  224            * behaviour and inserts new families in the right place.
  225            * It would have been marginally more efficient to directly obtain
  226            * the tree map and just insert new entries, but not so much as
  227            * to justify the extra internal interface.
  228            */
  229           TreeMap<String, String> map = fm.getCreatedFontFamilyNames();
  230           if (map == null || map.size() == 0) {
  231               return installed;
  232           } else {
  233               for (int i=0; i<installed.length; i++) {
  234                   map.put(installed[i].toLowerCase(requestedLocale),
  235                           installed[i]);
  236               }
  237               String[] retval =  new String[map.size()];
  238               Object [] keyNames = map.keySet().toArray();
  239               for (int i=0; i < keyNames.length; i++) {
  240                   retval[i] = (String)map.get(keyNames[i]);
  241               }
  242               return retval;
  243           }
  244       }
  245   
  246       public String[] getAvailableFontFamilyNames() {
  247           return getAvailableFontFamilyNames(Locale.getDefault());
  248       }
  249   
  250       /**
  251        * Return the bounds of a GraphicsDevice, less its screen insets.
  252        * See also java.awt.GraphicsEnvironment.getUsableBounds();
  253        */
  254       public static Rectangle getUsableBounds(GraphicsDevice gd) {
  255           GraphicsConfiguration gc = gd.getDefaultConfiguration();
  256           Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  257           Rectangle usableBounds = gc.getBounds();
  258   
  259           usableBounds.x += insets.left;
  260           usableBounds.y += insets.top;
  261           usableBounds.width -= (insets.left + insets.right);
  262           usableBounds.height -= (insets.top + insets.bottom);
  263   
  264           return usableBounds;
  265       }
  266   
  267       /**
  268        * From the DisplayChangedListener interface; called
  269        * when the display mode has been changed.
  270        */
  271       public void displayChanged() {
  272           // notify screens in device array to do display update stuff
  273           for (GraphicsDevice gd : getScreenDevices()) {
  274               if (gd instanceof DisplayChangedListener) {
  275                   ((DisplayChangedListener) gd).displayChanged();
  276               }
  277           }
  278   
  279           // notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and
  280           // SurfaceDataProxies) about the display change event
  281           displayChanger.notifyListeners();
  282       }
  283   
  284       /**
  285        * Part of the DisplayChangedListener interface:
  286        * propagate this event to listeners
  287        */
  288       public void paletteChanged() {
  289           displayChanger.notifyPaletteChanged();
  290       }
  291   
  292       /**
  293        * Returns true when the display is local, false for remote displays.
  294        *
  295        * @return true when the display is local, false for remote displays
  296        */
  297       public abstract boolean isDisplayLocal();
  298   
  299       /*
  300        * ----DISPLAY CHANGE SUPPORT----
  301        */
  302   
  303       protected SunDisplayChanger displayChanger = new SunDisplayChanger();
  304   
  305       /**
  306        * Add a DisplayChangeListener to be notified when the display settings
  307        * are changed.
  308        */
  309       public void addDisplayChangedListener(DisplayChangedListener client) {
  310           displayChanger.add(client);
  311       }
  312   
  313       /**
  314        * Remove a DisplayChangeListener from Win32GraphicsEnvironment
  315        */
  316       public void removeDisplayChangedListener(DisplayChangedListener client) {
  317           displayChanger.remove(client);
  318       }
  319   
  320       /*
  321        * ----END DISPLAY CHANGE SUPPORT----
  322        */
  323   
  324       /**
  325        * Returns true if FlipBufferStrategy with COPIED buffer contents
  326        * is preferred for this peer's GraphicsConfiguration over
  327        * BlitBufferStrategy, false otherwise.
  328        *
  329        * The reason FlipBS could be preferred is that in some configurations
  330        * an accelerated copy to the screen is supported (like Direct3D 9)
  331        *
  332        * @return true if flip strategy should be used, false otherwise
  333        */
  334       public boolean isFlipStrategyPreferred(ComponentPeer peer) {
  335           return false;
  336       }
  337   }

Home » openjdk-7 » sun » java2d » [javadoc | source]