/* 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.jtikz.TikzGraphics2D;


/**
 *
 * @author av
 */
public class JtikzMode 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 JtikzMode
     * @param c Drawj2d console context
     */
    public JtikzMode (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: JTikZ");
        
        res = stdres;
        
        if (suffix.equals("tikz") || suffix.equals("jtikz")) {
            type = TIKZ;
        } else  {
            System.err.println("Not supported: " + suffix);
            success = false;
        }
    }
    
    /** Overwrite the default resolution.
     * No effect for tikz!
     * @param resolution dpi */
    public void setResolution(double resolution) {
        if (!quiet) System.out.println("Setting resolution without effect for tikz");
        // 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 == TIKZ;
        
        // Write the output to a file
        FileOutputStream file = null;
        try {
            file = new FileOutputStream(filename);
            TikzGraphics2D tikz = new TikzGraphics2D(file);
            if (verbose) tikz.verbose = true;
            draw(tikz);
            /*
            tikz.scale(res/inch, res/inch);
            tikz.translate(originX, originY);
            
            HCLparser parser = new HCLparser(tikz);
            parser.setVerbose(verbose);
            success = parser.sourceFile(quelldatei);
            */
            tikz.close();
        } catch (FileNotFoundException ex) {
            if (debug) Logger.getLogger(JtikzMode.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(JtikzMode.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(TikzGraphics2D tikz) {
        switch (srcType) {
            case srcTypeBgd:
                tikz.scale(res / inch, res / inch);
                tikz.translate(originX, originY); // usually 0, autoscale does also autocenter.
                BgdInterpreter bgd_interp = new BgdInterpreter(tikz, width, height);
                bgd_interp.setVerbose(verbose);
                success = bgd_interp.sourceFile(quelldatei);
                boolean autoscale = true;
                if (success) bgd_interp.interpret(autoscale);
                break;
            case srcTypeYacasPlotData:
                System.err.println("Support for ypd files not implemented in JTikZ mode.");
                success = false;
                // Reason: Font metrics support in jtikz missing!
                // Exception in thread "main" java.lang.NullPointerException
                // at net.sf.drawj2d.yacas.Grapher.paint(Grapher.java:312)
                /*
                PlotDataParser ypd_parser = new PlotDataParser(tikz, 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:
                tikz.scale(res / inch, res / inch);
                tikz.translate(originX, originY);

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

 

Ads help maintain this website.