Example usage for org.eclipse.swt.graphics FontData getStyle

List of usage examples for org.eclipse.swt.graphics FontData getStyle

Introduction

In this page you can find the example usage for org.eclipse.swt.graphics FontData getStyle.

Prototype

public int getStyle() 

Source Link

Document

Returns the style of the receiver which is a bitwise OR of one or more of the SWT constants NORMAL, BOLD and ITALIC.

Usage

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);// w ww  . ja va 2s.co  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 .  j  a v 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: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.
 *///  ww w.  j  a v  a2 s. c  om
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 .  j av a2 s .  com

        /* 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  2 s  .c  o m
        // 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);/*  www. j  av a2 s.  c  om*/
    //      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  .  j av a2  s.  co  m
    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);
}

From source file:org.eclipse.swt.examples.texteditor.TextEditor.java

void setStyle(int style, int start, int length) {
    if (length == 0)
        return;//from  w  w w .  ja  v  a2 s  .c o  m

    /* Create new style range */
    StyleRange newRange = new StyleRange();
    if ((style & FONT) != 0) {
        newRange.font = textFont;
    }
    if ((style & FONT_STYLE) != 0) {
        newRange.fontStyle = style & FONT_STYLE;
    }
    if ((style & FOREGROUND) != 0) {
        newRange.foreground = textForeground;
    }
    if ((style & BACKGROUND) != 0) {
        newRange.background = textBackground;
    }
    if ((style & BASELINE_UP) != 0)
        newRange.rise++;
    if ((style & BASELINE_DOWN) != 0)
        newRange.rise--;
    if ((style & STRIKEOUT) != 0) {
        newRange.strikeout = true;
        newRange.strikeoutColor = strikeoutColor;
    }
    if ((style & UNDERLINE) != 0) {
        newRange.underline = true;
        newRange.underlineColor = underlineColor;
        switch (style & UNDERLINE) {
        case UNDERLINE_SINGLE:
            newRange.underlineStyle = SWT.UNDERLINE_SINGLE;
            break;
        case UNDERLINE_DOUBLE:
            newRange.underlineStyle = SWT.UNDERLINE_DOUBLE;
            break;
        case UNDERLINE_ERROR:
            newRange.underlineStyle = SWT.UNDERLINE_ERROR;
            break;
        case UNDERLINE_SQUIGGLE:
            newRange.underlineStyle = SWT.UNDERLINE_SQUIGGLE;
            break;
        case UNDERLINE_LINK:
            newRange.underlineColor = null;
            if (link != null && link.length() > 0) {
                newRange.underlineStyle = SWT.UNDERLINE_LINK;
                newRange.data = link;
            } else {
                newRange.underline = false;
            }
            break;
        }
    }
    if ((style & BORDER) != 0) {
        switch (style & BORDER) {
        case BORDER_DASH:
            newRange.borderStyle = SWT.BORDER_DASH;
            break;
        case BORDER_DOT:
            newRange.borderStyle = SWT.BORDER_DOT;
            break;
        case BORDER_SOLID:
            newRange.borderStyle = SWT.BORDER_SOLID;
            break;
        }
        newRange.borderColor = borderColor;
    }

    int newRangeStart = start;
    int newRangeLength = length;
    int[] ranges = styledText.getRanges(start, length);
    StyleRange[] styles = styledText.getStyleRanges(start, length, false);
    int maxCount = ranges.length * 2 + 2;
    int[] newRanges = new int[maxCount];
    StyleRange[] newStyles = new StyleRange[maxCount / 2];
    int count = 0;
    for (int i = 0; i < ranges.length; i += 2) {
        int rangeStart = ranges[i];
        int rangeLength = ranges[i + 1];
        StyleRange range = styles[i / 2];
        if (rangeStart > newRangeStart) {
            newRangeLength = rangeStart - newRangeStart;
            newRanges[count] = newRangeStart;
            newRanges[count + 1] = newRangeLength;
            newStyles[count / 2] = newRange;
            count += 2;
        }
        newRangeStart = rangeStart + rangeLength;
        newRangeLength = (start + length) - newRangeStart;

        /* Create merged style range*/
        StyleRange mergedRange = new StyleRange(range);
        //Note: fontStyle is not copied by the constructor
        mergedRange.fontStyle = range.fontStyle;
        if ((style & FONT) != 0) {
            mergedRange.font = newRange.font;
        }
        if ((style & FONT_STYLE) != 0) {
            mergedRange.fontStyle = range.fontStyle ^ newRange.fontStyle;
        }
        if (mergedRange.font != null && ((style & FONT) != 0 || (style & FONT_STYLE) != 0)) {
            boolean change = false;
            FontData[] fds = mergedRange.font.getFontData();
            for (FontData fd : fds) {
                if (fd.getStyle() != mergedRange.fontStyle) {
                    fd.setStyle(mergedRange.fontStyle);
                    change = true;
                }
            }
            if (change) {
                mergedRange.font = new Font(display, fds);
            }
        }
        if ((style & FOREGROUND) != 0) {
            mergedRange.foreground = newRange.foreground != range.foreground ? newRange.foreground : null;
        }
        if ((style & BACKGROUND) != 0) {
            mergedRange.background = newRange.background != range.background ? newRange.background : null;
        }
        if ((style & BASELINE_UP) != 0)
            mergedRange.rise++;
        if ((style & BASELINE_DOWN) != 0)
            mergedRange.rise--;
        if ((style & STRIKEOUT) != 0) {
            mergedRange.strikeout = !range.strikeout || range.strikeoutColor != newRange.strikeoutColor;
            mergedRange.strikeoutColor = mergedRange.strikeout ? newRange.strikeoutColor : null;
        }
        if ((style & UNDERLINE) != 0) {
            if ((style & UNDERLINE_LINK) != 0) {
                if (link != null && link.length() > 0) {
                    mergedRange.underline = !range.underline || range.underlineStyle != newRange.underlineStyle
                            || range.data != newRange.data;
                } else {
                    mergedRange.underline = false;
                }
                mergedRange.underlineColor = null;
            } else {
                mergedRange.underline = !range.underline || range.underlineStyle != newRange.underlineStyle
                        || range.underlineColor != newRange.underlineColor;
                mergedRange.underlineColor = mergedRange.underline ? newRange.underlineColor : null;
            }
            mergedRange.underlineStyle = mergedRange.underline ? newRange.underlineStyle : SWT.NONE;
            mergedRange.data = mergedRange.underline ? newRange.data : null;
        }
        if ((style & BORDER) != 0) {
            if (range.borderStyle != newRange.borderStyle || range.borderColor != newRange.borderColor) {
                mergedRange.borderStyle = newRange.borderStyle;
                mergedRange.borderColor = newRange.borderColor;
            } else {
                mergedRange.borderStyle = SWT.NONE;
                mergedRange.borderColor = null;
            }
        }

        newRanges[count] = rangeStart;
        newRanges[count + 1] = rangeLength;
        newStyles[count / 2] = mergedRange;
        count += 2;
    }
    if (newRangeLength > 0) {
        newRanges[count] = newRangeStart;
        newRanges[count + 1] = newRangeLength;
        newStyles[count / 2] = newRange;
        count += 2;
    }
    if (0 < count && count < maxCount) {
        int[] tmpRanges = new int[count];
        StyleRange[] tmpStyles = new StyleRange[count / 2];
        System.arraycopy(newRanges, 0, tmpRanges, 0, count);
        System.arraycopy(newStyles, 0, tmpStyles, 0, count / 2);
        newRanges = tmpRanges;
        newStyles = tmpStyles;
    }
    styledText.setStyleRanges(start, length, newRanges, newStyles);
    disposeRanges(styles);
}