1 36 37 40 41 import java.awt.Component ; 42 import java.awt.BorderLayout ; 43 import java.awt.CheckboxGroup ; 44 import java.awt.Container ; 45 import java.awt.Dimension ; 46 import java.awt.Font ; 47 import java.awt.Graphics ; 48 import java.awt.GraphicsEnvironment ; 49 import java.awt.GridBagConstraints ; 50 import java.awt.GridBagLayout ; 51 import java.awt.GridLayout ; 52 import java.awt.Insets ; 53 import java.awt.Toolkit ; 54 import java.awt.event.ActionEvent ; 55 import java.awt.event.ActionListener ; 56 import java.awt.event.ItemEvent ; 57 import java.awt.event.ItemListener ; 58 import java.awt.event.WindowAdapter ; 59 import java.awt.event.WindowEvent ; 60 import java.awt.image.BufferedImage ; 61 import java.io.BufferedInputStream ; 62 import java.io.BufferedOutputStream ; 63 import java.io.File ; 64 import java.io.FileInputStream ; 65 import java.io.FileOutputStream ; 66 import java.util.EnumSet ; 67 import java.util.StringTokenizer ; 68 import java.util.BitSet ; 69 import javax.swing.*; 70 import javax.swing.event.*; 71 72 79 80 82 public final class Font2DTest extends JPanel 83 implements ActionListener , ItemListener , ChangeListener { 84 85 private final JFrame parent; 87 private final FontPanel fp; 89 private final RangeMenu rm; 91 92 private final ChoiceV2 fontMenu; 94 private final JTextField sizeField; 95 private final ChoiceV2 styleMenu; 96 private final ChoiceV2 textMenu; 97 private int currentTextChoice = 0; 98 private final ChoiceV2 transformMenu; 99 private final ChoiceV2 transformMenuG2; 100 private final ChoiceV2 methodsMenu; 101 private final JComboBox antiAliasMenu; 102 private final JComboBox fracMetricsMenu; 103 104 private final JSlider contrastSlider; 105 106 private CheckboxMenuItemV2 displayGridCBMI; 108 private CheckboxMenuItemV2 force16ColsCBMI; 109 private CheckboxMenuItemV2 showFontInfoCBMI; 110 111 private JDialog userTextDialog; 113 private JTextArea userTextArea; 114 private JDialog printDialog; 115 private JDialog fontInfoDialog; 116 private LabelV2 fontInfos[] = new LabelV2[2]; 117 private JFileChooser filePromptDialog = null; 118 119 private ButtonGroup printCBGroup; 120 private JRadioButton printModeCBs[] = new JRadioButton[3]; 121 122 private final LabelV2 statusBar; 124 125 private int fontStyles [] = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC}; 126 127 private String tFileName; 129 130 private static boolean canDisplayCheck = true; 132 133 public Font2DTest( JFrame f, boolean isApplet ) { 135 parent = f; 136 137 rm = new RangeMenu( this, parent ); 138 fp = new FontPanel( this, parent ); 139 statusBar = new LabelV2(""); 140 141 fontMenu = new ChoiceV2( this, canDisplayCheck ); 142 sizeField = new JTextField( "12", 3 ); 143 sizeField.addActionListener( this ); 144 styleMenu = new ChoiceV2( this ); 145 textMenu = new ChoiceV2( ); transformMenu = new ChoiceV2( this ); 147 transformMenuG2 = new ChoiceV2( this ); 148 methodsMenu = new ChoiceV2( this ); 149 150 antiAliasMenu = 151 new JComboBox(EnumSet.allOf(FontPanel.AAValues.class).toArray()); 152 antiAliasMenu.addActionListener(this); 153 fracMetricsMenu = 154 new JComboBox(EnumSet.allOf(FontPanel.FMValues.class).toArray()); 155 fracMetricsMenu.addActionListener(this); 156 157 contrastSlider = new JSlider(JSlider.HORIZONTAL, 100, 250, 158 FontPanel.getDefaultLCDContrast().intValue()); 159 contrastSlider.setEnabled(false); 160 contrastSlider.setMajorTickSpacing(20); 161 contrastSlider.setMinorTickSpacing(10); 162 contrastSlider.setPaintTicks(true); 163 contrastSlider.setPaintLabels(true); 164 contrastSlider.addChangeListener(this); 165 setupPanel(); 166 setupMenu( isApplet ); 167 setupDialog( isApplet ); 168 169 if(canDisplayCheck) { 170 fireRangeChanged(); 171 } 172 } 173 174 private void setupPanel() { 176 GridBagLayout gbl = new GridBagLayout (); 177 GridBagConstraints gbc = new GridBagConstraints (); 178 gbc.fill = GridBagConstraints.HORIZONTAL; 179 gbc.weightx = 1; 180 gbc.insets = new Insets ( 2, 0, 2, 2 ); 181 this.setLayout( gbl ); 182 183 addLabeledComponentToGBL( "Font: ", fontMenu, gbl, gbc, this ); 184 addLabeledComponentToGBL( "Size: ", sizeField, gbl, gbc, this ); 185 gbc.gridwidth = GridBagConstraints.REMAINDER; 186 addLabeledComponentToGBL( "Font Transform:", 187 transformMenu, gbl, gbc, this ); 188 gbc.gridwidth = 1; 189 190 addLabeledComponentToGBL( "Range: ", rm, gbl, gbc, this ); 191 addLabeledComponentToGBL( "Style: ", styleMenu, gbl, gbc, this ); 192 gbc.gridwidth = GridBagConstraints.REMAINDER; 193 addLabeledComponentToGBL( "Graphics Transform: ", 194 transformMenuG2, gbl, gbc, this ); 195 gbc.gridwidth = 1; 196 197 gbc.anchor = GridBagConstraints.WEST; 198 addLabeledComponentToGBL( "Method: ", methodsMenu, gbl, gbc, this ); 199 addLabeledComponentToGBL("", null, gbl, gbc, this); 200 gbc.anchor = GridBagConstraints.EAST; 201 gbc.gridwidth = GridBagConstraints.REMAINDER; 202 addLabeledComponentToGBL( "Text to use:", textMenu, gbl, gbc, this ); 203 204 gbc.weightx=1; 205 gbc.gridwidth = 1; 206 gbc.fill = GridBagConstraints.HORIZONTAL; 207 gbc.anchor = GridBagConstraints.WEST; 208 addLabeledComponentToGBL("LCD contrast: ", 209 contrastSlider, gbl, gbc, this); 210 211 gbc.gridwidth = 1; 212 gbc.fill = GridBagConstraints.NONE; 213 addLabeledComponentToGBL("Antialiasing: ", 214 antiAliasMenu, gbl, gbc, this); 215 216 gbc.anchor = GridBagConstraints.EAST; 217 gbc.gridwidth = GridBagConstraints.REMAINDER; 218 addLabeledComponentToGBL("Fractional metrics: ", 219 fracMetricsMenu, gbl, gbc, this); 220 221 gbc.weightx = 1; 222 gbc.weighty = 1; 223 gbc.anchor = GridBagConstraints.WEST; 224 gbc.insets = new Insets ( 2, 0, 0, 2 ); 225 gbc.fill = GridBagConstraints.BOTH; 226 gbl.setConstraints( fp, gbc ); 227 this.add( fp ); 228 229 gbc.weighty = 0; 230 gbc.insets = new Insets ( 0, 2, 0, 0 ); 231 gbl.setConstraints( statusBar, gbc ); 232 this.add( statusBar ); 233 } 234 235 private void addLabeledComponentToGBL( String name, 237 JComponent c, 238 GridBagLayout gbl, 239 GridBagConstraints gbc, 240 Container target ) { 241 LabelV2 l = new LabelV2( name ); 242 GridBagConstraints gbcLabel = (GridBagConstraints ) gbc.clone(); 243 gbcLabel.insets = new Insets ( 2, 2, 2, 0 ); 244 gbcLabel.gridwidth = 1; 245 gbcLabel.weightx = 0; 246 247 if ( c == null ) 248 c = new JLabel( "" ); 249 250 gbl.setConstraints( l, gbcLabel ); 251 target.add( l ); 252 gbl.setConstraints( c, gbc ); 253 target.add( c ); 254 } 255 256 private void setupMenu( boolean isApplet ) { 258 JMenu fileMenu = new JMenu( "File" ); 259 JMenu optionMenu = new JMenu( "Option" ); 260 261 fileMenu.add( new MenuItemV2( "Save Selected Options...", this )); 262 fileMenu.add( new MenuItemV2( "Load Options...", this )); 263 fileMenu.addSeparator(); 264 fileMenu.add( new MenuItemV2( "Save as PNG...", this )); 265 fileMenu.add( new MenuItemV2( "Load PNG File to Compare...", this )); 266 fileMenu.add( new MenuItemV2( "Page Setup...", this )); 267 fileMenu.add( new MenuItemV2( "Print...", this )); 268 fileMenu.addSeparator(); 269 if ( !isApplet ) 270 fileMenu.add( new MenuItemV2( "Exit", this )); 271 else 272 fileMenu.add( new MenuItemV2( "Close", this )); 273 274 displayGridCBMI = new CheckboxMenuItemV2( "Display Grid", true, this ); 275 force16ColsCBMI = new CheckboxMenuItemV2( "Force 16 Columns", false, this ); 276 showFontInfoCBMI = new CheckboxMenuItemV2( "Display Font Info", false, this ); 277 optionMenu.add( displayGridCBMI ); 278 optionMenu.add( force16ColsCBMI ); 279 optionMenu.add( showFontInfoCBMI ); 280 281 JMenuBar mb = parent.getJMenuBar(); 282 if ( mb == null ) 283 mb = new JMenuBar(); 284 mb.add( fileMenu ); 285 mb.add( optionMenu ); 286 287 parent.setJMenuBar( mb ); 288 289 String fontList[] = 290 GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 291 292 for ( int i = 0; i < fontList.length; i++ ) 293 fontMenu.addItem( fontList[i] ); 294 fontMenu.setSelectedItem( "Dialog" ); 295 296 styleMenu.addItem( "Plain" ); 297 styleMenu.addItem( "Bold" ); 298 styleMenu.addItem( "Italic" ); 299 styleMenu.addItem( "Bold Italic" ); 300 301 transformMenu.addItem( "None" ); 302 transformMenu.addItem( "Scale" ); 303 transformMenu.addItem( "Shear" ); 304 transformMenu.addItem( "Rotate" ); 305 306 transformMenuG2.addItem( "None" ); 307 transformMenuG2.addItem( "Scale" ); 308 transformMenuG2.addItem( "Shear" ); 309 transformMenuG2.addItem( "Rotate" ); 310 311 methodsMenu.addItem( "drawString" ); 312 methodsMenu.addItem( "drawChars" ); 313 methodsMenu.addItem( "drawBytes" ); 314 methodsMenu.addItem( "drawGlyphVector" ); 315 methodsMenu.addItem( "TextLayout.draw" ); 316 methodsMenu.addItem( "GlyphVector.getOutline + draw" ); 317 methodsMenu.addItem( "TextLayout.getOutline + draw" ); 318 319 textMenu.addItem( "Unicode Range" ); 320 textMenu.addItem( "All Glyphs" ); 321 textMenu.addItem( "User Text" ); 322 textMenu.addItem( "Text File" ); 323 textMenu.addActionListener ( this ); } 325 326 private void setupDialog( boolean isApplet ) { 328 if (!isApplet) 329 filePromptDialog = new JFileChooser( ); 330 else 331 filePromptDialog = null; 332 333 userTextDialog = new JDialog( parent, "User Text", false ); 335 JPanel dialogTopPanel = new JPanel(); 336 JPanel dialogBottomPanel = new JPanel(); 337 LabelV2 message1 = new LabelV2( "Enter text below and then press update" ); 338 LabelV2 message2 = new LabelV2( "(Unicode char can be denoted by \\uXXXX)" ); 339 LabelV2 message3 = new LabelV2( "(Supplementary chars can be denoted by \\UXXXXXX)" ); 340 userTextArea = new JTextArea( "Java2D!" ); 341 ButtonV2 bUpdate = new ButtonV2( "Update", this ); 342 userTextArea.setFont( new Font ( "dialog", Font.PLAIN, 12 )); 343 dialogTopPanel.setLayout( new GridLayout ( 3, 1 )); 344 dialogTopPanel.add( message1 ); 345 dialogTopPanel.add( message2 ); 346 dialogTopPanel.add( message3 ); 347 dialogBottomPanel.add( bUpdate ); 348 JScrollPane userTextAreaSP = new JScrollPane(userTextArea); 350 userTextAreaSP.setPreferredSize(new Dimension (300, 100)); 351 352 userTextDialog.getContentPane().setLayout( new BorderLayout () ); 353 userTextDialog.getContentPane().add( "North", dialogTopPanel ); 354 userTextDialog.getContentPane().add( "Center", userTextAreaSP ); 355 userTextDialog.getContentPane().add( "South", dialogBottomPanel ); 356 userTextDialog.pack(); 357 userTextDialog.addWindowListener( new WindowAdapter () { 358 public void windowClosing( WindowEvent e ) { 359 userTextDialog.hide(); 360 } 361 }); 362 363 printCBGroup = new ButtonGroup(); 365 printModeCBs[ fp.ONE_PAGE ] = 366 new JRadioButton( "Print one page from currently displayed character/line", 367 true ); 368 printModeCBs[ fp.CUR_RANGE ] = 369 new JRadioButton( "Print all characters in currently selected range", 370 false ); 371 printModeCBs[ fp.ALL_TEXT ] = 372 new JRadioButton( "Print all lines of text", 373 false ); 374 LabelV2 l = 375 new LabelV2( "Note: Page range in native \"Print\" dialog will not affect the result" ); 376 JPanel buttonPanel = new JPanel(); 377 printModeCBs[ fp.ALL_TEXT ].setEnabled( false ); 378 buttonPanel.add( new ButtonV2( "Print", this )); 379 buttonPanel.add( new ButtonV2( "Cancel", this )); 380 381 printDialog = new JDialog( parent, "Print...", true ); 382 printDialog.setResizable( false ); 383 printDialog.addWindowListener( new WindowAdapter () { 384 public void windowClosing( WindowEvent e ) { 385 printDialog.hide(); 386 } 387 }); 388 printDialog.getContentPane().setLayout( new GridLayout ( printModeCBs.length + 2, 1 )); 389 printDialog.getContentPane().add( l ); 390 for ( int i = 0; i < printModeCBs.length; i++ ) { 391 printCBGroup.add( printModeCBs[i] ); 392 printDialog.getContentPane().add( printModeCBs[i] ); 393 } 394 printDialog.getContentPane().add( buttonPanel ); 395 printDialog.pack(); 396 397 fontInfoDialog = new JDialog( parent, "Font info", false ); 399 fontInfoDialog.setResizable( false ); 400 fontInfoDialog.addWindowListener( new WindowAdapter () { 401 public void windowClosing( WindowEvent e ) { 402 fontInfoDialog.hide(); 403 showFontInfoCBMI.setState( false ); 404 } 405 }); 406 JPanel fontInfoPanel = new JPanel(); 407 fontInfoPanel.setLayout( new GridLayout ( fontInfos.length, 1 )); 408 for ( int i = 0; i < fontInfos.length; i++ ) { 409 fontInfos[i] = new LabelV2(""); 410 fontInfoPanel.add( fontInfos[i] ); 411 } 412 fontInfoDialog.getContentPane().add( fontInfoPanel ); 413 414 userTextDialog.setLocation( 200, 300 ); 416 fontInfoDialog.setLocation( 0, 400 ); 417 } 418 419 public void fireRangeChanged() { 422 int range[] = rm.getSelectedRange(); 423 fp.setTextToDraw( fp.RANGE_TEXT, range, null, null ); 424 if(canDisplayCheck) { 425 setupFontList(range[0], range[1]); 426 } 427 if ( showFontInfoCBMI.getState() ) 428 fireUpdateFontInfo(); 429 } 430 431 public void fireChangeStatus( String message, boolean error ) { 433 statusBar.setText( message ); 436 if ( error ) 437 fp.showingError = true; 438 else 439 fp.showingError = false; 440 } 441 442 public void fireUpdateFontInfo() { 444 if ( showFontInfoCBMI.getState() ) { 445 String infos[] = fp.getFontInfo(); 446 for ( int i = 0; i < fontInfos.length; i++ ) 447 fontInfos[i].setText( infos[i] ); 448 fontInfoDialog.pack(); 449 } 450 } 451 452 private void setupFontList(int rangeStart, int rangeEnd) { 453 454 int listCount = fontMenu.getItemCount(); 455 int size = 16; 456 457 try { 458 size = Float.valueOf(sizeField.getText()).intValue(); 459 } 460 catch ( Exception e ) { 461 System.out.println("Invalid font size in the size textField. Using default value of 16"); 462 } 463 464 int style = fontStyles[styleMenu.getSelectedIndex()]; 465 Font f; 466 for (int i = 0; i < listCount; i++) { 467 String fontName = (String )fontMenu.getItemAt(i); 468 f = new Font (fontName, style, size); 469 if ((rm.getSelectedIndex() != RangeMenu.SURROGATES_AREA_INDEX) && 470 canDisplayRange(f, rangeStart, rangeEnd)) { 471 fontMenu.setBit(i, true); 472 } 473 else { 474 fontMenu.setBit(i, false); 475 } 476 } 477 478 fontMenu.repaint(); 479 } 480 481 protected boolean canDisplayRange(Font font, int rangeStart, int rangeEnd) { 482 for (int i = rangeStart; i < rangeEnd; i++) { 483 if (font.canDisplay(i)) { 484 return true; 485 } 486 } 487 return false; 488 } 489 490 private String promptFile( boolean isSave, String initFileName ) { 492 int retVal; 493 String str; 494 495 if ( filePromptDialog == null) 497 return null; 498 499 if ( isSave ) { 500 filePromptDialog.setDialogType( JFileChooser.SAVE_DIALOG ); 501 filePromptDialog.setDialogTitle( "Save..." ); 502 str = "Save"; 503 504 505 } 506 else { 507 filePromptDialog.setDialogType( JFileChooser.OPEN_DIALOG ); 508 filePromptDialog.setDialogTitle( "Load..." ); 509 str = "Load"; 510 } 511 512 if (initFileName != null) 513 filePromptDialog.setSelectedFile( new File ( initFileName ) ); 514 retVal = filePromptDialog.showDialog( this, str ); 515 516 if ( retVal == JFileChooser.APPROVE_OPTION ) { 517 File file = filePromptDialog.getSelectedFile(); 518 String fileName = file.getAbsolutePath(); 519 if ( fileName != null ) { 520 return fileName; 521 } 522 } 523 524 return null; 525 } 526 527 private String [] parseUserText( String orig ) { 531 int length = orig.length(); 532 StringTokenizer perLine = new StringTokenizer ( orig, "\n" ); 533 String textLines[] = new String [ perLine.countTokens() ]; 534 int lineNumber = 0; 535 536 while ( perLine.hasMoreElements() ) { 537 StringBuffer converted = new StringBuffer (); 538 String oneLine = perLine.nextToken(); 539 int lineLength = oneLine.length(); 540 int prevEscapeEnd = 0; 541 int nextEscape = -1; 542 do { 543 int nextBMPEscape = oneLine.indexOf( "\\u", prevEscapeEnd ); 544 int nextSupEscape = oneLine.indexOf( "\\U", prevEscapeEnd ); 545 nextEscape = (nextBMPEscape < 0) 546 ? ((nextSupEscape < 0) 547 ? -1 548 : nextSupEscape) 549 : ((nextSupEscape < 0) 550 ? nextBMPEscape 551 : Math.min(nextBMPEscape, nextSupEscape)); 552 553 if ( nextEscape != -1 ) { 554 if ( prevEscapeEnd < nextEscape ) 555 converted.append( oneLine.substring( prevEscapeEnd, nextEscape )); 556 557 prevEscapeEnd = nextEscape + (nextEscape == nextBMPEscape ? 6 : 8); 558 try { 559 String hex = oneLine.substring( nextEscape + 2, prevEscapeEnd ); 560 if (nextEscape == nextBMPEscape) { 561 converted.append( (char) Integer.parseInt( hex, 16 )); 562 } else { 563 converted.append( new String ( Character.toChars( Integer.parseInt( hex, 16 )))); 564 } 565 } 566 catch ( Exception e ) { 567 int copyLimit = Math.min(lineLength, prevEscapeEnd); 568 converted.append( oneLine.substring( nextEscape, copyLimit )); 569 } 570 } 571 } while (nextEscape != -1); 572 if ( prevEscapeEnd < lineLength ) 573 converted.append( oneLine.substring( prevEscapeEnd, lineLength )); 574 textLines[ lineNumber++ ] = converted.toString(); 575 } 576 return textLines; 577 } 578 579 private void readTextFile( String fileName ) { 582 try { 583 String fileText, textLines[]; 584 BufferedInputStream bis = 585 new BufferedInputStream ( new FileInputStream ( fileName )); 586 int numBytes = bis.available(); 587 if (numBytes == 0) { 588 throw new Exception ("Text file " + fileName + " is empty"); 589 } 590 byte byteData[] = new byte[ numBytes ]; 591 bis.read( byteData, 0, numBytes ); 592 bis.close(); 593 594 if (numBytes >= 2 && 596 (( byteData[0] == (byte) 0xFF && byteData[1] == (byte) 0xFE ) || 597 ( byteData[0] == (byte) 0xFE && byteData[1] == (byte) 0xFF ))) 598 fileText = new String ( byteData, "UTF-16" ); 599 else 601 fileText = new String ( byteData ); 602 603 int length = fileText.length(); 604 StringTokenizer perLine = new StringTokenizer ( fileText, "\n" ); 605 for ( int i = 0; i < length; i++ ) { 608 char iTh = fileText.charAt( i ); 609 if ( iTh == '\r' ) { 610 if ( i < length - 1 && fileText.charAt( i + 1 ) == '\n' ) 611 perLine = new StringTokenizer ( fileText, "\r\n" ); 612 else 613 perLine = new StringTokenizer ( fileText, "\r" ); 614 break; 615 } 616 else if ( iTh == '\n' ) 617 break; 619 } 620 int lineNumber = 0, numLines = perLine.countTokens(); 621 textLines = new String [ numLines ]; 622 623 while ( perLine.hasMoreElements() ) { 624 String oneLine = perLine.nextToken(); 625 if ( oneLine == null ) 626 oneLine = " "; 629 textLines[ lineNumber++ ] = oneLine; 630 } 631 fp.setTextToDraw( fp.FILE_TEXT, null, null, textLines ); 632 rm.setEnabled( false ); 633 methodsMenu.setEnabled( false ); 634 } 635 catch ( Exception ex ) { 636 fireChangeStatus( "ERROR: Failed to Read Text File; See Stack Trace", true ); 637 ex.printStackTrace(); 638 } 639 } 640 641 private void writeCurrentOptions( String fileName ) { 643 try { 644 String curOptions = fp.getCurrentOptions(); 645 BufferedOutputStream bos = 646 new BufferedOutputStream ( new FileOutputStream ( fileName )); 647 int range[] = rm.getSelectedRange(); 649 String completeOptions = 650 ( "Font2DTest Option File\n" + 651 displayGridCBMI.getState() + "\n" + 652 force16ColsCBMI.getState() + "\n" + 653 showFontInfoCBMI.getState() + "\n" + 654 rm.getSelectedItem() + "\n" + 655 range[0] + "\n" + range[1] + "\n" + curOptions + tFileName); 656 byte toBeWritten[] = completeOptions.getBytes( "UTF-16" ); 657 bos.write( toBeWritten, 0, toBeWritten.length ); 658 bos.close(); 659 } 660 catch ( Exception ex ) { 661 fireChangeStatus( "ERROR: Failed to Save Options File; See Stack Trace", true ); 662 ex.printStackTrace(); 663 } 664 } 665 666 private void updateGUI() { 668 int selectedText = textMenu.getSelectedIndex(); 669 670 if ( selectedText == fp.USER_TEXT ) 672 userTextDialog.show(); 673 else 674 userTextDialog.hide(); 675 printModeCBs[ fp.ONE_PAGE ].setSelected( true ); 677 if ( selectedText == fp.FILE_TEXT || selectedText == fp.USER_TEXT ) { 678 if ( selectedText == fp.FILE_TEXT ) 682 methodsMenu.setSelectedItem("TextLayout.draw"); 683 methodsMenu.setEnabled( selectedText == fp.USER_TEXT ); 684 printModeCBs[ fp.CUR_RANGE ].setEnabled( false ); 685 printModeCBs[ fp.ALL_TEXT ].setEnabled( true ); 686 } 687 else { 688 if ( selectedText == fp.ALL_GLYPHS ) 692 methodsMenu.setSelectedItem("drawGlyphVector"); 693 methodsMenu.setEnabled( selectedText == fp.RANGE_TEXT ); 694 printModeCBs[ fp.CUR_RANGE ].setEnabled( true ); 695 printModeCBs[ fp.ALL_TEXT ].setEnabled( false ); 696 } 697 if ( selectedText == fp.RANGE_TEXT ) { 699 fontInfos[1].setVisible( true ); 700 rm.setEnabled( true ); 701 } 702 else { 703 fontInfos[1].setVisible( false ); 704 rm.setEnabled( false ); 705 } 706 } 707 708 private void loadOptions( String fileName ) { 710 try { 711 BufferedInputStream bis = 712 new BufferedInputStream ( new FileInputStream ( fileName )); 713 int numBytes = bis.available(); 714 byte byteData[] = new byte[ numBytes ]; 715 bis.read( byteData, 0, numBytes ); 716 bis.close(); 717 if ( numBytes < 2 || 718 (byteData[0] != (byte) 0xFE || byteData[1] != (byte) 0xFF) ) 719 throw new Exception ( "Not a Font2DTest options file" ); 720 721 String options = new String ( byteData, "UTF-16" ); 722 StringTokenizer perLine = new StringTokenizer ( options, "\n" ); 723 String title = perLine.nextToken(); 724 if ( !title.equals( "Font2DTest Option File" )) 725 throw new Exception ( "Not a Font2DTest options file" ); 726 727 boolean displayGridOpt = Boolean.parseBoolean( perLine.nextToken() ); 729 boolean force16ColsOpt = Boolean.parseBoolean( perLine.nextToken() ); 730 boolean showFontInfoOpt = Boolean.parseBoolean( perLine.nextToken() ); 731 String rangeNameOpt = perLine.nextToken(); 732 int rangeStartOpt = Integer.parseInt( perLine.nextToken() ); 733 int rangeEndOpt = Integer.parseInt( perLine.nextToken() ); 734 String fontNameOpt = perLine.nextToken(); 735 float fontSizeOpt = Float.parseFloat( perLine.nextToken() ); 736 int fontStyleOpt = Integer.parseInt( perLine.nextToken() ); 737 int fontTransformOpt = Integer.parseInt( perLine.nextToken() ); 738 int g2TransformOpt = Integer.parseInt( perLine.nextToken() ); 739 int textToUseOpt = Integer.parseInt( perLine.nextToken() ); 740 int drawMethodOpt = Integer.parseInt( perLine.nextToken() ); 741 int antialiasOpt = Integer.parseInt(perLine.nextToken()); 742 int fractionalOpt = Integer.parseInt(perLine.nextToken()); 743 int lcdContrast = Integer.parseInt(perLine.nextToken()); 744 String userTextOpt[] = { "Java2D!" }, dialogEntry = "Java2D!"; 745 if (textToUseOpt == fp.USER_TEXT ) { 746 int numLines = perLine.countTokens(), lineNumber = 0; 747 if ( numLines != 0 ) { 748 userTextOpt = new String [ numLines ]; 749 dialogEntry = ""; 750 for ( ; perLine.hasMoreElements(); lineNumber++ ) { 751 userTextOpt[ lineNumber ] = perLine.nextToken(); 752 dialogEntry += userTextOpt[ lineNumber ] + "\n"; 753 } 754 } 755 } 756 757 displayGridCBMI.setState( displayGridOpt ); 759 force16ColsCBMI.setState( force16ColsOpt ); 760 showFontInfoCBMI.setState( showFontInfoOpt ); 761 rm.setSelectedRange( rangeNameOpt, rangeStartOpt, rangeEndOpt ); 762 fontMenu.setSelectedItem( fontNameOpt ); 763 sizeField.setText( String.valueOf( fontSizeOpt )); 764 styleMenu.setSelectedIndex( fontStyleOpt ); 765 transformMenu.setSelectedIndex( fontTransformOpt ); 766 transformMenuG2.setSelectedIndex( g2TransformOpt ); 767 textMenu.setSelectedIndex( textToUseOpt ); 768 methodsMenu.setSelectedIndex( drawMethodOpt ); 769 antiAliasMenu.setSelectedIndex( antialiasOpt ); 770 fracMetricsMenu.setSelectedIndex( fractionalOpt ); 771 contrastSlider.setValue(lcdContrast); 772 773 userTextArea.setText( dialogEntry ); 774 updateGUI(); 775 776 if ( textToUseOpt == fp.FILE_TEXT ) { 777 tFileName = perLine.nextToken(); 778 readTextFile(tFileName ); 779 } 780 781 fp.loadOptions( displayGridOpt, force16ColsOpt, 783 rangeStartOpt, rangeEndOpt, 784 fontNameOpt, fontSizeOpt, 785 fontStyleOpt, fontTransformOpt, g2TransformOpt, 786 textToUseOpt, drawMethodOpt, 787 antialiasOpt, fractionalOpt, 788 lcdContrast, userTextOpt ); 789 if ( showFontInfoOpt ) { 790 fireUpdateFontInfo(); 791 fontInfoDialog.show(); 792 } 793 else 794 fontInfoDialog.hide(); 795 } 796 catch ( Exception ex ) { 797 fireChangeStatus( "ERROR: Failed to Load Options File; See Stack Trace", true ); 798 ex.printStackTrace(); 799 } 800 } 801 802 private void loadComparisonPNG( String fileName ) { 804 try { 805 BufferedImage image = 806 javax.imageio.ImageIO.read(new File (fileName)); 807 JFrame f = new JFrame( "Comparison PNG" ); 808 ImagePanel ip = new ImagePanel( image ); 809 f.setResizable( false ); 810 f.getContentPane().add( ip ); 811 f.addWindowListener( new WindowAdapter () { 812 public void windowClosing( WindowEvent e ) { 813 ( (JFrame) e.getSource() ).dispose(); 814 } 815 }); 816 f.pack(); 817 f.show(); 818 } 819 catch ( Exception ex ) { 820 fireChangeStatus( "ERROR: Failed to Load PNG File; See Stack Trace", true ); 821 ex.printStackTrace(); 822 } 823 } 824 825 827 public void actionPerformed( ActionEvent e ) { 830 Object source = e.getSource(); 831 832 if ( source instanceof JMenuItem ) { 833 JMenuItem mi = (JMenuItem) source; 834 String itemName = mi.getText(); 835 836 if ( itemName.equals( "Save Selected Options..." )) { 837 String fileName = promptFile( true, "options.txt" ); 838 if ( fileName != null ) 839 writeCurrentOptions( fileName ); 840 } 841 else if ( itemName.equals( "Load Options..." )) { 842 String fileName = promptFile( false, "options.txt" ); 843 if ( fileName != null ) 844 loadOptions( fileName ); 845 } 846 else if ( itemName.equals( "Save as PNG..." )) { 847 String fileName = promptFile( true, fontMenu.getSelectedItem() + ".png" ); 848 if ( fileName != null ) 849 fp.doSavePNG( fileName ); 850 } 851 else if ( itemName.equals( "Load PNG File to Compare..." )) { 852 String fileName = promptFile( false, null ); 853 if ( fileName != null ) 854 loadComparisonPNG( fileName ); 855 } 856 else if ( itemName.equals( "Page Setup..." )) 857 fp.doPageSetup(); 858 else if ( itemName.equals( "Print..." )) 859 printDialog.show(); 860 else if ( itemName.equals( "Close" )) 861 parent.dispose(); 862 else if ( itemName.equals( "Exit" )) 863 System.exit(0); 864 } 865 866 else if ( source instanceof JTextField ) { 867 JTextField tf = (JTextField) source; 868 float sz = 12f; 869 try { 870 sz = Float.parseFloat(sizeField.getText()); 871 if (sz < 1f || sz > 120f) { 872 sz = 12f; 873 sizeField.setText("12"); 874 } 875 } catch (Exception se) { 876 sizeField.setText("12"); 877 } 878 if ( tf == sizeField ) 879 fp.setFontParams( fontMenu.getSelectedItem(), 880 sz, 881 styleMenu.getSelectedIndex(), 882 transformMenu.getSelectedIndex() ); 883 } 884 885 else if ( source instanceof JButton ) { 886 String itemName = ( (JButton) source ).getText(); 887 if ( itemName.equals( "Print" )) { 889 for ( int i = 0; i < printModeCBs.length; i++ ) 890 if ( printModeCBs[i].isSelected() ) { 891 printDialog.hide(); 892 fp.doPrint( i ); 893 } 894 } 895 else if ( itemName.equals( "Cancel" )) 896 printDialog.hide(); 897 else if ( itemName.equals( "Update" )) 899 fp.setTextToDraw( fp.USER_TEXT, null, 900 parseUserText( userTextArea.getText() ), null ); 901 } 902 else if ( source instanceof JComboBox ) { 903 JComboBox c = (JComboBox) source; 904 905 if ( c == fontMenu || c == styleMenu || c == transformMenu ) { 908 float sz = 12f; 909 try { 910 sz = Float.parseFloat(sizeField.getText()); 911 if (sz < 1f || sz > 120f) { 912 sz = 12f; 913 sizeField.setText("12"); 914 } 915 } catch (Exception se) { 916 sizeField.setText("12"); 917 } 918 fp.setFontParams(fontMenu.getSelectedItem(), 919 sz, 920 styleMenu.getSelectedIndex(), 921 transformMenu.getSelectedIndex()); 922 } else if ( c == methodsMenu ) 923 fp.setDrawMethod( methodsMenu.getSelectedIndex() ); 924 else if ( c == textMenu ) { 925 926 if(canDisplayCheck) { 927 fireRangeChanged(); 928 } 929 930 int selected = textMenu.getSelectedIndex(); 931 932 if ( selected == fp.RANGE_TEXT ) 933 fp.setTextToDraw( fp.RANGE_TEXT, rm.getSelectedRange(), 934 null, null ); 935 else if ( selected == fp.USER_TEXT ) 936 fp.setTextToDraw( fp.USER_TEXT, null, 937 parseUserText( userTextArea.getText() ), null ); 938 else if ( selected == fp.FILE_TEXT ) { 939 String fileName = promptFile( false, null ); 940 if ( fileName != null ) { 941 tFileName = fileName; 942 readTextFile( fileName ); 943 } else { 944 c.setSelectedIndex( currentTextChoice ); 946 return; 947 } 948 } 949 else if ( selected == fp.ALL_GLYPHS ) 950 fp.setTextToDraw( fp.ALL_GLYPHS, null, null, null ); 951 952 updateGUI(); 953 currentTextChoice = selected; 954 } 955 else if ( c == transformMenuG2 ) { 956 fp.setTransformG2( transformMenuG2.getSelectedIndex() ); 957 } 958 else if (c == antiAliasMenu || c == fracMetricsMenu) { 959 if (c == antiAliasMenu) { 960 boolean enabled = FontPanel.AAValues. 961 isLCDMode(antiAliasMenu.getSelectedItem()); 962 contrastSlider.setEnabled(enabled); 963 } 964 fp.setRenderingHints(antiAliasMenu.getSelectedItem(), 965 fracMetricsMenu.getSelectedItem(), 966 contrastSlider.getValue()); 967 } 968 } 969 } 970 971 public void stateChanged(ChangeEvent e) { 972 Object source = e.getSource(); 973 if (source instanceof JSlider) { 974 fp.setRenderingHints(antiAliasMenu.getSelectedItem(), 975 fracMetricsMenu.getSelectedItem(), 976 contrastSlider.getValue()); 977 } 978 } 979 980 public void itemStateChanged( ItemEvent e ) { 983 Object source = e.getSource(); 984 985 if ( source instanceof JCheckBoxMenuItem ) { 986 JCheckBoxMenuItem cbmi = (JCheckBoxMenuItem) source; 987 if ( cbmi == displayGridCBMI ) 988 fp.setGridDisplay( displayGridCBMI.getState() ); 989 else if ( cbmi == force16ColsCBMI ) 990 fp.setForce16Columns( force16ColsCBMI.getState() ); 991 else if ( cbmi == showFontInfoCBMI ) { 992 if ( showFontInfoCBMI.getState() ) { 993 fireUpdateFontInfo(); 994 fontInfoDialog.show(); 995 } 996 else 997 fontInfoDialog.hide(); 998 } 999 } 1000 } 1001 1002 private static void printUsage() { 1003 String usage = "Usage: java -jar Font2DTest.jar [options]\n" + 1004 "\nwhere options include:\n" + 1005 " -dcdc | -disablecandisplaycheck disable canDisplay check for font\n" + 1006 " -? | -help print this help message\n" + 1007 "\nExample :\n" + 1008 " To disable canDisplay check on font for ranges\n" + 1009 " java -jar Font2DTest.jar -dcdc"; 1010 System.out.println(usage); 1011 System.exit(0); 1012 } 1013 1014 public static void main(String argv[]) { 1016 1017 if(argv.length > 0) { 1018 if(argv[0].equalsIgnoreCase("-disablecandisplaycheck") || 1019 argv[0].equalsIgnoreCase("-dcdc")) { 1020 canDisplayCheck = false; 1021 } 1022 else { 1023 printUsage(); 1024 } 1025 } 1026 1027 UIManager.put("swing.boldMetal", Boolean.FALSE); 1028 final JFrame f = new JFrame( "Font2DTest" ); 1029 final Font2DTest f2dt = new Font2DTest( f, false ); 1030 f.addWindowListener( new WindowAdapter () { 1031 public void windowOpening( WindowEvent e ) { f2dt.repaint(); } 1032 public void windowClosing( WindowEvent e ) { System.exit(0); } 1033 }); 1034 1035 f.getContentPane().add( f2dt ); 1036 f.pack(); 1037 f.show(); 1038 } 1039 1040 1042 private final class ImagePanel extends JPanel { 1045 private final BufferedImage bi; 1046 1047 public ImagePanel( BufferedImage image ) { 1048 bi = image; 1049 } 1050 1051 public Dimension getPreferredSize() { 1052 return new Dimension ( bi.getWidth(), bi.getHeight() ); 1053 } 1054 1055 public void paintComponent( Graphics g ) { 1056 g.drawImage( bi, 0, 0, this ); 1057 } 1058 } 1059 1060 private final class ButtonV2 extends JButton { 1062 public ButtonV2( String name, ActionListener al ) { 1063 super( name ); 1064 this.addActionListener( al ); 1065 } 1066 } 1067 1068 private final class ChoiceV2 extends JComboBox { 1069 1070 private BitSet bitSet = null; 1071 1072 public ChoiceV2() {;} 1073 1074 public ChoiceV2( ActionListener al ) { 1075 super(); 1076 this.addActionListener( al ); 1077 } 1078 1079 public ChoiceV2( ActionListener al, boolean fontChoice) { 1080 this(al); 1081 if(fontChoice) { 1082 setToolTipText(""); 1084 bitSet = new BitSet (); 1085 setRenderer(new ChoiceV2Renderer(this)); 1086 } 1087 } 1088 1089 public String getToolTipText() { 1090 int index = this.getSelectedIndex(); 1091 String fontName = (String ) this.getSelectedItem(); 1092 if(fontName != null && 1093 (textMenu.getSelectedIndex() == fp.RANGE_TEXT)) { 1094 if (getBit(index)) { 1095 return "Font \"" + fontName + "\" can display some characters in \"" + 1096 rm.getSelectedItem() + "\" range"; 1097 } 1098 else { 1099 return "Font \"" + fontName + "\" cannot display any characters in \"" + 1100 rm.getSelectedItem() + "\" range"; 1101 } 1102 } 1103 return super.getToolTipText(); 1104 } 1105 1106 public void setBit(int bitIndex, boolean value) { 1107 bitSet.set(bitIndex, value); 1108 } 1109 1110 public boolean getBit(int bitIndex) { 1111 return bitSet.get(bitIndex); 1112 } 1113 } 1114 1115 private final class ChoiceV2Renderer extends DefaultListCellRenderer { 1116 1117 private ImageIcon yesImage, blankImage; 1118 private ChoiceV2 choice = null; 1119 1120 public ChoiceV2Renderer(ChoiceV2 choice) { 1121 try { 1122 yesImage = new ImageIcon(getClass().getResource("yes.gif")); 1123 blankImage = new ImageIcon(getClass().getResource("blank.gif")); 1124 } 1125 catch(Exception exception) { 1126 System.out.println("Exception : " + exception); 1127 } 1128 this.choice = choice; 1129 } 1130 1131 public Component getListCellRendererComponent(JList list, 1132 Object value, 1133 int index, 1134 boolean isSelected, 1135 boolean cellHasFocus) { 1136 1137 if(textMenu.getSelectedIndex() == fp.RANGE_TEXT) { 1138 1139 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 1140 1141 if(index == -1) { 1143 index = choice.getSelectedIndex(); 1144 } 1145 1146 if(choice.getBit(index)) { 1147 setIcon(yesImage); 1148 } 1149 else { 1150 setIcon(blankImage); 1151 } 1152 1153 } else { 1154 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 1155 setIcon(blankImage); 1156 } 1157 1158 return this; 1159 } 1160 } 1161 1162 private final class LabelV2 extends JLabel { 1163 public LabelV2( String name ) { 1164 super( name ); 1165 } 1166 } 1167 1168 private final class MenuItemV2 extends JMenuItem { 1169 public MenuItemV2( String name, ActionListener al ) { 1170 super( name ); 1171 this.addActionListener( al ); 1172 } 1173 } 1174 1175 private final class CheckboxMenuItemV2 extends JCheckBoxMenuItem { 1176 public CheckboxMenuItemV2( String name, boolean b, ItemListener il ) { 1177 super( name, b ); 1178 this.addItemListener( il ); 1179 } 1180 } 1181} 1182 | Popular Tags |