Java Code Examples for org.eclipse.swt.graphics.Font#getFontData()

The following examples show how to use org.eclipse.swt.graphics.Font#getFontData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThemeUIComposite.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
private void setDefaultFont(Font newFont) {
	String[] fontIds = { "org.eclipse.jface.textfont",
			"org.eclipse.ui.workbench.texteditor.blockSelectionModeFont" };
	FontData[] newData = newFont.getFontData();
	for (String fontId : fontIds) {
		setFont(fontId, newData);
	}

	newData = newFont.getFontData();
	FontData[] smaller = new FontData[newData.length];
	int j = 0;
	for (FontData fd : newData) {
		int height = fd.getHeight();
		if (height >= 12) {
			fd.setHeight(height - 2);
		} else if (height >= 10) {
			fd.setHeight(height - 1);
		}
		smaller[(j++)] = fd;
	}
	setFont("com.aptana.explorer.font", smaller);
}
 
Example 2
Source File: FontAwesome.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Return a FontAwesome font for SWT.
 * 
 * @param size
 * @return
 */
public static Font getFont(int size) {
	if (!fonts.containsKey(size)) {
		// GetFont() may return null, so handle this case.
		Font font = getFont();
		if (font == null) {
			return null;
		}
		FontData[] data = font.getFontData();
		for (FontData d : data) {
			d.setHeight(size);
		}
		fonts.put(size, new Font(Display.getDefault(), data));
	}
	return fonts.get(size);
}
 
Example 3
Source File: Utils.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Applies a certain font size to a font.
 * 
 * @param font Font to modify
 * @param size New font size
 * @return Font with new font size
 */
public static Font applyFontSize(final Font font, final int size) {
	if (font == null) {
		return null;
	}

	final FontData[] fontDataArray = font.getFontData();
	if (fontDataArray == null) {
		return null;
	}
	for (int index = 0; index < fontDataArray.length; index++) {
	    final FontData fData = fontDataArray[index];
		fData.setHeight(size);
	}

	return new Font(Display.getDefault(), fontDataArray);
}
 
Example 4
Source File: Utils.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Takes a font and gives it the typeface of the given style.
 * 
 * @param font Font to modify
 * @param style the new style for the given font (e.g. SWT.BOLD|SWT.ITALIC)
 * @param size New font size
 * @return Font with the given typeface and size
 */
public static Font applyFontData(final Font font, int style, int size) {
	if (font == null) {
		return null;
	}

	final FontData[] fontDataArray = font.getFontData();
	if (fontDataArray == null) {
		return null;
	}
	for (int index = 0; index < fontDataArray.length; index++) {
	    final FontData fData = fontDataArray[index];
		fData.setStyle(style);
		fData.setHeight(size);
	}

	return new Font(Display.getDefault(), fontDataArray);
}
 
Example 5
Source File: XtextDirectEditManager.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Given a label figure object, this will calculate the correct Font needed
 * to display into screen coordinates, taking into account the current
 * mapmode. This will typically be used by direct edit cell editors that
 * need to display independent of the zoom or any coordinate mapping that is
 * taking place on the drawing surface.
 * 
 * @param label
 *            the label to use for the font calculation
 * @return the <code>Font</code> that is scaled to the screen coordinates.
 *         Note: the returned <code>Font</code> should not be disposed since
 *         it is cached by a common resource manager.
 */
protected Font getScaledFont(IFigure label) {
	Font scaledFont = label.getFont();
	FontData data = scaledFont.getFontData()[0];
	Dimension fontSize = new Dimension(0, MapModeUtil.getMapMode(label).DPtoLP(data.getHeight()));
	label.translateToAbsolute(fontSize);

	if (Math.abs(data.getHeight() - fontSize.height) < 2)
		fontSize.height = data.getHeight();

	try {
		FontDescriptor fontDescriptor = FontDescriptor.createFrom(data);
		cachedFontDescriptors.add(fontDescriptor);
		return getResourceManager().createFont(fontDescriptor);
	} catch (DeviceResourceException e) {
		Trace.catching(DiagramUIPlugin.getInstance(), DiagramUIDebugOptions.EXCEPTIONS_CATCHING, getClass(),
				"getScaledFont", e); //$NON-NLS-1$
		Log.error(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING, "getScaledFont", e); //$NON-NLS-1$
	}
	return JFaceResources.getDefaultFont();
}
 
