| 1 |
|
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program; if not, write to the Free Software |
| 15 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 |
*/ |
| 17 |
|
| 18 |
/* Copyright (C) 2005,2006,2010 Yaacov Zamir, Nir Soffer */ |
| 19 |
|
| 20 |
|
| 21 |
#include <Python.h> |
| 22 |
#include <fribidi.h> |
| 23 |
|
| 24 |
#if PY_MAJOR_VERSION >= 3 |
| 25 |
#define PYFRIBIDI_UNICODE_OBJECT PyObject |
| 26 |
#else |
| 27 |
#define PYFRIBIDI_UNICODE_OBJECT PyUnicodeObject |
| 28 |
#endif |
| 29 |
|
| 30 |
static PyObject * |
| 31 |
unicode_log2vis (PYFRIBIDI_UNICODE_OBJECT * string, |
| 32 |
FriBidiParType base_direction, int clean, int reordernsm) |
| 33 |
{ |
| 34 |
int i; |
| 35 |
#if PY_MAJOR_VERSION >= 3 |
| 36 |
Py_ssize_t length = PyUnicode_GetLength(string); |
| 37 |
#else |
| 38 |
int length = string->length; |
| 39 |
#endif |
| 40 |
FriBidiChar *logical = NULL; /* input fribidi unicode buffer */ |
| 41 |
FriBidiChar *visual = NULL; /* output fribidi unicode buffer */ |
| 42 |
PYFRIBIDI_UNICODE_OBJECT *result = NULL; |
| 43 |
|
| 44 |
/* Allocate fribidi unicode buffers |
| 45 |
TODO - Don't copy strings if sizeof(FriBidiChar) == sizeof(Py_UNICODE) |
| 46 |
*/ |
| 47 |
|
| 48 |
logical = PyMem_New (FriBidiChar, length + 1); |
| 49 |
if (logical == NULL) { |
| 50 |
PyErr_NoMemory(); |
| 51 |
goto cleanup; |
| 52 |
} |
| 53 |
|
| 54 |
visual = PyMem_New (FriBidiChar, length + 1); |
| 55 |
if (visual == NULL) { |
| 56 |
PyErr_NoMemory(); |
| 57 |
goto cleanup; |
| 58 |
} |
| 59 |
|
| 60 |
PyUnicode_AsWideChar(string, logical, length); |
| 61 |
|
| 62 |
/* Convert to unicode and order visually */ |
| 63 |
fribidi_set_reorder_nsm(reordernsm); |
| 64 |
|
| 65 |
if (!fribidi_log2vis (logical, length, &base_direction, visual, |
| 66 |
NULL, NULL, NULL)) { |
| 67 |
|
| 68 |
PyErr_SetString (PyExc_RuntimeError, |
| 69 |
"fribidi failed to order string"); |
| 70 |
goto cleanup; |
| 71 |
} |
| 72 |
|
| 73 |
/* Cleanup the string if requested */ |
| 74 |
if (clean) { |
| 75 |
length = fribidi_remove_bidi_marks (visual, length, NULL, NULL, NULL); |
| 76 |
} |
| 77 |
|
| 78 |
result = PyUnicode_FromWideChar(visual, length); |
| 79 |
if (result == NULL) { |
| 80 |
goto cleanup; |
| 81 |
} |
| 82 |
|
| 83 |
cleanup: |
| 84 |
/* Delete unicode buffers */ |
| 85 |
PyMem_Del (logical); |
| 86 |
PyMem_Del (visual); |
| 87 |
|
| 88 |
return (PyObject *)result; |
| 89 |
} |
| 90 |
|
| 91 |
static PyObject * |
| 92 |
_pyfribidi_log2vis (PyObject * self, PyObject * args, PyObject * kw) |
| 93 |
{ |
| 94 |
PYFRIBIDI_UNICODE_OBJECT *logical = NULL; /* input unicode or string object */ |
| 95 |
FriBidiParType base = FRIBIDI_TYPE_RTL; /* optional direction */ |
| 96 |
int clean = 0; /* optional flag to clean the string */ |
| 97 |
int reordernsm = 1; /* optional flag to allow reordering of non spacing marks*/ |
| 98 |
|
| 99 |
static char *kwargs[] = |
| 100 |
{ "logical", "base_direction", "clean", "reordernsm", NULL }; |
| 101 |
|
| 102 |
if (!PyArg_ParseTupleAndKeywords (args, kw, "U|iii", kwargs, |
| 103 |
&logical, &base, &clean, &reordernsm)) { |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
/* Validate base */ |
| 108 |
|
| 109 |
if (!(base == FRIBIDI_TYPE_RTL |
| 110 |
|| base == FRIBIDI_TYPE_LTR |
| 111 |
|| base == FRIBIDI_TYPE_ON)) { |
| 112 |
return PyErr_Format (PyExc_ValueError, |
| 113 |
"invalid value %d: use either RTL, LTR or ON", |
| 114 |
base); |
| 115 |
} |
| 116 |
|
| 117 |
PyObject * return_pyobject = unicode_log2vis (logical, base, clean, reordernsm); |
| 118 |
|
| 119 |
return return_pyobject; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static PyMethodDef PyfribidiMethods[] = { |
| 124 |
{"log2vis", (PyCFunction) _pyfribidi_log2vis, METH_VARARGS | METH_KEYWORDS, NULL}, |
| 125 |
{NULL, NULL, 0, NULL} |
| 126 |
}; |
| 127 |
|
| 128 |
#if PY_MAJOR_VERSION >= 3 |
| 129 |
|
| 130 |
static struct PyModuleDef pyfribidi_moduledef = { |
| 131 |
PyModuleDef_HEAD_INIT, /* m_base */ |
| 132 |
"pyfribidi", /* m_name */ |
| 133 |
NULL, /* m_doc */ |
| 134 |
-1, /* m_size == -1 means cannot be reinitialized */ |
| 135 |
PyfribidiMethods, // |
| 136 |
NULL, // myextension_traverse, |
| 137 |
NULL, // myextension_clear, |
| 138 |
NULL |
| 139 |
}; |
| 140 |
|
| 141 |
PyMODINIT_FUNC |
| 142 |
PyInit__pyfribidi (void) |
| 143 |
#else |
| 144 |
|
| 145 |
void |
| 146 |
init_pyfribidi (void) |
| 147 |
#endif |
| 148 |
{ |
| 149 |
#if PY_MAJOR_VERSION >= 3 |
| 150 |
PyObject *module = PyModule_Create (&pyfribidi_moduledef); |
| 151 |
#else |
| 152 |
PyObject *module = Py_InitModule ("_pyfribidi", PyfribidiMethods); |
| 153 |
#endif |
| 154 |
if (module == NULL) return NULL; |
| 155 |
|
| 156 |
PyModule_AddIntConstant (module, "RTL", (long) FRIBIDI_TYPE_RTL); |
| 157 |
PyModule_AddIntConstant (module, "LTR", (long) FRIBIDI_TYPE_LTR); |
| 158 |
PyModule_AddIntConstant (module, "ON", (long) FRIBIDI_TYPE_ON); |
| 159 |
#if PY_MAJOR_VERSION >= 3 |
| 160 |
return module; |
| 161 |
#endif |
| 162 |
} |