Word like special font chooser : Font Chooser « Swing Components « Java






Word like special font chooser

    

/*
 * @(#)Font2DTest.java  1.28 05/11/17
 * 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @(#)Font2DTest.java  1.28 05/11/17
 */

import static java.awt.RenderingHints.KEY_ANTIALIASING;
import static java.awt.RenderingHints.KEY_FRACTIONALMETRICS;
import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING;
import static java.awt.RenderingHints.KEY_TEXT_LCD_CONTRAST;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_OFF;
import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON;
import static java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT;
import static java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
import static java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.AttributedString;
import java.util.BitSet;
import java.util.EnumSet;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Font2DTest.java
 * 
 * @version
 * @(#)Font2DTest.java 1.2 00/08/22
 * @author Shinsuke Fukuda
 * @author Ankit Patel [Conversion to Swing - 01/07/30]
 */

// / Main Font2DTest Class
public final class Font2DTest extends JPanel implements ActionListener, ItemListener,
    ChangeListener {

  // / JFrame that will contain Font2DTest
  private final JFrame parent;

  // / FontPanel class that will contain all graphical output
  private final FontPanel fp;

  // / RangeMenu class that contains info about the unicode ranges
  private final RangeMenu rm;

  // / Other menus to set parameters for text drawing
  private final ChoiceV2 fontMenu;

  private final JTextField sizeField;

  private final ChoiceV2 styleMenu;

  private final ChoiceV2 textMenu;

  private int currentTextChoice = 0;

  private final ChoiceV2 transformMenu;

  private final ChoiceV2 transformMenuG2;

  private final ChoiceV2 methodsMenu;

  private final JComboBox antiAliasMenu;

  private final JComboBox fracMetricsMenu;

  private final JSlider contrastSlider;

  // / CheckboxMenuItems
  private CheckboxMenuItemV2 displayGridCBMI;

  private CheckboxMenuItemV2 force16ColsCBMI;

  private CheckboxMenuItemV2 showFontInfoCBMI;

  // / JDialog boxes
  private JDialog userTextDialog;

  private JTextArea userTextArea;

  private JDialog printDialog;

  private JDialog fontInfoDialog;

  private LabelV2 fontInfos[] = new LabelV2[2];

  private JFileChooser filePromptDialog = null;

  private ButtonGroup printCBGroup;

  private JRadioButton printModeCBs[] = new JRadioButton[3];

  // / Status bar
  private final LabelV2 statusBar;

  private int fontStyles[] = { Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC };

  // / Text filename
  private String tFileName;

  // Enabled or disabled status of canDisplay check
  private static boolean canDisplayCheck = true;

  // / Initialize GUI variables and its layouts
  public Font2DTest(JFrame f, boolean isApplet) {
    parent = f;

    rm = new RangeMenu(this, parent);
    fp = new FontPanel(this, parent);
    statusBar = new LabelV2("");

    fontMenu = new ChoiceV2(this, canDisplayCheck);
    sizeField = new JTextField("12", 3);
    sizeField.addActionListener(this);
    styleMenu = new ChoiceV2(this);
    textMenu = new ChoiceV2(); // listener added later
    transformMenu = new ChoiceV2(this);
    transformMenuG2 = new ChoiceV2(this);
    methodsMenu = new ChoiceV2(this);

    antiAliasMenu = new JComboBox(EnumSet.allOf(FontPanel.AAValues.class).toArray());
    antiAliasMenu.addActionListener(this);
    fracMetricsMenu = new JComboBox(EnumSet.allOf(FontPanel.FMValues.class).toArray());
    fracMetricsMenu.addActionListener(this);

    contrastSlider = new JSlider(JSlider.HORIZONTAL, 100, 250, FontPanel.getDefaultLCDContrast()
        .intValue());
    contrastSlider.setEnabled(false);
    contrastSlider.setMajorTickSpacing(20);
    contrastSlider.setMinorTickSpacing(10);
    contrastSlider.setPaintTicks(true);
    contrastSlider.setPaintLabels(true);
    contrastSlider.addChangeListener(this);
    setupPanel();
    setupMenu(isApplet);
    setupDialog(isApplet);

    if (canDisplayCheck) {
      fireRangeChanged();
    }
  }

  // / Set up the main interface panel
  private void setupPanel() {
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 1;
    gbc.insets = new Insets(2, 0, 2, 2);
    this.setLayout(gbl);

    addLabeledComponentToGBL("Font: ", fontMenu, gbl, gbc, this);
    addLabeledComponentToGBL("Size: ", sizeField, gbl, gbc, this);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    addLabeledComponentToGBL("Font Transform:", transformMenu, gbl, gbc, this);
    gbc.gridwidth = 1;

    addLabeledComponentToGBL("Range: ", rm, gbl, gbc, this);
    addLabeledComponentToGBL("Style: ", styleMenu, gbl, gbc, this);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    addLabeledComponentToGBL("Graphics Transform: ", transformMenuG2, gbl, gbc, this);
    gbc.gridwidth = 1;

    gbc.anchor = GridBagConstraints.WEST;
    addLabeledComponentToGBL("Method: ", methodsMenu, gbl, gbc, this);
    addLabeledComponentToGBL("", null, gbl, gbc, this);
    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    addLabeledComponentToGBL("Text to use:", textMenu, gbl, gbc, this);

    gbc.weightx = 1;
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.WEST;
    addLabeledComponentToGBL("LCD contrast: ", contrastSlider, gbl, gbc, this);

    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;
    addLabeledComponentToGBL("Antialiasing: ", antiAliasMenu, gbl, gbc, this);

    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    addLabeledComponentToGBL("Fractional metrics: ", fracMetricsMenu, gbl, gbc, this);

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(2, 0, 0, 2);
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(fp, gbc);
    this.add(fp);

    gbc.weighty = 0;
    gbc.insets = new Insets(0, 2, 0, 0);
    gbl.setConstraints(statusBar, gbc);
    this.add(statusBar);
  }

  // / Adds a component to a container with a label to its left in GridBagLayout
  private void addLabeledComponentToGBL(String name, JComponent c, GridBagLayout gbl,
      GridBagConstraints gbc, Container target) {
    LabelV2 l = new LabelV2(name);
    GridBagConstraints gbcLabel = (GridBagConstraints) gbc.clone();
    gbcLabel.insets = new Insets(2, 2, 2, 0);
    gbcLabel.gridwidth = 1;
    gbcLabel.weightx = 0;

    if (c == null)
      c = new JLabel("");

    gbl.setConstraints(l, gbcLabel);
    target.add(l);
    gbl.setConstraints(c, gbc);
    target.add(c);
  }

  // / Sets up menu entries
  private void setupMenu(boolean isApplet) {
    JMenu fileMenu = new JMenu("File");
    JMenu optionMenu = new JMenu("Option");

    fileMenu.add(new MenuItemV2("Save Selected Options...", this));
    fileMenu.add(new MenuItemV2("Load Options...", this));
    fileMenu.addSeparator();
    fileMenu.add(new MenuItemV2("Save as PNG...", this));
    fileMenu.add(new MenuItemV2("Load PNG File to Compare...", this));
    fileMenu.add(new MenuItemV2("Page Setup...", this));
    fileMenu.add(new MenuItemV2("Print...", this));
    fileMenu.addSeparator();
    if (!isApplet)
      fileMenu.add(new MenuItemV2("Exit", this));
    else
      fileMenu.add(new MenuItemV2("Close", this));

    displayGridCBMI = new CheckboxMenuItemV2("Display Grid", true, this);
    force16ColsCBMI = new CheckboxMenuItemV2("Force 16 Columns", false, this);
    showFontInfoCBMI = new CheckboxMenuItemV2("Display Font Info", false, this);
    optionMenu.add(displayGridCBMI);
    optionMenu.add(force16ColsCBMI);
    optionMenu.add(showFontInfoCBMI);

    JMenuBar mb = parent.getJMenuBar();
    if (mb == null)
      mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(optionMenu);

    parent.setJMenuBar(mb);

    String fontList[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getAvailableFontFamilyNames();

    for (int i = 0; i < fontList.length; i++)
      fontMenu.addItem(fontList[i]);
    fontMenu.setSelectedItem("Dialog");

    styleMenu.addItem("Plain");
    styleMenu.addItem("Bold");
    styleMenu.addItem("Italic");
    styleMenu.addItem("Bold Italic");

    transformMenu.addItem("None");
    transformMenu.addItem("Scale");
    transformMenu.addItem("Shear");
    transformMenu.addItem("Rotate");

    transformMenuG2.addItem("None");
    transformMenuG2.addItem("Scale");
    transformMenuG2.addItem("Shear");
    transformMenuG2.addItem("Rotate");

    methodsMenu.addItem("drawString");
    methodsMenu.addItem("drawChars");
    methodsMenu.addItem("drawBytes");
    methodsMenu.addItem("drawGlyphVector");
    methodsMenu.addItem("TextLayout.draw");
    methodsMenu.addItem("GlyphVector.getOutline + draw");
    methodsMenu.addItem("TextLayout.getOutline + draw");

    textMenu.addItem("Unicode Range");
    textMenu.addItem("All Glyphs");
    textMenu.addItem("User Text");
    textMenu.addItem("Text File");
    textMenu.addActionListener(this); // listener added later so unneeded events
                                      // not thrown
  }

  // / Sets up the all dialogs used in Font2DTest...
  private void setupDialog(boolean isApplet) {
    if (!isApplet)
      filePromptDialog = new JFileChooser();
    else
      filePromptDialog = null;

    // / Prepare user text dialog...
    userTextDialog = new JDialog(parent, "User Text", false);
    JPanel dialogTopPanel = new JPanel();
    JPanel dialogBottomPanel = new JPanel();
    LabelV2 message1 = new LabelV2("Enter text below and then press update");
    LabelV2 message2 = new LabelV2("(Unicode char can be denoted by \\uXXXX)");
    LabelV2 message3 = new LabelV2("(Supplementary chars can be denoted by \\UXXXXXX)");
    userTextArea = new JTextArea("Java2D!");
    ButtonV2 bUpdate = new ButtonV2("Update", this);
    userTextArea.setFont(new Font("dialog", Font.PLAIN, 12));
    dialogTopPanel.setLayout(new GridLayout(3, 1));
    dialogTopPanel.add(message1);
    dialogTopPanel.add(message2);
    dialogTopPanel.add(message3);
    dialogBottomPanel.add(bUpdate);
    // ABP
    JScrollPane userTextAreaSP = new JScrollPane(userTextArea);
    userTextAreaSP.setPreferredSize(new Dimension(300, 100));

    userTextDialog.getContentPane().setLayout(new BorderLayout());
    userTextDialog.getContentPane().add("North", dialogTopPanel);
    userTextDialog.getContentPane().add("Center", userTextAreaSP);
    userTextDialog.getContentPane().add("South", dialogBottomPanel);
    userTextDialog.pack();
    userTextDialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        userTextDialog.hide();
      }
    });

    // / Prepare printing dialog...
    printCBGroup = new ButtonGroup();
    printModeCBs[fp.ONE_PAGE] = new JRadioButton(
        "Print one page from currently displayed character/line", true);
    printModeCBs[fp.CUR_RANGE] = new JRadioButton(
        "Print all characters in currently selected range", false);
    printModeCBs[fp.ALL_TEXT] = new JRadioButton("Print all lines of text", false);
    LabelV2 l = new LabelV2(
        "Note: Page range in native \"Print\" dialog will not affect the result");
    JPanel buttonPanel = new JPanel();
    printModeCBs[fp.ALL_TEXT].setEnabled(false);
    buttonPanel.add(new ButtonV2("Print", this));
    buttonPanel.add(new ButtonV2("Cancel", this));

    printDialog = new JDialog(parent, "Print...", true);
    printDialog.setResizable(false);
    printDialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        printDialog.hide();
      }
    });
    printDialog.getContentPane().setLayout(new GridLayout(printModeCBs.length + 2, 1));
    printDialog.getContentPane().add(l);
    for (int i = 0; i < printModeCBs.length; i++) {
      printCBGroup.add(printModeCBs[i]);
      printDialog.getContentPane().add(printModeCBs[i]);
    }
    printDialog.getContentPane().add(buttonPanel);
    printDialog.pack();

    // / Prepare font information dialog...
    fontInfoDialog = new JDialog(parent, "Font info", false);
    fontInfoDialog.setResizable(false);
    fontInfoDialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        fontInfoDialog.hide();
        showFontInfoCBMI.setState(false);
      }
    });
    JPanel fontInfoPanel = new JPanel();
    fontInfoPanel.setLayout(new GridLayout(fontInfos.length, 1));
    for (int i = 0; i < fontInfos.length; i++) {
      fontInfos[i] = new LabelV2("");
      fontInfoPanel.add(fontInfos[i]);
    }
    fontInfoDialog.getContentPane().add(fontInfoPanel);

    // / Move the location of the dialog...
    userTextDialog.setLocation(200, 300);
    fontInfoDialog.setLocation(0, 400);
  }

  // / RangeMenu object signals using this function
  // / when Unicode range has been changed and text needs to be redrawn
  public void fireRangeChanged() {
    int range[] = rm.getSelectedRange();
    fp.setTextToDraw(fp.RANGE_TEXT, range, null, null);
    if (canDisplayCheck) {
      setupFontList(range[0], range[1]);
    }
    if (showFontInfoCBMI.getState())
      fireUpdateFontInfo();
  }

  // / Changes the message on the status bar
  public void fireChangeStatus(String message, boolean error) {
    // / If this is not ran as an applet, use own status bar,
    // / Otherwise, use the appletviewer/browser's status bar
    statusBar.setText(message);
    if (error)
      fp.showingError = true;
    else
      fp.showingError = false;
  }

  // / Updates the information about the selected font
  public void fireUpdateFontInfo() {
    if (showFontInfoCBMI.getState()) {
      String infos[] = fp.getFontInfo();
      for (int i = 0; i < fontInfos.length; i++)
        fontInfos[i].setText(infos[i]);
      fontInfoDialog.pack();
    }
  }

  private void setupFontList(int rangeStart, int rangeEnd) {

    int listCount = fontMenu.getItemCount();
    int size = 16;

    try {
      size = Float.valueOf(sizeField.getText()).intValue();
    } catch (Exception e) {
      System.out.println("Invalid font size in the size textField. Using default value of 16");
    }

    int style = fontStyles[styleMenu.getSelectedIndex()];
    Font f;
    for (int i = 0; i < listCount; i++) {
      String fontName = (String) fontMenu.getItemAt(i);
      f = new Font(fontName, style, size);
      if ((rm.getSelectedIndex() != RangeMenu.SURROGATES_AREA_INDEX)
          && canDisplayRange(f, rangeStart, rangeEnd)) {
        fontMenu.setBit(i, true);
      } else {
        fontMenu.setBit(i, false);
      }
    }

    fontMenu.repaint();
  }

  protected boolean canDisplayRange(Font font, int rangeStart, int rangeEnd) {
    for (int i = rangeStart; i < rangeEnd; i++) {
      if (font.canDisplay(i)) {
        return true;
      }
    }
    return false;
  }

  // / Displays a file load/save dialog and returns the specified file
  private String promptFile(boolean isSave, String initFileName) {
    int retVal;
    String str;

    // / ABP
    if (filePromptDialog == null)
      return null;

    if (isSave) {
      filePromptDialog.setDialogType(JFileChooser.SAVE_DIALOG);
      filePromptDialog.setDialogTitle("Save...");
      str = "Save";

    } else {
      filePromptDialog.setDialogType(JFileChooser.OPEN_DIALOG);
      filePromptDialog.setDialogTitle("Load...");
      str = "Load";
    }

    if (initFileName != null)
      filePromptDialog.setSelectedFile(new File(initFileName));
    retVal = filePromptDialog.showDialog(this, str);

    if (retVal == JFileChooser.APPROVE_OPTION) {
      File file = filePromptDialog.getSelectedFile();
      String fileName = file.getAbsolutePath();
      if (fileName != null) {
        return fileName;
      }
    }

    return null;
  }

  // / Converts user text into arrays of String, delimited at newline character
  // / Also replaces any valid escape sequence with appropriate unicode
  // character
  // / Support \\UXXXXXX notation for surrogates
  private String[] parseUserText(String orig) {
    int length = orig.length();
    StringTokenizer perLine = new StringTokenizer(orig, "\n");
    String textLines[] = new String[perLine.countTokens()];
    int lineNumber = 0;

    while (perLine.hasMoreElements()) {
      StringBuffer converted = new StringBuffer();
      String oneLine = perLine.nextToken();
      int lineLength = oneLine.length();
      int prevEscapeEnd = 0;
      int nextEscape = -1;
      do {
        int nextBMPEscape = oneLine.indexOf("\\u", prevEscapeEnd);
        int nextSupEscape = oneLine.indexOf("\\U", prevEscapeEnd);
        nextEscape = (nextBMPEscape < 0) ? ((nextSupEscape < 0) ? -1 : nextSupEscape)
            : ((nextSupEscape < 0) ? nextBMPEscape : Math.min(nextBMPEscape, nextSupEscape));

        if (nextEscape != -1) {
          if (prevEscapeEnd < nextEscape)
            converted.append(oneLine.substring(prevEscapeEnd, nextEscape));

          prevEscapeEnd = nextEscape + (nextEscape == nextBMPEscape ? 6 : 8);
          try {
            String hex = oneLine.substring(nextEscape + 2, prevEscapeEnd);
            if (nextEscape == nextBMPEscape) {
              converted.append((char) Integer.parseInt(hex, 16));
            } else {
              converted.append(new String(Character.toChars(Integer.parseInt(hex, 16))));
            }
          } catch (Exception e) {
            int copyLimit = Math.min(lineLength, prevEscapeEnd);
            converted.append(oneLine.substring(nextEscape, copyLimit));
          }
        }
      } while (nextEscape != -1);
      if (prevEscapeEnd < lineLength)
        converted.append(oneLine.substring(prevEscapeEnd, lineLength));
      textLines[lineNumber++] = converted.toString();
    }
    return textLines;
  }

  // / Reads the text from specified file, detecting UTF-16 encoding
  // / Then breaks the text into String array, delimited at every line break
  private void readTextFile(String fileName) {
    try {
      String fileText, textLines[];
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
      int numBytes = bis.available();
      if (numBytes == 0) {
        throw new Exception("Text file " + fileName + " is empty");
      }
      byte byteData[] = new byte[numBytes];
      bis.read(byteData, 0, numBytes);
      bis.close();

      // / If byte mark is found, then use UTF-16 encoding to convert bytes...
      if (numBytes >= 2
          && ((byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE) || (byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF)))
        fileText = new String(byteData, "UTF-16");
      // / Otherwise, use system default encoding
      else
        fileText = new String(byteData);

      int length = fileText.length();
      StringTokenizer perLine = new StringTokenizer(fileText, "\n");
      // / Determine "Return Char" used in this file
      // / This simply finds first occurrence of CR, CR+LF or LF...
      for (int i = 0; i < length; i++) {
        char iTh = fileText.charAt(i);
        if (iTh == '\r') {
          if (i < length - 1 && fileText.charAt(i + 1) == '\n')
            perLine = new StringTokenizer(fileText, "\r\n");
          else
            perLine = new StringTokenizer(fileText, "\r");
          break;
        } else if (iTh == '\n')
          // / Use the one already created
          break;
      }
      int lineNumber = 0, numLines = perLine.countTokens();
      textLines = new String[numLines];

      while (perLine.hasMoreElements()) {
        String oneLine = perLine.nextToken();
        if (oneLine == null)
          // / To make LineBreakMeasurer to return a valid TextLayout
          // / on an empty line, simply feed it a space char...
          oneLine = " ";
        textLines[lineNumber++] = oneLine;
      }
      fp.setTextToDraw(fp.FILE_TEXT, null, null, textLines);
      rm.setEnabled(false);
      methodsMenu.setEnabled(false);
    } catch (Exception ex) {
      fireChangeStatus("ERROR: Failed to Read Text File; See Stack Trace", true);
      ex.printStackTrace();
    }
  }

  // / Returns a String storing current configuration
  private void writeCurrentOptions(String fileName) {
    try {
      String curOptions = fp.getCurrentOptions();
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
      // / Prepend title and the option that is only obtainable here
      int range[] = rm.getSelectedRange();
      String completeOptions = ("Font2DTest Option File\n" + displayGridCBMI.getState() + "\n"
          + force16ColsCBMI.getState() + "\n" + showFontInfoCBMI.getState() + "\n"
          + rm.getSelectedItem() + "\n" + range[0] + "\n" + range[1] + "\n" + curOptions + tFileName);
      byte toBeWritten[] = completeOptions.getBytes("UTF-16");
      bos.write(toBeWritten, 0, toBeWritten.length);
      bos.close();
    } catch (Exception ex) {
      fireChangeStatus("ERROR: Failed to Save Options File; See Stack Trace", true);
      ex.printStackTrace();
    }
  }

  // / Updates GUI visibility/status after some parameters have changed
  private void updateGUI() {
    int selectedText = textMenu.getSelectedIndex();

    // / Set the visibility of User Text dialog
    if (selectedText == fp.USER_TEXT)
      userTextDialog.show();
    else
      userTextDialog.hide();
    // / Change the visibility/status/availability of Print JDialog buttons
    printModeCBs[fp.ONE_PAGE].setSelected(true);
    if (selectedText == fp.FILE_TEXT || selectedText == fp.USER_TEXT) {
      // / ABP
      // / update methodsMenu to show that TextLayout.draw is being used
      // / when we are in FILE_TEXT mode
      if (selectedText == fp.FILE_TEXT)
        methodsMenu.setSelectedItem("TextLayout.draw");
      methodsMenu.setEnabled(selectedText == fp.USER_TEXT);
      printModeCBs[fp.CUR_RANGE].setEnabled(false);
      printModeCBs[fp.ALL_TEXT].setEnabled(true);
    } else {
      // / ABP
      // / update methodsMenu to show that drawGlyph is being used
      // / when we are in ALL_GLYPHS mode
      if (selectedText == fp.ALL_GLYPHS)
        methodsMenu.setSelectedItem("drawGlyphVector");
      methodsMenu.setEnabled(selectedText == fp.RANGE_TEXT);
      printModeCBs[fp.CUR_RANGE].setEnabled(true);
      printModeCBs[fp.ALL_TEXT].setEnabled(false);
    }
    // / Modify RangeMenu and fontInfo label availabilty
    if (selectedText == fp.RANGE_TEXT) {
      fontInfos[1].setVisible(true);
      rm.setEnabled(true);
    } else {
      fontInfos[1].setVisible(false);
      rm.setEnabled(false);
    }
  }

  // / Loads saved options and applies them
  private void loadOptions(String fileName) {
    try {
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
      int numBytes = bis.available();
      byte byteData[] = new byte[numBytes];
      bis.read(byteData, 0, numBytes);
      bis.close();
      if (numBytes < 2 || (byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF))
        throw new Exception("Not a Font2DTest options file");

      String options = new String(byteData, "UTF-16");
      StringTokenizer perLine = new StringTokenizer(options, "\n");
      String title = perLine.nextToken();
      if (!title.equals("Font2DTest Option File"))
        throw new Exception("Not a Font2DTest options file");

      // / Parse all options
      boolean displayGridOpt = Boolean.parseBoolean(perLine.nextToken());
      boolean force16ColsOpt = Boolean.parseBoolean(perLine.nextToken());
      boolean showFontInfoOpt = Boolean.parseBoolean(perLine.nextToken());
      String rangeNameOpt = perLine.nextToken();
      int rangeStartOpt = Integer.parseInt(perLine.nextToken());
      int rangeEndOpt = Integer.parseInt(perLine.nextToken());
      String fontNameOpt = perLine.nextToken();
      float fontSizeOpt = Float.parseFloat(perLine.nextToken());
      int fontStyleOpt = Integer.parseInt(perLine.nextToken());
      int fontTransformOpt = Integer.parseInt(perLine.nextToken());
      int g2TransformOpt = Integer.parseInt(perLine.nextToken());
      int textToUseOpt = Integer.parseInt(perLine.nextToken());
      int drawMethodOpt = Integer.parseInt(perLine.nextToken());
      int antialiasOpt = Integer.parseInt(perLine.nextToken());
      int fractionalOpt = Integer.parseInt(perLine.nextToken());
      int lcdContrast = Integer.parseInt(perLine.nextToken());
      String userTextOpt[] = { "Java2D!" }, dialogEntry = "Java2D!";
      if (textToUseOpt == fp.USER_TEXT) {
        int numLines = perLine.countTokens(), lineNumber = 0;
        if (numLines != 0) {
          userTextOpt = new String[numLines];
          dialogEntry = "";
          for (; perLine.hasMoreElements(); lineNumber++) {
            userTextOpt[lineNumber] = perLine.nextToken();
            dialogEntry += userTextOpt[lineNumber] + "\n";
          }
        }
      }

      // / Reset GUIs
      displayGridCBMI.setState(displayGridOpt);
      force16ColsCBMI.setState(force16ColsOpt);
      showFontInfoCBMI.setState(showFontInfoOpt);
      rm.setSelectedRange(rangeNameOpt, rangeStartOpt, rangeEndOpt);
      fontMenu.setSelectedItem(fontNameOpt);
      sizeField.setText(String.valueOf(fontSizeOpt));
      styleMenu.setSelectedIndex(fontStyleOpt);
      transformMenu.setSelectedIndex(fontTransformOpt);
      transformMenuG2.setSelectedIndex(g2TransformOpt);
      textMenu.setSelectedIndex(textToUseOpt);
      methodsMenu.setSelectedIndex(drawMethodOpt);
      antiAliasMenu.setSelectedIndex(antialiasOpt);
      fracMetricsMenu.setSelectedIndex(fractionalOpt);
      contrastSlider.setValue(lcdContrast);

      userTextArea.setText(dialogEntry);
      updateGUI();

      if (textToUseOpt == fp.FILE_TEXT) {
        tFileName = perLine.nextToken();
        readTextFile(tFileName);
      }

      // / Reset option variables and repaint
      fp.loadOptions(displayGridOpt, force16ColsOpt, rangeStartOpt, rangeEndOpt, fontNameOpt,
          fontSizeOpt, fontStyleOpt, fontTransformOpt, g2TransformOpt, textToUseOpt, drawMethodOpt,
          antialiasOpt, fractionalOpt, lcdContrast, userTextOpt);
      if (showFontInfoOpt) {
        fireUpdateFontInfo();
        fontInfoDialog.show();
      } else
        fontInfoDialog.hide();
    } catch (Exception ex) {
      fireChangeStatus("ERROR: Failed to Load Options File; See Stack Trace", true);
      ex.printStackTrace();
    }
  }

  // / Loads a previously saved image
  private void loadComparisonPNG(String fileName) {
    try {
      BufferedImage image = javax.imageio.ImageIO.read(new File(fileName));
      JFrame f = new JFrame("Comparison PNG");
      ImagePanel ip = new ImagePanel(image);
      f.setResizable(false);
      f.getContentPane().add(ip);
      f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          ((JFrame) e.getSource()).dispose();
        }
      });
      f.pack();
      f.show();
    } catch (Exception ex) {
      fireChangeStatus("ERROR: Failed to Load PNG File; See Stack Trace", true);
      ex.printStackTrace();
    }
  }

  // / Interface functions...

  // / ActionListener interface function
  // / Responds to JMenuItem, JTextField and JButton actions
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (source instanceof JMenuItem) {
      JMenuItem mi = (JMenuItem) source;
      String itemName = mi.getText();

      if (itemName.equals("Save Selected Options...")) {
        String fileName = promptFile(true, "options.txt");
        if (fileName != null)
          writeCurrentOptions(fileName);
      } else if (itemName.equals("Load Options...")) {
        String fileName = promptFile(false, "options.txt");
        if (fileName != null)
          loadOptions(fileName);
      } else if (itemName.equals("Save as PNG...")) {
        String fileName = promptFile(true, fontMenu.getSelectedItem() + ".png");
        if (fileName != null)
          fp.doSavePNG(fileName);
      } else if (itemName.equals("Load PNG File to Compare...")) {
        String fileName = promptFile(false, null);
        if (fileName != null)
          loadComparisonPNG(fileName);
      } else if (itemName.equals("Page Setup..."))
        fp.doPageSetup();
      else if (itemName.equals("Print..."))
        printDialog.show();
      else if (itemName.equals("Close"))
        parent.dispose();
      else if (itemName.equals("Exit"))
        System.exit(0);
    }

    else if (source instanceof JTextField) {
      JTextField tf = (JTextField) source;
      float sz = 12f;
      try {
        sz = Float.parseFloat(sizeField.getText());
        if (sz < 1f || sz > 120f) {
          sz = 12f;
          sizeField.setText("12");
        }
      } catch (Exception se) {
        sizeField.setText("12");
      }
      if (tf == sizeField)
        fp.setFontParams(fontMenu.getSelectedItem(), sz, styleMenu.getSelectedIndex(),
            transformMenu.getSelectedIndex());
    }

    else if (source instanceof JButton) {
      String itemName = ((JButton) source).getText();
      // / Print dialog buttons...
      if (itemName.equals("Print")) {
        for (int i = 0; i < printModeCBs.length; i++)
          if (printModeCBs[i].isSelected()) {
            printDialog.hide();
            fp.doPrint(i);
          }
      } else if (itemName.equals("Cancel"))
        printDialog.hide();
      // / Update button from Usert Text JDialog...
      else if (itemName.equals("Update"))
        fp.setTextToDraw(fp.USER_TEXT, null, parseUserText(userTextArea.getText()), null);
    } else if (source instanceof JComboBox) {
      JComboBox c = (JComboBox) source;

      // / RangeMenu handles actions by itself and then calls fireRangeChanged,
      // / so it is not listed or handled here
      if (c == fontMenu || c == styleMenu || c == transformMenu) {
        float sz = 12f;
        try {
          sz = Float.parseFloat(sizeField.getText());
          if (sz < 1f || sz > 120f) {
            sz = 12f;
            sizeField.setText("12");
          }
        } catch (Exception se) {
          sizeField.setText("12");
        }
        fp.setFontParams(fontMenu.getSelectedItem(), sz, styleMenu.getSelectedIndex(),
            transformMenu.getSelectedIndex());
      } else if (c == methodsMenu)
        fp.setDrawMethod(methodsMenu.getSelectedIndex());
      else if (c == textMenu) {

        if (canDisplayCheck) {
          fireRangeChanged();
        }

        int selected = textMenu.getSelectedIndex();

        if (selected == fp.RANGE_TEXT)
          fp.setTextToDraw(fp.RANGE_TEXT, rm.getSelectedRange(), null, null);
        else if (selected == fp.USER_TEXT)
          fp.setTextToDraw(fp.USER_TEXT, null, parseUserText(userTextArea.getText()), null);
        else if (selected == fp.FILE_TEXT) {
          String fileName = promptFile(false, null);
          if (fileName != null) {
            tFileName = fileName;
            readTextFile(fileName);
          } else {
            // / User cancelled selection; reset to previous choice
            c.setSelectedIndex(currentTextChoice);
            return;
          }
        } else if (selected == fp.ALL_GLYPHS)
          fp.setTextToDraw(fp.ALL_GLYPHS, null, null, null);

        updateGUI();
        currentTextChoice = selected;
      } else if (c == transformMenuG2) {
        fp.setTransformG2(transformMenuG2.getSelectedIndex());
      } else if (c == antiAliasMenu || c == fracMetricsMenu) {
        if (c == antiAliasMenu) {
          boolean enabled = FontPanel.AAValues.isLCDMode(antiAliasMenu.getSelectedItem());
          contrastSlider.setEnabled(enabled);
        }
        fp.setRenderingHints(antiAliasMenu.getSelectedItem(), fracMetricsMenu.getSelectedItem(),
            contrastSlider.getValue());
      }
    }
  }

  public void stateChanged(ChangeEvent e) {
    Object source = e.getSource();
    if (source instanceof JSlider) {
      fp.setRenderingHints(antiAliasMenu.getSelectedItem(), fracMetricsMenu.getSelectedItem(),
          contrastSlider.getValue());
    }
  }

  // / ItemListener interface function
  // / Responds to JCheckBoxMenuItem, JComboBox and JCheckBox actions
  public void itemStateChanged(ItemEvent e) {
    Object source = e.getSource();

    if (source instanceof JCheckBoxMenuItem) {
      JCheckBoxMenuItem cbmi = (JCheckBoxMenuItem) source;
      if (cbmi == displayGridCBMI)
        fp.setGridDisplay(displayGridCBMI.getState());
      else if (cbmi == force16ColsCBMI)
        fp.setForce16Columns(force16ColsCBMI.getState());
      else if (cbmi == showFontInfoCBMI) {
        if (showFontInfoCBMI.getState()) {
          fireUpdateFontInfo();
          fontInfoDialog.show();
        } else
          fontInfoDialog.hide();
      }
    }
  }

  private static void printUsage() {
    String usage = "Usage: java -jar Font2DTest.jar [options]\n" + "\nwhere options include:\n"
        + "    -dcdc | -disablecandisplaycheck disable canDisplay check for font\n"
        + "    -?    | -help                   print this help message\n" + "\nExample :\n"
        + "     To disable canDisplay check on font for ranges\n"
        + "     java -jar Font2DTest.jar -dcdc";
    System.out.println(usage);
    System.exit(0);
  }

  // / Main function
  public static void main(String argv[]) {

    if (argv.length > 0) {
      if (argv[0].equalsIgnoreCase("-disablecandisplaycheck") || argv[0].equalsIgnoreCase("-dcdc")) {
        canDisplayCheck = false;
      } else {
        printUsage();
      }
    }

    UIManager.put("swing.boldMetal", Boolean.FALSE);
    final JFrame f = new JFrame("Font2DTest");
    final Font2DTest f2dt = new Font2DTest(f, false);
    f.addWindowListener(new WindowAdapter() {
      public void windowOpening(WindowEvent e) {
        f2dt.repaint();
      }

      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    f.getContentPane().add(f2dt);
    f.pack();
    f.show();
  }

  // / Inner class definitions...

  // / Class to display just an image file
  // / Used to show the comparison PNG image
  private final class ImagePanel extends JPanel {
    private final BufferedImage bi;

    public ImagePanel(BufferedImage image) {
      bi = image;
    }

    public Dimension getPreferredSize() {
      return new Dimension(bi.getWidth(), bi.getHeight());
    }

    public void paintComponent(Graphics g) {
      g.drawImage(bi, 0, 0, this);
    }
  }

  // / Classes made to avoid repetitive calls... (being lazy)
  private final class ButtonV2 extends JButton {
    public ButtonV2(String name, ActionListener al) {
      super(name);
      this.addActionListener(al);
    }
  }

  private final class ChoiceV2 extends JComboBox {

    private BitSet bitSet = null;

    public ChoiceV2() {
      ;
    }

    public ChoiceV2(ActionListener al) {
      super();
      this.addActionListener(al);
    }

    public ChoiceV2(ActionListener al, boolean fontChoice) {
      this(al);
      if (fontChoice) {
        // Register this component in ToolTipManager
        setToolTipText("");
        bitSet = new BitSet();
        setRenderer(new ChoiceV2Renderer(this));
      }
    }

    public String getToolTipText() {
      int index = this.getSelectedIndex();
      String fontName = (String) this.getSelectedItem();
      if (fontName != null && (textMenu.getSelectedIndex() == fp.RANGE_TEXT)) {
        if (getBit(index)) {
          return "Font \"" + fontName + "\" can display some characters in \""
              + rm.getSelectedItem() + "\" range";
        } else {
          return "Font \"" + fontName + "\" cannot display any characters in \""
              + rm.getSelectedItem() + "\" range";
        }
      }
      return super.getToolTipText();
    }

    public void setBit(int bitIndex, boolean value) {
      bitSet.set(bitIndex, value);
    }

    public boolean getBit(int bitIndex) {
      return bitSet.get(bitIndex);
    }
  }

  private final class ChoiceV2Renderer extends DefaultListCellRenderer {

    private ImageIcon yesImage, blankImage;

    private ChoiceV2 choice = null;

    public ChoiceV2Renderer(ChoiceV2 choice) {
      try {
        yesImage = new ImageIcon(getClass().getResource("yes.gif"));
        blankImage = new ImageIcon(getClass().getResource("blank.gif"));
      } catch (Exception exception) {
        System.out.println("Exception : " + exception);
      }
      this.choice = choice;
    }

    public Component getListCellRendererComponent(JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {

      if (textMenu.getSelectedIndex() == fp.RANGE_TEXT) {

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        // For JComboBox if index is -1, its rendering the selected index.
        if (index == -1) {
          index = choice.getSelectedIndex();
        }

        if (choice.getBit(index)) {
          setIcon(yesImage);
        } else {
          setIcon(blankImage);
        }

      } else {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        setIcon(blankImage);
      }

      return this;
    }
  }

  private final class LabelV2 extends JLabel {
    public LabelV2(String name) {
      super(name);
    }
  }

  private final class MenuItemV2 extends JMenuItem {
    public MenuItemV2(String name, ActionListener al) {
      super(name);
      this.addActionListener(al);
    }
  }

  private final class CheckboxMenuItemV2 extends JCheckBoxMenuItem {
    public CheckboxMenuItemV2(String name, boolean b, ItemListener il) {
      super(name, b);
      this.addItemListener(il);
    }
  }
}

/*
 * @(#)FontPanel.java 1.20 05/11/17
 * 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS
 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or intended for
 * use in the design, construction, operation or maintenance of any nuclear
 * facility.
 */

/*
 * @(#)FontPanel.java 1.20 05/11/17
 */

/**
 * FontPanel.java
 * 
 * @version
 * @(#)FontPanel.java 1.1 00/08/22
 * @author Shinsuke Fukuda
 * @author Ankit Patel [Conversion to Swing - 01/07/30]
 */

// / This panel is combination of the text drawing area of Font2DTest
// / and the custom controlled scroll bar
final class FontPanel extends JPanel implements AdjustmentListener {

  // / Drawing Option Constants
  private final String STYLES[] = { "plain", "bold", "italic", "bold italic" };

  private final int NONE = 0;

  private final int SCALE = 1;

  private final int SHEAR = 2;

  private final int ROTATE = 3;

  private final String TRANSFORMS[] = { "with no transforms", "with scaling", "with Shearing",
      "with rotation" };

  private final int DRAW_STRING = 0;

  private final int DRAW_CHARS = 1;

  private final int DRAW_BYTES = 2;

  private final int DRAW_GLYPHV = 3;

  private final int TL_DRAW = 4;

  private final int GV_OUTLINE = 5;

  private final int TL_OUTLINE = 6;

  private final String METHODS[] = { "drawString", "drawChars", "drawBytes", "drawGlyphVector",
      "TextLayout.draw", "GlyphVector.getOutline", "TextLayout.getOutline" };

  public final int RANGE_TEXT = 0;

  public final int ALL_GLYPHS = 1;

  public final int USER_TEXT = 2;

  public final int FILE_TEXT = 3;

  private final String MS_OPENING[] = { " Unicode ", " Glyph Code ", " lines ", " lines " };

  private final String MS_CLOSING[] = { "", "", " of User Text ",
      " of LineBreakMeasurer-reformatted Text " };

  // / General Graphics Variable
  private final JScrollBar verticalBar;

  private final FontCanvas fc;

  private boolean updateBackBuffer = true;

  private boolean updateFontMetrics = true;

  private boolean updateFont = true;

  private boolean force16Cols = false;

  public boolean showingError = false;

  private int g2Transform = NONE; // / ABP

  // / Printing constants and variables
  public final int ONE_PAGE = 0;

  public final int CUR_RANGE = 1;

  public final int ALL_TEXT = 2;

  private int printMode = ONE_PAGE;

  private PageFormat page = null;

  private PrinterJob printer = null;

  // / Text drawing variables
  private String fontName = "Dialog";

  private float fontSize = 12;

  private int fontStyle = Font.PLAIN;

  private int fontTransform = NONE;

  private Font testFont = null;

  private Object antiAliasType = VALUE_TEXT_ANTIALIAS_DEFAULT;

  private Object fractionalMetricsType = VALUE_FRACTIONALMETRICS_DEFAULT;

  private Object lcdContrast = getDefaultLCDContrast();

  private int drawMethod = DRAW_STRING;

  private int textToUse = RANGE_TEXT;

  private String userText[] = null;

  private String fileText[] = null;

  private int drawRange[] = { 0x0000, 0x007f };

  private String fontInfos[] = new String[2];

  private boolean showGrid = true;

  // / Parent Font2DTest panel
  private final Font2DTest f2dt;

  private final JFrame parent;

  public FontPanel(Font2DTest demo, JFrame f) {
    f2dt = demo;
    parent = f;

    verticalBar = new JScrollBar(JScrollBar.VERTICAL);
    fc = new FontCanvas();

    this.setLayout(new BorderLayout());
    this.add("Center", fc);
    this.add("East", verticalBar);

    verticalBar.addAdjustmentListener(this);
    this.addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent e) {
        updateBackBuffer = true;
        updateFontMetrics = true;
      }
    });

    // / Initialize font and its infos
    testFont = new Font(fontName, fontStyle, (int) fontSize);
    if ((float) ((int) fontSize) != fontSize) {
      testFont = testFont.deriveFont(fontSize);
    }
    updateFontInfo();
  }

  public Dimension getPreferredSize() {
    return new Dimension(600, 200);
  }

  // / Functions called by the main programs to set the various parameters

  public void setTransformG2(int transform) {
    g2Transform = transform;
    updateBackBuffer = true;
    updateFontMetrics = true;
    fc.repaint();
  }

  // / convenience fcn to create AffineTransform of appropriate type
  private AffineTransform getAffineTransform(int transform) {
    // / ABP
    AffineTransform at = new AffineTransform();
    switch (transform) {
    case SCALE:
      at.setToScale(1.5f, 1.5f);
      break;
    case ROTATE:
      at.setToRotation(Math.PI / 6);
      break;
    case SHEAR:
      at.setToShear(0.4f, 0);
      break;
    case NONE:
      break;
    default:
      // System.err.println( "Illegal G2 Transform Arg: " + transform);
      break;
    }

    return at;
  }

  public void setFontParams(Object obj, float size, int style, int transform) {
    setFontParams((String) obj, size, style, transform);
  }

  public void setFontParams(String name, float size, int style, int transform) {
    boolean fontModified = false;
    if (!name.equals(fontName) || style != fontStyle)
      fontModified = true;

    fontName = name;
    fontSize = size;
    fontStyle = style;
    fontTransform = transform;

    // / Recreate the font as specified
    testFont = new Font(fontName, fontStyle, (int) fontSize);
    if ((float) ((int) fontSize) != fontSize) {
      testFont = testFont.deriveFont(fontSize);
    }

    if (fontTransform != NONE) {
      AffineTransform at = getAffineTransform(fontTransform);
      testFont = testFont.deriveFont(at);
    }
    updateBackBuffer = true;
    updateFontMetrics = true;
    fc.repaint();
    if (fontModified) {
      // / Tell main panel to update the font info
      updateFontInfo();
      f2dt.fireUpdateFontInfo();
    }
  }

  public void setRenderingHints(Object aa, Object fm, Object contrast) {
    antiAliasType = ((AAValues) aa).getHint();
    fractionalMetricsType = ((FMValues) fm).getHint();
    lcdContrast = contrast;
    updateBackBuffer = true;
    updateFontMetrics = true;
    fc.repaint();
  }

  public void setDrawMethod(int i) {
    drawMethod = i;
    updateBackBuffer = true;
    fc.repaint();
  }

  public void setTextToDraw(int i, int range[], String textSet[], String fileData[]) {
    textToUse = i;

    if (textToUse == RANGE_TEXT)
      drawRange = range;
    else if (textToUse == ALL_GLYPHS)
      drawMethod = DRAW_GLYPHV;
    else if (textToUse == USER_TEXT)
      userText = textSet;
    else if (textToUse == FILE_TEXT) {
      fileText = fileData;
      drawMethod = TL_DRAW;
    }

    updateBackBuffer = true;
    updateFontMetrics = true;
    fc.repaint();
    updateFontInfo();
  }

  public void setGridDisplay(boolean b) {
    showGrid = b;
    updateBackBuffer = true;
    fc.repaint();
  }

  public void setForce16Columns(boolean b) {
    force16Cols = b;
    updateBackBuffer = true;
    updateFontMetrics = true;
    fc.repaint();
  }

  // / Prints out the text display area
  public void doPrint(int i) {
    if (printer == null) {
      printer = PrinterJob.getPrinterJob();
      page = printer.defaultPage();
    }
    printMode = i;
    printer.setPrintable(fc, page);

    if (printer.printDialog()) {
      try {
        printer.print();
      } catch (Exception e) {
        f2dt.fireChangeStatus("ERROR: Printing Failed; See Stack Trace", true);
      }
    }
  }

  // / Displays the page setup dialog and updates PageFormat info
  public void doPageSetup() {
    if (printer == null) {
      printer = PrinterJob.getPrinterJob();
      page = printer.defaultPage();
    }
    page = printer.pageDialog(page);
  }

  // / Obtains the information about selected font
  private void updateFontInfo() {
    int numGlyphs = 0, numCharsInRange = drawRange[1] - drawRange[0] + 1;
    fontInfos[0] = "Font Face Name: " + testFont.getFontName();
    fontInfos[1] = "Glyphs in This Range: ";

    if (textToUse == RANGE_TEXT) {
      for (int i = drawRange[0]; i < drawRange[1]; i++)
        if (testFont.canDisplay(i))
          numGlyphs++;
      fontInfos[1] = fontInfos[1] + numGlyphs + " / " + numCharsInRange;
    } else
      fontInfos[1] = null;
  }

  // / Accessor for the font information
  public String[] getFontInfo() {
    return fontInfos;
  }

  // / Collects the currectly set options and returns them as string
  public String getCurrentOptions() {
    // / Create a new String to store the options
    // / The array will contain all 8 setting (font name, size...) and
    // / character range or user text data used (no file text data)
    int userTextSize = 0;
    String options;

    options = (fontName + "\n" + fontSize + "\n" + fontStyle + "\n" + fontTransform + "\n"
        + g2Transform + "\n" + textToUse + "\n" + drawMethod + "\n"
        + AAValues.getHintVal(antiAliasType) + "\n" + FMValues.getHintVal(fractionalMetricsType)
        + "\n" + lcdContrast + "\n");
    if (textToUse == USER_TEXT)
      for (int i = 0; i < userText.length; i++)
        options += (userText[i] + "\n");

    return options;
  }

  // / Reload all options and refreshes the canvas
  public void loadOptions(boolean grid, boolean force16, int start, int end, String name,
      float size, int style, int transform, int g2transform, int text, int method, int aa, int fm,
      int contrast, String user[]) {
    int range[] = { start, end };

    // / Since repaint call has a low priority, these functions will finish
    // / before the actual repainting is done
    setGridDisplay(grid);
    setForce16Columns(force16);
    // previous call to readTextFile has already set the text to draw
    if (textToUse != FILE_TEXT) {
      setTextToDraw(text, range, user, null);
    }
    setFontParams(name, size, style, transform);
    setTransformG2(g2transform); // ABP
    setDrawMethod(method);
    setRenderingHints(AAValues.getValue(aa), FMValues.getValue(fm), new Integer(contrast));
  }

  // / Writes the current screen to PNG file
  public void doSavePNG(String fileName) {
    fc.writePNG(fileName);
  }

  // / When scrolled using the scroll bar, update the backbuffer
  public void adjustmentValueChanged(AdjustmentEvent e) {
    updateBackBuffer = true;
    fc.repaint();
  }

  public void paintComponent(Graphics g) {
    // Windows does not repaint correctly, after
    // a zoom. Thus, we need to force the canvas
    // to repaint, but only once. After the first repaint,
    // everything stabilizes. [ABP]
    fc.repaint();
  }

  // / Inner class definition...

  // / Inner panel that holds the actual drawing area and its routines
  private class FontCanvas extends JPanel implements MouseListener, MouseMotionListener, Printable {

    // / Number of characters that will fit across and down this canvas
    private int numCharAcross, numCharDown;

    // / First and last character/line that will be drawn
    // / Limit is the end of range/text where no more draw will be done
    private int drawStart, drawEnd, drawLimit;

    // / FontMetrics variables
    // / Here, gridWidth is equivalent to maxAdvance (slightly bigger though)
    // / and gridHeight is equivalent to lineHeight
    private int maxAscent, maxDescent, gridWidth = 0, gridHeight = 0;

    // / Offset from the top left edge of the canvas where the draw will start
    private int canvasInset_X = 5, canvasInset_Y = 5;

    // / Offscreen buffer of this canvas
    private BufferedImage backBuffer = null;

    // / LineBreak'ed TextLayout vector
    private Vector lineBreakTLs = null;

    // / Whether the current draw command requested is for printing
    private boolean isPrinting = false;

    // / Other printing infos
    private int lastPage, printPageNumber, currentlyShownChar = 0;

    private final int PR_OFFSET = 10;

    private final int PR_TITLE_LINEHEIGHT = 30;

    // / Information about zooming (used with range text draw)
    private final JWindow zoomWindow;

    private BufferedImage zoomImage = null;

    private int mouseOverCharX = -1, mouseOverCharY = -1;

    private int currMouseOverChar = -1, prevZoomChar = -1;

    private float ZOOM = 2.0f;

    private boolean nowZooming = false;

    private boolean firstTime = true;

    // ABP

    // / Status bar message backup
    private String backupStatusString = null;

    // / Error constants
    private final String ERRORS[] = {
        "ERROR: drawBytes cannot handle characters beyond 0x00FF. Select different range or draw methods.",
        "ERROR: Cannot fit text with the current font size. Resize the window or use smaller font size.",
        "ERROR: Cannot print with the current font size. Use smaller font size.", };

    private final int DRAW_BYTES_ERROR = 0;

    private final int CANT_FIT_DRAW = 1;

    private final int CANT_FIT_PRINT = 2;

    // / Other variables
    private final Cursor blankCursor;

    public FontCanvas() {
      this.addMouseListener(this);
      this.addMouseMotionListener(this);
      this.setForeground(Color.black);
      this.setBackground(Color.white);

      // / Creates an invisble pointer by giving it bogus image
      // / Possibly find a workaround for this...
      Toolkit tk = Toolkit.getDefaultToolkit();
      byte bogus[] = { (byte) 0 };
      blankCursor = tk.createCustomCursor(tk.createImage(bogus), new Point(0, 0), "");

      zoomWindow = new JWindow(parent) {
        public void paint(Graphics g) {
          g.drawImage(zoomImage, 0, 0, zoomWindow);
        }
      };
      zoomWindow.setCursor(blankCursor);
      zoomWindow.pack();
    }

    public boolean firstTime() {
      return firstTime;
    }

    public void refresh() {
      firstTime = false;
      updateBackBuffer = true;
      repaint();
    }

    // / Sets the font, hints, according to the set parameters
    private void setParams(Graphics2D g2) {
      g2.setFont(testFont);
      g2.setRenderingHint(KEY_TEXT_ANTIALIASING, antiAliasType);
      g2.setRenderingHint(KEY_FRACTIONALMETRICS, fractionalMetricsType);
      g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, lcdContrast);
      /*
       * I am preserving a somewhat dubious behaviour of this program. Outline
       * text would be drawn anti-aliased by setting the graphics anti-aliasing
       * hint if the text anti-aliasing hint was set. The dubious element here
       * is that people simply using this program may think this is built-in
       * behaviour but its not - at least not when the app explictly draws
       * outline text. This becomes more dubious in cases such as "GASP" where
       * the size at which text is AA'ed is not something you can easily
       * calculate, so mimicing that behaviour isn't going to be easy. So I
       * precisely preserve the behaviour : this is done only if the AA value is
       * "ON". Its not applied in the other cases.
       */
      if (antiAliasType == VALUE_TEXT_ANTIALIAS_ON
          && (drawMethod == TL_OUTLINE || drawMethod == GV_OUTLINE)) {
        g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
      } else {
        g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_OFF);
      }
    }

    // / Draws the grid (Used for unicode/glyph range drawing)
    private void drawGrid(Graphics2D g2) {
      int totalGridWidth = numCharAcross * gridWidth;
      int totalGridHeight = numCharDown * gridHeight;

      g2.setColor(Color.black);
      for (int i = 0; i < numCharDown + 1; i++)
        g2.drawLine(canvasInset_X, i * gridHeight + canvasInset_Y, canvasInset_X + totalGridWidth,
            i * gridHeight + canvasInset_Y);
      for (int i = 0; i < numCharAcross + 1; i++)
        g2.drawLine(i * gridWidth + canvasInset_X, canvasInset_Y, i * gridWidth + canvasInset_X,
            canvasInset_Y + totalGridHeight);
    }

    // / Draws one character at time onto the canvas according to
    // / the method requested (Used for RANGE_TEXT and ALL_GLYPHS)
    public void modeSpecificDrawChar(Graphics2D g2, int charCode, int baseX, int baseY) {
      GlyphVector gv;
      int oneGlyph[] = { charCode };
      char charArray[] = Character.toChars(charCode);

      FontRenderContext frc = g2.getFontRenderContext();
      AffineTransform oldTX = g2.getTransform();

      // / Create GlyphVector to measure the exact visual advance
      // / Using that number, adjust the position of the character drawn
      if (textToUse == ALL_GLYPHS)
        gv = testFont.createGlyphVector(frc, oneGlyph);
      else
        gv = testFont.createGlyphVector(frc, charArray);
      Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
      int shiftedX = baseX;
      // getPixelBounds returns a result in device space.
      // we need to convert back to user space to be able to
      // calculate the shift as baseX is in user space.
      try {
        double pt[] = new double[4];
        pt[0] = r2d2.getX();
        pt[1] = r2d2.getY();
        pt[2] = r2d2.getX() + r2d2.getWidth();
        pt[3] = r2d2.getY() + r2d2.getHeight();
        oldTX.inverseTransform(pt, 0, pt, 0, 2);
        shiftedX = baseX - (int) (pt[2] / 2 + pt[0]);
      } catch (NoninvertibleTransformException e) {
      }

      // / ABP - keep track of old tform, restore it later

      g2.translate(shiftedX, baseY);
      g2.transform(getAffineTransform(g2Transform));

      if (textToUse == ALL_GLYPHS)
        g2.drawGlyphVector(gv, 0f, 0f);
      else {
        if (testFont.canDisplay(charCode))
          g2.setColor(Color.black);
        else {
          g2.setColor(Color.lightGray);
        }

        switch (drawMethod) {
        case DRAW_STRING:
          g2.drawString(new String(charArray), 0, 0);
          break;
        case DRAW_CHARS:
          g2.drawChars(charArray, 0, 1, 0, 0);
          break;
        case DRAW_BYTES:
          if (charCode > 0xff)
            throw new CannotDrawException(DRAW_BYTES_ERROR);
          byte oneByte[] = { (byte) charCode };
          g2.drawBytes(oneByte, 0, 1, 0, 0);
          break;
        case DRAW_GLYPHV:
          g2.drawGlyphVector(gv, 0f, 0f);
          break;
        case TL_DRAW:
          TextLayout tl = new TextLayout(new String(charArray), testFont, frc);
          tl.draw(g2, 0f, 0f);
          break;
        case GV_OUTLINE:
          r2d2 = gv.getVisualBounds();
          shiftedX = baseX - (int) (r2d2.getWidth() / 2 + r2d2.getX());
          g2.draw(gv.getOutline(0f, 0f));
          break;
        case TL_OUTLINE:
          r2d2 = gv.getVisualBounds();
          shiftedX = baseX - (int) (r2d2.getWidth() / 2 + r2d2.getX());
          TextLayout tlo = new TextLayout(new String(charArray), testFont, g2
              .getFontRenderContext());
          g2.draw(tlo.getOutline(null));
        }
      }

      // / ABP - restore old tform
      g2.setTransform(oldTX);
    }

    // Java2D!\\U01d586\\U01d587\\U01d588

    // / Draws one line of text at given position
    private void modeSpecificDrawLine(Graphics2D g2, String line, int baseX, int baseY) {
      // / ABP - keep track of old tform, restore it later
      AffineTransform oldTx = null;
      oldTx = g2.getTransform();
      g2.translate(baseX, baseY);
      g2.transform(getAffineTransform(g2Transform));

      switch (drawMethod) {
      case DRAW_STRING:
        g2.drawString(line, 0, 0);
        break;
      case DRAW_CHARS:
        g2.drawChars(line.toCharArray(), 0, line.length(), 0, 0);
        break;
      case DRAW_BYTES:
        try {
          byte lineBytes[] = line.getBytes("ISO-8859-1");
          g2.drawBytes(lineBytes, 0, lineBytes.length, 0, 0);
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
      case DRAW_GLYPHV:
        GlyphVector gv = testFont.createGlyphVector(g2.getFontRenderContext(), line);
        g2.drawGlyphVector(gv, (float) 0, (float) 0);
        break;
      case TL_DRAW:
        TextLayout tl = new TextLayout(line, testFont, g2.getFontRenderContext());
        tl.draw(g2, (float) 0, (float) 0);
        break;
      case GV_OUTLINE:
        GlyphVector gvo = testFont.createGlyphVector(g2.getFontRenderContext(), line);
        g2.draw(gvo.getOutline((float) 0, (float) 0));
        break;
      case TL_OUTLINE:
        TextLayout tlo = new TextLayout(line, testFont, g2.getFontRenderContext());
        AffineTransform at = new AffineTransform();
        g2.draw(tlo.getOutline(at));
      }

      // / ABP - restore old tform
      g2.setTransform(oldTx);

    }

    // / Draws one line of text at given position
    private void tlDrawLine(Graphics2D g2, TextLayout tl, float baseX, float baseY) {
      // / ABP - keep track of old tform, restore it later
      AffineTransform oldTx = null;
      oldTx = g2.getTransform();
      g2.translate(baseX, baseY);
      g2.transform(getAffineTransform(g2Transform));

      tl.draw(g2, (float) 0, (float) 0);

      // / ABP - restore old tform
      g2.setTransform(oldTx);

    }

    // / If textToUse is set to range drawing, then convert
    // / int to hex string and prepends 0s to make it length 4
    // / Otherwise line number was fed; simply return number + 1 converted to
    // String
    // / (This is because first line is 1, not 0)
    private String modeSpecificNumStr(int i) {
      if (textToUse == USER_TEXT || textToUse == FILE_TEXT)
        return String.valueOf(i + 1);

      StringBuffer s = new StringBuffer(Integer.toHexString(i));
      while (s.length() < 4)
        s.insert(0, "0");
      return s.toString().toUpperCase();
    }

    // / Resets the scrollbar to display correct range of text currently on
    // screen
    // / (This scrollbar is not part of a "ScrollPane". It merely simulates its
    // effect by
    // / indicating the necessary area to be drawn within the panel.
    // / By doing this, it prevents creating gigantic panel when large text
    // range,
    // / i.e. CJK Ideographs, is requested)
    private void resetScrollbar(int oldValue) {
      int totalNumRows = 1, numCharToDisplay;
      if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
        if (textToUse == RANGE_TEXT)
          numCharToDisplay = drawRange[1] - drawRange[0];
        else
          // / textToUse == ALL_GLYPHS
          numCharToDisplay = testFont.getNumGlyphs();

        totalNumRows = numCharToDisplay / numCharAcross;
        if (numCharToDisplay % numCharAcross != 0)
          totalNumRows++;
        if (oldValue / numCharAcross > totalNumRows)
          oldValue = 0;

        verticalBar.setValues(oldValue / numCharAcross, numCharDown, 0, totalNumRows);
      } else {
        if (textToUse == USER_TEXT)
          totalNumRows = userText.length;
        else
          // / textToUse == FILE_TEXT;
          totalNumRows = lineBreakTLs.size();
        verticalBar.setValues(oldValue, numCharDown, 0, totalNumRows);
      }
      if (totalNumRows <= numCharDown && drawStart == 0) {
        verticalBar.setEnabled(false);
      } else {
        verticalBar.setEnabled(true);
      }
    }

    // / Calculates the font's metrics that will be used for draw
    private void calcFontMetrics(Graphics2D g2d, int w, int h) {
      FontMetrics fm;
      Graphics2D g2 = (Graphics2D) g2d.create();

      // / ABP
      if (g2Transform != NONE && textToUse != FILE_TEXT) {
        g2.setFont(g2.getFont().deriveFont(getAffineTransform(g2Transform)));
        fm = g2.getFontMetrics();
      } else {
        fm = g2.getFontMetrics();
      }

      maxAscent = fm.getMaxAscent();
      maxDescent = fm.getMaxDescent();
      if (maxAscent == 0)
        maxAscent = 10;
      if (maxDescent == 0)
        maxDescent = 5;
      if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
        // / Give slight extra room for each character
        maxAscent += 3;
        maxDescent += 3;
        gridWidth = fm.getMaxAdvance() + 6;
        gridHeight = maxAscent + maxDescent;
        if (force16Cols)
          numCharAcross = 16;
        else
          numCharAcross = (w - 10) / gridWidth;
        numCharDown = (h - 10) / gridHeight;

        canvasInset_X = (w - numCharAcross * gridWidth) / 2;
        canvasInset_Y = (h - numCharDown * gridHeight) / 2;
        if (numCharDown == 0 || numCharAcross == 0)
          throw new CannotDrawException(isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW);

        if (!isPrinting)
          resetScrollbar(verticalBar.getValue() * numCharAcross);
      } else {
        maxDescent += fm.getLeading();
        canvasInset_X = 5;
        canvasInset_Y = 5;
        // / gridWidth and numCharAcross will not be used in this mode...
        gridHeight = maxAscent + maxDescent;
        numCharDown = (h - canvasInset_Y * 2) / gridHeight;

        if (numCharDown == 0)
          throw new CannotDrawException(isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW);
        // / If this is text loaded from file, prepares the LineBreak'ed
        // / text layout at this point
        if (textToUse == FILE_TEXT) {
          if (!isPrinting)
            f2dt.fireChangeStatus("LineBreaking Text... Please Wait", false);
          lineBreakTLs = new Vector();
          for (int i = 0; i < fileText.length; i++) {
            AttributedString as = new AttributedString(fileText[i], g2.getFont().getAttributes());

            LineBreakMeasurer lbm = new LineBreakMeasurer(as.getIterator(), g2
                .getFontRenderContext());

            while (lbm.getPosition() < fileText[i].length())
              lineBreakTLs.add(lbm.nextLayout((float) w));

          }
        }
        if (!isPrinting)
          resetScrollbar(verticalBar.getValue());
      }
    }

    // / Calculates the amount of text that will be displayed on screen
    private void calcTextRange() {
      String displaying = null;

      if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
        if (isPrinting)
          if (printMode == ONE_PAGE)
            drawStart = currentlyShownChar;
          else
            // / printMode == CUR_RANGE
            drawStart = numCharAcross * numCharDown * printPageNumber;
        else
          drawStart = verticalBar.getValue() * numCharAcross;
        if (textToUse == RANGE_TEXT) {
          drawStart += drawRange[0];
          drawLimit = drawRange[1];
        } else
          drawLimit = testFont.getNumGlyphs();
        drawEnd = drawStart + numCharAcross * numCharDown - 1;

        if (drawEnd >= drawLimit)
          drawEnd = drawLimit;
      } else {
        if (isPrinting)
          if (printMode == ONE_PAGE)
            drawStart = currentlyShownChar;
          else
            // / printMode == ALL_TEXT
            drawStart = numCharDown * printPageNumber;
        else {
          drawStart = verticalBar.getValue();
        }

        drawEnd = drawStart + numCharDown - 1;

        if (textToUse == USER_TEXT)
          drawLimit = userText.length - 1;
        else
          drawLimit = lineBreakTLs.size() - 1;

        if (drawEnd >= drawLimit)
          drawEnd = drawLimit;
      }

      // ABP
      if (drawStart > drawEnd) {
        drawStart = 0;
        verticalBar.setValue(drawStart);
      }

      // / Change the status bar if not printing...
      if (!isPrinting) {
        backupStatusString = ("Displaying" + MS_OPENING[textToUse] + modeSpecificNumStr(drawStart)
            + " to " + modeSpecificNumStr(drawEnd) + MS_CLOSING[textToUse]);
        f2dt.fireChangeStatus(backupStatusString, false);
      }
    }

    // / Draws text according to the parameters set by Font2DTest GUI
    private void drawText(Graphics g, int w, int h) {
      Graphics2D g2;

      // / Create back buffer when not printing, and its Graphics2D
      // / Then set drawing parameters for that Graphics2D object
      if (isPrinting)
        g2 = (Graphics2D) g;
      else {
        backBuffer = (BufferedImage) this.createImage(w, h);
        g2 = backBuffer.createGraphics();
        g2.setColor(Color.white);
        g2.fillRect(0, 0, w, h);
        g2.setColor(Color.black);
      }

      // / sets font, RenderingHints.
      setParams(g2);

      // / If flag is set, recalculate fontMetrics and reset the scrollbar
      if (updateFontMetrics || isPrinting) {
        // / NOTE: re-calculates in case G2 transform
        // / is something other than NONE
        calcFontMetrics(g2, w, h);
        updateFontMetrics = false;
      }
      // / Calculate the amount of text that can be drawn...
      calcTextRange();

      // / Draw according to the set "Text to Use" mode
      if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
        int charToDraw = drawStart;
        if (showGrid)
          drawGrid(g2);
        if (!isPrinting)
          g.drawImage(backBuffer, 0, 0, this);

        for (int i = 0; i < numCharDown && charToDraw <= drawEnd; i++) {
          for (int j = 0; j < numCharAcross && charToDraw <= drawEnd; j++, charToDraw++) {
            int gridLocX = j * gridWidth + canvasInset_X;
            int gridLocY = i * gridHeight + canvasInset_Y;

            modeSpecificDrawChar(g2, charToDraw, gridLocX + gridWidth / 2, gridLocY + maxAscent);
            // if ( !isPrinting ) {
            // g.setClip( gridLocX, gridLocY, gridWidth + 1, gridHeight + 1 );
            // g.drawImage( backBuffer, 0, 0, this );
            // }

          }
        }
      } else if (textToUse == USER_TEXT) {
        g2.drawRect(0, 0, w - 1, h - 1);
        if (!isPrinting)
          g.drawImage(backBuffer, 0, 0, this);

        for (int i = drawStart; i <= drawEnd; i++) {
          int lineStartX = canvasInset_Y;
          int lineStartY = (i - drawStart) * gridHeight + maxAscent;
          modeSpecificDrawLine(g2, userText[i], lineStartX, lineStartY);
        }
      } else {
        float xPos, yPos = (float) canvasInset_Y;
        g2.drawRect(0, 0, w - 1, h - 1);
        if (!isPrinting)
          g.drawImage(backBuffer, 0, 0, this);

        for (int i = drawStart; i <= drawEnd; i++) {
          TextLayout oneLine = (TextLayout) lineBreakTLs.elementAt(i);
          xPos = oneLine.isLeftToRight() ? canvasInset_X
              : ((float) w - oneLine.getAdvance() - canvasInset_X);

          float fmData[] = { 0, oneLine.getAscent(), 0, oneLine.getDescent(), 0,
              oneLine.getLeading() };
          if (g2Transform != NONE) {
            AffineTransform at = getAffineTransform(g2Transform);
            at.transform(fmData, 0, fmData, 0, 3);
          }
          // yPos += oneLine.getAscent();
          yPos += fmData[1]; // ascent
          // oneLine.draw( g2, xPos, yPos );
          tlDrawLine(g2, oneLine, xPos, yPos);
          // yPos += oneLine.getDescent() + oneLine.getLeading();
          yPos += fmData[3] + fmData[5]; // descent + leading
        }
      }
      if (!isPrinting)
        g.drawImage(backBuffer, 0, 0, this);
      g2.dispose();
    }

    // / Component paintComponent function...
    // / Draws/Refreshes canvas according to flag(s) set by other functions
    public void paintComponent(Graphics g) {
      if (updateBackBuffer) {
        Dimension d = this.getSize();
        isPrinting = false;
        try {
          drawText(g, d.width, d.height);
        } catch (CannotDrawException e) {
          f2dt.fireChangeStatus(ERRORS[e.id], true);
          super.paintComponent(g);
          return;
        }
      } else {
        // / Screen refresh
        g.drawImage(backBuffer, 0, 0, this);
      }

      showingError = false;
      updateBackBuffer = false;
    }

    // / Printable interface function
    // / Component print function...
    public int print(Graphics g, PageFormat pf, int pageIndex) {
      if (pageIndex == 0) {
        // / Reset the last page index to max...
        lastPage = Integer.MAX_VALUE;
        currentlyShownChar = verticalBar.getValue() * numCharAcross;
      }

      if (printMode == ONE_PAGE) {
        if (pageIndex > 0)
          return NO_SUCH_PAGE;
      } else {
        if (pageIndex > lastPage)
          return NO_SUCH_PAGE;
      }

      int pageWidth = (int) pf.getImageableWidth();
      int pageHeight = (int) pf.getImageableHeight();
      // / Back up metrics and other drawing info before printing modifies it
      int backupDrawStart = drawStart, backupDrawEnd = drawEnd;
      int backupNumCharAcross = numCharAcross, backupNumCharDown = numCharDown;
      Vector backupLineBreakTLs = null;
      if (textToUse == FILE_TEXT)
        backupLineBreakTLs = (Vector) lineBreakTLs.clone();

      printPageNumber = pageIndex;
      isPrinting = true;
      // / Push the actual draw area 60 down to allow info to be printed
      g.translate((int) pf.getImageableX(), (int) pf.getImageableY() + 60);
      try {
        drawText(g, pageWidth, pageHeight - 60);
      } catch (CannotDrawException e) {
        f2dt.fireChangeStatus(ERRORS[e.id], true);
        return NO_SUCH_PAGE;
      }

      // / Draw information about what is being printed
      String hints = (" with antialias " + antiAliasType + "and" + " fractional metrics "
          + fractionalMetricsType + " and lcd contrast = " + lcdContrast);
      String infoLine1 = ("Printing" + MS_OPENING[textToUse] + modeSpecificNumStr(drawStart)
          + " to " + modeSpecificNumStr(drawEnd) + MS_CLOSING[textToUse]);
      String infoLine2 = ("With " + fontName + " " + STYLES[fontStyle] + " at " + fontSize
          + " point size " + TRANSFORMS[fontTransform]);
      String infoLine3 = "Using " + METHODS[drawMethod] + hints;
      String infoLine4 = "Page: " + (pageIndex + 1);
      g.setFont(new Font("dialog", Font.PLAIN, 12));
      g.setColor(Color.black);
      g.translate(0, -60);
      g.drawString(infoLine1, 15, 10);
      g.drawString(infoLine2, 15, 22);
      g.drawString(infoLine3, 15, 34);
      g.drawString(infoLine4, 15, 46);

      if (drawEnd == drawLimit)
        // / This indicates that the draw will be completed with this page
        lastPage = pageIndex;

      // / Restore the changed values back...
      // / This is important for JScrollBar settings and LineBreak'ed TLs
      drawStart = backupDrawStart;
      drawEnd = backupDrawEnd;
      numCharAcross = backupNumCharAcross;
      numCharDown = backupNumCharDown;
      if (textToUse == FILE_TEXT)
        lineBreakTLs = backupLineBreakTLs;
      return PAGE_EXISTS;
    }

    // / Ouputs the current canvas into a given PNG file
    public void writePNG(String fileName) {
      try {
        ImageIO.write(backBuffer, "png", new java.io.File(fileName));
      } catch (Exception e) {
        f2dt.fireChangeStatus("ERROR: Failed to Save PNG image; See stack trace", true);
        e.printStackTrace();
      }
    }

    // / Figures out whether a character at the pointer location is valid
    // / And if so, updates mouse location informations, as well as
    // / the information on the status bar
    private boolean checkMouseLoc(MouseEvent e) {
      if (gridWidth != 0 && gridHeight != 0)
        if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
          int charLocX = (e.getX() - canvasInset_X) / gridWidth;
          int charLocY = (e.getY() - canvasInset_Y) / gridHeight;

          // / Check to make sure the mouse click location is within drawn area
          if (charLocX >= 0 && charLocY >= 0 && charLocX < numCharAcross && charLocY < numCharDown) {
            int mouseOverChar = charLocX + (verticalBar.getValue() + charLocY) * numCharAcross;
            if (textToUse == RANGE_TEXT)
              mouseOverChar += drawRange[0];
            if (mouseOverChar > drawEnd)
              return false;

            mouseOverCharX = charLocX;
            mouseOverCharY = charLocY;
            currMouseOverChar = mouseOverChar;
            // / Update status bar
            f2dt.fireChangeStatus("Pointing to" + MS_OPENING[textToUse]
                + modeSpecificNumStr(mouseOverChar), false);
            return true;
          }
        }
      return false;
    }

    // / Shows (updates) the character zoom window
    public void showZoomed() {
      GlyphVector gv;
      Font backup = testFont;
      Point canvasLoc = this.getLocationOnScreen();

      // / Calculate the zoom area's location and size...
      int dialogOffsetX = (int) (gridWidth * (ZOOM - 1) / 2);
      int dialogOffsetY = (int) (gridHeight * (ZOOM - 1) / 2);
      int zoomAreaX = mouseOverCharX * gridWidth + canvasInset_X - dialogOffsetX;
      int zoomAreaY = mouseOverCharY * gridHeight + canvasInset_Y - dialogOffsetY;
      int zoomAreaWidth = (int) (gridWidth * ZOOM);
      int zoomAreaHeight = (int) (gridHeight * ZOOM);

      // / Position and set size of zoom window as needed
      zoomWindow.setLocation(canvasLoc.x + zoomAreaX, canvasLoc.y + zoomAreaY);
      if (!nowZooming) {
        if (zoomWindow.getWarningString() != null)
          // / If this is not opened as a "secure" window,
          // / it has a banner below the zoom dialog which makes it look really
          // BAD
          // / So enlarge it by a bit
          zoomWindow.setSize(zoomAreaWidth + 1, zoomAreaHeight + 20);
        else
          zoomWindow.setSize(zoomAreaWidth + 1, zoomAreaHeight + 1);
      }

      // / Prepare zoomed image
      zoomImage = (BufferedImage) zoomWindow.createImage(zoomAreaWidth + 1, zoomAreaHeight + 1);
      Graphics2D g2 = (Graphics2D) zoomImage.getGraphics();
      testFont = testFont.deriveFont(fontSize * ZOOM);
      setParams(g2);
      g2.setColor(Color.white);
      g2.fillRect(0, 0, zoomAreaWidth, zoomAreaHeight);
      g2.setColor(Color.black);
      g2.drawRect(0, 0, zoomAreaWidth, zoomAreaHeight);
      modeSpecificDrawChar(g2, currMouseOverChar, zoomAreaWidth / 2, (int) (maxAscent * ZOOM));
      g2.dispose();
      if (!nowZooming)
        zoomWindow.show();
      // / This is sort of redundant... since there is a paint function
      // / inside zoomWindow definition that does the drawImage.
      // / (I should be able to call just repaint() here)
      // / However, for some reason, that paint function fails to respond
      // / from second time and on; So I have to force the paint here...
      zoomWindow.getGraphics().drawImage(zoomImage, 0, 0, this);

      nowZooming = true;
      prevZoomChar = currMouseOverChar;
      testFont = backup;

      // Windows does not repaint correctly, after
      // a zoom. Thus, we need to force the canvas
      // to repaint, but only once. After the first repaint,
      // everything stabilizes. [ABP]
      if (firstTime()) {
        refresh();
      }
    }

    // / Listener Functions

    // / MouseListener interface function
    // / Zooms a character when mouse is pressed above it
    public void mousePressed(MouseEvent e) {
      if (!showingError) {
        if (checkMouseLoc(e)) {
          showZoomed();
          this.setCursor(blankCursor);
        }
      }
    }

    // / MouseListener interface function
    // / Redraws the area that was drawn over by zoomed character
    public void mouseReleased(MouseEvent e) {
      if (textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS) {
        if (nowZooming)
          zoomWindow.hide();
        nowZooming = false;
      }
      this.setCursor(Cursor.getDefaultCursor());
    }

    // / MouseListener interface function
    // / Resets the status bar to display range instead of a specific character
    public void mouseExited(MouseEvent e) {
      if (!showingError && !nowZooming)
        f2dt.fireChangeStatus(backupStatusString, false);
    }

    // / MouseMotionListener interface function
    // / Adjusts the status bar message when mouse moves over a character
    public void mouseMoved(MouseEvent e) {
      if (!showingError) {
        if (!checkMouseLoc(e))
          f2dt.fireChangeStatus(backupStatusString, false);
      }
    }

    // / MouseMotionListener interface function
    // / Scrolls the zoomed character when mouse is dragged
    public void mouseDragged(MouseEvent e) {
      if (!showingError)
        if (nowZooming) {
          if (checkMouseLoc(e) && currMouseOverChar != prevZoomChar)
            showZoomed();
        }
    }

    // / Empty function to comply with interface requirement
    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
  }

  private final class CannotDrawException extends RuntimeException {
    // / Error ID
    public final int id;

    public CannotDrawException(int i) {
      id = i;
    }
  }

  enum FMValues {
    FMDEFAULT("DEFAULT", VALUE_FRACTIONALMETRICS_DEFAULT), FMOFF("OFF", VALUE_FRACTIONALMETRICS_OFF), FMON(
        "ON", VALUE_FRACTIONALMETRICS_ON);

    private String name;

    private Object hint;

    private static FMValues[] valArray;

    FMValues(String s, Object o) {
      name = s;
      hint = o;
    }

    public String toString() {
      return name;
    }

    public Object getHint() {
      return hint;
    }

    public static Object getValue(int ordinal) {
      if (valArray == null) {
        valArray = (FMValues[]) EnumSet.allOf(FMValues.class).toArray(new FMValues[0]);
      }
      for (int i = 0; i < valArray.length; i++) {
        if (valArray[i].ordinal() == ordinal) {
          return valArray[i];
        }
      }
      return valArray[0];
    }

    private static FMValues[] getArray() {
      if (valArray == null) {
        valArray = (FMValues[]) EnumSet.allOf(FMValues.class).toArray(new FMValues[0]);
      }
      return valArray;
    }

    public static int getHintVal(Object hint) {
      getArray();
      for (int i = 0; i < valArray.length; i++) {
        if (valArray[i].getHint() == hint) {
          return i;
        }
      }
      return 0;
    }
  }

  enum AAValues {
    AADEFAULT("DEFAULT", VALUE_TEXT_ANTIALIAS_DEFAULT), AAOFF("OFF", VALUE_TEXT_ANTIALIAS_OFF), AAON(
        "ON", VALUE_TEXT_ANTIALIAS_ON), AAGASP("GASP", VALUE_TEXT_ANTIALIAS_GASP), AALCDHRGB(
        "LCD_HRGB", VALUE_TEXT_ANTIALIAS_LCD_HRGB), AALCDHBGR("LCD_HBGR",
        VALUE_TEXT_ANTIALIAS_LCD_HBGR), AALCDVRGB("LCD_VRGB", VALUE_TEXT_ANTIALIAS_LCD_VRGB), AALCDVBGR(
        "LCD_VBGR", VALUE_TEXT_ANTIALIAS_LCD_VBGR);

    private String name;

    private Object hint;

    private static AAValues[] valArray;

    AAValues(String s, Object o) {
      name = s;
      hint = o;
    }

    public String toString() {
      return name;
    }

    public Object getHint() {
      return hint;
    }

    public static boolean isLCDMode(Object o) {
      return (o instanceof AAValues && ((AAValues) o).ordinal() >= AALCDHRGB.ordinal());
    }

    public static Object getValue(int ordinal) {
      if (valArray == null) {
        valArray = (AAValues[]) EnumSet.allOf(AAValues.class).toArray(new AAValues[0]);
      }
      for (int i = 0; i < valArray.length; i++) {
        if (valArray[i].ordinal() == ordinal) {
          return valArray[i];
        }
      }
      return valArray[0];
    }

    private static AAValues[] getArray() {
      if (valArray == null) {
        Object[] oa = EnumSet.allOf(AAValues.class).toArray(new AAValues[0]);
        valArray = (AAValues[]) (EnumSet.allOf(AAValues.class).toArray(new AAValues[0]));
      }
      return valArray;
    }

    public static int getHintVal(Object hint) {
      getArray();
      for (int i = 0; i < valArray.length; i++) {
        if (valArray[i].getHint() == hint) {
          return i;
        }
      }
      return 0;
    }

  }

  private static Integer defaultContrast;

  static Integer getDefaultLCDContrast() {
    if (defaultContrast == null) {
      GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
          .getDefaultScreenDevice().getDefaultConfiguration();
      Graphics2D g2d = (Graphics2D) (gc.createCompatibleImage(1, 1).getGraphics());
      defaultContrast = (Integer) g2d.getRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST);
    }
    return defaultContrast;
  }
}

