347 * The TrueType format was extended to become the OpenType
348 * format, which adds support for fonts with Postscript outlines,
349 * this tag therefore references these fonts, as well as those
350 * with TrueType outlines.
351 * @since 1.3
352 */
353
354 public static final int TRUETYPE_FONT = 0;
355
356 /**
357 * Identify a font resource of type TYPE1.
358 * Used to specify a Type1 font resource to the
359 * {@link #createFont} method.
360 * @since 1.5
361 */
362 public static final int TYPE1_FONT = 1;
363
364 /**
365 * The logical name of this <code>Font</code>, as passed to the
366 * constructor.
367 * @since JDK1.0
368 *
369 * @serial
370 * @see #getName
371 */
372 protected String name;
373
374 /**
375 * The style of this <code>Font</code>, as passed to the constructor.
376 * This style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
377 * @since JDK1.0
378 *
379 * @serial
380 * @see #getStyle()
381 */
382 protected int style;
383
384 /**
385 * The point size of this <code>Font</code>, rounded to integer.
386 * @since JDK1.0
387 *
388 * @serial
389 * @see #getSize()
390 */
391 protected int size;
392
393 /**
394 * The point size of this <code>Font</code> in <code>float</code>.
395 *
396 * @serial
397 * @see #getSize()
398 * @see #getSize2D()
399 */
400 protected float pointSize;
401
402 /**
403 * The platform specific font information.
404 */
405 private transient FontPeer peer;
406 private transient long pData; // native JDK1.1 font pointer
419 * This is true if the font transform is not identity. It
420 * is used to avoid unnecessary instantiation of an AffineTransform.
421 */
422 private transient boolean nonIdentityTx;
423
424 /*
425 * A cached value used when a transform is required for internal
426 * use. This must not be exposed to callers since AffineTransform
427 * is mutable.
428 */
429 private static final AffineTransform identityTx = new AffineTransform();
430
431 /*
432 * JDK 1.1 serialVersionUID
433 */
434 private static final long serialVersionUID = -4206021311591459213L;
435
436 /**
437 * Gets the peer of this <code>Font</code>.
438 * @return the peer of the <code>Font</code>.
439 * @since JDK1.1
440 * @deprecated Font rendering is now platform independent.
441 */
442 @Deprecated
443 public FontPeer getPeer(){
444 return getPeer_NoClientCode();
445 }
446 // NOTE: This method is called by privileged threads.
447 // We implement this functionality in a package-private method
448 // to insure that it cannot be overridden by client subclasses.
449 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
450 @SuppressWarnings("deprecation")
451 final FontPeer getPeer_NoClientCode() {
452 if(peer == null) {
453 Toolkit tk = Toolkit.getDefaultToolkit();
454 this.peer = tk.getFontPeer(name, style);
455 }
456 return peer;
457 }
458
459 /**
545 * <p>
546 *
547 * @param name the font name. This can be a font face name or a font
548 * family name, and may represent either a logical font or a physical
549 * font found in this {@code GraphicsEnvironment}.
550 * The family names for logical fonts are: Dialog, DialogInput,
551 * Monospaced, Serif, or SansSerif. Pre-defined String constants exist
552 * for all of these names, for example, {@code DIALOG}. If {@code name} is
553 * {@code null}, the <em>logical font name</em> of the new
554 * {@code Font} as returned by {@code getName()} is set to
555 * the name "Default".
556 * @param style the style constant for the {@code Font}
557 * The style argument is an integer bitmask that may
558 * be {@code PLAIN}, or a bitwise union of {@code BOLD} and/or
559 * {@code ITALIC} (for example, {@code ITALIC} or {@code BOLD|ITALIC}).
560 * If the style argument does not conform to one of the expected
561 * integer bitmasks then the style is set to {@code PLAIN}.
562 * @param size the point size of the {@code Font}
563 * @see GraphicsEnvironment#getAllFonts
564 * @see GraphicsEnvironment#getAvailableFontFamilyNames
565 * @since JDK1.0
566 */
567 public Font(String name, int style, int size) {
568 this.name = (name != null) ? name : "Default";
569 this.style = (style & ~0x03) == 0 ? style : 0;
570 this.size = size;
571 this.pointSize = size;
572 }
573
574 private Font(String name, int style, float sizePts) {
575 this.name = (name != null) ? name : "Default";
576 this.style = (style & ~0x03) == 0 ? style : 0;
577 this.size = (int)(sizePts + 0.5);
578 this.pointSize = sizePts;
579 }
580
581 /* This constructor is used by deriveFont when attributes is null */
582 private Font(String name, int style, float sizePts,
583 boolean created, Font2DHandle handle) {
584 this(name, style, sizePts);
585 this.createdFont = created;
1163 1.0591564f,
1164 };
1165
1166 /**
1167 * Returns the family name of this <code>Font</code>.
1168 *
1169 * <p>The family name of a font is font specific. Two fonts such as
1170 * Helvetica Italic and Helvetica Bold have the same family name,
1171 * <i>Helvetica</i>, whereas their font face names are
1172 * <i>Helvetica Bold</i> and <i>Helvetica Italic</i>. The list of
1173 * available family names may be obtained by using the
1174 * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
1175 *
1176 * <p>Use <code>getName</code> to get the logical name of the font.
1177 * Use <code>getFontName</code> to get the font face name of the font.
1178 * @return a <code>String</code> that is the family name of this
1179 * <code>Font</code>.
1180 *
1181 * @see #getName
1182 * @see #getFontName
1183 * @since JDK1.1
1184 */
1185 public String getFamily() {
1186 return getFamily_NoClientCode();
1187 }
1188 // NOTE: This method is called by privileged threads.
1189 // We implement this functionality in a package-private
1190 // method to insure that it cannot be overridden by client
1191 // subclasses.
1192 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1193 final String getFamily_NoClientCode() {
1194 return getFamily(Locale.getDefault());
1195 }
1196
1197 /**
1198 * Returns the family name of this <code>Font</code>, localized for
1199 * the specified locale.
1200 *
1201 * <p>The family name of a font is font specific. Two fonts such as
1202 * Helvetica Italic and Helvetica Bold have the same family name,
1203 * <i>Helvetica</i>, whereas their font face names are
1223 /**
1224 * Returns the postscript name of this <code>Font</code>.
1225 * Use <code>getFamily</code> to get the family name of the font.
1226 * Use <code>getFontName</code> to get the font face name of the font.
1227 * @return a <code>String</code> representing the postscript name of
1228 * this <code>Font</code>.
1229 * @since 1.2
1230 */
1231 public String getPSName() {
1232 return getFont2D().getPostscriptName();
1233 }
1234
1235 /**
1236 * Returns the logical name of this <code>Font</code>.
1237 * Use <code>getFamily</code> to get the family name of the font.
1238 * Use <code>getFontName</code> to get the font face name of the font.
1239 * @return a <code>String</code> representing the logical name of
1240 * this <code>Font</code>.
1241 * @see #getFamily
1242 * @see #getFontName
1243 * @since JDK1.0
1244 */
1245 public String getName() {
1246 return name;
1247 }
1248
1249 /**
1250 * Returns the font face name of this <code>Font</code>. For example,
1251 * Helvetica Bold could be returned as a font face name.
1252 * Use <code>getFamily</code> to get the family name of the font.
1253 * Use <code>getName</code> to get the logical name of the font.
1254 * @return a <code>String</code> representing the font face name of
1255 * this <code>Font</code>.
1256 * @see #getFamily
1257 * @see #getName
1258 * @since 1.2
1259 */
1260 public String getFontName() {
1261 return getFontName(Locale.getDefault());
1262 }
1263
1269 * @param l a locale for which to get the font face name
1270 * @return a <code>String</code> representing the font face name,
1271 * localized for the specified locale.
1272 * @see #getFamily
1273 * @see java.util.Locale
1274 */
1275 public String getFontName(Locale l) {
1276 if (l == null) {
1277 throw new NullPointerException("null locale doesn't mean default");
1278 }
1279 return getFont2D().getFontName(l);
1280 }
1281
1282 /**
1283 * Returns the style of this <code>Font</code>. The style can be
1284 * PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
1285 * @return the style of this <code>Font</code>
1286 * @see #isPlain
1287 * @see #isBold
1288 * @see #isItalic
1289 * @since JDK1.0
1290 */
1291 public int getStyle() {
1292 return style;
1293 }
1294
1295 /**
1296 * Returns the point size of this <code>Font</code>, rounded to
1297 * an integer.
1298 * Most users are familiar with the idea of using <i>point size</i> to
1299 * specify the size of glyphs in a font. This point size defines a
1300 * measurement between the baseline of one line to the baseline of the
1301 * following line in a single spaced text document. The point size is
1302 * based on <i>typographic points</i>, approximately 1/72 of an inch.
1303 * <p>
1304 * The Java(tm)2D API adopts the convention that one point is
1305 * equivalent to one unit in user coordinates. When using a
1306 * normalized transform for converting user space coordinates to
1307 * device space coordinates 72 user
1308 * space units equal 1 inch in device space. In this case one point
1309 * is 1/72 of an inch.
1310 * @return the point size of this <code>Font</code> in 1/72 of an
1311 * inch units.
1312 * @see #getSize2D
1313 * @see GraphicsConfiguration#getDefaultTransform
1314 * @see GraphicsConfiguration#getNormalizingTransform
1315 * @since JDK1.0
1316 */
1317 public int getSize() {
1318 return size;
1319 }
1320
1321 /**
1322 * Returns the point size of this <code>Font</code> in
1323 * <code>float</code> value.
1324 * @return the point size of this <code>Font</code> as a
1325 * <code>float</code> value.
1326 * @see #getSize
1327 * @since 1.2
1328 */
1329 public float getSize2D() {
1330 return pointSize;
1331 }
1332
1333 /**
1334 * Indicates whether or not this <code>Font</code> object's style is
1335 * PLAIN.
1336 * @return <code>true</code> if this <code>Font</code> has a
1337 * PLAIN style;
1338 * <code>false</code> otherwise.
1339 * @see java.awt.Font#getStyle
1340 * @since JDK1.0
1341 */
1342 public boolean isPlain() {
1343 return style == 0;
1344 }
1345
1346 /**
1347 * Indicates whether or not this <code>Font</code> object's style is
1348 * BOLD.
1349 * @return <code>true</code> if this <code>Font</code> object's
1350 * style is BOLD;
1351 * <code>false</code> otherwise.
1352 * @see java.awt.Font#getStyle
1353 * @since JDK1.0
1354 */
1355 public boolean isBold() {
1356 return (style & BOLD) != 0;
1357 }
1358
1359 /**
1360 * Indicates whether or not this <code>Font</code> object's style is
1361 * ITALIC.
1362 * @return <code>true</code> if this <code>Font</code> object's
1363 * style is ITALIC;
1364 * <code>false</code> otherwise.
1365 * @see java.awt.Font#getStyle
1366 * @since JDK1.0
1367 */
1368 public boolean isItalic() {
1369 return (style & ITALIC) != 0;
1370 }
1371
1372 /**
1373 * Indicates whether or not this <code>Font</code> object has a
1374 * transform that affects its size in addition to the Size
1375 * attribute.
1376 * @return <code>true</code> if this <code>Font</code> object
1377 * has a non-identity AffineTransform attribute.
1378 * <code>false</code> otherwise.
1379 * @see java.awt.Font#getTransform
1380 * @since 1.4
1381 */
1382 public boolean isTransformed() {
1383 return nonIdentityTx;
1384 }
1385
1386 /**
1467 *
1468 * <p>
1469 * The default size is 12 and the default style is PLAIN.
1470 * If <code>str</code> does not specify a valid size, the returned
1471 * <code>Font</code> has a size of 12. If <code>str</code> does not
1472 * specify a valid style, the returned Font has a style of PLAIN.
1473 * If you do not specify a valid font name in
1474 * the <code>str</code> argument, this method will return
1475 * a font with the family name "Dialog".
1476 * To determine what font family names are available on
1477 * your system, use the
1478 * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
1479 * If <code>str</code> is <code>null</code>, a new <code>Font</code>
1480 * is returned with the family name "Dialog", a size of 12 and a
1481 * PLAIN style.
1482 * @param str the name of the font, or <code>null</code>
1483 * @return the <code>Font</code> object that <code>str</code>
1484 * describes, or a new default <code>Font</code> if
1485 * <code>str</code> is <code>null</code>.
1486 * @see #getFamily
1487 * @since JDK1.1
1488 */
1489 public static Font decode(String str) {
1490 String fontName = str;
1491 String styleName = "";
1492 int fontSize = 12;
1493 int fontStyle = Font.PLAIN;
1494
1495 if (str == null) {
1496 return new Font(DIALOG, fontStyle, fontSize);
1497 }
1498
1499 int lastHyphen = str.lastIndexOf('-');
1500 int lastSpace = str.lastIndexOf(' ');
1501 char sepChar = (lastHyphen > lastSpace) ? '-' : ' ';
1502 int sizeIndex = str.lastIndexOf(sepChar);
1503 int styleIndex = str.lastIndexOf(sepChar, sizeIndex-1);
1504 int strlen = str.length();
1505
1506 if (sizeIndex > 0 && sizeIndex+1 < strlen) {
1507 try {
1578 * @return the <code>Font</code> value of the property.
1579 * @throws NullPointerException if nm is null.
1580 * @see #decode(String)
1581 */
1582 public static Font getFont(String nm, Font font) {
1583 String str = null;
1584 try {
1585 str =System.getProperty(nm);
1586 } catch(SecurityException e) {
1587 }
1588 if (str == null) {
1589 return font;
1590 }
1591 return decode ( str );
1592 }
1593
1594 transient int hash;
1595 /**
1596 * Returns a hashcode for this <code>Font</code>.
1597 * @return a hashcode value for this <code>Font</code>.
1598 * @since JDK1.0
1599 */
1600 public int hashCode() {
1601 if (hash == 0) {
1602 hash = name.hashCode() ^ style ^ size;
1603 /* It is possible many fonts differ only in transform.
1604 * So include the transform in the hash calculation.
1605 * nonIdentityTx is set whenever there is a transform in
1606 * 'values'. The tests for null are required because it can
1607 * also be set for other reasons.
1608 */
1609 if (nonIdentityTx &&
1610 values != null && values.getTransform() != null) {
1611 hash ^= values.getTransform().hashCode();
1612 }
1613 }
1614 return hash;
1615 }
1616
1617 /**
1618 * Compares this <code>Font</code> object to the specified
1619 * <code>Object</code>.
1620 * @param obj the <code>Object</code> to compare
1621 * @return <code>true</code> if the objects are the same
1622 * or if the argument is a <code>Font</code> object
1623 * describing the same font as this object;
1624 * <code>false</code> otherwise.
1625 * @since JDK1.0
1626 */
1627 public boolean equals(Object obj) {
1628 if (obj == this) {
1629 return true;
1630 }
1631
1632 if (obj != null) {
1633 try {
1634 Font font = (Font)obj;
1635 if (size == font.size &&
1636 style == font.style &&
1637 nonIdentityTx == font.nonIdentityTx &&
1638 hasLayoutAttributes == font.hasLayoutAttributes &&
1639 pointSize == font.pointSize &&
1640 name.equals(font.name)) {
1641
1642 /* 'values' is usually initialized lazily, except when
1643 * the font is constructed from a Map, or derived using
1644 * a Map or other values. So if only one font has
1645 * the field initialized we need to initialize it in
1650 return true;
1651 } else {
1652 return getAttributeValues().equals(font.values);
1653 }
1654 } else {
1655 return values.equals(font.getAttributeValues());
1656 }
1657 }
1658 }
1659 catch (ClassCastException e) {
1660 }
1661 }
1662 return false;
1663 }
1664
1665 /**
1666 * Converts this <code>Font</code> object to a <code>String</code>
1667 * representation.
1668 * @return a <code>String</code> representation of this
1669 * <code>Font</code> object.
1670 * @since JDK1.0
1671 */
1672 // NOTE: This method may be called by privileged threads.
1673 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1674 public String toString() {
1675 String strStyle;
1676
1677 if (isBold()) {
1678 strStyle = isItalic() ? "bolditalic" : "bold";
1679 } else {
1680 strStyle = isItalic() ? "italic" : "plain";
1681 }
1682
1683 return getClass().getName() + "[family=" + getFamily() + ",name=" + name + ",style=" +
1684 strStyle + ",size=" + size + "]";
1685 } // toString()
1686
1687
1688 /** Serialization support. A <code>readObject</code>
1689 * method is neccessary because the constructor creates
1690 * the font's peer, and we can't serialize the peer.
|
347 * The TrueType format was extended to become the OpenType
348 * format, which adds support for fonts with Postscript outlines,
349 * this tag therefore references these fonts, as well as those
350 * with TrueType outlines.
351 * @since 1.3
352 */
353
354 public static final int TRUETYPE_FONT = 0;
355
356 /**
357 * Identify a font resource of type TYPE1.
358 * Used to specify a Type1 font resource to the
359 * {@link #createFont} method.
360 * @since 1.5
361 */
362 public static final int TYPE1_FONT = 1;
363
364 /**
365 * The logical name of this <code>Font</code>, as passed to the
366 * constructor.
367 * @since 1.0
368 *
369 * @serial
370 * @see #getName
371 */
372 protected String name;
373
374 /**
375 * The style of this <code>Font</code>, as passed to the constructor.
376 * This style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
377 * @since 1.0
378 *
379 * @serial
380 * @see #getStyle()
381 */
382 protected int style;
383
384 /**
385 * The point size of this <code>Font</code>, rounded to integer.
386 * @since 1.0
387 *
388 * @serial
389 * @see #getSize()
390 */
391 protected int size;
392
393 /**
394 * The point size of this <code>Font</code> in <code>float</code>.
395 *
396 * @serial
397 * @see #getSize()
398 * @see #getSize2D()
399 */
400 protected float pointSize;
401
402 /**
403 * The platform specific font information.
404 */
405 private transient FontPeer peer;
406 private transient long pData; // native JDK1.1 font pointer
419 * This is true if the font transform is not identity. It
420 * is used to avoid unnecessary instantiation of an AffineTransform.
421 */
422 private transient boolean nonIdentityTx;
423
424 /*
425 * A cached value used when a transform is required for internal
426 * use. This must not be exposed to callers since AffineTransform
427 * is mutable.
428 */
429 private static final AffineTransform identityTx = new AffineTransform();
430
431 /*
432 * JDK 1.1 serialVersionUID
433 */
434 private static final long serialVersionUID = -4206021311591459213L;
435
436 /**
437 * Gets the peer of this <code>Font</code>.
438 * @return the peer of the <code>Font</code>.
439 * @since 1.1
440 * @deprecated Font rendering is now platform independent.
441 */
442 @Deprecated
443 public FontPeer getPeer(){
444 return getPeer_NoClientCode();
445 }
446 // NOTE: This method is called by privileged threads.
447 // We implement this functionality in a package-private method
448 // to insure that it cannot be overridden by client subclasses.
449 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
450 @SuppressWarnings("deprecation")
451 final FontPeer getPeer_NoClientCode() {
452 if(peer == null) {
453 Toolkit tk = Toolkit.getDefaultToolkit();
454 this.peer = tk.getFontPeer(name, style);
455 }
456 return peer;
457 }
458
459 /**
545 * <p>
546 *
547 * @param name the font name. This can be a font face name or a font
548 * family name, and may represent either a logical font or a physical
549 * font found in this {@code GraphicsEnvironment}.
550 * The family names for logical fonts are: Dialog, DialogInput,
551 * Monospaced, Serif, or SansSerif. Pre-defined String constants exist
552 * for all of these names, for example, {@code DIALOG}. If {@code name} is
553 * {@code null}, the <em>logical font name</em> of the new
554 * {@code Font} as returned by {@code getName()} is set to
555 * the name "Default".
556 * @param style the style constant for the {@code Font}
557 * The style argument is an integer bitmask that may
558 * be {@code PLAIN}, or a bitwise union of {@code BOLD} and/or
559 * {@code ITALIC} (for example, {@code ITALIC} or {@code BOLD|ITALIC}).
560 * If the style argument does not conform to one of the expected
561 * integer bitmasks then the style is set to {@code PLAIN}.
562 * @param size the point size of the {@code Font}
563 * @see GraphicsEnvironment#getAllFonts
564 * @see GraphicsEnvironment#getAvailableFontFamilyNames
565 * @since 1.0
566 */
567 public Font(String name, int style, int size) {
568 this.name = (name != null) ? name : "Default";
569 this.style = (style & ~0x03) == 0 ? style : 0;
570 this.size = size;
571 this.pointSize = size;
572 }
573
574 private Font(String name, int style, float sizePts) {
575 this.name = (name != null) ? name : "Default";
576 this.style = (style & ~0x03) == 0 ? style : 0;
577 this.size = (int)(sizePts + 0.5);
578 this.pointSize = sizePts;
579 }
580
581 /* This constructor is used by deriveFont when attributes is null */
582 private Font(String name, int style, float sizePts,
583 boolean created, Font2DHandle handle) {
584 this(name, style, sizePts);
585 this.createdFont = created;
1163 1.0591564f,
1164 };
1165
1166 /**
1167 * Returns the family name of this <code>Font</code>.
1168 *
1169 * <p>The family name of a font is font specific. Two fonts such as
1170 * Helvetica Italic and Helvetica Bold have the same family name,
1171 * <i>Helvetica</i>, whereas their font face names are
1172 * <i>Helvetica Bold</i> and <i>Helvetica Italic</i>. The list of
1173 * available family names may be obtained by using the
1174 * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
1175 *
1176 * <p>Use <code>getName</code> to get the logical name of the font.
1177 * Use <code>getFontName</code> to get the font face name of the font.
1178 * @return a <code>String</code> that is the family name of this
1179 * <code>Font</code>.
1180 *
1181 * @see #getName
1182 * @see #getFontName
1183 * @since 1.1
1184 */
1185 public String getFamily() {
1186 return getFamily_NoClientCode();
1187 }
1188 // NOTE: This method is called by privileged threads.
1189 // We implement this functionality in a package-private
1190 // method to insure that it cannot be overridden by client
1191 // subclasses.
1192 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1193 final String getFamily_NoClientCode() {
1194 return getFamily(Locale.getDefault());
1195 }
1196
1197 /**
1198 * Returns the family name of this <code>Font</code>, localized for
1199 * the specified locale.
1200 *
1201 * <p>The family name of a font is font specific. Two fonts such as
1202 * Helvetica Italic and Helvetica Bold have the same family name,
1203 * <i>Helvetica</i>, whereas their font face names are
1223 /**
1224 * Returns the postscript name of this <code>Font</code>.
1225 * Use <code>getFamily</code> to get the family name of the font.
1226 * Use <code>getFontName</code> to get the font face name of the font.
1227 * @return a <code>String</code> representing the postscript name of
1228 * this <code>Font</code>.
1229 * @since 1.2
1230 */
1231 public String getPSName() {
1232 return getFont2D().getPostscriptName();
1233 }
1234
1235 /**
1236 * Returns the logical name of this <code>Font</code>.
1237 * Use <code>getFamily</code> to get the family name of the font.
1238 * Use <code>getFontName</code> to get the font face name of the font.
1239 * @return a <code>String</code> representing the logical name of
1240 * this <code>Font</code>.
1241 * @see #getFamily
1242 * @see #getFontName
1243 * @since 1.0
1244 */
1245 public String getName() {
1246 return name;
1247 }
1248
1249 /**
1250 * Returns the font face name of this <code>Font</code>. For example,
1251 * Helvetica Bold could be returned as a font face name.
1252 * Use <code>getFamily</code> to get the family name of the font.
1253 * Use <code>getName</code> to get the logical name of the font.
1254 * @return a <code>String</code> representing the font face name of
1255 * this <code>Font</code>.
1256 * @see #getFamily
1257 * @see #getName
1258 * @since 1.2
1259 */
1260 public String getFontName() {
1261 return getFontName(Locale.getDefault());
1262 }
1263
1269 * @param l a locale for which to get the font face name
1270 * @return a <code>String</code> representing the font face name,
1271 * localized for the specified locale.
1272 * @see #getFamily
1273 * @see java.util.Locale
1274 */
1275 public String getFontName(Locale l) {
1276 if (l == null) {
1277 throw new NullPointerException("null locale doesn't mean default");
1278 }
1279 return getFont2D().getFontName(l);
1280 }
1281
1282 /**
1283 * Returns the style of this <code>Font</code>. The style can be
1284 * PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
1285 * @return the style of this <code>Font</code>
1286 * @see #isPlain
1287 * @see #isBold
1288 * @see #isItalic
1289 * @since 1.0
1290 */
1291 public int getStyle() {
1292 return style;
1293 }
1294
1295 /**
1296 * Returns the point size of this <code>Font</code>, rounded to
1297 * an integer.
1298 * Most users are familiar with the idea of using <i>point size</i> to
1299 * specify the size of glyphs in a font. This point size defines a
1300 * measurement between the baseline of one line to the baseline of the
1301 * following line in a single spaced text document. The point size is
1302 * based on <i>typographic points</i>, approximately 1/72 of an inch.
1303 * <p>
1304 * The Java(tm)2D API adopts the convention that one point is
1305 * equivalent to one unit in user coordinates. When using a
1306 * normalized transform for converting user space coordinates to
1307 * device space coordinates 72 user
1308 * space units equal 1 inch in device space. In this case one point
1309 * is 1/72 of an inch.
1310 * @return the point size of this <code>Font</code> in 1/72 of an
1311 * inch units.
1312 * @see #getSize2D
1313 * @see GraphicsConfiguration#getDefaultTransform
1314 * @see GraphicsConfiguration#getNormalizingTransform
1315 * @since 1.0
1316 */
1317 public int getSize() {
1318 return size;
1319 }
1320
1321 /**
1322 * Returns the point size of this <code>Font</code> in
1323 * <code>float</code> value.
1324 * @return the point size of this <code>Font</code> as a
1325 * <code>float</code> value.
1326 * @see #getSize
1327 * @since 1.2
1328 */
1329 public float getSize2D() {
1330 return pointSize;
1331 }
1332
1333 /**
1334 * Indicates whether or not this <code>Font</code> object's style is
1335 * PLAIN.
1336 * @return <code>true</code> if this <code>Font</code> has a
1337 * PLAIN style;
1338 * <code>false</code> otherwise.
1339 * @see java.awt.Font#getStyle
1340 * @since 1.0
1341 */
1342 public boolean isPlain() {
1343 return style == 0;
1344 }
1345
1346 /**
1347 * Indicates whether or not this <code>Font</code> object's style is
1348 * BOLD.
1349 * @return <code>true</code> if this <code>Font</code> object's
1350 * style is BOLD;
1351 * <code>false</code> otherwise.
1352 * @see java.awt.Font#getStyle
1353 * @since 1.0
1354 */
1355 public boolean isBold() {
1356 return (style & BOLD) != 0;
1357 }
1358
1359 /**
1360 * Indicates whether or not this <code>Font</code> object's style is
1361 * ITALIC.
1362 * @return <code>true</code> if this <code>Font</code> object's
1363 * style is ITALIC;
1364 * <code>false</code> otherwise.
1365 * @see java.awt.Font#getStyle
1366 * @since 1.0
1367 */
1368 public boolean isItalic() {
1369 return (style & ITALIC) != 0;
1370 }
1371
1372 /**
1373 * Indicates whether or not this <code>Font</code> object has a
1374 * transform that affects its size in addition to the Size
1375 * attribute.
1376 * @return <code>true</code> if this <code>Font</code> object
1377 * has a non-identity AffineTransform attribute.
1378 * <code>false</code> otherwise.
1379 * @see java.awt.Font#getTransform
1380 * @since 1.4
1381 */
1382 public boolean isTransformed() {
1383 return nonIdentityTx;
1384 }
1385
1386 /**
1467 *
1468 * <p>
1469 * The default size is 12 and the default style is PLAIN.
1470 * If <code>str</code> does not specify a valid size, the returned
1471 * <code>Font</code> has a size of 12. If <code>str</code> does not
1472 * specify a valid style, the returned Font has a style of PLAIN.
1473 * If you do not specify a valid font name in
1474 * the <code>str</code> argument, this method will return
1475 * a font with the family name "Dialog".
1476 * To determine what font family names are available on
1477 * your system, use the
1478 * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
1479 * If <code>str</code> is <code>null</code>, a new <code>Font</code>
1480 * is returned with the family name "Dialog", a size of 12 and a
1481 * PLAIN style.
1482 * @param str the name of the font, or <code>null</code>
1483 * @return the <code>Font</code> object that <code>str</code>
1484 * describes, or a new default <code>Font</code> if
1485 * <code>str</code> is <code>null</code>.
1486 * @see #getFamily
1487 * @since 1.1
1488 */
1489 public static Font decode(String str) {
1490 String fontName = str;
1491 String styleName = "";
1492 int fontSize = 12;
1493 int fontStyle = Font.PLAIN;
1494
1495 if (str == null) {
1496 return new Font(DIALOG, fontStyle, fontSize);
1497 }
1498
1499 int lastHyphen = str.lastIndexOf('-');
1500 int lastSpace = str.lastIndexOf(' ');
1501 char sepChar = (lastHyphen > lastSpace) ? '-' : ' ';
1502 int sizeIndex = str.lastIndexOf(sepChar);
1503 int styleIndex = str.lastIndexOf(sepChar, sizeIndex-1);
1504 int strlen = str.length();
1505
1506 if (sizeIndex > 0 && sizeIndex+1 < strlen) {
1507 try {
1578 * @return the <code>Font</code> value of the property.
1579 * @throws NullPointerException if nm is null.
1580 * @see #decode(String)
1581 */
1582 public static Font getFont(String nm, Font font) {
1583 String str = null;
1584 try {
1585 str =System.getProperty(nm);
1586 } catch(SecurityException e) {
1587 }
1588 if (str == null) {
1589 return font;
1590 }
1591 return decode ( str );
1592 }
1593
1594 transient int hash;
1595 /**
1596 * Returns a hashcode for this <code>Font</code>.
1597 * @return a hashcode value for this <code>Font</code>.
1598 * @since 1.0
1599 */
1600 public int hashCode() {
1601 if (hash == 0) {
1602 hash = name.hashCode() ^ style ^ size;
1603 /* It is possible many fonts differ only in transform.
1604 * So include the transform in the hash calculation.
1605 * nonIdentityTx is set whenever there is a transform in
1606 * 'values'. The tests for null are required because it can
1607 * also be set for other reasons.
1608 */
1609 if (nonIdentityTx &&
1610 values != null && values.getTransform() != null) {
1611 hash ^= values.getTransform().hashCode();
1612 }
1613 }
1614 return hash;
1615 }
1616
1617 /**
1618 * Compares this <code>Font</code> object to the specified
1619 * <code>Object</code>.
1620 * @param obj the <code>Object</code> to compare
1621 * @return <code>true</code> if the objects are the same
1622 * or if the argument is a <code>Font</code> object
1623 * describing the same font as this object;
1624 * <code>false</code> otherwise.
1625 * @since 1.0
1626 */
1627 public boolean equals(Object obj) {
1628 if (obj == this) {
1629 return true;
1630 }
1631
1632 if (obj != null) {
1633 try {
1634 Font font = (Font)obj;
1635 if (size == font.size &&
1636 style == font.style &&
1637 nonIdentityTx == font.nonIdentityTx &&
1638 hasLayoutAttributes == font.hasLayoutAttributes &&
1639 pointSize == font.pointSize &&
1640 name.equals(font.name)) {
1641
1642 /* 'values' is usually initialized lazily, except when
1643 * the font is constructed from a Map, or derived using
1644 * a Map or other values. So if only one font has
1645 * the field initialized we need to initialize it in
1650 return true;
1651 } else {
1652 return getAttributeValues().equals(font.values);
1653 }
1654 } else {
1655 return values.equals(font.getAttributeValues());
1656 }
1657 }
1658 }
1659 catch (ClassCastException e) {
1660 }
1661 }
1662 return false;
1663 }
1664
1665 /**
1666 * Converts this <code>Font</code> object to a <code>String</code>
1667 * representation.
1668 * @return a <code>String</code> representation of this
1669 * <code>Font</code> object.
1670 * @since 1.0
1671 */
1672 // NOTE: This method may be called by privileged threads.
1673 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1674 public String toString() {
1675 String strStyle;
1676
1677 if (isBold()) {
1678 strStyle = isItalic() ? "bolditalic" : "bold";
1679 } else {
1680 strStyle = isItalic() ? "italic" : "plain";
1681 }
1682
1683 return getClass().getName() + "[family=" + getFamily() + ",name=" + name + ",style=" +
1684 strStyle + ",size=" + size + "]";
1685 } // toString()
1686
1687
1688 /** Serialization support. A <code>readObject</code>
1689 * method is neccessary because the constructor creates
1690 * the font's peer, and we can't serialize the peer.
|