Example 6
Source File: LabelFigure.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static int getMinimumFontSize( Font ft )
{
	if ( ft != null && ft.getFontData( ).length > 0 )
	{
		return ft.getFontData( )[0].getHeight( );
	}

	return 0;
}
 
Example 7
Source File: GanttToolTip.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private static Font applyBoldFont(final Font font) {
    if (font == null) {
        return null;
    }

    final FontData[] fontDataArray = font.getFontData();
    if (fontDataArray == null) { return null; }
    for (int index = 0; index < fontDataArray.length; index++) {
        final FontData fData = fontDataArray[index];
        fData.setStyle(SWT.BOLD);
    }

    return new Font(Display.getDefault(), fontDataArray);
}
 
Example 8
Source File: ExcelExporter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private String getFontInCSSFormat(Font font) {
	FontData fontData = font.getFontData()[0];
	String fontName = fontData.getName();
	int fontStyle = fontData.getStyle();
	String HTML_STYLES[] = new String[] { "NORMAL", "BOLD", "ITALIC" };

	return String.format("font: %s; font-family: %s",
	                     fontStyle <= 2 ? HTML_STYLES[fontStyle] : HTML_STYLES[0],
	                     fontName);
}
 
Example 9
Source File: SWTGraphicUtil.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Build a font from a given control. Useful if we just want a bold label for
 * example
 *
 * @param control control that handle the default font
 * @param style new style
 * @return a font with the given style
 */
public static Font buildFontFrom(final Control control, final int style) {
	final Font temp = control.getFont();
	final FontData[] fontData = temp.getFontData();
	if (fontData == null || fontData.length == 0) {
		return temp;
	}
	return new Font(control.getDisplay(), fontData[0].getName(), fontData[0].getHeight(), style);
}
 
Example 10
Source File: XtextDirectEditManager.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method obtains the fonts that are being used by the figure at its
 * zoom level.
 * 
 * @param gep
 *            the associated <code>GraphicalEditPart</code> of the figure
 * @param actualFont
 *            font being used by the figure
 * @param display
 * @return <code>actualFont</code> if zoom level is 1.0 (or when there's an
 *         error), new Font otherwise.
 */
private Font getZoomLevelFont(Font actualFont, Display display) {
	Object zoom = getEditPart().getViewer().getProperty(ZoomManager.class.toString());

	if (zoom != null) {
		double zoomLevel = ((ZoomManager) zoom).getZoom();

		if (zoomLevel == 1.0f)
			return actualFont;

		FontData[] fd = new FontData[actualFont.getFontData().length];
		FontData tempFD = null;

		for (int i = 0; i < fd.length; i++) {
			tempFD = actualFont.getFontData()[i];

			fd[i] = new FontData(tempFD.getName(), (int) (zoomLevel * tempFD.getHeight()), tempFD.getStyle());
		}

		try {
			FontDescriptor fontDescriptor = FontDescriptor.createFrom(fd);
			cachedFontDescriptors.add(fontDescriptor);
			return getResourceManager().createFont(fontDescriptor);
		} catch (DeviceResourceException e) {
			Trace.catching(DiagramUIPlugin.getInstance(), DiagramUIDebugOptions.EXCEPTIONS_CATCHING, getClass(),
					"getZoomLevelFonts", e); //$NON-NLS-1$
			Log.error(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING,
					"getZoomLevelFonts", e); //$NON-NLS-1$

			return actualFont;
		}
	} else
		return actualFont;
}
 
Example 11
Source File: ExcelExporter.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private String getFontInCSSFormat(Font font) {
	FontData fontData = font.getFontData()[0];
	String fontName = fontData.getName();
	int fontStyle = fontData.getStyle();
	String HTML_STYLES[] = new String[] { "NORMAL", "BOLD", "ITALIC" };

	return String.format("font: %s; font-family: %s",
	                     fontStyle <= 2 ? HTML_STYLES[fontStyle] : HTML_STYLES[0],
	                     fontName);
}
 
