Java Code Examples for org.eclipse.swt.graphics.FontMetrics#getLeading()

The following examples show how to use org.eclipse.swt.graphics.FontMetrics#getLeading() . 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: AbstractDebugVariableCodeMining.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Draw square of the given rgb.
 *
 * @param rgb        the rgb color
 * @param gc         the graphic context
 * @param textWidget the text widget
 * @param x          the location y
 * @param y          the location y
 * @return the square width.
 */
private int drawSquare(RGB rgb, GC gc, StyledText textWidget, int x, int y) {
	FontMetrics fontMetrics = gc.getFontMetrics();
	int size = getSquareSize(fontMetrics);
	x += fontMetrics.getLeading();
	y += fontMetrics.getDescent();

	Rectangle rect = new Rectangle(x, y, size, size);

	// Fill square
	gc.setBackground(getColor(rgb, textWidget.getDisplay()));
	gc.fillRectangle(rect);

	// Draw square box
	gc.setForeground(textWidget.getForeground());
	gc.drawRectangle(rect);
	return getSquareWidth(gc.getFontMetrics());
}
 
Example 2
Source File: CommonLineNumberRulerColumn.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the difference between the baseline of the widget and the
 * baseline as specified by the font for <code>gc</code>. When drawing
 * line numbers, the returned bias should be added to obtain text lined up
 * on the correct base line of the text widget.
 *
 * @param gc the <code>GC</code> to get the font metrics from
 * @param widgetLine the widget line
 * @return the baseline bias to use when drawing text that is lined up with
 *         <code>fCachedTextWidget</code>
 * @since 3.2
 */
private int getBaselineBias(GC gc, int widgetLine) {
	/*
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=62951
	 * widget line height may be more than the font height used for the
	 * line numbers, since font styles (bold, italics...) can have larger
	 * font metrics than the simple font used for the numbers.
	 */
	int offset= fCachedTextWidget.getOffsetAtLine(widgetLine);
	int widgetBaseline= fCachedTextWidget.getBaseline(offset);

	FontMetrics fm= gc.getFontMetrics();
	int fontBaseline= fm.getAscent() + fm.getLeading();
	int baselineBias= widgetBaseline - fontBaseline;
	return Math.max(0, baselineBias);
}
 
Example 3
Source File: TextPrint.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private PrintPiece internalNext(int width, int height) {
	FontMetrics fm = gc.getFontMetrics();

	final int lineHeight = fm.getHeight();
	if (height < lineHeight)
		return null;

	final int maxLines = height / lineHeight;
	String[] nextLines = nextLines(width, maxLines);
	if (nextLines.length == 0)
		return null;

	int maxWidth = maxExtent(nextLines).x;
	Point size = new Point(maxWidth, nextLines.length * lineHeight);
	int ascent = fm.getAscent() + fm.getLeading();

	return new TextPiece(device, style, nextLines, size, ascent);
}
 
Example 4
Source File: BookmarkRulerColumn.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
private int getBaselineBias(GC gc, int widgetLine) {
    /*
     * https://bugs.eclipse.org/bugs/show_bug.cgi?id=62951
     * widget line height may be more than the font height used for the
     * line numbers, since font styles (bold, italics...) can have larger
     * font metrics than the simple font used for the numbers.
     */
    int offset= fCachedTextWidget.getOffsetAtLine(widgetLine);
    int widgetBaseline= fCachedTextWidget.getBaseline(offset);

    FontMetrics fm= gc.getFontMetrics();
    int fontBaseline= fm.getAscent() + fm.getLeading();
    int baselineBias= widgetBaseline - fontBaseline;
    return Math.max(0, baselineBias);
}
 
Example 5
Source File: WhitespaceCharacterPainter.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draw string at widget offset.
 * 
 * @param gc
 * @param offset
 *            the widget offset
 * @param s
 *            the string to be drawn
 * @param fg
 *            the foreground color
 */
private void draw(GC gc, int offset, String s, Color fg)
{
	// Compute baseline delta (see
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=165640)
	int baseline = fTextWidget.getBaseline(offset);
	FontMetrics fontMetrics = gc.getFontMetrics();
	int fontBaseline = fontMetrics.getAscent() + fontMetrics.getLeading();
	int baslineDelta = baseline - fontBaseline;

	Point pos = fTextWidget.getLocationAtOffset(offset);
	gc.setForeground(fg);
	gc.drawString(s, pos.x, pos.y + baslineDelta, true);
}
 
