Commit | Line | Data |
---|---|---|
b7b65b15 EZ |
1 | /* Low-level bidirectional buffer-scanning functions for GNU Emacs. |
2 | Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 | Boston, MA 02110-1301, USA. */ | |
21 | ||
2d6e4628 EZ |
22 | /* Written by Eli Zaretskii <eliz@gnu.org>. |
23 | ||
24 | A sequential implementation of the Unicode Bidirectional algorithm, | |
b7b65b15 EZ |
25 | as per UAX#9, a part of the Unicode Standard. |
26 | ||
27 | Unlike the reference and most other implementations, this one is | |
28 | designed to be called once for every character in the buffer. | |
29 | ||
30 | The main entry point is bidi_get_next_char_visually. Each time it | |
31 | is called, it finds the next character in the visual order, and | |
32 | returns its information in a special structure. The caller is then | |
33 | expected to process this character for display or any other | |
34 | purposes, and call bidi_get_next_char_visually for the next | |
35 | character. See the comments in bidi_get_next_char_visually for | |
36 | more details about its algorithm that finds the next visual-order | |
37 | character by resolving their levels on the fly. | |
38 | ||
39 | A note about references to UAX#9 rules: if the reference says | |
40 | something like "X9/Retaining", it means that you need to refer to | |
41 | rule X9 and to its modifications decribed in the "Implementation | |
42 | Notes" section of UAX#9, under "Retaining Format Codes". */ | |
43 | ||
44 | #ifdef HAVE_CONFIG_H | |
45 | #include <config.h> | |
46 | #endif | |
47 | ||
48 | #include <stdio.h> | |
49 | ||
50 | #ifdef HAVE_STRING_H | |
51 | #include <string.h> | |
52 | #endif | |
53 | ||
54 | #include "lisp.h" | |
55 | #include "buffer.h" | |
56 | #include "character.h" | |
57 | #include "dispextern.h" | |
58 | ||
59 | static int bidi_initialized = 0; | |
60 | ||
61 | static Lisp_Object bidi_type_table; | |
62 | ||
63 | #define LRM_CHAR 0x200E | |
64 | #define RLM_CHAR 0x200F | |
65 | #define LRE_CHAR 0x202A | |
66 | #define RLE_CHAR 0x202B | |
67 | #define PDF_CHAR 0x202C | |
68 | #define LRO_CHAR 0x202D | |
69 | #define RLO_CHAR 0x202E | |
70 | ||
71 | #define CHARSET_HEBREW 0x88 | |
72 | #define CHARSET_ARABIC 0x87 | |
73 | #define CHARSET_SYRIAC -1 /* these are undefined yet, -1 is invalid */ | |
74 | #define CHARSET_THAANA -1 | |
75 | ||
76 | /* FIXME: need to define wrappers for FETCH_CHAR etc. that return | |
77 | BIDI_EOB when they hit ZV. */ | |
78 | #define BIDI_EOB -1 | |
79 | #define BIDI_BOB -2 | |
80 | ||
81 | #ifdef TEST_STANDALONE | |
82 | /* Testing. */ | |
83 | ||
84 | static unsigned char *input_buf; | |
85 | static size_t input_buf_size; | |
86 | ||
87 | int _fetch_multibyte_char_len, _c_c_; | |
88 | ||
89 | #undef FETCH_CHAR_ADVANCE | |
90 | #define FETCH_CHAR_ADVANCE(ch, cp, bp) \ | |
91 | do { \ | |
92 | ch = input_buf[cp]; \ | |
93 | (cp)++; \ | |
94 | (bp)++; \ | |
95 | if (ch == '\0') \ | |
96 | ch = BIDI_EOB; \ | |
97 | } while (0) | |
98 | ||
99 | #undef FETCH_CHAR | |
100 | #define FETCH_CHAR(n) ((_c_c_ = input_buf[n]) ? _c_c_ : BIDI_EOB) | |
101 | ||
102 | #undef CHAR_CHARSET | |
103 | #define CHAR_CHARSET(c) \ | |
104 | (((c) >= 128 || ((c) < 8 && (c)) || ((c) >= 'A' && (c) < 'X')) \ | |
105 | ? CHARSET_HEBREW \ | |
106 | : ((((c) >= 'X' && (c) <= 'Z') || ((c) >= '6' && (c) <= '9')) \ | |
107 | ? CHARSET_ARABIC \ | |
108 | : CHARSET_ASCII)) | |
109 | ||
110 | #undef CHAR_TO_BYTE | |
111 | #define CHAR_TO_BYTE(pos) (pos) | |
112 | ||
113 | #define char_bytes(ch) 1 | |
114 | ||
115 | #undef LRE_CHAR | |
116 | #undef LRO_CHAR | |
117 | #undef RLE_CHAR | |
118 | #undef RLO_CHAR | |
119 | #undef PDF_CHAR | |
120 | #undef RLM_CHAR | |
121 | #undef LRM_CHAR | |
122 | ||
123 | #define LRE_CHAR 1 | |
124 | #define LRO_CHAR 2 | |
125 | #define RLE_CHAR 3 | |
126 | #define RLO_CHAR 4 | |
127 | #define PDF_CHAR 5 | |
128 | #define RLM_CHAR 6 | |
129 | #define LRM_CHAR 7 | |
130 | ||
131 | static const char *bidi_name[] = | |
132 | { | |
133 | "[???]", "[LRE]", "[LRO]", "[RLE]", "[RLO]", "[PDF]", "[RLM]", "[LRM]" | |
134 | }; | |
135 | ||
136 | #endif /* TEST_STANDALONE */ | |
137 | ||
138 | /* Local data structures. (Look in dispextern.h for the rest.) */ | |
139 | ||
140 | /* What we need to know about the current paragraph. */ | |
141 | struct bidi_paragraph_info { | |
142 | int start_bytepos; /* byte position where it begins */ | |
143 | int end_bytepos; /* byte position where it ends */ | |
144 | int embedding_level; /* its basic embedding level */ | |
145 | bidi_dir_t base_dir; /* its base direction */ | |
146 | }; | |
147 | ||
148 | /* Data type for describing the bidirectional character categories. */ | |
149 | typedef enum { | |
150 | UNKNOWN_BC, | |
151 | NEUTRAL, | |
152 | WEAK, | |
153 | STRONG | |
154 | } bidi_category_t; | |
155 | ||
156 | int bidi_ignore_explicit_marks_for_paragraph_level = 1; | |
157 | ||
a88bbf05 EZ |
158 | /* FIXME: Should be user-definable. */ |
159 | bidi_dir_t bidi_overriding_paragraph_direction = L2R; | |
b7b65b15 | 160 | |
f44e260c | 161 | /* FIXME: Unused? */ |
b7b65b15 EZ |
162 | #define ASCII_BIDI_TYPE_SET(STR, TYPE) \ |
163 | do { \ | |
164 | unsigned char *p; \ | |
165 | for (p = (STR); *p; p++) \ | |
166 | CHAR_TABLE_SET (bidi_type_table, *p, (TYPE)); \ | |
167 | } while (0) | |
168 | ||
169 | static void | |
170 | bidi_initialize () | |
171 | { | |
172 | /* FIXME: This should come from the Unicode Database. */ | |
173 | struct { | |
174 | int from, to; | |
175 | bidi_type_t type; | |
176 | } bidi_type[] = | |
177 | { { 0x0000, 0x0008, WEAK_BN }, | |
178 | { 0x0009, 0x0000, NEUTRAL_S }, | |
179 | { 0x000A, 0x0000, NEUTRAL_B }, | |
180 | { 0x000B, 0x0000, NEUTRAL_S }, | |
181 | { 0x000C, 0x0000, NEUTRAL_WS }, | |
182 | { 0x000D, 0x0000, NEUTRAL_B }, | |
183 | { 0x000E, 0x001B, WEAK_BN }, | |
184 | { 0x001C, 0x001E, NEUTRAL_B }, | |
185 | { 0x001F, 0x0000, NEUTRAL_S }, | |
186 | { 0x0020, 0x0000, NEUTRAL_WS }, | |
187 | { 0x0021, 0x0022, NEUTRAL_ON }, | |
188 | { 0x0023, 0x0025, WEAK_ET }, | |
189 | { 0x0026, 0x002A, NEUTRAL_ON }, | |
190 | { 0x002B, 0x0000, WEAK_ET }, | |
191 | { 0x002C, 0x0000, WEAK_CS }, | |
192 | { 0x002D, 0x0000, WEAK_ET }, | |
193 | { 0x002E, 0x0000, WEAK_CS }, | |
194 | { 0x002F, 0x0000, WEAK_ES }, | |
195 | { 0x0030, 0x0039, WEAK_EN }, | |
196 | { 0x003A, 0x0000, WEAK_CS }, | |
197 | { 0x003B, 0x0040, NEUTRAL_ON }, | |
198 | { 0x005B, 0x0060, NEUTRAL_ON }, | |
199 | { 0x007B, 0x007E, NEUTRAL_ON }, | |
200 | { 0x007F, 0x0084, WEAK_BN }, | |
201 | { 0x0085, 0x0000, NEUTRAL_B }, | |
202 | { 0x0086, 0x009F, WEAK_BN }, | |
203 | { 0x00A0, 0x0000, WEAK_CS }, | |
204 | { 0x00A1, 0x0000, NEUTRAL_ON }, | |
205 | { 0x00A2, 0x00A5, WEAK_ET }, | |
206 | { 0x00A6, 0x00A9, NEUTRAL_ON }, | |
207 | { 0x00AB, 0x00AF, NEUTRAL_ON }, | |
208 | { 0x00B0, 0x00B1, WEAK_ET }, | |
209 | { 0x00B2, 0x00B3, WEAK_EN }, | |
210 | { 0x00B4, 0x0000, NEUTRAL_ON }, | |
211 | { 0x00B6, 0x00B8, NEUTRAL_ON }, | |
212 | { 0x00B9, 0x0000, WEAK_EN }, | |
213 | { 0x00BB, 0x00BF, NEUTRAL_ON }, | |
214 | { 0x00D7, 0x0000, NEUTRAL_ON }, | |
215 | { 0x00F7, 0x0000, NEUTRAL_ON }, | |
216 | { 0x02B9, 0x02BA, NEUTRAL_ON }, | |
217 | { 0x02C2, 0x02CF, NEUTRAL_ON }, | |
218 | { 0x02D2, 0x02DF, NEUTRAL_ON }, | |
219 | { 0x02E5, 0x02ED, NEUTRAL_ON }, | |
220 | { 0x0300, 0x036F, WEAK_NSM }, | |
221 | { 0x0374, 0x0375, NEUTRAL_ON }, | |
222 | { 0x037E, 0x0385, NEUTRAL_ON }, | |
223 | { 0x0387, 0x0000, NEUTRAL_ON }, | |
224 | { 0x03F6, 0x0000, NEUTRAL_ON }, | |
225 | { 0x0483, 0x0489, WEAK_NSM }, | |
226 | { 0x058A, 0x0000, NEUTRAL_ON }, | |
227 | { 0x0591, 0x05BD, WEAK_NSM }, | |
228 | { 0x05BE, 0x0000, STRONG_R }, | |
229 | { 0x05BF, 0x0000, WEAK_NSM }, | |
230 | { 0x05C0, 0x0000, STRONG_R }, | |
231 | { 0x05C1, 0x05C2, WEAK_NSM }, | |
232 | { 0x05C3, 0x0000, STRONG_R }, | |
233 | { 0x05C4, 0x0000, WEAK_NSM }, | |
234 | { 0x05D0, 0x05F4, STRONG_R }, | |
235 | { 0x060C, 0x0000, WEAK_CS }, | |
236 | { 0x061B, 0x064A, STRONG_AL }, | |
237 | { 0x064B, 0x0655, WEAK_NSM }, | |
238 | { 0x0660, 0x0669, WEAK_AN }, | |
239 | { 0x066A, 0x0000, WEAK_ET }, | |
240 | { 0x066B, 0x066C, WEAK_AN }, | |
241 | { 0x066D, 0x066F, STRONG_AL }, | |
242 | { 0x0670, 0x0000, WEAK_NSM }, | |
243 | { 0x0671, 0x06D5, STRONG_AL }, | |
244 | { 0x06D6, 0x06DC, WEAK_NSM }, | |
245 | { 0x06DD, 0x0000, STRONG_AL }, | |
246 | { 0x06DE, 0x06E4, WEAK_NSM }, | |
247 | { 0x06E5, 0x06E6, STRONG_AL }, | |
248 | { 0x06E7, 0x06E8, WEAK_NSM }, | |
249 | { 0x06E9, 0x0000, NEUTRAL_ON }, | |
250 | { 0x06EA, 0x06ED, WEAK_NSM }, | |
251 | { 0x06F0, 0x06F9, WEAK_EN }, | |
252 | { 0x06FA, 0x070D, STRONG_AL }, | |
253 | { 0x070F, 0x0000, WEAK_BN }, | |
254 | { 0x0710, 0x0000, STRONG_AL }, | |
255 | { 0x0711, 0x0000, WEAK_NSM }, | |
256 | { 0x0712, 0x072C, STRONG_AL }, | |
257 | { 0x0730, 0x074A, WEAK_NSM }, | |
258 | { 0x0780, 0x07A5, STRONG_AL }, | |
259 | { 0x07A6, 0x07B0, WEAK_NSM }, | |
260 | { 0x07B1, 0x0000, STRONG_AL }, | |
261 | { 0x0901, 0x0902, WEAK_NSM }, | |
262 | { 0x093C, 0x0000, WEAK_NSM }, | |
263 | { 0x0941, 0x0948, WEAK_NSM }, | |
264 | { 0x094D, 0x0000, WEAK_NSM }, | |
265 | { 0x0951, 0x0954, WEAK_NSM }, | |
266 | { 0x0962, 0x0963, WEAK_NSM }, | |
267 | { 0x0981, 0x0000, WEAK_NSM }, | |
268 | { 0x09BC, 0x0000, WEAK_NSM }, | |
269 | { 0x09C1, 0x09C4, WEAK_NSM }, | |
270 | { 0x09CD, 0x0000, WEAK_NSM }, | |
271 | { 0x09E2, 0x09E3, WEAK_NSM }, | |
272 | { 0x09F2, 0x09F3, WEAK_ET }, | |
273 | { 0x0A02, 0x0000, WEAK_NSM }, | |
274 | { 0x0A3C, 0x0000, WEAK_NSM }, | |
275 | { 0x0A41, 0x0A4D, WEAK_NSM }, | |
276 | { 0x0A70, 0x0A71, WEAK_NSM }, | |
277 | { 0x0A81, 0x0A82, WEAK_NSM }, | |
278 | { 0x0ABC, 0x0000, WEAK_NSM }, | |
279 | { 0x0AC1, 0x0AC8, WEAK_NSM }, | |
280 | { 0x0ACD, 0x0000, WEAK_NSM }, | |
281 | { 0x0B01, 0x0000, WEAK_NSM }, | |
282 | { 0x0B3C, 0x0000, WEAK_NSM }, | |
283 | { 0x0B3F, 0x0000, WEAK_NSM }, | |
284 | { 0x0B41, 0x0B43, WEAK_NSM }, | |
285 | { 0x0B4D, 0x0B56, WEAK_NSM }, | |
286 | { 0x0B82, 0x0000, WEAK_NSM }, | |
287 | { 0x0BC0, 0x0000, WEAK_NSM }, | |
288 | { 0x0BCD, 0x0000, WEAK_NSM }, | |
289 | { 0x0C3E, 0x0C40, WEAK_NSM }, | |
290 | { 0x0C46, 0x0C56, WEAK_NSM }, | |
291 | { 0x0CBF, 0x0000, WEAK_NSM }, | |
292 | { 0x0CC6, 0x0000, WEAK_NSM }, | |
293 | { 0x0CCC, 0x0CCD, WEAK_NSM }, | |
294 | { 0x0D41, 0x0D43, WEAK_NSM }, | |
295 | { 0x0D4D, 0x0000, WEAK_NSM }, | |
296 | { 0x0DCA, 0x0000, WEAK_NSM }, | |
297 | { 0x0DD2, 0x0DD6, WEAK_NSM }, | |
298 | { 0x0E31, 0x0000, WEAK_NSM }, | |
299 | { 0x0E34, 0x0E3A, WEAK_NSM }, | |
300 | { 0x0E3F, 0x0000, WEAK_ET }, | |
301 | { 0x0E47, 0x0E4E, WEAK_NSM }, | |
302 | { 0x0EB1, 0x0000, WEAK_NSM }, | |
303 | { 0x0EB4, 0x0EBC, WEAK_NSM }, | |
304 | { 0x0EC8, 0x0ECD, WEAK_NSM }, | |
305 | { 0x0F18, 0x0F19, WEAK_NSM }, | |
306 | { 0x0F35, 0x0000, WEAK_NSM }, | |
307 | { 0x0F37, 0x0000, WEAK_NSM }, | |
308 | { 0x0F39, 0x0000, WEAK_NSM }, | |
309 | { 0x0F3A, 0x0F3D, NEUTRAL_ON }, | |
310 | { 0x0F71, 0x0F7E, WEAK_NSM }, | |
311 | { 0x0F80, 0x0F84, WEAK_NSM }, | |
312 | { 0x0F86, 0x0F87, WEAK_NSM }, | |
313 | { 0x0F90, 0x0FBC, WEAK_NSM }, | |
314 | { 0x0FC6, 0x0000, WEAK_NSM }, | |
315 | { 0x102D, 0x1030, WEAK_NSM }, | |
316 | { 0x1032, 0x1037, WEAK_NSM }, | |
317 | { 0x1039, 0x0000, WEAK_NSM }, | |
318 | { 0x1058, 0x1059, WEAK_NSM }, | |
319 | { 0x1680, 0x0000, NEUTRAL_WS }, | |
320 | { 0x169B, 0x169C, NEUTRAL_ON }, | |
321 | { 0x1712, 0x1714, WEAK_NSM }, | |
322 | { 0x1732, 0x1734, WEAK_NSM }, | |
323 | { 0x1752, 0x1753, WEAK_NSM }, | |
324 | { 0x1772, 0x1773, WEAK_NSM }, | |
325 | { 0x17B7, 0x17BD, WEAK_NSM }, | |
326 | { 0x17C6, 0x0000, WEAK_NSM }, | |
327 | { 0x17C9, 0x17D3, WEAK_NSM }, | |
328 | { 0x17DB, 0x0000, WEAK_ET }, | |
329 | { 0x1800, 0x180A, NEUTRAL_ON }, | |
330 | { 0x180B, 0x180D, WEAK_NSM }, | |
331 | { 0x180E, 0x0000, WEAK_BN }, | |
332 | { 0x18A9, 0x0000, WEAK_NSM }, | |
333 | { 0x1FBD, 0x0000, NEUTRAL_ON }, | |
334 | { 0x1FBF, 0x1FC1, NEUTRAL_ON }, | |
335 | { 0x1FCD, 0x1FCF, NEUTRAL_ON }, | |
336 | { 0x1FDD, 0x1FDF, NEUTRAL_ON }, | |
337 | { 0x1FED, 0x1FEF, NEUTRAL_ON }, | |
338 | { 0x1FFD, 0x1FFE, NEUTRAL_ON }, | |
339 | { 0x2000, 0x200A, NEUTRAL_WS }, | |
340 | { 0x200B, 0x200D, WEAK_BN }, | |
341 | { 0x200F, 0x0000, STRONG_R }, | |
342 | { 0x2010, 0x2027, NEUTRAL_ON }, | |
343 | { 0x2028, 0x0000, NEUTRAL_WS }, | |
344 | { 0x2029, 0x0000, NEUTRAL_B }, | |
345 | { 0x202A, 0x0000, LRE }, | |
346 | { 0x202B, 0x0000, RLE }, | |
347 | { 0x202C, 0x0000, PDF }, | |
348 | { 0x202D, 0x0000, LRO }, | |
349 | { 0x202E, 0x0000, RLO }, | |
350 | { 0x202F, 0x0000, NEUTRAL_WS }, | |
351 | { 0x2030, 0x2034, WEAK_ET }, | |
352 | { 0x2035, 0x2057, NEUTRAL_ON }, | |
353 | { 0x205F, 0x0000, NEUTRAL_WS }, | |
354 | { 0x2060, 0x206F, WEAK_BN }, | |
355 | { 0x2070, 0x0000, WEAK_EN }, | |
356 | { 0x2074, 0x2079, WEAK_EN }, | |
357 | { 0x207A, 0x207B, WEAK_ET }, | |
358 | { 0x207C, 0x207E, NEUTRAL_ON }, | |
359 | { 0x2080, 0x2089, WEAK_EN }, | |
360 | { 0x208A, 0x208B, WEAK_ET }, | |
361 | { 0x208C, 0x208E, NEUTRAL_ON }, | |
362 | { 0x20A0, 0x20B1, WEAK_ET }, | |
363 | { 0x20D0, 0x20EA, WEAK_NSM }, | |
364 | { 0x2100, 0x2101, NEUTRAL_ON }, | |
365 | { 0x2103, 0x2106, NEUTRAL_ON }, | |
366 | { 0x2108, 0x2109, NEUTRAL_ON }, | |
367 | { 0x2114, 0x0000, NEUTRAL_ON }, | |
368 | { 0x2116, 0x2118, NEUTRAL_ON }, | |
369 | { 0x211E, 0x2123, NEUTRAL_ON }, | |
370 | { 0x2125, 0x0000, NEUTRAL_ON }, | |
371 | { 0x2127, 0x0000, NEUTRAL_ON }, | |
372 | { 0x2129, 0x0000, NEUTRAL_ON }, | |
373 | { 0x212E, 0x0000, WEAK_ET }, | |
374 | { 0x2132, 0x0000, NEUTRAL_ON }, | |
375 | { 0x213A, 0x0000, NEUTRAL_ON }, | |
376 | { 0x2140, 0x2144, NEUTRAL_ON }, | |
377 | { 0x214A, 0x215F, NEUTRAL_ON }, | |
378 | { 0x2190, 0x2211, NEUTRAL_ON }, | |
379 | { 0x2212, 0x2213, WEAK_ET }, | |
380 | { 0x2214, 0x2335, NEUTRAL_ON }, | |
381 | { 0x237B, 0x2394, NEUTRAL_ON }, | |
382 | { 0x2396, 0x244A, NEUTRAL_ON }, | |
383 | { 0x2460, 0x249B, WEAK_EN }, | |
384 | { 0x24EA, 0x0000, WEAK_EN }, | |
385 | { 0x24EB, 0x2FFB, NEUTRAL_ON }, | |
386 | { 0x3000, 0x0000, NEUTRAL_WS }, | |
387 | { 0x3001, 0x3004, NEUTRAL_ON }, | |
388 | { 0x3008, 0x3020, NEUTRAL_ON }, | |
389 | { 0x302A, 0x302F, WEAK_NSM }, | |
390 | { 0x3030, 0x0000, NEUTRAL_ON }, | |
391 | { 0x3036, 0x3037, NEUTRAL_ON }, | |
392 | { 0x303D, 0x303F, NEUTRAL_ON }, | |
393 | { 0x3099, 0x309A, WEAK_NSM }, | |
394 | { 0x309B, 0x309C, NEUTRAL_ON }, | |
395 | { 0x30A0, 0x0000, NEUTRAL_ON }, | |
396 | { 0x30FB, 0x0000, NEUTRAL_ON }, | |
397 | { 0x3251, 0x325F, NEUTRAL_ON }, | |
398 | { 0x32B1, 0x32BF, NEUTRAL_ON }, | |
399 | { 0xA490, 0xA4C6, NEUTRAL_ON }, | |
400 | { 0xFB1D, 0x0000, STRONG_R }, | |
401 | { 0xFB1E, 0x0000, WEAK_NSM }, | |
402 | { 0xFB1F, 0xFB28, STRONG_R }, | |
403 | { 0xFB29, 0x0000, WEAK_ET }, | |
404 | { 0xFB2A, 0xFB4F, STRONG_R }, | |
405 | { 0xFB50, 0xFD3D, STRONG_AL }, | |
406 | { 0xFD3E, 0xFD3F, NEUTRAL_ON }, | |
407 | { 0xFD50, 0xFDFC, STRONG_AL }, | |
408 | { 0xFE00, 0xFE23, WEAK_NSM }, | |
409 | { 0xFE30, 0xFE4F, NEUTRAL_ON }, | |
410 | { 0xFE50, 0x0000, WEAK_CS }, | |
411 | { 0xFE51, 0x0000, NEUTRAL_ON }, | |
412 | { 0xFE52, 0x0000, WEAK_CS }, | |
413 | { 0xFE54, 0x0000, NEUTRAL_ON }, | |
414 | { 0xFE55, 0x0000, WEAK_CS }, | |
415 | { 0xFE56, 0xFE5E, NEUTRAL_ON }, | |
416 | { 0xFE5F, 0x0000, WEAK_ET }, | |
417 | { 0xFE60, 0xFE61, NEUTRAL_ON }, | |
418 | { 0xFE62, 0xFE63, WEAK_ET }, | |
419 | { 0xFE64, 0xFE68, NEUTRAL_ON }, | |
420 | { 0xFE69, 0xFE6A, WEAK_ET }, | |
421 | { 0xFE6B, 0x0000, NEUTRAL_ON }, | |
422 | { 0xFE70, 0xFEFC, STRONG_AL }, | |
423 | { 0xFEFF, 0x0000, WEAK_BN }, | |
424 | { 0xFF01, 0xFF02, NEUTRAL_ON }, | |
425 | { 0xFF03, 0xFF05, WEAK_ET }, | |
426 | { 0xFF06, 0xFF0A, NEUTRAL_ON }, | |
427 | { 0xFF0B, 0x0000, WEAK_ET }, | |
428 | { 0xFF0C, 0x0000, WEAK_CS }, | |
429 | { 0xFF0D, 0x0000, WEAK_ET }, | |
430 | { 0xFF0E, 0x0000, WEAK_CS }, | |
431 | { 0xFF0F, 0x0000, WEAK_ES }, | |
432 | { 0xFF10, 0xFF19, WEAK_EN }, | |
433 | { 0xFF1A, 0x0000, WEAK_CS }, | |
434 | { 0xFF1B, 0xFF20, NEUTRAL_ON }, | |
435 | { 0xFF3B, 0xFF40, NEUTRAL_ON }, | |
436 | { 0xFF5B, 0xFF65, NEUTRAL_ON }, | |
437 | { 0xFFE0, 0xFFE1, WEAK_ET }, | |
438 | { 0xFFE2, 0xFFE4, NEUTRAL_ON }, | |
439 | { 0xFFE5, 0xFFE6, WEAK_ET }, | |
440 | { 0xFFE8, 0xFFEE, NEUTRAL_ON }, | |
441 | { 0xFFF9, 0xFFFB, WEAK_BN }, | |
442 | { 0xFFFC, 0xFFFD, NEUTRAL_ON }, | |
443 | { 0x1D167, 0x1D169, WEAK_NSM }, | |
444 | { 0x1D173, 0x1D17A, WEAK_BN }, | |
445 | { 0x1D17B, 0x1D182, WEAK_NSM }, | |
446 | { 0x1D185, 0x1D18B, WEAK_NSM }, | |
447 | { 0x1D1AA, 0x1D1AD, WEAK_NSM }, | |
448 | { 0x1D7CE, 0x1D7FF, WEAK_EN }, | |
449 | { 0xE0001, 0xE007F, WEAK_BN } }; | |
450 | int i; | |
451 | ||
452 | bidi_type_table = Fmake_char_table (Qnil, make_number (STRONG_L)); | |
2d6e4628 | 453 | staticpro (&bidi_type_table); |
b7b65b15 EZ |
454 | |
455 | for (i = 0; i < sizeof bidi_type / sizeof bidi_type[0]; i++) | |
f44e260c EZ |
456 | char_table_set_range (bidi_type_table, bidi_type[i].from, |
457 | bidi_type[i].to ? bidi_type[i].to : bidi_type[i].from, | |
b7b65b15 EZ |
458 | make_number (bidi_type[i].type)); |
459 | bidi_initialized = 1; | |
460 | } | |
461 | ||
462 | static int | |
463 | bidi_is_arabic_number (int ch) | |
464 | { | |
465 | #ifdef TEST_STANDALONE | |
466 | return ch >= '6' && ch <= '9'; | |
467 | #else | |
468 | return 0; /* FIXME! */ | |
469 | #endif | |
470 | } | |
471 | ||
472 | /* Return the bidi type of a character CH. */ | |
473 | bidi_type_t | |
474 | bidi_get_type (int ch) | |
475 | { | |
476 | return (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch)); | |
477 | } | |
478 | ||
2d6e4628 EZ |
479 | void |
480 | bidi_check_type (bidi_type_t type) | |
481 | { | |
482 | if (type < UNKNOWN_BT || type > NEUTRAL_ON) | |
483 | abort (); | |
484 | } | |
485 | ||
b7b65b15 EZ |
486 | /* Given a bidi TYPE of a character, return its category. */ |
487 | bidi_category_t | |
488 | bidi_get_category (bidi_type_t type) | |
489 | { | |
490 | switch (type) | |
491 | { | |
492 | case UNKNOWN_BT: | |
493 | return UNKNOWN_BC; | |
494 | case STRONG_L: | |
495 | case STRONG_R: | |
496 | case STRONG_AL: | |
497 | case LRE: | |
498 | case LRO: | |
499 | case RLE: | |
500 | case RLO: | |
501 | return STRONG; | |
502 | case PDF: /* ??? really?? */ | |
503 | case WEAK_EN: | |
504 | case WEAK_ES: | |
505 | case WEAK_ET: | |
506 | case WEAK_AN: | |
507 | case WEAK_CS: | |
508 | case WEAK_NSM: | |
509 | case WEAK_BN: | |
510 | return WEAK; | |
511 | case NEUTRAL_B: | |
512 | case NEUTRAL_S: | |
513 | case NEUTRAL_WS: | |
514 | case NEUTRAL_ON: | |
515 | return NEUTRAL; | |
516 | default: | |
517 | abort (); | |
518 | } | |
519 | } | |
520 | ||
521 | /* FIXME: exceedingly temporary! Should consult the Unicode database | |
522 | of character properties. */ | |
523 | int | |
524 | bidi_mirror_char (int c) | |
525 | { | |
526 | static const char mirrored_pairs[] = "()<>[]{}"; | |
527 | const char *p = strchr (mirrored_pairs, c); | |
528 | ||
529 | if (p) | |
530 | { | |
531 | size_t i = p - mirrored_pairs; | |
532 | ||
533 | if ((i & 1) == 0) | |
534 | return mirrored_pairs[i + 1]; | |
535 | else | |
536 | return mirrored_pairs[i - 1]; | |
537 | } | |
538 | return c; | |
539 | } | |
540 | ||
541 | /* Copy the bidi iterator from FROM to TO. To save cycles, this only | |
542 | copies the part of the level stack that is actually in use. */ | |
543 | static inline void | |
544 | bidi_copy_it (struct bidi_it *to, struct bidi_it *from) | |
545 | { | |
546 | int i; | |
547 | ||
548 | /* Copy everything except the level stack. */ | |
549 | memcpy (to, from, ((int)&((struct bidi_it *)0)->level_stack[0])); | |
550 | ||
551 | /* Copy the active part of the level stack. */ | |
552 | to->level_stack[0] = from->level_stack[0]; /* level zero is always in use */ | |
553 | for (i = 1; i <= from->stack_idx; i++) | |
554 | to->level_stack[i] = from->level_stack[i]; | |
555 | } | |
556 | ||
557 | /* Caching the bidi iterator states. */ | |
558 | ||
559 | static struct bidi_it bidi_cache[1000]; /* FIXME: make this dynamically allocated! */ | |
560 | static int bidi_cache_idx; | |
561 | static int bidi_cache_last_idx; | |
562 | ||
563 | static inline void | |
564 | bidi_cache_reset (void) | |
565 | { | |
566 | bidi_cache_idx = 0; | |
567 | bidi_cache_last_idx = -1; | |
568 | } | |
569 | ||
570 | static inline void | |
571 | bidi_cache_fetch_state (int idx, struct bidi_it *bidi_it) | |
572 | { | |
573 | int current_scan_dir = bidi_it->scan_dir; | |
574 | ||
575 | if (idx < 0 || idx >= bidi_cache_idx) | |
576 | abort (); | |
577 | ||
578 | bidi_copy_it (bidi_it, &bidi_cache[idx]); | |
579 | bidi_it->scan_dir = current_scan_dir; | |
580 | bidi_cache_last_idx = idx; | |
581 | } | |
582 | ||
583 | /* Find a cached state with a given CHARPOS and resolved embedding | |
584 | level less or equal to LEVEL. if LEVEL is -1, disregard the | |
585 | resolved levels in cached states. DIR, if non-zero, means search | |
586 | in that direction from the last cache hit. */ | |
587 | static inline int | |
588 | bidi_cache_search (int charpos, int level, int dir) | |
589 | { | |
590 | int i, i_start; | |
591 | ||
592 | if (bidi_cache_idx) | |
593 | { | |
594 | if (charpos < bidi_cache[bidi_cache_last_idx].charpos) | |
595 | dir = -1; | |
596 | else if (charpos > bidi_cache[bidi_cache_last_idx].charpos) | |
597 | dir = 1; | |
598 | if (dir) | |
599 | i_start = bidi_cache_last_idx; | |
600 | else | |
601 | { | |
602 | dir = -1; | |
603 | i_start = bidi_cache_idx - 1; | |
604 | } | |
605 | ||
606 | if (dir < 0) | |
607 | { | |
608 | /* Linear search for now; FIXME! */ | |
609 | for (i = i_start; i >= 0; i--) | |
610 | if (bidi_cache[i].charpos == charpos | |
611 | && (level == -1 || bidi_cache[i].resolved_level <= level)) | |
612 | return i; | |
613 | } | |
614 | else | |
615 | { | |
616 | for (i = i_start; i < bidi_cache_idx; i++) | |
617 | if (bidi_cache[i].charpos == charpos | |
618 | && (level == -1 || bidi_cache[i].resolved_level <= level)) | |
619 | return i; | |
620 | } | |
621 | } | |
622 | ||
623 | return -1; | |
624 | } | |
625 | ||
626 | /* Find a cached state where the resolved level changes to a value | |
627 | that is lower than LEVEL, and return its cache slot index. DIR is | |
628 | the direction to search, starting with the last used cache slot. | |
629 | BEFORE, if non-zero, means return the index of the slot that is | |
630 | ``before'' the level change in the search direction. That is, | |
631 | given the cached levels like this: | |
632 | ||
633 | 1122333442211 | |
634 | AB C | |
635 | ||
636 | and assuming we are at the position cached at the slot marked with | |
637 | C, searching backwards (DIR = -1) for LEVEL = 2 will return the | |
638 | index of slot B or A, depending whether BEFORE is, respectively, | |
639 | non-zero or zero. */ | |
640 | static int | |
641 | bidi_cache_find_level_change (int level, int dir, int before) | |
642 | { | |
643 | if (bidi_cache_idx) | |
644 | { | |
645 | int i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1; | |
646 | int incr = before ? 1 : 0; | |
647 | ||
648 | if (!dir) | |
649 | dir = -1; | |
650 | else if (!incr) | |
651 | i += dir; | |
652 | ||
653 | if (dir < 0) | |
654 | { | |
655 | while (i >= incr) | |
656 | { | |
657 | if (bidi_cache[i - incr].resolved_level >= 0 | |
658 | && bidi_cache[i - incr].resolved_level < level) | |
659 | return i; | |
660 | i--; | |
661 | } | |
662 | } | |
663 | else | |
664 | { | |
665 | while (i < bidi_cache_idx - incr) | |
666 | { | |
667 | if (bidi_cache[i + incr].resolved_level >= 0 | |
668 | && bidi_cache[i + incr].resolved_level < level) | |
669 | return i; | |
670 | i++; | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | return -1; | |
676 | } | |
677 | ||
678 | static inline void | |
679 | bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved) | |
680 | { | |
681 | int idx; | |
682 | ||
683 | /* We should never cache on backward scans. */ | |
684 | if (bidi_it->scan_dir == -1) | |
685 | abort (); | |
686 | idx = bidi_cache_search (bidi_it->charpos, -1, 1); | |
687 | ||
688 | if (idx < 0) | |
689 | { | |
690 | idx = bidi_cache_idx; | |
691 | if (idx > sizeof (bidi_cache) / sizeof (bidi_cache[0]) - 1) | |
692 | abort (); | |
693 | bidi_copy_it (&bidi_cache[idx], bidi_it); | |
694 | if (!resolved) | |
695 | bidi_cache[idx].resolved_level = -1; | |
696 | } | |
697 | else | |
698 | { | |
699 | /* Copy only the members which could have changed, to avoid | |
700 | costly copying of the entire struct. */ | |
701 | bidi_cache[idx].type = bidi_it->type; | |
2d6e4628 | 702 | bidi_check_type (bidi_it->type); |
b7b65b15 | 703 | bidi_cache[idx].orig_type = bidi_it->orig_type; |
2d6e4628 | 704 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
705 | if (resolved) |
706 | bidi_cache[idx].resolved_level = bidi_it->resolved_level; | |
707 | else | |
708 | bidi_cache[idx].resolved_level = -1; | |
709 | bidi_cache[idx].invalid_levels = bidi_it->invalid_levels; | |
710 | bidi_cache[idx].invalid_rl_levels = bidi_it->invalid_rl_levels; | |
711 | bidi_cache[idx].next_for_neutral = bidi_it->next_for_neutral; | |
712 | bidi_cache[idx].next_for_ws = bidi_it->next_for_ws; | |
713 | bidi_cache[idx].ignore_bn_limit = bidi_it->ignore_bn_limit; | |
714 | } | |
715 | ||
716 | bidi_cache_last_idx = idx; | |
717 | if (idx >= bidi_cache_idx) | |
718 | bidi_cache_idx = idx + 1; | |
719 | } | |
720 | ||
721 | static inline bidi_type_t | |
722 | bidi_cache_find (int charpos, int level, struct bidi_it *bidi_it) | |
723 | { | |
724 | int i = bidi_cache_search (charpos, level, bidi_it->scan_dir); | |
725 | ||
726 | if (i >= 0) | |
727 | { | |
728 | bidi_dir_t current_scan_dir = bidi_it->scan_dir; | |
729 | ||
730 | *bidi_it = bidi_cache[i]; | |
731 | bidi_cache_last_idx = i; | |
732 | /* Don't let scan direction from from the cached state override | |
733 | the current scan direction. */ | |
734 | bidi_it->scan_dir = current_scan_dir; | |
735 | return bidi_it->type; | |
736 | } | |
737 | ||
738 | return UNKNOWN_BT; | |
739 | } | |
740 | ||
741 | static inline int | |
742 | bidi_peek_at_next_level (struct bidi_it *bidi_it) | |
743 | { | |
744 | if (bidi_cache_idx == 0 || bidi_cache_last_idx == -1) | |
745 | abort (); | |
746 | return bidi_cache[bidi_cache_last_idx + bidi_it->scan_dir].resolved_level; | |
747 | } | |
748 | ||
749 | /* Return non-zero if buffer's byte position POS is the last character | |
750 | of a paragraph. THIS_CH is the character preceding the one at POS in | |
751 | the buffer. */ | |
752 | int | |
753 | bidi_at_paragraph_end (int this_ch, int pos) | |
754 | { | |
755 | int next_ch = FETCH_CHAR (pos); | |
756 | ||
757 | /* FIXME: This should support all Unicode characters that can end a | |
758 | paragraph. */ | |
759 | return (this_ch == '\n' && next_ch == '\n') || this_ch == BIDI_EOB; | |
760 | } | |
761 | ||
762 | /* Determine the start-of-run (sor) directional type given the two | |
763 | embedding levels on either side of the run boundary. Also, update | |
764 | the saved info about previously seen characters, since that info is | |
765 | generally valid for a single level run. */ | |
766 | static inline void | |
767 | bidi_set_sor_type (struct bidi_it *bidi_it, int level_before, int level_after) | |
768 | { | |
769 | int higher_level = level_before > level_after ? level_before : level_after; | |
770 | ||
771 | /* The prev_was_pdf gork is required for when we have several PDFs | |
772 | in a row. In that case, we want to compute the sor type for the | |
773 | next level run only once: when we see the first PDF. That's | |
774 | because the sor type depends only on the higher of the two levels | |
775 | that we find on the two sides of the level boundary (see UAX#9, | |
776 | clause X10), and so we don't need to know the final embedding | |
777 | level to which we descend after processing all the PDFs. */ | |
778 | if (level_before < level_after || !bidi_it->prev_was_pdf) | |
779 | /* FIXME: should the default sor direction be user selectable? */ | |
780 | bidi_it->sor = (higher_level & 1) != 0 ? R2L : L2R; | |
781 | if (level_before > level_after) | |
782 | bidi_it->prev_was_pdf = 1; | |
783 | ||
784 | bidi_it->prev.type = UNKNOWN_BT; | |
785 | bidi_it->last_strong.type = bidi_it->last_strong.orig_type = | |
786 | bidi_it->last_strong.pristine_type = UNKNOWN_BT; | |
787 | bidi_it->prev_for_neutral.type = bidi_it->sor == R2L ? STRONG_R : STRONG_L; | |
788 | bidi_it->prev_for_neutral.charpos = bidi_it->charpos; | |
789 | bidi_it->prev_for_neutral.bytepos = bidi_it->bytepos; | |
790 | bidi_it->next_for_neutral.type = bidi_it->next_for_neutral.orig_type = | |
791 | bidi_it->next_for_neutral.pristine_type = UNKNOWN_BT; | |
792 | bidi_it->ignore_bn_limit = 0; /* meaning it's unknown */ | |
793 | } | |
794 | ||
795 | void | |
796 | bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it) | |
797 | { | |
798 | bidi_it->level_stack[0].level = 0; | |
799 | if (dir == R2L) | |
800 | bidi_it->level_stack[0].level = 1; | |
801 | else if (dir == NEUTRAL_DIR) /* P2 */ | |
802 | { | |
803 | bidi_type_t type; | |
804 | int pos = bidi_it->charpos, bytepos = bidi_it->bytepos; | |
805 | int ch; | |
806 | ||
807 | if (pos < 0) | |
808 | pos = bytepos = 0; | |
809 | else if (bidi_it->ch != BIDI_EOB) | |
810 | { | |
811 | pos++; | |
812 | bytepos += bidi_it->ch_len; | |
813 | } | |
814 | ||
815 | ch = FETCH_CHAR (bytepos); | |
816 | pos++; | |
817 | bytepos += CHAR_BYTES (ch); | |
818 | ||
819 | /* FIXME: should actually go to where the paragraph begins and | |
820 | start the loop below from there, since UAX#9 says to find the | |
821 | first strong directional character in the paragraph. */ | |
822 | ||
823 | for (type = bidi_get_type (ch); | |
824 | /* NOTE: UAX#9 says to search only for L, AL, or R types of | |
825 | characters, and ignore RLE, RLO, LRE, and LRO. However, | |
826 | I'm not sure it makes sense to omit those 4; should try | |
827 | with and without that to see the effect. */ | |
828 | (bidi_get_category (type) != STRONG) | |
829 | || (bidi_ignore_explicit_marks_for_paragraph_level | |
830 | && (type == RLE || type == RLO | |
831 | || type == LRE || type == LRO)); | |
832 | type = bidi_get_type (ch)) | |
833 | { | |
834 | if (type == NEUTRAL_B || bidi_at_paragraph_end (ch, bytepos)) | |
835 | break; | |
836 | FETCH_CHAR_ADVANCE (ch, pos, bytepos); | |
837 | } | |
838 | if (type == STRONG_R || type == STRONG_AL) /* P3 */ | |
839 | bidi_it->level_stack[0].level = 1; | |
840 | } | |
841 | bidi_it->scan_dir = 1; /* FIXME: do we need to have control on this? */ | |
842 | bidi_it->resolved_level = bidi_it->level_stack[0].level; | |
843 | bidi_it->level_stack[0].override = NEUTRAL_DIR; /* X1 */ | |
844 | bidi_it->invalid_levels = 0; | |
845 | bidi_it->invalid_rl_levels = -1; | |
846 | bidi_it->new_paragraph = 0; | |
847 | bidi_it->next_en_pos = -1; | |
848 | bidi_it->next_for_ws.type = UNKNOWN_BT; | |
849 | bidi_set_sor_type (bidi_it, bidi_it->level_stack[0].level, 0); /* X10 */ | |
850 | ||
851 | bidi_cache_reset (); | |
852 | } | |
853 | ||
854 | /* Do whatever UAX#9 clause X8 says should be done at paragraph's end, | |
855 | and set the new paragraph flag in the iterator. */ | |
856 | static inline void | |
857 | bidi_set_paragraph_end (struct bidi_it *bidi_it) | |
858 | { | |
859 | bidi_it->invalid_levels = 0; | |
860 | bidi_it->invalid_rl_levels = -1; | |
861 | bidi_it->stack_idx = 0; | |
862 | bidi_it->resolved_level = bidi_it->level_stack[0].level; | |
863 | bidi_it->new_paragraph = 1; | |
864 | } | |
865 | ||
866 | /* Initialize the bidi iterator from buffer position POS for paragraph | |
867 | direction DIR. Return the embedding level at POS. */ | |
868 | void | |
869 | bidi_init_it (int pos, bidi_dir_t dir, struct bidi_it *bidi_it) | |
870 | { | |
871 | if (! bidi_initialized) | |
872 | bidi_initialize (); | |
873 | bidi_set_paragraph_end (bidi_it); | |
874 | bidi_it->charpos = pos; | |
875 | if (pos <= 0) | |
876 | { | |
877 | bidi_it->bytepos = bidi_it->charpos; | |
878 | bidi_it->ch_len = 1; /* so that incrementing bytepos works */ | |
879 | } | |
880 | else | |
881 | { | |
882 | bidi_it->bytepos = CHAR_TO_BYTE (pos); | |
883 | bidi_it->ch_len | |
884 | = MULTIBYTE_FORM_LENGTH (BYTE_POS_ADDR (bidi_it->bytepos), | |
885 | MAX_MULTIBYTE_LENGTH); | |
886 | } | |
887 | bidi_it->ch = '\x1d'; /* FIXME: should be U+2029 */ | |
888 | bidi_it->type = NEUTRAL_B; | |
889 | bidi_it->orig_type = UNKNOWN_BT; | |
890 | bidi_it->pristine_type = UNKNOWN_BT; | |
891 | bidi_it->prev_was_pdf = 0; | |
892 | bidi_it->prev.type = bidi_it->prev.orig_type = UNKNOWN_BT; | |
893 | bidi_it->last_strong.type = bidi_it->last_strong.orig_type = | |
894 | bidi_it->last_strong.pristine_type = UNKNOWN_BT; | |
895 | bidi_it->next_for_neutral.charpos = -1; | |
896 | bidi_it->next_for_neutral.type = | |
897 | bidi_it->next_for_neutral.orig_type = | |
898 | bidi_it->next_for_neutral.pristine_type = UNKNOWN_BT; | |
899 | bidi_it->prev_for_neutral.charpos = -1; | |
900 | bidi_it->prev_for_neutral.type = | |
901 | bidi_it->prev_for_neutral.orig_type = | |
902 | bidi_it->prev_for_neutral.pristine_type = UNKNOWN_BT; | |
903 | bidi_it->sor = L2R; /* FIXME: should it be user-selectable? */ | |
904 | bidi_paragraph_init (dir, bidi_it); | |
905 | } | |
906 | ||
907 | /* Push the current embedding level and override status; reset the | |
908 | current level to LEVEL and the current override status to OVERRIDE. */ | |
909 | static inline void | |
910 | bidi_push_embedding_level (struct bidi_it *bidi_it, | |
911 | int level, bidi_dir_t override) | |
912 | { | |
913 | bidi_it->stack_idx++; | |
914 | if (bidi_it->stack_idx >= BIDI_MAXLEVEL) | |
915 | abort (); | |
916 | bidi_it->level_stack[bidi_it->stack_idx].level = level; | |
917 | bidi_it->level_stack[bidi_it->stack_idx].override = override; | |
918 | } | |
919 | ||
920 | /* Pop the embedding level and directional override status from the | |
921 | stack, and return the new level. */ | |
922 | static inline int | |
923 | bidi_pop_embedding_level (struct bidi_it *bidi_it) | |
924 | { | |
925 | /* UAX#9 says to ignore invalid PDFs. */ | |
926 | if (bidi_it->stack_idx > 0) | |
927 | bidi_it->stack_idx--; | |
928 | return bidi_it->level_stack[bidi_it->stack_idx].level; | |
929 | } | |
930 | ||
931 | /* Record in SAVED_INFO the information about the current character. */ | |
932 | static inline void | |
933 | bidi_remember_char (struct bidi_saved_info *saved_info, | |
934 | struct bidi_it *bidi_it) | |
935 | { | |
936 | saved_info->charpos = bidi_it->charpos; | |
937 | saved_info->bytepos = bidi_it->bytepos; | |
938 | saved_info->type = bidi_it->type; | |
2d6e4628 | 939 | bidi_check_type (bidi_it->type); |
b7b65b15 | 940 | saved_info->orig_type = bidi_it->orig_type; |
2d6e4628 | 941 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 | 942 | saved_info->pristine_type = bidi_it->pristine_type; |
2d6e4628 | 943 | bidi_check_type (bidi_it->pristine_type); |
b7b65b15 EZ |
944 | } |
945 | ||
946 | /* Resolve the type of a neutral character according to the type of | |
947 | surrounding strong text and the current embedding level. */ | |
948 | static inline bidi_type_t | |
949 | bidi_resolve_neutral_1 (bidi_type_t prev_type, bidi_type_t next_type, int lev) | |
950 | { | |
951 | /* N1: European and Arabic numbers are treated as though they were R. */ | |
952 | if (next_type == WEAK_EN || next_type == WEAK_AN) | |
953 | next_type = STRONG_R; | |
954 | if (prev_type == WEAK_EN || prev_type == WEAK_AN) | |
955 | prev_type = STRONG_R; | |
956 | ||
957 | if (next_type == prev_type) /* N1 */ | |
958 | return next_type; | |
959 | else if ((lev & 1) == 0) /* N2 */ | |
960 | return STRONG_L; | |
961 | else | |
962 | return STRONG_R; | |
963 | } | |
964 | ||
965 | static inline int | |
966 | bidi_explicit_dir_char (int c) | |
967 | { | |
968 | /* FIXME: this should be replaced with a lookup table with suitable | |
969 | bits set, like standard C ctype macros do. */ | |
970 | return (c == LRE_CHAR || c == LRO_CHAR | |
971 | || c == RLE_CHAR || c == RLO_CHAR || c == PDF_CHAR); | |
972 | } | |
973 | ||
974 | /* A helper function for bidi_resolve_explicit. It advances to the | |
975 | next character in logical order and determines the new embedding | |
976 | level and directional override, but does not take into account | |
977 | empty embeddings. */ | |
978 | static int | |
979 | bidi_resolve_explicit_1 (struct bidi_it *bidi_it) | |
980 | { | |
981 | int curchar; | |
982 | bidi_type_t type; | |
983 | int current_level; | |
984 | int new_level; | |
985 | bidi_dir_t override; | |
986 | ||
987 | if (bidi_it->charpos < 0) | |
988 | bidi_it->charpos = bidi_it->bytepos = 0; | |
989 | else | |
990 | { | |
991 | bidi_it->charpos++; | |
992 | bidi_it->bytepos += bidi_it->ch_len; | |
993 | } | |
994 | ||
995 | current_level = bidi_it->level_stack[bidi_it->stack_idx].level; /* X1 */ | |
996 | override = bidi_it->level_stack[bidi_it->stack_idx].override; | |
997 | new_level = current_level; | |
998 | ||
999 | /* in case it is a unibyte character (not yet implemented) */ | |
1000 | /* _fetch_multibyte_char_len = 1; */ | |
1001 | curchar = FETCH_CHAR (bidi_it->bytepos); | |
1002 | bidi_it->ch = curchar; | |
1003 | bidi_it->ch_len = CHAR_BYTES (curchar); | |
1004 | ||
1005 | type = bidi_get_type (curchar); | |
1006 | bidi_it->pristine_type = type; | |
2d6e4628 | 1007 | bidi_check_type (bidi_it->pristine_type); |
b7b65b15 EZ |
1008 | |
1009 | if (type != PDF) | |
1010 | bidi_it->prev_was_pdf = 0; | |
1011 | ||
1012 | bidi_it->orig_type = UNKNOWN_BT; | |
1013 | ||
1014 | switch (type) | |
1015 | { | |
1016 | case RLE: /* X2 */ | |
1017 | case RLO: /* X4 */ | |
1018 | bidi_it->orig_type = type; | |
2d6e4628 | 1019 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1020 | type = WEAK_BN; /* X9/Retaining */ |
1021 | if (bidi_it->ignore_bn_limit <= 0) | |
1022 | { | |
1023 | if (current_level <= BIDI_MAXLEVEL - 4) | |
1024 | { | |
1025 | /* Compute the least odd embedding level greater than | |
1026 | the current level. */ | |
1027 | new_level = ((current_level + 1) & ~1) + 1; | |
1028 | if (bidi_it->orig_type == RLE) | |
1029 | override = NEUTRAL_DIR; | |
1030 | else | |
1031 | override = R2L; | |
1032 | if (current_level == BIDI_MAXLEVEL - 4) | |
1033 | bidi_it->invalid_rl_levels = 0; | |
1034 | bidi_push_embedding_level (bidi_it, new_level, override); | |
1035 | } | |
1036 | else | |
1037 | { | |
1038 | bidi_it->invalid_levels++; | |
1039 | /* See the commentary about invalid_rl_levels below. */ | |
1040 | if (bidi_it->invalid_rl_levels < 0) | |
1041 | bidi_it->invalid_rl_levels = 0; | |
1042 | bidi_it->invalid_rl_levels++; | |
1043 | } | |
1044 | } | |
1045 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | |
1046 | || bidi_it->next_en_pos > bidi_it->charpos) | |
1047 | type = WEAK_EN; | |
1048 | break; | |
1049 | case LRE: /* X3 */ | |
1050 | case LRO: /* X5 */ | |
1051 | bidi_it->orig_type = type; | |
2d6e4628 | 1052 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1053 | type = WEAK_BN; /* X9/Retaining */ |
1054 | if (bidi_it->ignore_bn_limit <= 0) | |
1055 | { | |
1056 | if (current_level <= BIDI_MAXLEVEL - 5) | |
1057 | { | |
1058 | /* Compute the least even embedding level greater than | |
1059 | the current level. */ | |
1060 | new_level = ((current_level + 2) & ~1); | |
1061 | if (bidi_it->orig_type == LRE) | |
1062 | override = NEUTRAL_DIR; | |
1063 | else | |
1064 | override = L2R; | |
1065 | bidi_push_embedding_level (bidi_it, new_level, override); | |
1066 | } | |
1067 | else | |
1068 | { | |
1069 | bidi_it->invalid_levels++; | |
1070 | /* invalid_rl_levels counts invalid levels encountered | |
1071 | while the embedding level was already too high for | |
1072 | LRE/LRO, but not for RLE/RLO. That is because | |
1073 | there may be exactly one PDF which we should not | |
1074 | ignore even though invalid_levels is non-zero. | |
1075 | invalid_rl_levels helps to know what PDF is | |
1076 | that. */ | |
1077 | if (bidi_it->invalid_rl_levels >= 0) | |
1078 | bidi_it->invalid_rl_levels++; | |
1079 | } | |
1080 | } | |
1081 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | |
1082 | || bidi_it->next_en_pos > bidi_it->charpos) | |
1083 | type = WEAK_EN; | |
1084 | break; | |
1085 | case PDF: /* X7 */ | |
1086 | bidi_it->orig_type = type; | |
2d6e4628 | 1087 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1088 | type = WEAK_BN; /* X9/Retaining */ |
1089 | if (bidi_it->ignore_bn_limit <= 0) | |
1090 | { | |
1091 | if (!bidi_it->invalid_rl_levels) | |
1092 | { | |
1093 | new_level = bidi_pop_embedding_level (bidi_it); | |
1094 | bidi_it->invalid_rl_levels = -1; | |
1095 | if (bidi_it->invalid_levels) | |
1096 | bidi_it->invalid_levels--; | |
1097 | /* else nothing: UAX#9 says to ignore invalid PDFs */ | |
1098 | } | |
1099 | if (!bidi_it->invalid_levels) | |
1100 | new_level = bidi_pop_embedding_level (bidi_it); | |
1101 | else | |
1102 | { | |
1103 | bidi_it->invalid_levels--; | |
1104 | bidi_it->invalid_rl_levels--; | |
1105 | } | |
1106 | } | |
1107 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | |
1108 | || bidi_it->next_en_pos > bidi_it->charpos) | |
1109 | type = WEAK_EN; | |
1110 | break; | |
1111 | default: | |
1112 | /* Nothing. */ | |
1113 | break; | |
1114 | } | |
1115 | ||
1116 | bidi_it->type = type; | |
2d6e4628 | 1117 | bidi_check_type (bidi_it->type); |
b7b65b15 EZ |
1118 | |
1119 | return new_level; | |
1120 | } | |
1121 | ||
1122 | /* Given an iterator state in BIDI_IT, advance one character position | |
1123 | in the buffer to the next character (in the logical order), resolve | |
1124 | any explicit embeddings and directional overrides, and return the | |
1125 | embedding level of the character after resolving explicit | |
1126 | directives and ignoring empty embeddings. */ | |
1127 | static int | |
1128 | bidi_resolve_explicit (struct bidi_it *bidi_it) | |
1129 | { | |
1130 | int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1131 | int new_level = bidi_resolve_explicit_1 (bidi_it); | |
1132 | ||
1133 | if (prev_level < new_level | |
1134 | && bidi_it->type == WEAK_BN | |
1135 | && bidi_it->ignore_bn_limit == 0 /* only if not already known */ | |
1136 | && bidi_explicit_dir_char (FETCH_CHAR (bidi_it->bytepos | |
1137 | + bidi_it->ch_len))) | |
1138 | { | |
1139 | /* Avoid pushing and popping embedding levels if the level run | |
1140 | is empty, as this breaks level runs where it shouldn't. | |
1141 | UAX#9 removes all the explicit embedding and override codes, | |
1142 | so empty embeddings disappear without a trace. We need to | |
1143 | behave as if we did the same. */ | |
1144 | struct bidi_it saved_it; | |
1145 | int level = prev_level; | |
1146 | ||
1147 | bidi_copy_it (&saved_it, bidi_it); | |
1148 | ||
1149 | while (bidi_explicit_dir_char (FETCH_CHAR (bidi_it->bytepos | |
1150 | + bidi_it->ch_len))) | |
1151 | { | |
1152 | level = bidi_resolve_explicit_1 (bidi_it); | |
1153 | } | |
1154 | ||
1155 | if (level == prev_level) /* empty embedding */ | |
1156 | saved_it.ignore_bn_limit = bidi_it->charpos + 1; | |
1157 | else /* this embedding is non-empty */ | |
1158 | saved_it.ignore_bn_limit = -1; | |
1159 | ||
1160 | bidi_copy_it (bidi_it, &saved_it); | |
1161 | if (bidi_it->ignore_bn_limit > 0) | |
1162 | { | |
1163 | /* We pushed a level, but we shouldn't have. Undo that. */ | |
1164 | if (!bidi_it->invalid_rl_levels) | |
1165 | { | |
1166 | new_level = bidi_pop_embedding_level (bidi_it); | |
1167 | bidi_it->invalid_rl_levels = -1; | |
1168 | if (bidi_it->invalid_levels) | |
1169 | bidi_it->invalid_levels--; | |
1170 | } | |
1171 | if (!bidi_it->invalid_levels) | |
1172 | new_level = bidi_pop_embedding_level (bidi_it); | |
1173 | else | |
1174 | { | |
1175 | bidi_it->invalid_levels--; | |
1176 | bidi_it->invalid_rl_levels--; | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | /* For when the paragraph end is defined by anything other than a | |
1182 | special Unicode character (a.k.a. ``higher protocols''). */ | |
1183 | if (bidi_it->type != NEUTRAL_B) | |
1184 | if (bidi_at_paragraph_end (bidi_it->ch, | |
1185 | bidi_it->bytepos + bidi_it->ch_len)) | |
1186 | bidi_it->type = NEUTRAL_B; | |
1187 | ||
1188 | if (bidi_it->type == NEUTRAL_B) /* X8 */ | |
1189 | { | |
1190 | bidi_set_paragraph_end (bidi_it); | |
1191 | bidi_it->orig_type = bidi_it->type; /* needed below and in L1 */ | |
2d6e4628 | 1192 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1193 | } |
1194 | ||
1195 | return new_level; | |
1196 | } | |
1197 | ||
1198 | /* Advance in the buffer, resolve weak types and return the type of | |
1199 | the next character after weak type resolution. */ | |
1200 | bidi_type_t | |
1201 | bidi_resolve_weak (struct bidi_it *bidi_it) | |
1202 | { | |
1203 | bidi_type_t type; | |
1204 | bidi_dir_t override; | |
1205 | int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1206 | int new_level = bidi_resolve_explicit (bidi_it); | |
1207 | int next_char; | |
1208 | bidi_type_t type_of_next; | |
1209 | struct bidi_it saved_it; | |
1210 | ||
1211 | type = bidi_it->type; | |
1212 | override = bidi_it->level_stack[bidi_it->stack_idx].override; | |
1213 | ||
1214 | if (type == UNKNOWN_BT | |
1215 | || type == LRE | |
1216 | || type == LRO | |
1217 | || type == RLE | |
1218 | || type == RLO | |
1219 | || type == PDF) | |
1220 | abort (); | |
1221 | ||
1222 | if (new_level != prev_level | |
1223 | || bidi_it->type == NEUTRAL_B) | |
1224 | { | |
1225 | /* We've got a new embedding level run, compute the directional | |
1226 | type of sor and initialize per-run variables (UAX#9, clause | |
1227 | X10). */ | |
1228 | bidi_set_sor_type (bidi_it, prev_level, new_level); | |
1229 | } | |
1230 | else if (type == NEUTRAL_S || type == NEUTRAL_WS | |
1231 | || type == WEAK_BN || type == STRONG_AL) | |
1232 | bidi_it->orig_type = type; /* needed in L1 */ | |
2d6e4628 | 1233 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1234 | |
1235 | /* Level and directional override status are already recorded in | |
1236 | bidi_it, and do not need any change; see X6. */ | |
1237 | if (override == R2L) /* X6 */ | |
1238 | type = STRONG_R; | |
1239 | else if (override == L2R) | |
1240 | type = STRONG_L; | |
1241 | else if (type == STRONG_AL) | |
1242 | type = STRONG_R; /* W3 */ | |
1243 | else if (type == WEAK_NSM) /* W1 */ | |
1244 | { | |
1245 | /* Note that we don't need to consider the case where the prev | |
1246 | character has its type overridden by an RLO or LRO: such | |
1247 | characters are outside the current level run, and thus not | |
1248 | relevant to this NSM. Thus, NSM gets the pristine_type of | |
1249 | the previous character. */ | |
1250 | if (bidi_it->prev.type != UNKNOWN_BT) | |
1251 | type = bidi_it->prev.pristine_type; | |
1252 | else if (bidi_it->sor == R2L) | |
1253 | type = STRONG_R; | |
1254 | else if (bidi_it->sor == L2R) | |
1255 | type = STRONG_L; | |
1256 | else /* shouldn't happen! */ | |
1257 | abort (); | |
1258 | if (type == WEAK_EN /* W2 after W1 */ | |
1259 | && bidi_it->last_strong.orig_type == STRONG_AL) | |
1260 | type = WEAK_AN; | |
1261 | } | |
1262 | else if (type == WEAK_EN /* W2 */ | |
1263 | && bidi_it->last_strong.orig_type == STRONG_AL) | |
1264 | type = WEAK_AN; | |
1265 | else if ((type == WEAK_ES | |
1266 | && (bidi_it->prev.orig_type == WEAK_EN /* W4 */ | |
1267 | && (bidi_it->prev.pristine_type == WEAK_EN | |
1268 | || bidi_it->prev.pristine_type == WEAK_NSM))) /* aft W1 */ | |
1269 | || (type == WEAK_CS | |
1270 | && ((bidi_it->prev.orig_type == WEAK_EN | |
1271 | && (bidi_it->prev.pristine_type == WEAK_EN /* W4 */ | |
1272 | || bidi_it->prev.pristine_type == WEAK_NSM)) /* a/W1 */ | |
1273 | || bidi_it->prev.orig_type == WEAK_AN))) /* W4 */ | |
1274 | { | |
1275 | next_char = FETCH_CHAR (bidi_it->bytepos + bidi_it->ch_len); | |
1276 | type_of_next = bidi_get_type (next_char); | |
1277 | ||
1278 | if (type_of_next == WEAK_BN | |
1279 | || bidi_explicit_dir_char (next_char)) | |
1280 | { | |
1281 | bidi_copy_it (&saved_it, bidi_it); | |
1282 | while (bidi_resolve_explicit (bidi_it) == new_level | |
1283 | && bidi_it->type == WEAK_BN) | |
1284 | ; | |
1285 | type_of_next = bidi_it->type; | |
1286 | bidi_copy_it (bidi_it, &saved_it); | |
1287 | } | |
1288 | ||
1289 | /* If the next character is EN, but the last strong-type | |
1290 | character is AL, that next EN will be changed to AN when we | |
1291 | process it in W2 above. So in that case, this ES should not | |
1292 | be changed into EN. */ | |
1293 | if (type == WEAK_ES | |
1294 | && type_of_next == WEAK_EN | |
1295 | && bidi_it->last_strong.orig_type != STRONG_AL) | |
1296 | type = WEAK_EN; | |
1297 | else if (type == WEAK_CS) | |
1298 | { | |
1299 | if (bidi_it->prev.orig_type == WEAK_AN | |
1300 | && (type_of_next == WEAK_AN | |
1301 | /* If the next character is EN, but the last | |
1302 | strong-type character is AL, EN will be later | |
1303 | changed to AN when we process it in W2 above. So | |
1304 | in that case, this ES should not be changed into | |
1305 | EN. */ | |
1306 | || (type_of_next == WEAK_EN | |
1307 | && bidi_it->last_strong.orig_type == STRONG_AL))) | |
1308 | type = WEAK_AN; | |
1309 | else if (bidi_it->prev.orig_type == WEAK_EN | |
1310 | && type_of_next == WEAK_EN | |
1311 | && bidi_it->last_strong.orig_type != STRONG_AL) | |
1312 | type = WEAK_EN; | |
1313 | } | |
1314 | } | |
1315 | else if (type == WEAK_ET /* W5: ET with EN before or after it */ | |
1316 | || type == WEAK_BN) /* W5/Retaining */ | |
1317 | { | |
1318 | if (bidi_it->prev.orig_type == WEAK_EN /* ET/BN with EN before it */ | |
1319 | || bidi_it->next_en_pos > bidi_it->charpos) | |
1320 | type = WEAK_EN; | |
1321 | /* W5: ET with EN after it. */ | |
1322 | else | |
1323 | { | |
1324 | int en_pos = bidi_it->charpos + 1; | |
1325 | ||
1326 | next_char = FETCH_CHAR (bidi_it->bytepos + bidi_it->ch_len); | |
1327 | type_of_next = bidi_get_type (next_char); | |
1328 | ||
1329 | if (type_of_next == WEAK_ET | |
1330 | || type_of_next == WEAK_BN | |
1331 | || bidi_explicit_dir_char (next_char)) | |
1332 | { | |
1333 | bidi_copy_it (&saved_it, bidi_it); | |
1334 | while (bidi_resolve_explicit (bidi_it) == new_level | |
1335 | && (bidi_it->type == WEAK_BN || bidi_it->type == WEAK_ET)) | |
1336 | ; | |
1337 | type_of_next = bidi_it->type; | |
1338 | en_pos = bidi_it->charpos; | |
1339 | bidi_copy_it (bidi_it, &saved_it); | |
1340 | } | |
1341 | if (type_of_next == WEAK_EN) | |
1342 | { | |
1343 | /* If the last strong character is AL, the EN we've | |
1344 | found will become AN when we get to it (W2). */ | |
1345 | if (bidi_it->last_strong.orig_type != STRONG_AL) | |
1346 | { | |
1347 | type = WEAK_EN; | |
1348 | /* Remember this EN position, to speed up processing | |
1349 | of the next ETs. */ | |
1350 | bidi_it->next_en_pos = en_pos; | |
1351 | } | |
1352 | else if (type == WEAK_BN) | |
1353 | type = NEUTRAL_ON; /* W6/Retaining */ | |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | if (type == WEAK_ES || type == WEAK_ET || type == WEAK_CS /* W6 */ | |
1359 | || (type == WEAK_BN && (bidi_it->prev.orig_type == WEAK_CS /* W6/Ret. */ | |
1360 | || bidi_it->prev.orig_type == WEAK_ES | |
1361 | || bidi_it->prev.orig_type == WEAK_ET))) | |
1362 | type = NEUTRAL_ON; | |
1363 | ||
1364 | /* Store the type we've got so far, before we clobber it with strong | |
1365 | types in W7 and while resolving neutral types. But leave alone | |
1366 | the original types that were recorded above, because we will need | |
1367 | them for the L1 clause. */ | |
1368 | if (bidi_it->orig_type == UNKNOWN_BT) | |
1369 | bidi_it->orig_type = type; | |
2d6e4628 | 1370 | bidi_check_type (bidi_it->orig_type); |
b7b65b15 EZ |
1371 | |
1372 | if (type == WEAK_EN) /* W7 */ | |
1373 | { | |
1374 | if ((bidi_it->last_strong.orig_type == STRONG_L) | |
1375 | || (bidi_it->last_strong.type == UNKNOWN_BT && bidi_it->sor == L2R)) | |
1376 | type = STRONG_L; | |
1377 | } | |
1378 | ||
1379 | bidi_it->type = type; | |
2d6e4628 | 1380 | bidi_check_type (bidi_it->type); |
b7b65b15 EZ |
1381 | return type; |
1382 | } | |
1383 | ||
1384 | bidi_type_t | |
1385 | bidi_resolve_neutral (struct bidi_it *bidi_it) | |
1386 | { | |
1387 | int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1388 | bidi_type_t type = bidi_resolve_weak (bidi_it); | |
1389 | int current_level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1390 | ||
1391 | if (!(type == STRONG_R | |
1392 | || type == STRONG_L | |
1393 | || type == WEAK_BN | |
1394 | || type == WEAK_EN | |
1395 | || type == WEAK_AN | |
1396 | || type == NEUTRAL_B | |
1397 | || type == NEUTRAL_S | |
1398 | || type == NEUTRAL_WS | |
1399 | || type == NEUTRAL_ON)) | |
1400 | abort (); | |
1401 | ||
1402 | if (bidi_get_category (type) == NEUTRAL | |
1403 | || (type == WEAK_BN && prev_level == current_level)) | |
1404 | { | |
1405 | if (bidi_it->next_for_neutral.type != UNKNOWN_BT) | |
1406 | type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type, | |
1407 | bidi_it->next_for_neutral.type, | |
1408 | current_level); | |
1409 | else | |
1410 | { | |
1411 | /* Arrrgh!! The UAX#9 algorithm is too deeply entrenched in | |
1412 | the assumption of batch-style processing; see clauses W4, | |
1413 | W5, and especially N1, which require to look far forward | |
1414 | (as well as back) in the buffer. May the fleas of a | |
1415 | thousand camels infest the armpits of those who design | |
1416 | supposedly general-purpose algorithms by looking at their | |
1417 | own implementations, and fail to consider other possible | |
1418 | implementations! */ | |
1419 | struct bidi_it saved_it; | |
1420 | bidi_type_t next_type; | |
1421 | ||
1422 | if (bidi_it->scan_dir == -1) | |
1423 | abort (); | |
1424 | ||
1425 | bidi_copy_it (&saved_it, bidi_it); | |
1426 | /* Scan the text forward until we find the first non-neutral | |
1427 | character, and then use that to resolve the neutral we | |
1428 | are dealing with now. We also cache the scanned iterator | |
1429 | states, to salvage some of the effort later. */ | |
1430 | bidi_cache_iterator_state (bidi_it, 0); | |
1431 | do { | |
1432 | /* Record the info about the previous character, so that | |
1433 | it will be cached below with this state. */ | |
1434 | if (bidi_it->orig_type != WEAK_BN /* W1/Retaining */ | |
1435 | && bidi_it->type != WEAK_BN) | |
1436 | bidi_remember_char (&bidi_it->prev, bidi_it); | |
1437 | type = bidi_resolve_weak (bidi_it); | |
1438 | /* Paragraph separators have their levels fully resolved | |
1439 | at this point, so cache them as resolved. */ | |
1440 | bidi_cache_iterator_state (bidi_it, type == NEUTRAL_B); | |
1441 | /* FIXME: implement L1 here, by testing for a newline and | |
1442 | resetting the level for any sequence of whitespace | |
1443 | characters adjacent to it. */ | |
1444 | } while (!(type == NEUTRAL_B | |
1445 | || (type != WEAK_BN | |
1446 | && bidi_get_category (type) != NEUTRAL) | |
1447 | /* This is all per level run, so stop when we | |
1448 | reach the end of this level run. */ | |
1449 | || bidi_it->level_stack[bidi_it->stack_idx].level != | |
1450 | current_level)); | |
1451 | ||
1452 | bidi_remember_char (&saved_it.next_for_neutral, bidi_it); | |
1453 | ||
1454 | switch (type) | |
1455 | { | |
1456 | case STRONG_L: | |
1457 | case STRONG_R: | |
1458 | case STRONG_AL: | |
1459 | next_type = type; | |
1460 | break; | |
1461 | case WEAK_EN: | |
1462 | case WEAK_AN: | |
1463 | /* N1: ``European and Arabic numbers are treated as | |
1464 | though they were R.'' */ | |
1465 | next_type = STRONG_R; | |
1466 | saved_it.next_for_neutral.type = STRONG_R; | |
1467 | break; | |
1468 | case WEAK_BN: | |
1469 | if (!bidi_explicit_dir_char (bidi_it->ch)) | |
1470 | abort (); /* can't happen: BNs are skipped */ | |
1471 | /* FALLTHROUGH */ | |
1472 | case NEUTRAL_B: | |
1473 | /* Marched all the way to the end of this level run. | |
1474 | We need to use the eor type, whose information is | |
1475 | stored by bidi_set_sor_type in the prev_for_neutral | |
1476 | member. */ | |
1477 | if (saved_it.type != WEAK_BN | |
1478 | || bidi_get_category (bidi_it->prev.orig_type) == NEUTRAL) | |
1479 | { | |
1480 | next_type = bidi_it->prev_for_neutral.type; | |
1481 | saved_it.next_for_neutral.type = next_type; | |
2d6e4628 | 1482 | bidi_check_type (next_type); |
b7b65b15 EZ |
1483 | } |
1484 | else | |
1485 | { | |
1486 | /* This is a BN which does not adjoin neutrals. | |
1487 | Leave its type alone. */ | |
1488 | bidi_copy_it (bidi_it, &saved_it); | |
1489 | return bidi_it->type; | |
1490 | } | |
1491 | break; | |
1492 | default: | |
1493 | abort (); | |
1494 | } | |
1495 | type = bidi_resolve_neutral_1 (saved_it.prev_for_neutral.type, | |
1496 | next_type, current_level); | |
1497 | saved_it.type = type; | |
2d6e4628 | 1498 | bidi_check_type (type); |
b7b65b15 EZ |
1499 | bidi_copy_it (bidi_it, &saved_it); |
1500 | } | |
1501 | } | |
1502 | return type; | |
1503 | } | |
1504 | ||
1505 | /* Given an iterator state in BIDI_IT, advance one character position | |
1506 | in the buffer to the next character (in the logical order), resolve | |
1507 | the bidi type of that next character, and return that type. */ | |
1508 | bidi_type_t | |
1509 | bidi_type_of_next_char (struct bidi_it *bidi_it) | |
1510 | { | |
1511 | bidi_type_t type; | |
1512 | ||
1513 | /* This should always be called during a forward scan. */ | |
1514 | if (bidi_it->scan_dir != 1) | |
1515 | abort (); | |
1516 | ||
1517 | /* Reset the limit until which to ignore BNs if we step out of the | |
1518 | area where we found only empty levels. */ | |
1519 | if ((bidi_it->ignore_bn_limit > 0 | |
1520 | && bidi_it->ignore_bn_limit <= bidi_it->charpos) | |
1521 | || (bidi_it->ignore_bn_limit == -1 | |
1522 | && !bidi_explicit_dir_char (bidi_it->ch))) | |
1523 | bidi_it->ignore_bn_limit = 0; | |
1524 | ||
1525 | type = bidi_resolve_neutral (bidi_it); | |
1526 | ||
1527 | return type; | |
1528 | } | |
1529 | ||
1530 | /* Given an iterator state BIDI_IT, advance one character position in | |
1531 | the buffer to the next character (in the logical order), resolve | |
1532 | the embedding and implicit levels of that next character, and | |
1533 | return the resulting level. */ | |
1534 | int | |
1535 | bidi_level_of_next_char (struct bidi_it *bidi_it) | |
1536 | { | |
1537 | bidi_type_t type; | |
1538 | int level, prev_level = -1; | |
1539 | struct bidi_saved_info next_for_neutral; | |
1540 | ||
1541 | if (bidi_it->scan_dir == 1) | |
1542 | { | |
1543 | /* There's no sense in trying to advance if we hit end of text. */ | |
1544 | if (bidi_it->ch == BIDI_EOB) | |
1545 | return bidi_it->resolved_level; | |
1546 | ||
1547 | /* Record the info about the previous character. */ | |
1548 | if (bidi_it->orig_type != WEAK_BN /* W1/Retaining */ | |
1549 | && bidi_it->type != WEAK_BN) | |
1550 | bidi_remember_char (&bidi_it->prev, bidi_it); | |
1551 | if (bidi_it->orig_type == STRONG_R || bidi_it->orig_type == STRONG_L | |
1552 | || bidi_it->orig_type == STRONG_AL) | |
1553 | bidi_remember_char (&bidi_it->last_strong, bidi_it); | |
1554 | /* FIXME: it sounds like we don't need both prev and | |
1555 | prev_for_neutral members, but I'm leaving them both for now. */ | |
1556 | if (bidi_it->type == STRONG_R || bidi_it->type == STRONG_L | |
1557 | || bidi_it->type == WEAK_EN || bidi_it->type == WEAK_AN) | |
1558 | bidi_remember_char (&bidi_it->prev_for_neutral, bidi_it); | |
1559 | ||
1560 | /* If we overstepped the characters used for resolving neutrals | |
1561 | and whitespace, invalidate their info in the iterator. */ | |
1562 | if (bidi_it->charpos >= bidi_it->next_for_neutral.charpos) | |
1563 | bidi_it->next_for_neutral.type = UNKNOWN_BT; | |
1564 | if (bidi_it->next_en_pos >= 0 | |
1565 | && bidi_it->charpos >= bidi_it->next_en_pos) | |
1566 | bidi_it->next_en_pos = -1; | |
1567 | if (bidi_it->next_for_ws.type != UNKNOWN_BT | |
1568 | && bidi_it->charpos >= bidi_it->next_for_ws.charpos) | |
1569 | bidi_it->next_for_ws.type = UNKNOWN_BT; | |
1570 | ||
1571 | /* This must be taken before we fill the iterator with the info | |
1572 | about the next char. If we scan backwards, the iterator | |
1573 | state must be already cached, so there's no need to know the | |
1574 | embedding level of the previous character, since we will be | |
1575 | returning to our caller shortly. */ | |
1576 | prev_level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1577 | } | |
1578 | next_for_neutral = bidi_it->next_for_neutral; | |
1579 | ||
1580 | /* Perhaps it is already cached. */ | |
1581 | type = bidi_cache_find (bidi_it->charpos + bidi_it->scan_dir, -1, bidi_it); | |
1582 | if (type != UNKNOWN_BT) | |
1583 | { | |
1584 | /* Don't lose the information for resolving neutrals! The | |
1585 | cached states could have been cached before their | |
1586 | next_for_neutral member was computed. If we are on our way | |
1587 | forward, we can simply take the info from the previous | |
1588 | state. */ | |
1589 | if (bidi_it->scan_dir == 1 | |
1590 | && bidi_it->next_for_neutral.type == UNKNOWN_BT) | |
1591 | bidi_it->next_for_neutral = next_for_neutral; | |
1592 | ||
1593 | /* If resolved_level is -1, it means this state was cached | |
1594 | before it was completely resolved, so we cannot return | |
1595 | it. */ | |
1596 | if (bidi_it->resolved_level != -1) | |
1597 | return bidi_it->resolved_level; | |
1598 | } | |
1599 | if (bidi_it->scan_dir == -1) | |
1600 | /* If we are going backwards, the iterator state is already cached | |
1601 | from previous scans, and should be fully resolved. */ | |
1602 | abort (); | |
1603 | ||
1604 | if (type == UNKNOWN_BT) | |
1605 | type = bidi_type_of_next_char (bidi_it); | |
1606 | ||
1607 | if (type == NEUTRAL_B) | |
1608 | return bidi_it->resolved_level; | |
1609 | ||
1610 | level = bidi_it->level_stack[bidi_it->stack_idx].level; | |
1611 | if ((bidi_get_category (type) == NEUTRAL /* && type != NEUTRAL_B */) | |
1612 | || (type == WEAK_BN && prev_level == level)) | |
1613 | { | |
1614 | if (bidi_it->next_for_neutral.type == UNKNOWN_BT) | |
1615 | abort (); | |
1616 | ||
1617 | /* If the cached state shows a neutral character, it was not | |
1618 | resolved by bidi_resolve_neutral, so do it now. */ | |
1619 | type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type, | |
1620 | bidi_it->next_for_neutral.type, | |
1621 | level); | |
1622 | } | |
1623 | ||
1624 | if (!(type == STRONG_R | |
1625 | || type == STRONG_L | |
1626 | || type == WEAK_BN | |
1627 | || type == WEAK_EN | |
1628 | || type == WEAK_AN)) | |
1629 | abort (); | |
1630 | bidi_it->type = type; | |
2d6e4628 | 1631 | bidi_check_type (bidi_it->type); |
b7b65b15 EZ |
1632 | |
1633 | /* For L1 below, we need to know, for each WS character, whether | |
1634 | it belongs to a sequence of WS characters preceeding a newline | |
1635 | or a TAB or a paragraph separator. */ | |
1636 | if (bidi_it->pristine_type == NEUTRAL_WS | |
1637 | && bidi_it->next_for_ws.type == UNKNOWN_BT) | |
1638 | { | |
1639 | int ch; | |
1640 | int clen = bidi_it->ch_len; | |
1641 | int bpos = bidi_it->bytepos; | |
1642 | int cpos = bidi_it->charpos; | |
1643 | bidi_type_t chtype; | |
1644 | ||
1645 | do { | |
1646 | /*_fetch_multibyte_char_len = 1;*/ | |
1647 | ch = FETCH_CHAR (bpos + clen); | |
1648 | bpos += clen; | |
1649 | cpos++; | |
1650 | clen = CHAR_BYTES (ch); | |
1651 | if (ch == '\n' /* || ch == LINESEP_CHAR */) | |
1652 | chtype = NEUTRAL_B; | |
1653 | else | |
1654 | chtype = bidi_get_type (ch); | |
1655 | } while (chtype == NEUTRAL_WS || chtype == WEAK_BN | |
1656 | || bidi_explicit_dir_char (ch)); /* L1/Retaining */ | |
1657 | bidi_it->next_for_ws.type = chtype; | |
2d6e4628 | 1658 | bidi_check_type (bidi_it->next_for_ws.type); |
b7b65b15 EZ |
1659 | bidi_it->next_for_ws.charpos = cpos; |
1660 | bidi_it->next_for_ws.bytepos = bpos; | |
1661 | } | |
1662 | ||
1663 | /* Resolve implicit levels, with a twist: PDFs get the embedding | |
1664 | level of the enbedding they terminate. See below for the | |
1665 | reason. */ | |
1666 | if (bidi_it->pristine_type == PDF | |
1667 | /* Don't do this if this formatting code didn't change the | |
1668 | embedding level due to invalid or empty embeddings. */ | |
1669 | && prev_level != level) | |
1670 | { | |
1671 | /* Don't look in UAX#9 for the reason for this: it's our own | |
1672 | private quirk. The reason is that we want the formatting | |
1673 | codes to be delivered so that they bracket the text of their | |
1674 | embedding. For example, given the text | |
1675 | ||
1676 | {RLO}teST{PDF} | |
1677 | ||
1678 | we want it to be displayed as | |
1679 | ||
1680 | {RLO}STet{PDF} | |
1681 | ||
1682 | not as | |
1683 | ||
1684 | STet{RLO}{PDF} | |
1685 | ||
1686 | which will result because we bump up the embedding level as | |
1687 | soon as we see the RLO and pop it as soon as we see the PDF, | |
1688 | so RLO itself has the same embedding level as "teST", and | |
1689 | thus would be normally delivered last, just before the PDF. | |
1690 | The switch below fiddles with the level of PDF so that this | |
1691 | ugly side effect does not happen. | |
1692 | ||
1693 | (This is, of course, only important if the formatting codes | |
1694 | are actually displayed, but Emacs does display them if the | |
1695 | user wants to.) */ | |
1696 | level = prev_level; | |
1697 | } | |
1698 | else if (bidi_it->pristine_type == NEUTRAL_B /* L1 */ | |
1699 | || bidi_it->pristine_type == NEUTRAL_S | |
1700 | || bidi_it->ch == '\n' /* || bidi_it->ch == LINESEP_CHAR */ | |
1701 | || (bidi_it->pristine_type == NEUTRAL_WS | |
1702 | && (bidi_it->next_for_ws.type == NEUTRAL_B | |
1703 | || bidi_it->next_for_ws.type == NEUTRAL_S))) | |
1704 | level = bidi_it->level_stack[0].level; | |
1705 | else if ((level & 1) == 0) /* I1 */ | |
1706 | { | |
1707 | if (type == STRONG_R) | |
1708 | level++; | |
1709 | else if (type == WEAK_EN || type == WEAK_AN) | |
1710 | level += 2; | |
1711 | } | |
1712 | else /* I2 */ | |
1713 | { | |
1714 | if (type == STRONG_L || type == WEAK_EN || type == WEAK_AN) | |
1715 | level++; | |
1716 | } | |
1717 | ||
1718 | bidi_it->resolved_level = level; | |
1719 | return level; | |
1720 | } | |
1721 | ||
1722 | /* Move to the other edge of a level given by LEVEL. If END_FLAG is | |
1723 | non-zero, we are at the end of a level, and we need to prepare to | |
1724 | resume the scan of the lower level. | |
1725 | ||
1726 | If this level's other edge is cached, we simply jump to it, filling | |
1727 | the iterator structure with the iterator state on the other edge. | |
1728 | Otherwise, we walk the buffer until we come back to the same level | |
1729 | as LEVEL. | |
1730 | ||
1731 | Note: we are not talking here about a ``level run'' in the UAX#9 | |
1732 | sense of the term, but rather about a ``level'' which includes | |
1733 | all the levels higher than it. In other words, given the levels | |
1734 | like this: | |
1735 | ||
1736 | 11111112222222333333334443343222222111111112223322111 | |
1737 | A B C | |
1738 | ||
1739 | and assuming we are at point A scanning left to right, this | |
1740 | function moves to point C, whereas the UAX#9 ``level 2 run'' ends | |
1741 | at point B. */ | |
1742 | static void | |
1743 | bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag) | |
1744 | { | |
1745 | int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir; | |
1746 | int idx; | |
1747 | ||
1748 | /* Try the cache first. */ | |
1749 | if ((idx = bidi_cache_find_level_change (level, dir, end_flag)) >= 0) | |
1750 | bidi_cache_fetch_state (idx, bidi_it); | |
1751 | else | |
1752 | { | |
1753 | int new_level; | |
1754 | ||
1755 | if (end_flag) | |
1756 | abort (); /* if we are at end of level, its edges must be cached */ | |
1757 | ||
1758 | bidi_cache_iterator_state (bidi_it, 1); | |
1759 | do { | |
1760 | new_level = bidi_level_of_next_char (bidi_it); | |
1761 | bidi_cache_iterator_state (bidi_it, 1); | |
1762 | } while (new_level >= level); | |
1763 | } | |
1764 | } | |
1765 | ||
1766 | void | |
1767 | bidi_get_next_char_visually (struct bidi_it *bidi_it) | |
1768 | { | |
1769 | int old_level, new_level, next_level; | |
1770 | struct bidi_it prev_bidi_it; | |
1771 | ||
1772 | if (bidi_it->scan_dir == 0) | |
1773 | { | |
1774 | bidi_it->scan_dir = 1; /* default to logical order */ | |
1775 | } | |
1776 | ||
1777 | if (bidi_it->new_paragraph) | |
1778 | bidi_paragraph_init (bidi_overriding_paragraph_direction, bidi_it); | |
1779 | if (bidi_cache_idx == 0) | |
1780 | bidi_copy_it (&prev_bidi_it, bidi_it); | |
1781 | ||
1782 | old_level = bidi_it->resolved_level; | |
1783 | new_level = bidi_level_of_next_char (bidi_it); | |
1784 | if (bidi_it->ch == BIDI_EOB) | |
1785 | return; | |
1786 | ||
1787 | /* Reordering of resolved levels (clause L2) is implemented by | |
1788 | jumping to the other edge of the level and flipping direction of | |
1789 | scanning the buffer whenever we find a level change. */ | |
1790 | if (new_level != old_level) | |
1791 | { | |
1792 | int ascending = new_level > old_level; | |
1793 | int level_to_search = ascending ? old_level + 1 : old_level; | |
1794 | int incr = ascending ? 1 : -1; | |
1795 | int expected_next_level = old_level + incr; | |
1796 | ||
1797 | /* If we don't have anything cached yet, we need to cache the | |
1798 | previous character we've seen, since we'll need it to record | |
1799 | where to jump when the last non-base level is exhausted. */ | |
1800 | if (bidi_cache_idx == 0) | |
1801 | bidi_cache_iterator_state (&prev_bidi_it, 1); | |
1802 | /* Jump (or walk) to the other edge of this level. */ | |
1803 | bidi_find_other_level_edge (bidi_it, level_to_search, !ascending); | |
1804 | /* Switch scan direction and peek at the next character in the | |
1805 | new direction. */ | |
1806 | bidi_it->scan_dir = -bidi_it->scan_dir; | |
1807 | ||
1808 | /* The following loop handles the case where the resolved level | |
1809 | jumps by more than one. This is typical for numbers inside a | |
1810 | run of text with left-to-right embedding direction, but can | |
1811 | also happen in other situations. In those cases the decision | |
1812 | where to continue after a level change, and in what direction, | |
1813 | is tricky. For example, given a text like below: | |
1814 | ||
1815 | abcdefgh | |
1816 | 11336622 | |
1817 | ||
1818 | (where the numbers below the text show the resolved levels), | |
1819 | the result of reordering according to UAX#9 should be this: | |
1820 | ||
1821 | efdcghba | |
1822 | ||
1823 | This is implemented by the loop below which flips direction | |
1824 | and jumps to the other edge of the level each time it finds | |
1825 | the new level not to be the expected one. The expected level | |
1826 | is always one more or one less than the previous one. */ | |
1827 | next_level = bidi_peek_at_next_level (bidi_it); | |
1828 | while (next_level != expected_next_level) | |
1829 | { | |
1830 | expected_next_level += incr; | |
1831 | level_to_search += incr; | |
1832 | bidi_find_other_level_edge (bidi_it, level_to_search, !ascending); | |
1833 | bidi_it->scan_dir = -bidi_it->scan_dir; | |
1834 | next_level = bidi_peek_at_next_level (bidi_it); | |
1835 | } | |
1836 | ||
1837 | /* Finally, deliver the next character in the new direction. */ | |
1838 | next_level = bidi_level_of_next_char (bidi_it); | |
1839 | } | |
1840 | ||
1841 | if (bidi_it->scan_dir == 1 && bidi_cache_idx) | |
1842 | { | |
1843 | /* If we are at paragraph's base embedding level and beyond the | |
1844 | last cached position, the cache's job is done and we can | |
1845 | discard it. */ | |
1846 | if (bidi_it->resolved_level == bidi_it->level_stack[0].level | |
1847 | && bidi_it->charpos > bidi_cache[bidi_cache_idx - 1].charpos) | |
1848 | bidi_cache_reset (); | |
1849 | /* But as long as we are caching during forward scan, we must | |
1850 | cache each state, or else the cache integrity will be | |
1851 | compromised: it assumes cached states correspond to buffer | |
1852 | positions 1:1. */ | |
1853 | else | |
1854 | bidi_cache_iterator_state (bidi_it, 1); | |
1855 | } | |
1856 | } | |
1857 | ||
1858 | /* This is meant to be called from within the debugger, whenever you | |
1859 | wish to examine the cache contents. */ | |
1860 | void | |
1861 | bidi_dump_cached_states (void) | |
1862 | { | |
1863 | int i; | |
1864 | int ndigits = 1; | |
1865 | ||
1866 | if (bidi_cache_idx == 0) | |
1867 | { | |
1868 | fprintf (stderr, "The cache is empty.\n"); | |
1869 | return; | |
1870 | } | |
1871 | fprintf (stderr, "Total of %d state%s in cache:\n", | |
1872 | bidi_cache_idx, bidi_cache_idx == 1 ? "" : "s"); | |
1873 | ||
1874 | for (i = bidi_cache[bidi_cache_idx - 1].charpos; i > 0; i /= 10) | |
1875 | ndigits++; | |
1876 | fputs ("ch ", stderr); | |
1877 | for (i = 0; i < bidi_cache_idx; i++) | |
1878 | fprintf (stderr, "%*c", ndigits, bidi_cache[i].ch); | |
1879 | fputs ("\n", stderr); | |
1880 | fputs ("lvl ", stderr); | |
1881 | for (i = 0; i < bidi_cache_idx; i++) | |
1882 | fprintf (stderr, "%*d", ndigits, bidi_cache[i].resolved_level); | |
1883 | fputs ("\n", stderr); | |
1884 | fputs ("pos ", stderr); | |
1885 | for (i = 0; i < bidi_cache_idx; i++) | |
1886 | fprintf (stderr, "%*d", ndigits, bidi_cache[i].charpos); | |
1887 | fputs ("\n", stderr); | |
1888 | } | |
1889 | ||
1890 | #ifdef TEST_STANDALONE | |
1891 | ||
1892 | #include <sys/stat.h> | |
1893 | #include <signal.h> | |
1894 | ||
1895 | static char display_line[80]; | |
1896 | static int simulate_display; | |
1897 | static int incr = 1; | |
1898 | ||
1899 | void | |
1900 | signal_catcher (int sig) | |
1901 | { | |
1902 | if (simulate_display) | |
1903 | puts (display_line); | |
1904 | else | |
1905 | { | |
1906 | puts ("<"); | |
1907 | fflush (stdout); | |
1908 | } | |
1909 | signal (sig, SIG_DFL); | |
1910 | raise (sig); | |
1911 | } | |
1912 | ||
1913 | void | |
1914 | put_line (char *p) | |
1915 | { | |
1916 | if (simulate_display) | |
1917 | { | |
1918 | if (incr == -1) | |
1919 | { | |
1920 | if (p >= display_line) | |
1921 | memset (display_line, ' ', p - display_line + 1); | |
1922 | } | |
1923 | else | |
1924 | *p = '\0'; | |
1925 | fputs (display_line, stdout); | |
1926 | } | |
1927 | fflush (stdout); | |
1928 | } | |
1929 | ||
1930 | char * | |
1931 | init_display_direction (bidi_dir_t default_dir, int base_level) | |
1932 | { | |
1933 | char *p; | |
1934 | ||
1935 | /* To which display margin should we flush the lines? */ | |
1936 | switch (default_dir) | |
1937 | { | |
1938 | case NEUTRAL_DIR: | |
1939 | if ((base_level & 1) == 0) | |
1940 | { | |
1941 | p = display_line; | |
1942 | incr = 1; | |
1943 | } | |
1944 | else | |
1945 | { | |
1946 | p = display_line + sizeof (display_line) - 1; | |
1947 | *p-- = '\0'; | |
1948 | incr = -1; | |
1949 | } | |
1950 | break; | |
1951 | case L2R: | |
1952 | p = display_line; | |
1953 | incr = 1; | |
1954 | break; | |
1955 | case R2L: | |
1956 | p = display_line + sizeof (display_line) - 1; | |
1957 | *p-- = '\0'; | |
1958 | incr = -1; | |
1959 | break; | |
1960 | default: | |
1961 | abort (); | |
1962 | } | |
1963 | ||
1964 | return p; | |
1965 | } | |
1966 | ||
1967 | static char * | |
1968 | continuation_line (char *p, int need) | |
1969 | { | |
1970 | if (incr == -1) | |
1971 | { | |
1972 | if (p < display_line + need) | |
1973 | { | |
1974 | *p-- = '/'; | |
1975 | put_line (p); | |
1976 | putc ('\n', stdout); | |
1977 | memset (display_line, '>', sizeof(display_line) - 1); | |
1978 | p = display_line + sizeof (display_line) - 1; | |
1979 | *p-- = '\0'; | |
1980 | } | |
1981 | } | |
1982 | else | |
1983 | { | |
1984 | if (p > display_line + sizeof(display_line) - need - 2) | |
1985 | { | |
1986 | *p++ = '\\'; | |
1987 | put_line (p); | |
1988 | putc ('\n', stdout); | |
1989 | memset (display_line, '<', sizeof(display_line) - 1); | |
1990 | p = display_line; | |
1991 | } | |
1992 | } | |
1993 | ||
1994 | return p; | |
1995 | } | |
1996 | ||
1997 | int main (int argc, char *argv[]) | |
1998 | { | |
1999 | bidi_dir_t default_dir = NEUTRAL_DIR; | |
2000 | char lots_of_equals[] = "\n===============================================================================\n"; | |
2001 | ||
2002 | ||
2003 | if (argc > 1 && argv[1][0] == '-') | |
2004 | { | |
2005 | switch (argv[1][1]) | |
2006 | { | |
2007 | case 'R': | |
2008 | default_dir = R2L; | |
2009 | simulate_display = 1; | |
2010 | ++argv; | |
2011 | break; | |
2012 | case 'L': | |
2013 | default_dir = L2R; | |
2014 | simulate_display = 1; | |
2015 | ++argv; | |
2016 | break; | |
2017 | case 'N': | |
2018 | simulate_display = 1; | |
2019 | ++argv; | |
2020 | break; | |
2021 | default: | |
2022 | break; | |
2023 | } | |
2024 | bidi_overriding_paragraph_direction = default_dir; | |
2025 | } | |
2026 | ||
2027 | for (argv++; *argv; argv++) | |
2028 | { | |
2029 | FILE *in = fopen (*argv, "rb"); | |
2030 | struct stat stat_buf; | |
2031 | struct bidi_it iterator; | |
2032 | size_t i; | |
2033 | char *p = display_line; | |
2034 | int base_level; | |
2035 | unsigned char *s, *d, *s_end; | |
2036 | ||
2037 | if (!in || stat (*argv, &stat_buf)) | |
2038 | { | |
2039 | perror (*argv); | |
2040 | continue; | |
2041 | } | |
2042 | ||
2043 | if (stat_buf.st_size > input_buf_size) | |
2044 | { | |
2045 | input_buf = realloc (input_buf, stat_buf.st_size + 1); | |
2046 | if (!input_buf) | |
2047 | { | |
2048 | perror ("realloc input buffer"); | |
2049 | continue; | |
2050 | } | |
2051 | input_buf_size = stat_buf.st_size; | |
2052 | } | |
2053 | if (fread (input_buf, 1, stat_buf.st_size, in) != stat_buf.st_size) | |
2054 | { | |
2055 | perror ("reading input"); | |
2056 | continue; | |
2057 | } | |
2058 | input_buf[stat_buf.st_size] = '\0'; | |
2059 | for (d = s = input_buf, s_end = s + stat_buf.st_size - 1; *s; s++) | |
2060 | { | |
2061 | if (*s != '\r' || s >= s_end || s[1] != '\n') | |
2062 | *d++ = *s; | |
2063 | } | |
2064 | stat_buf.st_size = d - input_buf; | |
2065 | input_buf[stat_buf.st_size] = '\0'; | |
2066 | ||
2067 | /* Done with administrivia, now for some real work... */ | |
2068 | signal (SIGABRT, signal_catcher); | |
2069 | signal (SIGINT, signal_catcher); | |
2070 | bidi_init_it (-1, default_dir, &iterator); | |
2071 | if (simulate_display) | |
2072 | { | |
2073 | p = init_display_direction (default_dir, | |
2074 | iterator.level_stack[0].level); | |
2075 | } | |
2076 | ||
2077 | memset (display_line, incr == -1 ? '>' : '<', sizeof (display_line) - 1); | |
2078 | display_line[sizeof (display_line) - 1] = '\0'; | |
2079 | base_level = iterator.level_stack[0].level; | |
2080 | ||
2081 | for (i = 0; i <= stat_buf.st_size; i++) | |
2082 | { | |
2083 | int c; | |
2084 | ||
2085 | bidi_get_next_char_visually (&iterator); | |
2086 | c = iterator.ch; | |
2087 | ||
2088 | if (c == '\n' || c == BIDI_EOB) | |
2089 | { | |
2090 | if (simulate_display) | |
2091 | { | |
2092 | put_line (p); | |
2093 | /* FIXME: if -R or -L, need to init paragraph here. */ | |
2094 | } | |
2095 | if (c == BIDI_EOB) | |
2096 | break; | |
2097 | putc (c, stdout); | |
2098 | } | |
2099 | else if (c >= LRE_CHAR && c <= LRM_CHAR) | |
2100 | { | |
2101 | if (simulate_display) | |
2102 | { | |
2103 | p = continuation_line (p, 5); | |
2104 | if (incr == -1) | |
2105 | { | |
2106 | memcpy (p - 4, bidi_name[c], 5); | |
2107 | p -= 5; | |
2108 | } | |
2109 | else | |
2110 | { | |
2111 | memcpy (p, bidi_name[c], 5); | |
2112 | p += 5; | |
2113 | } | |
2114 | } | |
2115 | else | |
2116 | fputs (bidi_name[c], stdout); | |
2117 | } | |
2118 | else if (c < ' ') | |
2119 | { | |
2120 | if (simulate_display) | |
2121 | { | |
2122 | p = continuation_line (p, 2); | |
2123 | if (incr == -1) | |
2124 | { | |
2125 | *p-- = '@' + c; | |
2126 | *p-- = '^'; | |
2127 | } | |
2128 | else | |
2129 | { | |
2130 | *p++ = '^'; | |
2131 | *p++ = '@' + c; | |
2132 | } | |
2133 | } | |
2134 | else | |
2135 | printf ("^%c", (c | 0x40)); | |
2136 | } | |
2137 | else | |
2138 | { | |
2139 | int c1 = (iterator.type == STRONG_R) ? bidi_mirror_char (c) : c; | |
2140 | ||
2141 | if (simulate_display) | |
2142 | { | |
2143 | p = continuation_line (p, 1); | |
2144 | *p = c1; | |
2145 | p += incr; | |
2146 | } | |
2147 | else | |
2148 | putc (c1, stdout); | |
2149 | } | |
2150 | ||
2151 | if (iterator.ch == '\n') | |
2152 | { | |
2153 | if (base_level != iterator.level_stack[0].level) | |
2154 | base_level = iterator.level_stack[0].level; | |
2155 | p = init_display_direction (default_dir, base_level); | |
2156 | memset (display_line, incr == -1 ? '>' : '<', | |
2157 | sizeof (display_line) - 1); | |
2158 | } | |
2159 | } | |
2160 | fputs (lots_of_equals, stdout); | |
2161 | fclose (in); | |
2162 | } | |
2163 | return 0; | |
2164 | } | |
2165 | #endif |