/* Copyright (C) 2014-2015 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.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JPanel;
import javax.imageio.ImageIO;

import org.freehep.graphics2d.VectorGraphics;
import org.freehep.graphicsio.pdf.PDFGraphics2D;
import org.freehep.graphicsio.svg.SVGGraphics2D;
import org.freehep.graphicsio.ps.PSGraphics2D;
import org.freehep.graphicsio.emf.EMFGraphics2D;
import org.freehep.graphicsio.ImageGraphics2D;
import org.freehep.graphicsbase.swing.Headless;

/**
 *
 * @author av
 */
public class FreehepMode implements inConst {
    protected double res;

    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;
    protected final double width;
    protected final double height;
    private final double originX;
    private final double originY;
    private final int srcType;

    private JPanel yourPanel;
    private Dimension dim;
    
    private final boolean debug = false;

    /**
     * Creates a new instance of FreehepMode
     *
     * @param c Drawj2d console context
     */
    public FreehepMode(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: Freehep");

        res = stdres;
        if (suffix.equals("svg")) {
            type = SVG;
            res = svgres;
        } else if (suffix.equals("eps") || suffix.equals("ps")) {
            type = EPS;
        } else if (suffix.equals("pdf")) {
            type = PDF;
        } else if (suffix.equals("emf")) {
            type = EMF;
            res = emfres;
        } else {
            String IOsuffixes[] = ImageIO.getWriterFileSuffixes();
            for (String t : IOsuffixes) {
                if (debug) System.out.println(t); //aus JRE1.7: jpg,bmp,jpeg,wbmp,png,gif, aus freehep: ppm, raw
                if (t.equals(suffix)) {
                    type = IMG;
                    res = imgres;
                }
            }
        }
    }
  
    /** Overwrite the default resolution.
     * No effect for pdf and eps
     * @param resolution dpi */
    public void setResolution(double resolution) {
        switch (type) {
                case PDF:
                case EPS:
                    if (!quiet) System.out.println("Setting resolution without effect for " + suffix);
                    // always keep default resolution 72dpi
                    break;
                case EMF:
                case SVG:
                case IMG:
                default:
                    res = resolution;
        }
    }
    
    /** Create the drawing.
     * @return true if successful */
    public boolean run() {
        int pxwidth = (int) Math.rint(width * res / inch);
        int pxheight = (int) Math.rint(height * res / inch);
        dim = new Dimension(pxwidth, pxheight);

        yourPanel = new YourPanel(dim, this, quelldatei, originX, originY, verbose, srcType);

        // run with -Djava.awt.headless=true
        Headless headless = new Headless(yourPanel);
        headless.pack();
        headless.setVisible(true);

        writeOutput();
        return success;
    }

