 /*****************************************************************************
 FontInfo.java

 An applet to show FontMetrics information

 bruce.miller@nist.gov
 (NIST is a part of the US Government; 
 software is therefore not subject to copyright)
******************************************************************************/

package test;
import java.applet.*;
import java.awt.*;
import gui.*;

public class FontInfo extends Applet {
  Toolkit toolkit;
  String fontnames[], stylenames[] = new String[3];
  ChoiceInput fontChoice, styleChoice;
  IntegerInput sizeField;
  Checkbox patchField;
  Button describeButton;

  SampleCanvas sample;
  TextArea output;

  public void init() {
    stylenames[Font.PLAIN] = "Plain";
    stylenames[Font.BOLD] = "Bold";
    stylenames[Font.ITALIC] = "Italic";
    setLayout(new StackLayout(StackLayout.VERTICAL));

    toolkit = getToolkit();
    fontnames = toolkit.getFontList();
    
    add("TopLeft",fontChoice = new ChoiceInput("Font Name",fontnames));
    add("TopLeft",sizeField = new IntegerInput("Font Size",12,10,2,32));
    add("TopLeft",styleChoice=new ChoiceInput("Font Style",stylenames));
    add("TopLeft",patchField = new Checkbox("Apply Patch?",null,false));

    add("TopCenter", describeButton = new Button("Describe Font"));
    add("Fill",sample = new SampleCanvas());
    add("Fill",output = new TextArea());     
  }

  void describeFont() {
    try {
      String name  = fontChoice.getChoice();
      int    size  = sizeField.intValue();
      int    snum  = styleChoice.getSelectedIndex();
      String style = styleChoice.getChoice();
      boolean patched = patchField.getState();

      Font font = new Font(name,snum,size);
      FontMetrics orig = getFontMetrics(font);
      FontMetrics metrics = (patched ? PatchFontMetrics.patch(orig) : orig);
      sample.setMetrics(font,metrics);
      output.setText("Font : " + name + 
                      " size = " + size + 
                      " style= " + style+ 
                      ", Is " + (patched ? "" : "not") + " patched\n");
      output.appendText("Height  = " + metrics.getHeight() + "\n");
      output.appendText("Ascent  = " + metrics.getAscent() + 
                      " MaxAscent  = " + metrics.getMaxAscent() + "\n");
      output.appendText("Descent = " + metrics.getDescent() +
                      " MaxDescent  = " + metrics.getMaxDescent() + "\n");
      output.appendText("Leading = " + metrics.getLeading() + "\n");
      output.appendText("MaxAdvance = " + metrics.getMaxAdvance() + "\n");
      String interesting = "Wix";
      for(int i=0; i<interesting.length(); i++) {
        char c = interesting.charAt(i);
        output.appendText("Width of " + c +" = " + metrics.charWidth(c) + "\n");}
      } catch(Exception e) { new PopUpNotice(e); }}

  public boolean handleEvent(Event e) {
    if((e.id == Event.ACTION_EVENT) && (e.target == describeButton)) {
      describeFont(); 
      return true; }
    return false; }
}


class SampleCanvas extends Canvas {
   String text = "Text Sample";
   Font f;
   FontMetrics m;
   
  void setMetrics(Font font, FontMetrics metrics) {
     f = font;
     m = metrics;
     repaint(); }

  public void paint(Graphics g) {
    if (m != null) {
      Dimension s = size();
      int h = m.getHeight();
      int w = m.stringWidth(text);
      int x = (s.width-w)/2;
      int y = (s.height+h)/2;
      g.setFont(f);
      g.drawString(text,x,y);
      g.drawRect(x-1,y-h-1,w+2,h+2); }}
}
