Search in sources :

Example 6 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class TextPainterWithPadding method setFont.

public void setFont(Font font) {
    TextLayout layout = new TextLayout(Display.getDefault());
    try {
        if (font != null) {
            this.font = font;
            Font boldFont = getFont(SWT.BOLD), italicFont = getFont(SWT.ITALIC), boldItalicFont = getFont(SWT.BOLD | SWT.ITALIC);
            layout.setText("    ");
            layout.setFont(font);
            layout.setStyle(new TextStyle(font, null, null), 0, 0);
            layout.setStyle(new TextStyle(boldFont, null, null), 1, 1);
            layout.setStyle(new TextStyle(italicFont, null, null), 2, 2);
            layout.setStyle(new TextStyle(boldItalicFont, null, null), 3, 3);
            FontMetrics metrics = layout.getLineMetrics(0);
            ascent = metrics.getAscent() + metrics.getLeading();
            descent = metrics.getDescent();
            boldFont.dispose();
            italicFont.dispose();
            boldItalicFont.dispose();
            boldFont = italicFont = boldItalicFont = null;
        }
        layout.dispose();
        layout = new TextLayout(Display.getDefault());
        layout.setFont(this.font);
        StringBuffer tabBuffer = new StringBuffer(tabSize);
        for (int i = 0; i < tabSize; i++) {
            tabBuffer.append(' ');
        }
        layout.setText(tabBuffer.toString());
        tabWidth = layout.getBounds().width;
        layout.dispose();
    } finally {
        if (layout != null && !layout.isDisposed()) {
            layout.dispose();
        }
    }
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) FontMetrics(org.eclipse.swt.graphics.FontMetrics) Font(org.eclipse.swt.graphics.Font) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 7 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class InnerTagScanner method createTextStyle.

private TextStyle createTextStyle() {
    TextStyle style = new TextStyle();
    style.metrics = new GlyphMetrics(0, 0, 0);
    return style;
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics)

Example 8 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class TagStyleConfigurator method configure.

public static void configure(TextLayout textLayout) {
    String text = textLayout.getText();
    Document doc = new Document(text);
    ITokenScanner scanner = getRecipeScanner(doc);
    scanner.setRange(doc, 0, doc.getLength());
    IToken token;
    while ((token = scanner.nextToken()) != Token.EOF) {
        int offset = scanner.getTokenOffset();
        int length = scanner.getTokenLength();
        Object data = token.getData();
        if (data != null && data instanceof TextStyle) {
            TextStyle textStyle = (TextStyle) data;
            textLayout.setStyle(textStyle, offset, offset + length - 1);
        }
    }
    scanner = null;
    doc = null;
}
Also used : IToken(org.eclipse.jface.text.rules.IToken) TextStyle(org.eclipse.swt.graphics.TextStyle) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ITokenScanner(org.eclipse.jface.text.rules.ITokenScanner)

Example 9 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project translationstudio8 by heartsome.

the class SourceColunmCellRenderer method paint.

/**
	 * {@inheritDoc}
	 */