/*
 * @(#)RangeMenu.java 1.18 05/11/17
 * 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * -Redistribution of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS
 * LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
 * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or intended for
 * use in the design, construction, operation or maintenance of any nuclear
 * facility.
 */

/*
 * @(#)RangeMenu.java 1.18 05/11/17
 */

/**
 * RangeMenu.java
 * 
 * @version
 * @(#)RangeMenu.java 1.1 00/08/22
 * @author Shinsuke Fukuda
 * @author Ankit Patel [Conversion to Swing - 01/07/30]
 */

// / Custom made choice menu that holds data for unicode range
final class RangeMenu extends JComboBox implements ActionListener {

  // / Painfully extracted from java.lang.Character.UnicodeBlock. Arrrgh!
  // / Unicode 3.0 data.

  private final int[][] UNICODE_RANGES = { { 0x000000, 0x00007f }, // /
                                                                    // BASIC_LATIN
      { 0x000080, 0x0000ff }, // / LATIN_1_SUPPLEMENT
      { 0x000100, 0x00017f }, // / LATIN_EXTENDED_A
      { 0x000180, 0x00024f }, // / LATIN_EXTENDED_B
      { 0x000250, 0x0002af }, // / IPA_EXTENSIONS
      { 0x0002b0, 0x0002ff }, // / SPACING_MODIFIER_LETTERS
      { 0x000300, 0x00036f }, // / COMBINING_DIACRITICAL_MARKS
      { 0x000370, 0x0003ff }, // / GREEK
      { 0x000400, 0x0004ff }, // / CYRILLIC
      { 0x000500, 0x00052f }, // / CYRILLIC_SUPPLEMENTARY
      { 0x000530, 0x00058f }, // / ARMENIAN
      { 0x000590, 0x0005ff }, // / HEBREW
      { 0x000600, 0x0006ff }, // / ARABIC
      { 0x000700, 0x00074f }, // / SYRIAC
      { 0x000780, 0x0007bf }, // / THAANA
      { 0x000900, 0x00097f }, // / DEVANAGARI
      { 0x000980, 0x0009ff }, // / BENGALI
      { 0x000a00, 0x000a7f }, // / GURMUKHI
      { 0x000a80, 0x000aff }, // / GUJARATI
      { 0x000b00, 0x000b7f }, // / ORIYA
      { 0x000b80, 0x000bff }, // / TAMIL
      { 0x000c00, 0x000c7f }, // / TELUGU
      { 0x000c80, 0x000cff }, // / KANNADA
      { 0x000d00, 0x000d7f }, // / MALAYALAM
      { 0x000d80, 0x000dff }, // / SINHALA
      { 0x000e00, 0x000e7f }, // / THAI
      { 0x000e80, 0x000eff }, // / LAO
      { 0x000f00, 0x000fff }, // / TIBETAN
      { 0x001000, 0x00109f }, // / MYANMAR
      { 0x0010a0, 0x0010ff }, // / GEORGIAN
      { 0x001100, 0x0011ff }, // / HANGUL_JAMO
      { 0x001200, 0x00137f }, // / ETHIOPIC
      { 0x0013a0, 0x0013ff }, // / CHEROKEE
      { 0x001400, 0x00167f }, // / UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS
      { 0x001680, 0x00169f }, // / OGHAM
      { 0x0016a0, 0x0016ff }, // / RUNIC
      { 0x001700, 0x00171f }, // / TAGALOG
      { 0x001720, 0x00173f }, // / HANUNOO
      { 0x001740, 0x00175f }, // / BUHID
      { 0x001760, 0x00177f }, // / TAGBANWA
      { 0x001780, 0x0017ff }, // / KHMER
      { 0x001800, 0x0018af }, // / MONGOLIAN
      { 0x001900, 0x00194f }, // / LIMBU
      { 0x001950, 0x00197f }, // / TAI_LE
      { 0x0019e0, 0x0019ff }, // / KHMER_SYMBOLS
      { 0x001d00, 0x001d7f }, // / PHONETIC_EXTENSIONS
      { 0x001e00, 0x001eff }, // / LATIN_EXTENDED_ADDITIONAL
      { 0x001f00, 0x001fff }, // / GREEK_EXTENDED
      { 0x002000, 0x00206f }, // / GENERAL_PUNCTUATION
      { 0x002070, 0x00209f }, // / SUPERSCRIPTS_AND_SUBSCRIPTS
      { 0x0020a0, 0x0020cf }, // / CURRENCY_SYMBOLS
      { 0x0020d0, 0x0020ff }, // / COMBINING_MARKS_FOR_SYMBOLS
      { 0x002100, 0x00214f }, // / LETTERLIKE_SYMBOLS
      { 0x002150, 0x00218f }, // / NUMBER_FORMS
      { 0x002190, 0x0021ff }, // / ARROWS
      { 0x002200, 0x0022ff }, // / MATHEMATICAL_OPERATORS
      { 0x002300, 0x0023ff }, // / MISCELLANEOUS_TECHNICAL
      { 0x002400, 0x00243f }, // / CONTROL_PICTURES
      { 0x002440, 0x00245f }, // / OPTICAL_CHARACTER_RECOGNITION
      { 0x002460, 0x0024ff }, // / ENCLOSED_ALPHANUMERICS
      { 0x002500, 0x00257f }, // / BOX_DRAWING
      { 0x002580, 0x00259f }, // / BLOCK_ELEMENTS
      { 0x0025a0, 0x0025ff }, // / GEOMETRIC_SHAPES
      { 0x002600, 0x0026ff }, // / MISCELLANEOUS_SYMBOLS
      { 0x002700, 0x0027bf }, // / DINGBATS
      { 0x0027c0, 0x0027ef }, // / MISCELLANEOUS_MATHEMATICAL_SYMBOLS_A
      { 0x0027f0, 0x0027ff }, // / SUPPLEMENTAL_ARROWS_A
      { 0x002800, 0x0028ff }, // / BRAILLE_PATTERNS
      { 0x002900, 0x00297f }, // / SUPPLEMENTAL_ARROWS_B
      { 0x002980, 0x0029ff }, // / MISCELLANEOUS_MATHEMATICAL_SYMBOLS_B
      { 0x002a00, 0x002aff }, // / SUPPLEMENTAL_MATHEMATICAL_OPERATORS
      { 0x002b00, 0x002bff }, // / MISCELLANEOUS_SYMBOLS_AND_ARROWS
      { 0x002e80, 0x002eff }, // / CJK_RADICALS_SUPPLEMENT
      { 0x002f00, 0x002fdf }, // / KANGXI_RADICALS
      { 0x002ff0, 0x002fff }, // / IDEOGRAPHIC_DESCRIPTION_CHARACTERS
      { 0x003000, 0x00303f }, // / CJK_SYMBOLS_AND_PUNCTUATION
      { 0x003040, 0x00309f }, // / HIRAGANA
      { 0x0030a0, 0x0030ff }, // / KATAKANA
      { 0x003100, 0x00312f }, // / BOPOMOFO
      { 0x003130, 0x00318f }, // / HANGUL_COMPATIBILITY_JAMO
      { 0x003190, 0x00319f }, // / KANBUN
      { 0x0031a0, 0x0031bf }, // / BOPOMOFO_EXTENDED
      { 0x0031f0, 0x0031ff }, // / KATAKANA_PHONETIC_EXTENSIONS
      { 0x003200, 0x0032ff }, // / ENCLOSED_CJK_LETTERS_AND_MONTHS
      { 0x003300, 0x0033ff }, // / CJK_COMPATIBILITY
      { 0x003400, 0x004dbf }, // / CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
      { 0x004dc0, 0x004dff }, // / YIJING_HEXAGRAM_SYMBOLS
      { 0x004e00, 0x009fff }, // / CJK_UNIFIED_IDEOGRAPHS
      { 0x00a000, 0x00a48f }, // / YI_SYLLABLES
      { 0x00a490, 0x00a4cf }, // / YI_RADICALS
      { 0x00ac00, 0x00d7af }, // / HANGUL_SYLLABLES
      { 0x00d800, 0x00dfff }, // / SURROGATES_AREA
      { 0x00e000, 0x00f8ff }, // / PRIVATE_USE_AREA
      { 0x00f900, 0x00faff }, // / CJK_COMPATIBILITY_IDEOGRAPHS
      { 0x00fb00, 0x00fb4f }, // / ALPHABETIC_PRESENTATION_FORMS
      { 0x00fb50, 0x00fdff }, // / ARABIC_PRESENTATION_FORMS_A
      { 0x00fe00, 0x00fe0f }, // / VARIATION_SELECTORS
      { 0x00fe20, 0x00fe2f }, // / COMBINING_HALF_MARKS
      { 0x00fe30, 0x00fe4f }, // / CJK_COMPATIBILITY_FORMS
      { 0x00fe50, 0x00fe6f }, // / SMALL_FORM_VARIANTS
      { 0x00fe70, 0x00feff }, // / ARABIC_PRESENTATION_FORMS_B
      { 0x00ff00, 0x00ffef }, // / HALFWIDTH_AND_FULLWIDTH_FORMS
      { 0x00fff0, 0x00ffff }, // / SPECIALS
      { 0x010000, 0x01007f }, // / LINEAR_B_SYLLABARY
      { 0x010080, 0x0100ff }, // / LINEAR_B_IDEOGRAMS
      { 0x010100, 0x01013f }, // / AEGEAN_NUMBERS
      { 0x010300, 0x01032f }, // / OLD_ITALIC
      { 0x010330, 0x01034f }, // / GOTHIC
      { 0x010380, 0x01039f }, // / UGARITIC
      { 0x010400, 0x01044f }, // / DESERET
      { 0x010450, 0x01047f }, // / SHAVIAN
      { 0x010480, 0x0104af }, // / OSMANYA
      { 0x010800, 0x01083f }, // / CYPRIOT_SYLLABARY
      { 0x01d000, 0x01d0ff }, // / BYZANTINE_MUSICAL_SYMBOLS
      { 0x01d100, 0x01d1ff }, // / MUSICAL_SYMBOLS
      { 0x01d300, 0x01d35f }, // / TAI_XUAN_JING_SYMBOLS
      { 0x01d400, 0x01d7ff }, // / MATHEMATICAL_ALPHANUMERIC_SYMBOLS
      { 0x020000, 0x02a6df }, // / CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
      { 0x02f800, 0x02fa1f }, // / CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT
      { 0x0e0000, 0x0e007f }, // / TAGS
      { 0x0e0100, 0x0e01ef }, // / VARIATION_SELECTORS_SUPPLEMENT
      { 0x0f0000, 0x0fffff }, // / SUPPLEMENTARY_PRIVATE_USE_AREA_A
      { 0x100000, 0x10ffff }, // / SUPPLEMENTARY_PRIVATE_USE_AREA_B
      { 0x000000, 0x00007f }, // / OTHER [USER DEFINED RANGE]
  };

