using freebidi from python using ctypes
GNU FriBidi is an implementation of the Unicode Bidirectional Algorithm (bidi). There is a Python binding PyFribidi, but it is not complete. What I need is not a visual presentation of a string, but information where direction is changing. This function is not provided by the binding, therefore I've made an alternative using ctypes.
Download: http://uucode.com/download/2014/ctfribidi.zip.
License: public domain.
It was a chance for me to experiment with ctypes. With google and gdb, the success is archived. Don't use my code as a source for inspiration. Most likely, some things can be done better.
Usage:
import ctfribidi
s = u"......"
# Get the visual representation
s2 = ctfribidi.log2vis(s, 1)
print repr(s2)
# Mark direction changes
levels = ctfribidi.log2levels(s, 1)
s2 = ctfribidi.insert_markers(s, levels, 1)
print repr(s2)
The function log2levels (from fribidi) returns an array. Each member corresponds to a character of the source string and is the nesting direction level.
The Python code insert_markers uses the array to insert markers into the string. The markers are the characters:
- LEFT-TO-RIGHT EMBEDDING (U+202A)
- RIGHT-TO-LEFT EMBEDDING (U+202B)
- POP DIRECTIONAL FORMATTING (U+202C)
So far I see, the nesting is correct. Each LRE and RLE is closed by PDF. But nothing promises this desired behaviour.
Inside the function one finds commented-out code to insert ZWNJ:
- ZERO WIDTH NON-JOINER' (U+200C)
It was an attempt to solve a comma-position problem: http://tex.stackexchange.com/questions/214773/. For my needs, the problem was solved by other means, but maybe you still want the original attempt.