List of usage examples for org.eclipse.swt.graphics FontData setStyle
public void setStyle(int style)
SWT
constants NORMAL, BOLD and ITALIC. 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 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; }
From source file:widgetTest3.java
/** * Fonts ** */ public static void drawItalic(Composite composite, GC gc) { // Get Display instance Display display = composite.getDisplay(); // Fetch system font Font systemFont = display.getSystemFont(); // FontData objects contain the font properties. // With some operating systems a font may possess multiple // FontData instances. We only use the first one. FontData[] data = systemFont.getFontData(); FontData data0 = data[0]; // Set the font style to italic data0.setStyle(SWT.ITALIC); // Create a new font Font italicFont = new Font(display, data0); // Set the new font in the graphics context gc.setFont(italicFont);/*from ww w . jav a2 s . c o m*/ // TODO: call italicFont.dispose() in the DisposeListener // of composite // Draw text at position (4,4) with a transparent background (true). gc.drawText("Hello", 4, 4, true); }
From source file:org.eclipse.swt.examples.texteditor.TextEditor.java
void setStyle(int style, int start, int length) { if (length == 0) return;/*from w ww. j a v a2 s .c om*/ /* Create new style range */ StyleRange newRange = new StyleRange(); if ((style & FONT) != 0) { newRange.font = textFont; } if ((style & FONT_STYLE) != 0) { newRange.fontStyle = style & FONT_STYLE; } if ((style & FOREGROUND) != 0) { newRange.foreground = textForeground; } if ((style & BACKGROUND) != 0) { newRange.background = textBackground; } if ((style & BASELINE_UP) != 0) newRange.rise++; if ((style & BASELINE_DOWN) != 0) newRange.rise--; if ((style & STRIKEOUT) != 0) { newRange.strikeout = true; newRange.strikeoutColor = strikeoutColor; } if ((style & UNDERLINE) != 0) { newRange.underline = true; newRange.underlineColor = underlineColor; switch (style & UNDERLINE) { case UNDERLINE_SINGLE: newRange.underlineStyle = SWT.UNDERLINE_SINGLE; break; case UNDERLINE_DOUBLE: newRange.underlineStyle = SWT.UNDERLINE_DOUBLE; break; case UNDERLINE_ERROR: newRange.underlineStyle = SWT.UNDERLINE_ERROR; break; case UNDERLINE_SQUIGGLE: newRange.underlineStyle = SWT.UNDERLINE_SQUIGGLE; break; case UNDERLINE_LINK: newRange.underlineColor = null; if (link != null && link.length() > 0) { newRange.underlineStyle = SWT.UNDERLINE_LINK; newRange.data = link; } else { newRange.underline = false; } break; } } if ((style & BORDER) != 0) { switch (style & BORDER) { case BORDER_DASH: newRange.borderStyle = SWT.BORDER_DASH; break; case BORDER_DOT: newRange.borderStyle = SWT.BORDER_DOT; break; case BORDER_SOLID: newRange.borderStyle = SWT.BORDER_SOLID; break; } newRange.borderColor = borderColor; } int newRangeStart = start; int newRangeLength = length; int[] ranges = styledText.getRanges(start, length); StyleRange[] styles = styledText.getStyleRanges(start, length, false); int maxCount = ranges.length * 2 + 2; int[] newRanges = new int[maxCount]; StyleRange[] newStyles = new StyleRange[maxCount / 2]; int count = 0; for (int i = 0; i < ranges.length; i += 2) { int rangeStart = ranges[i]; int rangeLength = ranges[i + 1]; StyleRange range = styles[i / 2]; if (rangeStart > newRangeStart) { newRangeLength = rangeStart - newRangeStart; newRanges[count] = newRangeStart; newRanges[count + 1] = newRangeLength; newStyles[count / 2] = newRange; count += 2; } newRangeStart = rangeStart + rangeLength; newRangeLength = (start + length) - newRangeStart; /* Create merged style range*/ StyleRange mergedRange = new StyleRange(range); //Note: fontStyle is not copied by the constructor mergedRange.fontStyle = range.fontStyle; if ((style & FONT) != 0) { mergedRange.font = newRange.font; } if ((style & FONT_STYLE) != 0) { mergedRange.fontStyle = range.fontStyle ^ newRange.fontStyle; } if (mergedRange.font != null && ((style & FONT) != 0 || (style & FONT_STYLE) != 0)) { boolean change = false; FontData[] fds = mergedRange.font.getFontData(); for (FontData fd : fds) { if (fd.getStyle() != mergedRange.fontStyle) { fd.setStyle(mergedRange.fontStyle); change = true; } } if (change) { mergedRange.font = new Font(display, fds); } } if ((style & FOREGROUND) != 0) { mergedRange.foreground = newRange.foreground != range.foreground ? newRange.foreground : null; } if ((style & BACKGROUND) != 0) { mergedRange.background = newRange.background != range.background ? newRange.background : null; } if ((style & BASELINE_UP) != 0) mergedRange.rise++; if ((style & BASELINE_DOWN) != 0) mergedRange.rise--; if ((style & STRIKEOUT) != 0) { mergedRange.strikeout = !range.strikeout || range.strikeoutColor != newRange.strikeoutColor; mergedRange.strikeoutColor = mergedRange.strikeout ? newRange.strikeoutColor : null; } if ((style & UNDERLINE) != 0) { if ((style & UNDERLINE_LINK) != 0) { if (link != null && link.length() > 0) { mergedRange.underline = !range.underline || range.underlineStyle != newRange.underlineStyle || range.data != newRange.data; } else { mergedRange.underline = false; } mergedRange.underlineColor = null; } else { mergedRange.underline = !range.underline || range.underlineStyle != newRange.underlineStyle || range.underlineColor != newRange.underlineColor; mergedRange.underlineColor = mergedRange.underline ? newRange.underlineColor : null; } mergedRange.underlineStyle = mergedRange.underline ? newRange.underlineStyle : SWT.NONE; mergedRange.data = mergedRange.underline ? newRange.data : null; } if ((style & BORDER) != 0) { if (range.borderStyle != newRange.borderStyle || range.borderColor != newRange.borderColor) { mergedRange.borderStyle = newRange.borderStyle; mergedRange.borderColor = newRange.borderColor; } else { mergedRange.borderStyle = SWT.NONE; mergedRange.borderColor = null; } } newRanges[count] = rangeStart; newRanges[count + 1] = rangeLength; newStyles[count / 2] = mergedRange; count += 2; } if (newRangeLength > 0) { newRanges[count] = newRangeStart; newRanges[count + 1] = newRangeLength; newStyles[count / 2] = newRange; count += 2; } if (0 < count && count < maxCount) { int[] tmpRanges = new int[count]; StyleRange[] tmpStyles = new StyleRange[count / 2]; System.arraycopy(newRanges, 0, tmpRanges, 0, count); System.arraycopy(newStyles, 0, tmpStyles, 0, count / 2); newRanges = tmpRanges; newStyles = tmpStyles; } styledText.setStyleRanges(start, length, newRanges, newStyles); disposeRanges(styles); }