/[classpath]/classpath/java/awt/FontMetrics.java
ViewVC logotype

Diff of /classpath/java/awt/FontMetrics.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.11 by mkoch, Tue Apr 12 12:04:39 2005 UTC revision 1.12 by mkoch, Tue Apr 12 12:15:56 2005 UTC
# Line 50  import java.text.CharacterIterator; Line 50  import java.text.CharacterIterator;
50    * This class returns information about the display characteristics of    * This class returns information about the display characteristics of
51    * a font.  It is abstract, and concrete subclasses should implement at    * a font.  It is abstract, and concrete subclasses should implement at
52    * least the following methods:    * least the following methods:
53    * <p>    *
54    * <ul>    * <ul>
55    * <li>getAscent()</li>    * <li>getAscent()</li>
56    * <li>getDescent()</li>    * <li>getDescent()</li>
# Line 64  import java.text.CharacterIterator; Line 64  import java.text.CharacterIterator;
64    */    */
65  public abstract class FontMetrics implements java.io.Serializable  public abstract class FontMetrics implements java.io.Serializable
66  {  {
67      // Serialization constant.
68      private static final long serialVersionUID = 1681126225205050147L;
69    
70  /*    /**
71   * Static Variables     * This is the font for which metrics will be returned.
72   */     */
73      protected Font font;
74  // Serialization constant  
75  private static final long serialVersionUID = 1681126225205050147L;    /**
76       * Initializes a new instance of <code>FontMetrics</code> for the
77  /*************************************************************************/     * specified font.
78       *
79  /*     * @param font The font to return metric information for.
80   * Instance Variables     */
81   */    protected FontMetrics(Font font)
82      {
83  /**      this.font = font;
84    * This is the font for which metrics will be returned.    }
   */  
 protected Font font;  
   
 /*************************************************************************/  
   
 /*  
  * Constructors  
  */  
   
 /**  
   * Initializes a new instance of <code>FontMetrics</code> for the  
   * specified font.  
   *  
   * @param font The font to return metric information for.  
   */  
 protected  
 FontMetrics(Font font)  
 {  
   this.font = font;  
 }  
   
 /*************************************************************************/  
   
 /*  
  * Instance Methods  
  */  
   
 /**  
   * Returns the font that this object is creating metric information fo.  
   *  
   * @return The font for this object.  
   */  
 public Font  
 getFont()  
 {  
   return(font);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the leading, or spacing between lines, for this font.  
   *  
   * @return The font leading.  
   */  
 public int  
 getLeading()  
 {  
   return(0);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the ascent of the font, which is the distance from the base  
   * to the top of the majority of characters in the set.  Some characters  
   * can exceed this value however.  
   *  
   * @return The font ascent.  
   */  
 public int  
 getAscent()  
 {  
   return(1);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the descent of the font, which is the distance from the base  
   * to the bottom of the majority of characters in the set.  Some characters  
   * can exceed this value however.  
   *  
   * @return The font descent.  
   */  
 public int  
 getDescent()  
 {  
   return(1);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the height of a line in this font.  This will be the sum  
   * of the leading, the ascent, and the descent.  
   *  
   * @return The height of the font.  
   */  
 public int  
 getHeight()  
 {  
   return(getAscent() + getDescent() + getLeading());  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the maximum ascent value.  This is the maximum distance any  
   * character in the font rised above the baseline.  
   *  
   * @return The maximum ascent for this font.  
   */  
 public int  
 getMaxAscent()  
 {  
   return(getAscent());  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns the maximum descent value.  This is the maximum distance any  
   * character in the font extends below the baseline.  
   *  
   * @return The maximum descent for this font.  
   */  
 public int  
 getMaxDescent()  
 {  
   return getMaxDecent ();  
 }  
   
 /*************************************************************************/  
85    
86  /**    /**
87    * Returns the maximum descent value.  This is the maximum distance any     * Returns the font that this object is creating metric information fo.
88    * character in the font extends below the baseline.     *
89    *     * @return The font for this object.
90    * @return The maximum descent for this font.     */
91    *    public Font getFont()
92    * @deprecated This method is deprecated in favor of    {
93    * <code>getMaxDescent()</code>.      return font;
94    */    }
 public int  
 getMaxDecent()  
 {  
   return getDescent ();  
 }  
95    
96  /*************************************************************************/    /**
97       * Returns the leading, or spacing between lines, for this font.
98       *
99       * @return The font leading.
100       */
101      public int getLeading()
102      {
103        return 0;
104      }
105    
106  /**    /**
107    * Returns the width of the widest character in the font.     * Returns the ascent of the font, which is the distance from the base
108    *     * to the top of the majority of characters in the set.  Some characters
109    * @return The width of the widest character in the font.     * can exceed this value however.
110    */     *
111  public int     * @return The font ascent.
112  getMaxAdvance()     */
113  {    public int getAscent()
114    return(-1);    {
115  }      return 1;
116      }
117    
118  /*************************************************************************/    /**
119       * Returns the descent of the font, which is the distance from the base
120       * to the bottom of the majority of characters in the set.  Some characters
121       * can exceed this value however.
122       *
123       * @return The font descent.
124       */
125      public int getDescent()
126      {
127        return 1;
128      }
129    
130  /**    /**
131    * Returns the width of the specified character.     * Returns the height of a line in this font.  This will be the sum
132    *     * of the leading, the ascent, and the descent.
133    * @param ch The character to return the width of.     *
134    *     * @return The height of the font.
135    * @return The width of the specified character.     */
136    */    public int getHeight()
137  public int    {
138  charWidth(int ch)      return getAscent() + getDescent() + getLeading();
139  {    }
   return(charWidth((char)ch));  
 }  
140    
141  /*************************************************************************/    /**
142       * Returns the maximum ascent value.  This is the maximum distance any
143       * character in the font rised above the baseline.
144       *
145       * @return The maximum ascent for this font.
146       */
147      public int getMaxAscent()
148      {
149        return getAscent();
150      }
151    
152  /**    /**
153    * Returns the width of the specified character.     * Returns the maximum descent value.  This is the maximum distance any
154    *     * character in the font extends below the baseline.
155    * @param ch The character to return the width of.     *
156    *     * @return The maximum descent for this font.
157    * @return The width of the specified character.     */
158    */    public int getMaxDescent()
159  public int    {
160  charWidth(char ch)      return getMaxDecent();
161  {    }
   return(1);  
 }  
162    
163  /*************************************************************************/    /**
164       * Returns the maximum descent value.  This is the maximum distance any
165       * character in the font extends below the baseline.
166       *
167       * @return The maximum descent for this font.
168       *
169       * @deprecated This method is deprecated in favor of
170       * <code>getMaxDescent()</code>.
171       */
172      public int getMaxDecent()
173      {
174        return getDescent();
175      }
176    
177  /**    /**
178    * Returns the total width of the specified string     * Returns the width of the widest character in the font.
179    *     *
180    * @param str The string to return the width of.     * @return The width of the widest character in the font.
181    *     */
182    * @return The width of the string.    public int getMaxAdvance()
183    */    {
184  public int      return -1;
185  stringWidth(String str)    }
 {  
   char[] buf = new char[str.length()];  
   str.getChars(0, str.length(), buf, 0);  
186    
187    return(charsWidth(buf, 0, buf.length));    /**
188  }     * Returns the width of the specified character.
189       *
190       * @param ch The character to return the width of.
191       *
192       * @return The width of the specified character.
193       */
194      public int charWidth(int ch)
195      {
196        return charWidth((char) ch);
197      }
198    
199  /*************************************************************************/    /**
200       * Returns the width of the specified character.
201       *
202       * @param ch The character to return the width of.
203       *
204       * @return The width of the specified character.
205       */
206      public int charWidth(char ch)
207      {
208        return 1;
209      }
210    
211  /**    /**
212    * Returns the total width of the specified character array.     * Returns the total width of the specified string
213    *     *
214    * @param buf The character array containing the data.     * @param str The string to return the width of.
215    * @param offset The offset into the array to start calculating from.     *
216    * @param len The total number of bytes to process.     * @return The width of the string.
217    *     */
218    * @return The width of the requested characters.    public int stringWidth(String str)
219    */    {
220  public int      char[] buf = new char[str.length()];
221  charsWidth(char buf[], int offset, int len)      str.getChars(0, str.length(), buf, 0);
 {  
   int total_width = 0;  
   for (int i = offset; i < len; i++)  
     total_width += charWidth(buf[i]);  
   return(total_width);  
 }  
222    
223  /*************************************************************************/      return charsWidth(buf, 0, buf.length);
224      }
225    
226  /**    /**
227    * Returns the total width of the specified byte array.     * Returns the total width of the specified character array.
228    *     *
229    * @param buf The byte array containing the data.     * @param buf The character array containing the data.
230    * @param offset The offset into the array to start calculating from.     * @param offset The offset into the array to start calculating from.
231    * @param len The total number of bytes to process.     * @param len The total number of bytes to process.
232    *     *
233    * @return The width of the requested characters.     * @return The width of the requested characters.
234    */     */
235  public int    public int charsWidth(char[] buf, int offset, int len)
236  bytesWidth(byte buf[], int offset, int len)    {
237  {      int total_width = 0;
238    int total_width = 0;      for (int i = offset; i < len; i++)
239    for (int i = offset; i < len; i++)        total_width += charWidth(buf[i]);
240      total_width = charWidth((char)buf[i]);      return total_width;
241      }
242    
243    return(total_width);    /**
244  }     * Returns the total width of the specified byte array.
245       *
246       * @param buf The byte array containing the data.
247       * @param offset The offset into the array to start calculating from.
248       * @param len The total number of bytes to process.
249       *
250       * @return The width of the requested characters.
251       */
252      public int bytesWidth(byte[] buf, int offset, int len)
253      {
254        int total_width = 0;
255        for (int i = offset; i < len; i++)
256          total_width = charWidth((char) buf[i]);
257    
258  /*************************************************************************/      return total_width;
259      }
260    
261  /**    /**
262    * Returns the widths of the first 256 characters in the font.     * Returns the widths of the first 256 characters in the font.
263    *     *
264    * @return The widths of the first 256 characters in the font.     * @return The widths of the first 256 characters in the font.
265    */     */
266  public int[]    public int[] getWidths()
 getWidths()  
 {  
   int [] result = new int[256];  
   for(char i = 0; i < 256; i++)  
267    {    {
268      result[i]= charWidth(i);      int[] result = new int[256];
269        for (char i = 0; i < 256; i++)
270          result[i] = charWidth(i);
271        return result;
272    }    }
   return(result);  
 }  
   
 /*************************************************************************/  
   
 /**  
   * Returns a string representation of this object.  
   *  
   * @return A string representation of this object.  
   */  
 public String  
 toString()  
 {  
   return (this.getClass() + "[font=" + font + ",ascent=" + getAscent()  
           + ",descent=" + getDescent() + ",height=" + getHeight() + "]");  
 }  
273    
274      /**
275       * Returns a string representation of this object.
276       *
277       * @return A string representation of this object.
278       */
279      public String toString()
280      {
281        return (this.getClass() + "[font=" + font + ",ascent=" + getAscent()
282                + ",descent=" + getDescent() + ",height=" + getHeight() + "]");
283      }
284    
285  // Generic FontRenderContext used when getLineMetrics is called with a    // Generic FontRenderContext used when getLineMetrics is called with a
286  // plain Graphics object.    // plain Graphics object.
287  private static final FontRenderContext gRC = new FontRenderContext(null,    private static final FontRenderContext gRC = new FontRenderContext(null,
288                                                                     false,                                                                       false,
289                                                                     false);                                                                       false);
290    
291  /**    /**
292    * Returns a {@link LineMetrics} object constructed with the     * Returns a {@link LineMetrics} object constructed with the
293    * specified text and the {@link FontRenderContext} of the Graphics     * specified text and the {@link FontRenderContext} of the Graphics
294    * object when it is an instance of Graphics2D or a generic     * object when it is an instance of Graphics2D or a generic
295    * FontRenderContext with a null transform, not anti-aliased and not     * FontRenderContext with a null transform, not anti-aliased and not
296    * using fractional metrics.     * using fractional metrics.
297    *     *
298    * @param text The string to calculate metrics from.     * @param text The string to calculate metrics from.
299    * @param g The Graphics object that will be used.     * @param g The Graphics object that will be used.
300    *     *
301    * @return A new {@link LineMetrics} object.     * @return A new {@link LineMetrics} object.
302    */     */
303  public LineMetrics getLineMetrics(String text, Graphics g)    public LineMetrics getLineMetrics(String text, Graphics g)
304  {    {
305    return getLineMetrics(text, 0, text.length(), g);      return getLineMetrics(text, 0, text.length(), g);
306  }    }
307    
308  /**    /**
309   * Returns a {@link LineMetrics} object constructed with the     * Returns a {@link LineMetrics} object constructed with the
310   * specified text and the {@link FontRenderContext} of the Graphics     * specified text and the {@link FontRenderContext} of the Graphics
311   * object when it is an instance of Graphics2D or a generic     * object when it is an instance of Graphics2D or a generic
312   * FontRenderContext with a null transform, not anti-aliased and not     * FontRenderContext with a null transform, not anti-aliased and not
313   * using fractional metrics.     * using fractional metrics.
314   *     *
315   * @param text The string to calculate metrics from.     * @param text The string to calculate metrics from.
316   * @param begin Index of first character in <code>text</code> to measure.     * @param begin Index of first character in <code>text</code> to measure.
317   * @param limit Index of last character in <code>text</code> to measure.     * @param limit Index of last character in <code>text</code> to measure.
318   * @param g The Graphics object that will be used.     * @param g The Graphics object that will be used.
319   *     *
320   * @return A new {@link LineMetrics} object.     * @return A new {@link LineMetrics} object.
321   *     *
322   * @throws IndexOutOfBoundsException if the range [begin, limit] is     * @throws IndexOutOfBoundsException if the range [begin, limit] is
323   * invalid in <code>text</code>.     * invalid in <code>text</code>.
324   */     */
325  public LineMetrics getLineMetrics(String text, int begin,    public LineMetrics getLineMetrics(String text, int begin, int limit,
326                                    int limit, Graphics g)                                      Graphics g)
327  {    {
328    FontRenderContext rc;      FontRenderContext rc;
329    if (g instanceof Graphics2D)      if (g instanceof Graphics2D)
330      rc = ((Graphics2D) g).getFontRenderContext();        rc = ((Graphics2D) g).getFontRenderContext();
331    else      else
332      rc = gRC;        rc = gRC;
333    return font.getLineMetrics(text, begin, limit, rc);      return font.getLineMetrics(text, begin, limit, rc);
334  }    }
335    
336  /**    /**
337   * Returns a {@link LineMetrics} object constructed with the     * Returns a {@link LineMetrics} object constructed with the
338   * specified text and the {@link FontRenderContext} of the Graphics     * specified text and the {@link FontRenderContext} of the Graphics
339   * object when it is an instance of Graphics2D or a generic     * object when it is an instance of Graphics2D or a generic
340   * FontRenderContext with a null transform, not anti-aliased and not     * FontRenderContext with a null transform, not anti-aliased and not
341   * using fractional metrics.     * using fractional metrics.
342   *     *
343   * @param chars The string to calculate metrics from.     * @param chars The string to calculate metrics from.
344   * @param begin Index of first character in <code>text</code> to measure.     * @param begin Index of first character in <code>text</code> to measure.
345   * @param limit Index of last character in <code>text</code> to measure.     * @param limit Index of last character in <code>text</code> to measure.
346   * @param g The Graphics object that will be used.     * @param g The Graphics object that will be used.
347   *     *
348   * @return A new {@link LineMetrics} object.     * @return A new {@link LineMetrics} object.
349   *     *
350   * @throws IndexOutOfBoundsException if the range [begin, limit] is     * @throws IndexOutOfBoundsException if the range [begin, limit] is
351   * invalid in <code>text</code>.     * invalid in <code>text</code>.
352   */     */
353  public LineMetrics getLineMetrics(char[] chars, int begin,    public LineMetrics getLineMetrics(char[] chars, int begin, int limit,
354                                    int limit, Graphics g)                                      Graphics g)
355  {    {
356    FontRenderContext rc;      FontRenderContext rc;
357    if (g instanceof Graphics2D)      if (g instanceof Graphics2D)
358      rc = ((Graphics2D) g).getFontRenderContext();        rc = ((Graphics2D) g).getFontRenderContext();
359    else      else
360      rc = gRC;        rc = gRC;
361    return font.getLineMetrics(chars, begin, limit, rc);      return font.getLineMetrics(chars, begin, limit, rc);
362  }    }
363    
364  /**    /**
365   * Returns a {@link LineMetrics} object constructed with the     * Returns a {@link LineMetrics} object constructed with the
366   * specified text and the {@link FontRenderContext} of the Graphics     * specified text and the {@link FontRenderContext} of the Graphics
367   * object when it is an instance of Graphics2D or a generic     * object when it is an instance of Graphics2D or a generic
368   * FontRenderContext with a null transform, not anti-aliased and not     * FontRenderContext with a null transform, not anti-aliased and not
369   * using fractional metrics.     * using fractional metrics.
370   *     *
371   * @param ci An iterator over the string to calculate metrics from.     * @param ci An iterator over the string to calculate metrics from.
372   * @param begin Index of first character in <code>text</code> to measure.     * @param begin Index of first character in <code>text</code> to measure.
373   * @param limit Index of last character in <code>text</code> to measure.     * @param limit Index of last character in <code>text</code> to measure.
374   * @param g The Graphics object that will be used.     * @param g The Graphics object that will be used.
375   *     *
376   * @return A new {@link LineMetrics} object.     * @return A new {@link LineMetrics} object.
377   *     *
378   * @throws IndexOutOfBoundsException if the range [begin, limit] is     * @throws IndexOutOfBoundsException if the range [begin, limit] is
379   * invalid in <code>text</code>.     * invalid in <code>text</code>.
380   */     */
381  public LineMetrics getLineMetrics(CharacterIterator ci, int begin,    public LineMetrics getLineMetrics(CharacterIterator ci, int begin,
382                                    int limit, Graphics g)                                      int limit, Graphics g)
383  {    {
384    FontRenderContext rc;      FontRenderContext rc;
385    if (g instanceof Graphics2D)      if (g instanceof Graphics2D)
386      rc = ((Graphics2D) g).getFontRenderContext();        rc = ((Graphics2D) g).getFontRenderContext();
387    else      else
388      rc = gRC;        rc = gRC;
389    return font.getLineMetrics(ci, begin, limit, rc);      return font.getLineMetrics(ci, begin, limit, rc);
390  }    }
391    
392    public Rectangle2D getStringBounds(String str, Graphics context)    public Rectangle2D getStringBounds(String str, Graphics context)
393    {    {
394      return font.getStringBounds(str, getFontRenderContext(context));      return font.getStringBounds(str, getFontRenderContext(context));
395    }    }
396    
397    public Rectangle2D getStringBounds(String str, int beginIndex, int limit, Graphics context)    public Rectangle2D getStringBounds(String str, int beginIndex, int limit,
398                                         Graphics context)
399    {    {
400      return font.getStringBounds(str, beginIndex, limit, getFontRenderContext(context));      return font.getStringBounds(str, beginIndex, limit,
401                                    getFontRenderContext(context));
402    }    }
403    
404    public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, Graphics context)    public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit,
405                                         Graphics context)
406    {    {
407      return font.getStringBounds(chars, beginIndex, limit, getFontRenderContext(context));      return font.getStringBounds(chars, beginIndex, limit,
408                                    getFontRenderContext(context));
409    }    }
410    
411    public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, Graphics context)    public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex,
412                                         int limit, Graphics context)
413    {    {
414      return font.getStringBounds(ci, beginIndex, limit, getFontRenderContext(context));      return font.getStringBounds(ci, beginIndex, limit,
415                                    getFontRenderContext(context));
416    }    }
417    
418    private FontRenderContext getFontRenderContext(Graphics context)    private FontRenderContext getFontRenderContext(Graphics context)
# Line 487  public LineMetrics getLineMetrics(Charac Line 420  public LineMetrics getLineMetrics(Charac
420      if (context instanceof Graphics2D)      if (context instanceof Graphics2D)
421        return ((Graphics2D) context).getFontRenderContext();        return ((Graphics2D) context).getFontRenderContext();
422    
423      throw new UnsupportedOperationException("not implemented for Graphics contexts");      return gRC;
424    }    }
425  }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26