< prev index next >

src/demo/share/jfc/Font2DTest/Font2DTest.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  99     private final ChoiceV2 textMenu;
 100     private int currentTextChoice = 0;
 101     private final ChoiceV2 transformMenu;
 102     private final ChoiceV2 transformMenuG2;
 103     private final ChoiceV2 methodsMenu;
 104     private final JComboBox antiAliasMenu;
 105     private final JComboBox fracMetricsMenu;
 106 
 107     private final JSlider contrastSlider;
 108 
 109     /// CheckboxMenuItems
 110     private CheckboxMenuItemV2 displayGridCBMI;
 111     private CheckboxMenuItemV2 force16ColsCBMI;
 112     private CheckboxMenuItemV2 showFontInfoCBMI;
 113 
 114     /// JDialog boxes
 115     private JDialog userTextDialog;
 116     private JTextArea userTextArea;
 117     private JDialog printDialog;
 118     private JDialog fontInfoDialog;
 119     private LabelV2 fontInfos[] = new LabelV2[2];
 120     private JFileChooser filePromptDialog = null;
 121 
 122     private ButtonGroup printCBGroup;
 123     private JRadioButton printModeCBs[] = new JRadioButton[3];
 124 
 125     /// Status bar
 126     private final LabelV2 statusBar;
 127 
 128     private int fontStyles [] = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
 129 
 130     /// Text filename
 131     private String tFileName;
 132 
 133     // Enabled or disabled status of canDisplay check
 134     private static boolean canDisplayCheck = true;
 135 
 136     /// Initialize GUI variables and its layouts
 137     public Font2DTest( JFrame f, boolean isApplet ) {
 138         parent = f;
 139 
 140         rm = new RangeMenu( this, parent );
 141         fp = new FontPanel( this, parent );
 142         statusBar = new LabelV2("");
 143 
 144         fontMenu = new ChoiceV2( this, canDisplayCheck );
 145         sizeField = new JTextField( "12", 3 );
 146         sizeField.addActionListener( this );
 147         styleMenu = new ChoiceV2( this );
 148         textMenu = new ChoiceV2( ); // listener added later


 272         if ( !isApplet )
 273           fileMenu.add( new MenuItemV2( "Exit", this ));
 274         else
 275           fileMenu.add( new MenuItemV2( "Close", this ));
 276 
 277         displayGridCBMI = new CheckboxMenuItemV2( "Display Grid", true, this );
 278         force16ColsCBMI = new CheckboxMenuItemV2( "Force 16 Columns", false, this );
 279         showFontInfoCBMI = new CheckboxMenuItemV2( "Display Font Info", false, this );
 280         optionMenu.add( displayGridCBMI );
 281         optionMenu.add( force16ColsCBMI );
 282         optionMenu.add( showFontInfoCBMI );
 283 
 284         JMenuBar mb = parent.getJMenuBar();
 285         if ( mb == null )
 286           mb = new JMenuBar();
 287         mb.add( fileMenu );
 288         mb.add( optionMenu );
 289 
 290         parent.setJMenuBar( mb );
 291 
 292         String fontList[] =
 293           GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
 294 
 295         for ( int i = 0; i < fontList.length; i++ )
 296           fontMenu.addItem( fontList[i] );
 297         fontMenu.setSelectedItem( "Dialog" );
 298 
 299         styleMenu.addItem( "Plain" );
 300         styleMenu.addItem( "Bold" );
 301         styleMenu.addItem( "Italic" );
 302         styleMenu.addItem( "Bold Italic" );
 303 
 304         transformMenu.addItem( "None" );
 305         transformMenu.addItem( "Scale" );
 306         transformMenu.addItem( "Shear" );
 307         transformMenu.addItem( "Rotate" );
 308 
 309         transformMenuG2.addItem( "None" );
 310         transformMenuG2.addItem( "Scale" );
 311         transformMenuG2.addItem( "Shear" );
 312         transformMenuG2.addItem( "Rotate" );


 405                 fontInfoDialog.hide();
 406                 showFontInfoCBMI.setState( false );
 407             }
 408         });
 409         JPanel fontInfoPanel = new JPanel();
 410         fontInfoPanel.setLayout( new GridLayout( fontInfos.length, 1 ));
 411         for ( int i = 0; i < fontInfos.length; i++ ) {
 412             fontInfos[i] = new LabelV2("");
 413             fontInfoPanel.add( fontInfos[i] );
 414         }
 415         fontInfoDialog.getContentPane().add( fontInfoPanel );
 416 
 417         /// Move the location of the dialog...
 418         userTextDialog.setLocation( 200, 300 );
 419         fontInfoDialog.setLocation( 0, 400 );
 420     }
 421 
 422     /// RangeMenu object signals using this function
 423     /// when Unicode range has been changed and text needs to be redrawn
 424     public void fireRangeChanged() {
 425         int range[] = rm.getSelectedRange();
 426         fp.setTextToDraw( fp.RANGE_TEXT, range, null, null );
 427         if(canDisplayCheck) {
 428             setupFontList(range[0], range[1]);
 429         }
 430         if ( showFontInfoCBMI.getState() )
 431           fireUpdateFontInfo();
 432     }
 433 
 434     /// Changes the message on the status bar
 435     public void fireChangeStatus( String message, boolean error ) {
 436         /// If this is not ran as an applet, use own status bar,
 437         /// Otherwise, use the appletviewer/browser's status bar
 438         statusBar.setText( message );
 439         if ( error )
 440           fp.showingError = true;
 441         else
 442           fp.showingError = false;
 443     }
 444 
 445     /// Updates the information about the selected font
 446     public void fireUpdateFontInfo() {
 447         if ( showFontInfoCBMI.getState() ) {
 448             String infos[] = fp.getFontInfo();
 449             for ( int i = 0; i < fontInfos.length; i++ )
 450               fontInfos[i].setText( infos[i] );
 451             fontInfoDialog.pack();
 452         }
 453     }
 454 
 455     private void setupFontList(int rangeStart, int rangeEnd) {
 456 
 457         int listCount = fontMenu.getItemCount();
 458         int size = 16;
 459 
 460         try {
 461             size =  Float.valueOf(sizeField.getText()).intValue();
 462         }
 463         catch ( Exception e ) {
 464             System.out.println("Invalid font size in the size textField. Using default value of 16");
 465         }
 466 
 467         int style = fontStyles[styleMenu.getSelectedIndex()];
 468         Font f;


 516                 filePromptDialog.setSelectedFile( new File( initFileName ) );
 517         retVal = filePromptDialog.showDialog( this, str );
 518 
 519         if ( retVal == JFileChooser.APPROVE_OPTION ) {
 520                 File file = filePromptDialog.getSelectedFile();
 521                 String fileName = file.getAbsolutePath();
 522                 if ( fileName != null ) {
 523                         return fileName;
 524                 }
 525         }
 526 
 527         return null;
 528     }
 529 
 530     /// Converts user text into arrays of String, delimited at newline character
 531     /// Also replaces any valid escape sequence with appropriate unicode character
 532     /// Support \\UXXXXXX notation for surrogates
 533     private String[] parseUserText( String orig ) {
 534         int length = orig.length();
 535         StringTokenizer perLine = new StringTokenizer( orig, "\n" );
 536         String textLines[] = new String[ perLine.countTokens() ];
 537         int lineNumber = 0;
 538 
 539         while ( perLine.hasMoreElements() ) {
 540             StringBuffer converted = new StringBuffer();
 541             String oneLine = perLine.nextToken();
 542             int lineLength = oneLine.length();
 543             int prevEscapeEnd = 0;
 544             int nextEscape = -1;
 545             do {
 546                 int nextBMPEscape = oneLine.indexOf( "\\u", prevEscapeEnd );
 547                 int nextSupEscape = oneLine.indexOf( "\\U", prevEscapeEnd );
 548                 nextEscape = (nextBMPEscape < 0)
 549                     ? ((nextSupEscape < 0)
 550                        ? -1
 551                        : nextSupEscape)
 552                     : ((nextSupEscape < 0)
 553                        ? nextBMPEscape
 554                        : Math.min(nextBMPEscape, nextSupEscape));
 555 
 556                 if ( nextEscape != -1 ) {


 566                             converted.append( new String( Character.toChars( Integer.parseInt( hex, 16 ))));
 567                         }
 568                     }
 569                     catch ( Exception e ) {
 570                         int copyLimit = Math.min(lineLength, prevEscapeEnd);
 571                         converted.append( oneLine.substring( nextEscape, copyLimit ));
 572                     }
 573                 }
 574             } while (nextEscape != -1);
 575             if ( prevEscapeEnd < lineLength )
 576               converted.append( oneLine.substring( prevEscapeEnd, lineLength ));
 577             textLines[ lineNumber++ ] = converted.toString();
 578         }
 579         return textLines;
 580     }
 581 
 582     /// Reads the text from specified file, detecting UTF-16 encoding
 583     /// Then breaks the text into String array, delimited at every line break
 584     private void readTextFile( String fileName ) {
 585         try {
 586             String fileText, textLines[];
 587             BufferedInputStream bis =
 588               new BufferedInputStream( new FileInputStream( fileName ));
 589             int numBytes = bis.available();
 590             if (numBytes == 0) {
 591                 throw new Exception("Text file " + fileName + " is empty");
 592             }
 593             byte byteData[] = new byte[ numBytes ];
 594             bis.read( byteData, 0, numBytes );
 595             bis.close();
 596 
 597             /// If byte mark is found, then use UTF-16 encoding to convert bytes...
 598             if (numBytes >= 2 &&
 599                 (( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) ||
 600                  ( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF )))
 601               fileText = new String( byteData, "UTF-16" );
 602             /// Otherwise, use system default encoding
 603             else
 604               fileText = new String( byteData );
 605 
 606             int length = fileText.length();
 607             StringTokenizer perLine = new StringTokenizer( fileText, "\n" );
 608             /// Determine "Return Char" used in this file
 609             /// This simply finds first occurrence of CR, CR+LF or LF...
 610             for ( int i = 0; i < length; i++ ) {
 611                 char iTh = fileText.charAt( i );
 612                 if ( iTh == '\r' ) {
 613                     if ( i < length - 1 && fileText.charAt( i + 1 ) == '\n' )


 631                   oneLine = " ";
 632                 textLines[ lineNumber++ ] = oneLine;
 633             }
 634             fp.setTextToDraw( fp.FILE_TEXT, null, null, textLines );
 635             rm.setEnabled( false );
 636             methodsMenu.setEnabled( false );
 637         }
 638         catch ( Exception ex ) {
 639             fireChangeStatus( "ERROR: Failed to Read Text File; See Stack Trace", true );
 640             ex.printStackTrace();
 641         }
 642     }
 643 
 644     /// Returns a String storing current configuration
 645     private void writeCurrentOptions( String fileName ) {
 646         try {
 647             String curOptions = fp.getCurrentOptions();
 648             BufferedOutputStream bos =
 649               new BufferedOutputStream( new FileOutputStream( fileName ));
 650             /// Prepend title and the option that is only obtainable here
 651             int range[] = rm.getSelectedRange();
 652             String completeOptions =
 653               ( "Font2DTest Option File\n" +
 654                 displayGridCBMI.getState() + "\n" +
 655                 force16ColsCBMI.getState() + "\n" +
 656                 showFontInfoCBMI.getState() + "\n" +
 657                 rm.getSelectedItem() + "\n" +
 658                 range[0] + "\n" + range[1] + "\n" + curOptions + tFileName);
 659             byte toBeWritten[] = completeOptions.getBytes( "UTF-16" );
 660             bos.write( toBeWritten, 0, toBeWritten.length );
 661             bos.close();
 662         }
 663         catch ( Exception ex ) {
 664             fireChangeStatus( "ERROR: Failed to Save Options File; See Stack Trace", true );
 665             ex.printStackTrace();
 666         }
 667     }
 668 
 669     /// Updates GUI visibility/status after some parameters have changed
 670     private void updateGUI() {
 671         int selectedText = textMenu.getSelectedIndex();
 672 
 673         /// Set the visibility of User Text dialog
 674         if ( selectedText == fp.USER_TEXT )
 675           userTextDialog.show();
 676         else
 677           userTextDialog.hide();
 678         /// Change the visibility/status/availability of Print JDialog buttons
 679         printModeCBs[ fp.ONE_PAGE ].setSelected( true );


 697             printModeCBs[ fp.CUR_RANGE ].setEnabled( true );
 698             printModeCBs[ fp.ALL_TEXT ].setEnabled( false );
 699         }
 700         /// Modify RangeMenu and fontInfo label availabilty
 701         if ( selectedText == fp.RANGE_TEXT ) {
 702             fontInfos[1].setVisible( true );
 703             rm.setEnabled( true );
 704         }
 705         else {
 706             fontInfos[1].setVisible( false );
 707             rm.setEnabled( false );
 708         }
 709     }
 710 
 711     /// Loads saved options and applies them
 712     private void loadOptions( String fileName ) {
 713         try {
 714             BufferedInputStream bis =
 715               new BufferedInputStream( new FileInputStream( fileName ));
 716             int numBytes = bis.available();
 717             byte byteData[] = new byte[ numBytes ];
 718             bis.read( byteData, 0, numBytes );
 719             bis.close();
 720             if ( numBytes < 2 ||
 721                 (byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) )
 722               throw new Exception( "Not a Font2DTest options file" );
 723 
 724             String options = new String( byteData, "UTF-16" );
 725             StringTokenizer perLine = new StringTokenizer( options, "\n" );
 726             String title = perLine.nextToken();
 727             if ( !title.equals( "Font2DTest Option File" ))
 728               throw new Exception( "Not a Font2DTest options file" );
 729 
 730             /// Parse all options
 731             boolean displayGridOpt = Boolean.parseBoolean( perLine.nextToken() );
 732             boolean force16ColsOpt = Boolean.parseBoolean( perLine.nextToken() );
 733             boolean showFontInfoOpt = Boolean.parseBoolean( perLine.nextToken() );
 734             String rangeNameOpt = perLine.nextToken();
 735             int rangeStartOpt = Integer.parseInt( perLine.nextToken() );
 736             int rangeEndOpt = Integer.parseInt( perLine.nextToken() );
 737             String fontNameOpt = perLine.nextToken();
 738             float fontSizeOpt = Float.parseFloat( perLine.nextToken() );
 739             int fontStyleOpt = Integer.parseInt( perLine.nextToken() );
 740             int fontTransformOpt = Integer.parseInt( perLine.nextToken() );
 741             int g2TransformOpt = Integer.parseInt( perLine.nextToken() );
 742             int textToUseOpt = Integer.parseInt( perLine.nextToken() );
 743             int drawMethodOpt = Integer.parseInt( perLine.nextToken() );
 744             int antialiasOpt = Integer.parseInt(perLine.nextToken());
 745             int fractionalOpt = Integer.parseInt(perLine.nextToken());
 746             int lcdContrast = Integer.parseInt(perLine.nextToken());
 747             String userTextOpt[] = { "Font2DTest!" };
 748             String dialogEntry = "Font2DTest!";
 749             if (textToUseOpt == fp.USER_TEXT )  {
 750                 int numLines = perLine.countTokens(), lineNumber = 0;
 751                 if ( numLines != 0 ) {
 752                     userTextOpt = new String[ numLines ];
 753                     dialogEntry = "";
 754                     for ( ; perLine.hasMoreElements(); lineNumber++ ) {
 755                         userTextOpt[ lineNumber ] = perLine.nextToken();
 756                         dialogEntry += userTextOpt[ lineNumber ] + "\n";
 757                     }
 758                 }
 759             }
 760 
 761             /// Reset GUIs
 762             displayGridCBMI.setState( displayGridOpt );
 763             force16ColsCBMI.setState( force16ColsOpt );
 764             showFontInfoCBMI.setState( showFontInfoOpt );
 765             rm.setSelectedRange( rangeNameOpt, rangeStartOpt, rangeEndOpt );
 766             fontMenu.setSelectedItem( fontNameOpt );
 767             sizeField.setText( String.valueOf( fontSizeOpt ));


 999                 }
1000                 else
1001                   fontInfoDialog.hide();
1002             }
1003         }
1004     }
1005 
1006     private static void printUsage() {
1007         String usage = "Usage: java -jar Font2DTest.jar [options]\n" +
1008             "\nwhere options include:\n" +
1009             "    -dcdc | -disablecandisplaycheck disable canDisplay check for font\n" +
1010             "    -?    | -help                   print this help message\n" +
1011             "\nExample :\n" +
1012             "     To disable canDisplay check on font for ranges\n" +
1013             "     java -jar Font2DTest.jar -dcdc";
1014         System.out.println(usage);
1015         System.exit(0);
1016     }
1017 
1018     /// Main function
1019     public static void main(String argv[]) {
1020 
1021         if(argv.length > 0) {
1022             if(argv[0].equalsIgnoreCase("-disablecandisplaycheck") ||
1023                argv[0].equalsIgnoreCase("-dcdc")) {
1024                 canDisplayCheck = false;
1025             }
1026             else {
1027                 printUsage();
1028             }
1029         }
1030 
1031         UIManager.put("swing.boldMetal", Boolean.FALSE);
1032         final JFrame f = new JFrame( "Font2DTest" );
1033         final Font2DTest f2dt = new Font2DTest( f, false );
1034         f.addWindowListener( new WindowAdapter() {
1035             public void windowOpening( WindowEvent e ) { f2dt.repaint(); }
1036             public void windowClosing( WindowEvent e ) { System.exit(0); }
1037         });
1038 
1039         f.getContentPane().add( f2dt );


   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


  99     private final ChoiceV2 textMenu;
 100     private int currentTextChoice = 0;
 101     private final ChoiceV2 transformMenu;
 102     private final ChoiceV2 transformMenuG2;
 103     private final ChoiceV2 methodsMenu;
 104     private final JComboBox antiAliasMenu;
 105     private final JComboBox fracMetricsMenu;
 106 
 107     private final JSlider contrastSlider;
 108 
 109     /// CheckboxMenuItems
 110     private CheckboxMenuItemV2 displayGridCBMI;
 111     private CheckboxMenuItemV2 force16ColsCBMI;
 112     private CheckboxMenuItemV2 showFontInfoCBMI;
 113 
 114     /// JDialog boxes
 115     private JDialog userTextDialog;
 116     private JTextArea userTextArea;
 117     private JDialog printDialog;
 118     private JDialog fontInfoDialog;
 119     private LabelV2[] fontInfos = new LabelV2[2];
 120     private JFileChooser filePromptDialog = null;
 121 
 122     private ButtonGroup printCBGroup;
 123     private JRadioButton[] printModeCBs = new JRadioButton[3];
 124 
 125     /// Status bar
 126     private final LabelV2 statusBar;
 127 
 128     private int[] fontStyles  = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC};
 129 
 130     /// Text filename
 131     private String tFileName;
 132 
 133     // Enabled or disabled status of canDisplay check
 134     private static boolean canDisplayCheck = true;
 135 
 136     /// Initialize GUI variables and its layouts
 137     public Font2DTest( JFrame f, boolean isApplet ) {
 138         parent = f;
 139 
 140         rm = new RangeMenu( this, parent );
 141         fp = new FontPanel( this, parent );
 142         statusBar = new LabelV2("");
 143 
 144         fontMenu = new ChoiceV2( this, canDisplayCheck );
 145         sizeField = new JTextField( "12", 3 );
 146         sizeField.addActionListener( this );
 147         styleMenu = new ChoiceV2( this );
 148         textMenu = new ChoiceV2( ); // listener added later


 272         if ( !isApplet )
 273           fileMenu.add( new MenuItemV2( "Exit", this ));
 274         else
 275           fileMenu.add( new MenuItemV2( "Close", this ));
 276 
 277         displayGridCBMI = new CheckboxMenuItemV2( "Display Grid", true, this );
 278         force16ColsCBMI = new CheckboxMenuItemV2( "Force 16 Columns", false, this );
 279         showFontInfoCBMI = new CheckboxMenuItemV2( "Display Font Info", false, this );
 280         optionMenu.add( displayGridCBMI );
 281         optionMenu.add( force16ColsCBMI );
 282         optionMenu.add( showFontInfoCBMI );
 283 
 284         JMenuBar mb = parent.getJMenuBar();
 285         if ( mb == null )
 286           mb = new JMenuBar();
 287         mb.add( fileMenu );
 288         mb.add( optionMenu );
 289 
 290         parent.setJMenuBar( mb );
 291 
 292         String[] fontList =
 293           GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
 294 
 295         for ( int i = 0; i < fontList.length; i++ )
 296           fontMenu.addItem( fontList[i] );
 297         fontMenu.setSelectedItem( "Dialog" );
 298 
 299         styleMenu.addItem( "Plain" );
 300         styleMenu.addItem( "Bold" );
 301         styleMenu.addItem( "Italic" );
 302         styleMenu.addItem( "Bold Italic" );
 303 
 304         transformMenu.addItem( "None" );
 305         transformMenu.addItem( "Scale" );
 306         transformMenu.addItem( "Shear" );
 307         transformMenu.addItem( "Rotate" );
 308 
 309         transformMenuG2.addItem( "None" );
 310         transformMenuG2.addItem( "Scale" );
 311         transformMenuG2.addItem( "Shear" );
 312         transformMenuG2.addItem( "Rotate" );


 405                 fontInfoDialog.hide();
 406                 showFontInfoCBMI.setState( false );
 407             }
 408         });
 409         JPanel fontInfoPanel = new JPanel();
 410         fontInfoPanel.setLayout( new GridLayout( fontInfos.length, 1 ));
 411         for ( int i = 0; i < fontInfos.length; i++ ) {
 412             fontInfos[i] = new LabelV2("");
 413             fontInfoPanel.add( fontInfos[i] );
 414         }
 415         fontInfoDialog.getContentPane().add( fontInfoPanel );
 416 
 417         /// Move the location of the dialog...
 418         userTextDialog.setLocation( 200, 300 );
 419         fontInfoDialog.setLocation( 0, 400 );
 420     }
 421 
 422     /// RangeMenu object signals using this function
 423     /// when Unicode range has been changed and text needs to be redrawn
 424     public void fireRangeChanged() {
 425         int[] range = rm.getSelectedRange();
 426         fp.setTextToDraw( fp.RANGE_TEXT, range, null, null );
 427         if(canDisplayCheck) {
 428             setupFontList(range[0], range[1]);
 429         }
 430         if ( showFontInfoCBMI.getState() )
 431           fireUpdateFontInfo();
 432     }
 433 
 434     /// Changes the message on the status bar
 435     public void fireChangeStatus( String message, boolean error ) {
 436         /// If this is not ran as an applet, use own status bar,
 437         /// Otherwise, use the appletviewer/browser's status bar
 438         statusBar.setText( message );
 439         if ( error )
 440           fp.showingError = true;
 441         else
 442           fp.showingError = false;
 443     }
 444 
 445     /// Updates the information about the selected font
 446     public void fireUpdateFontInfo() {
 447         if ( showFontInfoCBMI.getState() ) {
 448             String[] infos = fp.getFontInfo();
 449             for ( int i = 0; i < fontInfos.length; i++ )
 450               fontInfos[i].setText( infos[i] );
 451             fontInfoDialog.pack();
 452         }
 453     }
 454 
 455     private void setupFontList(int rangeStart, int rangeEnd) {
 456 
 457         int listCount = fontMenu.getItemCount();
 458         int size = 16;
 459 
 460         try {
 461             size =  Float.valueOf(sizeField.getText()).intValue();
 462         }
 463         catch ( Exception e ) {
 464             System.out.println("Invalid font size in the size textField. Using default value of 16");
 465         }
 466 
 467         int style = fontStyles[styleMenu.getSelectedIndex()];
 468         Font f;


 516                 filePromptDialog.setSelectedFile( new File( initFileName ) );
 517         retVal = filePromptDialog.showDialog( this, str );
 518 
 519         if ( retVal == JFileChooser.APPROVE_OPTION ) {
 520                 File file = filePromptDialog.getSelectedFile();
 521                 String fileName = file.getAbsolutePath();
 522                 if ( fileName != null ) {
 523                         return fileName;
 524                 }
 525         }
 526 
 527         return null;
 528     }
 529 
 530     /// Converts user text into arrays of String, delimited at newline character
 531     /// Also replaces any valid escape sequence with appropriate unicode character
 532     /// Support \\UXXXXXX notation for surrogates
 533     private String[] parseUserText( String orig ) {
 534         int length = orig.length();
 535         StringTokenizer perLine = new StringTokenizer( orig, "\n" );
 536         String[] textLines = new String[ perLine.countTokens() ];
 537         int lineNumber = 0;
 538 
 539         while ( perLine.hasMoreElements() ) {
 540             StringBuffer converted = new StringBuffer();
 541             String oneLine = perLine.nextToken();
 542             int lineLength = oneLine.length();
 543             int prevEscapeEnd = 0;
 544             int nextEscape = -1;
 545             do {
 546                 int nextBMPEscape = oneLine.indexOf( "\\u", prevEscapeEnd );
 547                 int nextSupEscape = oneLine.indexOf( "\\U", prevEscapeEnd );
 548                 nextEscape = (nextBMPEscape < 0)
 549                     ? ((nextSupEscape < 0)
 550                        ? -1
 551                        : nextSupEscape)
 552                     : ((nextSupEscape < 0)
 553                        ? nextBMPEscape
 554                        : Math.min(nextBMPEscape, nextSupEscape));
 555 
 556                 if ( nextEscape != -1 ) {


 566                             converted.append( new String( Character.toChars( Integer.parseInt( hex, 16 ))));
 567                         }
 568                     }
 569                     catch ( Exception e ) {
 570                         int copyLimit = Math.min(lineLength, prevEscapeEnd);
 571                         converted.append( oneLine.substring( nextEscape, copyLimit ));
 572                     }
 573                 }
 574             } while (nextEscape != -1);
 575             if ( prevEscapeEnd < lineLength )
 576               converted.append( oneLine.substring( prevEscapeEnd, lineLength ));
 577             textLines[ lineNumber++ ] = converted.toString();
 578         }
 579         return textLines;
 580     }
 581 
 582     /// Reads the text from specified file, detecting UTF-16 encoding
 583     /// Then breaks the text into String array, delimited at every line break
 584     private void readTextFile( String fileName ) {
 585         try {
 586             String fileText;String[] textLines;
 587             BufferedInputStream bis =
 588               new BufferedInputStream( new FileInputStream( fileName ));
 589             int numBytes = bis.available();
 590             if (numBytes == 0) {
 591                 throw new Exception("Text file " + fileName + " is empty");
 592             }
 593             byte[] byteData = new byte[ numBytes ];
 594             bis.read( byteData, 0, numBytes );
 595             bis.close();
 596 
 597             /// If byte mark is found, then use UTF-16 encoding to convert bytes...
 598             if (numBytes >= 2 &&
 599                 (( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) ||
 600                  ( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF )))
 601               fileText = new String( byteData, "UTF-16" );
 602             /// Otherwise, use system default encoding
 603             else
 604               fileText = new String( byteData );
 605 
 606             int length = fileText.length();
 607             StringTokenizer perLine = new StringTokenizer( fileText, "\n" );
 608             /// Determine "Return Char" used in this file
 609             /// This simply finds first occurrence of CR, CR+LF or LF...
 610             for ( int i = 0; i < length; i++ ) {
 611                 char iTh = fileText.charAt( i );
 612                 if ( iTh == '\r' ) {
 613                     if ( i < length - 1 && fileText.charAt( i + 1 ) == '\n' )


 631                   oneLine = " ";
 632                 textLines[ lineNumber++ ] = oneLine;
 633             }
 634             fp.setTextToDraw( fp.FILE_TEXT, null, null, textLines );
 635             rm.setEnabled( false );
 636             methodsMenu.setEnabled( false );
 637         }
 638         catch ( Exception ex ) {
 639             fireChangeStatus( "ERROR: Failed to Read Text File; See Stack Trace", true );
 640             ex.printStackTrace();
 641         }
 642     }
 643 
 644     /// Returns a String storing current configuration
 645     private void writeCurrentOptions( String fileName ) {
 646         try {
 647             String curOptions = fp.getCurrentOptions();
 648             BufferedOutputStream bos =
 649               new BufferedOutputStream( new FileOutputStream( fileName ));
 650             /// Prepend title and the option that is only obtainable here
 651             int[] range = rm.getSelectedRange();
 652             String completeOptions =
 653               ( "Font2DTest Option File\n" +
 654                 displayGridCBMI.getState() + "\n" +
 655                 force16ColsCBMI.getState() + "\n" +
 656                 showFontInfoCBMI.getState() + "\n" +
 657                 rm.getSelectedItem() + "\n" +
 658                 range[0] + "\n" + range[1] + "\n" + curOptions + tFileName);
 659             byte[] toBeWritten = completeOptions.getBytes( "UTF-16" );
 660             bos.write( toBeWritten, 0, toBeWritten.length );
 661             bos.close();
 662         }
 663         catch ( Exception ex ) {
 664             fireChangeStatus( "ERROR: Failed to Save Options File; See Stack Trace", true );
 665             ex.printStackTrace();
 666         }
 667     }
 668 
 669     /// Updates GUI visibility/status after some parameters have changed
 670     private void updateGUI() {
 671         int selectedText = textMenu.getSelectedIndex();
 672 
 673         /// Set the visibility of User Text dialog
 674         if ( selectedText == fp.USER_TEXT )
 675           userTextDialog.show();
 676         else
 677           userTextDialog.hide();
 678         /// Change the visibility/status/availability of Print JDialog buttons
 679         printModeCBs[ fp.ONE_PAGE ].setSelected( true );


 697             printModeCBs[ fp.CUR_RANGE ].setEnabled( true );
 698             printModeCBs[ fp.ALL_TEXT ].setEnabled( false );
 699         }
 700         /// Modify RangeMenu and fontInfo label availabilty
 701         if ( selectedText == fp.RANGE_TEXT ) {
 702             fontInfos[1].setVisible( true );
 703             rm.setEnabled( true );
 704         }
 705         else {
 706             fontInfos[1].setVisible( false );
 707             rm.setEnabled( false );
 708         }
 709     }
 710 
 711     /// Loads saved options and applies them
 712     private void loadOptions( String fileName ) {
 713         try {
 714             BufferedInputStream bis =
 715               new BufferedInputStream( new FileInputStream( fileName ));
 716             int numBytes = bis.available();
 717             byte[] byteData = new byte[ numBytes ];
 718             bis.read( byteData, 0, numBytes );
 719             bis.close();
 720             if ( numBytes < 2 ||
 721                 (byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) )
 722               throw new Exception( "Not a Font2DTest options file" );
 723 
 724             String options = new String( byteData, "UTF-16" );
 725             StringTokenizer perLine = new StringTokenizer( options, "\n" );
 726             String title = perLine.nextToken();
 727             if ( !title.equals( "Font2DTest Option File" ))
 728               throw new Exception( "Not a Font2DTest options file" );
 729 
 730             /// Parse all options
 731             boolean displayGridOpt = Boolean.parseBoolean( perLine.nextToken() );
 732             boolean force16ColsOpt = Boolean.parseBoolean( perLine.nextToken() );
 733             boolean showFontInfoOpt = Boolean.parseBoolean( perLine.nextToken() );
 734             String rangeNameOpt = perLine.nextToken();
 735             int rangeStartOpt = Integer.parseInt( perLine.nextToken() );
 736             int rangeEndOpt = Integer.parseInt( perLine.nextToken() );
 737             String fontNameOpt = perLine.nextToken();
 738             float fontSizeOpt = Float.parseFloat( perLine.nextToken() );
 739             int fontStyleOpt = Integer.parseInt( perLine.nextToken() );
 740             int fontTransformOpt = Integer.parseInt( perLine.nextToken() );
 741             int g2TransformOpt = Integer.parseInt( perLine.nextToken() );
 742             int textToUseOpt = Integer.parseInt( perLine.nextToken() );
 743             int drawMethodOpt = Integer.parseInt( perLine.nextToken() );
 744             int antialiasOpt = Integer.parseInt(perLine.nextToken());
 745             int fractionalOpt = Integer.parseInt(perLine.nextToken());
 746             int lcdContrast = Integer.parseInt(perLine.nextToken());
 747             String[] userTextOpt = { "Font2DTest!" };
 748             String dialogEntry = "Font2DTest!";
 749             if (textToUseOpt == fp.USER_TEXT )  {
 750                 int numLines = perLine.countTokens(), lineNumber = 0;
 751                 if ( numLines != 0 ) {
 752                     userTextOpt = new String[ numLines ];
 753                     dialogEntry = "";
 754                     for ( ; perLine.hasMoreElements(); lineNumber++ ) {
 755                         userTextOpt[ lineNumber ] = perLine.nextToken();
 756                         dialogEntry += userTextOpt[ lineNumber ] + "\n";
 757                     }
 758                 }
 759             }
 760 
 761             /// Reset GUIs
 762             displayGridCBMI.setState( displayGridOpt );
 763             force16ColsCBMI.setState( force16ColsOpt );
 764             showFontInfoCBMI.setState( showFontInfoOpt );
 765             rm.setSelectedRange( rangeNameOpt, rangeStartOpt, rangeEndOpt );
 766             fontMenu.setSelectedItem( fontNameOpt );
 767             sizeField.setText( String.valueOf( fontSizeOpt ));


 999                 }
1000                 else
1001                   fontInfoDialog.hide();
1002             }
1003         }
1004     }
1005 
1006     private static void printUsage() {
1007         String usage = "Usage: java -jar Font2DTest.jar [options]\n" +
1008             "\nwhere options include:\n" +
1009             "    -dcdc | -disablecandisplaycheck disable canDisplay check for font\n" +
1010             "    -?    | -help                   print this help message\n" +
1011             "\nExample :\n" +
1012             "     To disable canDisplay check on font for ranges\n" +
1013             "     java -jar Font2DTest.jar -dcdc";
1014         System.out.println(usage);
1015         System.exit(0);
1016     }
1017 
1018     /// Main function
1019     public static void main(String[] argv) {
1020 
1021         if(argv.length > 0) {
1022             if(argv[0].equalsIgnoreCase("-disablecandisplaycheck") ||
1023                argv[0].equalsIgnoreCase("-dcdc")) {
1024                 canDisplayCheck = false;
1025             }
1026             else {
1027                 printUsage();
1028             }
1029         }
1030 
1031         UIManager.put("swing.boldMetal", Boolean.FALSE);
1032         final JFrame f = new JFrame( "Font2DTest" );
1033         final Font2DTest f2dt = new Font2DTest( f, false );
1034         f.addWindowListener( new WindowAdapter() {
1035             public void windowOpening( WindowEvent e ) { f2dt.repaint(); }
1036             public void windowClosing( WindowEvent e ) { System.exit(0); }
1037         });
1038 
1039         f.getContentPane().add( f2dt );


< prev index next >