Skip to content

Commit

Permalink
Fix addressed feedback (Add error handling, add rendering hints, code…
Browse files Browse the repository at this point in the history
… cleanups)
  • Loading branch information
Chris2011 committed Dec 27, 2024
1 parent 920fe5a commit 70a5764
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
10 changes: 9 additions & 1 deletion ide/svg/src/org/netbeans/modules/svg/SVGPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package org.netbeans.modules.svg;

import com.github.weisj.jsvg.SVGDocument;

import com.github.weisj.jsvg.SVGRenderingHints;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
Expand All @@ -29,6 +31,8 @@
*/
public class SVGPanel extends JPanel {

private static final Logger LOG = Logger.getLogger(SVGViewerElement.class.getName());

private SVGDocument svgDocument;
private double scale = 1.0D;
private BackgroundMode bgMode = BackgroundMode.DEFAULT;
Expand Down Expand Up @@ -84,9 +88,13 @@ protected void paintComponent(Graphics g) {
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY);

g2d.scale(scale, scale);

svgDocument.render(this, g2d);
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage());
} finally {
g2d.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ private void setNewContent(final DataObject dataObject) {
getComponent();
}

try {
SVGLoader svgLoader = new SVGLoader();
SVGDocument svgDocument = svgLoader.load(fo.toURL());
SVGLoader svgLoader = new SVGLoader();
SVGDocument svgDocument = svgLoader.load(fo.toURL());

SwingUtilities.invokeLater(() -> panelUI.setSVG(svgDocument));
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage());
if (svgDocument == null) {
return;
}

SwingUtilities.invokeLater(() -> panelUI.setSVG(svgDocument));
});
}

Expand Down Expand Up @@ -199,7 +199,7 @@ public void fileChanged(final FileEvent fe) {
currentDataObject = DataObject.find(fe.getFile());
setNewContent(currentDataObject);
} catch (DataObjectNotFoundException ex) {
Logger.getLogger(SVGNavigatorPanel.class.getName()).info(NbBundle.getMessage(SVGNavigatorPanel.class, "ERR_DataObject"));
LOG.log(Level.INFO, NbBundle.getMessage(SVGNavigatorPanel.class, "ERR_DataObject"));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
package org.netbeans.modules.svg.navigation;

import com.github.weisj.jsvg.SVGDocument;
import com.github.weisj.jsvg.SVGRenderingHints;
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 java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.netbeans.modules.svg.SVGViewerElement;
import org.openide.awt.GraphicsUtils;
import org.openide.util.NbBundle;

Expand All @@ -36,6 +41,8 @@
*/
public class SVGPreviewPanel extends JPanel {

private static final Logger LOG = Logger.getLogger(SVGViewerElement.class.getName());

private SVGDocument svgDocument;
private final int stringGapSize = 10;
private final Color background = UIManager.getColor("Table.background");
Expand Down Expand Up @@ -78,11 +85,19 @@ protected void paintComponent(Graphics g) {

Graphics2D g2d = (Graphics2D) g.create((this.getWidth() - scaledWidth) / 2, (this.getHeight() - scaledHeight) / 2, scaledWidth, scaledHeight);

g2d.scale(1.0 / ratio, 1.0 / ratio);
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setRenderingHint(SVGRenderingHints.KEY_MASK_CLIP_RENDERING, SVGRenderingHints.VALUE_MASK_CLIP_RENDERING_ACCURACY);

svgDocument.render(this, g2d);
g2d.scale(1.0 / ratio, 1.0 / ratio);

g2d.dispose();
svgDocument.render(this, g2d);
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage());
} finally {
g2d.dispose();
}
} else {
g.setColor(Color.RED);
FontMetrics fm = this.getFontMetrics(g.getFont());
Expand Down

0 comments on commit 70a5764

Please sign in to comment.