1 /* RenderingHints.java --
2 Copyright (C) 2000, 2001, 2002, 2004, 2005 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.Iterator;
49 * A collection of (key, value) items that provide 'hints' for the
50 * {@link java.awt.Graphics2D} rendering pipeline. Because these
51 * items are hints only, they may be ignored by a particular
52 * {@link java.awt.Graphics2D} implementation.
54 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
55 * @author Eric Blake (ebb9@email.byu.edu)
57 public class RenderingHints
58 implements Map<Object,Object>, Cloneable
61 * The base class used to represent keys.
63 public abstract static class Key
65 private final int key;
70 * @param privateKey the private key.
72 protected Key(int privateKey)
78 * Returns <code>true</code> if the specified value is compatible with
79 * this key, and <code>false</code> otherwise.
81 * @param value the value (<code>null</code> permitted).
85 public abstract boolean isCompatibleValue(Object value);
88 * Returns the private key for this instance.
90 * @return The private key.
92 protected final int intKey()
98 * Returns a hash code for the key.
100 * @return A hash code.
102 public final int hashCode()
104 return System.identityHashCode(this);
108 * Checks this key for equality with an arbitrary object.
110 * @param other the object (<code>null</code> permitted)
114 public final boolean equals(Object other)
116 return this == other;
120 private static final class KeyImpl extends Key
122 final String description;
127 KeyImpl(int privateKey, String description,
128 Object v1, Object v2, Object v3)
131 this.description = description;
138 * Returns <code>true</code> if the specified value is compatible with
139 * this key, and <code>false</code> otherwise.
141 * @param value the value (<code>null</code> permitted).
145 public boolean isCompatibleValue(Object value)
147 return value == v1 || value == v2 || value == v3;
151 * Returns a string representation of the key.
155 public String toString()
161 private HashMap<Object,Object> hintMap = new HashMap<Object,Object>();
164 * A key for the 'antialiasing' hint. Permitted values are:
168 * <td>{@link #VALUE_ANTIALIAS_OFF}</td>
169 * <td>Render without antialiasing (better speed).</td>
172 * <td>{@link #VALUE_ANTIALIAS_ON}</td>
173 * <td>Render with antialiasing (better quality).</td>
176 * <td>{@link #VALUE_ANTIALIAS_DEFAULT}</td>
177 * <td>Use the default value for antialiasing.</td>
181 public static final Key KEY_ANTIALIASING;
184 * This value is for use with the {@link #KEY_ANTIALIASING} key.
186 public static final Object VALUE_ANTIALIAS_ON
187 = "Antialiased rendering mode";
190 * This value is for use with the {@link #KEY_ANTIALIASING} key.
192 public static final Object VALUE_ANTIALIAS_OFF
193 = "Nonantialiased rendering mode";
196 * This value is for use with the {@link #KEY_ANTIALIASING} key.
198 public static final Object VALUE_ANTIALIAS_DEFAULT
199 = "Default antialiasing rendering mode";
202 * A key for the 'rendering' hint. Permitted values are:
206 * <td>{@link #VALUE_RENDER_SPEED}</td>
207 * <td>Prefer speed over quality when rendering.</td>
210 * <td>{@link #VALUE_RENDER_QUALITY}</td>
211 * <td>Prefer quality over speed when rendering.</td>
214 * <td>{@link #VALUE_RENDER_DEFAULT}</td>
215 * <td>Use the default value for quality vs. speed when rendering.</td>
219 public static final Key KEY_RENDERING;
222 * This value is for use with the {@link #KEY_RENDERING} key.
224 public static final Object VALUE_RENDER_SPEED
225 = "Fastest rendering methods";
228 * This value is for use with the {@link #KEY_RENDERING} key.
230 public static final Object VALUE_RENDER_QUALITY
231 = "Highest quality rendering methods";
234 * This value is for use with the {@link #KEY_RENDERING} key.
236 public static final Object VALUE_RENDER_DEFAULT
237 = "Default rendering methods";
240 * A key for the 'dithering' hint. Permitted values are:
244 * <td>{@link #VALUE_DITHER_DISABLE}</td>
245 * <td>Disable dithering.</td>
248 * <td>{@link #VALUE_DITHER_ENABLE}</td>
249 * <td>Enable dithering.</td>
252 * <td>{@link #VALUE_DITHER_DEFAULT}</td>
253 * <td>Use the default value for dithering.</td>
257 public static final Key KEY_DITHERING;
260 * This value is for use with the {@link #KEY_DITHERING} key.
262 public static final Object VALUE_DITHER_DISABLE
263 = "Nondithered rendering mode";
266 * This value is for use with the {@link #KEY_DITHERING} key.
268 public static final Object VALUE_DITHER_ENABLE
269 = "Dithered rendering mode";
272 * This value is for use with the {@link #KEY_DITHERING} key.
274 public static final Object VALUE_DITHER_DEFAULT
275 = "Default dithering mode";
278 * A key for the 'text antialiasing' hint. Permitted values are:
282 * <td>{@link #VALUE_TEXT_ANTIALIAS_ON}</td>
283 * <td>Render text with antialiasing (better quality usually).</td>
286 * <td>{@link #VALUE_TEXT_ANTIALIAS_OFF}</td>
287 * <td>Render test without antialiasing (better speed).</td>
290 * <td>{@link #VALUE_TEXT_ANTIALIAS_DEFAULT}</td>
291 * <td>Use the default value for text antialiasing.</td>
295 public static final Key KEY_TEXT_ANTIALIASING;
298 * This value is for use with the {@link #KEY_TEXT_ANTIALIASING} key.
300 public static final Object VALUE_TEXT_ANTIALIAS_ON
301 = "Antialiased text mode";
304 * This value is for use with the {@link #KEY_TEXT_ANTIALIASING} key.
306 public static final Object VALUE_TEXT_ANTIALIAS_OFF
307 = "Nonantialiased text mode";
310 * This value is for use with the {@link #KEY_TEXT_ANTIALIASING} key.
312 public static final Object VALUE_TEXT_ANTIALIAS_DEFAULT
313 = "Default antialiasing text mode";
316 * A key for the 'fractional metrics' hint. Permitted values are:
320 * <td>{@link #VALUE_FRACTIONALMETRICS_OFF}</td>
321 * <td>Render text with fractional metrics off.</td>
324 * <td>{@link #VALUE_FRACTIONALMETRICS_ON}</td>
325 * <td>Render text with fractional metrics on.</td>
328 * <td>{@link #VALUE_FRACTIONALMETRICS_DEFAULT}</td>
329 * <td>Use the default value for fractional metrics.</td>
333 public static final Key KEY_FRACTIONALMETRICS;
336 * This value is for use with the {@link #KEY_FRACTIONALMETRICS} key.
338 public static final Object VALUE_FRACTIONALMETRICS_OFF
339 = "Integer text metrics mode";
342 * This value is for use with the {@link #KEY_FRACTIONALMETRICS} key.
344 public static final Object VALUE_FRACTIONALMETRICS_ON
345 = "Fractional text metrics mode";
348 * This value is for use with the {@link #KEY_FRACTIONALMETRICS} key.
350 public static final Object VALUE_FRACTIONALMETRICS_DEFAULT
351 = "Default fractional text metrics mode";
354 * A key for the 'interpolation' hint. Permitted values are:
358 * <td>{@link #VALUE_INTERPOLATION_NEAREST_NEIGHBOR}</td>
359 * <td>Use nearest neighbour interpolation.</td>
362 * <td>{@link #VALUE_INTERPOLATION_BILINEAR}</td>
363 * <td>Use bilinear interpolation.</td>
366 * <td>{@link #VALUE_INTERPOLATION_BICUBIC}</td>
367 * <td>Use bicubic interpolation.</td>
371 public static final Key KEY_INTERPOLATION;
374 * This value is for use with the {@link #KEY_INTERPOLATION} key.
376 public static final Object VALUE_INTERPOLATION_NEAREST_NEIGHBOR
377 = "Nearest Neighbor image interpolation mode";
380 * This value is for use with the {@link #KEY_INTERPOLATION} key.
382 public static final Object VALUE_INTERPOLATION_BILINEAR
383 = "Bilinear image interpolation mode";
386 * This value is for use with the {@link #KEY_INTERPOLATION} key.
388 public static final Object VALUE_INTERPOLATION_BICUBIC
389 = "Bicubic image interpolation mode";
392 * A key for the 'alpha interpolation' hint. Permitted values are:
396 * <td>{@link #VALUE_ALPHA_INTERPOLATION_SPEED}</td>
397 * <td>Prefer speed over quality.</td>
400 * <td>{@link #VALUE_ALPHA_INTERPOLATION_QUALITY}</td>
401 * <td>Prefer quality over speed.</td>
404 * <td>{@link #VALUE_ALPHA_INTERPOLATION_DEFAULT}</td>
405 * <td>Use the default setting.</td>
409 public static final Key KEY_ALPHA_INTERPOLATION;
412 * This value is for use with the {@link #KEY_ALPHA_INTERPOLATION} key.
414 public static final Object VALUE_ALPHA_INTERPOLATION_SPEED
415 = "Fastest alpha blending methods";
418 * This value is for use with the {@link #KEY_ALPHA_INTERPOLATION} key.
420 public static final Object VALUE_ALPHA_INTERPOLATION_QUALITY
421 = "Highest quality alpha blending methods";
424 * This value is for use with the {@link #KEY_ALPHA_INTERPOLATION} key.
426 public static final Object VALUE_ALPHA_INTERPOLATION_DEFAULT
427 = "Default alpha blending methods";
430 * A key for the 'color rendering' hint. Permitted values are:
434 * <td>{@link #VALUE_COLOR_RENDER_SPEED}</td>
435 * <td>Prefer speed over quality.</td>
438 * <td>{@link #VALUE_COLOR_RENDER_QUALITY}</td>
439 * <td>Prefer quality over speed.</td>
442 * <td>{@link #VALUE_COLOR_RENDER_DEFAULT}</td>
443 * <td>Use the default setting.</td>
447 public static final Key KEY_COLOR_RENDERING;
450 * This value is for use with the {@link #KEY_COLOR_RENDERING} key.
452 public static final Object VALUE_COLOR_RENDER_SPEED
453 = "Fastest color rendering mode";
456 * This value is for use with the {@link #KEY_COLOR_RENDERING} key.
458 public static final Object VALUE_COLOR_RENDER_QUALITY
459 = "Highest quality color rendering mode";
462 * This value is for use with the {@link #KEY_COLOR_RENDERING} key.
464 public static final Object VALUE_COLOR_RENDER_DEFAULT
465 = "Default color rendering mode";
468 * A key for the 'stroke control' hint. Permitted values are:
472 * <td>{@link #VALUE_STROKE_DEFAULT}</td>
473 * <td>Use the default setting.</td>
476 * <td>{@link #VALUE_STROKE_NORMALIZE}</td>
480 * <td>{@link #VALUE_STROKE_PURE}</td>
485 public static final Key KEY_STROKE_CONTROL;
488 * This value is for use with the {@link #KEY_STROKE_CONTROL} key.
490 public static final Object VALUE_STROKE_DEFAULT
491 = "Default stroke normalization";
494 * This value is for use with the {@link #KEY_STROKE_CONTROL} key.
496 public static final Object VALUE_STROKE_NORMALIZE
497 = "Normalize strokes for consistent rendering";
500 * This value is for use with the {@link #KEY_STROKE_CONTROL} key.
502 public static final Object VALUE_STROKE_PURE
503 = "Pure stroke conversion for accurate paths";
507 KEY_ANTIALIASING = new KeyImpl(1, "Global antialiasing enable key",
510 VALUE_ANTIALIAS_DEFAULT);
511 KEY_RENDERING = new KeyImpl(2, "Global rendering quality key",
513 VALUE_RENDER_QUALITY,
514 VALUE_RENDER_DEFAULT);
515 KEY_DITHERING = new KeyImpl(3, "Dithering quality key",
516 VALUE_DITHER_DISABLE,
518 VALUE_DITHER_DEFAULT);
519 KEY_TEXT_ANTIALIASING
520 = new KeyImpl(4, "Text-specific antialiasing enable key",
521 VALUE_TEXT_ANTIALIAS_ON,
522 VALUE_TEXT_ANTIALIAS_OFF,
523 VALUE_TEXT_ANTIALIAS_DEFAULT);
524 KEY_FRACTIONALMETRICS = new KeyImpl(5, "Fractional metrics enable key",
525 VALUE_FRACTIONALMETRICS_OFF,
526 VALUE_FRACTIONALMETRICS_ON,
527 VALUE_FRACTIONALMETRICS_DEFAULT);
528 KEY_INTERPOLATION = new KeyImpl(6, "Image interpolation method key",
529 VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
530 VALUE_INTERPOLATION_BILINEAR,
531 VALUE_INTERPOLATION_BICUBIC);
532 KEY_ALPHA_INTERPOLATION
533 = new KeyImpl(7, "Alpha blending interpolation method key",
534 VALUE_ALPHA_INTERPOLATION_SPEED,
535 VALUE_ALPHA_INTERPOLATION_QUALITY,
536 VALUE_ALPHA_INTERPOLATION_DEFAULT);
537 KEY_COLOR_RENDERING = new KeyImpl(8, "Color rendering quality key",
538 VALUE_COLOR_RENDER_SPEED,
539 VALUE_COLOR_RENDER_QUALITY,
540 VALUE_COLOR_RENDER_DEFAULT);
541 KEY_STROKE_CONTROL = new KeyImpl(9, "Stroke normalization control key",
542 VALUE_STROKE_DEFAULT,
543 VALUE_STROKE_NORMALIZE,
548 * Creates a new collection of hints containing all the (key, value) pairs
549 * in the specified map.
551 * @param init a map containing a collection of hints (<code>null</code>
554 public RenderingHints(Map<Key,?> init)
561 * Creates a new collection containing a single (key, value) pair.
563 * @param key the key.
564 * @param value the value.
566 public RenderingHints(Key key, Object value)
572 * Returns the number of hints in the collection.
574 * @return The number of hints.
578 return hintMap.size();
582 * Returns <code>true</code> if there are no hints in the collection,
583 * and <code>false</code> otherwise.
587 public boolean isEmpty()
589 return hintMap.isEmpty();
593 * Returns <code>true</code> if the collection of hints contains the
594 * specified key, and <code>false</code> otherwise.
596 * @param key the key (<code>null</code> not permitted).
600 * @throws NullPointerException if <code>key</code> is <code>null</code>.
601 * @throws ClassCastException if <code>key</code> is not a {@link Key}.
603 public boolean containsKey(Object key)
606 throw new NullPointerException();
607 // don't remove the cast, it is necessary to throw the required exception
608 return hintMap.containsKey((Key) key);
612 * Returns <code>true</code> if the collection of hints contains the
613 * specified value, and <code>false</code> otherwise.
615 * @param value the value.
619 public boolean containsValue(Object value)
621 return hintMap.containsValue(value);
625 * Returns the value associated with the specified key, or <code>null</code>
626 * if there is no value defined for the key.
628 * @param key the key (<code>null</code> permitted).
630 * @return The value (possibly <code>null</code>).
632 * @throws ClassCastException if <code>key</code> is not a {@link Key}.
634 * @see #containsKey(Object)
636 public Object get(Object key)
638 // don't remove the cast, it is necessary to throw the required exception
639 return hintMap.get((Key) key);
643 * Adds a (key, value) pair to the collection of hints (if the
644 * collection already contains the specified key, then the
647 * @param key the key.
648 * @param value the value.
650 * @return the previous value of the key or <code>null</code> if the key
651 * didn't have a value yet.
653 public Object put(Object key, Object value)
655 if (key == null || value == null)
656 throw new NullPointerException();
657 if (! ((Key) key).isCompatibleValue(value))
658 throw new IllegalArgumentException();
659 return hintMap.put(key, value);
663 * Adds all the hints from a collection to this collection.
665 * @param hints the hint collection.
667 public void add(RenderingHints hints)
669 hintMap.putAll(hints);
673 * Clears all the hints from this collection.
681 * Removes a hint from the collection.
683 * @param key the key.
685 * @return The value that was associated with the key, or <code>null</code> if
686 * the key was not part of the collection
688 * @throws ClassCastException if the key is not a subclass of
689 * {@link RenderingHints.Key}.
691 public Object remove(Object key)
693 // don't remove the (Key) cast, it is necessary to throw the exception
694 // required by the spec
695 return hintMap.remove((Key) key);
699 * Adds a collection of (key, value) pairs to the collection.
701 * @param m a map containing (key, value) items.
703 * @throws ClassCastException if the map contains a key that is not
704 * a subclass of {@link RenderingHints.Key}.
705 * @throws IllegalArgumentException if the map contains a value that is
706 * not compatible with its key.
708 public void putAll(Map<?,?> m)
710 // preprocess map to generate appropriate exceptions
711 Iterator iterator = m.keySet().iterator();
712 while (iterator.hasNext())
714 Key key = (Key) iterator.next();
715 if (!key.isCompatibleValue(m.get(key)))
716 throw new IllegalArgumentException();
723 * Returns a set containing the keys from this collection.
725 * @return A set of keys.
727 public Set<Object> keySet()
729 return hintMap.keySet();
733 * Returns a collection of the values from this hint collection. The
734 * collection is backed by the <code>RenderingHints</code> instance,
735 * so updates to one will affect the other.
737 * @return A collection of values.
739 public Collection<Object> values()
741 return hintMap.values();
745 * Returns a set of entries from the collection.
747 * @return A set of entries.
749 public Set<Map.Entry<Object,Object>> entrySet()
751 return Collections.unmodifiableSet(hintMap.entrySet());
755 * Checks this collection for equality with an arbitrary object.
757 * @param o the object (<code>null</code> permitted)
761 public boolean equals(Object o)
763 return hintMap.equals(o);
767 * Returns a hash code for the collection of hints.
769 * @return A hash code.
771 public int hashCode()
773 return hintMap.hashCode();
777 * Creates a clone of this instance.
781 public Object clone()
785 RenderingHints copy = (RenderingHints) super.clone();
786 copy.hintMap = new HashMap<Object,Object>(hintMap);
789 catch (CloneNotSupportedException e)
791 throw (Error) new InternalError().initCause(e); // Impossible
796 * Returns a string representation of this instance.
800 public String toString()
802 return hintMap.toString();
804 } // class RenderingHints