public void paint(GC gc, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    boolean drawBackground = true;
    boolean drawAsSelected = isSelected();
    if (isCellSelected()) {
        drawAsSelected = true;
    }
    gc.setForeground(item.getForeground(getColumn()));
    if (drawAsSelected) {
        gc.setBackground((Color) item.getParent().getData("selectedBgColor"));
    } else {
        if (item.getParent().isEnabled()) {
            Color back = item.getBackground(getColumn());
            if (back != null) {
                gc.setBackground(back);
            } else {
                drawBackground = false;
            }
        } else {
            gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }
    }
    if (drawBackground) {
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    }
    String text = segmentViewer.getTextWidget().getText();
    // 创建 TextLayout
    TextLayout layout = getTextLayout(gc, item, getColumn(), false, false);
    String displayStr = layout.getText();
    // 附加内部标记样式前,先和源文比较,不一样的地方着色实现
    if (!tuSrcText.equals(displayStr)) {
        // TextStyle style = new TextStyle(layout.getFont(), colorConfigBean.getSrcDiffFgColor(),
        // colorConfigBean.getSrcDiffBgColor());
        // List<Position> diff = Comparator.Compare(tuSrcText, displayStr);
        // for (Iterator<Position> iterator = diff.iterator(); iterator.hasNext();) {
        // Position position = iterator.next();
        // layout.setStyle(style, position.offset, position.length);
        // }
        TextStyle diffStyle = new TextStyle(layout.getFont(), null, diffColor);
        // Increase or
        TextStyle InOrDecreaseStyle = new TextStyle(layout.getFont(), null, inOrDecreaseColor);
        // decrease
        TextStyle positionStyle = new TextStyle(layout.getFont(), null, positionColor);
        TextDiffMatcher dmp = new TextDiffMatcher();
        LinkedList<Diff> diffs = dmp.diff_main(text, displayStr);
        dmp.diff_cleanupEfficiency(diffs);
        boolean pFlg = positionDiff(diffs, text, displayStr);
        if (pFlg) {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE && drawAsSelected) {
                    String delText = f.text;
                    Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                    TextStyle style = positionStyle;
                    boolean flg = m.find();
                    if (flg && delText.length() != 1) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        int mark = 0;
                        List<StyleRange> rangeList = new ArrayList<StyleRange>();
                        do {
                            int off = m.start();
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = off - mark;
                            mark = m.end();
                            rangeList.add(range);
                        } while (m.find());
                        if (mark < delText.length()) {
                            StyleRange range = new StyleRange(style);
                            range.start = start + mark;
                            range.length = delText.length() - mark;
                            rangeList.add(range);
                        }
                        for (StyleRange range : rangeList) {
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    } else if (!flg) {
                        int start = text.indexOf(delText, delTextStart);
                        delTextStart = start + delText.length();
                        StyleRange range = new StyleRange(style);
                        range.start = start;
                        range.length = delText.length();
                        segmentViewer.getTextWidget().setStyleRange(range);
                    }
                } else if (f.operation == Operation.INSERT) {
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(positionStyle, start, start + insText.length() - 1);
                }
            }
        } else {
            int insTextStart = 0;
            int delTextStart = 0;
            for (int i = 0; i < diffs.size(); i++) {
                Diff f = diffs.get(i);
                if (f.operation == Operation.EQUAL) {
                    int l = f.text.length() - 1;
                    insTextStart += l;
                    delTextStart += l;
                    continue;
                } else if (f.operation == Operation.DELETE) {
                    String delText = f.text;
                    boolean isDiff = false;
                    if (i + 1 < diffs.size() && diffs.get(i + 1).operation == Operation.INSERT) {
                        // 库和当前句子存在不一致部分
                        i += 1;
                        // 处理库
                        f = diffs.get(i);
                        String insText = f.text;
                        int start = displayStr.indexOf(insText, insTextStart);
                        layout.setStyle(diffStyle, start, start + insText.length() - 1);
                        insTextStart = start + insText.length();
                        isDiff = true;
                    }
                    // 处理当前句子
                    if (drawAsSelected) {
                        Matcher m = PlaceHolderEditModeBuilder.PATTERN.matcher(delText);
                        TextStyle style = isDiff ? diffStyle : InOrDecreaseStyle;
                        boolean flg = m.find();
                        if (flg && delText.length() != 1) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            int mark = 0;
                            List<StyleRange> rangeList = new ArrayList<StyleRange>();
                            do {
                                int off = m.start();
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = off - mark;
                                mark = m.end();
                                rangeList.add(range);
                            } while (m.find());
                            if (mark < delText.length()) {
                                StyleRange range = new StyleRange(style);
                                range.start = start + mark;
                                range.length = delText.length() - mark;
                                rangeList.add(range);
                            }
                            for (StyleRange range : rangeList) {
                                segmentViewer.getTextWidget().setStyleRange(range);
                            }
                        } else if (!flg) {
                            int start = text.indexOf(delText, delTextStart);
                            delTextStart = start + delText.length();
                            StyleRange range = new StyleRange(style);
                            range.start = start;
                            range.length = delText.length();
                            segmentViewer.getTextWidget().setStyleRange(range);
                        }
                    }
                } else if (f.operation == Operation.INSERT) {
                    // 库多出来的
                    String insText = f.text;
                    int start = displayStr.indexOf(insText, insTextStart);
                    insTextStart += insText.length();
                    layout.setStyle(InOrDecreaseStyle, start, start + insText.length() - 1);
                }
            }
        }
    }
    // 添加标记样式,并创建标记
    attachInnertTagStyle(gc, layout, false);
    try {
        int y = getBounds().y + textTopMargin + topMargin;
        y += getVerticalAlignmentAdjustment(layout.getBounds().height, getBounds().height);
        if (item.getParent().isAutoHeight()) {
            int textHeight = topMargin + textTopMargin;
            // fix Bug #3116 库匹配面板--显示的匹配有截断 by Jason
            // for (int cnt = 0; cnt < layout.getLineCount(); cnt++)
            // textHeight += layout.getLineBounds(cnt).height;
            textHeight += layout.getBounds().height;
            textHeight += textBottomMargin + bottomMargin;
            item.setData("itemHeight", textHeight);
        }
        Point selection = copyEnable.getSelectionRange(getColumn(), item);
        if (selection == null || selection.x == selection.y) {
            layout.draw(gc, getBounds().x + leftMargin, y);
        } else {
            int x = getBounds().x + leftMargin;
            int start = Math.max(0, selection.x);
            int end = Math.min(displayStr.length(), selection.y);
            layout.draw(gc, x, y, start, end - 1, getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT), getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        }
        drawInnerTag(gc, layout);
        if (item.getParent().getLinesVisible()) {
            if (isCellSelected()) {
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
            } else {
                gc.setForeground(item.getParent().getLineColor());
            }
            gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
            gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
        }
    } finally {
        if (layout != null) {
            layout.dispose();
        }
    }
}
Also used : Diff(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher.Diff) Matcher(java.util.regex.Matcher) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TextDiffMatcher(net.heartsome.cat.ts.ui.translation.comparator.TextDiffMatcher)

