List of usage examples for org.eclipse.swt.graphics FontData FontData
public FontData()
From source file:SWTUtils.java
/** * Create a <code>FontData</code> object which encapsulate * the essential data to create a swt font. The data is taken * from the provided awt Font./*from w w w . ja v a 2s.c om*/ * <p>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. * <p>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 <code>FontData</code> 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; }