Java Code Examples for java.text.Bidi#DIRECTION_DEFAULT_LEFT_TO_RIGHT

The following examples show how to use java.text.Bidi#DIRECTION_DEFAULT_LEFT_TO_RIGHT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditorView.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void reinitSettings() {
  assertIsDispatchThread();
  synchronized (myLock) {
    myPlainSpaceWidth = -1;
    myTabSize = -1;
    setFontRenderContext(null);
  }
  switch (EditorSettingsExternalizable.getInstance().getBidiTextDirection()) {
    case LTR:
      myBidiFlags = Bidi.DIRECTION_LEFT_TO_RIGHT;
      break;
    case RTL:
      myBidiFlags = Bidi.DIRECTION_RIGHT_TO_LEFT;
      break;
    default:
      myBidiFlags = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
  }
  myLogicalPositionCache.reset(false);
  myTextLayoutCache.resetToDocumentSize(false);
  invalidateFoldRegionLayouts();
  myCharWidthCache.clear();
  setPrefix(myPrefixText, myPrefixAttributes); // recreate prefix layout
  mySizeManager.reset();
}
 
Example 2
Source File: Helper.java    From memoir with Apache License 2.0 6 votes vote down vote up
/**
 * This method determines if the direction of a substring is right-to-left.
 * If the string is empty that determination is based on the default system language
 * Locale.getDefault().
 * The method can handle invalid substring definitions (start > end etc.), in which case the
 * method returns False.
 *
 * @return True if the text direction is right-to-left, false otherwise.
 */
public static boolean isRTL(CharSequence s, int start, int end) {
    if (s == null || s.length() == 0) {
        // empty string --> determine the direction from the default language
        return isRTL(Locale.getDefault());
    }

    if (start == end) {
        // if no character is selected we need to expand the selection
        start = Math.max(0, --start);
        if (start == end) {
            end = Math.min(s.length(), ++end);
        }
    }

    try {
        Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        return ! bidi.baseIsLeftToRight();
    }
    catch (IndexOutOfBoundsException e) {
        return false;
    }
}
 
Example 3
Source File: BidiSurrogateTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, boolean directionIsRTL) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    if (bidi.isMixed()) {
        throw new RuntimeException("bidi is mixed");
    }
    if (bidi.isRightToLeft() != directionIsRTL) {
        throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
    }
}
 
Example 4
Source File: BidiSurrogateTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, int rtlstart, int rtllength) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi.getRunCount(); ++i) {
        if ((bidi.getRunLevel(i) & 1) != 0) {
            if (bidi.getRunStart(i) != rtlstart ||
                bidi.getRunLimit(i) != rtlstart + rtllength) {
                throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
            }
            break;
        }
    }
}
 
Example 5
Source File: BidiSurrogateTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, boolean directionIsRTL) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    if (bidi.isMixed()) {
        throw new RuntimeException("bidi is mixed");
    }
    if (bidi.isRightToLeft() != directionIsRTL) {
        throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
    }
}
 
Example 6
Source File: BidiSurrogateTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, boolean directionIsRTL) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    if (bidi.isMixed()) {
        throw new RuntimeException("bidi is mixed");
    }
    if (bidi.isRightToLeft() != directionIsRTL) {
        throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
    }
}
 
Example 7
Source File: BidiSurrogateTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, boolean directionIsRTL) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    if (bidi.isMixed()) {
        throw new RuntimeException("bidi is mixed");
    }
    if (bidi.isRightToLeft() != directionIsRTL) {
        throw new RuntimeException("bidi is not " + (directionIsRTL ? "rtl" : "ltr"));
    }
}
 
Example 8
Source File: BidiSurrogateTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, int rtlstart, int rtllength) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi.getRunCount(); ++i) {
        if ((bidi.getRunLevel(i) & 1) != 0) {
            if (bidi.getRunStart(i) != rtlstart ||
                bidi.getRunLimit(i) != rtlstart + rtllength) {
                throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
            }
            break;
        }
    }
}
 
Example 9
Source File: BidiSurrogateTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, int rtlstart, int rtllength) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi.getRunCount(); ++i) {
        if ((bidi.getRunLevel(i) & 1) != 0) {
            if (bidi.getRunStart(i) != rtlstart ||
                bidi.getRunLimit(i) != rtlstart + rtllength) {
                throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
            }
            break;
        }
    }
}
 
Example 10
Source File: BidiSurrogateTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, int rtlstart, int rtllength) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi.getRunCount(); ++i) {
        if ((bidi.getRunLevel(i) & 1) != 0) {
            if (bidi.getRunStart(i) != rtlstart ||
                bidi.getRunLimit(i) != rtlstart + rtllength) {
                throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
            }
            break;
        }
    }
}
 
Example 11
Source File: BidiSurrogateTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void testBidi(String string, int rtlstart, int rtllength) {
    Bidi bidi = new Bidi(string, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi.getRunCount(); ++i) {
        if ((bidi.getRunLevel(i) & 1) != 0) {
            if (bidi.getRunStart(i) != rtlstart ||
                bidi.getRunLimit(i) != rtlstart + rtllength) {
                throw new RuntimeException("first rtl run didn't match " + rtlstart + ", " + rtllength);
            }
            break;
        }
    }
}
 