Example 12
Source File: OpenTypeSelectionDialog.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Font get() {
	final Font font = getDialogArea().getFont();
	final FontData[] data = font.getFontData();
	for (int i = 0; i < data.length; i++) {
		data[i].setStyle(BOLD);
	}
	return new Font(font.getDevice(), data);
}
 
Example 13
Source File: FontFactoryTest.java    From swt-bling with MIT License 4 votes vote down vote up
private boolean isFontDataMatch(Font font, String name, int size, int style) {
  final FontData fontData = font.getFontData()[0];
  return name.equals(fontData.getName()) && size == fontData.getHeight() && style == fontData.getStyle();
}
 
Example 14
Source File: XYGraph.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param titleFont
 *            the titleFont to set
 */
public void setTitleFont(Font titleFont) {
	titleLabel.setFont(titleFont);
	titleFontData = titleFont.getFontData()[0];
}
 
Example 15
Source File: Axis.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param titleFont
 *            the titleFont to set
 */
public void setTitleFont(final Font titleFont) {
	this.titleFont = titleFont;
	this.titleFontData = titleFont.getFontData()[0];
	repaint();
}
 
Example 16
Source File: AbstractPaintManager.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
public void drawEventString(final GanttComposite ganttComposite, final ISettings settings, final IColorManager colorManager, final GanttEvent event, final GC gc, final String toDraw, final boolean threeDee, final int x, final int y, final int eventWidth, final Rectangle bounds) {
    int textEndX = 0;
    int yTextPos = y + (event.getHeight() / 2);

    Font oldFont = null;

    gc.setForeground(colorManager.getTextColor());
    if (event.showBoldText()) {
        oldFont = gc.getFont();
        final FontData[] old = oldFont.getFontData();
        old[0].setStyle(SWT.BOLD);
        final Font f = new Font(Display.getDefault(), old);
        gc.setFont(f);
        // DISPOSE FONT or we'll run out of handles
        f.dispose();
    }

    // font overrides a bold setting
    if (event.getTextFont() != null) {
        gc.setFont(event.getTextFont());
    }

    final Point toDrawExtent = event.getNameExtent();

    final int textSpacer = ganttComposite.isConnected(event) ? settings.getTextSpacerConnected() : settings.getTextSpacerNonConnected();

    int textXStart = 0;

    // find the horizontal text location
    switch (event.getHorizontalTextLocation()) {
        case SWT.LEFT:
            textXStart = x - textSpacer - toDrawExtent.x;
            break;
        case SWT.CENTER:
        	if (!settings.shiftHorizontalCenteredEventString() || toDrawExtent.x < eventWidth) {
            textXStart = x + (eventWidth / 2) - (toDrawExtent.x / 2);
            break;
        	}
        case SWT.RIGHT:
            //textXStart = x + eventWidth + textSpacer;
        	int eventOrPictureWidth = eventWidth;

        	// bugzilla feature request #309808
        	if (!settings.scaleImageToDayWidth() && event.getPicture() != null) {
        		// the image is drawn centered, therefore consider only half of its width
        		eventOrPictureWidth = Math.max(eventOrPictureWidth, event.getPicture().getImageData().width / 2);
        	}
        	
        	textXStart = x + eventOrPictureWidth + textSpacer;
            break;
        default:
            break;
    }

    // find the vertical text location
    switch (event.getVerticalTextLocation()) {
        case SWT.TOP:
            yTextPos = event.getY() - toDrawExtent.y;
            break;
        case SWT.CENTER:
            yTextPos -= (toDrawExtent.y / 2) - 1;
            break;
        case SWT.BOTTOM:
            yTextPos = event.getBottomY();
            break;
        default:
            break;
    }

    gc.drawString(toDraw, textXStart, yTextPos, true);
    int extra = textSpacer + toDrawExtent.x;
    textEndX = x + eventWidth + extra;

    // draw lock icon if parent phase is locked
    if (event.isLocked()) {
        final Image lockImage = settings.getLockImage();
        if (textEndX != 0 && lockImage != null) {
            gc.drawImage(lockImage, textEndX, y);
            extra += lockImage.getBounds().width;
        }
    }

    // regardless of horizontal alignment, it will still add on, so we can leave this as is
    event.setWidthWithText(event.getWidth() + extra);

    // reset font
    gc.setFont(oldFont);
}
 
