weisJ commented on code in PR #7938:
URL: https://github.com/apache/netbeans/pull/7938#discussion_r1898102404


##########
ide/svg/src/org/netbeans/modules/svg/navigation/SVGPreviewPanel.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.netbeans.modules.svg.navigation;
+
+import com.github.weisj.jsvg.SVGDocument;
+import com.github.weisj.jsvg.geometry.size.FloatSize;
+import java.awt.Color;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import javax.swing.JPanel;
+import javax.swing.UIManager;
+import org.openide.awt.GraphicsUtils;
+import org.openide.util.NbBundle;
+
+/**
+ * JPanel used for SVG preview in Navigator window
+ *
+ * @author christian lenz
+ */
+public class SVGPreviewPanel extends JPanel {
+
+    private SVGDocument svgDocument;
+    private final int stringGapSize = 10;
+    private final Color background = UIManager.getColor("Table.background");
+    private final Color foreground = UIManager.getColor("Table.foreground");
+
+    public void setSVG(SVGDocument svgDoc) {
+        this.svgDocument = svgDoc;
+        this.setBackground(background);
+        this.revalidate();
+        this.repaint();
+    }
+
+    @Override
+    protected void paintComponent(Graphics g) {
+        GraphicsUtils.configureDefaultRenderingHints(g);
+        super.paintComponent(g);
+
+        if (svgDocument != null) {
+            g.setColor(foreground);
+
+            FloatSize size = svgDocument.size();
+            int originalWidth = (int) size.getWidth();
+            int originalHeight = (int) size.getHeight();
+            String sizes = "Dimensions: " + originalWidth + " x " + 
originalHeight;
+
+            g.drawString(sizes, (int) (this.getWidth() * 0.05), 
this.getHeight() - stringGapSize);
+
+            int thumbnailMaxSize = 100;
+
+            double widthRatio = (double) originalWidth / thumbnailMaxSize;
+            double heightRatio = (double) originalHeight / thumbnailMaxSize;
+            double ratio = 1.0;
+
+            if (widthRatio > 1 || heightRatio > 1) {
+                ratio = widthRatio > heightRatio ? widthRatio : heightRatio;
+            }
+
+            int scaledWidth = (int) (originalWidth / ratio);
+            int scaledHeight = (int) (originalHeight / ratio);
+
+            Graphics2D g2d = (Graphics2D) g.create((this.getWidth() - 
scaledWidth) / 2, (this.getHeight() - scaledHeight) / 2, scaledWidth, 
scaledHeight);

Review Comment:
   Rendering hints should also be set here to make rendering consistent across 
components.



##########
ide/svg/src/org/netbeans/modules/svg/SVGPanel.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.netbeans.modules.svg;
+
+import com.github.weisj.jsvg.SVGDocument;
+
+import java.awt.*;
+import javax.swing.*;
+
+/**
+ *
+ * @author Christian Lenz
+ */
+public class SVGPanel extends JPanel {
+
+    private SVGDocument svgDocument;
+    private double scale = 1.0D;
+    private BackgroundMode bgMode = BackgroundMode.DEFAULT;
+
+    public void setSVGDocument(SVGDocument doc) {
+        this.svgDocument = doc;
+
+        repaint();
+    }
+
+    public void setScale(double scale) {
+        this.scale = scale;
+
+        repaint();
+    }
+
+    public void setBackgroundMode(BackgroundMode mode) {
+        this.bgMode = mode;
+
+        repaint();
+    }
+
+    @Override
+    protected void paintComponent(Graphics g) {
+        super.paintComponent(g);
+
+        Rectangle visibleRect = getVisibleRectangle();
+
+        switch (bgMode) {
+            case BLACK -> {
+                g.setColor(Color.BLACK);
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+            case WHITE -> {
+                g.setColor(Color.WHITE);
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+
+            case TRANSPARENT, DARK_TRANSPARENT ->
+                Utils.drawChestTilePattern(g, visibleRect, 20, bgMode == 
BackgroundMode.DARK_TRANSPARENT);
+            case DEFAULT -> {
+                g.setColor(getBackground());
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+        }
+
+        if (svgDocument == null) {
+            return;
+        }
+
+        Graphics2D g2d = (Graphics2D) g.create();
+
+        try {
+            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
+            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
RenderingHints.VALUE_STROKE_PURE);
+            g2d.scale(scale, scale);
+
+            svgDocument.render(this, g2d);

Review Comment:
   You might want to wrap this in a `try {} catch {}` block. Exceptions might 
happen due to bugs and you probably don't want the UI to crash in this case.



##########
ide/svg/src/org/netbeans/modules/svg/SVGPanel.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.netbeans.modules.svg;
+
+import com.github.weisj.jsvg.SVGDocument;
+
+import java.awt.*;
+import javax.swing.*;
+
+/**
+ *
+ * @author Christian Lenz
+ */
+public class SVGPanel extends JPanel {
+
+    private SVGDocument svgDocument;
+    private double scale = 1.0D;
+    private BackgroundMode bgMode = BackgroundMode.DEFAULT;
+
+    public void setSVGDocument(SVGDocument doc) {
+        this.svgDocument = doc;
+
+        repaint();
+    }
+
+    public void setScale(double scale) {
+        this.scale = scale;
+
+        repaint();
+    }
+
+    public void setBackgroundMode(BackgroundMode mode) {
+        this.bgMode = mode;
+
+        repaint();
+    }
+
+    @Override
+    protected void paintComponent(Graphics g) {
+        super.paintComponent(g);
+
+        Rectangle visibleRect = getVisibleRectangle();
+
+        switch (bgMode) {
+            case BLACK -> {
+                g.setColor(Color.BLACK);
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+            case WHITE -> {
+                g.setColor(Color.WHITE);
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+
+            case TRANSPARENT, DARK_TRANSPARENT ->
+                Utils.drawChestTilePattern(g, visibleRect, 20, bgMode == 
BackgroundMode.DARK_TRANSPARENT);
+            case DEFAULT -> {
+                g.setColor(getBackground());
+                g.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, 
visibleRect.height);
+            }
+        }
+
+        if (svgDocument == null) {
+            return;
+        }
+
+        Graphics2D g2d = (Graphics2D) g.create();
+
+        try {
+            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
+            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
RenderingHints.VALUE_STROKE_PURE);

Review Comment:
   There are more SVG specific rendering hints in 
`com.github.weisj.jsvg.SVGRenderingHints`, which you might want to enable here. 
Specifically `KEY_MASK_CLIP_RENDERING` could probably be set to 
`VALUE_MASK_CLIP_RENDERING_ACCURACY` here. It will be a bit slower if masks are 
used, however only in this case it will produce the correct result in all case. 
The default is `VALUE_MASK_CLIP_RENDERING_FAST` which makes some assumptions 
about what features SVGs use to make some optimisations.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to