RSS 2.0
Sign In
# Sunday, July 28, 2013

Time after time we run into the same problem on different platforms, with different languages. The problem's name is "Visual to Logical conversion for right-to-left or bidirectional text". The problem is usually due to legacy code, which stores texts in visual order from left to right. In case of English it's ok, but with Hebrew this means that texts are partially reversed.

It worth to note that we've solved the same task with Windows API for native and .NET applications more than 10 years ago.

On the other hand, for Java, we yet didn't see any acceptable standalone solution. To remedy this omission, we publish here our solution to this problem.

package com.nesterovskyBros.text;

import java.text.Bidi;

 * Utility that uses {@link Bidi} class. 
public class BidiUtils
   * Implements visual to logical order converter.
   * @author <a href="">Nesterovsky bros</a>
   * @param text an input text in visual order to convert.
   * @return a String value in logical order.
  public static String visualToLogical(String text)
    if ((text == null) || (text.length() == 0))
        return text;
    Bidi bidi = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
    if (bidi.isLeftToRight())
        return text;
    int count = bidi.getRunCount();
    byte[] levels = new byte[count];
    Integer[] runs = new Integer[count];
    for (int i = 0; i < count; i++)
       levels[i] = (byte)bidi.getRunLevel(i);
       runs[i] = i;
    Bidi.reorderVisually(levels, 0, runs, 0, count);

    StringBuilder result = new StringBuilder();

    for (int i = 0; i < count; i++)
       int index = runs[i];
       int start = bidi.getRunStart(index);
       int end = bidi.getRunLimit(index);
       int level = levels[index];
       if ((level & 1) != 0)
         for (; --end >= start;)
         result.append(text, start, end);

    return result.toString();

This method utilizes embeded Bidi's algorithm, see class java.text.Bidi.

Be aware that there is no perfect algorithm that covers all possible cases, since BIDI was written for an opposite task, but our implementation based on Bidi.reorderVisually is usually acceptable.

Here is an JUnit test for this method:

package com.nesterovskyBors.text;

import static org.junit.Assert.*;

import org.junit.Test;

import com.nesterovskyBros.text.BidiUtils;

public class BidiUtilsTests

  public void testsVisualToLogical()
    String text = "123 יתימאה ןחבמ";
    String actual = BidiUtils.visualToLogical(text);
    String expected = "מבחן האמיתי 123";
    assertEquals(expected, actual);
    text = "תירבע English תירבע בוש";
    actual = BidiUtils.visualToLogical(text);
    expected = "שוב עברית English עברית";
    assertEquals(expected, actual);
Sunday, July 28, 2013 3:44:17 PM UTC  #    Comments [0] -
Java | Tips and tricks
All comments require the approval of the site owner before being displayed.
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview
<January 2021>
Total Posts: 377
This Year: 1
This Month: 1
This Week: 0
Comments: 758
Locations of visitors to this page
The opinions expressed herein are our own personal opinions and do not represent our employer's view in anyway.

© 2021, Nesterovsky bros
All Content © 2021, Nesterovsky bros
DasBlog theme 'Business' created by Christoph De Baene (delarou)