History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/java2d/SunGraphicsEnvironment.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/java2d/SunGraphicsEnvironment.java+337−0
@@ -0,0 +1,337 @@
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
26package sun.java2d;
27
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;
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 */
76public 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}