/* Copyright (C) 2014 A.Vontobel  
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The license is in LICENCE-GPL.txt.
 * If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 */

package net.sf.drawj2d;

import net.sf.drawj2d.cmd.HCLparser;
import net.sf.drawj2d.yacas.PlotDataParser;
import net.sf.drawj2d.bgd.BgdInterpreter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.epsgraphics.ColorMode;
import net.sf.epsgraphics.EpsGraphics;


/**
 *
 * @author av
 */
public class EpsgraphicsMode implements inConst {
    private double res;
    private int pxwidth;
    private int pxheight;
    
    boolean success = true;
    private int type;
    private final boolean quiet;
    private final boolean verbose;
    private final String filename;
    private final String suffix;
    private final String quelldatei;
    private final double width;
    private final double height;
    private final double originX;
    private final double originY;
    private final int srcType;
    
    private final boolean debug = false;
    
    /**
     * Creates a new instance of EpsgraphicsMode
     * @param c Drawj2d console context
     */
    public EpsgraphicsMode (Drawj2d c) {
        quiet = c.quiet;
        verbose = c.verbose;
        width = c.width;
        height = c.height;
        originX = c.originX;
        originY = c.originY;
        filename = c.zieldatei; 
        suffix = c.outputtype.toLowerCase();
        quelldatei = c.quelldatei;
        srcType = c.srcType;
        if (!quiet) System.out.println("Mode: EpsGraphics");
        
        res = stdres;
        
        if (suffix.equals("eps") || suffix.equals("ps")) {
            type = EPS;
        } else  {
            System.err.println("Not supported: " + suffix);
            success = false;
        }
    }
    
    /** Overwrite the default resolution.
     * No effect for eps!
     * @param resolution dpi */
    public void setResolution(double resolution) {
        if (!quiet) System.out.println("Setting resolution without effect for eps");
        // always keep default resolution 72dpi
    }
    
    
    /** Create the drawing.
     * @return true if successful */
    public boolean run() {
        pxwidth = (int) Math.rint(width*res/inch);
        pxheight = (int) Math.rint(height*res/inch);
        
        if (success) writeOutput();
        return success;
    }
    
    private void writeOutput() {
        assert type == EPS;
        
        // Write the output to a file
        FileOutputStream file = null;
        try {
            file = new FileOutputStream(filename);
//            try {
//                Class c = Class.forName("net.sf.epsgraphics.EpsGraphics");
//            } catch (ClassNotFoundException ce) {
//                throw new Exception("EpsGraphics-printer not available.");
//            }
            EpsGraphics eps = new EpsGraphics(filename, file,
                    0, 0, pxwidth, pxheight, ColorMode.COLOR_RGB);
            
            draw(eps);
            
            eps.close();
        } catch (FileNotFoundException ex) {
            if (debug) Logger.getLogger(EpsgraphicsMode.class.getName()).log(Level.SEVERE, null, ex);
            else System.err.println(ex.getLocalizedMessage());
            success = false;
        } catch (IOException ex) {
            if (debug) Logger.getLogger(EpsgraphicsMode.class.getName()).log(Level.SEVERE, null, ex);
            else System.err.println(ex.getLocalizedMessage());
            success = false;
        } finally {
            try {
                if (file != null) {
                    file.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(EpsgraphicsMode.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if (success) {
            System.out.println("Output written to: " + filename);
        }
        else {
            System.out.println("Error occurred. Check for error messages.");
        }
    }
    
    private void draw(EpsGraphics eps) {
        switch (srcType) {
            case srcTypeBgd:
                eps.scale(res / inch, res / inch);
                eps.translate(originX, originY); // usually 0, autoscale does also autocenter.
                BgdInterpreter bgd_interp = new BgdInterpreter(eps, width, height);
                bgd_interp.setVerbose(verbose);
                success = bgd_interp.sourceFile(quelldatei);
                boolean autoscale = true;
                if (success) bgd_interp.interpret(autoscale);
                break;
            case srcTypeYacasPlotData:
                PlotDataParser ypd_parser = new PlotDataParser(eps, pxwidth, pxheight);
                ypd_parser.setOrigin((int) Math.rint(originX*res/inch), (int) Math.rint(originY*res/inch));
                ypd_parser.setVerbose(verbose);
                success = ypd_parser.sourceFile(quelldatei);
                break;
            case srcTypeHcl:
            default:
                eps.scale(res / inch, res / inch);
                eps.translate(originX, originY);

                HCLparser parser = new HCLparser(eps);
                parser.setVerbose(verbose);
                success = parser.sourceFile(quelldatei);
        }
    }
}

 

Ads help maintain this website.