Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rtl_preprocess.py « bl_i18n_utils « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d28f87cf04253bac03f1440d2c4e87e80b031403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/python3

# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENSE BLOCK *****

# <pep8 compliant>

# Preprocess right-to-left languages.
# You can use it either standalone, or through import_po_from_branches or
# update_trunk.
#
# Notes: This has been tested on Linux, not 100% it will work nicely on
#        Windows or OsX.
#        This uses ctypes, as there is no py3 binding for fribidi currently.
#        This implies you only need the compiled C library to run it.
#        Finally, note that it handles some formating/escape codes (like
#        \", %s, %x12, %.4f, etc.), protecting them from ugly (evil) fribidi,
#        which seems completely unaware of such things (as unicode is...).

import sys
import ctypes
import re

try:
    import settings
    import utils
except:
    from . import (settings, utils)


FRIBIDI_LIB = settings.FRIBIDI_LIB

###### Import C library and recreate "defines". #####
fbd = ctypes.CDLL(FRIBIDI_LIB)


#define FRIBIDI_MASK_NEUTRAL	0x00000040L	/* Is neutral */
FRIBIDI_PAR_ON = 0x00000040


#define FRIBIDI_FLAG_SHAPE_MIRRORING	0x00000001
#define FRIBIDI_FLAG_REORDER_NSM	0x00000002

#define FRIBIDI_FLAG_SHAPE_ARAB_PRES	0x00000100
#define FRIBIDI_FLAG_SHAPE_ARAB_LIGA	0x00000200
#define FRIBIDI_FLAG_SHAPE_ARAB_CONSOLE	0x00000400

#define FRIBIDI_FLAG_REMOVE_BIDI	0x00010000
#define FRIBIDI_FLAG_REMOVE_JOINING	0x00020000
#define FRIBIDI_FLAG_REMOVE_SPECIALS	0x00040000

#define FRIBIDI_FLAGS_DEFAULT		( \
#	FRIBIDI_FLAG_SHAPE_MIRRORING	| \
#	FRIBIDI_FLAG_REORDER_NSM	| \
#	FRIBIDI_FLAG_REMOVE_SPECIALS	)

#define FRIBIDI_FLAGS_ARABIC		( \
#	FRIBIDI_FLAG_SHAPE_ARAB_PRES	| \
#	FRIBIDI_FLAG_SHAPE_ARAB_LIGA	)

FRIBIDI_FLAG_SHAPE_MIRRORING = 0x00000001
FRIBIDI_FLAG_REORDER_NSM = 0x00000002
FRIBIDI_FLAG_REMOVE_SPECIALS = 0x00040000

FRIBIDI_FLAG_SHAPE_ARAB_PRES = 0x00000100
FRIBIDI_FLAG_SHAPE_ARAB_LIGA = 0x00000200

FRIBIDI_FLAGS_DEFAULT = FRIBIDI_FLAG_SHAPE_MIRRORING | \
                        FRIBIDI_FLAG_REORDER_NSM | \
                        FRIBIDI_FLAG_REMOVE_SPECIALS

FRIBIDI_FLAGS_ARABIC = FRIBIDI_FLAG_SHAPE_ARAB_PRES | \
                       FRIBIDI_FLAG_SHAPE_ARAB_LIGA


MENU_DETECT_REGEX = re.compile("%x\\d+\\|")


##### Kernel processing funcs. #####
def protect_format_seq(msg):
    """
    Find some specific escaping/formating sequences (like \", %s, etc.,
    and protect them from any modification!
    """
#    LRM = "\u200E"
#    RLM = "\u200F"
    LRE = "\u202A"
    RLE = "\u202B"
    PDF = "\u202C"
    LRO = "\u202D"
    RLO = "\u202E"
    uctrl = {LRE, RLE, PDF, LRO, RLO}
    # Most likely incomplete, but seems to cover current needs.
    format_codes = set("tslfd")
    digits = set(".0123456789")

    if not msg:
        return msg
    elif MENU_DETECT_REGEX.search(msg):
        # An ugly "menu" message, just force it whole LRE if not yet done.
        if msg[0] not in {LRE, LRO}:
            msg = LRE + msg

    idx = 0
    ret = []
    ln = len(msg)
    while idx < ln:
        dlt = 1
