org.eclipse.swt.graphics
Class Font

java.lang.Object
  extended by org.eclipse.swt.graphics.Resource
      extended by org.eclipse.swt.graphics.Font

public final class Font
extends Resource

Instances of this class manage operating system resources that define how text looks when it is displayed. Fonts may be constructed by providing a device and either name, size and style information or a FontData object which encapsulates this data.

Application code must explicitly invoke the Font.dispose() method to release the operating system resources managed by each instance when those instances are no longer required.

See Also:
FontData, Font snippets, SWT Examples: GraphicsExample, PaintExample, Sample code and further information

Field Summary
 long handle
          the handle to the OS font resource (Warning: This field is platform dependent)
 
Constructor Summary
Font(Device device, FontData fd)
          Constructs a new font given a device and font data which describes the desired font's appearance.
Font(Device device, FontData[] fds)
          Constructs a new font given a device and an array of font data which describes the desired font's appearance.
Font(Device device, java.lang.String name, int height, int style)
          Constructs a new font given a device, a font name, the height of the desired font in points, and a font style.
 
Method Summary
 boolean equals(java.lang.Object object)
          Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.
 FontData[] getFontData()
          Returns an array of FontDatas representing the receiver.
static Font gtk_new(Device device, long handle)
          Invokes platform specific functionality to allocate a new font.
 int hashCode()
          Returns an integer hash code for the receiver.
 boolean isDisposed()
          Returns true if the font has been disposed, and false otherwise.
 java.lang.String toString()
          Returns a string containing a concise, human-readable description of the receiver.
 
Methods inherited from class org.eclipse.swt.graphics.Resource
dispose, getDevice
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

handle

public long handle
the handle to the OS font resource (Warning: This field is platform dependent)

IMPORTANT: This field is not part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms and should never be accessed from application code.

Constructor Detail

Font

public Font(Device device,
            FontData fd)
Constructs a new font given a device and font data which describes the desired font's appearance.

You must dispose the font when it is no longer required.

Parameters:
device - the device to create the font on
fd - the FontData that describes the desired font (must not be null)
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • ERROR_NULL_ARGUMENT - if the fd argument is null
SWTError -
  • ERROR_NO_HANDLES - if a font could not be created from the given font data

Font

public Font(Device device,
            FontData[] fds)
Constructs a new font given a device and an array of font data which describes the desired font's appearance.

You must dispose the font when it is no longer required.

Parameters:
device - the device to create the font on
fds - the array of FontData that describes the desired font (must not be null)
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • ERROR_NULL_ARGUMENT - if the fds argument is null
  • ERROR_INVALID_ARGUMENT - if the length of fds is zero
  • ERROR_NULL_ARGUMENT - if any fd in the array is null
SWTError -
  • ERROR_NO_HANDLES - if a font could not be created from the given font data
Since:
2.1

Font

public Font(Device device,
            java.lang.String name,
            int height,
            int style)
Constructs a new font given a device, a font name, the height of the desired font in points, and a font style.

You must dispose the font when it is no longer required.

Parameters:
device - the device to create the font on
name - the name of the font (must not be null)
height - the font height in points
style - a bit or combination of NORMAL, BOLD, ITALIC
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if device is null and there is no current device
  • ERROR_NULL_ARGUMENT - if the name argument is null
  • ERROR_INVALID_ARGUMENT - if the height is negative
SWTError -
  • ERROR_NO_HANDLES - if a font could not be created from the given arguments
Method Detail

equals

public boolean equals(java.lang.Object object)
Compares the argument to the receiver, and returns true if they represent the same object using a class specific comparison.

Overrides:
equals in class java.lang.Object
Parameters:
object - the object to compare with this object
Returns:
true if the object is the same as this object and false otherwise
See Also:
hashCode()

getFontData

public FontData[] getFontData()
Returns an array of FontDatas representing the receiver. On Windows, only one FontData will be returned per font. On X however, a Font object may be composed of multiple X fonts. To support this case, we return an array of font data objects.

Returns:
an array of font data objects describing the receiver
Throws:
SWTException -
  • ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed

gtk_new

public static Font gtk_new(Device device,
                           long handle)
Invokes platform specific functionality to allocate a new font.

IMPORTANT: This method is not part of the public API for Font. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

Parameters:
device - the device on which to allocate the color
handle - the handle for the font

hashCode

public int hashCode()
Returns an integer hash code for the receiver. Any two objects that return true when passed to equals must return the same value for this method.

Overrides:
hashCode in class java.lang.Object
Returns:
the receiver's hash
See Also:
equals(java.lang.Object)

isDisposed

public boolean isDisposed()
Returns true if the font has been disposed, and false otherwise.

This method gets the dispose state for the font. When a font has been disposed, it is an error to invoke any other method using the font.

Specified by:
isDisposed in class Resource
Returns:
true when the font is disposed and false otherwise

toString

public java.lang.String toString()
Returns a string containing a concise, human-readable description of the receiver.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the receiver