Java Code Examples for org.eclipse.swt.graphics.GC#getFont()

The following examples show how to use org.eclipse.swt.graphics.GC#getFont() . 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: PageNumberPrint.java    From nebula with Eclipse Public License 2.0 6 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();

	Point size = getSize();

	try {
		ResourcePool resources = ResourcePool.forDevice(device);
		gc.setFont(resources.getFont(textStyle.getFontData()));
		gc.setForeground(resources.getColor(textStyle.getForeground()));

		String text = format.format(pageNumber);
		gc.drawText(text, x
				+ getHorzAlignmentOffset(gc.textExtent(text).x, size.x), y,
				true);
	} finally {
		gc.setFont(oldFont);
		gc.setForeground(oldForeground);
	}
}
 
Example 2
Source File: AlwStartPage.java    From XPagesExtensionLibrary with Apache License 2.0 6 votes vote down vote up
protected int getTextAreaHeight(final GC gc, final int fullWidth, final LayoutConfig lc) {
    Font origFont = gc.getFont();
    
    // Add the title height
    gc.setFont(_titleFont);
    int textHeight = getTextHeight(gc, "X") + SPACER;
    gc.setFont(origFont);
    if (lc.responsive && _showResponsiveIcon) {
        textHeight = Math.max(textHeight, getImageHeight(_responsiveImage) + SPACER);
    }
    
    // Add the description height
    int lineCount = getLines(gc, lc.description, fullWidth - _textMargin - MARGIN).length;
    textHeight += (lineCount * getTextHeight(gc, "X"));
    
    // Add in the footer height
    if (lc.sampleURL != null) {
        textHeight += getTextHeight(gc, "X") + SPACER;
    }
    
    return textHeight;
}
 
Example 3
Source File: NatTableCustomCellPainter.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
private int drawTextPart(GC gc, int yStartPos, int xStartPos, TextPart text){
	Point textExtent = new Point(0, 0);
	if (text.getStyle() == TextPart.PartStyle.NORMAL) {
		textExtent = gc.stringExtent(text.getText());
		gc.drawText(text.getText(), xStartPos, yStartPos + spacing,
			SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER);
	} else if (text.getStyle() == TextPart.PartStyle.BOLD) {
		Font origFont = gc.getFont();
		FontDescriptor boldDescriptor =
			FontDescriptor.createFrom(gc.getFont()).setStyle(SWT.BOLD);
		Font boldFont = boldDescriptor.createFont(Display.getDefault());
		gc.setFont(boldFont);
		textExtent = gc.stringExtent(text.getText());
		gc.drawText(text.getText(), xStartPos, yStartPos + spacing,
			SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER);
		gc.setFont(origFont);
		boldFont.dispose();
	}
	return xStartPos + textExtent.x;
}
 
Example 4
Source File: MultilineListCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
        IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    drawBackground(gc, drawingArea, cellStyle, selected, printing);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);
    DummyRow dr = (DummyRow) row;

    Image img = dr.getImg();
    int x = rect.x + 4;
    int y = rect.y + (rect.height - img.getBounds().height) / 2;
    gc.drawImage(img, x, y);

    Font save = gc.getFont();

    gc.setFont(boldFont);
    gc.drawString(dr.getT2(), rect.x + 70, y + 5);
    gc.setFont(normalFont);
    gc.drawString(dr.getT3(), rect.x + 70, y + 25);

    gc.setFont(save);
    if (drawFocus) {
        drawFocus(gc, drawingArea);
    }
    drawSelection(gc, drawingArea, cellStyle, selected, printing);

}
 
Example 5
Source File: SpanStylePaintInstruction.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void paint(GC gc, Rectangle area) {
	if (this.foregroundColor != null) {
		// remember the previous set value
		// to be able to reset on close
		this.state.addPreviousColor(gc.getForeground());
		// set the style value
		gc.setForeground(this.foregroundColor);
	}

	if (this.backgroundColor != null) {
		// remember the previous set value
		// to be able to reset on close
		this.state.addPreviousBgColor(gc.getBackground());
		// set the style value
		gc.setBackground(this.backgroundColor);
	}

	if (this.fontSize != null || this.fontType != null) {
		Font currentFont = gc.getFont();

		// remember the previous set value
		// to be able to reset on close
		this.state.addPreviousFont(currentFont);

		// set the style value
		gc.setFont(ResourceHelper.getFont(currentFont, this.fontType, this.fontSize));
	}
}
 
Example 6
Source File: TextPainterWithPadding.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private int getWidthFromCache(GC gc, String text) {
	String originalString = text;
	StringBuilder buffer = new StringBuilder();
	buffer.append(text);
	if (gc.getFont() != null) {
		FontData[] datas = fontDataCache.get(gc.getFont());
		if (datas == null) {
			datas = gc.getFont().getFontData();
			fontDataCache.put(gc.getFont(), datas);
		}
		if (datas != null && datas.length > 0) {
			buffer.append(datas[0].getName());
			buffer.append(",");
			buffer.append(datas[0].getHeight());
			buffer.append(",");
			buffer.append(datas[0].getStyle());
		}
	}
	text = buffer.toString();
	Integer width = temporaryMap.get(text);
	if (width == null) {
		width = Integer.valueOf(gc.textExtent(originalString).x);
		temporaryMap.put(text, width);
	}

	return width.intValue();
}
 
