예제 #1
0
 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_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_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_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();
  }