home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / Font.java < prev    next >
Text File  |  1997-10-27  |  7KB  |  295 lines

  1. /*
  2.  * @(#)Font.java    1.27 97/02/13
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.peer.FontPeer;
  25.  
  26. /** 
  27.  * A class that produces font objects. 
  28.  *
  29.  * @version     1.27, 02/13/97
  30.  * @author     Sami Shaio
  31.  * @author     Arthur van Hoff
  32.  * @author     Jim Graham
  33.  */
  34. public class Font implements java.io.Serializable {
  35.  
  36.     /* 
  37.      * Constants to be used for styles. Can be combined to mix
  38.      * styles. 
  39.      */
  40.  
  41.     /**
  42.      * The plain style constant.  This can be combined with the other style
  43.      * constants for mixed styles.
  44.      */
  45.     public static final int PLAIN    = 0;
  46.  
  47.     /**
  48.      * The bold style constant.  This can be combined with the other style
  49.      * constants for mixed styles.
  50.      */
  51.     public static final int BOLD    = 1;
  52.  
  53.     /**
  54.      * The italicized style constant.  This can be combined with the other
  55.      * style constants for mixed styles.
  56.      */
  57.     public static final int ITALIC    = 2;
  58.  
  59.     /**
  60.      * Private data.
  61.      */
  62.     transient private int pData;
  63.  
  64.     /** 
  65.      * The platform specific family name of this font. 
  66.      */
  67.     transient private String family;
  68.  
  69.     /** 
  70.      * The logical name of this font. 
  71.      */
  72.     protected String name;
  73.  
  74.     /** 
  75.      * The style of the font. This is the sum of the
  76.      * constants PLAIN, BOLD, or ITALIC. 
  77.      */
  78.     protected int style;
  79.  
  80.     /** 
  81.      * The point size of this font. 
  82.      */
  83.     protected int size;
  84.  
  85.     /**
  86.      * The platform specific font information.
  87.      */
  88.     transient FontPeer peer;
  89.  
  90.     /*
  91.      * JDK 1.1 serialVersionUID 
  92.      */
  93.      private static final long serialVersionUID = -4206021311591459213L;
  94.  
  95.     /**
  96.      * Gets the peer of the font.
  97.      */
  98.     public FontPeer getPeer(){
  99.     return peer;
  100.     }
  101.  
  102.     private void initializeFont()
  103.     {
  104.       family = System.getProperty("awt.font." + name.toLowerCase(), name);
  105.       this.peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
  106.     }
  107.  
  108.     /**
  109.      * Creates a new font with the specified name, style and point size.
  110.      * @param name the font name
  111.      * @param style the constant style used
  112.      * @param size the point size of the font
  113.      * @see Toolkit#getFontList
  114.      */
  115.     public Font(String name, int style, int size) {
  116.     this.name = name;
  117.     this.style = style;
  118.     this.size = size;
  119.     initializeFont();
  120.     }
  121.  
  122.     /**
  123.      * Gets the platform specific family name of the font.
  124.      * Use getName to get the logical name of the font.
  125.      * @see #getName
  126.      */
  127.     public String getFamily() {
  128.     return family;
  129.     }
  130.  
  131.     /**
  132.      * Gets the logical name of the font.
  133.      * @see #getFamily
  134.      */
  135.     public String getName() {
  136.     return name;
  137.     }
  138.  
  139.     /**
  140.      * Gets the style of the font.
  141.      * @see #isPlain
  142.      * @see #isBold
  143.      * @see #isItalic
  144.      */
  145.     public int getStyle() {
  146.     return style;
  147.     }
  148.  
  149.     /**
  150.      * Gets the point size of the font.
  151.      */
  152.     public int getSize() {
  153.     return size;
  154.     }
  155.  
  156.     /**
  157.      * Returns true if the font is plain.
  158.      * @see #getStyle
  159.      */
  160.     public boolean isPlain() {
  161.     return style == 0;
  162.     }
  163.  
  164.     /**
  165.      * Returns true if the font is bold.
  166.      * @see #getStyle
  167.      */
  168.     public boolean isBold() {
  169.     return (style & BOLD) != 0;
  170.     }
  171.  
  172.     /**
  173.      * Returns true if the font is italic.
  174.      * @see #getStyle
  175.      */
  176.     public boolean isItalic() {
  177.     return (style & ITALIC) != 0;
  178.     }
  179.  
  180.     /**
  181.      * Gets a font from the system properties list.
  182.      * @param nm the property name
  183.      */
  184.     public static Font getFont(String nm) {
  185.     return getFont(nm, null);
  186.     }
  187.  
  188.     /**
  189.      * Gets the specified font using the name passed in.
  190.      * @param str the name
  191.      */
  192.     public static Font decode(String str) {
  193.     String fontName = str;
  194.     int fontSize = 12;
  195.     int fontStyle = Font.PLAIN;
  196.  
  197.     int i = str.indexOf('-');
  198.     if (i >= 0) {
  199.         fontName = str.substring(0, i);
  200.         str = str.substring(i+1);
  201.         if ((i = str.indexOf('-')) >= 0) {
  202.         if (str.startsWith("bold-")) {
  203.             fontStyle = Font.BOLD;
  204.         } else if (str.startsWith("italic-")) {
  205.             fontStyle = Font.ITALIC;
  206.         } else if (str.startsWith("bolditalic-")) {
  207.             fontStyle = Font.BOLD | Font.ITALIC;
  208.         }
  209.         str = str.substring(i + 1);
  210.         }
  211.         try {
  212.         fontSize = Integer.valueOf(str).intValue();
  213.         } catch (NumberFormatException e) {
  214.         }
  215.     }
  216.     return new Font(fontName, fontStyle, fontSize);
  217.     }
  218.  
  219.     /**
  220.      * Gets the specified font from the system properties list.
  221.      * @param nm the property name
  222.      * @param font a default font to return if property 'nm' is not defined
  223.      */
  224.     public static Font getFont(String nm, Font font) {
  225.     String str = System.getProperty(nm);
  226.     if (str == null) {
  227.         return font;
  228.     }
  229.     return Font.decode(str);
  230.     }
  231.  
  232.     /**
  233.      * Returns a hashcode for this font.
  234.      */
  235.     public int hashCode() {
  236.     return name.hashCode() ^ style ^ size;
  237.     }
  238.     
  239.     /**
  240.      * Compares this object to the specifed object.
  241.      * @param obj the object to compare with
  242.      * @return true if the objects are the same; false otherwise.
  243.      */
  244.     public boolean equals(Object obj) {
  245.     if (obj instanceof Font) {
  246.         Font font = (Font)obj;
  247.         return (size == font.size) && (style == font.style) && name.equals(font.name);
  248.     }
  249.     return false;
  250.     }
  251.  
  252.     /** 
  253.      * Converts this object to a String representation. 
  254.      */
  255.     public String toString() {
  256.     String    strStyle;
  257.  
  258.     if (isBold()) {
  259.         strStyle = isItalic() ? "bolditalic" : "bold";
  260.     } else {
  261.         strStyle = isItalic() ? "italic" : "plain";
  262.     }
  263.  
  264.     return getClass().getName() + "[family=" + family + ",name=" + name + ",style=" +
  265.         strStyle + ",size=" + size + "]";
  266.     }
  267.  
  268.  
  269.     /* Serialization support.  A readObject method is neccessary because
  270.      * the constructor creates the fonts peer, and we can't serialize the
  271.      * peer.  Similarly the computed font "family" may be different
  272.      * at readObject time than at writeObject time.  An integer version is 
  273.      * written so that future versions of this class will be able to recognize 
  274.      * serialized output from this one.
  275.      */
  276.  
  277.     private int fontSerializedDataVersion = 1;
  278.  
  279.     private void writeObject(java.io.ObjectOutputStream s)
  280.       throws java.lang.ClassNotFoundException,
  281.          java.io.IOException 
  282.     {
  283.       s.defaultWriteObject();
  284.     }
  285.  
  286.     private void readObject(java.io.ObjectInputStream s)
  287.       throws java.lang.ClassNotFoundException,
  288.          java.io.IOException 
  289.     {
  290.       s.defaultReadObject();
  291.       initializeFont();
  292.     }
  293. }
  294.  
  295.