diff options
author | Behnam Esfahbod <behnam@zwnj.org> | 2008-12-02 02:29:23 +0330 |
---|---|---|
committer | Behnam ZWNJ Esfahbod <behnam@zwnj.org> | 2008-12-02 02:29:23 +0330 |
commit | 530802453518b251f4e20213e70169bb688bb95a (patch) | |
tree | eff3fce9e431a920ca727312f66ead66f7f1323a |
First working try
-rwxr-xr-x | fribidi.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/fribidi.py b/fribidi.py new file mode 100755 index 0000000..19fd62e --- /dev/null +++ b/fribidi.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# coding=UTF-8 + +import ctypes +import sys + + +libfribidi = ctypes.CDLL("libfribidi.so") + + +def malloc_utf8_array (l): + Utf8Array = ctypes.c_char * l + return Utf8Array() + +def malloc_utf8_array_from_string (s): + return ctypes.c_char_p(s) + +def malloc_utc32_array (l): + Utc32Array = ctypes.c_uint32 * l + return Utc32Array() + + +def pyunicode_to_utc32_p (a_unicode): + print + print 'Enter pyunicode_to_utc32_p' + print + + utf8_pystr = a_unicode.encode('utf-8') + utf8_len = len(utf8_pystr) + utf8_p = malloc_utf8_array_from_string(utf8_pystr) + + print 'utf8_p.value', utf8_p.value + print 'utf8_len', utf8_len + + utc32_p = malloc_utc32_array(utf8_len+1) + libfribidi.fribidi_utf8_to_unicode (utf8_p, utf8_len, utc32_p) + + print 'utc32_p [%04x, %04x, %04x, %04x]' % (utc32_p[0], utc32_p[1], utc32_p[2], utc32_p[3]) + + print + + # XX: Caller should free it! + return utc32_p + + +def utc32_p_to_pyunicode (a_utc32_p, a_len): + print + print 'Enter utc32_p_to_pyunicode' + print + + print 'a_utc32_p [%04x, %04x, %04x, %04x]' % (a_utc32_p[0], a_utc32_p[1], a_utc32_p[2], a_utc32_p[3]) + + utf8_p = malloc_utf8_array(6*a_len+1) + libfribidi.fribidi_unicode_to_utf8 (a_utc32_p, a_len, utf8_p) + + print + + return utf8_p.value + + + +def log2vis (input_pyunicode): + l = len(input_pyunicode) + print 'input_pyunicode', input_pyunicode + print 'l', l + print + + input_utc32_p = pyunicode_to_utc32_p(input_pyunicode) + + #print libfribidi.fribidi_log2vis() + output_utc32_p = input_utc32_p + + output_u = utc32_p_to_pyunicode(output_utc32_p, l) + print 'output_u', output_u + + + +if __name__=='__main__': + i = u"سلام" + + o = log2vis(i) + + print o + |