| 1 | """simple Python binding for fribidi. |
| 2 | |
| 3 | pyfribidi uses libfribidi to order text visually using the unicode |
| 4 | algorithm. pyfribidi can also convert text from visual order to |
| 5 | logical order, but the conversion may be wrong in certain cases. |
| 6 | """ |
| 7 | |
| 8 | __version__ = "0.12.0" |
| 9 | import six |
| 10 | from _pyfribidi import LTR, ON, RTL, log2vis as _log2vis |
| 11 | |
| 12 | def log2vis(logical, base_direction=RTL, encoding="utf-8", clean=False, reordernsm=True): |
| 13 | """ |
| 14 | Return string reordered visually according to base direction. |
| 15 | Return the same type of input string, either unicode or string using |
| 16 | encoding. |
| 17 | |
| 18 | Note that this function does not handle line breaking. You should |
| 19 | call log2vis with each line. |
| 20 | |
| 21 | Arguments: |
| 22 | - logical: unicode or encoded string |
| 23 | - base_direction: optional logical base direction. Accepts one of |
| 24 | the constants LTR, RTL or ON, defined in this module. ON calculate |
| 25 | the base direction according to the BiDi algorithm. |
| 26 | - encoding: optional string encoding (ignored for unicode input) |
| 27 | """ |
| 28 | |
| 29 | if not isinstance(logical, six.text_type): |
| 30 | logical = six.text_type(logical, encoding) |
| 31 | else: |
| 32 | encoding = None |
| 33 | res = _log2vis(logical, base_direction=base_direction, clean=clean, reordernsm=reordernsm) |
| 34 | if encoding: |
| 35 | return res.encode(encoding) |
| 36 | return res |