FontData
object which encapsulate
* the essential data to create a swt font. The data is taken
* from the provided awt Font.
* Generally speaking, given a font size, the returned swt font * will display differently on the screen than the awt one. * Because the SWT toolkit use native graphical resources whenever * it is possible, this fact is platform dependent. To address * this issue, it is possible to enforce the method to return * a font with the same size (or at least as close as possible) * as the awt one. *
When the object is no more used, the user must explicitly
* call the dispose method on the returned font to free the
* operating system resources (the garbage collector won't do it).
*
* @param device The swt device to draw on (display or gc device).
* @param font The awt font from which to get the data.
* @param ensureSameSize A boolean used to enforce the same size
* (in pixels) between the awt font and the newly created swt font.
* @return a FontData
object.
*/
public static FontData toSwtFontData(Device device, java.awt.Font font,
boolean ensureSameSize) {
FontData fontData = new FontData();
fontData.setName(font.getFamily());
// SWT and AWT share the same style constants.
fontData.setStyle(font.getStyle());
// convert the font size (in pt for awt) to height in pixels for swt
int height = (int) Math.round(font.getSize() * 72.0
/ device.getDPI().y);
fontData.setHeight(height);
// hack to ensure the newly created swt fonts will be rendered with the
// same height as the awt one
if (ensureSameSize) {
GC tmpGC = new GC(device);
Font tmpFont = new Font(device, fontData);
tmpGC.setFont(tmpFont);
if (tmpGC.textExtent(Az).x
> DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
while (tmpGC.textExtent(Az).x
> DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
tmpFont.dispose();
height--;
fontData.setHeight(height);
tmpFont = new Font(device, fontData);
tmpGC.setFont(tmpFont);
}
}
else if (tmpGC.textExtent(Az).x
< DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
while (tmpGC.textExtent(Az).x
< DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) {
tmpFont.dispose();
height++;
fontData.setHeight(height);
tmpFont = new Font(device, fontData);
tmpGC.setFont(tmpFont);
}
}
tmpFont.dispose();
tmpGC.dispose();
}
return fontData;
}
/**
* Create an awt font by converting as much information
* as possible from the provided swt FontData
.
*
Generally speaking, given a font size, an swt font will * display differently on the screen than the corresponding awt * one. Because the SWT toolkit use native graphical ressources whenever * it is possible, this fact is platform dependent. To address * this issue, it is possible to enforce the method to return * an awt font with the same height as the swt one. * * @param device The swt device being drawn on (display or gc device). * @param fontData The swt font to convert. * @param ensureSameSize A boolean used to enforce the same size * (in pixels) between the swt font and the newly created awt font. * @return An awt font converted from the provided swt font. */ public static java.awt.Font toAwtFont(Device device, FontData fontData, boolean ensureSameSize) { int height = (int) Math.round(fontData.getHeight() * device.getDPI().y / 72.0); // hack to ensure the newly created awt fonts will be rendered with the // same height as the swt one if (ensureSameSize) { GC tmpGC = new GC(device); Font tmpFont = new Font(device, fontData); tmpGC.setFont(tmpFont); JPanel DUMMY_PANEL = new JPanel(); java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { height--; tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); } } else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { height++; tmpAwtFont = new java.awt.Font(fontData.getName(), fontData.getStyle(), height); } } tmpFont.dispose(); tmpGC.dispose(); } return new java.awt.Font(fontData.getName(), fontData.getStyle(), height); } }