You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by eb...@apache.org on 2021/09/29 22:00:31 UTC
[netbeans] 01/02: [NETBEANS-5931 part 1/2] Fix missing antialiasing
in non-editable property sheet values on Windows. Move code for
configuration of rendering hints out of HtmlLabelUI and into a new common
GraphicsUtils class.
This is an automated email from the ASF dual-hosted git repository.
ebakke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
commit f46794972d01e249a40930af916acc37192d12c5
Author: Eirik Bakke <eb...@ultorg.com>
AuthorDate: Tue Sep 28 18:08:22 2021 -0400
[NETBEANS-5931 part 1/2] Fix missing antialiasing in non-editable property sheet values on Windows. Move code for configuration of rendering hints out of HtmlLabelUI and into a new common GraphicsUtils class.
---
platform/openide.awt/apichanges.xml | 16 ++++
.../src/org/openide/awt/GraphicsUtils.java | 100 +++++++++++++++++++++
.../src/org/openide/awt/HtmlLabelUI.java | 48 +---------
.../src/org/openide/awt/HtmlRenderer.java | 18 +---
platform/openide.explorer/nbproject/project.xml | 2 +-
.../explorer/propertysheet/RendererFactory.java | 2 +
6 files changed, 122 insertions(+), 64 deletions(-)
diff --git a/platform/openide.awt/apichanges.xml b/platform/openide.awt/apichanges.xml
index 2fa337b..66498e9 100644
--- a/platform/openide.awt/apichanges.xml
+++ b/platform/openide.awt/apichanges.xml
@@ -26,6 +26,22 @@
<apidef name="awt">AWT API</apidef>
</apidefs>
<changes>
+ <change id="configureDefaultRenderingHints">
+ <api name="awt"/>
+ <summary>Added GraphicsUtils.configureDefaultRenderingHints(Graphics).</summary>
+ <version major="7" minor="82"/>
+ <date day="28" month="9" year="2021"/>
+ <author login="ebakke"/>
+ <compatibility addition="yes" binary="compatible" source="compatible" semantic="compatible" deprecation="no" deletion="no" modification="no"/>
+ <description>
+ Add the GraphicsUtils.configureDefaultRenderingHints(Graphics) method (with a new
+ GraphicsUtils class), to allow configuration of standard anti-aliasing settings from custom
+ JComponent.paint(Graphics) implementations. These standard incantations were previously
+ duplicated extensively throughout the NetBeans codebase.
+ </description>
+ <class package="org.openide.awt" name="GraphicsUtils"/>
+ <issue number="NETBEANS-5931"/>
+ </change>
<change id="getArrowIcon">
<api name="awt"/>
<summary>Added DropDownButtonFactory.getArrowIcon method.</summary>
diff --git a/platform/openide.awt/src/org/openide/awt/GraphicsUtils.java b/platform/openide.awt/src/org/openide/awt/GraphicsUtils.java
new file mode 100644
index 0000000..ba0c3c2
--- /dev/null
+++ b/platform/openide.awt/src/org/openide/awt/GraphicsUtils.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.openide.awt;
+
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.Toolkit;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import javax.swing.UIManager;
+import javax.swing.JComponent;
+
+public final class GraphicsUtils {
+ private GraphicsUtils() {
+ }
+
+ private static final boolean antialias =
+ // System property to automatically turn on antialiasing for html strings
+ Boolean.getBoolean("nb.cellrenderer.antialiasing") // NOI18N
+ ||Boolean.getBoolean("swing.aatext") // NOI18N
+ ||(isGTK() && gtkShouldAntialias()) // NOI18N
+ || isAqua();
+ private static Boolean gtkAA;
+ private static Map<Object,Object> hintsMap;
+
+ private static boolean isAqua () {
+ return "Aqua".equals(UIManager.getLookAndFeel().getID());
+ }
+
+ private static boolean isGTK () {
+ return "GTK".equals(UIManager.getLookAndFeel().getID());
+ }
+
+ private static final boolean gtkShouldAntialias() {
+ if (gtkAA == null) {
+ Object o = Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/Antialias"); //NOI18N
+ gtkAA = Integer.valueOf(1).equals(o);
+ }
+
+ return gtkAA.booleanValue();
+ }
+
+ /**
+ * Configure default IDE-wide rendering hints on the supplied {@link Graphics} object. This
+ * enables anti-aliasing of manually painted text and 2D graphics, using settings that are
+ * consistent throughout the IDE. This method is typically called at the beginning of custom
+ * {@link JComponent#paint(Graphics)} implementations. By convention, callers passing the
+ * {@code Graphics} object from {@code paint} do not need to bother restoring the old rendering
+ * hints after they are done using the {@code Graphics} object.
+ */
+ public static void configureDefaultRenderingHints(Graphics graphics) {
+ if (graphics == null) {
+ throw new NullPointerException();
+ }
+ if (graphics instanceof Graphics2D) {
+ ((Graphics2D) graphics).addRenderingHints(getRenderingHints());
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private static final Map<?,?> getRenderingHints() {
+ Map<Object,Object> ret = hintsMap;
+ if (ret == null) {
+ //Thanks to Phil Race for making this possible
+ ret = (Map)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
+ if (ret == null) {
+ ret = new HashMap<Object,Object>();
+ if (antialias) {
+ ret.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ }
+ }
+ if (antialias ||
+ !RenderingHints.VALUE_TEXT_ANTIALIAS_OFF.equals(ret.get(RenderingHints.KEY_TEXT_ANTIALIASING)))
+ {
+ // Required to get non-text antialiasing on Windows.
+ ret.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ }
+ hintsMap = Collections.unmodifiableMap(ret);
+ }
+ return ret;
+ }
+}
diff --git a/platform/openide.awt/src/org/openide/awt/HtmlLabelUI.java b/platform/openide.awt/src/org/openide/awt/HtmlLabelUI.java
index 2584179..89b7330 100644
--- a/platform/openide.awt/src/org/openide/awt/HtmlLabelUI.java
+++ b/platform/openide.awt/src/org/openide/awt/HtmlLabelUI.java
@@ -25,14 +25,8 @@ import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
-import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
-import java.awt.RenderingHints;
-import java.awt.Toolkit;
-
-import java.util.HashMap;
-import java.util.Map;
import javax.swing.Icon;
import javax.swing.JComponent;
@@ -46,14 +40,6 @@ import org.openide.util.Exceptions;
* instance should ever exist.
*/
class HtmlLabelUI extends LabelUI {
-
- /** System property to automatically turn on antialiasing for html strings */
-
- static final boolean antialias = Boolean.getBoolean("nb.cellrenderer.antialiasing") // NOI18N
- ||Boolean.getBoolean("swing.aatext") // NOI18N
- ||(isGTK() && gtkShouldAntialias()) // NOI18N
- || isAqua();
-
private static HtmlLabelUI uiInstance;
private static int FIXED_HEIGHT;
@@ -71,10 +57,8 @@ class HtmlLabelUI extends LabelUI {
}
}
- private static Map<Object,Object> hintsMap;
private static Color unfocusedSelBg;
private static Color unfocusedSelFg;
- private static Boolean gtkAA;
public static ComponentUI createUI(JComponent c) {
assert c instanceof HtmlRendererImpl;
@@ -171,7 +155,7 @@ class HtmlLabelUI extends LabelUI {
//Antialiasing affects the text metrics, so use it if needed when
//calculating preferred size or the result here will be narrower
//than the space actually needed
- ((Graphics2D) g).addRenderingHints(getHints());
+ GraphicsUtils.configureDefaultRenderingHints(g);
int textwidth = textWidth(text, g, font, r.isHtml()) + 4;
@@ -188,24 +172,6 @@ class HtmlLabelUI extends LabelUI {
return prefSize;
}
- @SuppressWarnings("unchecked")
- static final Map<?,?> getHints() {
- //XXX We REALLY need to put this in a graphics utils lib
- if (hintsMap == null) {
- //Thanks to Phil Race for making this possible
- hintsMap = (Map)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
- if (hintsMap == null) {
- hintsMap = new HashMap<Object,Object>();
- if (antialias) {
- hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- }
- }
- }
- Map<?,?> ret = hintsMap;
- assert ret != null; // does this method need to be synchronized?
- return ret;
- }
-
public @Override void update(Graphics g, JComponent c) {
Color bg = getBackgroundFor((HtmlRendererImpl) c);
HtmlRendererImpl h = (HtmlRendererImpl) c;
@@ -245,8 +211,7 @@ class HtmlLabelUI extends LabelUI {
}
public @Override void paint(Graphics g, JComponent c) {
-
- ((Graphics2D) g).addRenderingHints(getHints());
+ GraphicsUtils.configureDefaultRenderingHints(g);
HtmlRendererImpl r = (HtmlRendererImpl) c;
@@ -581,13 +546,4 @@ class HtmlLabelUI extends LabelUI {
return unfocusedSelFg;
}
-
- public static final boolean gtkShouldAntialias() {
- if (gtkAA == null) {
- Object o = Toolkit.getDefaultToolkit().getDesktopProperty("gnome.Xft/Antialias"); //NOI18N
- gtkAA = Integer.valueOf(1).equals(o);
- }
-
- return gtkAA.booleanValue();
- }
}
diff --git a/platform/openide.awt/src/org/openide/awt/HtmlRenderer.java b/platform/openide.awt/src/org/openide/awt/HtmlRenderer.java
index 493cbe1..d67daf3 100644
--- a/platform/openide.awt/src/org/openide/awt/HtmlRenderer.java
+++ b/platform/openide.awt/src/org/openide/awt/HtmlRenderer.java
@@ -24,18 +24,14 @@ import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
-import java.awt.Graphics2D;
import java.awt.Rectangle;
-import java.awt.RenderingHints;
import java.awt.Shape;
-import java.awt.Toolkit;
import java.awt.font.LineMetrics;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
-import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
@@ -520,18 +516,6 @@ public final class HtmlRenderer {
return _renderHTML( s, pos, g, x, y, w, h, f, defaultColor, style, paint, background, false );
}
- private static void configureRenderingHints(Graphics graphics) {
- Graphics2D g = (Graphics2D) graphics;
- Object desktopHints
- = Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints");
- if (desktopHints instanceof Map<?, ?>) {
- g.addRenderingHints((Map<?, ?>) desktopHints);
- } else if (HtmlLabelUI.antialias) {
- g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- }
-
- }
-
/** Implementation of HTML rendering */
static double _renderHTML(
String s, int pos, Graphics g, int x, int y, int w, int h, Font f, Color defaultColor, int style, boolean paint,
@@ -558,7 +542,7 @@ public final class HtmlRenderer {
g.setColor(defaultColor);
g.setFont(f);
- configureRenderingHints(g);
+ GraphicsUtils.configureDefaultRenderingHints(g);
char[] chars = s.toCharArray();
int origX = x;
diff --git a/platform/openide.explorer/nbproject/project.xml b/platform/openide.explorer/nbproject/project.xml
index 3ed8b7d..0da4747 100644
--- a/platform/openide.explorer/nbproject/project.xml
+++ b/platform/openide.explorer/nbproject/project.xml
@@ -46,7 +46,7 @@
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
- <specification-version>7.43</specification-version>
+ <specification-version>7.82</specification-version>
</run-dependency>
</dependency>
<dependency>
diff --git a/platform/openide.explorer/src/org/openide/explorer/propertysheet/RendererFactory.java b/platform/openide.explorer/src/org/openide/explorer/propertysheet/RendererFactory.java
index 63fa686..d3a5f9f 100644
--- a/platform/openide.explorer/src/org/openide/explorer/propertysheet/RendererFactory.java
+++ b/platform/openide.explorer/src/org/openide/explorer/propertysheet/RendererFactory.java
@@ -62,6 +62,7 @@ import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicGraphicsUtils;
+import org.openide.awt.GraphicsUtils;
import org.openide.awt.HtmlRenderer;
import org.openide.nodes.Node.Property;
import org.openide.util.ImageUtilities;
@@ -791,6 +792,7 @@ final class RendererFactory {
&& ! isEnabled() && ! htmlValueUsed) {
// the shadow effect from the label was making a problem
// let's paint the text "manually" in this case
+ GraphicsUtils.configureDefaultRenderingHints(g);
g.setColor(lbl.getBackground());
g.fillRect(0, 0, lbl.getWidth(), lbl.getHeight());
g.setColor(lbl.getForeground());
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists