Search in sources :

Example 71 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_StyledText method rtfCopy.

/**
 * Regression test for bug 19985
 */
protected void rtfCopy() {
    String lines = "Line0\nLine1\nLine2\nLine3\nLine4\nLine5";
    final int[] linesCalled = new int[] { 0 };
    LineStyleListener listener = event -> {
        Display display = Display.getDefault();
        Color red = display.getSystemColor(SWT.COLOR_RED);
        StyledText styledText = (StyledText) event.widget;
        int lineIndex = styledText.getLineAtOffset(event.lineOffset);
        int lineStart = event.lineOffset;
        int lineEnd = lineStart + event.lineText.length();
        StyleRange goodRange = new StyleRange(0, 1, red, red);
        event.styles = new StyleRange[2];
        switch(lineIndex % 6) {
            case 0:
                event.styles[0] = goodRange;
                event.styles[1] = new StyleRange(lineEnd, 1, red, red);
                linesCalled[0]++;
                break;
            case 1:
                event.styles[0] = goodRange;
                event.styles[1] = new StyleRange(lineEnd, -1, red, red);
                linesCalled[0]++;
                break;
            case 2:
                event.styles[0] = goodRange;
                event.styles[1] = new StyleRange(lineEnd - 1, -1, red, red);
                linesCalled[0]++;
                break;
            case 3:
                event.styles[0] = goodRange;
                event.styles[1] = new StyleRange(lineStart, -1, red, red);
                linesCalled[0]++;
                break;
            case 4:
                event.styles[0] = new StyleRange(lineStart, 1, red, red);
                event.styles[1] = new StyleRange(lineStart, -1, red, red);
                linesCalled[0]++;
                break;
            case 5:
                event.styles[0] = new StyleRange(lineEnd / 2, 1, red, red);
                event.styles[1] = new StyleRange(lineEnd / 2, -1, red, red);
                linesCalled[0]++;
                break;
        }
    };
    text.setText(lines);
    // cause StyledText to call the listener.
    text.setSelection(0, text.getCharCount());
    text.addLineStyleListener(listener);
    linesCalled[0] = 0;
    text.copy();
    assertTrue("not all lines tested for RTF copy", linesCalled[0] == text.getLineCount());
    Clipboard clipboard = new Clipboard(text.getDisplay());
    RTFTransfer rtfTranfer = RTFTransfer.getInstance();
    String clipboardText = (String) clipboard.getContents(rtfTranfer);
    assertTrue("RTF copy failed", clipboardText.length() > 0);
    clipboard.dispose();
    text.removeLineStyleListener(listener);
}
Also used : Printer(org.eclipse.swt.printing.Printer) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) StyledTextContent(org.eclipse.swt.custom.StyledTextContent) VerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener) StyledText(org.eclipse.swt.custom.StyledText) Rectangle(org.eclipse.swt.graphics.Rectangle) HashMap(java.util.HashMap) MovementEvent(org.eclipse.swt.custom.MovementEvent) TextChangeListener(org.eclipse.swt.custom.TextChangeListener) Point(org.eclipse.swt.graphics.Point) Clipboard(org.eclipse.swt.dnd.Clipboard) Event(org.eclipse.swt.widgets.Event) LineBackgroundListener(org.eclipse.swt.custom.LineBackgroundListener) VerifyListener(org.eclipse.swt.events.VerifyListener) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Map(java.util.Map) TextTransfer(org.eclipse.swt.dnd.TextTransfer) After(org.junit.After) RGB(org.eclipse.swt.graphics.RGB) Assert.fail(org.junit.Assert.fail) BidiSegmentListener(org.eclipse.swt.custom.BidiSegmentListener) Font(org.eclipse.swt.graphics.Font) LineStyleListener(org.eclipse.swt.custom.LineStyleListener) ExtendedModifyListener(org.eclipse.swt.custom.ExtendedModifyListener) Before(org.junit.Before) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Assert.assertNotNull(org.junit.Assert.assertNotNull) MovementListener(org.eclipse.swt.custom.MovementListener) Assert.assertTrue(org.junit.Assert.assertTrue) StyleRange(org.eclipse.swt.custom.StyleRange) Caret(org.eclipse.swt.widgets.Caret) Test(org.junit.Test) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) Display(org.eclipse.swt.widgets.Display) CaretListener(org.eclipse.swt.custom.CaretListener) Transfer(org.eclipse.swt.dnd.Transfer) Assert.assertNull(org.junit.Assert.assertNull) Color(org.eclipse.swt.graphics.Color) BidiUtil(org.eclipse.swt.internal.BidiUtil) Assert.assertFalse(org.junit.Assert.assertFalse) ModifyListener(org.eclipse.swt.events.ModifyListener) SWT(org.eclipse.swt.SWT) ST(org.eclipse.swt.custom.ST) FontData(org.eclipse.swt.graphics.FontData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Assert.assertEquals(org.junit.Assert.assertEquals) SelectionListener(org.eclipse.swt.events.SelectionListener) GridLayout(org.eclipse.swt.layout.GridLayout) StyledText(org.eclipse.swt.custom.StyledText) Color(org.eclipse.swt.graphics.Color) StyleRange(org.eclipse.swt.custom.StyleRange) Clipboard(org.eclipse.swt.dnd.Clipboard) RTFTransfer(org.eclipse.swt.dnd.RTFTransfer) LineStyleListener(org.eclipse.swt.custom.LineStyleListener) Display(org.eclipse.swt.widgets.Display)

Example 72 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_StyledText method test_getRanges.

@Test
public void test_getRanges() {
    StyleRange style0 = new StyleRange();
    style0.rise = 10;
    StyleRange style1 = new StyleRange();
    style1.rise = 5;
    StyleRange style2 = new StyleRange();
    style2.rise = 30;
    StyleRange[] expectedStyles;
    int[] expectedRanges;
    // tests using the new API
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    expectedStyles = new StyleRange[] { style0 };
    expectedRanges = new int[] { 0, 10 };
    testStyles("Test 1", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    expectedStyles = new StyleRange[] { style1 };
    expectedRanges = new int[] { 3, 4 };
    testStyles("Test 2", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    expectedStyles = new StyleRange[] { style0, style1, style0 };
    expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
    testStyles("Test 3", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 1, 4 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style1, style0 };
    expectedRanges = new int[] { 0, 1, 1, 4, 5, 2, 7, 3 };
    testStyles("Test 4", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 1, 8 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style0 };
    expectedRanges = new int[] { 0, 1, 1, 8, 9, 1 };
    testStyles("Test 5", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 0, 5 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style2, style1, style0 };
    expectedRanges = new int[] { 0, 5, 5, 2, 7, 3 };
    testStyles("Test 6", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 1, 6 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style0 };
    expectedRanges = new int[] { 0, 1, 1, 6, 7, 3 };
    testStyles("Test 7", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style0 };
    expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
    testStyles("Test 8", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 0, 3 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style2, style1, style0 };
    expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
    testStyles("Test 9", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 7, 3 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style1, style2 };
    expectedRanges = new int[] { 0, 3, 3, 4, 7, 3 };
    testStyles("Test 10", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 4, 2 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style1, style2, style1, style0 };
    expectedRanges = new int[] { 0, 3, 3, 1, 4, 2, 6, 1, 7, 3 };
    testStyles("Test 11", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 3, 4 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 2, 6 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style0 };
    expectedRanges = new int[] { 0, 2, 2, 6, 8, 2 };
    testStyles("Test 12", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 0, 10 }, new StyleRange[] { style1 });
    expectedStyles = new StyleRange[] { style1 };
    expectedRanges = new int[] { 0, 10 };
    testStyles("Test 13", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789AB");
    text.setStyleRanges(0, 0, new int[] { 1, 3 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 5, 1 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 7, 1 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 9, 2 }, new StyleRange[] { style1 });
    expectedStyles = new StyleRange[] { style0, style1, style0, style1 };
    expectedRanges = new int[] { 1, 3, 5, 1, 7, 1, 9, 2 };
    testStyles("Test 14", text.getRanges(0, 12), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789AB");
    text.setStyleRanges(0, 0, new int[] { 1, 3 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 5, 1 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 7, 1 }, new StyleRange[] { style0 });
    text.setStyleRanges(0, 0, new int[] { 9, 2 }, new StyleRange[] { style1 });
    text.setStyleRanges(0, 0, new int[] { 2, 8 }, new StyleRange[] { style2 });
    expectedStyles = new StyleRange[] { style0, style2, style1 };
    expectedRanges = new int[] { 1, 1, 2, 8, 10, 1 };
    testStyles("Test 15", text.getRanges(0, 12), expectedRanges, text.getStyleRanges(false), expectedStyles);
    // tests mixing the old API and the new API
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) });
    expectedStyles = new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) };
    expectedRanges = new int[] { 3, 4 };
    testStyles("Test 16", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    // test the merging code
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
    text.setStyleRanges(0, 0, new int[] { 4, 2 }, new StyleRange[] { style0 });
    expectedStyles = new StyleRange[] { style0 };
    expectedRanges = new int[] { 1, 8 };
    testStyles("Test 17", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
    text.setStyleRanges(0, 0, new int[] { 2, 6 }, new StyleRange[] { style0 });
    expectedStyles = new StyleRange[] { style0 };
    expectedRanges = new int[] { 1, 8 };
    testStyles("Test 18", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(new int[] { 1, 3, 6, 3 }, new StyleRange[] { style0, style0 });
    text.setStyleRanges(3, 4, new int[] { 0, 1, 9, 1 }, new StyleRange[] { style0, style0 });
    expectedStyles = new StyleRange[] { style0, style0 };
    expectedRanges = new int[] { 0, 3, 7, 3 };
    testStyles("Test 19", text.getRanges(0, 10), expectedRanges, text.getStyleRanges(false), expectedStyles);
    // tests mixing the old API and the new API
    text.setText("");
    text.setText("0123456789");
    text.setStyleRanges(new StyleRange[] { new StyleRange(3, 4, null, null, SWT.BOLD) });
    expectedStyles = new StyleRange[] {};
    expectedRanges = new int[] {};
    testStyles("Test 20", text.getRanges(3, 0), expectedRanges, text.getStyleRanges(3, 0, false), expectedStyles);
    // bug 250859 (getRanges)
    text.setText("");
    text.setText("The Eclipse Foundation is currently going through the exercise");
    text.setStyleRanges(new int[] { 12, 10, 36, 5 }, new StyleRange[] { style0, style1 });
    expectedStyles = new StyleRange[] { style0 };
    expectedRanges = new int[] { 12, 10 };
    testStyles("Test 21 (bug 250859)", text.getRanges(12, 10), expectedRanges, text.getStyleRanges(12, 10, false), expectedStyles);
    // bug 250193
    text.setText("");
    text.setText("The Eclipse Foundation is currently going through the exercise");
    StyleRange sr1 = new StyleRange();
    sr1.underline = true;
    StyleRange sr2 = new StyleRange();
    sr2.strikeout = true;
    sr1.start = 12;
    sr1.length = 10;
    sr2.start = 36;
    sr2.length = 5;
    text.setStyleRange(sr1);
    text.setStyleRange(sr2);
    text.replaceTextRange(12, 10, "");
    expectedStyles = new StyleRange[] { sr2 };
    expectedRanges = new int[] { 26, 5 };
    testStyles("Test 22 (bug 250193)", text.getRanges(26, 5), expectedRanges, text.getStyleRanges(26, 5, false), expectedStyles);
    // bug 212851 (getStyleRanges)
    text.setText("");
    text.setText("line0\nline1\nline2");
    text.setStyleRanges(new int[] { 0, 2, 2, 2, 4, 4, 13, 3 }, new StyleRange[] { style0, style1, style2, style0 });
    expectedRanges = new int[] { 6, 2 };
    expectedStyles = new StyleRange[] { style2 };
    testStyles("Test 23 (bug 212851 - getRanges)", text.getRanges(6, 6), expectedRanges, text.getStyleRanges(6, 6, false), expectedStyles);
    StyleRange[] styles = text.getStyleRanges(6, 6, true);
    int[] ranges = new int[styles.length * 2];
    for (int i = 0; i < ranges.length; i += 2) {
        ranges[i] = styles[i / 2].start;
        ranges[i + 1] = styles[i / 2].length;
    }
    testStyles("Test 24 (bug 212851 - getStyleRanges)", ranges, expectedRanges, text.getStyleRanges(6, 6, false), expectedStyles);
    expectedRanges = new int[] { 6, 2, 13, 1 };
    expectedStyles = new StyleRange[] { style2, style0 };
    testStyles("Test 25 ", text.getRanges(6, 8), expectedRanges, text.getStyleRanges(6, 8, false), expectedStyles);
    styles = text.getStyleRanges(6, 8, true);
    ranges = new int[styles.length * 2];
    for (int i = 0; i < ranges.length; i += 2) {
        ranges[i] = styles[i / 2].start;
        ranges[i + 1] = styles[i / 2].length;
    }
    testStyles("Test 26 ", ranges, expectedRanges, text.getStyleRanges(6, 8, false), expectedStyles);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Example 73 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testFont.

@Test
public void testFont() {
    styledText.setText("a\nb\nc\nd");
    // Create little, big font
    Font initialFont = styledText.getFont();
    FontData[] fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(24);
    }
    Font bigFont = new Font(styledText.getDisplay(), fontData);
    fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(fontData[i].getHeight() - 1);
    }
    Font littleFont = new Font(styledText.getDisplay(), fontData);
    int length = styledText.getCharCount();
    // No style
    assertVariableLineHeightEquals(0, 0);
    // style with big font
    StyleRange style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = bigFont;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0, bigFont);
    // style with no font
    style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = null;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0);
    // style with big font
    style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = bigFont;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0, bigFont);
    // style with little font
    style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = littleFont;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0);
    // style with no font
    style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = null;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0);
    // style with little font
    style = new StyleRange();
    style.start = 0;
    style.length = length;
    style.foreground = styledText.getDisplay().getSystemColor(SWT.COLOR_BLUE);
    style.font = littleFont;
    styledText.replaceStyleRanges(0, length, new StyleRange[] { style });
    assertVariableLineHeightEquals(0, 0);
    bigFont.dispose();
    littleFont.dispose();
}
Also used : FontData(org.eclipse.swt.graphics.FontData) StyleRange(org.eclipse.swt.custom.StyleRange) Font(org.eclipse.swt.graphics.Font) Test(org.junit.Test)

