📄 font2dtest.java
字号:
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++ ) { printModeCBs[i].setFont( labelFont ); 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 ( 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(); } } /// 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 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() ) { String oneLine = perLine.nextToken(); int lineLength = oneLine.length(); int prevEscapeEnd = 0; int nextEscape = oneLine.indexOf( "\\u" ); StringBuffer converted = new StringBuffer(); while ( nextEscape != -1 ) { if ( prevEscapeEnd < nextEscape ) converted.append( oneLine.substring( prevEscapeEnd, nextEscape )); prevEscapeEnd = nextEscape + 6; try { String hex = oneLine.substring( nextEscape + 2, nextEscape + 6 ); converted.append( (char) Integer.parseInt( hex, 16 )); } catch ( Exception e ) { int copyLimit = ( nextEscape + 6 < lineLength ) ? ( nextEscape + 6 ) : lineLength; converted.append( oneLine.substring( nextEscape, copyLimit )); } nextEscape = oneLine.indexOf( "\\u", prevEscapeEnd ); } 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" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -