001 package com.croftsoft.core.awt.font;
002
003 import java.awt.*;
004 import java.awt.font.*;
005 import java.awt.geom.*;
006
007 /*********************************************************************
008 * Library of static methods for manipulating Fonts.
009 *
010 * @version
011 * 2003-02-10
012 * @since
013 * 2002-03-02
014 * @author
015 * <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
016 *********************************************************************/
017
018 public final class FontLib
019 //////////////////////////////////////////////////////////////////////
020 //////////////////////////////////////////////////////////////////////
021 {
022
023 public static Rectangle2D getTextLayoutBounds (
024 Component component,
025 String text )
026 //////////////////////////////////////////////////////////////////////
027 {
028 Graphics2D graphics2D
029 = ( Graphics2D ) component.getGraphics ( );
030
031 FontRenderContext fontRenderContext
032 = graphics2D.getFontRenderContext ( );
033
034 Font font = component.getFont ( );
035
036 TextLayout textLayout = new TextLayout (
037 text, font, fontRenderContext );
038
039 Rectangle2D textLayoutBounds = textLayout.getBounds ( );
040
041 graphics2D.dispose ( );
042
043 return textLayoutBounds;
044 }
045
046 /*********************************************************************
047 * Sets the Graphics Font size so that it maximizes the bounded text.
048 *********************************************************************/
049 public static void setMaxFont (
050 Graphics graphics,
051 String text,
052 String fontName,
053 int fontStyle,
054 double maxWidth,
055 double maxHeight )
056 //////////////////////////////////////////////////////////////////////
057 {
058 int fontSize = 2;
059
060 while ( true )
061 {
062 graphics.setFont ( new Font ( fontName, fontStyle, fontSize ) );
063
064 FontMetrics fontMetrics = graphics.getFontMetrics ( );
065
066 Rectangle2D textBounds
067 = fontMetrics.getStringBounds ( text, graphics );
068
069 if ( ( textBounds.getWidth ( ) > maxWidth )
070 || ( textBounds.getHeight ( ) > maxHeight ) )
071 {
072 graphics.setFont (
073 new Font ( fontName, fontStyle, fontSize - 1 ) );
074
075 break;
076 }
077
078 fontSize++;
079 }
080 }
081
082 /*********************************************************************
083 * Sets the Graphics Font size so that it maximizes the bounded text.
084 *********************************************************************/
085 public static void setMaxFont (
086 Component component,
087 String text,
088 String fontName,
089 int fontStyle,
090 double maxWidth,
091 double maxHeight )
092 //////////////////////////////////////////////////////////////////////
093 {
094 Graphics graphics = component.getGraphics ( );
095
096 setMaxFont (
097 graphics, text, fontName, fontStyle, maxWidth, maxHeight );
098
099 component.setFont ( graphics.getFont ( ) );
100
101 graphics.dispose ( );
102 }
103
104 //////////////////////////////////////////////////////////////////////
105 //////////////////////////////////////////////////////////////////////
106
107 private FontLib ( ) { }
108
109 //////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////
111 }