Example 6
Source File: TextPaintInstruction.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void paint(GC gc, Rectangle area) {
	if (!this.state.isRendering()) {
		return;
	}

	FontMetrics metrics = gc.getFontMetrics();
	FontMetrics metricsToUse = metrics;
	int baseline = metrics.getAscent() + metrics.getLeading();

	int yAdvance = 0;
	if (this.state.getCurrentLineHeight() > metrics.getHeight()) {
		int biggerBaseline = this.state.getCurrentBiggestFontMetrics().getAscent() + this.state.getCurrentBiggestFontMetrics().getLeading();
		yAdvance = biggerBaseline - baseline;

		metricsToUse = this.state.getCurrentBiggestFontMetrics();
	}

	Point pointer = this.state.getPointer();

	// on alignment justify render word by word
	int textLength = 0;
	if (TextAlignment.JUSTIFY.equals(this.state.getTextAlignment())) {
		LinePainter line = this.state.getCurrentLine();
		for (String word : this.words) {
			gc.drawText(word, pointer.x, pointer.y + yAdvance, (!this.state.hasPreviousBgColor()));
			int length = gc.textExtent(word).x + line.getNextJustifySpace();
			pointer.x += length;
			textLength += length;
		}
	} else {
		textLength = getTextLength(gc);
		gc.drawText(text, pointer.x, pointer.y + yAdvance, (!this.state.hasPreviousBgColor()));
	}

	if (this.state.isUnderlineActive()) {
		int underlineY = pointer.y + baseline + yAdvance + (metricsToUse.getDescent() / 2);
		gc.drawLine(pointer.x, underlineY, pointer.x + textLength, underlineY);
	}
	if (this.state.isStrikethroughActive()) {
		int strikeY = pointer.y + yAdvance + (metrics.getHeight() / 2) + (metrics.getLeading() / 2);
		gc.drawLine(pointer.x, strikeY, pointer.x + textLength, strikeY);
	}

	if (!TextAlignment.JUSTIFY.equals(this.state.getTextAlignment())) {
		pointer.x += textLength;
	}
}
 
Example 7
Source File: TextPiece.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
public void paint(final GC gc, final int x, final int y) {
	Font oldFont = gc.getFont();
	Color oldForeground = gc.getForeground();
	Color oldBackground = gc.getBackground();

	final int width = getSize().x;
	final int align = style.getAlignment();

	try {
		boolean transparent = initGC(gc);

		FontMetrics fm = gc.getFontMetrics();
		int lineHeight = fm.getHeight();

		boolean strikeout = style.getStrikeout();
		boolean underline = style.getUnderline();
		int lineThickness = Math.max(1, fm.getDescent() / 3);
		int strikeoutOffset = fm.getLeading() + fm.getAscent() / 2;
		int underlineOffset = ascent + lineThickness;

		for (int i = 0; i < lines.length; i++) {
			String line = lines[i];
			int lineWidth = gc.stringExtent(line).x;
			int offset = getHorzAlignmentOffset(align, lineWidth, width);

			gc.drawString(lines[i], x + offset, y + lineHeight * i,
					transparent);
			if (strikeout || underline) {
				Color saveBackground = gc.getBackground();
				gc.setBackground(gc.getForeground());
				if (strikeout)
					gc.fillRectangle(x + offset, y + lineHeight * i
							+ strikeoutOffset, lineWidth, lineThickness);
				if (underline)
					gc.fillRectangle(x + offset, y + lineHeight * i
							+ underlineOffset, lineWidth, lineThickness);
				gc.setBackground(saveBackground);
			}
		}
	} finally {
		restoreGC(gc, oldFont, oldForeground, oldBackground);
	}
}
 
Example 8
Source File: SWTPainter.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public float getFMAscent(){
	this.setAdvanced(false);
	
	FontMetrics fm = this.getControl().getFontMetrics();
	return (fm.getAscent() + fm.getLeading());
}