Export: avoir memleak 50/10850/3
Calixte DENIZET [Fri, 15 Mar 2013 13:28:37 +0000 (14:28 +0100)]
Change-Id: I6473af2c5c125ce971de8b94ebe83b404b9907c9

scilab/modules/graphic_export/src/java/org/scilab/modules/graphic_export/Export.java

index f1ff989..f5ba949 100644 (file)
@@ -207,11 +207,11 @@ public class Export {
             Canvas canvas = G2DCanvasFactory.createCanvas(g2d, width, height);
             DrawerVisitor oldVisitor = DrawerVisitor.getVisitor(uid);
             DrawerVisitor visitor = new DrawerVisitor(null, canvas, figure) {
-                    @Override
-                    public void updateObject(String id, int property) {
-                        // Don't update during the export
-                    }
-                };
+                @Override
+                public void updateObject(String id, int property) {
+                    // Don't update during the export
+                }
+            };
 
             try {
                 canvas.setMainDrawer(visitor);
@@ -310,16 +310,16 @@ public class Export {
                 DrawerVisitor oldVisitor = DrawerVisitor.getVisitor(uid);
                 joglCanvas = (JoGLCanvas) JoGLCanvasFactory.createCanvas(dims[0], dims[1]);
                 DrawerVisitor visitor = new DrawerVisitor(null, joglCanvas, figure) {
-                        @Override
-                        public void updateObject(String id, int property) {
-                            // Don't update during the export
-                        }
+                    @Override
+                    public void updateObject(String id, int property) {
+                        // Don't update during the export
+                    }
 
-                        @Override
-                        public void deleteObject(String id) {
-                            // Don't delete during the export
-                        }
-                    };
+                    @Override
+                    public void deleteObject(String id) {
+                        // Don't delete during the export
+                    }
+                };
                 joglCanvas.setMainDrawer(visitor);
                 joglCanvas.redraw();
                 GraphicController.getController().unregister(visitor);
@@ -347,14 +347,14 @@ public class Export {
     public static void setVisitor(String uid, int type, final ExportParams params) {
         final Exporter exporter = getExporter(types[type]);
         Figure figure = (Figure) GraphicController.getController().getObjectFromId(uid);
-        Integer[] dims = figure.getAxesSize();
+        final Integer[] dims = figure.getAxesSize();
         int width = dims[0];
         int height = dims[1];
 
-        Graphics2D g2d = exporter.getGraphics2D(width, height, null, params);
+        final Graphics2D g2d = exporter.getGraphics2D(width, height, null, params);
         params.setParamsOnGraphics(g2d);
 
-        G2DCanvas canvas = G2DCanvasFactory.createCanvas(g2d, width, height);
+        final G2DCanvas canvas = G2DCanvasFactory.createCanvas(g2d, width, height);
         canvas.disableDraw();
         DrawerVisitor visitor = new DrawerVisitor(null, canvas, figure) {
             @Override
@@ -366,15 +366,17 @@ public class Export {
             public void updateObject(String id, int property) {
                 if (property == GraphicObjectProperties.__GO_AXES_SIZE__) {
                     Integer[] size = getFigure().getAxesSize();
-                    Graphics2D g2d = exporter.getGraphics2D(size[0], size[1], null, params);
-                    params.setParamsOnGraphics(g2d);
-
-                    G2DCanvas canvas = G2DCanvasFactory.createCanvas(g2d, size[0], size[1]);
-                    canvas.disableDraw();
-                    setCanvas(canvas);
-
-                    setDrawingTools(canvas.getDrawingTools());
-                    canvas.setMainDrawer(this);
+                    if (size[0] != dims[0] || size[1] != dims[1]) {
+                        Graphics2D newg2d = exporter.getGraphics2D(size[0], size[1], null, params);
+                        params.setParamsOnGraphics(newg2d);
+                        canvas.setGraphics(newg2d, size[0], size[1]);
+                        dims[0] = size[0];
+                        dims[1] = size[1];
+
+                        g2d.dispose();
+                    }
+                } else if (property == GraphicObjectProperties.__GO_ANTIALIASING__) {
+                    canvas.setAntiAliasingLevel(getFigure().getAntialiasing());
                 }
             }
         };
@@ -390,33 +392,33 @@ public class Export {
      */
     private static Exporter getExporter(TYPE type) {
         switch (type) {
-        case PNG :
-            return new PNGExporter();
-        case GIF :
-            return new GIFExporter();
-        case JPEG :
-            return new JPEGExporter();
-        case BMP :
-            return new BMPExporter();
-        case PPM :
-            return new PPMExporter();
-        case SVG :
-            if (!svgLoaded) {
-                ScilabCommonsUtils.loadOnUse(CLASSPATH_SVG_EXPORT_NAME);
-                svgLoaded = true;
-            }
-            return new SVGExporter();
-        case PDF :
-            loadPDF();
-            return new PDFExporter();
-        case PS :
-            loadPDF();
-            return new PSExporter();
-        case EPS :
-            loadPDF();
-            return new EPSExporter();
-        default :
-            break;
+            case PNG :
+                return new PNGExporter();
+            case GIF :
+                return new GIFExporter();
+            case JPEG :
+                return new JPEGExporter();
+            case BMP :
+                return new BMPExporter();
+            case PPM :
+                return new PPMExporter();
+            case SVG :
+                if (!svgLoaded) {
+                    ScilabCommonsUtils.loadOnUse(CLASSPATH_SVG_EXPORT_NAME);
+                    svgLoaded = true;
+                }
+                return new SVGExporter();
+            case PDF :
+                loadPDF();
+                return new PDFExporter();
+            case PS :
+                loadPDF();
+                return new PSExporter();
+            case EPS :
+                loadPDF();
+                return new EPSExporter();
+            default :
+                break;
         }
 
         return null;
@@ -590,18 +592,18 @@ public class Export {
             ctx.setEmbeddedFontsOn(true);
             g2d = new SVGGraphics2D(ctx, false) {
 
-                    @Override
-                    public void drawString(String s, float x, float y) {
-                        textAsShapes = getFont().getFontName().startsWith("jlm");
-                        super.drawString(s, x, y);
-                    }
+                @Override
+                public void drawString(String s, float x, float y) {
+                    textAsShapes = getFont().getFontName().startsWith("jlm");
+                    super.drawString(s, x, y);
+                }
 
-                    @Override
-                    public void drawString(AttributedCharacterIterator ati, float x, float y) {
-                        textAsShapes = getFont().getFontName().startsWith("jlm");
-                        super.drawString(ati, x, y);
-                    }
-                };
+                @Override
+                public void drawString(AttributedCharacterIterator ati, float x, float y) {
+                    textAsShapes = getFont().getFontName().startsWith("jlm");
+                    super.drawString(ati, x, y);
+                }
+            };
             if (params.orientation == ExportParams.LANDSCAPE) {
                 g2d.setSVGCanvasSize(new Dimension(height, width));
                 AffineTransform transf = AffineTransform.getRotateInstance(Math.PI / 2);
@@ -721,88 +723,88 @@ public class Export {
                     out = new BufferedOutputStream(new FileOutputStream(file));
                 }
                 g2d = new PSDocumentGraphics2D(true, out, width, height) {
-                        @Override
-                        protected void writePageHeader() throws IOException {
-                            super.writePageHeader();
-                            if (params.orientation == ExportParams.LANDSCAPE) {
-                                gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
-                            } else {
-                                gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Portrait");
-                            }
-                            gen.writeln("/ReEncode { /MyEncoding exch def exch findfont dup length dict begin {def} forall /Encoding MyEncoding def currentdict end definefont } def");
-                            gen.writeln("/Helvetica /HelveticaLatin1 ISOLatin1Encoding ReEncode");
-                            gen.writeln("/Times /TimesLatin1 ISOLatin1Encoding ReEncode");
+                    @Override
+                    protected void writePageHeader() throws IOException {
+                        super.writePageHeader();
+                        if (params.orientation == ExportParams.LANDSCAPE) {
+                            gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
+                        } else {
+                            gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Portrait");
                         }
+                        gen.writeln("/ReEncode { /MyEncoding exch def exch findfont dup length dict begin {def} forall /Encoding MyEncoding def currentdict end definefont } def");
+                        gen.writeln("/Helvetica /HelveticaLatin1 ISOLatin1Encoding ReEncode");
+                        gen.writeln("/Times /TimesLatin1 ISOLatin1Encoding ReEncode");
+                    }
+
+                    @Override
+                    public void drawString(String s, float x, float y) {
+                        if (s != null && !s.isEmpty()) {
+                            CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
+                            if (encoder.canEncode(s)) {
+                                Font font = getFont();
+                                boolean sserif = font.getName().equals("SansSerif");
+                                boolean serif = font.getName().equals("Serif");
+                                if (sserif || serif) {
+                                    try {
+                                        preparePainting();
+                                        establishColor(getColor());
+                                        gen.writeln((sserif ? "/HelveticaLatin1" : "/TimesLatin1") + " " + gen.formatDouble(getFont().getSize()) + " F");
+
+                                        gen.saveGraphicsState();
+                                        Shape imclip = getClip();
+                                        writeClip(imclip);
+
+                                        AffineTransform trans = getTransform();
+                                        boolean newTransform = gen.getCurrentState().checkTransform(trans) && !trans.isIdentity();
+
+                                        if (newTransform) {
+                                            gen.concatMatrix(trans);
+                                        }
+
+                                        gen.writeln(gen.formatDouble(x)
+                                                    + " " + gen.formatDouble(y)
+                                                    + " M 1 -1 scale");
 
-                        @Override
-                        public void drawString(String s, float x, float y) {
-                            if (s != null && !s.isEmpty()) {
-                                CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
-                                if (encoder.canEncode(s)) {
-                                    Font font = getFont();
-                                    boolean sserif = font.getName().equals("SansSerif");
-                                    boolean serif = font.getName().equals("Serif");
-                                    if (sserif || serif) {
-                                        try {
-                                            preparePainting();
-                                            establishColor(getColor());
-                                            gen.writeln((sserif ? "/HelveticaLatin1" : "/TimesLatin1") + " " + gen.formatDouble(getFont().getSize()) + " F");
-
-                                            gen.saveGraphicsState();
-                                            Shape imclip = getClip();
-                                            writeClip(imclip);
-
-                                            AffineTransform trans = getTransform();
-                                            boolean newTransform = gen.getCurrentState().checkTransform(trans) && !trans.isIdentity();
-
-                                            if (newTransform) {
-                                                gen.concatMatrix(trans);
-                                            }
-
-                                            gen.writeln(gen.formatDouble(x)
-                                                        + " " + gen.formatDouble(y)
-                                                        + " M 1 -1 scale");
-
-                                            StringBuffer buf = new StringBuffer("(");
-                                            for (int i = 0; i < s.length(); i++) {
-                                                PSGenerator.escapeChar(s.charAt(i), buf);
-                                            }
-                                            buf.append(") t");
-
-                                            gen.writeln(buf.toString());
-
-                                            gen.restoreGraphicsState();
-                                        } catch (IOException e) {
-                                            System.err.println(e);
+                                        StringBuffer buf = new StringBuffer("(");
+                                        for (int i = 0; i < s.length(); i++) {
+                                            PSGenerator.escapeChar(s.charAt(i), buf);
                                         }
+                                        buf.append(") t");
 
-                                        return;
+                                        gen.writeln(buf.toString());
+
+                                        gen.restoreGraphicsState();
+                                    } catch (IOException e) {
+                                        System.err.println(e);
                                     }
-                                }
 
-                                super.drawString(s, x, y);
+                                    return;
+                                }
                             }
+
+                            super.drawString(s, x, y);
                         }
+                    }
 
-                        @Override
-                        public int processShape(Shape s) throws IOException {
-                            if (s instanceof Ellipse2D.Double) {
-                                Ellipse2D.Double ell = (Ellipse2D.Double) s;
-                                if (ell.height == ell.width) {
-                                    gen.writeln(gen.formatDouble(ell.x + ell.width / 2)
-                                                + " " + gen.formatDouble(ell.y + ell.height / 2)
-                                                + " " + gen.formatDouble(ell.width / 2)
-                                                + " " + gen.formatDouble(0d)
-                                                + " " + gen.formatDouble(360d)
-                                                + " arc cp");
-
-                                    return PathIterator.WIND_NON_ZERO;
-                                }
+                    @Override
+                    public int processShape(Shape s) throws IOException {
+                        if (s instanceof Ellipse2D.Double) {
+                            Ellipse2D.Double ell = (Ellipse2D.Double) s;
+                            if (ell.height == ell.width) {
+                                gen.writeln(gen.formatDouble(ell.x + ell.width / 2)
+                                            + " " + gen.formatDouble(ell.y + ell.height / 2)
+                                            + " " + gen.formatDouble(ell.width / 2)
+                                            + " " + gen.formatDouble(0d)
+                                            + " " + gen.formatDouble(360d)
+                                            + " arc cp");
+
+                                return PathIterator.WIND_NON_ZERO;
                             }
-
-                            return super.processShape(s);
                         }
-                    };
+
+                        return super.processShape(s);
+                    }
+                };
                 g2d.setGraphicContext(new GraphicContext());
             } catch (IOException e) { }
 
@@ -853,89 +855,89 @@ public class Export {
                     out = new BufferedOutputStream(new FileOutputStream(file));
                 }
                 g2d = new EPSDocumentGraphics2D(true) {
-                        @Override
-                        protected void writePageHeader() throws IOException {
-                            super.writePageHeader();
-                            if (params.orientation == ExportParams.LANDSCAPE) {
-                                gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
-                            } else {
-                                gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Portrait");
-                            }
-                            gen.writeln("/ReEncode { /MyEncoding exch def exch findfont dup length dict begin {def} forall /Encoding MyEncoding def currentdict end definefont } def");
-                            gen.writeln("/Helvetica /HelveticaLatin1 ISOLatin1Encoding ReEncode");
-                            gen.writeln("/Times /TimesLatin1 ISOLatin1Encoding ReEncode");
+                    @Override
+                    protected void writePageHeader() throws IOException {
+                        super.writePageHeader();
+                        if (params.orientation == ExportParams.LANDSCAPE) {
+                            gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Landscape");
+                        } else {
+                            gen.writeDSCComment(DSCConstants.PAGE_ORIENTATION, "Portrait");
                         }
+                        gen.writeln("/ReEncode { /MyEncoding exch def exch findfont dup length dict begin {def} forall /Encoding MyEncoding def currentdict end definefont } def");
+                        gen.writeln("/Helvetica /HelveticaLatin1 ISOLatin1Encoding ReEncode");
+                        gen.writeln("/Times /TimesLatin1 ISOLatin1Encoding ReEncode");
+                    }
 
-                        @Override
-                        public void drawString(String s, float x, float y) {
-                            if (s != null && !s.isEmpty()) {
-                                CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
-                                if (encoder.canEncode(s)) {
-                                    Font font = getFont();
-                                    boolean sserif = font.getName().equals("SansSerif");
-                                    boolean serif = font.getName().equals("Serif");
-                                    if (sserif || serif) {
-                                        try {
-                                            preparePainting();
-                                            establishColor(getColor());
-                                            gen.writeln((sserif ? "/HelveticaLatin1" : "/TimesLatin1") + " " + gen.formatDouble(getFont().getSize()) + " F");
-
-                                            gen.saveGraphicsState();
-                                            Shape imclip = getClip();
-                                            writeClip(imclip);
-
-                                            AffineTransform trans = getTransform();
-                                            boolean newTransform = gen.getCurrentState().checkTransform(trans) && !trans.isIdentity();
-
-                                            if (newTransform) {
-                                                gen.concatMatrix(trans);
-                                            }
-
-                                            gen.writeln(gen.formatDouble(x)
-                                                        + " " + gen.formatDouble(y)
-                                                        + " M 1 -1 scale");
-
-                                            StringBuffer buf = new StringBuffer("(");
-                                            for (int i = 0; i < s.length(); i++) {
-                                                PSGenerator.escapeChar(s.charAt(i), buf);
-                                            }
-                                            buf.append(") t");
-
-                                            gen.writeln(buf.toString());
-
-                                            gen.restoreGraphicsState();
-                                        } catch (IOException e) {
-                                            System.err.println(e);
+                    @Override
+                    public void drawString(String s, float x, float y) {
+                        if (s != null && !s.isEmpty()) {
+                            CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();
+                            if (encoder.canEncode(s)) {
+                                Font font = getFont();
+                                boolean sserif = font.getName().equals("SansSerif");
+                                boolean serif = font.getName().equals("Serif");
+                                if (sserif || serif) {
+                                    try {
+                                        preparePainting();
+                                        establishColor(getColor());
+                                        gen.writeln((sserif ? "/HelveticaLatin1" : "/TimesLatin1") + " " + gen.formatDouble(getFont().getSize()) + " F");
+
+                                        gen.saveGraphicsState();
+                                        Shape imclip = getClip();
+                                        writeClip(imclip);
+
+                                        AffineTransform trans = getTransform();
+                                        boolean newTransform = gen.getCurrentState().checkTransform(trans) && !trans.isIdentity();
+
+                                        if (newTransform) {
+                                            gen.concatMatrix(trans);
+                                        }
+
+                                        gen.writeln(gen.formatDouble(x)
+                                                    + " " + gen.formatDouble(y)
+                                                    + " M 1 -1 scale");
+
+                                        StringBuffer buf = new StringBuffer("(");
+                                        for (int i = 0; i < s.length(); i++) {
+                                            PSGenerator.escapeChar(s.charAt(i), buf);
                                         }
+                                        buf.append(") t");
+
+                                        gen.writeln(buf.toString());
 
-                                        return;
+                                        gen.restoreGraphicsState();
+                                    } catch (IOException e) {
+                                        System.err.println(e);
                                     }
-                                }
 
-                                super.drawString(s, x, y);
+                                    return;
+                                }
                             }
+
+                            super.drawString(s, x, y);
                         }
+                    }
 
-                        @Override
-                        public int processShape(Shape s) throws IOException {
-                            if (s instanceof Ellipse2D.Double) {
-                                Ellipse2D.Double ell = (Ellipse2D.Double) s;
-                                if (ell.height == ell.width) {
-                                    gen.writeln(gen.formatDouble(ell.x + ell.width / 2)
-                                                + " " + gen.formatDouble(ell.y + ell.height / 2)
-                                                + " " + gen.formatDouble(ell.width / 2)
-                                                + " " + gen.formatDouble(0d)
-                                                + " " + gen.formatDouble(360d)
-                                                + " arc cp");
-
-                                    return PathIterator.WIND_NON_ZERO;
-                                }
+                    @Override
+                    public int processShape(Shape s) throws IOException {
+                        if (s instanceof Ellipse2D.Double) {
+                            Ellipse2D.Double ell = (Ellipse2D.Double) s;
+                            if (ell.height == ell.width) {
+                                gen.writeln(gen.formatDouble(ell.x + ell.width / 2)
+                                            + " " + gen.formatDouble(ell.y + ell.height / 2)
+                                            + " " + gen.formatDouble(ell.width / 2)
+                                            + " " + gen.formatDouble(0d)
+                                            + " " + gen.formatDouble(360d)
+                                            + " arc cp");
+
+                                return PathIterator.WIND_NON_ZERO;
                             }
-
-                            return super.processShape(s);
                         }
 
-                    };
+                        return super.processShape(s);
+                    }
+
+                };
                 g2d.setupDocument(out, width, height);
                 g2d.setGraphicContext(new GraphicContext());
             } catch (IOException e) { }