Example 10 with TextStyle

use of org.eclipse.swt.graphics.TextStyle in project eclipse.platform.swt by eclipse.

the class MJ_Table method styledItems_Snippet236.

@Test
public void styledItems_Snippet236() {
    Shell shell = mkShell("Verify tableitems with custom styleText control looks proper & multiple items can be selected");
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    for (int i = 0; i < 10; i++) {
        new TableItem(table, SWT.NONE);
    }
    final TextLayout textLayout = new TextLayout(display);
    textLayout.setText("SWT: Standard Widget Toolkit");
    Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD);
    Font font2 = new Font(display, "Tahoma", 10, SWT.NORMAL);
    Font font3 = new Font(display, "Tahoma", 14, SWT.ITALIC);
    TextStyle style1 = new TextStyle(font1, display.getSystemColor(SWT.COLOR_BLUE), null);
    TextStyle style2 = new TextStyle(font2, display.getSystemColor(SWT.COLOR_MAGENTA), null);
    TextStyle style3 = new TextStyle(font3, display.getSystemColor(SWT.COLOR_RED), null);
    textLayout.setStyle(style1, 0, 0);
    textLayout.setStyle(style1, 5, 12);
    textLayout.setStyle(style2, 1, 1);
    textLayout.setStyle(style2, 14, 19);
    textLayout.setStyle(style3, 2, 2);
    textLayout.setStyle(style3, 21, 27);
    /*
		 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
		 * Therefore, it is critical for performance that these methods be
		 * as efficient as possible.
		 */
    table.addListener(SWT.PaintItem, event -> textLayout.draw(event.gc, event.x, event.y));
    final Rectangle textLayoutBounds = textLayout.getBounds();
    table.addListener(SWT.MeasureItem, e -> {
        e.width = textLayoutBounds.width + 2;
        e.height = textLayoutBounds.height + 2;
    });
    shell.setSize(SWIDTH, SHEIGHT);
    shell.open();
    shell.addDisposeListener(e -> {
        font1.dispose();
        font2.dispose();
        font3.dispose();
        textLayout.dispose();
    });
    mainLoop(shell);
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Table(org.eclipse.swt.widgets.Table) TextStyle(org.eclipse.swt.graphics.TextStyle) TableItem(org.eclipse.swt.widgets.TableItem) Rectangle(org.eclipse.swt.graphics.Rectangle) FillLayout(org.eclipse.swt.layout.FillLayout) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout) Test(org.junit.Test)

Aggregations

TextStyle (org.eclipse.swt.graphics.TextStyle)25 Point (org.eclipse.swt.graphics.Point)12 TextLayout (org.eclipse.swt.graphics.TextLayout)10 Color (org.eclipse.swt.graphics.Color)7 Font (org.eclipse.swt.graphics.Font)7 Rectangle (org.eclipse.swt.graphics.Rectangle)7 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 StyleRange (org.eclipse.swt.custom.StyleRange)5 IToken (org.eclipse.jface.text.rules.IToken)4 RGB (org.eclipse.swt.graphics.RGB)4 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)3 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)3 List (java.util.List)2 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 ITokenScanner (org.eclipse.jface.text.rules.ITokenScanner)2 Styler (org.eclipse.jface.viewers.StyledString.Styler)2 GridItem (org.eclipse.nebula.widgets.grid.GridItem)2 StyledText (org.eclipse.swt.custom.StyledText)2