  private final String[] UNICODE_RANGE_NAMES = { "Basic Latin", "Latin-1 Supplement",
      "Latin Extended-A", "Latin Extended-B", "IPA Extensions", "Spacing Modifier Letters",
      "Combining Diacritical Marks", "Greek", "Cyrillic", "Cyrillic Supplement", "Armenian",
      "Hebrew", "Arabic", "Syriac", "Thaana", "Devanagari", "Bengali", "Gurmukhi", "Gujarati",
      "Oriya", "Tamil", "Telugu", "Kannada", "Malayalam", "Sinhala", "Thai", "Lao", "Tibetan",
      "Myanmar", "Georgian", "Hangul Jamo", "Ethiopic", "Cherokee",
      "Unified Canadian Aboriginal Syllabics", "Ogham", "Runic", "Tagalog", "Hanunoo", "Buhid",
      "Tagbanwa", "Khmer", "Mongolian", "Limbu", "Tai Le", "Khmer Symbols", "Phonetic Extensions",
      "Latin Extended Additional", "Greek Extended", "General Punctuation",
      "Superscripts and Subscripts", "Currency Symbols", "Combining Marks for Symbols",
      "Letterlike Symbols", "Number Forms", "Arrows", "Mathematical Operators",
      "Miscellaneous Technical", "Control Pictures", "Optical Character Recognition",
      "Enclosed Alphanumerics", "Box Drawing", "Block Elements", "Geometric Shapes",
      "Miscellaneous Symbols", "Dingbats", "Miscellaneous Mathematical Symbols-A",
      "Supplemental Arrows-A", "Braille Patterns", "Supplemental Arrows-B",
      "Miscellaneous Mathematical Symbols-B", "Supplemental Mathematical Operators",
      "Miscellaneous Symbols and Arrows", "CJK Radicals Supplement", "Kangxi Radicals",
      "Ideographic Description Characters", "CJK Symbols and Punctuation", "Hiragana", "Katakana",
      "Bopomofo", "Hangul Compatibility Jamo", "Kanbun", "Bopomofo Extended",
      "Katakana Phonetic Extensions", "Enclosed CJK Letters and Months", "CJK Compatibility",
      "CJK Unified Ideographs Extension A", "Yijing Hexagram Symbols", "CJK Unified Ideographs",
      "Yi Syllables", "Yi Radicals", "Hangul Syllables", "Surrogates Area", // High
                                                                            // Surrogates,
                                                                            // High
                                                                            // Private
                                                                            // Use
                                                                            // Surrogates,
                                                                            // Low
                                                                            // Surrogates
      "Private Use Area", "CJK Compatibility Ideographs", "Alphabetic Presentation Forms",
      "Arabic Presentation Forms-A", "Variation Selectors", "Combining Half Marks",
      "CJK Compatibility Forms", "Small Form Variants", "Arabic Presentation Forms-B",
      "Halfwidth and Fullwidth Forms", "Specials", "Linear B Syllabary", "Linear B Ideograms",
      "Aegean Numbers", "Old Italic", "Gothic", "Ugaritic", "Deseret", "Shavian", "Osmanya",
      "Cypriot Syllabary", "Byzantine Musical Symbols", "Musical Symbols", "Tai Xuan Jing Symbols",
      "Mathematical Alphanumeric Symbols", "CJK Unified Ideographs Extension B",
      "CJK Compatibility Ideographs Supplement", "Tags", "Variation Selectors Supplement",
      "Supplementary Private Use Area-A", "Supplementary Private Use Area-B", "Custom...", };