Example 17
Source File: VerticalLabel.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Fix the issues in the ImageUtilities where the size of the image is the
 * ascent of the font instead of being its height.
 * 
 * Also uses the GC for the rotation.
 * 
 * The biggest issue is the very idea of using an image. The size of the
 * font should be given by the mapmode, not in absolute device pixel as it
 * does look ugly when zooming in.
 * 
 * 
 * @param string
 *            the String to be rendered
 * @param font
 *            the font
 * @param foreground
 *            the text's color
 * @param background
 *            the background color
 * @param useGCTransform
 *            true to use the Transform on the GC object.
 *            false to rely on the homemade algorithm. Transform seems to
 *            not be supported in eclipse-3.2 except on windows.
 *            It is working on macos-3.3M3.
 * @return an Image which must be disposed
 */
public Image createRotatedImageOfString(Graphics g, String string,
        Font font, Color foreground, Color background,
        boolean useGCTransform) {
    Display display = Display.getCurrent();
    if (display == null) {
        SWT.error(SWT.ERROR_THREAD_INVALID_ACCESS);
    }

    List<FontData> fontDatas = new ArrayList<FontData>();
    // take all font datas, mac and linux specific
    for (FontData data : font.getFontData()) {
        FontData data2 = new FontData(data.getName(),
                (int) (data.getHeight() * g.getAbsoluteScale()), data.getStyle());
        fontDatas.add(data2);
    }
    // create the new font
    Font zoomedFont = new Font(display, fontDatas.toArray(new FontData[fontDatas.size()]));
    // get the dimension in this font
    Dimension strDim = FigureUtilities
            .getTextExtents(string, zoomedFont);
    if (strDim.width == 0 || strDim.height == 0) {
        strDim = FigureUtilities
                .getTextExtents(string, font);
    }
    Image srcImage = useGCTransform ?
            new Image(display, strDim.height, strDim.width) :
            new Image(display, strDim.width, strDim.height);
    GC gc = new GC(srcImage);
    if (useGCTransform) {
        Transform transform = new Transform(display);
        transform.rotate(-90);

        gc.setTransform(transform);
    }
    gc.setFont(zoomedFont);
    gc.setForeground(foreground);
    gc.setBackground(background);
    gc.fillRectangle(gc.getClipping());
    gc.drawText(string, gc.getClipping().x, gc.getClipping().y
            // - metrics.getLeading()
            );
    gc.dispose();
    if (useGCTransform) {
        srcImage.dispose();
        zoomedFont.dispose();
        return srcImage;
    }
    disposeImage();
    Image rotated = ImageUtilities.createRotatedImage(srcImage);
    srcImage.dispose();
    zoomedFont.dispose();
    return rotated;
}
 
Example 18
Source File: FontSize.java    From Rel with Apache License 2.0 4 votes vote down vote up
public static Font getThisFontInNewSize(Font font, int size, int style) {
	FontData[] fontdata = font.getFontData();
	return SWTResourceManager.getFont(fontdata[0].getName(), size, style);
}
 
Example 19
Source File: TableViewEditPart.java    From ermasterr with Apache License 2.0 3 votes vote down vote up
protected Font changeFont(final TableFigure tableFigure) {
    final Font font = super.changeFont(tableFigure);

    final FontData fonData = font.getFontData()[0];

    titleFont = Resources.getFont(fonData.getName(), fonData.getHeight(), SWT.BOLD);

    tableFigure.setFont(font, titleFont);

    return font;
}
 
Example 20
Source File: SWTUtils.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create an awt font by converting as much information
 * as possible from the provided swt <code>Font</code>.
 *
 * @param device The swt device to draw on (display or gc device).
 * @param font The swt font to convert.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(Device device, Font font) {
    FontData fontData = font.getFontData()[0];
    return toAwtFont(device, fontData, true);
}