📄 fontpanel.java
字号:
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 ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -