Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/utils/RenderingUtils.java

raw
1 /*
2 * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.utils;
31
32 import java.awt.*;
33 import java.awt.print.PrinterGraphics;
34 import java.util.*;
35
36 import javax.swing.CellRendererPane;
37 import javax.swing.SwingUtilities;
38
39 public class RenderingUtils {
40 private static final String PROP_DESKTOPHINTS = "awt.font.desktophints";
41
42 private static Map<String, Map> desktopHintsCache = new HashMap<String, Map>();
43
44 public static Map installDesktopHintsOld(Graphics2D g2, Component c) {
45 if (SwingUtilities.getAncestorOfClass(CellRendererPane.class, c) != null) {
46 return null;
47 }
48
49 Map oldRenderingHints = null;
50 Map desktopHints = desktopHints(g2);
51 if (desktopHints != null && !desktopHints.isEmpty()) {
52 oldRenderingHints = new HashMap(desktopHints.size());
53 RenderingHints.Key key;
54 for (Iterator i = desktopHints.keySet().iterator(); i.hasNext();) {
55 key = (RenderingHints.Key) i.next();
56 oldRenderingHints.put(key, g2.getRenderingHint(key));
57 }
58 g2.addRenderingHints(desktopHints);
59 if (c != null) {
60 Font font = c.getFont();
61 if (font != null) {
62 if (font.getSize() > 15) {
63 g2.setRenderingHint(
64 RenderingHints.KEY_TEXT_ANTIALIASING,
65 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
66 }
67 }
68 }
69 } else {
70 // the following is temporary until the Apple VM 6.0 supports the
71 // desktop AA hinting settings.
72 if (LookUtils.IS_JAVA_6 && LookUtils.IS_OS_MAC) {
73 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
74 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
75 }
76 }
77 return oldRenderingHints;
78 }
79
80 public static void installDesktopHints(Graphics2D g2, Component c) {
81 if (SwingUtilities.getAncestorOfClass(CellRendererPane.class, c) != null) {
82 return;
83 }
84
85 Map desktopHints = desktopHints(g2);
86 if (desktopHints != null && !desktopHints.isEmpty()) {
87 g2.addRenderingHints(desktopHints);
88 if (c != null) {
89 Font font = c.getFont();
90 if (font != null) {
91 if (font.getSize() > 15) {
92 g2.setRenderingHint(
93 RenderingHints.KEY_TEXT_ANTIALIASING,
94 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
95 }
96 }
97 }
98 } else {
99 // the following is temporary until the Apple VM 6.0 supports the
100 // desktop AA hinting settings.
101 if (LookUtils.IS_JAVA_6 && LookUtils.IS_OS_MAC) {
102 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
103 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
104 }
105 }
106 }
107
108 private static Map desktopHints(Graphics2D g2) {
109 if (isPrinting(g2)) {
110 return null;
111 }
112 Toolkit toolkit = Toolkit.getDefaultToolkit();
113 String deviceId = "";
114 GraphicsConfiguration config = g2.getDeviceConfiguration();
115 if (config != null) {
116 GraphicsDevice device = config.getDevice();
117 if (device != null) {
118 deviceId = device.getIDstring();
119 }
120 }
121 if (!desktopHintsCache.containsKey(deviceId)) {
122 Map desktopHints = (Map) toolkit
123 .getDesktopProperty(PROP_DESKTOPHINTS + '.'
124 + deviceId);
125 if (desktopHints == null) {
126 desktopHints = (Map) toolkit
127 .getDesktopProperty(PROP_DESKTOPHINTS);
128 }
129 // It is possible to get a non-empty map but with disabled AA.
130 if (desktopHints != null) {
131 Object aaHint = desktopHints
132 .get(RenderingHints.KEY_TEXT_ANTIALIASING);
133 if ((aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)
134 || (aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) {
135 desktopHints = null;
136 }
137 }
138
139 if (desktopHints == null)
140 desktopHints = new HashMap();
141
142 desktopHintsCache.put(deviceId, desktopHints);
143 }
144
145 return desktopHintsCache.get(deviceId);
146 }
147
148 private static boolean isPrinting(Graphics g) {
149 return g instanceof PrintGraphics || g instanceof PrinterGraphics;
150 }
151 }