001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    
016    package lapisx.swing;
017    
018    import java.awt.*;
019    import java.awt.font.*;
020    import javax.swing.*;
021    //import javax.swing.text.*;
022    import javax.swing.text.html.*;
023    import javax.swing.plaf.*;
024    import javax.swing.plaf.metal.*;
025    
026    public abstract class FontUtil {
027    
028        /**
029         * Get height of a font.
030         * @param f Font
031         * @param c Component that will display the font.  Must be showing
032         * on the screen, otherwise we can't get its Graphics context.
033         * @return height of font (leading + ascent + descent)
034         * @throws IllegalArgumentException if c is not showing on the screen.
035         * 
036         */
037        public static int getHeight (Font f, Component c) {
038            Graphics g = c.getGraphics ();
039            if (g == null)
040                throw new IllegalArgumentException ("component not visible");
041            FontRenderContext frc = ((Graphics2D) g).getFontRenderContext ();
042            LineMetrics lm = f.getLineMetrics ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", frc);
043            return (int) Math.ceil (lm.getHeight ());
044        }
045    
046        /**
047         * Sets the size of a component's font.
048         * @param c Component to change
049         * @param size font size in points
050         * @effects calls c.setFont() to change c's font to a font with the same font family and style, but
051         * the given font size.
052         */
053        public static void resizeFont (Component c, int size) {
054            Font f = c.getFont ();
055            if (f == null)
056                return;
057            c.setFont (resizeFont (f, size));
058        }
059    
060        /**
061         * Enlarges or shrinks a font.
062         * @param f Font to use
063         * @param size new font size in points
064         * @return font with same family and style as f, but the given size
065         */
066        public static Font resizeFont (Font f, int size) { 
067            return new Font (f.getName (), f.getStyle (), size);
068        }
069    
070        /**
071         * Sets the size of a JEditorPane's HTML fonts.
072         * @param c JEditorPane to change
073         * @param size font size in points
074         * @effects sets the size of body and paragraph fonts in c's HTML stylesheet
075         */
076        public static void resizeHTMLFont (JEditorPane c, int size) {
077            HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKitForContentType ("text/html");
078            StyleSheet stylesheet = kit.getStyleSheet ();
079            stylesheet.addRule ("body {font-size: " + size + "pt}");
080            stylesheet.addRule ("p {font-size: " + size + "pt}");
081        }
082    
083        /**
084         * Makes a Metal theme with resized fonts.
085         * @param size font size in points
086         * @return MetalTheme that uses the given font size for all fonts.
087         * Use {@link MetalLookAndFeel#setCurrentTheme} to install it.
088         */
089        public static MetalTheme makeResizedMetalTheme (final int size) {
090            return new DefaultMetalTheme () {
091                    public String getName () { return "lapisx.swing.FontAdjustment"; }
092    
093                    public FontUIResource getControlTextFont() { 
094                        return new FontUIResource (resizeFont (super.getControlTextFont (), size));
095                    }
096                
097                    public FontUIResource getSystemTextFont() { 
098                        return new FontUIResource (resizeFont (super.getSystemTextFont (), size));
099                    }
100                
101                    public FontUIResource getUserTextFont() { 
102                        return new FontUIResource (resizeFont (super.getUserTextFont (), size));
103                    }
104                
105                    public FontUIResource getMenuTextFont() { 
106                        return new FontUIResource (resizeFont (super.getMenuTextFont (), size));
107                    }
108                
109                    public FontUIResource getWindowTitleFont() { 
110                        return new FontUIResource (resizeFont (super.getWindowTitleFont (), size));
111                    }
112                };
113        }
114    
115    }
116