Example 7
Source File: RendererBase.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method saving several GC attributes to loal variables. The values can be restored with
 * <code>restoreGCAttributes</code>.
 * 
 * @param gc GC to save attributes for
 */
protected void saveGCAttributes(GC gc) {
    _bgColor = gc.getBackground();
    _fgColor = gc.getForeground();
    _font = gc.getFont();
    _lineWidth = gc.getLineWidth();
}
 
Example 8
Source File: LineBreakPrint.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private static int calculateLineHeight(LineBreakPrint print, Device device,
		GC gc) {
	Font oldFont = gc.getFont();

	gc.setFont(ResourcePool.forDevice(device).getFont(print.font));
	int result = gc.getFontMetrics().getHeight();

	gc.setFont(oldFont);

	return result;
}
 
Example 9
Source File: TextCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int getPreferredHeight(GC gc, ICellStyle cellStyle, int width, IRow row, IColumn column) {
    Object value = convertValue(row, column);
    Font font = gc.getFont();
    int height = -1;
    if (value != null) {
        String s = value.toString();
        gc.setFont(getFont(cellStyle, false, gc.getFont()));
        height = TextRenderer.getHeight(gc, getInnerWidth(width, cellStyle), true, s);
    }
    gc.setFont(font);
    return height + getVerticalSpacesSum(cellStyle);
}
 
Example 10
Source File: TextPainterWithPadding.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
private int getWidthFromCache(GC gc, String text) {
	String originalString = text;
	StringBuilder buffer = new StringBuilder();
	buffer.append(text);
	if (gc.getFont() != null) {
		FontData[] datas = fontDataCache.get(gc.getFont());
		if (datas == null) {
			datas = gc.getFont().getFontData();
			fontDataCache.put(gc.getFont(), datas);
		}
		if (datas != null && datas.length > 0) {
			buffer.append(datas[0].getName());
			buffer.append(",");
			buffer.append(datas[0].getHeight());
			buffer.append(",");
			buffer.append(datas[0].getStyle());
		}
	}
	text = buffer.toString();
	Integer width = temporaryMap.get(text);
	if (width == null) {
		width = Integer.valueOf(gc.textExtent(originalString).x);
		temporaryMap.put(text, width);
	}

	return width.intValue();
}
 
Example 11
Source File: FontFactoryTest.java    From swt-bling with MIT License 5 votes vote down vote up
@Test
public void getFont_bogusFontName_returnFontWithSystemDefaultName() {
  final Font font = FontFactory.getFont(Display.getCurrent(), SIZE, STYLE, FAKE_FONT_NAME);

  final GC gc = new GC(Display.getCurrent());
  final Font systemFont = gc.getFont();
  gc.dispose();

  assertTrue(isFontDataMatch(font, systemFont.getFontData()[0].getName(), SIZE, STYLE));
}
 
Example 12
Source File: TextPainter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
private int getWidthFromCache(GC gc, String text) {
	String originalString = text;
	StringBuilder buffer = new StringBuilder();
	buffer.append(text);
	if (gc.getFont() != null) {
		FontData[] datas = fontDataCache.get(gc.getFont());
		if (datas == null) {
			datas = gc.getFont().getFontData();
			fontDataCache.put(gc.getFont(), datas);
		}
		if (datas != null && datas.length > 0) {
			buffer.append(datas[0].getName());
			buffer.append(",");
			buffer.append(datas[0].getHeight());
			buffer.append(",");
			buffer.append(datas[0].getStyle());
		}
	}
	text = buffer.toString();
	Integer width = temporaryMap.get(text);
	if (width == null) {
		width = Integer.valueOf(gc.textExtent(originalString).x);
		temporaryMap.put(text, width);
	}

	return width.intValue();
}
 
Example 13
Source File: RendererBase.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method saving several GC attributes to loal variables. The values can be restored with
 * <code>restoreGCAttributes</code>.
 * 
 * @param gc GC to save attributes for
 */
protected void saveGCAttributes(GC gc) {
    _bgColor = gc.getBackground();
    _fgColor = gc.getForeground();
    _font = gc.getFont();
    _lineWidth = gc.getLineWidth();
}
 
Example 14
Source File: DefaultRowHeaderRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public Point computeSize(GC gc, int wHint, int hHint, Object value)
   {
       GridItem item = (GridItem) value;

       String text = getHeaderText(item);
       Image image = getHeaderImage(item);

       Font previousFont = gc.getFont();
	if (getHeaderFont(item) == null) {
		gc.setFont(item.getParent().getFont());
	} else {
		gc.setFont(getHeaderFont(item));
	}

       int x = leftMargin;

       if( image != null ) {
       	x += image.getBounds().width + 5;
       }

       x += gc.stringExtent(text).x + rightMargin;

       int y = topMargin;

       if( image != null ) {
       	y += Math.max(gc.getFontMetrics().getHeight(),image.getBounds().height);
       } else {
       	y += gc.getFontMetrics().getHeight();
       }


       y += bottomMargin;
       gc.setFont(previousFont);

       return new Point(x, y);
   }
 
