View Javadoc
1   /*
2    * Copyright (c) 1998, 2013, 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.awt;
27  
28  import java.awt.RenderingHints;
29  import java.lang.annotation.Native;
30  
31  /**
32   * This class contains rendering hints that can be used by the
33   * {@link java.awt.Graphics2D} class, and classes that implement
34   * {@link java.awt.image.BufferedImageOp} and
35   * {@link java.awt.image.Raster}.
36   */
37  public class SunHints {
38      /**
39       * Defines the type of all keys used to control various
40       * aspects of the rendering and imaging pipelines.  Instances
41       * of this class are immutable and unique which means that
42       * tests for matches can be made using the == operator instead
43       * of the more expensive equals() method.
44       */
45      public static class Key extends RenderingHints.Key {
46          String description;
47  
48          /**
49           * Construct a key using the indicated private key.  Each
50           * subclass of Key maintains its own unique domain of integer
51           * keys.  No two objects with the same integer key and of the
52           * same specific subclass can be constructed.  An exception
53           * will be thrown if an attempt is made to construct another
54           * object of a given class with the same integer key as a
55           * pre-existing instance of that subclass of Key.
56           */
57          public Key(int privatekey, String description) {
58              super(privatekey);
59              this.description = description;
60          }
61  
62          /**
63           * Returns the numeric index associated with this Key.  This
64           * is useful for use in switch statements and quick lookups
65           * of the setting of a particular key.
66           */
67          public final int getIndex() {
68              return intKey();
69          }
70  
71          /**
72           * Returns a string representation of the Key.
73           */
74          public final String toString() {
75              return description;
76          }
77  
78          /**
79           * Returns true if the specified object is a valid value
80           * for this Key.
81           */
82          public boolean isCompatibleValue(Object val) {
83              if (val instanceof Value) {
84                  return ((Value)val).isCompatibleKey(this);
85              }
86              return false;
87          }
88      }
89  
90      /**
91       * Defines the type of all "enumerative" values used to control
92       * various aspects of the rendering and imaging pipelines.  Instances
93       * of this class are immutable and unique which means that
94       * tests for matches can be made using the == operator instead
95       * of the more expensive equals() method.
96       */
97      public static class Value {
98          private SunHints.Key myKey;
99          private int index;
100         private String description;
101 
102         private static Value[][] ValueObjects =
103             new Value[NUM_KEYS][VALS_PER_KEY];
104 
105         private synchronized static void register(SunHints.Key key,
106                                                   Value value) {
107             int kindex = key.getIndex();
108             int vindex = value.getIndex();
109             if (ValueObjects[kindex][vindex] != null) {
110                 throw new InternalError("duplicate index: "+vindex);
111             }
112             ValueObjects[kindex][vindex] = value;
113         }
114 
115         public static Value get(int keyindex, int valueindex) {
116             return ValueObjects[keyindex][valueindex];
117         }
118 
119         /**
120          * Construct a value using the indicated private index.  Each
121          * subclass of Value maintains its own unique domain of integer
122          * indices.  Enforcing the uniqueness of the integer indices
123          * is left to the subclass.
124          */
125         public Value(SunHints.Key key, int index, String description) {
126             this.myKey = key;
127             this.index = index;
128             this.description = description;
129 
130             register(key, this);
131         }
132 
133         /**
134          * Returns the numeric index associated with this Key.  This
135          * is useful for use in switch statements and quick lookups
136          * of the setting of a particular key.
137          */
138         public final int getIndex() {
139             return index;
140         }
141 
142         /**
143          * Returns a string representation of this Value.
144          */
145         public final String toString() {
146             return description;
147         }
148 
149         /**
150          * Returns true if the specified object is a valid Key
151          * for this Value.
152          */
153         public final boolean isCompatibleKey(Key k) {
154             return myKey == k;
155         }
156 
157         /**
158          * The hash code for all SunHints.Value objects will be the same
159          * as the system identity code of the object as defined by the
160          * System.identityHashCode() method.
161          */
162         public final int hashCode() {
163             return System.identityHashCode(this);
164         }
165 
166         /**
167          * The equals method for all SunHints.Value objects will return
168          * the same result as the equality operator '=='.
169          */
170         public final boolean equals(Object o) {
171             return this == o;
172         }
173     }
174 
175     private static final int NUM_KEYS = 9;
176     private static final int VALS_PER_KEY = 8;
177 
178     /**
179      * Rendering hint key and values
180      */
181     @Native public static final int INTKEY_RENDERING = 0;
182     @Native public static final int INTVAL_RENDER_DEFAULT = 0;
183     @Native public static final int INTVAL_RENDER_SPEED = 1;
184     @Native public static final int INTVAL_RENDER_QUALITY = 2;
185 
186     /**
187      * Antialiasing hint key and values
188      */
189     @Native public static final int INTKEY_ANTIALIASING = 1;
190     @Native public static final int INTVAL_ANTIALIAS_DEFAULT = 0;
191     @Native public static final int INTVAL_ANTIALIAS_OFF = 1;
192     @Native public static final int INTVAL_ANTIALIAS_ON = 2;
193 
194     /**
195      * Text antialiasing hint key and values
196      */
197     @Native public static final int INTKEY_TEXT_ANTIALIASING = 2;
198     @Native public static final int INTVAL_TEXT_ANTIALIAS_DEFAULT = 0;
199     @Native public static final int INTVAL_TEXT_ANTIALIAS_OFF = 1;
200     @Native public static final int INTVAL_TEXT_ANTIALIAS_ON = 2;
201     @Native public static final int INTVAL_TEXT_ANTIALIAS_GASP = 3;
202     @Native public static final int INTVAL_TEXT_ANTIALIAS_LCD_HRGB = 4;
203     @Native public static final int INTVAL_TEXT_ANTIALIAS_LCD_HBGR = 5;
204     @Native public static final int INTVAL_TEXT_ANTIALIAS_LCD_VRGB = 6;
205     @Native public static final int INTVAL_TEXT_ANTIALIAS_LCD_VBGR = 7;
206 
207     /**
208      * Font fractional metrics hint key and values
209      */
210     @Native public static final int INTKEY_FRACTIONALMETRICS = 3;
211     @Native public static final int INTVAL_FRACTIONALMETRICS_DEFAULT = 0;
212     @Native public static final int INTVAL_FRACTIONALMETRICS_OFF = 1;
213     @Native public static final int INTVAL_FRACTIONALMETRICS_ON = 2;
214 
215     /**
216      * Dithering hint key and values
217      */
218     @Native public static final int INTKEY_DITHERING = 4;
219     @Native public static final int INTVAL_DITHER_DEFAULT = 0;
220     @Native public static final int INTVAL_DITHER_DISABLE = 1;
221     @Native public static final int INTVAL_DITHER_ENABLE = 2;
222 
223     /**
224      * Interpolation hint key and values
225      */
226     @Native public static final int INTKEY_INTERPOLATION = 5;
227     @Native public static final int INTVAL_INTERPOLATION_NEAREST_NEIGHBOR = 0;
228     @Native public static final int INTVAL_INTERPOLATION_BILINEAR = 1;
229     @Native public static final int INTVAL_INTERPOLATION_BICUBIC = 2;
230 
231     /**
232      * Alpha interpolation hint key and values
233      */
234     @Native public static final int INTKEY_ALPHA_INTERPOLATION = 6;
235     @Native public static final int INTVAL_ALPHA_INTERPOLATION_DEFAULT = 0;
236     @Native public static final int INTVAL_ALPHA_INTERPOLATION_SPEED = 1;
237     @Native public static final int INTVAL_ALPHA_INTERPOLATION_QUALITY = 2;
238 
239     /**
240      * Color rendering hint key and values
241      */
242     @Native public static final int INTKEY_COLOR_RENDERING = 7;
243     @Native public static final int INTVAL_COLOR_RENDER_DEFAULT = 0;
244     @Native public static final int INTVAL_COLOR_RENDER_SPEED = 1;
245     @Native public static final int INTVAL_COLOR_RENDER_QUALITY = 2;
246 
247     /**
248      * Stroke normalization control hint key and values
249      */
250     @Native public static final int INTKEY_STROKE_CONTROL = 8;
251     @Native public static final int INTVAL_STROKE_DEFAULT = 0;
252     @Native public static final int INTVAL_STROKE_NORMALIZE = 1;
253     @Native public static final int INTVAL_STROKE_PURE = 2;
254 
255     /**
256      * LCD text contrast control hint key.
257      * Value is "100" to make discontiguous with the others which
258      * are all enumerative and are of a different class.
259      */
260     @Native public static final int INTKEY_AATEXT_LCD_CONTRAST = 100;
261 
262     /**
263      * Rendering hint key and value objects
264      */
265     public static final Key KEY_RENDERING =
266         new SunHints.Key(SunHints.INTKEY_RENDERING,
267                          "Global rendering quality key");
268     public static final Object VALUE_RENDER_SPEED =
269         new SunHints.Value(KEY_RENDERING,
270                            SunHints.INTVAL_RENDER_SPEED,
271                            "Fastest rendering methods");
272     public static final Object VALUE_RENDER_QUALITY =
273         new SunHints.Value(KEY_RENDERING,
274                            SunHints.INTVAL_RENDER_QUALITY,
275                            "Highest quality rendering methods");
276     public static final Object VALUE_RENDER_DEFAULT =
277         new SunHints.Value(KEY_RENDERING,
278                            SunHints.INTVAL_RENDER_DEFAULT,
279                            "Default rendering methods");
280 
281     /**
282      * Antialiasing hint key and value objects
283      */
284     public static final Key KEY_ANTIALIASING =
285         new SunHints.Key(SunHints.INTKEY_ANTIALIASING,
286                          "Global antialiasing enable key");
287     public static final Object VALUE_ANTIALIAS_ON =
288         new SunHints.Value(KEY_ANTIALIASING,
289                            SunHints.INTVAL_ANTIALIAS_ON,
290                            "Antialiased rendering mode");
291     public static final Object VALUE_ANTIALIAS_OFF =
292         new SunHints.Value(KEY_ANTIALIASING,
293                            SunHints.INTVAL_ANTIALIAS_OFF,
294                            "Nonantialiased rendering mode");
295     public static final Object VALUE_ANTIALIAS_DEFAULT =
296         new SunHints.Value(KEY_ANTIALIASING,
297                            SunHints.INTVAL_ANTIALIAS_DEFAULT,
298                            "Default antialiasing rendering mode");
299 
300     /**
301      * Text antialiasing hint key and value objects
302      */
303     public static final Key KEY_TEXT_ANTIALIASING =
304         new SunHints.Key(SunHints.INTKEY_TEXT_ANTIALIASING,
305                          "Text-specific antialiasing enable key");
306     public static final Object VALUE_TEXT_ANTIALIAS_ON =
307         new SunHints.Value(KEY_TEXT_ANTIALIASING,
308                            SunHints.INTVAL_TEXT_ANTIALIAS_ON,
309                            "Antialiased text mode");
310     public static final Object VALUE_TEXT_ANTIALIAS_OFF =
311         new SunHints.Value(KEY_TEXT_ANTIALIASING,
312                            SunHints.INTVAL_TEXT_ANTIALIAS_OFF,
313                            "Nonantialiased text mode");
314     public static final Object VALUE_TEXT_ANTIALIAS_DEFAULT =
315         new SunHints.Value(KEY_TEXT_ANTIALIASING,
316                            SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT,
317                            "Default antialiasing text mode");
318     public static final Object VALUE_TEXT_ANTIALIAS_GASP =
319         new SunHints.Value(KEY_TEXT_ANTIALIASING,
320                            SunHints.INTVAL_TEXT_ANTIALIAS_GASP,
321                            "gasp antialiasing text mode");
322     public static final Object VALUE_TEXT_ANTIALIAS_LCD_HRGB =
323         new SunHints.Value(KEY_TEXT_ANTIALIASING,
324                            SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HRGB,
325                            "LCD HRGB antialiasing text mode");
326     public static final Object VALUE_TEXT_ANTIALIAS_LCD_HBGR =
327         new SunHints.Value(KEY_TEXT_ANTIALIASING,
328                            SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HBGR,
329                            "LCD HBGR antialiasing text mode");
330     public static final Object VALUE_TEXT_ANTIALIAS_LCD_VRGB =
331         new SunHints.Value(KEY_TEXT_ANTIALIASING,
332                            SunHints.INTVAL_TEXT_ANTIALIAS_LCD_VRGB,
333                            "LCD VRGB antialiasing text mode");
334     public static final Object VALUE_TEXT_ANTIALIAS_LCD_VBGR =
335         new SunHints.Value(KEY_TEXT_ANTIALIASING,
336                            SunHints.INTVAL_TEXT_ANTIALIAS_LCD_VBGR,
337                            "LCD VBGR antialiasing text mode");
338 
339     /**
340      * Font fractional metrics hint key and value objects
341      */
342     public static final Key KEY_FRACTIONALMETRICS =
343         new SunHints.Key(SunHints.INTKEY_FRACTIONALMETRICS,
344                          "Fractional metrics enable key");
345     public static final Object VALUE_FRACTIONALMETRICS_ON =
346         new SunHints.Value(KEY_FRACTIONALMETRICS,
347                            SunHints.INTVAL_FRACTIONALMETRICS_ON,
348                            "Fractional text metrics mode");
349     public static final Object VALUE_FRACTIONALMETRICS_OFF =
350         new SunHints.Value(KEY_FRACTIONALMETRICS,
351                            SunHints.INTVAL_FRACTIONALMETRICS_OFF,
352                            "Integer text metrics mode");
353     public static final Object VALUE_FRACTIONALMETRICS_DEFAULT =
354         new SunHints.Value(KEY_FRACTIONALMETRICS,
355                            SunHints.INTVAL_FRACTIONALMETRICS_DEFAULT,
356                            "Default fractional text metrics mode");
357 
358     /**
359      * Dithering hint key and value objects
360      */
361     public static final Key KEY_DITHERING =
362         new SunHints.Key(SunHints.INTKEY_DITHERING,
363                          "Dithering quality key");
364     public static final Object VALUE_DITHER_ENABLE =
365         new SunHints.Value(KEY_DITHERING,
366                            SunHints.INTVAL_DITHER_ENABLE,
367                            "Dithered rendering mode");
368     public static final Object VALUE_DITHER_DISABLE =
369         new SunHints.Value(KEY_DITHERING,
370                            SunHints.INTVAL_DITHER_DISABLE,
371                            "Nondithered rendering mode");
372     public static final Object VALUE_DITHER_DEFAULT =
373         new SunHints.Value(KEY_DITHERING,
374                            SunHints.INTVAL_DITHER_DEFAULT,
375                            "Default dithering mode");
376 
377     /**
378      * Interpolation hint key and value objects
379      */
380     public static final Key KEY_INTERPOLATION =
381         new SunHints.Key(SunHints.INTKEY_INTERPOLATION,
382                          "Image interpolation method key");
383     public static final Object VALUE_INTERPOLATION_NEAREST_NEIGHBOR =
384         new SunHints.Value(KEY_INTERPOLATION,
385                            SunHints.INTVAL_INTERPOLATION_NEAREST_NEIGHBOR,
386                            "Nearest Neighbor image interpolation mode");
387     public static final Object VALUE_INTERPOLATION_BILINEAR =
388         new SunHints.Value(KEY_INTERPOLATION,
389                            SunHints.INTVAL_INTERPOLATION_BILINEAR,
390                            "Bilinear image interpolation mode");
391     public static final Object VALUE_INTERPOLATION_BICUBIC =
392         new SunHints.Value(KEY_INTERPOLATION,
393                            SunHints.INTVAL_INTERPOLATION_BICUBIC,
394                            "Bicubic image interpolation mode");
395 
396     /**
397      * Alpha interpolation hint key and value objects
398      */
399     public static final Key KEY_ALPHA_INTERPOLATION =
400         new SunHints.Key(SunHints.INTKEY_ALPHA_INTERPOLATION,
401                          "Alpha blending interpolation method key");
402     public static final Object VALUE_ALPHA_INTERPOLATION_SPEED =
403         new SunHints.Value(KEY_ALPHA_INTERPOLATION,
404                            SunHints.INTVAL_ALPHA_INTERPOLATION_SPEED,
405                            "Fastest alpha blending methods");
406     public static final Object VALUE_ALPHA_INTERPOLATION_QUALITY =
407         new SunHints.Value(KEY_ALPHA_INTERPOLATION,
408                            SunHints.INTVAL_ALPHA_INTERPOLATION_QUALITY,
409                            "Highest quality alpha blending methods");
410     public static final Object VALUE_ALPHA_INTERPOLATION_DEFAULT =
411         new SunHints.Value(KEY_ALPHA_INTERPOLATION,
412                            SunHints.INTVAL_ALPHA_INTERPOLATION_DEFAULT,
413                            "Default alpha blending methods");
414 
415     /**
416      * Color rendering hint key and value objects
417      */
418     public static final Key KEY_COLOR_RENDERING =
419         new SunHints.Key(SunHints.INTKEY_COLOR_RENDERING,
420                          "Color rendering quality key");
421     public static final Object VALUE_COLOR_RENDER_SPEED =
422         new SunHints.Value(KEY_COLOR_RENDERING,
423                            SunHints.INTVAL_COLOR_RENDER_SPEED,
424                            "Fastest color rendering mode");
425     public static final Object VALUE_COLOR_RENDER_QUALITY =
426         new SunHints.Value(KEY_COLOR_RENDERING,
427                            SunHints.INTVAL_COLOR_RENDER_QUALITY,
428                            "Highest quality color rendering mode");
429     public static final Object VALUE_COLOR_RENDER_DEFAULT =
430         new SunHints.Value(KEY_COLOR_RENDERING,
431                            SunHints.INTVAL_COLOR_RENDER_DEFAULT,
432                            "Default color rendering mode");
433 
434     /**
435      * Stroke normalization control hint key and value objects
436      */
437     public static final Key KEY_STROKE_CONTROL =
438         new SunHints.Key(SunHints.INTKEY_STROKE_CONTROL,
439                          "Stroke normalization control key");
440     public static final Object VALUE_STROKE_DEFAULT =
441         new SunHints.Value(KEY_STROKE_CONTROL,
442                            SunHints.INTVAL_STROKE_DEFAULT,
443                            "Default stroke normalization");
444     public static final Object VALUE_STROKE_NORMALIZE =
445         new SunHints.Value(KEY_STROKE_CONTROL,
446                            SunHints.INTVAL_STROKE_NORMALIZE,
447                            "Normalize strokes for consistent rendering");
448     public static final Object VALUE_STROKE_PURE =
449         new SunHints.Value(KEY_STROKE_CONTROL,
450                            SunHints.INTVAL_STROKE_PURE,
451                            "Pure stroke conversion for accurate paths");
452 
453 
454     public static class LCDContrastKey extends Key {
455 
456         public LCDContrastKey(int privatekey, String description) {
457             super(privatekey, description);
458         }
459 
460         /**
461          * Returns true if the specified object is a valid value
462          * for this Key. The allowable range is 100 to 250.
463          */
464         public final boolean isCompatibleValue(Object val) {
465             if (val instanceof Integer) {
466                 int ival = ((Integer)val).intValue();
467                 return ival >= 100 && ival <= 250;
468             }
469             return false;
470         }
471 
472     }
473 
474     /**
475      * LCD text contrast hint key
476      */
477     public static final RenderingHints.Key
478         KEY_TEXT_ANTIALIAS_LCD_CONTRAST =
479         new LCDContrastKey(SunHints.INTKEY_AATEXT_LCD_CONTRAST,
480                            "Text-specific LCD contrast key");
481 }