home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / Font.java < prev    next >
Text File  |  1997-08-30  |  10KB  |  358 lines

  1. /*
  2.  * @(#)Font.java    1.28 97/06/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.28, 06/13/97
  30.  * @author     Sami Shaio
  31.  * @author     Arthur van Hoff
  32.  * @author     Jim Graham
  33.  * @since       JDK1.0
  34.  */
  35. public class Font implements java.io.Serializable {
  36.  
  37.     /* 
  38.      * Constants to be used for styles. Can be combined to mix
  39.      * styles. 
  40.      */
  41.  
  42.     /**
  43.      * The plain style constant.  This style can be combined with 
  44.      * the other style constants for mixed styles.
  45.      * @since JDK1.0
  46.      */
  47.     public static final int PLAIN    = 0;
  48.  
  49.     /**
  50.      * The bold style constant.  This style can be combined with the 
  51.      * other style constants for mixed styles.
  52.      * @since JDK1.0
  53.      */
  54.     public static final int BOLD    = 1;
  55.  
  56.     /**
  57.      * The italicized style constant.  This style can be combined 
  58.      * with the other style constants for mixed styles.
  59.      * @since JDK1.0
  60.      */
  61.     public static final int ITALIC    = 2;
  62.  
  63.     /**
  64.      * Private data.
  65.      */
  66.     transient private int pData;
  67.  
  68.     /** 
  69.      * The platform specific family name of this font. 
  70.      */
  71.     transient private String family;
  72.  
  73.     /** 
  74.      * The logical name of this font. 
  75.      * @since JDK1.0
  76.      */
  77.     protected String name;
  78.  
  79.     /** 
  80.      * The style of the font. This is the sum of the
  81.      * constants <code>PLAIN</code>, <code>BOLD</code>, 
  82.      * or <code>ITALIC</code>. 
  83.      */
  84.     protected int style;
  85.  
  86.     /** 
  87.      * The point size of this font. 
  88.      * @since JDK1.0
  89.      */
  90.     protected int size;
  91.  
  92.     /**
  93.      * The platform specific font information.
  94.      */
  95.     transient FontPeer peer;
  96.  
  97.     /*
  98.      * JDK 1.1 serialVersionUID 
  99.      */
  100.      private static final long serialVersionUID = -4206021311591459213L;
  101.  
  102.     /**
  103.      * Gets the peer of the font.
  104.      * @return  the peer of the font.
  105.      * @since JDK1.1
  106.      */
  107.     public FontPeer getPeer(){
  108.     return peer;
  109.     }
  110.  
  111.     private void initializeFont()
  112.     {
  113.       family = System.getProperty("awt.font." + name.toLowerCase(), name);
  114.       this.peer = Toolkit.getDefaultToolkit().getFontPeer(name, style);
  115.     }
  116.  
  117.     /**
  118.      * Creates a new font with the specified name, style and point size.
  119.      * @param name the font name
  120.      * @param style the constant style used
  121.      * @param size the point size of the font
  122.      * @see Toolkit#getFontList
  123.      * @since JDK1.0
  124.      */
  125.     public Font(String name, int style, int size) {
  126.     this.name = name;
  127.     this.style = style;
  128.     this.size = size;
  129.     initializeFont();
  130.     }
  131.  
  132.     /**
  133.      * Gets the platform specific family name of the font. Use the 
  134.      * <code>getName</code> method to get the logical name of the font.
  135.      * @return    a string, the platform specific family name.
  136.      * @see       java.awt.Font#getName
  137.      * @since     JDK1.0
  138.      */
  139.     public String getFamily() {
  140.     return family;
  141.     }
  142.  
  143.     /**
  144.      * Gets the logical name of the font.
  145.      * @return    a string, the logical name of the font.
  146.      * @see #getFamily
  147.      * @since     JDK1.0
  148.      */
  149.     public String getName() {
  150.     return name;
  151.     }
  152.  
  153.     /**
  154.      * Gets the style of the font.
  155.      * @return the style of this font.
  156.      * @see #isPlain
  157.      * @see #isBold
  158.      * @see #isItalic
  159.      * @since JDK1.0
  160.      */
  161.     public int getStyle() {
  162.     return style;
  163.     }
  164.  
  165.     /**
  166.      * Gets the point size of the font.
  167.      * @return the point size of this font.
  168.      * @since JDK1.0
  169.      */
  170.     public int getSize() {
  171.     return size;
  172.     }
  173.  
  174.     /**
  175.      * Indicates whether the font's style is plain.
  176.      * @return     <code>true</code> if the font is neither 
  177.      *                bold nor italic; <code>false</code> otherwise.
  178.      * @see        java.awt.Font#getStyle
  179.      * @since      JDK1.0
  180.      */
  181.     public boolean isPlain() {
  182.     return style == 0;
  183.     }
  184.  
  185.     /**
  186.      * Indicates whether the font's style is bold.
  187.      * @return    <code>true</code> if the font is bold; 
  188.      *            <code>false</code> otherwise.
  189.      * @see       java.awt.Font#getStyle
  190.      * @since     JDK1.0
  191.      */
  192.     public boolean isBold() {
  193.     return (style & BOLD) != 0;
  194.     }
  195.  
  196.     /**
  197.      * Indicates whether the font's style is italic.
  198.      * @return    <code>true</code> if the font is italic; 
  199.      *            <code>false</code> otherwise.
  200.      * @see       java.awt.Font#getStyle
  201.      * @since     JDK1.0
  202.      */
  203.     public boolean isItalic() {
  204.     return (style & ITALIC) != 0;
  205.     }
  206.  
  207.     /**
  208.      * Gets a font from the system properties list.
  209.      * @param nm the property name
  210.      * @see       java.awt.Font#getFont(java.lang.String, java.awt.Font)
  211.      * @since     JDK1.0
  212.      */
  213.     public static Font getFont(String nm) {
  214.     return getFont(nm, null);
  215.     }
  216.  
  217.     /**
  218.      * Gets the specified font using the name passed in.
  219.      * @param str the name
  220.      * @since JDK1.1
  221.      */
  222.     public static Font decode(String str) {
  223.     String fontName = str;
  224.     int fontSize = 12;
  225.     int fontStyle = Font.PLAIN;
  226.  
  227.     int i = str.indexOf('-');
  228.     if (i >= 0) {
  229.         fontName = str.substring(0, i);
  230.         str = str.substring(i+1);
  231.         if ((i = str.indexOf('-')) >= 0) {
  232.         if (str.startsWith("bold-")) {
  233.             fontStyle = Font.BOLD;
  234.         } else if (str.startsWith("italic-")) {
  235.             fontStyle = Font.ITALIC;
  236.         } else if (str.startsWith("bolditalic-")) {
  237.             fontStyle = Font.BOLD | Font.ITALIC;
  238.         }
  239.         str = str.substring(i + 1);
  240.         }
  241.         try {
  242.         fontSize = Integer.valueOf(str).intValue();
  243.         } catch (NumberFormatException e) {
  244.         }
  245.     }
  246.     return new Font(fontName, fontStyle, fontSize);
  247.     }
  248.  
  249.     /**
  250.      * Gets the specified font from the system properties list.
  251.      * The first argument is treated as the name of a system property to 
  252.      * be obtained as if by the method <code>System.getProperty</code>.  
  253.      * The string value of this property is then interpreted as a font. 
  254.      * <p>
  255.      * The property value should be one of the following forms: 
  256.      * <ul>
  257.      * <li><em>fontname-style-pointsize</em>
  258.      * <li><em>fontname-pointsize</em>
  259.      * <li><em>fontname-style</em>
  260.      * <li><em>fontname</em>
  261.      * </ul>
  262.      * where <i>style</i> is one of the three strings 
  263.      * <code>"BOLD"</code>, <code>"BOLDITALIC"</code>, or 
  264.      * <code>"ITALIC"</code>, and point size is a decimal 
  265.      * representation of the point size. 
  266.      * <p>
  267.      * The default style is <code>PLAIN</code>. The default point size 
  268.      * is 12. 
  269.      * <p>
  270.      * If the specified property is not found, the <code>font</code> 
  271.      * argument is returned instead. 
  272.      * @param nm the property name
  273.      * @param font a default font to return if property <code>nm</code> 
  274.      *             is not defined
  275.      * @return    the <code>Font</code> value of the property.
  276.      * @since     JDK1.0
  277.      */
  278.     public static Font getFont(String nm, Font font) {
  279.     String str = System.getProperty(nm);
  280.     if (str == null) {
  281.         return font;
  282.     }
  283.     return Font.decode(str);
  284.     }
  285.  
  286.     /**
  287.      * Returns a hashcode for this font.
  288.      * @return     a hashcode value for this font.
  289.      * @since      JDK1.0
  290.      */
  291.     public int hashCode() {
  292.     return name.hashCode() ^ style ^ size;
  293.     }
  294.     
  295.     /**
  296.      * Compares this object to the specifed object.
  297.      * The result is <code>true</code> if and only if the argument is not 
  298.      * <code>null</code> and is a <code>Font</code> object with the same 
  299.      * name, style, and point size as this font. 
  300.      * @param     obj   the object to compare this font with.
  301.      * @return    <code>true</code> if the objects are equal; 
  302.      *            <code>false</code> otherwise.
  303.      * @since     JDK1.0
  304.      */
  305.     public boolean equals(Object obj) {
  306.     if (obj instanceof Font) {
  307.         Font font = (Font)obj;
  308.         return (size == font.size) && (style == font.style) && name.equals(font.name);
  309.     }
  310.     return false;
  311.     }
  312.  
  313.     /** 
  314.      * Converts this object to a String representation. 
  315.      * @return     a string representation of this object
  316.      * @since      JDK1.0
  317.      */
  318.     public String toString() {
  319.     String    strStyle;
  320.  
  321.     if (isBold()) {
  322.         strStyle = isItalic() ? "bolditalic" : "bold";
  323.     } else {
  324.         strStyle = isItalic() ? "italic" : "plain";
  325.     }
  326.  
  327.     return getClass().getName() + "[family=" + family + ",name=" + name + ",style=" +
  328.         strStyle + ",size=" + size + "]";
  329.     }
  330.  
  331.  
  332.     /* Serialization support.  A readObject method is neccessary because
  333.      * the constructor creates the fonts peer, and we can't serialize the
  334.      * peer.  Similarly the computed font "family" may be different
  335.      * at readObject time than at writeObject time.  An integer version is 
  336.      * written so that future versions of this class will be able to recognize 
  337.      * serialized output from this one.
  338.      */
  339.  
  340.     private int fontSerializedDataVersion = 1;
  341.  
  342.     private void writeObject(java.io.ObjectOutputStream s)
  343.       throws java.lang.ClassNotFoundException,
  344.          java.io.IOException 
  345.     {
  346.       s.defaultWriteObject();
  347.     }
  348.  
  349.     private void readObject(java.io.ObjectInputStream s)
  350.       throws java.lang.ClassNotFoundException,
  351.          java.io.IOException 
  352.     {
  353.       s.defaultReadObject();
  354.       initializeFont();
  355.     }
  356. }
  357.  
  358.