Package ket.math.purpose.function

Source Code of ket.math.purpose.function.TextFunctionForm

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* 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 3 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, see <http://www.gnu.org/licenses/>
*/

package ket.math.purpose.function;

import java.awt.*;
import java.util.*;

import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxList;
import ket.display.box.BoxTools;
import ket.display.box.BoxWord;
import ket.math.*;
import ket.math.convert.Like;
import ket.math.purpose.NumberValue;
import ket.math.purpose.SymbolicState;

/**
* A specific mathematical operation that acts on arguments in a case-specific
* way.
*/
public class TextFunctionForm extends FunctionForm {

  public TextFunctionForm(Function f) {
    super(f);
  }

  @Override
  public String toLatex(Vector<Argument> args) {
    switch (args.size()) {
      case 0:
        return Symbol.CROSS.toLatex() + "()";
      case 1:
        return Symbol.CROSS.toLatex() + args.firstElement().toLatex();
      default :
        // Only explicitly display a multiplication sign when displaying no arguments,
        // one argument or side-by-side number pairs. 
        if (Like.numberLikeList(args)) {
          String latexString = "";
          latexString += args.get(0).toLatex();
          for (int i=1; i<args.size(); i++) {
            latexString += Symbol.TIMES.toLatex() + " " + args.get(i).toLatex();
          }
          return latexString;
        } else {
          String latexString = ""; // Compress this repeated code into a single method (see Text...)
          latexString += args.get(0).toLatex();
          for (int i=1; i<args.size(); i++) {
            latexString += " " + args.get(i).toLatex();
          }
          return latexString;
        }
    }
  }

  @Override
  public String toHTML(Vector<Argument> args) {
    boolean explicit = Like.numberLikeList(args); // Show * symbols rather than spaces?
    String operation = explicit ? Symbol.TIMES.toHTML() : "&nbsp;";
    switch (args.size()) {
      case 0:
        return Symbol.TIMES.toHTML();
      case 1:
        return operation + args.firstElement().toHTML();
      default:
        String html = "";
        for (int i=0; i<args.size()-1; i++) {
          html += args.get(i).toHTML() + operation;
        }
        html += args.lastElement().toHTML();
        return html;
    }
  }

  @Override
  public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
    settings |= Box.LEFT_ALIGN;
    settings |= Box.TOP_ALIGN;
    int minimumValidNumberOfArguments = getArgLowerLimit(args.size());
    Vector<Box> boxArgs = new Vector<Box>();
    appendRecurToBox(boxArgs, args, colourScheme);
    int missingNumberOfArgs = minimumValidNumberOfArguments-args.size();
    for (int j=0; j<missingNumberOfArgs; j++) {
      //- Box box = new BoxWord(null, "?", 0L, colourScheme.getAbsentColour());
      Box box = new BoxWord(null, "?", 0L);
      boxArgs.add(box);
    }
    if (boxArgs.size()<2) {
      return super.toBox(argument, settings, colourScheme, args);
    }
    Box gap = new BoxWord(null, " ", Box.PLAIN_FONT|Box.LEFT_ALIGN|Box.TOP_ALIGN);
    BoxList box = BoxTools.operatorFactory(argument, null, gap, null, boxArgs, settings);
    box.setWrap();
    return box;
  }

  private void appendRecurToBox(Vector<Box> boxArgs, Vector<Argument> args, ColourScheme colourScheme) {
    for (Argument argument : args) {
      if (argument.getFunction() instanceof TextFunction) {
        Vector<Argument> kids = argument.asBranch().getChildren();
        appendRecurToBox(boxArgs, kids, colourScheme);
      } else {
        boxArgs.add(argument.toBox(Box.SUPPRESS_BRACKETS, colourScheme));
      }
    }
  }

}
TOP

Related Classes of ket.math.purpose.function.TextFunctionForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.