  private boolean useCustomRange = false;

  private int[] customRange = { 0x0000, 0x007f };

  // / Custom range dialog variables
  private final JDialog customRangeDialog;

  private final JTextField customRangeStart = new JTextField("0000", 4);

  private final JTextField customRangeEnd = new JTextField("007F", 4);

  private final int CUSTOM_RANGE_INDEX = UNICODE_RANGE_NAMES.length - 1;

  // / Parent Font2DTest Object holder
  private final Font2DTest parent;

  public static final int SURROGATES_AREA_INDEX = 91;

  public RangeMenu(Font2DTest demo, JFrame f) {
    super();
    parent = demo;

    for (int i = 0; i < UNICODE_RANGE_NAMES.length; i++)
      addItem(UNICODE_RANGE_NAMES[i]);

    setSelectedIndex(0);
    addActionListener(this);

    // / Set up custom range dialog...
    customRangeDialog = new JDialog(f, "Custom Unicode Range", true);
    customRangeDialog.setResizable(false);

    JPanel dialogTop = new JPanel();
    JPanel dialogBottom = new JPanel();
    JButton okButton = new JButton("OK");
    JLabel from = new JLabel("From:");
    JLabel to = new JLabel("To:");
    Font labelFont = new Font("dialog", Font.BOLD, 12);
    from.setFont(labelFont);
    to.setFont(labelFont);
    okButton.setFont(labelFont);

    dialogTop.add(from);
    dialogTop.add(customRangeStart);
    dialogTop.add(to);
    dialogTop.add(customRangeEnd);
    dialogBottom.add(okButton);
    okButton.addActionListener(this);

    customRangeDialog.getContentPane().setLayout(new BorderLayout());
    customRangeDialog.getContentPane().add("North", dialogTop);
    customRangeDialog.getContentPane().add("South", dialogBottom);
    customRangeDialog.pack();
  }

