Пример #1
0
  public void testSimpleHebrewParagraph() {
    bd = new Bidi("\u05D0", Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(1, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(1, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertFalse(bd.isMixed());
    assertTrue(bd.isRightToLeft());

    bd = new Bidi("\u05D0", Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(1, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(1, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertFalse(bd.isMixed());
    assertTrue(bd.isRightToLeft());

    bd = new Bidi("\u05D0", Bidi.DIRECTION_RIGHT_TO_LEFT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(1, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(1, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertFalse(bd.isMixed());
    assertTrue(bd.isRightToLeft());
  }
Пример #2
0
  /**
   * Updates the <code>TextMeasurer</code> after a single character has been deleted from the
   * paragraph currently represented by this <code>TextMeasurer</code>. After this call, this <code>
   * TextMeasurer</code> is equivalent to a new <code>TextMeasurer</code> created from the text;
   * however, it will usually be more efficient to update an existing <code>TextMeasurer</code> than
   * to create a new one from scratch.
   *
   * @param newParagraph the text of the paragraph after performing the deletion. Cannot be null.
   * @param deletePos the position in the text where the character was removed. Must not be less
   *     than the start of <code>newParagraph</code>, and must not be greater than the end of <code>
   *     newParagraph</code>.
   * @throws IndexOutOfBoundsException if <code>deletePos</code> is less than the start of <code>
   *     newParagraph</code> or greater than the end of <code>newParagraph</code>
   * @throws NullPointerException if <code>newParagraph</code> is <code>null</code>
   */
  public void deleteChar(AttributedCharacterIterator newParagraph, int deletePos) {

    fStart = newParagraph.getBeginIndex();
    int end = newParagraph.getEndIndex();
    if (end - fStart != fChars.length - 1) {
      initAll(newParagraph);
    }

    char[] newChars = new char[end - fStart];
    int changedIndex = deletePos - fStart;

    System.arraycopy(fChars, 0, newChars, 0, deletePos - fStart);
    System.arraycopy(fChars, changedIndex + 1, newChars, changedIndex, end - deletePos);
    fChars = newChars;

    if (fBidi != null) {
      fBidi = new Bidi(newParagraph);
      if (fBidi.isLeftToRight()) {
        fBidi = null;
      }
    }

    fParagraph = StyledParagraph.deleteChar(newParagraph, fChars, deletePos, fParagraph);
    invalidateComponents();
  }
Пример #3
0
 public void testCreateLineBidi() {
   bd =
       new Bidi(
           "a\u05D0a\u05D0a\u05D0\"\u05D0a".toCharArray(),
           0,
           new byte[] {0, 0, 0, -3, -3, 2, 2, 0, 3},
           0,
           9,
           Bidi.DIRECTION_RIGHT_TO_LEFT);
   Bidi line = bd.createLineBidi(2, 7);
   assertFalse(line.baseIsLeftToRight());
   assertEquals(1, line.getBaseLevel());
   assertEquals(5, line.getLength());
   assertEquals(2, line.getLevelAt(0));
   assertEquals(3, line.getLevelAt(1));
   assertEquals(3, line.getLevelAt(2));
   assertEquals(3, line.getLevelAt(3));
   assertEquals(2, line.getLevelAt(4));
   assertEquals(1, line.getLevelAt(1000));
   assertEquals(3, line.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 1, 2}, {1, 4, 3}, {4, 5, 2},
       },
       line);
   assertFalse(line.isLeftToRight());
   assertTrue(line.isMixed());
   assertFalse(line.isRightToLeft());
 }
Пример #4
0
 public void testComplicatedOverrideBidi() {
   bd =
       new Bidi(
           "a\u05D0a\"a\u05D0\"\u05D0a".toCharArray(),
           0,
           new byte[] {0, 0, 0, -3, -3, 2, 2, 0, 3},
           0,
           9,
           Bidi.DIRECTION_RIGHT_TO_LEFT);
   assertFalse(bd.baseIsLeftToRight());
   assertEquals(1, bd.getBaseLevel());
   assertEquals(9, bd.getLength());
   assertEquals(2, bd.getLevelAt(0));
   assertEquals(1, bd.getLevelAt(1));
   assertEquals(2, bd.getLevelAt(2));
   assertEquals(3, bd.getLevelAt(3));
   assertEquals(3, bd.getLevelAt(4));
   assertEquals(3, bd.getLevelAt(5));
   assertEquals(2, bd.getLevelAt(6));
   assertEquals(1, bd.getLevelAt(7));
   assertEquals(4, bd.getLevelAt(8));
   assertEquals(1, bd.getLevelAt(1000));
   assertEquals(7, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 1, 2}, {1, 2, 1}, {2, 3, 2}, {3, 6, 3}, {6, 7, 2}, {7, 8, 1}, {8, 9, 4},
       },
       bd);
   assertFalse(bd.isLeftToRight());
   assertTrue(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #5
0
 public void testComplicatedBidi() {
   bd = new Bidi("a\u05D0a\"a\u05D0\"\u05D0a", Bidi.DIRECTION_RIGHT_TO_LEFT);
   assertFalse(bd.baseIsLeftToRight());
   assertEquals(1, bd.getBaseLevel());
   assertEquals(9, bd.getLength());
   assertEquals(2, bd.getLevelAt(0));
   assertEquals(1, bd.getLevelAt(1));
   assertEquals(2, bd.getLevelAt(2));
   assertEquals(2, bd.getLevelAt(3));
   assertEquals(2, bd.getLevelAt(4));
   assertEquals(1, bd.getLevelAt(5));
   assertEquals(1, bd.getLevelAt(6));
   assertEquals(1, bd.getLevelAt(7));
   assertEquals(2, bd.getLevelAt(8));
   assertEquals(1, bd.getLevelAt(1000));
   assertEquals(5, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 1, 2}, {1, 2, 1}, {2, 5, 2}, {5, 8, 1}, {8, 9, 2},
       },
       bd);
   assertFalse(bd.isLeftToRight());
   assertTrue(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #6
0
 public void testRelativeEmbeddings() {
   bd =
       new Bidi(
           new char[] {'s', 's', 's'},
           0,
           new byte[] {(byte) 1, (byte) 2, (byte) 3},
           0,
           3,
           Bidi.DIRECTION_RIGHT_TO_LEFT);
   assertFalse(bd.baseIsLeftToRight());
   assertEquals(1, bd.getBaseLevel());
   assertEquals(3, bd.getLength());
   assertEquals(2, bd.getLevelAt(0));
   assertEquals(2, bd.getLevelAt(1));
   assertEquals(4, bd.getLevelAt(2));
   assertEquals(1, bd.getLevelAt(1000));
   assertEquals(2, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 2, 2}, {2, 3, 4},
       },
       bd);
   assertFalse(bd.isLeftToRight());
   assertTrue(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #7
0
 public void testIncompatibleLineAlgorithm() {
   // ICU treat a new line as in the same run, however RI does not
   bd =
       new Bidi(
           "aaaaa".toCharArray(),
           0,
           new byte[] {-2, -1, -3, -3, -2},
           0,
           5,
           Bidi.DIRECTION_RIGHT_TO_LEFT);
   Bidi line = bd.createLineBidi(1, 4);
   assertFalse(line.baseIsLeftToRight());
   assertEquals(1, line.getBaseLevel());
   assertEquals(3, line.getLength());
   assertEquals(1, line.getLevelAt(0));
   assertEquals(1, line.getLevelAt(1));
   assertEquals(1, line.getLevelAt(2));
   assertEquals(1, line.getLevelAt(1000));
   assertEquals(1, line.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 3, 1},
       },
       line);
   assertFalse(line.isLeftToRight());
   assertFalse(line.isMixed());
   assertTrue(line.isRightToLeft());
 }
Пример #8
0
 // this is essentially the same bug as Bug_1
 public void testRIBug_2() {
   bd = new Bidi("\u05D0", Bidi.DIRECTION_LEFT_TO_RIGHT);
   assertTrue(bd.baseIsLeftToRight());
   assertEquals(0, bd.getBaseLevel());
   assertEquals(1, bd.getLength());
   assertEquals(1, bd.getLevelAt(0));
   assertEquals(0, bd.getLevelAt(1000));
   assertEquals(1, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 1, 1},
       },
       bd);
   assertFalse(bd.isLeftToRight());
   assertTrue(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #9
0
 public void testBadFlags() {
   bd = new Bidi("", 173);
   assertTrue(bd.baseIsLeftToRight());
   assertEquals(0, bd.getBaseLevel());
   assertEquals(0, bd.getLength());
   assertEquals(0, bd.getLevelAt(0));
   assertEquals(0, bd.getLevelAt(1000));
   assertEquals(1, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 0, 0},
       },
       bd);
   assertTrue(bd.isLeftToRight());
   assertFalse(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #10
0
 /*
  * spec reads: public static final int DIRECTION_RIGHT_TO_LEFT Constant
  * indicating base direction is right-to-left. according to that, the method
  * baseIsLeftToRight() here should return false. however, RI doesn't act so.
  */
 public void testRIBug_1() {
   bd = new Bidi("t", Bidi.DIRECTION_RIGHT_TO_LEFT);
   assertFalse(bd.baseIsLeftToRight());
   // the base level it the essential cause
   assertEquals(1, bd.getBaseLevel());
   assertEquals(1, bd.getLength());
   assertEquals(2, bd.getLevelAt(0));
   assertEquals(1, bd.getLevelAt(1000));
   assertEquals(1, bd.getRunCount());
   assertRunArrayEquals(
       new int[][] {
         {0, 1, 2},
       },
       bd);
   assertFalse(bd.isLeftToRight());
   assertTrue(bd.isMixed());
   assertFalse(bd.isRightToLeft());
 }
Пример #11
0
  /**
   * Updates the <code>TextMeasurer</code> after a single character has been inserted into the
   * paragraph currently represented by this <code>TextMeasurer</code>. After this call, this <code>
   * TextMeasurer</code> is equivalent to a new <code>TextMeasurer</code> created from the text;
   * however, it will usually be more efficient to update an existing <code>TextMeasurer</code> than
   * to create a new one from scratch.
   *
   * @param newParagraph the text of the paragraph after performing the insertion. Cannot be null.
   * @param insertPos the position in the text where the character was inserted. Must not be less
   *     than the start of <code>newParagraph</code>, and must be less than the end of <code>
   *     newParagraph</code>.
   * @throws IndexOutOfBoundsException if <code>insertPos</code> is less than the start of <code>
   *     newParagraph</code> or greater than or equal to the end of <code>newParagraph</code>
   * @throws NullPointerException if <code>newParagraph</code> is <code>null</code>
   */
  public void insertChar(AttributedCharacterIterator newParagraph, int insertPos) {

    if (collectStats) {
      printStats();
    }
    if (wantStats) {
      collectStats = true;
    }

    fStart = newParagraph.getBeginIndex();
    int end = newParagraph.getEndIndex();
    if (end - fStart != fChars.length + 1) {
      initAll(newParagraph);
    }

    char[] newChars = new char[end - fStart];
    int newCharIndex = insertPos - fStart;
    System.arraycopy(fChars, 0, newChars, 0, newCharIndex);

    char newChar = newParagraph.setIndex(insertPos);
    newChars[newCharIndex] = newChar;
    System.arraycopy(fChars, newCharIndex, newChars, newCharIndex + 1, end - insertPos - 1);
    fChars = newChars;

    if (fBidi != null
        || Bidi.requiresBidi(newChars, newCharIndex, newCharIndex + 1)
        || newParagraph.getAttribute(TextAttribute.BIDI_EMBEDDING) != null) {

      fBidi = new Bidi(newParagraph);
      if (fBidi.isLeftToRight()) {
        fBidi = null;
      }
    }

    fParagraph = StyledParagraph.insertChar(newParagraph, fChars, insertPos, fParagraph);
    invalidateComponents();
  }
Пример #12
0
  /** Initialize state, including fChars array, direction, and fBidi. */
  private void initAll(AttributedCharacterIterator text) {

    fStart = text.getBeginIndex();

    // extract chars
    fChars = new char[text.getEndIndex() - fStart];

    int n = 0;
    for (char c = text.first(); c != CharacterIterator.DONE; c = text.next()) {
      fChars[n++] = c;
    }

    text.first();

    fBidi = new Bidi(text);
    if (fBidi.isLeftToRight()) {
      fBidi = null;
    }

    text.first();
    Map<? extends Attribute, ?> paragraphAttrs = text.getAttributes();
    NumericShaper shaper = AttributeValues.getNumericShaping(paragraphAttrs);
    if (shaper != null) {
      shaper.shape(fChars, 0, fChars.length);
    }

    fParagraph = new StyledParagraph(text, fChars);

    // set paragraph attributes
    {
      // If there's an embedded graphic at the start of the
      // paragraph, look for the first non-graphic character
      // and use it and its font to initialize the paragraph.
      // If not, use the first graphic to initialize.
      fJustifyRatio = AttributeValues.getJustification(paragraphAttrs);

      boolean haveFont = TextLine.advanceToFirstFont(text);

      if (haveFont) {
        Font defaultFont = TextLine.getFontAtCurrentPos(text);
        int charsStart = text.getIndex() - text.getBeginIndex();
        LineMetrics lm = defaultFont.getLineMetrics(fChars, charsStart, charsStart + 1, fFrc);
        fBaseline = (byte) lm.getBaselineIndex();
        fBaselineOffsets = lm.getBaselineOffsets();
      } else {
        // hmmm what to do here?  Just try to supply reasonable
        // values I guess.

        GraphicAttribute graphic =
            (GraphicAttribute) paragraphAttrs.get(TextAttribute.CHAR_REPLACEMENT);
        fBaseline = TextLayout.getBaselineFromGraphic(graphic);
        Hashtable<Attribute, ?> fmap = new Hashtable<>(5, (float) 0.9);
        Font dummyFont = new Font(fmap);
        LineMetrics lm = dummyFont.getLineMetrics(" ", 0, 1, fFrc);
        fBaselineOffsets = lm.getBaselineOffsets();
      }
      fBaselineOffsets = TextLine.getNormalizedOffsets(fBaselineOffsets, fBaseline);
    }

    invalidateComponents();
  }
Пример #13
0
  public void testHebrewOverrideEmbeddings() {
    bd =
        new Bidi(
            new char[] {'\u05D0', '\u05D0', '\u05D0'},
            0,
            new byte[] {(byte) -1, (byte) -2, (byte) -3},
            0,
            3,
            Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(3, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(2, bd.getLevelAt(1));
    assertEquals(3, bd.getLevelAt(2));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(3, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1}, {1, 2, 2}, {2, 3, 3},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertTrue(bd.isMixed());
    assertFalse(bd.isRightToLeft());

    bd =
        new Bidi(
            new char[] {'\u05D0', '\u05D0', '\u05D0'},
            0,
            new byte[] {(byte) -1, (byte) -2, (byte) -3},
            0,
            3,
            Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(3, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(2, bd.getLevelAt(1));
    assertEquals(3, bd.getLevelAt(2));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(3, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1}, {1, 2, 2}, {2, 3, 3},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertTrue(bd.isMixed());
    assertFalse(bd.isRightToLeft());

    bd =
        new Bidi(
            new char[] {'\u05D0', '\u05D0', '\u05D0'},
            0,
            new byte[] {(byte) -1, (byte) -2, (byte) -3},
            0,
            3,
            Bidi.DIRECTION_LEFT_TO_RIGHT);
    assertTrue(bd.baseIsLeftToRight());
    assertEquals(0, bd.getBaseLevel());
    assertEquals(3, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(2, bd.getLevelAt(1));
    assertEquals(3, bd.getLevelAt(2));
    assertEquals(0, bd.getLevelAt(1000));
    assertEquals(3, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1}, {1, 2, 2}, {2, 3, 3},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertTrue(bd.isMixed());
    assertFalse(bd.isRightToLeft());

    bd =
        new Bidi(
            new char[] {'\u05D0', '\u05D0', '\u05D0'},
            0,
            new byte[] {(byte) -1, (byte) -2, (byte) -3},
            0,
            3,
            Bidi.DIRECTION_RIGHT_TO_LEFT);
    assertFalse(bd.baseIsLeftToRight());
    assertEquals(1, bd.getBaseLevel());
    assertEquals(3, bd.getLength());
    assertEquals(1, bd.getLevelAt(0));
    assertEquals(2, bd.getLevelAt(1));
    assertEquals(3, bd.getLevelAt(2));
    assertEquals(1, bd.getLevelAt(1000));
    assertEquals(3, bd.getRunCount());
    assertRunArrayEquals(
        new int[][] {
          {0, 1, 1}, {1, 2, 2}, {2, 3, 3},
        },
        bd);
    assertFalse(bd.isLeftToRight());
    assertTrue(bd.isMixed());
    assertFalse(bd.isRightToLeft());
  }