package textbender.o.awt; // Copyright 2001-2005, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.*; import java.awt.font.FontRenderContext; import java.awt.geom.Dimension2D; import java.awt.geom.Rectangle2D; import javax.swing.*; import textbender.o.swing.Swing; /** Font utilities and constants. */ public class FontX { private FontX() {} /** @return same as {@link #largest2PowerPointSize largest2PowerPointSize} * but search is further restricted to sizes that are less than or * equal to baseFont's size [never tested] */ public static float largest2PowerFractionalPointSize( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext ){ return largest2PowerFractionalPointSize ( baseFont, referenceString, maxDimension, fontRenderContext, baseFont.getStringBounds( referenceString, fontRenderContext ) ); } /** @return same as {@link #largest2PowerPointSize largest2PowerPointSize} * but search is further restricted to sizes that are greater than or * equal to baseFont's size */ public static float largest2PowerMultiplePointSize( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext ){ return largest2PowerMultiplePointSize ( baseFont, referenceString, maxDimension, fontRenderContext, baseFont.getStringBounds( referenceString, fontRenderContext ) ); } /** @return same as {@link #largestPointSize largestPointSize} but search is * further restricted to sizes that are an integral power of 2 (half, * double, etc.) of baseFont's size [never tested] */ public static float largest2PowerPointSize( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext ){ Rectangle2D baseRectangle = baseFont.getStringBounds( referenceString, fontRenderContext ); if( baseRectangle.getWidth() > maxDimension.getWidth() || baseRectangle.getHeight() > maxDimension.getHeight() ) return largest2PowerFractionalPointSize ( baseFont, referenceString, maxDimension, fontRenderContext, baseRectangle ); else return largest2PowerMultiplePointSize ( baseFont, referenceString, maxDimension, fontRenderContext, baseRectangle ); } /** @return largest point size of baseFont that, when used to render * referenceString, will allow it to fit within maxDimension [incomplete] */ public static float largestPointSize( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext ){ float minPoints = largest2PowerPointSize ( baseFont, referenceString, maxDimension, fontRenderContext ); float maxPoints = minPoints * 2; // yet to be coded throw new UnsupportedOperationException(); } /** Rough size of the typographic en space, in pixels. * Calculated as {@linkplain #EM EM}/2. *
* An en space is one-half the width of an em space... * [http://atlas.gc.ca/english/carto/type.html] *
*/ public static final int EN; /** Rough size of the typographic em space, in pixels. * Calculated from the platform default font, * making it suitable for gross-scale layout. *
* An em space is the square of the body of the printed type being used. * In twelve-point type an em would be 12 x 12 points, * in ten-point type an em would be 10 x 10 points, and so on. * The expression is derived from the letter 'm' * which is the widest letter in the lower-case alphabet. * [http://atlas.gc.ca/english/carto/type.html] *
*
* "Em's are typically used for font-relative horizontal sizes." * [http://www.w3.org/TR/MathML2/appendixh.html] *
*/ public static final int EM; static { EM = Swing.typicalComponentFont().getSize(); EN = EM / 2; } // /** The size of the typographic ex space, in pixels. // *
// * A font-relative measure that is the height of an 'x' in the font. // * Ex's are typically used for font-relative vertical sizes. // * [http://www.w3.org/TR/MathML2/appendixh.html] // *
// */ // public static final int EX; // ///// cannot calculate without an instance of Graphics //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static float largest2PowerFractionalPointSize ( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext, Rectangle2D baseRectangle ){ float pointSize = baseFont.getSize2D(); while( baseRectangle.getWidth() > maxDimension.getWidth() || baseRectangle.getHeight() > maxDimension.getHeight() ) { pointSize /= 2; baseFont = baseFont.deriveFont( pointSize ); baseRectangle = baseFont.getStringBounds( referenceString, fontRenderContext ); } return pointSize; } private static float largest2PowerMultiplePointSize ( Font baseFont, String referenceString, Dimension2D maxDimension, FontRenderContext fontRenderContext, Rectangle2D baseRectangle ){ float pointSize = baseFont.getSize2D(); while( baseRectangle.getWidth() <= maxDimension.getWidth() && baseRectangle.getHeight() <= maxDimension.getHeight() ) { pointSize *= 2; baseFont = baseFont.deriveFont( pointSize ); baseRectangle = baseFont.getStringBounds( referenceString, fontRenderContext ); } return pointSize/2; } }