You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2019/11/21 13:21:33 UTC

[GitHub] [netbeans] sdedic commented on a change in pull request #1278: [NETBEANS-2604] Support SVG icon loading from ImageUtilities

sdedic commented on a change in pull request #1278: [NETBEANS-2604] Support SVG icon loading from ImageUtilities
URL: https://github.com/apache/netbeans/pull/1278#discussion_r348528039
 
 

 ##########
 File path: platform/openide.util.ui.svg/src/org/openide/util/svg/SVGIcon.java
 ##########
 @@ -0,0 +1,215 @@
+/*
+ * 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.util.svg;
+
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.RenderingHints;
+import java.awt.geom.Dimension2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.Icon;
+import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
+import org.apache.batik.bridge.BridgeContext;
+import org.apache.batik.bridge.DocumentLoader;
+import org.apache.batik.bridge.GVTBuilder;
+import org.apache.batik.bridge.UserAgent;
+import org.apache.batik.bridge.UserAgentAdapter;
+import org.apache.batik.ext.awt.image.GraphicsUtil;
+import org.apache.batik.gvt.GraphicsNode;
+import org.apache.batik.util.XMLResourceDescriptor;
+import org.openide.util.CachedHiDPIIcon;
+import org.openide.util.Parameters;
+import org.w3c.dom.Document;
+
+/**
+ * An icon loaded from an SVG file resource. Renders in high resolution on HiDPI displays.
+ * Thread-safe.
+ */
+final class SVGIcon extends CachedHiDPIIcon {
+    private static final Logger LOG = Logger.getLogger(SVGIcon.class.getName());
+    /* A limit of 8192 pixels on each side means the resulting maximum image buffer would take 268
+    megabytes. It's also twice twice as long as the longest side of a 4K display. This is small
+    enough to avoid an OutOfMemoryError but large enough to cater for most SVG loading scenarios.
+    Photoshop had 10000 pixels as a maximum limit for many years. */
+    private static final int MAX_DIMENSION_PIXELS = 8192;
+    // XML document factories are expensive to initialize, so do it once per thread only.
+    private static final ThreadLocal<SAXSVGDocumentFactory> DOCUMENT_FACTORY =
+            new ThreadLocal<SAXSVGDocumentFactory>()
+    {
+        @Override
+        protected SAXSVGDocumentFactory initialValue() {
+            return new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
+        }
+    };
+
+    private final URL url;
+    /**
+     * Cache of the parsed SVG document. Just painting the GraphicsNode is much faster than also
+     * re-parsing the underlying SVG file, yet we want to avoid keeping potentially complex object
+     * trees in memory for the lifetime of the Icon instance. Thus we allow the GraphicsNode to be
+     * garbage collected after the first paint. The rasterized bitmap will be cached separately by
+     * the superclass.
+     */
+    private WeakReference<GraphicsNode> graphicsNodeWeakRef;
+    /**
+     * A strong reference version of {@link #graphicsNodeWeakRef}, which can be set to ensure that
+     * the latter is not yet garbage collected. Used to ensure that the initially loaded
+     * GraphicsNode is cached at least until the first time the icon is painted. May be null.
+     */
+    private GraphicsNode graphicsNodeStrongRef;
+
+    private SVGIcon(URL url, GraphicsNode initialGraphicsNode, int width, int height) {
+        super(width, height);
+        Parameters.notNull("url", url);
+        Parameters.notNull("initialGraphicsNode", initialGraphicsNode);
+        if (width < 0) {
 
 Review comment:
   This is probably useless, as the same check is in the superclass.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists