List of usage examples for org.eclipse.swt.graphics FontData setHeight
public void setHeight(int height)
From source file:org.eclipse.swt.snippets.Snippet367.java
private static void paintImage2(GC gc, Point size, int f) { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle(0, 0, size.x, size.y); // Scale line width, corner roundness, and font size. // Caveat: line width expands in all directions, so the origin also has to move. gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.setLineWidth(f);// ww w .j a v a 2s . co m gc.drawRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); FontData fontData = gc.getFont().getFontData()[0]; fontData.setHeight(fontData.getHeight() * f); Font font = new Font(gc.getDevice(), fontData); try { gc.setFont(font); gc.drawText(fontData.toString(), 10 * f, 10 * f, true); } finally { font.dispose(); } }
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.// w w w.ja va 2 s . c o m * <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; }