Example 74 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testBigFontSeveralRanges.

@Test
public void testBigFontSeveralRanges() {
    styledText.setText("abc\nd");
    // Create little, big font
    Font initialFont = styledText.getFont();
    FontData[] fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(fontData[i].getHeight() * 2);
    }
    Font bigFont = new Font(styledText.getDisplay(), fontData);
    fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(fontData[i].getHeight() / 2);
    }
    Font littleFont = new Font(styledText.getDisplay(), fontData);
    StyleRange[] ranges = new StyleRange[2];
    ranges[0] = new StyleRange();
    ranges[0].start = 0;
    ranges[0].length = 1;
    ranges[0].font = bigFont;
    ranges[1] = new StyleRange();
    ranges[1].start = 1;
    ranges[1].length = 1;
    ranges[1].font = littleFont;
    styledText.replaceStyleRanges(0, 3, ranges);
    // line height is the height of the big font
    assertVariableLineHeightEquals(0, 0, bigFont);
    bigFont.dispose();
    littleFont.dispose();
}
Also used : FontData(org.eclipse.swt.graphics.FontData) StyleRange(org.eclipse.swt.custom.StyleRange) Font(org.eclipse.swt.graphics.Font) Test(org.junit.Test)

Example 75 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_custom_StyledText_VariableLineHeight method testLittleFontSeveralRanges.