Example 15
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 16
Source File: ItalicPaintInstruction.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void paint(GC gc, Rectangle area) {
	Font currentFont = gc.getFont();
	this.state.addPreviousFont(currentFont);
	gc.setFont(ResourceHelper.getItalicFont(currentFont));
}
 
Example 17
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 18
Source File: TextCellRenderer.java    From translationstudio8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
        IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    
    drawBackground(gc, drawingArea, cellStyle, selected, printing);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);

    // convert the value to a string
    String s = convertValue(row, column);

    Color fg = gc.getForeground();
    Color bg = gc.getBackground();
    Font font = gc.getFont();

   

    // draw comment marker if comment is present and not printing
    if (!printing && getComment(row, column) != null) {
        drawCommentMarker(gc, drawingArea, _commentColor, COMMENTMARKER_SIZE);
    }

    if (drawFocus) {
        drawFocus(gc, drect);
    }
    drawSelection(gc, drawingArea, cellStyle, selected, printing);

    gc.setForeground(fg);
    gc.setBackground(bg);
    gc.setFont(font);
    if (s != null) {
        if (selected && !printing) {
            gc.setBackground(SELECTIONCOLOR);
        } else {
            gc.setBackground(getBackgroundColor(cellStyle, printing));
        }
        gc.setForeground(getForegroundColor(cellStyle, printing));
        gc.setFont(getFont(cellStyle, printing, gc.getFont()));

        drawCellString(gc, rect, s, cellStyle);
        if (s.indexOf("we") != -1) {
            TextLayout textLayout = new TextLayout(gc.getDevice());
            textLayout.setText(s);
            textLayout.setFont(gc.getFont());
            textLayout.setWidth(rect.width);
            Color color = new Color(gc.getDevice(), 150, 100, 100);
    		Font font2 = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont()
    				.getFontData()[0].getHeight(), SWT.ITALIC);
    		TextStyle style = new TextStyle(font2, color, null);
    		for (int i = 1; i < s.length(); i++) {
    			int j = indexOf(s, "we", i, false);
    			if (j != -1) {
    				textLayout.setStyle(style, j, j + 3);
    			} else {
    				break;
    			}

    		}
    		gc.fillRectangle(rect);
    		textLayout.draw(gc, rect.x, rect.y);
    		gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
            }
    }
}
 
Example 19
Source File: TextCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row,
        IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    
    drawBackground(gc, drawingArea, cellStyle, selected, printing);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);

    // convert the value to a string
    String s = convertValue(row, column);

    Color fg = gc.getForeground();
    Color bg = gc.getBackground();
    Font font = gc.getFont();

   

    // draw comment marker if comment is present and not printing
    if (!printing && getComment(row, column) != null) {
        drawCommentMarker(gc, drawingArea, _commentColor, COMMENTMARKER_SIZE);
    }

    if (drawFocus) {
        drawFocus(gc, drect);
    }
    drawSelection(gc, drawingArea, cellStyle, selected, printing);

    gc.setForeground(fg);
    gc.setBackground(bg);
    gc.setFont(font);
    if (s != null) {
        if (selected && !printing) {
            gc.setBackground(SELECTIONCOLOR);
        } else {
            gc.setBackground(getBackgroundColor(cellStyle, printing));
        }
        gc.setForeground(getForegroundColor(cellStyle, printing));
        gc.setFont(getFont(cellStyle, printing, gc.getFont()));

        drawCellString(gc, rect, s, cellStyle);
        if (s.indexOf("we") != -1) {
            TextLayout textLayout = new TextLayout(gc.getDevice());
            textLayout.setText(s);
            textLayout.setFont(gc.getFont());
            textLayout.setWidth(rect.width);
            Color color = new Color(gc.getDevice(), 150, 100, 100);
    		Font font2 = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont()
    				.getFontData()[0].getHeight(), SWT.ITALIC);
    		TextStyle style = new TextStyle(font2, color, null);
    		for (int i = 1; i < s.length(); i++) {
    			int j = indexOf(s, "we", i, false);
    			if (j != -1) {
    				textLayout.setStyle(style, j, j + 3);
    			} else {
    				break;
    			}

    		}
    		gc.fillRectangle(rect);
    		textLayout.draw(gc, rect.x, rect.y);
    		gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
            }
    }
}
 
Example 20
Source File: BoldPaintInstruction.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void paint(GC gc, Rectangle area) {
	Font currentFont = gc.getFont();
	this.state.addPreviousFont(currentFont);
	gc.setFont(ResourceHelper.getBoldFont(currentFont));
}