Rectangle[] getRectangles(int linkIndex) { int lineCount = layout.getLineCount(); Rectangle[] rects = new Rectangle[lineCount]; int[] lineOffsets = layout.getLineOffsets(); Point point = offsets[linkIndex]; int lineStart = 1; while (point.x > lineOffsets[lineStart]) lineStart++; int lineEnd = 1; while (point.y > lineOffsets[lineEnd]) lineEnd++; int index = 0; if (lineStart == lineEnd) { rects[index++] = layout.getBounds(point.x, point.y); } else { rects[index++] = layout.getBounds(point.x, lineOffsets[lineStart] - 1); rects[index++] = layout.getBounds(lineOffsets[lineEnd - 1], point.y); if (lineEnd - lineStart > 1) { for (int i = lineStart; i < lineEnd - 1; i++) { rects[index++] = layout.getLineBounds(i); } } } if (rects.length != index) { Rectangle[] tmp = new Rectangle[index]; System.arraycopy(rects, 0, tmp, 0, index); rects = tmp; } return rects; }
@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(); }
@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(); }