@Test
public void testLittleFontSeveralRanges() {
    styledText.setText("abc\nd");
    // Create little, big font
    Font initialFont = styledText.getFont();
    FontData[] fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(fontData[i].getHeight() / 2);
    }
    Font littleFont1 = new Font(styledText.getDisplay(), fontData);
    fontData = initialFont.getFontData();
    for (int i = 0; i < fontData.length; i++) {
        fontData[i].setHeight(fontData[i].getHeight() / 4);
    }
    Font littleFont2 = new Font(styledText.getDisplay(), fontData);
    StyleRange[] ranges = new StyleRange[2];
    ranges[0] = new StyleRange();
    ranges[0].start = 0;
    ranges[0].length = 1;
    ranges[0].font = littleFont1;
    ranges[1] = new StyleRange();
    ranges[1].start = 1;
    ranges[1].length = 1;
    ranges[1].font = littleFont2;
    styledText.replaceStyleRanges(0, 3, ranges);
    // 2 little fonts, line height is the height of the default font of StyledText
    assertVariableLineHeightEquals(0, 0);
    littleFont1.dispose();
    littleFont2.dispose();
}
Also used : FontData(org.eclipse.swt.graphics.FontData) StyleRange(org.eclipse.swt.custom.StyleRange) Font(org.eclipse.swt.graphics.Font) Test(org.junit.Test)

Aggregations

StyleRange (org.eclipse.swt.custom.StyleRange)145 Point (org.eclipse.swt.graphics.Point)52 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)25 Color (org.eclipse.swt.graphics.Color)22 StyledText (org.eclipse.swt.custom.StyledText)13 Font (org.eclipse.swt.graphics.Font)10 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)10 Matcher (java.util.regex.Matcher)9 FontData (org.eclipse.swt.graphics.FontData)8 RGB (org.eclipse.swt.graphics.RGB)8 Rectangle (org.eclipse.swt.graphics.Rectangle)7 IOException (java.io.IOException)5 TextPresentation (org.eclipse.jface.text.TextPresentation)5 Image (org.eclipse.swt.graphics.Image)5 Control (org.eclipse.swt.widgets.Control)5 Position (org.eclipse.jface.text.Position)4 TextStyle (org.eclipse.swt.graphics.TextStyle)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3