Example 12
Source File: PDFTextStripper.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given
 * word. If the word is a full line, the results will be the best. If the word contains of single words or
 * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and
 * characters!
 * 
 * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx
 * 
 * @param word The word that shall be processed
 * @return new word with the correct direction of the containing characters
 */
private String handleDirection(String word)
{
    Bidi bidi = new Bidi(word, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);

    // if there is pure LTR text no need to process further
    if (!bidi.isMixed() && bidi.getBaseLevel() == Bidi.DIRECTION_LEFT_TO_RIGHT)
    {
        return word;
    }
    
    // collect individual bidi information
    int runCount = bidi.getRunCount();
    byte[] levels = new byte[runCount];
    Integer[] runs = new Integer[runCount];
  
    for (int i = 0; i < runCount; i++)
    {
       levels[i] = (byte)bidi.getRunLevel(i);
       runs[i] = i;
    }

    // reorder individual parts based on their levels
    Bidi.reorderVisually(levels, 0, runs, 0, runCount);
    
    // collect the parts based on the direction within the run
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < runCount; i++)
    {
       int index = runs[i];
       int start = bidi.getRunStart(index);
       int end = bidi.getRunLimit(index);

        int level = levels[index];

        if ((level & 1) != 0)
        {
            while (--end >= start)
            {
                char character = word.charAt(end);
                if (Character.isMirrored(word.codePointAt(end)))
                {
                    if (MIRRORING_CHAR_MAP.containsKey(character))
                    {
                        result.append(MIRRORING_CHAR_MAP.get(character));
                    }
                    else
                    {
                        result.append(character);
                    }
                }
                else
                {
                    result.append(character);
                }
            }
        }
        else
        {
            result.append(word, start, end);
        }
    }
    
    return result.toString();
}
 
Example 13
Source File: BidiUtils.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the LTR and RTL direction of the given words. The whole implementation stands and falls with the given
 * word. If the word is a full line, the results will be the best. If the word contains of single words or
 * characters, the order of the characters in a word or words in a line may wrong, due to RTL and LTR marks and
 * characters!
 * 
 * Based on http://www.nesterovsky-bros.com/weblog/2013/07/28/VisualToLogicalConversionInJava.aspx
 * 
 * @param text The word that shall be processed
 * @return new word with the correct direction of the containing characters
 */
public static String visualToLogical(String text)
{
    if (!CharUtils.isBlank(text))
    {
        Bidi bidi = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        if (!bidi.isLeftToRight())
        {
            // collect individual bidi information
            int runCount = bidi.getRunCount();
            byte[] levels = new byte[runCount];
            Integer[] runs = new Integer[runCount];

            for (int i = 0; i < runCount; i++)
            {
                levels[i] = (byte) bidi.getRunLevel(i);
                runs[i] = i;
            }

            // reorder individual parts based on their levels
            Bidi.reorderVisually(levels, 0, runs, 0, runCount);

            // collect the parts based on the direction within the run
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < runCount; i++)
            {
                int index = runs[i];
                int start = bidi.getRunStart(index);
                int end = bidi.getRunLimit(index);
                int level = levels[index];

                if ((level & 1) != 0)
                {
                    while (--end >= start)
                    {
                        char character = text.charAt(end);
                        if (Character.isMirrored(text.codePointAt(end))
                                && MIRRORING_CHAR_MAP.containsKey(character))
                        {
                            result.append(MIRRORING_CHAR_MAP.get(character));
                        }
                        else
                        {
                            result.append(character);
                        }
                    }
                }
                else
                {
                    result.append(text, start, end);
                }
            }

            return result.toString();
        }
    }
    return text;
}
 
Example 14
Source File: BidiEmbeddingTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 15
Source File: BidiEmbeddingTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 16
Source File: BidiEmbeddingTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 17
Source File: BidiEmbeddingTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 18
Source File: BidiEmbeddingTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 19
Source File: BidiEmbeddingTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}
 
Example 20
Source File: BidiEmbeddingTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test1() {
    String target = "BACK WARDS";
    String str = "If this text is >" + target + "< the test passed.";
    int start = str.indexOf(target);
    int limit = start + target.length();

    System.out.println("start: " + start + " limit: " + limit);

    AttributedString astr = new AttributedString(str);
    astr.addAttribute(TextAttribute.BIDI_EMBEDDING,
                     new Integer(-1),
                     start,
                     limit);

    Bidi bidi = new Bidi(astr.getIterator());

    for (int i = 0; i < bidi.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi.getRunStart(i) +
                           " to " + bidi.getRunLimit(i) +
                           " at level " + bidi.getRunLevel(i));
    }

    System.out.println(bidi);

    byte[] embs = new byte[str.length() + 3];
    for (int i = start + 1; i < limit + 1; ++i) {
        embs[i] = -1;
    }

    Bidi bidi2 = new Bidi(str.toCharArray(), 0, embs, 1, str.length(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    for (int i = 0; i < bidi2.getRunCount(); ++i) {
        System.out.println("run " + i +
                           " from " + bidi2.getRunStart(i) +
                           " to " + bidi2.getRunLimit(i) +
                           " at level " + bidi2.getRunLevel(i));
    }

    System.out.println(bidi2 + "\n");

    if (bidi.getRunCount() != 3 || bidi2.getRunCount() != 3) {
        throw new Error("Bidi run count incorrect");
    } else {
        System.out.println("test1() passed.\n");
    }
}