private void updateLabels() { Object value = source.getPropertyValue(descriptor.getId()); if (nameLayout != null && !nameLayout.isDisposed()) { nameLayout.setText(descriptor.getDisplayName()); nameLayout.setStyle( new TextStyle(canvas.getFont(), canvas.getForeground(), null), 0, nameLayout.getText().length()); } ILabelDescriptor labelDescriptor = descriptor.getLabelDescriptor(); ImageDescriptor image = labelDescriptor == null ? null : labelDescriptor.getImage(value); if (valueImage != null) { valueImage.dispose(); } valueImage = image == null ? null : image.createImage(false, Display.getCurrent()); ColorDescriptor color = labelDescriptor == null ? null : labelDescriptor.getForeground(value); if (valueColor != null) { valueColor.dispose(); } valueColor = color == null ? null : color.createColor(Display.getCurrent()); FontDescriptor font = labelDescriptor == null ? null : labelDescriptor.getFont(value); if (valueFont != null) { valueFont.dispose(); } valueFont = font == null ? null : font.createFont(Display.getCurrent()); if (valueLayout != null && !valueLayout.isDisposed()) { String valueText = labelDescriptor == null ? (value == null ? "" //$NON-NLS-1$ : value.toString()) : labelDescriptor.getText(value); if (valueText == null) valueText = ""; // $NON-NLS-1$ valueLayout.setText(valueText); valueLayout.setStyle( new TextStyle( valueFont == null ? canvas.getFont() : valueFont, valueColor == null ? canvas.getForeground() : valueColor, null), 0, valueText.length()); } canvas.layout(true); canvas.redraw(); }
/** * Sets the receiver's text. * * <p>The string can contain both regular text and hyperlinks. A hyperlink is delimited by an * anchor tag, <A> and </A>. Within an anchor, a single HREF attribute is supported. * When a hyperlink is selected, the text field of the selection event contains either the text of * the hyperlink or the value of its HREF, if one was specified. In the rare case of identical * hyperlinks within the same string, the HREF attribute can be used to distinguish between them. * The string may include the mnemonic character and line delimiters. The only delimiter the HREF * attribute supports is the quotation mark ("). * * <p>Mnemonics are indicated by an '&' that causes the next character to be the mnemonic. The * receiver can have a mnemonic in the text preceding each link. When the user presses a key * sequence that matches the mnemonic, focus is assigned to the link that follows the text. * Mnemonics in links and in the trailing text are ignored. On most platforms, the mnemonic * appears underlined but may be emphasised in a platform specific manner. The mnemonic indicator * character '&' can be escaped by doubling it in the string, causing a single '&' to be * displayed. * * @param string the new text * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the text is null * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setText(String string) { checkWidget(); if (string == null) error(SWT.ERROR_NULL_ARGUMENT); if (string.equals(text)) return; text = string; layout.setText(parse(string)); focusIndex = offsets.length > 0 ? 0 : -1; selection.x = selection.y = -1; boolean enabled = (state & DISABLED) == 0; TextStyle linkStyle = new TextStyle(null, enabled ? linkColor : disabledColor, null); linkStyle.underline = true; int[] bidiSegments = new int[offsets.length * 2]; for (int i = 0; i < offsets.length; i++) { Point point = offsets[i]; layout.setStyle(linkStyle, point.x, point.y); bidiSegments[i * 2] = point.x; bidiSegments[i * 2 + 1] = point.y + 1; } layout.setSegments(bidiSegments); TextStyle mnemonicStyle = new TextStyle(null, null, null); mnemonicStyle.underline = true; for (int i = 0; i < mnemonics.length; i++) { int mnemonic = mnemonics[i]; if (mnemonic != -1) { layout.setStyle(mnemonicStyle, mnemonic, mnemonic); } } redraw(); }
/** * Shorten the given text <code>t</code> so that its length doesn't exceed the given width. The * default implementation replaces characters in the center of the original string with an * ellipsis ("..."). Override if you need a different strategy. * * @param gc the gc to use for text measurement * @param t the text to shorten * @param width the width to shorten the text to, in pixels * @return the shortened text */ protected String shortenText(GC gc, String t, int width) { if (t == null) return null; int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; if (width <= w) return t; int l = t.length(); int max = l / 2; int min = 0; int mid = (max + min) / 2 - 1; if (mid <= 0) return t; TextLayout layout = new TextLayout(getDisplay()); layout.setText(t); mid = validateOffset(layout, mid); while (min < mid && mid < max) { String s1 = t.substring(0, mid); String s2 = t.substring(validateOffset(layout, l - mid), l); int l1 = gc.textExtent(s1, DRAW_FLAGS).x; int l2 = gc.textExtent(s2, DRAW_FLAGS).x; if (l1 + w + l2 > width) { max = mid; mid = validateOffset(layout, (max + min) / 2); } else if (l1 + w + l2 < width) { min = mid; mid = validateOffset(layout, (max + min) / 2); } else { min = max; } } String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l - mid), l); layout.dispose(); return result; }
@Test public void test_getLineIndex() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getLineIndex(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text = "0123456\n890123\n"; layout.setText(text); assertEquals(0, layout.getLineIndex(0)); assertEquals(0, layout.getLineIndex(5)); assertEquals(0, layout.getLineIndex(7)); assertEquals(1, layout.getLineIndex(8)); assertEquals(1, layout.getLineIndex(12)); assertEquals(1, layout.getLineIndex(14)); assertEquals(2, layout.getLineIndex(15)); try { layout.getLineIndex(-1); fail("invalid range expection expected"); } catch (Exception e) { } try { layout.getLineIndex(text.length() + 1); fail("invalid range expection expected"); } catch (Exception e) { } layout.dispose(); }
@Test public void test_getText() { TextLayout layout = new TextLayout(display); String text = "Test"; layout.setText(text); assertEquals(text, layout.getText()); layout.dispose(); }
@Test public void test_getLineOffsets() { TextLayout layout = new TextLayout(display); String text = "0123456\n890123\n"; layout.setText(text); int[] offsets = layout.getLineOffsets(); int[] expected = new int[] {0, 8, 15, 15}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText(""); offsets = layout.getLineOffsets(); expected = new int[] {0, 0}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText("\n"); offsets = layout.getLineOffsets(); expected = new int[] {0, 1, 1}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText("WMWM"); int width = layout.getBounds().width; layout.setWidth(width / 4 + 1); offsets = layout.getLineOffsets(); expected = new int[] {0, 1, 2, 3, 4}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setWidth(width / 2 + 1); offsets = layout.getLineOffsets(); expected = new int[] {0, 2, 4}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.dispose(); }
@Test public void test_getLineBounds() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getLineBonds(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text = "0123456\n890123\n"; layout.setText(text); Rectangle rect0 = layout.getLineBounds(0); Rectangle rect1 = layout.getLineBounds(1); Rectangle rect2 = layout.getLineBounds(2); assertTrue(rect0.width > rect1.width); assertEquals(0, rect2.width); assertEquals(0, rect0.x); assertEquals(0, rect1.x); assertEquals(0, rect2.x); Rectangle bounds = layout.getBounds(); assertEquals(bounds.width, rect0.width); layout.setWidth(bounds.width); layout.setAlignment(SWT.CENTER); assertEquals(bounds, layout.getBounds()); layout.setWidth(bounds.width + 100); Rectangle newRect1 = layout.getLineBounds(0); assertEquals(50, newRect1.x); layout.setAlignment(SWT.RIGHT); newRect1 = layout.getLineBounds(0); assertEquals(100, newRect1.x); assertEquals(bounds.height, rect0.height + rect1.height + rect2.height); try { layout.getLineBounds(-1); fail("invalid range expection expected"); } catch (Exception e) { } try { layout.getLineBounds(3); fail("invalid range expection expected"); } catch (Exception e) { } layout.dispose(); }
/** * Sets the receiver's text. * * <p>The string can contain both regular text and hyperlinks. A hyperlink is delimited by an * anchor tag, <A> and </A>. Within an anchor, a single HREF attribute is supported. * When a hyperlink is selected, the text field of the selection event contains either the text of * the hyperlink or the value of its HREF, if one was specified. In the rare case of identical * hyperlinks within the same string, the HREF attribute can be used to distinguish between them. * The string may include the mnemonic character and line delimiters. The only delimiter the HREF * attribute supports is the quotation mark ("). * * <p>Mnemonics are indicated by an '&' that causes the next character to be the mnemonic. The * receiver can have a mnemonic in the text preceding each link. When the user presses a key * sequence that matches the mnemonic, focus is assigned to the link that follows the text. * Mnemonics in links and in the trailing text are ignored. On most platforms, the mnemonic * appears underlined but may be emphasised in a platform specific manner. The mnemonic indicator * character '&' can be escaped by doubling it in the string, causing a single '&' to be * displayed. * * @param string the new text * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the text is null * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setText(String string) { checkWidget(); if (string == null) error(SWT.ERROR_NULL_ARGUMENT); if (string.equals(text)) return; text = string; if (OS.COMCTL32_MAJOR >= 6) { boolean enabled = OS.IsWindowEnabled(handle); /* * Bug in Windows. For some reason, when SetWindowText() * is used to set the text of a link control to the empty * string, the old text remains. The fix is to set the * text to a space instead. */ if (string.length() == 0) string = " "; // $NON-NLS-1$ TCHAR buffer = new TCHAR(getCodePage(), string, true); OS.SetWindowText(handle, buffer); parse(text); enableWidget(enabled); } else { layout.setText(parse(text)); focusIndex = offsets.length > 0 ? 0 : -1; selection.x = selection.y = -1; int bits = OS.GetWindowLong(handle, OS.GWL_STYLE); if (offsets.length > 0) { bits |= OS.WS_TABSTOP; } else { bits &= ~OS.WS_TABSTOP; } OS.SetWindowLong(handle, OS.GWL_STYLE, bits); boolean enabled = OS.IsWindowEnabled(handle); TextStyle linkStyle = new TextStyle(null, enabled ? linkColor : disabledColor, null); linkStyle.underline = true; for (int i = 0; i < offsets.length; i++) { Point point = offsets[i]; layout.setStyle(linkStyle, point.x, point.y); } TextStyle mnemonicStyle = new TextStyle(null, null, null); mnemonicStyle.underline = true; for (int i = 0; i < mnemonics.length; i++) { int mnemonic = mnemonics[i]; if (mnemonic != -1) { layout.setStyle(mnemonicStyle, mnemonic, mnemonic); } } redraw(); } }
@Test public void test_getNextOffset2() { if (SwtTestUtil.isGTK || SwtTestUtil.isCocoa) { // TODO Fix GTK and Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getNextOffset2(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)"); } return; } // Text thai cluster separate so it can be excluded // for gtk, testing machine (rhel4) is too old to // support thai. TextLayout layout = new TextLayout(display); layout.setText("A\u0E19\u0E49\u0E33B"); String[] messages = { "no segments", "segments", "segments (duplicate at 0)", "segments (duplicate at 1)", "segments (duplicate at 2)", "segments (duplicate at 3)", "segments (duplicate at 4)", "segments (duplicate at 5)" }; // int[][] segments = {null, {0, 1, 2, 3, 4, 5}, {0, 0, 1, 2, 3, 4, 5}, {0, 1, 1, 2, 3, 4, // 5}, {0, 1, 2, 2, 3, 4, 5}, {0, 1, 2, 3, 3, 4, 5}, // {0, 1, 2, 3, 4, 4, 5}, {0, 1, 2, 3, 4, 5, 5}}; int[][] segments = {null}; for (int i = 0; i < segments.length; i++) { String m = messages[i]; layout.setSegments(segments[i]); assertEquals(m, 1, layout.getNextOffset(0, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 4, layout.getNextOffset(1, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 5, layout.getNextOffset(4, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 4, layout.getPreviousOffset(5, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 1, layout.getPreviousOffset(4, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 0, layout.getPreviousOffset(1, SWT.MOVEMENT_CLUSTER)); } layout.dispose(); }
private void decideShellSize(Shell newShell) { GC gc = new GC(newShell); Rectangle imgRect = getImage().getBounds(); Point extent = gc.textExtent(getTitle()); int titleWidth = imgRect.width + extent.x + 27; TextLayout layout = new TextLayout(newShell.getDisplay()); layout.setWidth(215); layout.setText(getTip()); shellWidth = Math.max(titleWidth, 215 + getLeftMargin() + getRightMargin() + 6); shellHeight = layout.getBounds().height + getTopMargin() + getBottomMargin() + Math.max(18, extent.y + 6) + 13 + btnDefault.getSize().y; gc.dispose(); layout.dispose(); }
@Test public void test_getLocation() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getLocation(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text = "AB\u05E9\u05E0"; layout.setText(text); assertEquals(0, layout.getLocation(0, false).x); assertEquals(layout.getLocation(0, true).x, layout.getLocation(1, false).x); assertEquals(layout.getLocation(2, false).x, layout.getLineBounds(0).width); assertEquals(layout.getLocation(2, true).x, layout.getLocation(3, false).x); assertEquals(layout.getLocation(3, true).x, layout.getLocation(1, true).x); assertEquals(layout.getLocation(4, false).x, layout.getLineBounds(0).width); assertEquals(layout.getLocation(4, true).x, layout.getLineBounds(0).width); layout.dispose(); }
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED); shell.setText("Underline, Strike Out"); Font font = shell.getFont(); String text = "Here is some text that is underlined or struck out or both."; final TextLayout layout = new TextLayout(display); layout.setText(text); TextStyle style1 = new TextStyle(font, null, null); style1.underline = true; layout.setStyle(style1, 26, 35); TextStyle style2 = new TextStyle(font, null, null); style2.strikeout = true; layout.setStyle(style2, 40, 49); TextStyle style3 = new TextStyle(font, null, null); style3.underline = true; style3.strikeout = true; layout.setStyle(style3, 54, 57); shell.addListener( SWT.Paint, new Listener() { @Override public void handleEvent(Event event) { Point point = new Point(10, 10); int width = shell.getClientArea().width - 2 * point.x; layout.setWidth(width); layout.draw(event.gc, point.x, point.y); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } layout.dispose(); display.dispose(); }
/*Test suite start*/ @Test public void test_getLevel() { TextLayout layout = new TextLayout(display); String text = "abc55\u05e9\u05e066"; layout.setText(text); assertEquals(0, layout.getLevel(0)); assertEquals(0, layout.getLevel(2)); // assertEquals(0, layout.getLevel(4)); //bug in windows (uniscribe) assertEquals(1, layout.getLevel(5)); assertEquals(1, layout.getLevel(6)); assertEquals(2, layout.getLevel(7)); assertEquals(0, layout.getLevel(9)); try { layout.getLevel(-1); fail("invalid range expection expected"); } catch (Exception e) { } try { layout.getLevel(text.length() + 1); fail("invalid range expection expected"); } catch (Exception e) { } layout.dispose(); }
@Test public void test_getOffset() { boolean isCocoa = SwtTestUtil.isCocoa; if (isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Partially excluded test_getOffset(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } } TextLayout layout = new TextLayout(display); String text = "AB \u05E9\u05E0 CD\nHello"; layout.setText(text); int[] trailing = new int[1]; assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); Point point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); Rectangle bounds = layout.getBounds(); layout.setWidth(bounds.width + 100); layout.setAlignment(SWT.CENTER); assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); layout.setAlignment(SWT.RIGHT); assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); if (!isCocoa) assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); text = "Text"; layout.setText(text); int width = layout.getBounds().width; layout.setAlignment(SWT.LEFT); assertEquals(0, layout.getOffset(1, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1, 0, null)); layout.setWidth(width + 100); layout.setAlignment(SWT.CENTER); assertEquals(0, layout.getOffset(1 + 50, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1 + 50, 0, null)); layout.setAlignment(SWT.RIGHT); assertEquals(0, layout.getOffset(1 + 100, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1 + 100, 0, null)); layout.dispose(); }
@Test public void test_getSegments() { TextLayout layout = new TextLayout(display); layout.setText("AB"); String[] messages = { "no segments", "segments", "segments (duplicate at 0)", "segments (duplicate at 1)", "segments (duplicate at 2)" }; int[][] segments = {null, {0, 1, 2}, {0, 0, 1, 2}, {0, 1, 1, 2}, {0, 1, 2, 2}}; for (int i = 0; i < segments.length; i++) { String m = messages[i]; layout.setSegments(segments[i]); assertEquals(m, 1, layout.getNextOffset(0, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 2, layout.getNextOffset(1, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 2, layout.getNextOffset(2, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 1, layout.getPreviousOffset(2, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 0, layout.getPreviousOffset(1, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 0, layout.getPreviousOffset(0, SWT.MOVEMENT_CLUSTER)); } // Bug 295513 layout.setText("Line"); layout.setAscent(20); layout.setDescent(6); layout.setSegments(new int[] {0, layout.getText().length()}); layout.getBounds(); layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 74 layout.setText("word word word"); layout.setSegments(new int[] {0, 5, 10}); int offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(14, offset); layout.setWidth(layout.getBounds().width / 2); layout.setAscent(20); layout.setDescent(6); offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(14, offset); layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 64 layout.setText("\nAB"); layout.setSegments(new int[] {0, 1, 3}); int[] expected = new int[] {0, 1, 3}; int[] offsets = layout.getLineOffsets(); for (int i = 0; i < offsets.length; i++) { assertEquals(" i = " + i, expected[i], offsets[i]); } layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 80 layout.setText("MNMNMN"); layout.setSegments(new int[] {0, 1, 6}); layout.getBounds(); assertEquals( layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null)); layout.setAscent(9); assertEquals( layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null)); layout.dispose(); layout = new TextLayout(display); // Bug 486600 comment 2 layout.setText("Abcdef\nGhij\nKl"); Rectangle bounds1 = layout.getBounds(0, 6 - 1); int[] trailing = new int[1]; offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing); assertEquals(10, offset); assertEquals(1, trailing[0]); layout.dispose(); layout = new TextLayout(display); layout.setText("AbcdefGhijKl"); layout.setSegments(new int[] {6, 10}); layout.setSegmentsChars(new char[] {'\n', '\n'}); bounds1 = layout.getBounds(0, 6 - 1); trailing = new int[1]; offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing); assertEquals(9, offset); assertEquals(1, trailing[0]); layout.dispose(); layout = new TextLayout(display); // Lina's bug (bug 241482, comment 37) boolean doit = false; // known to be broken if (doit) { int length = layout.getText().length(); layout.setSegments(new int[] {length}); trailing = new int[1]; int width = layout.getBounds().width + 20; assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing)); assertEquals("hit test to the left (trailing)", 0, trailing[0]); assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing)); assertEquals("hit test to the right (trailing)", 1, trailing[0]); layout.setSegmentsChars(new char[] {'*'}); assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing)); assertEquals("hit test to the left (trailing)", 0, trailing[0]); assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing)); assertEquals("hit test to the right (trailing)", 1, trailing[0]); } /* wrong: internal testing */ // String text = "AB"; // int textLength = text.length(); // layout.setText(text); // messages = new String [] { // "no segments", // "segments", // "segments (duplicate at 0)", // "segments (duplicate at 1)", // "segments (duplicate at 2)" }; // segments = new int [][] { // null, // { 0, 1, 2 }, // { 0, 0, 1, 2 }, // { 0, 1, 1, 2 }, // { 0, 1, 2, 2 } }; // int[][] translatedOffsets = { // { 0, 1, 2 }, // { 1, 3, 5 }, // { 2, 4, 6 }, // { 1, 4, 6 }, // { 1, 3, 6 } }; // int[][] untranslatedOffsets = { // { 0, 1, 2 }, // { 0, 0, 1, 1, 2, 2 }, // { 0, 0, 0, 1, 1, 2, 2 }, // { 0, 0, 1, 1, 1, 2, 2 }, // { 0, 0, 1, 1, 2, 2, 2 } }; // for (int i = 0; i < segments.length; i++) { // layout.setSegments(segments[i]); // layout.getBounds(); // for (int j = 0; j <= textLength; j++) { // assertEquals(messages[i] + " j = " + j, translatedOffsets[i][j], // layout.translateOffset(j)); // } // for (int j = 0, n = layout.getSegments() == null ? 0 : textLength + // layout.getSegments().length; j < n; j++) { // assertEquals(messages[i] + " j = " + j, untranslatedOffsets[i][j], // layout.untranslateOffset(j)); // } // } layout.dispose(); }
@Test public void test_setStyle() { TextLayout layout; TextStyle s1, s2, s3, s4; s1 = new TextStyle(null, null, null); s2 = new TextStyle(null, null, null); s3 = new TextStyle(null, null, null); s4 = new TextStyle(null, null, null); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 2, 3); assertEquals(null, layout.getStyle(0)); assertEquals(null, layout.getStyle(1)); assertEquals(s1, layout.getStyle(2)); assertEquals(s1, layout.getStyle(3)); assertEquals(null, layout.getStyle(4)); assertEquals(null, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); assertEquals(s1, layout.getStyle(0)); assertEquals(s1, layout.getStyle(1)); assertEquals(s2, layout.getStyle(2)); assertEquals(s2, layout.getStyle(3)); assertEquals(s3, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(s4, 1, 2); assertEquals(s1, layout.getStyle(0)); assertEquals(s4, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s2, layout.getStyle(3)); assertEquals(s3, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(s4, 3, 4); assertEquals(s1, layout.getStyle(0)); assertEquals(s1, layout.getStyle(1)); assertEquals(s2, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s4, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(s4, 1, 4); assertEquals(s1, layout.getStyle(0)); assertEquals(s4, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s4, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 0); layout.setStyle(s2, 1, 4); layout.setStyle(s3, 5, 5); layout.setStyle(s4, 2, 3); assertEquals(s1, layout.getStyle(0)); assertEquals(s2, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s2, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(s4, 0, 3); assertEquals(s4, layout.getStyle(0)); assertEquals(s4, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s3, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 0); layout.setStyle(s2, 1, 4); layout.setStyle(s3, 5, 5); layout.setStyle(s4, 2, 4); assertEquals(s1, layout.getStyle(0)); assertEquals(s2, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s4, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 0); layout.setStyle(s2, 1, 4); layout.setStyle(s3, 5, 5); layout.setStyle(s4, 1, 3); assertEquals(s1, layout.getStyle(0)); assertEquals(s4, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s2, layout.getStyle(4)); assertEquals(s3, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(null, 0, 5); assertEquals(null, layout.getStyle(0)); assertEquals(null, layout.getStyle(1)); assertEquals(null, layout.getStyle(2)); assertEquals(null, layout.getStyle(3)); assertEquals(null, layout.getStyle(4)); assertEquals(null, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 0, 1); layout.setStyle(s2, 2, 3); layout.setStyle(s3, 4, 5); layout.setStyle(s4, 0, 5); assertEquals(s4, layout.getStyle(0)); assertEquals(s4, layout.getStyle(1)); assertEquals(s4, layout.getStyle(2)); assertEquals(s4, layout.getStyle(3)); assertEquals(s4, layout.getStyle(4)); assertEquals(s4, layout.getStyle(5)); layout.dispose(); layout = new TextLayout(display); layout.setText("aabbcc"); layout.setStyle(s1, 2, 2); layout.setStyle(s2, 2, 3); layout.setStyle(null, 3, 3); assertEquals(null, layout.getStyle(0)); assertEquals(null, layout.getStyle(1)); assertEquals(s2, layout.getStyle(2)); assertEquals(null, layout.getStyle(3)); assertEquals(null, layout.getStyle(4)); assertEquals(null, layout.getStyle(5)); layout.dispose(); }
@Test public void test_getNextOffset() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getNextOffset(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text; int offset; text = "word word word"; layout.setText(text); offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(4, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(9, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(14, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(9, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(4, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_END); assertEquals(0, offset); assertEquals(4, layout.getNextOffset(2, SWT.MOVEMENT_WORD_END)); assertEquals(4, layout.getNextOffset(3, SWT.MOVEMENT_WORD_END)); assertEquals(9, layout.getNextOffset(8, SWT.MOVEMENT_WORD_END)); assertEquals(14, layout.getNextOffset(10, SWT.MOVEMENT_WORD_END)); assertEquals(14, layout.getNextOffset(13, SWT.MOVEMENT_WORD_END)); assertEquals(14, layout.getNextOffset(14, SWT.MOVEMENT_WORD_END)); assertEquals(0, layout.getPreviousOffset(0, SWT.MOVEMENT_WORD_END)); assertEquals(0, layout.getPreviousOffset(4, SWT.MOVEMENT_WORD_END)); assertEquals(4, layout.getPreviousOffset(5, SWT.MOVEMENT_WORD_END)); assertEquals(9, layout.getPreviousOffset(10, SWT.MOVEMENT_WORD_END)); assertEquals(9, layout.getPreviousOffset(11, SWT.MOVEMENT_WORD_END)); offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(14, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(0, offset); assertEquals(5, layout.getNextOffset(2, SWT.MOVEMENT_WORD_START)); assertEquals(5, layout.getNextOffset(4, SWT.MOVEMENT_WORD_START)); assertEquals(10, layout.getNextOffset(9, SWT.MOVEMENT_WORD_START)); assertEquals(14, layout.getNextOffset(10, SWT.MOVEMENT_WORD_START)); assertEquals(14, layout.getNextOffset(13, SWT.MOVEMENT_WORD_START)); assertEquals(14, layout.getNextOffset(14, SWT.MOVEMENT_WORD_START)); assertEquals(0, layout.getPreviousOffset(0, SWT.MOVEMENT_WORD_START)); assertEquals(0, layout.getPreviousOffset(3, SWT.MOVEMENT_WORD_START)); assertEquals(0, layout.getPreviousOffset(4, SWT.MOVEMENT_WORD_START)); assertEquals(0, layout.getPreviousOffset(5, SWT.MOVEMENT_WORD_START)); assertEquals(5, layout.getPreviousOffset(6, SWT.MOVEMENT_WORD_START)); text = "AB \u05E9\u05E0 CD\nHello"; layout.setText(text); offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(3, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(6, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(8, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(6, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(3, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(0, offset); offset = 7; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(8, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(9, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(text.length(), offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(9, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(8, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); assertEquals(9, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); assertEquals(10, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); assertEquals(9, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); assertEquals(8, offset); offset = layout.getPreviousOffset(offset, SWT.MOVEMENT_CLUSTER); assertEquals(7, offset); for (int i = 0; i < text.length(); i++) { assertEquals(i + 1, layout.getNextOffset(i, SWT.MOVEMENT_CLUSTER)); } for (int i = text.length(); i > 0; i--) { assertEquals(i - 1, layout.getPreviousOffset(i, SWT.MOVEMENT_CLUSTER)); } layout.dispose(); }
@Test public void test_getSegmentsChars() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getSegmentsChars(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text = "ab\u05d0\u05d1.\u05d2cd"; int textLength = text.length(); layout.setText(text); String[] messages = { "no segments", "Embedding RTL dir test", "Embedding LTR dir test", "LRO test", "RLO test", "Traditional segments", "Traditional segments invalid" }; int[][] segments = { null, {0, 0, 4, 4, 5, 5, 8, 8}, {0, 0, 4, 4, 5, 5, 8, 8}, {0, textLength}, {0, textLength}, {0, 4, 8}, {1} }; char[][] chars = { null, {'\u202a', '\u202b', '\u202c', '\u200e', '\u200e', '\u202b', '\u202c', '\u202c'}, {'\u202b', '\u202a', '\u202c', '\u200f', '\u200f', '\u202a', '\u202c', '\u202c'}, {0x202d, 0x202c}, {0x202e, 0x202c}, null, null }; int[][] levels = { {0, 0, 1, 1, 1, 1, 0, 0}, {4, 4, 3, 3, 2, 3, 4, 4}, {2, 2, 3, 3, 1, 3, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}, // Fails on cocoa, where it returns {0, 0, 0, 0, 0, 0, 0, 0} {1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 1, 1, 0, 1, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 0} }; int[] offsets = {0, textLength}; for (int i = 0; i < segments.length; i++) { layout.setSegments(segments[i]); layout.setSegmentsChars(chars[i]); assertArrayEquals("Test line offsets" + ": group: " + i, offsets, layout.getLineOffsets()); for (int j = 0; j < textLength; j++) { assertEquals( messages[i] + ": group: " + i + ", index: " + j, levels[i][j], layout.getLevel(j)); } } layout.dispose(); }