#        # If we find a control char, skip any additional protection!
#        if msg[idx] in uctrl:
#            ret.append(msg[idx:])
#            break
        # \" or \'
        if idx < (ln - 1) and msg[idx] == '\\' and msg[idx + 1] in "\"\'":
            dlt = 2
        # %x12|
        elif idx < (ln - 2) and msg[idx] == '%' and msg[idx + 1] in "x" and msg[idx + 2] in digits:
            dlt = 2
            while (idx + dlt) < ln and msg[idx + dlt] in digits:
                dlt += 1
            if (idx + dlt) < ln  and msg[idx + dlt] is '|':
                dlt += 1
        # %.4f
        elif idx < (ln - 3) and msg[idx] == '%' and msg[idx + 1] in digits:
            dlt = 2
            while (idx + dlt) < ln and msg[idx + dlt] in digits:
                dlt += 1
            if (idx + dlt) < ln and msg[idx + dlt] in format_codes:
                dlt += 1
            else:
                dlt = 1
        # %s
        elif idx < (ln - 1) and msg[idx] == '%' and msg[idx + 1] in format_codes:
            dlt = 2

        if dlt > 1:
            ret.append(LRE)
        ret += msg[idx:idx + dlt]
        idx += dlt
        if dlt > 1:
            ret.append(PDF)

    return "".join(ret)


def log2vis(msgs):
    """
    Globally mimics deprecated fribidi_log2vis.
    msgs should be an iterable of messages to rtl-process.
    """
    for msg in msgs:
        msg = protect_format_seq(msg)

        fbc_str = ctypes.create_unicode_buffer(msg)
        ln = len(fbc_str) - 1
#        print(fbc_str.value, ln)
        btypes = (ctypes.c_int * ln)()
        embed_lvl = (ctypes.c_uint8 * ln)()
        pbase_dir = ctypes.c_int(FRIBIDI_PAR_ON)
        jtypes = (ctypes.c_uint8 * ln)()
        flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC

        # Find out direction of each char.
        fbd.fribidi_get_bidi_types(fbc_str, ln, ctypes.byref(btypes))

#        print(*btypes)

        fbd.fribidi_get_par_embedding_levels(btypes, ln,
                                             ctypes.byref(pbase_dir),
                                             embed_lvl)

#        print(*embed_lvl)

        # Joinings for arabic chars.
        fbd.fribidi_get_joining_types(fbc_str, ln, jtypes)
#        print(*jtypes)
        fbd.fribidi_join_arabic(btypes, ln, embed_lvl, jtypes)
#        print(*jtypes)

        # Final Shaping!
        fbd.fribidi_shape(flags, embed_lvl, ln, jtypes, fbc_str)

#        print(fbc_str.value)
#        print(*(ord(c) for c in fbc_str))
        # And now, the reordering.
        # Note that here, we expect a single line, so no need to do
        # fancy things...
        fbd.fribidi_reorder_line(flags, btypes, ln, 0, pbase_dir, embed_lvl,
                                 fbc_str, None)
#        print(fbc_str.value)
#        print(*(ord(c) for c in fbc_str))

        yield fbc_str.value


##### Command line stuff. #####
def main():
    import argparse
    parser = argparse.ArgumentParser(description="" \
                    "Preprocesses right-to-left languages.\n" \
                    "You can use it either standalone, or through " \
                    "import_po_from_branches or update_trunk.\n\n" \
                    "Note: This has been tested on Linux, not 100% it will " \
                    "work nicely on Windows or OsX.\n" \
                    "Note: This uses ctypes, as there is no py3 binding for " \
                    "fribidi currently. This implies you only need the " \
                    "compiled C library to run it.\n" \
                    "Note: It handles some formating/escape codes (like " \
                    "\\\", %s, %x12, %.4f, etc.), protecting them from ugly " \
                    "(evil) fribidi, which seems completely unaware of such " \
                    "things (as unicode is...).")
    parser.add_argument('dst', metavar='dst.po',
                        help="The dest po into which write the " \
                             "pre-processed messages.")
    parser.add_argument('src', metavar='src.po',
                        help="The po's to pre-process messages.")
    args = parser.parse_args()

    msgs, state, u1 = utils.parse_messages(args.src)
    if state["is_broken"]:
        print("Source po is BROKEN, aborting.")
        return 1

    keys = []
    trans = []
    for key, val in msgs.items():
        keys.append(key)
        trans.append("".join(val["msgstr_lines"]))
    trans = log2vis(trans)
    for key, trn in zip(keys, trans):
        # Mono-line for now...
        msgs[key]["msgstr_lines"] = [trn]

    utils.write_messages(args.dst, msgs, state["comm_msg"], state["fuzzy_msg"])

    print("RTL pre-process completed.")
    return 0


if __name__ == "__main__":
    print("\n\n *** Running {} *** \n".format(__file__))
    sys.exit(main())