FontMetrics return garbage font descent

175 Views Asked by At

I am facing a weird problem in Eclipse, and even after lot of search, haven't found any Bugs also in this regard. My Problem is with the handle field in FontMetrics Class. Since the API says its Platform Dependent, there is not much I can do about it. The problem is like this:

I have to export some diagrams, made of draw2d widgets and connections, to Word and PDF. Till now, the export feature was available as an Action to the toolbar of the Editor, in which Diagrams are drawn. It has been working fine. All I do is paint the FigureCanvas to an SWT Image, and save it to a File. There are APIs available with me, which then insert it to Word/PDF. Now, I need to that offline, i.e. without actually drawing the diagram on Screen. I did something like this to achieve this:

Job job = new Job("Making DFD for " + data.getName()) {
        @SuppressWarnings("unchecked")
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            final Display display = new Display();
            final Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
            Composite composite = new Composite(shell, SWT.NONE);
            try {

                imageFolder = new File(tempFolder + IOUtils.FILE_SEPARATOR + 
                        "dfd-" + (new Date()).getTime());

                composite.setLayout(new FillLayout());
                final FigureCanvas canvas = new FigureCanvas(composite);
                ArrayList<DFDFigureData> figureData = renderer.getDfdFigureDatas();
                final DFDMaker3 dfdMaker;
                dfdMaker = new DFDMaker3(canvas, "", figureData, null, false);
                Logger.println("Shell Size:", shell.computeSize(-1, -1, true));
                display.syncExec(new Runnable(){
                    public void run() {
                        dfdMaker.makeDFD();
                        shell.setSize(-1, -1);
                        dfdMaker.selectNode(-1, display);
                    }
                });
                Logger.println("Shell Size:", shell.computeSize(-1, -1, true));

                /* Image Export Stuff Goes here */
                return Status.OK_STATUS;
            } catch (Exception e) {
                Logger.println("Error in DFD Creation Job", e.toString());
                return Status.CANCEL_STATUS;
            } finally {
                composite.dispose();
                shell.dispose();
                display.dispose();
            }
        }
    };
    job.setPriority(Job.SHORT);
    job.schedule();

When I run this for the first time, both the Log statements tell me a good story:


Shell Size::: Point {72, 98} Shell Size::: Point {1216, 524}


But when I run the same code 2nd time, without closing the application, I get:


Shell Size::: Point {72, 98} Shell Size::: Point {1216, 1945541082}


The large Height value of the shell spoils everything. After intense debugging, I found that the second time, a FlowPage, that I am using, get a wrong value for Font's descent. The method FontMetrics.getDescent() returns a Random large value.

I am not sure how exactly to proceed in this. I've disposed all the resources that I used the first time. The Display, Shell, Composite, Canvas, and even the GC and SWTGraphics. Can anyone tell me if its a bug? If not, any idea how can I find the problem here?

0

There are 0 best solutions below