List of usage examples for org.eclipse.swt.graphics FontData getHeight
public int getHeight()
From source file:StyledTextStyleRangeFont.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text);/*from www . ja va2s . c o m*/ FontData data = styledText.getFont().getFontData()[0]; Font font1 = new Font(display, data.getName(), data.getHeight() * 2, data.getStyle()); Font font2 = new Font(display, data.getName(), data.getHeight() * 4 / 5, data.getStyle()); StyleRange[] styles = new StyleRange[8]; styles[0] = new StyleRange(); styles[0].font = font1; styles[1] = new StyleRange(); styles[1].rise = data.getHeight() / 3; styles[2] = new StyleRange(); styles[2].background = display.getSystemColor(SWT.COLOR_GREEN); styles[3] = new StyleRange(); styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA); styles[4] = new StyleRange(); styles[4].font = font2; styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE); ; styles[4].underline = true; styles[5] = new StyleRange(); styles[5].rise = -data.getHeight() / 3; styles[5].strikeout = true; styles[5].underline = true; styles[6] = new StyleRange(); styles[6].font = font1; styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW); styles[6].background = display.getSystemColor(SWT.COLOR_BLUE); styles[7] = new StyleRange(); styles[7].rise = data.getHeight() / 3; styles[7].underline = true; styles[7].fontStyle = SWT.BOLD; styles[7].foreground = display.getSystemColor(SWT.COLOR_RED); styles[7].background = display.getSystemColor(SWT.COLOR_BLACK); int[] ranges = new int[] { 16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7 }; styledText.setStyleRanges(ranges, styles); shell.setSize(300, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } font1.dispose(); font2.dispose(); display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet211.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 211"); shell.setLayout(new FillLayout()); StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text);/*from w ww . jav a 2 s .c o m*/ FontData data = styledText.getFont().getFontData()[0]; Font font1 = new Font(display, data.getName(), data.getHeight() * 2, data.getStyle()); Font font2 = new Font(display, data.getName(), data.getHeight() * 4 / 5, data.getStyle()); StyleRange[] styles = new StyleRange[8]; styles[0] = new StyleRange(); styles[0].font = font1; styles[1] = new StyleRange(); styles[1].rise = data.getHeight() / 3; styles[2] = new StyleRange(); styles[2].background = display.getSystemColor(SWT.COLOR_GREEN); styles[3] = new StyleRange(); styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA); styles[4] = new StyleRange(); styles[4].font = font2; styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE); styles[4].underline = true; styles[5] = new StyleRange(); styles[5].rise = -data.getHeight() / 3; styles[5].strikeout = true; styles[5].underline = true; styles[6] = new StyleRange(); styles[6].font = font1; styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW); styles[6].background = display.getSystemColor(SWT.COLOR_BLUE); styles[7] = new StyleRange(); styles[7].rise = data.getHeight() / 3; styles[7].underline = true; styles[7].fontStyle = SWT.BOLD; styles[7].foreground = display.getSystemColor(SWT.COLOR_RED); styles[7].background = display.getSystemColor(SWT.COLOR_BLACK); int[] ranges = new int[] { 16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7 }; styledText.setStyleRanges(ranges, styles); shell.setSize(300, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } font1.dispose(); font2.dispose(); display.dispose(); }
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);/*from w ww .j a v a 2 s . c om*/ 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 an awt font by converting as much information * as possible from the provided swt <code>FontData</code>. * <p>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. *///from w w w . jav a2 s.c o m 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); }
From source file:PrintingExample.java
void print(Printer printer) { if (printer.startJob("Text")) { // the string is the job name - shows up in the printer's job list Rectangle clientArea = printer.getClientArea(); Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); leftMargin = dpi.x + trim.x; // one inch from left side of paper rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper topMargin = dpi.y + trim.y; // one inch from top edge of paper bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper /* Create a buffer for computing tab width. */ int tabSize = 4; // is tab width a user setting in your UI? StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); tabs = tabBuffer.toString();//from w w w . jav a2 s . c o m /* Create printer GC, and create and set the printer font & foreground color. */ gc = new GC(printer); FontData fontData = font.getFontData()[0]; printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont); tabWidth = gc.stringExtent(tabs).x; lineHeight = gc.getFontMetrics().getHeight(); RGB rgb = foregroundColor.getRGB(); printerForegroundColor = new Color(printer, rgb); gc.setForeground(printerForegroundColor); rgb = backgroundColor.getRGB(); printerBackgroundColor = new Color(printer, rgb); gc.setBackground(printerBackgroundColor); /* Print text to current gc using word wrap */ printText(); printer.endJob(); /* Cleanup graphics resources used in printing */ printerFont.dispose(); printerForegroundColor.dispose(); printerBackgroundColor.dispose(); gc.dispose(); } }
From source file:Snippet133.java
void print(Printer printer) { if (printer.startJob("Text")) { // the string is the job name - shows up // in the printer's job list Rectangle clientArea = printer.getClientArea(); Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); leftMargin = dpi.x + trim.x; // one inch from left side of paper rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one // inch/*from ww w. j a v a 2s . c om*/ // from // right // side // of // paper topMargin = dpi.y + trim.y; // one inch from top edge of paper bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one // inch // from // bottom // edge // of // paper /* Create a buffer for computing tab width. */ int tabSize = 4; // is tab width a user setting in your UI? StringBuffer tabBuffer = new StringBuffer(tabSize); for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); tabs = tabBuffer.toString(); /* * Create printer GC, and create and set the printer font & * foreground color. */ gc = new GC(printer); FontData fontData = font.getFontData()[0]; printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont); tabWidth = gc.stringExtent(tabs).x; lineHeight = gc.getFontMetrics().getHeight(); RGB rgb = foregroundColor.getRGB(); printerForegroundColor = new Color(printer, rgb); gc.setForeground(printerForegroundColor); rgb = backgroundColor.getRGB(); printerBackgroundColor = new Color(printer, rgb); gc.setBackground(printerBackgroundColor); /* Print text to current gc using word wrap */ printText(); printer.endJob(); /* Cleanup graphics resources used in printing */ printerFont.dispose(); printerForegroundColor.dispose(); printerBackgroundColor.dispose(); gc.dispose(); } }
From source file:org.gumtree.vis.core.internal.SWTChartComposite.java
protected void print(Printer printer) { org.eclipse.swt.graphics.Rectangle clientArea = printer.getClientArea(); org.eclipse.swt.graphics.Rectangle trim = printer.computeTrim(0, 0, 0, 0); Point dpi = printer.getDPI(); int leftMargin = dpi.x + trim.x; // one inch from left side of paper int rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper int topMargin = dpi.y + trim.y; // one inch from top edge of paper int bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper // printer.startJob("Plot"); GC gc = new GC(printer); Font font = new Font(getDisplay(), "Courier", 10, SWT.NORMAL); FontData fontData = font.getFontData()[0]; Font printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); gc.setFont(printerFont);/* ww w .jav a 2 s.com*/ // printer.startPage(); try { paint(gc, leftMargin, topMargin, rightMargin, bottomMargin); } catch (Exception e) { e.printStackTrace(); MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.ICON_ERROR); messageBox.setMessage(e.getMessage()); messageBox.open(); } finally { gc.dispose(); font.dispose(); printerFont.dispose(); System.out.println("printer job finished"); printer.endPage(); printer.endJob(); printer.dispose(); } }
From source file:org.eclipse.swt.examples.texteditor.TextEditor.java
void updateToolBar() { styleState = 0;/*from w w w .ja va 2 s.c om*/ link = null; boolean bold = false, italic = false; Font font = null; int offset = styledText.getCaretOffset(); StyleRange range = offset > 0 ? styledText.getStyleRangeAtOffset(offset - 1) : null; if (range != null) { if (range.font != null) { font = range.font; FontData[] fds = font.getFontData(); for (FontData fd : fds) { int fontStyle = fd.getStyle(); if (!bold && (fontStyle & SWT.BOLD) != 0) bold = true; if (!italic && (fontStyle & SWT.ITALIC) != 0) italic = true; } } else { bold = (range.fontStyle & SWT.BOLD) != 0; italic = (range.fontStyle & SWT.ITALIC) != 0; } if (range.foreground != null) { styleState |= FOREGROUND; if (textForeground != range.foreground) { disposeResource(textForeground); textForeground = range.foreground; } } if (range.background != null) { styleState |= BACKGROUND; if (textBackground != range.background) { disposeResource(textBackground); textBackground = range.background; } } if (range.underline) { switch (range.underlineStyle) { case SWT.UNDERLINE_SINGLE: styleState |= UNDERLINE_SINGLE; break; case SWT.UNDERLINE_DOUBLE: styleState |= UNDERLINE_DOUBLE; break; case SWT.UNDERLINE_SQUIGGLE: styleState |= UNDERLINE_SQUIGGLE; break; case SWT.UNDERLINE_ERROR: styleState |= UNDERLINE_ERROR; break; case SWT.UNDERLINE_LINK: styleState |= UNDERLINE_LINK; link = (String) range.data; break; } if (range.underlineStyle != SWT.UNDERLINE_LINK) { underlineSingleItem.setSelection((styleState & UNDERLINE_SINGLE) != 0); underlineDoubleItem.setSelection((styleState & UNDERLINE_DOUBLE) != 0); underlineErrorItem.setSelection((styleState & UNDERLINE_ERROR) != 0); underlineSquiggleItem.setSelection((styleState & UNDERLINE_SQUIGGLE) != 0); disposeResource(underlineColor); underlineColor = range.underlineColor; } } if (range.strikeout) { styleState |= STRIKEOUT; disposeResource(strikeoutColor); strikeoutColor = range.strikeoutColor; } if (range.borderStyle != SWT.NONE) { switch (range.borderStyle) { case SWT.BORDER_SOLID: styleState |= BORDER_SOLID; break; case SWT.BORDER_DASH: styleState |= BORDER_DASH; break; case SWT.BORDER_DOT: styleState |= BORDER_DOT; break; } borderSolidItem.setSelection((styleState & BORDER_SOLID) != 0); borderDashItem.setSelection((styleState & BORDER_DASH) != 0); borderDotItem.setSelection((styleState & BORDER_DOT) != 0); disposeResource(borderColor); borderColor = range.borderColor; } } boldControl.setSelection(bold); italicControl.setSelection(italic); FontData fontData = font != null ? font.getFontData()[0] : styledText.getFont().getFontData()[0]; int index = 0; int count = fontNameControl.getItemCount(); String fontName = fontData.getName(); while (index < count) { if (fontNameControl.getItem(index).equals(fontName)) { fontNameControl.select(index); break; } index++; } index = 0; count = fontSizeControl.getItemCount(); int fontSize = fontData.getHeight(); while (index < count) { int size = Integer.parseInt(fontSizeControl.getItem(index)); if (fontSize == size) { fontSizeControl.select(index); break; } if (size > fontSize) { fontSizeControl.add(String.valueOf(fontSize), index); fontSizeControl.select(index); break; } index++; } disposeResource(textFont); textFont = font; int lineIndex = styledText.getLineAtOffset(offset); int alignment = styledText.getLineAlignment(lineIndex); leftAlignmentItem.setSelection((alignment & SWT.LEFT) != 0); centerAlignmentItem.setSelection((alignment & SWT.CENTER) != 0); rightAlignmentItem.setSelection((alignment & SWT.RIGHT) != 0); boolean justify = styledText.getLineJustify(lineIndex); justifyAlignmentItem.setSelection(justify); }