/* Copyright (C) 2014-2016 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.cmd;

import org.hecl.*;

/**
 * UnitCmd implements the "unitlength" command, which sets unit lenght.
 */

public class UnitCmd implements Command {
    API api;
    boolean debug = false;
    
    public UnitCmd (API api) {
        this.api = api;
    }
    
    @Override
    public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
        assert argv.length > 0;
        if (debug) System.out.println(argv[0].toString());
        
        
        switch (argv.length - 1) {
            case 0:
                // just return the unit length
                // prior to version 0.8.1 the command "unitlength" reset the unit length to 1: api.unitlength(1);
                break;
            case 1:
                double unit_in_mm = NumberThing.asNumber(argv[1]).doubleValue();
                api.unitlength(unit_in_mm);
                break;
            case 2:
                double scale = NumberThing.asNumber(argv[1]).doubleValue();
                String unitname = StringThing.get(argv[2]);
                double unit;
                switch (unitname) {
                    case "mm":
                        unit = 1;
                        break;
                    case "cm":
                        unit = 10;
                        break;
                    case "meter":
                    case "m":
                        unit = 1000;
                        break;
                    case "km":
                        unit = 1000000;
                        break;
                    case "µm":
                        unit = 0.001;
                        break;
                    case "nm":
                        unit = 0.000001;
                        break;
                    case "inch":
                    case "in":
                        unit = 25.4;
                        break;
                    case "foot":
                    case "feet":
                    case "ft":
                        unit = 12*25.4;
                        break;
                    case "yard":
                    case "yd":
                        unit = 3*12*25.4;
                        break;
                    case "mile":
                        unit = 5280*12*25.4;
                        break;
                    case "point": // PostScript point
                    case "pt":
                        unit = 25.4 / 72.;
                        break;
                    default:
                        try {
                            unit = Double.parseDouble(unitname);
                        } catch (NumberFormatException e) {
                            throw new HeclException("Unknown unit: " + unitname);
                        }
                }
                api.unitlength(scale, unit);
                break;
            default:
                throw new HeclException("Illegal nb of arguments.");
        }
        
        String answer = StringThing.get(DoubleThing.create(api.getunitlength()));
        
	return new Thing(answer);
    }
}

 

Ads help maintain this website.