  // / Return the range that is currently selected

  public int[] getSelectedRange() {
    if (useCustomRange) {
      int startIndex, endIndex;
      String startText, endText;
      String empty = "";
      try {
        startText = customRangeStart.getText().trim();
        endText = customRangeEnd.getText().trim();
        if (startText.equals(empty) && !endText.equals(empty)) {
          endIndex = Integer.parseInt(endText, 16);
          startIndex = endIndex - 7 * 25;
        } else if (!startText.equals(empty) && endText.equals(empty)) {
          startIndex = Integer.parseInt(startText, 16);
          endIndex = startIndex + 7 * 25;
        } else {
          startIndex = Integer.parseInt(customRangeStart.getText(), 16);
          endIndex = Integer.parseInt(customRangeEnd.getText(), 16);
        }
      } catch (Exception e) {
        // / Error in parsing the hex number ---
        // / Reset the range to what it was before and return that
        customRangeStart.setText(Integer.toString(customRange[0], 16));
        customRangeEnd.setText(Integer.toString(customRange[1], 16));
        return customRange;
      }

      if (startIndex < 0)
        startIndex = 0;
      if (endIndex > 0xffff)
        endIndex = 0xffff;
      if (startIndex > endIndex)
        startIndex = endIndex;

      customRange[0] = startIndex;
      customRange[1] = endIndex;
      return customRange;
    } else
      return UNICODE_RANGES[getSelectedIndex()];
  }

