public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text); FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 16, SWT.BOLD); styledText.setFont(font); styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); styledText.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle rect = styledText.getClientArea(); Image newImage = new Image(display, 1, Math.max(1, rect.height)); GC gc = new GC(newImage); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true); gc.dispose(); styledText.setBackgroundImage(newImage); if (oldImage != null) oldImage.dispose(); oldImage = newImage; } }); shell.setSize(700, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (oldImage != null) oldImage.dispose(); font.dispose(); display.dispose(); }
/** * Returns a bold version of the given {@link Font}. * * @param baseFont the {@link Font} for which a bold version is desired * @return the bold version of the given {@link Font} */ public static Font getBoldFont(Font baseFont) { Font font = m_fontToBoldFontMap.get(baseFont); if (font == null) { FontData fontDatas[] = baseFont.getFontData(); FontData data = fontDatas[0]; font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD); m_fontToBoldFontMap.put(baseFont, font); } return font; }
/** * Returns <code>FontData</code> objects which describe the fonts that match the given arguments. * If the <code>faceName</code> is null, all fonts will be returned. * * @param faceName the name of the font to look for, or null * @param scalable if true only scalable fonts are returned, otherwise only non-scalable fonts are * returned. * @return the matching font data * @exception SWTException * <ul> * <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed * </ul> */ public FontData[] getFontList(String faceName, boolean scalable) { checkDevice(); if (!scalable) return new FontData[0]; int typefaces; if (faceName != null) { int length = faceName.length(); char[] chars = new char[length + 1]; faceName.getChars(0, length, chars, 0); int str = OS.gcnew_String(chars); int fontFamily = OS.gcnew_FontFamily(str); typefaces = OS.FontFamily_GetTypefaces(fontFamily); OS.GCHandle_Free(fontFamily); OS.GCHandle_Free(str); } else { typefaces = OS.Fonts_SystemTypefaces(); } int count = OS.TypefaceCollection_Count(typefaces); int index = 0; FontData[] result = new FontData[count]; int enumerator = OS.TypefaceCollection_GetEnumerator(typefaces); while (OS.IEnumerator_MoveNext(enumerator)) { int typeface = OS.TypefaceCollection_Current(enumerator); int fontFamily = OS.Typeface_FontFamily(typeface); int style = OS.Typeface_Style(typeface); int weight = OS.Typeface_Weight(typeface); int stretch = OS.Typeface_Stretch(typeface); int str = OS.FontFamily_Source(fontFamily); int charArray = OS.String_ToCharArray(str); char[] chars = new char[OS.String_Length(str)]; OS.memcpy(chars, charArray, chars.length * 2); int fontStyle = OS.FontStyles_Normal; if (OS.Object_Equals(style, OS.FontStyles_Italic)) fontStyle = OS.FontStyles_Italic; if (OS.Object_Equals(style, OS.FontStyles_Oblique)) fontStyle = OS.FontStyles_Oblique; FontData data = FontData.wpf_new( new String(chars), fontStyle, OS.FontWeight_ToOpenTypeWeight(weight), OS.FontStretch_ToOpenTypeStretch(stretch), 0); OS.GCHandle_Free(charArray); OS.GCHandle_Free(str); OS.GCHandle_Free(fontFamily); OS.GCHandle_Free(style); OS.GCHandle_Free(weight); OS.GCHandle_Free(stretch); OS.GCHandle_Free(typeface); result[index++] = data; } OS.GCHandle_Free(enumerator); OS.GCHandle_Free(typefaces); return result; }
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); 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(); } }