/* Copyright (C) 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.*;
/**
* TextCmd implements the "text" command, which
* writes some text.
*/
public class TextCmd implements Command {
API api;
boolean debug = false;
public TextCmd (API api) {
this.api = api;
}
@Override
public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
if (debug) System.out.println(argv[0].toString());
String text;
if (argv.length > 1) {
text = StringThing.get(argv[1]);
} else text = "";
double availablewidth;
boolean justified;
switch (argv.length - 1) {
case 0: // line feed
api.text("");
break;
case 1: // label
api.text(text);
break;
case 2: // label, availablewidth
try {
availablewidth = DoubleThing.get(argv[2]);
justified = false;
} catch (NumberFormatException e) {
availablewidth = -1; // use previous value
justified = getTextStyle(StringThing.get(argv[2]));
}
api.text(text, availablewidth, justified);
break;
case 3: // label, availablewidth, justified
availablewidth = 0;
String str = "";
boolean ok;
try {
availablewidth = DoubleThing.get(argv[2]);
str = StringThing.get(argv[3]);
ok = true;
} catch (NumberFormatException e) {
ok = false;
}
if (!ok) {
try {
str = StringThing.get(argv[2]);
availablewidth = DoubleThing.get(argv[3]);
ok = true;
} catch (NumberFormatException e) {
ok = false;
}
}
if (ok) {
justified = getTextStyle(str);
api.text(text, availablewidth, justified);
} else {
throw new HeclException ("Illegal arguments for text: " + StringThing.get(argv[2]) + " " + StringThing.get(argv[3]));
}
break;
default:
throw new HeclException ("Illegal nb of arguments.");
}
return null;
}
private boolean getTextStyle(String str) throws HeclException {
String s = str.toLowerCase();
if (s.equals("justify")) return true;
if (s.equals("left")) return false;
throw new HeclException ("Illegal argument for text alignment : " + str);
}
}
Ads help maintain this website.