import java.awt.*;
import java.applet.*;
import java.util.*;
import java.net.*;
import Box;
import Parse;
import CharRender;

/****************************************************************/
/* Copyright 1996 The Regents of the University of Minnesota    */
/* All rights Reserved                                          */
/*                                                              */
/* Author: Robert R. Miner                                      */
/****************************************************************/

/****************************************************************/
/*  WebEQ is a wrapper applet class				*/
/*  init() accepts any number of parameter lines and 		*/
/*    concatenates them into an input string. It then		*/	
/*    loads the syntax tree by calling Parse.director()		*/
/****************************************************************/

public class WebEQ extends java.applet.Applet {

  String input = null;
  int[] pts = new int[3];      /* Text Font sizes */
  int[] spts = new int[3];     /* Symbol Font sizes */
  URL href;

  public Box root;

  public void init() {

    root = new Box(this);           
    int i = 1;
    String tmp;
    StringBuffer line = new StringBuffer(300);
    int app_height=100, app_width=200;
    int point=0;

    // initialize input line from parameter lines

    tmp = getParameter("size");
    if (tmp != null) {
      point = Integer.parseInt(tmp);
    } 
    else {
      point = 18;
    }

    tmp = getParameter("color");
    try {
      if (tmp != null) {
	Color bg = new Color(Integer.parseInt(tmp.substring(2), 16));
	setBackground(bg);
      }
    } catch (Exception e) {
      System.out.println("Color Format Error: use color=0xffffff");
    }

    tmp = getParameter("href");
    try {
      if (tmp != null) {
	href = new URL(getDocumentBase(), tmp);
      }
    } catch (Exception e) {
      System.out.println("href format error");
    }

    tmp = getParameter("line1");
    while(tmp != null){
      line.append(tmp);
      i++;
      tmp = getParameter("line" + Integer.toString(i));
    }
    input = line.toString();
    
    validateFonts(point);     //choose valid font sizes
      
    Parse parser = new Parse(input);
    try {
      parser.director(root, '\0');  //load syntax tree
    } catch(ParseException e) {
      System.err.println(e);
    }
    
    root.size();   //build box sizes (pass in point size);
    
    app_height = root.voffset;      // compute applet canvas size
    if (root.height - root.voffset > app_height) {
      app_height = root.height - root.voffset;
    }
    app_height = 2 * app_height + 4;
    app_width = root.width + 4;
    
    System.out.println("Approximate applet dimensions:" +  app_width + "x" + app_height); 
    root.setX(2);		   //position root box in applet 
    root.setY((int)(app_height / 2));
    root.position();	           //recursively position all boxes
  }

  public void paint(Graphics g) {
    root.paint(g);
  }

  public void start() {
    repaint();
  }

  public boolean handleEvent(Event e) {
    if (e.id == Event.MOUSE_ENTER) {
      String tmp = "WebEQ copyright 1996, The Geometry Center.  ";
      if (href != null) tmp += href.toString();
      showStatus(tmp);
      return true;
    }
    else if (e.id == Event.MOUSE_EXIT) {
      showStatus("");
      return true;
    }
    else if (e.id == Event.MOUSE_DOWN) {
      if (href != null)	getAppletContext().showDocument(href);
      return true;
    }
    return false;
  }




  public void validateFonts(int size_request) {
    int[] testsizes = {36,30,24,18,16,14,12,10,8};
    int[] symbolsizes = {34,29,25,21,18,15,0};
    int[] realsizes = new int[9];
    int[] a = new int[3];
    int numfonts = 0;
    int tmp = 0;
    int i;
    int debug = 0;
    Font fn1, fn2;
    
    for (i=0; i<9; i++) {
      fn1 = new Font("TimesRoman", Font.PLAIN,  testsizes[i]);
      fn2 = new Font("TimesRoman", Font.ITALIC, testsizes[i]);
      if (fn1 != null && fn2 != null) {
	realsizes[numfonts] = testsizes[i];
if (debug > 0) {System.out.println("system font " + testsizes[i] + " is available");}
	numfonts++;
      }
    }
    
    if (numfonts == 0) {
      System.out.println("No System Fonts!");
    }
    
    while ( (size_request < realsizes[tmp]) && (tmp < numfonts) ) {
      tmp++;
    }

    pts[0] = realsizes[tmp];           //set main font size

    int up = numfonts - tmp -1;           //set smaller sizes
    switch (up) {
      case 0:
        pts[1] = realsizes[tmp];
        pts[2] = realsizes[tmp];
	break;
      case 1:
        pts[1] = realsizes[tmp+1];
        pts[2] = realsizes[tmp+1];
	break;
      case 2:
	pts[1] = realsizes[tmp + 1];
	pts[2] = realsizes[tmp + 2];
	break;
      case 3:
	pts[1] = realsizes[tmp + 2];
	pts[2] = realsizes[tmp + 3];
	break;
      default:
	pts[1] = realsizes[tmp + 2];
	pts[2] = realsizes[tmp + 4];
      }

    for (i=0; i<3; i++) {
      fn1 = new Font("TimesRoman", Font.PLAIN,  pts[i]);
      FontMetrics fm = getFontMetrics(fn1);
      a[i] = fm.getHeight();
    }
    
    tmp=0;
    for (i=0; i<3; i++) {
      while(symbolsizes[tmp]>a[i]) {
	tmp++;
      }
      spts[i] = 7-tmp;
    }

    if (debug > 0) { 
      for (i=0; i<3; i++) {
	System.out.println("Text Font " + i + " is " + pts[i] + "pt");
	System.out.println("Symbol Font " + i + " is sym-p" + spts[i]);
      }
    }
  }

  public int standardPointsize(int depth) {
    if (depth < 0) {depth = 0;}
    if (depth > 2)  {depth = 2;}
    return pts[depth];
  }

  public int standardSymbolsize(int depth) {
    if (depth < 0) {depth = 0;}
    if (depth > 2)  {depth = 2;}
    return spts[depth];
  }
}