  // / Function used by loadOptions in Font2DTest main panel
  // / to reset setting and range selection
  public void setSelectedRange(String name, int start, int end) {
    setSelectedItem(name);
    customRange[0] = start;
    customRange[1] = end;
    parent.fireRangeChanged();
  }

  // / ActionListener interface function
  // / ABP
  // / moved JComboBox event code into this fcn from
  // / itemStateChanged() method. Part of change to Swing.
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (source instanceof JComboBox) {
      String rangeName = (String) ((JComboBox) source).getSelectedItem();

      if (rangeName.equals("Custom...")) {
        useCustomRange = true;
        customRangeDialog.setLocationRelativeTo(parent);
        customRangeDialog.show();
      } else {
        useCustomRange = false;
      }
      parent.fireRangeChanged();
    } else if (source instanceof JButton) {
      // / Since it is only "OK" button that sends any action here...
      customRangeDialog.hide();
    }
  }
}

          
    
    
  








Related examples in the same category

1.Font Chooser Source CodeFont Chooser Source Code
2.JFreeChart: Font DialogJFreeChart: Font Dialog
3.Font Chooser extends javax.swing.JDialogFont Chooser extends javax.swing.JDialog
4.Font Dialog from clariboleFont Dialog from claribole
5.Font Loader Dialog
6.FontChooser by Noah w
7.FontChooser, adapted from NwFontChooserS by Noah WairauchFontChooser, adapted from NwFontChooserS by Noah Wairauch
8.JFont Chooser
9.Font dialog
10.A dialog allow selection and a font and its associated info.A dialog allow selection and a font and its associated info.
11.The JFontChooser class is a swing component for font selection.