    private void writeOutput() {
        File out = new File(filename);
        VectorGraphics graphics = null;
        try {
            switch (type) {
                case SVG:
                    // Create a new SVG document
                    graphics = new SVGGraphics2D(out, yourPanel);
                    graphics.setCreator("Drawj2d");
                    break;
                case EPS:
                    // Create a new EPS document
                    graphics = new PSGraphics2D(out, yourPanel);
                    Properties peps = new Properties();
                    peps.setProperty(PSGraphics2D.PAGE_SIZE, PSGraphics2D.CUSTOM_PAGE_SIZE);
                    peps.setProperty(PSGraphics2D.CUSTOM_PAGE_SIZE, "" + dim.width + ", " + dim.height);
                    peps.setProperty(PSGraphics2D.PAGE_MARGINS, "0, 0, 0, 0");
                    peps.setProperty(PSGraphics2D.FIT_TO_PAGE, "false");
                    //peps.setProperty(PSGraphics2D.PREVIEW, "true");
                    // TODO bounding box
                    graphics.setProperties(peps);
                    break;
                case PDF:
                    // Create a new PDF document
                    graphics = new PDFGraphics2D(out, yourPanel);
                    Properties p = new Properties();
                    p.setProperty(PDFGraphics2D.PAGE_SIZE, PDFGraphics2D.CUSTOM_PAGE_SIZE);
                    p.setProperty(PDFGraphics2D.CUSTOM_PAGE_SIZE, "" + dim.width + ", " + dim.height);
                    p.setProperty(PDFGraphics2D.PAGE_MARGINS, "0, 0, 0, 0");
                    p.setProperty(PDFGraphics2D.FIT_TO_PAGE, "false");
                    graphics.setProperties(p);
                    graphics.setCreator("Drawj2d");
                    break;
                case EMF:
                    // Create a new SVG document
                    graphics = new EMFGraphics2D(out, yourPanel);
                    graphics.setDeviceIndependent(true);
                    break;
                case IMG:
                    // Create a new IMG document
                    graphics = new ImageGraphics2D(out, yourPanel, suffix);
                    break;
                default:
                    System.err.println("Not supported: " + suffix);
                    success = false;
            }
        } catch (FileNotFoundException ex) {
            if (debug) Logger.getLogger(FreehepMode.class.getName()).log(Level.SEVERE, null, ex);
            else System.err.println(ex.getLocalizedMessage());
            graphics = null;
            success = false;
        } catch (IOException ex) {
            if (debug) Logger.getLogger(FreehepMode.class.getName()).log(Level.SEVERE, null, ex);
            else System.err.println(ex.getLocalizedMessage());
            graphics = null;
            success = false;
        }

        if (success && graphics != null) {
            graphics.startExport();
            yourPanel.print(graphics);
            graphics.endExport();
        }

        if (success) {
            System.out.println("Output written to: " + filename);
        } else {
            System.out.println("Error occurred. Check for error messages.");
        }
    }
}

class YourPanel extends JPanel implements inConst {

    private final Dimension dim;
    private double originX = 0;
    private double originY = 0;
    private final int srcType;
    private final String quelldatei;
    private final boolean verbose;
    FreehepMode mode;

    YourPanel(Dimension dim, FreehepMode mode, String quelldatei, double originX, double originY, boolean verbose, int srcType) {
        this.mode = mode;
        this.quelldatei = quelldatei;
        this.dim = dim;
        this.originX = originX;
        this.originY = originY;
        this.verbose = verbose;
        this.srcType = srcType;
        setPreferredSize(dim);
    }

    @Override
    public void paint(Graphics g) { //paintComponent(Graphics g) {
        if (g == null) {
            return;
        }

        VectorGraphics vg = VectorGraphics.create(g);
        
        Graphics2D tempGraphics = (Graphics2D) vg.create();
        tempGraphics.setStroke(vg.getStroke());
        
        draw(tempGraphics);
        
        tempGraphics.dispose();
    }
    
    private void draw(Graphics2D g) {
        switch (srcType) {
            case srcTypeBgd:
                g.scale(mode.res / inch, mode.res / inch);
                g.translate(originX, originY); // usually 0, autoscale does also autocenter.
                BgdInterpreter bgd_interp = new BgdInterpreter(g, mode.width, mode.height);
                bgd_interp.setVerbose(verbose);
                mode.success = bgd_interp.sourceFile(quelldatei);
                boolean autoscale = true;
                if (mode.success) bgd_interp.interpret(autoscale);
                break;
            case srcTypeYacasPlotData:
                PlotDataParser ypd_parser = new PlotDataParser(g, dim.width, dim.height);
                ypd_parser.setOrigin((int) Math.rint(originX*mode.res/inch), (int) Math.rint(originY*mode.res/inch));
                ypd_parser.setVerbose(verbose);
                mode.success = ypd_parser.sourceFile(quelldatei);
                break;
            case srcTypeHcl:
            default:
                g.scale(mode.res / inch, mode.res / inch);
                g.translate(originX, originY);
                HCLparser vg_parser = new HCLparser(g);
                vg_parser.setVerbose(verbose);
                mode.success = vg_parser.sourceFile(quelldatei);
        }
    }
}

 

Ads help maintain this website.