/* 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 java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import org.hecl.*;

/**
 * FontCmd implements the "font" command, which 
 * sets font type (Serif, Sans_Serif, Monospaced) and size.
 */

public class FontCmd implements Command {
    API api;
    boolean debug = false;
    
    public FontCmd (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());
        String font = "";
        int style = Font.PLAIN;
        int size = 0;
        
        if (argv.length == 1) {
            api.font();
        } else {
            assert argv.length > 1;
            for (int i = 1; i < argv.length; i++) {
                Thing arg = argv[i];
                if (NumberThing.isNumber(arg)) {
                    size = NumberThing.asNumber(arg).intValue();
                }
                else {
                    String str = StringThing.get(arg);
                    int fontstyle = getFontStyle(str);
                    if (fontstyle >= 0) {
                        style = fontstyle;
                    } else {
                        font = str;
                    }
                }
            }
            try {
                api.font(font, style, size);
            }
            catch (FontFormatException e) {throw new HeclException(e.getLocalizedMessage());}
            catch (IOException e) {throw new HeclException(e.getLocalizedMessage());}
        }
	return null;
    }
    
    private int getFontStyle(String str) {
        String s = str.toLowerCase();
        if (s.equals("bold") || s.equals("bf")) return Font.BOLD;
        if (s.equals("italic") || s.equals("it")) return Font.ITALIC;
        if (s.equals("plain") || s.equals("up")) return Font.PLAIN; // upright
        return -1;
    }
    
}

 

Ads help maintain this website.