home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Font.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  232 lines

  1. /*
  2.  * @(#)Font.java    1.15 96/03/28 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. /** 
  22.  * A class that produces font objects. 
  23.  *
  24.  * @version     1.15, 28 Mar 1996
  25.  * @author     Sami Shaio
  26.  * @author     Arthur van Hoff
  27.  * @author     Jim Graham
  28.  */
  29. public class Font {
  30.  
  31.     /* 
  32.      * Constants to be used for styles. Can be combined to mix
  33.      * styles. 
  34.      */
  35.  
  36.     /**
  37.      * The plain style constant.  This can be combined with the other style
  38.      * constants for mixed styles.
  39.      */
  40.     public static final int PLAIN    = 0;
  41.  
  42.     /**
  43.      * The bold style constant.  This can be combined with the other style
  44.      * constants for mixed styles.
  45.      */
  46.     public static final int BOLD    = 1;
  47.  
  48.     /**
  49.      * The italicized style constant.  This can be combined with the other
  50.      * style constants for mixed styles.
  51.      */
  52.     public static final int ITALIC    = 2;
  53.  
  54.     /**
  55.      * Private data.
  56.      */
  57.     private int pData;
  58.  
  59.     /** 
  60.      * The platform specific family name of this font. 
  61.      */
  62.     private String family;
  63.  
  64.     /** 
  65.      * The logical name of this font. 
  66.      */
  67.     protected String name;
  68.  
  69.     /** 
  70.      * The style of the font. This is the sum of the
  71.      * constants PLAIN, BOLD, or ITALIC. 
  72.      */
  73.     protected int style;
  74.  
  75.     /** 
  76.      * The point size of this font. 
  77.      */
  78.     protected int size;
  79.  
  80.     /**
  81.      * Creates a new font with the specified name, style and point size.
  82.      * @param name the font name
  83.      * @param style the constant style used
  84.      * @param size the point size of the font
  85.      * @see Toolkit#getFontList
  86.      */
  87.     public Font(String name, int style, int size) {
  88.     this.family = System.getProperty("awt.font." + name.toLowerCase(), name);
  89.     this.name = name;
  90.     this.style = style;
  91.     this.size = size;
  92.     }
  93.  
  94.     /**
  95.      * Gets the platform specific family name of the font.
  96.      * Use getName to get the logical name of the font.
  97.      * @see #getName
  98.      */
  99.     public String getFamily() {
  100.     return family;
  101.     }
  102.  
  103.     /**
  104.      * Gets the logical name of the font.
  105.      * @see #getFamily
  106.      */
  107.     public String getName() {
  108.     return name;
  109.     }
  110.  
  111.     /**
  112.      * Gets the style of the font.
  113.      * @see #isPlain
  114.      * @see #isBold
  115.      * @see #isItalic
  116.      */
  117.     public int getStyle() {
  118.     return style;
  119.     }
  120.  
  121.     /**
  122.      * Gets the point size of the font.
  123.      */
  124.     public int getSize() {
  125.     return size;
  126.     }
  127.  
  128.     /**
  129.      * Returns true if the font is plain.
  130.      * @see #getStyle
  131.      */
  132.     public boolean isPlain() {
  133.     return style == 0;
  134.     }
  135.  
  136.     /**
  137.      * Returns true if the font is bold.
  138.      * @see #getStyle
  139.      */
  140.     public boolean isBold() {
  141.     return (style & BOLD) != 0;
  142.     }
  143.  
  144.     /**
  145.      * Returns true if the font is italic.
  146.      * @see #getStyle
  147.      */
  148.     public boolean isItalic() {
  149.     return (style & ITALIC) != 0;
  150.     }
  151.  
  152.     /**
  153.      * Gets a font from the system properties list.
  154.      * @param nm the property name
  155.      */
  156.     public static Font getFont(String nm) {
  157.     return getFont(nm, null);
  158.     }
  159.  
  160.     /**
  161.      * Gets the specified font from the system properties list.
  162.      * @param nm the property name
  163.      * @param font a default font to return if property 'nm' is not defined
  164.      */
  165.     public static Font getFont(String nm, Font font) {
  166.     String str = System.getProperty(nm);
  167.     if (str == null) {
  168.         return font;
  169.     }
  170.     String fontName = str;
  171.     int fontSize = 12;
  172.     int fontStyle = Font.PLAIN;
  173.  
  174.     int i = str.indexOf('-');
  175.     if (i >= 0) {
  176.         fontName = str.substring(0, i);
  177.         str = str.substring(i+1);
  178.         if ((i = str.indexOf('-')) >= 0) {
  179.         if (str.startsWith("bold-")) {
  180.             fontStyle = Font.BOLD;
  181.         } else if (str.startsWith("italic-")) {
  182.             fontStyle = Font.ITALIC;
  183.         } else if (str.startsWith("bolditalic-")) {
  184.             fontStyle = Font.BOLD | Font.ITALIC;
  185.         }
  186.         str = str.substring(i + 1);
  187.         }
  188.         try {
  189.         fontSize = Integer.valueOf(str).intValue();
  190.         } catch (NumberFormatException e) {
  191.         }
  192.     }
  193.     return new Font(fontName, fontStyle, fontSize);
  194.     }
  195.  
  196.     /**
  197.      * Returns a hashcode for this font.
  198.      */
  199.     public int hashCode() {
  200.     return name.hashCode() ^ style ^ size;
  201.     }
  202.     
  203.     /**
  204.      * Compares this object to the specifed object.
  205.      * @param obj the object to compare with
  206.      * @return true if the objects are the same; false otherwise.
  207.      */
  208.     public boolean equals(Object obj) {
  209.     if (obj instanceof Font) {
  210.         Font font = (Font)obj;
  211.         return (size == font.size) && (style == font.style) && name.equals(font.name);
  212.     }
  213.     return false;
  214.     }
  215.  
  216.     /** 
  217.      * Converts this object to a String representation. 
  218.      */
  219.     public String toString() {
  220.     String    strStyle;
  221.  
  222.     if (isBold()) {
  223.         strStyle = isItalic() ? "bolditalic" : "bold";
  224.     } else {
  225.         strStyle = isItalic() ? "italic" : "plain";
  226.     }
  227.  
  228.     return getClass().getName() + "[family=" + family + ",name=" + name + ",style=" +
  229.         strStyle + ",size=" + size + "]";
  230.     }
  231. }
  232.