1 /* Low-level bidirectional buffer-scanning functions for GNU Emacs.
2 Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
22 /* A sequential implementation of the Unicode Bidirectional algorithm,
23 as per UAX#9, a part of the Unicode Standard.
25 Unlike the reference and most other implementations, this one is
26 designed to be called once for every character in the buffer.
28 The main entry point is bidi_get_next_char_visually. Each time it
29 is called, it finds the next character in the visual order, and
30 returns its information in a special structure. The caller is then
31 expected to process this character for display or any other
32 purposes, and call bidi_get_next_char_visually for the next
33 character. See the comments in bidi_get_next_char_visually for
34 more details about its algorithm that finds the next visual-order
35 character by resolving their levels on the fly.
37 A note about references to UAX#9 rules: if the reference says
38 something like "X9/Retaining", it means that you need to refer to
39 rule X9 and to its modifications decribed in the "Implementation
40 Notes" section of UAX#9, under "Retaining Format Codes". */
54 #include "character.h"
55 #include "dispextern.h"
57 static int bidi_initialized
= 0;
59 static Lisp_Object bidi_type_table
;
61 #define LRM_CHAR 0x200E
62 #define RLM_CHAR 0x200F
63 #define LRE_CHAR 0x202A
64 #define RLE_CHAR 0x202B
65 #define PDF_CHAR 0x202C
66 #define LRO_CHAR 0x202D
67 #define RLO_CHAR 0x202E
69 #define CHARSET_HEBREW 0x88
70 #define CHARSET_ARABIC 0x87
71 #define CHARSET_SYRIAC -1 /* these are undefined yet, -1 is invalid */
72 #define CHARSET_THAANA -1
74 /* FIXME: need to define wrappers for FETCH_CHAR etc. that return
75 BIDI_EOB when they hit ZV. */
79 #ifdef TEST_STANDALONE
82 static unsigned char *input_buf
;
83 static size_t input_buf_size
;
85 int _fetch_multibyte_char_len
, _c_c_
;
87 #undef FETCH_CHAR_ADVANCE
88 #define FETCH_CHAR_ADVANCE(ch, cp, bp) \
98 #define FETCH_CHAR(n) ((_c_c_ = input_buf[n]) ? _c_c_ : BIDI_EOB)
101 #define CHAR_CHARSET(c) \
102 (((c) >= 128 || ((c) < 8 && (c)) || ((c) >= 'A' && (c) < 'X')) \
104 : ((((c) >= 'X' && (c) <= 'Z') || ((c) >= '6' && (c) <= '9')) \
109 #define CHAR_TO_BYTE(pos) (pos)
111 #define char_bytes(ch) 1
129 static const char *bidi_name
[] =
131 "[???]", "[LRE]", "[LRO]", "[RLE]", "[RLO]", "[PDF]", "[RLM]", "[LRM]"
134 #endif /* TEST_STANDALONE */
136 /* Local data structures. (Look in dispextern.h for the rest.) */
138 /* What we need to know about the current paragraph. */
139 struct bidi_paragraph_info
{
140 int start_bytepos
; /* byte position where it begins */
141 int end_bytepos
; /* byte position where it ends */
142 int embedding_level
; /* its basic embedding level */
143 bidi_dir_t base_dir
; /* its base direction */
146 /* Data type for describing the bidirectional character categories. */
154 int bidi_ignore_explicit_marks_for_paragraph_level
= 1;
156 bidi_dir_t bidi_overriding_paragraph_direction
= NEUTRAL_DIR
;
158 #define ASCII_BIDI_TYPE_SET(STR, TYPE) \
161 for (p = (STR); *p; p++) \
162 CHAR_TABLE_SET (bidi_type_table, *p, (TYPE)); \
168 /* FIXME: This should come from the Unicode Database. */
173 { { 0x0000, 0x0008, WEAK_BN
},
174 { 0x0009, 0x0000, NEUTRAL_S
},
175 { 0x000A, 0x0000, NEUTRAL_B
},
176 { 0x000B, 0x0000, NEUTRAL_S
},
177 { 0x000C, 0x0000, NEUTRAL_WS
},
178 { 0x000D, 0x0000, NEUTRAL_B
},
179 { 0x000E, 0x001B, WEAK_BN
},
180 { 0x001C, 0x001E, NEUTRAL_B
},
181 { 0x001F, 0x0000, NEUTRAL_S
},
182 { 0x0020, 0x0000, NEUTRAL_WS
},
183 { 0x0021, 0x0022, NEUTRAL_ON
},
184 { 0x0023, 0x0025, WEAK_ET
},
185 { 0x0026, 0x002A, NEUTRAL_ON
},
186 { 0x002B, 0x0000, WEAK_ET
},
187 { 0x002C, 0x0000, WEAK_CS
},
188 { 0x002D, 0x0000, WEAK_ET
},
189 { 0x002E, 0x0000, WEAK_CS
},
190 { 0x002F, 0x0000, WEAK_ES
},
191 { 0x0030, 0x0039, WEAK_EN
},
192 { 0x003A, 0x0000, WEAK_CS
},
193 { 0x003B, 0x0040, NEUTRAL_ON
},
194 { 0x005B, 0x0060, NEUTRAL_ON
},
195 { 0x007B, 0x007E, NEUTRAL_ON
},
196 { 0x007F, 0x0084, WEAK_BN
},
197 { 0x0085, 0x0000, NEUTRAL_B
},
198 { 0x0086, 0x009F, WEAK_BN
},
199 { 0x00A0, 0x0000, WEAK_CS
},
200 { 0x00A1, 0x0000, NEUTRAL_ON
},
201 { 0x00A2, 0x00A5, WEAK_ET
},
202 { 0x00A6, 0x00A9, NEUTRAL_ON
},
203 { 0x00AB, 0x00AF, NEUTRAL_ON
},
204 { 0x00B0, 0x00B1, WEAK_ET
},
205 { 0x00B2, 0x00B3, WEAK_EN
},
206 { 0x00B4, 0x0000, NEUTRAL_ON
},
207 { 0x00B6, 0x00B8, NEUTRAL_ON
},
208 { 0x00B9, 0x0000, WEAK_EN
},
209 { 0x00BB, 0x00BF, NEUTRAL_ON
},
210 { 0x00D7, 0x0000, NEUTRAL_ON
},
211 { 0x00F7, 0x0000, NEUTRAL_ON
},
212 { 0x02B9, 0x02BA, NEUTRAL_ON
},
213 { 0x02C2, 0x02CF, NEUTRAL_ON
},
214 { 0x02D2, 0x02DF, NEUTRAL_ON
},
215 { 0x02E5, 0x02ED, NEUTRAL_ON
},
216 { 0x0300, 0x036F, WEAK_NSM
},
217 { 0x0374, 0x0375, NEUTRAL_ON
},
218 { 0x037E, 0x0385, NEUTRAL_ON
},
219 { 0x0387, 0x0000, NEUTRAL_ON
},
220 { 0x03F6, 0x0000, NEUTRAL_ON
},
221 { 0x0483, 0x0489, WEAK_NSM
},
222 { 0x058A, 0x0000, NEUTRAL_ON
},
223 { 0x0591, 0x05BD, WEAK_NSM
},
224 { 0x05BE, 0x0000, STRONG_R
},
225 { 0x05BF, 0x0000, WEAK_NSM
},
226 { 0x05C0, 0x0000, STRONG_R
},
227 { 0x05C1, 0x05C2, WEAK_NSM
},
228 { 0x05C3, 0x0000, STRONG_R
},
229 { 0x05C4, 0x0000, WEAK_NSM
},
230 { 0x05D0, 0x05F4, STRONG_R
},
231 { 0x060C, 0x0000, WEAK_CS
},
232 { 0x061B, 0x064A, STRONG_AL
},
233 { 0x064B, 0x0655, WEAK_NSM
},
234 { 0x0660, 0x0669, WEAK_AN
},
235 { 0x066A, 0x0000, WEAK_ET
},
236 { 0x066B, 0x066C, WEAK_AN
},
237 { 0x066D, 0x066F, STRONG_AL
},
238 { 0x0670, 0x0000, WEAK_NSM
},
239 { 0x0671, 0x06D5, STRONG_AL
},
240 { 0x06D6, 0x06DC, WEAK_NSM
},
241 { 0x06DD, 0x0000, STRONG_AL
},
242 { 0x06DE, 0x06E4, WEAK_NSM
},
243 { 0x06E5, 0x06E6, STRONG_AL
},
244 { 0x06E7, 0x06E8, WEAK_NSM
},
245 { 0x06E9, 0x0000, NEUTRAL_ON
},
246 { 0x06EA, 0x06ED, WEAK_NSM
},
247 { 0x06F0, 0x06F9, WEAK_EN
},
248 { 0x06FA, 0x070D, STRONG_AL
},
249 { 0x070F, 0x0000, WEAK_BN
},
250 { 0x0710, 0x0000, STRONG_AL
},
251 { 0x0711, 0x0000, WEAK_NSM
},
252 { 0x0712, 0x072C, STRONG_AL
},
253 { 0x0730, 0x074A, WEAK_NSM
},
254 { 0x0780, 0x07A5, STRONG_AL
},
255 { 0x07A6, 0x07B0, WEAK_NSM
},
256 { 0x07B1, 0x0000, STRONG_AL
},
257 { 0x0901, 0x0902, WEAK_NSM
},
258 { 0x093C, 0x0000, WEAK_NSM
},
259 { 0x0941, 0x0948, WEAK_NSM
},
260 { 0x094D, 0x0000, WEAK_NSM
},
261 { 0x0951, 0x0954, WEAK_NSM
},
262 { 0x0962, 0x0963, WEAK_NSM
},
263 { 0x0981, 0x0000, WEAK_NSM
},
264 { 0x09BC, 0x0000, WEAK_NSM
},
265 { 0x09C1, 0x09C4, WEAK_NSM
},
266 { 0x09CD, 0x0000, WEAK_NSM
},
267 { 0x09E2, 0x09E3, WEAK_NSM
},
268 { 0x09F2, 0x09F3, WEAK_ET
},
269 { 0x0A02, 0x0000, WEAK_NSM
},
270 { 0x0A3C, 0x0000, WEAK_NSM
},
271 { 0x0A41, 0x0A4D, WEAK_NSM
},
272 { 0x0A70, 0x0A71, WEAK_NSM
},
273 { 0x0A81, 0x0A82, WEAK_NSM
},
274 { 0x0ABC, 0x0000, WEAK_NSM
},
275 { 0x0AC1, 0x0AC8, WEAK_NSM
},
276 { 0x0ACD, 0x0000, WEAK_NSM
},
277 { 0x0B01, 0x0000, WEAK_NSM
},
278 { 0x0B3C, 0x0000, WEAK_NSM
},
279 { 0x0B3F, 0x0000, WEAK_NSM
},
280 { 0x0B41, 0x0B43, WEAK_NSM
},
281 { 0x0B4D, 0x0B56, WEAK_NSM
},
282 { 0x0B82, 0x0000, WEAK_NSM
},
283 { 0x0BC0, 0x0000, WEAK_NSM
},
284 { 0x0BCD, 0x0000, WEAK_NSM
},
285 { 0x0C3E, 0x0C40, WEAK_NSM
},
286 { 0x0C46, 0x0C56, WEAK_NSM
},
287 { 0x0CBF, 0x0000, WEAK_NSM
},
288 { 0x0CC6, 0x0000, WEAK_NSM
},
289 { 0x0CCC, 0x0CCD, WEAK_NSM
},
290 { 0x0D41, 0x0D43, WEAK_NSM
},
291 { 0x0D4D, 0x0000, WEAK_NSM
},
292 { 0x0DCA, 0x0000, WEAK_NSM
},
293 { 0x0DD2, 0x0DD6, WEAK_NSM
},
294 { 0x0E31, 0x0000, WEAK_NSM
},
295 { 0x0E34, 0x0E3A, WEAK_NSM
},
296 { 0x0E3F, 0x0000, WEAK_ET
},
297 { 0x0E47, 0x0E4E, WEAK_NSM
},
298 { 0x0EB1, 0x0000, WEAK_NSM
},
299 { 0x0EB4, 0x0EBC, WEAK_NSM
},
300 { 0x0EC8, 0x0ECD, WEAK_NSM
},
301 { 0x0F18, 0x0F19, WEAK_NSM
},
302 { 0x0F35, 0x0000, WEAK_NSM
},
303 { 0x0F37, 0x0000, WEAK_NSM
},
304 { 0x0F39, 0x0000, WEAK_NSM
},
305 { 0x0F3A, 0x0F3D, NEUTRAL_ON
},
306 { 0x0F71, 0x0F7E, WEAK_NSM
},
307 { 0x0F80, 0x0F84, WEAK_NSM
},
308 { 0x0F86, 0x0F87, WEAK_NSM
},
309 { 0x0F90, 0x0FBC, WEAK_NSM
},
310 { 0x0FC6, 0x0000, WEAK_NSM
},
311 { 0x102D, 0x1030, WEAK_NSM
},
312 { 0x1032, 0x1037, WEAK_NSM
},
313 { 0x1039, 0x0000, WEAK_NSM
},
314 { 0x1058, 0x1059, WEAK_NSM
},
315 { 0x1680, 0x0000, NEUTRAL_WS
},
316 { 0x169B, 0x169C, NEUTRAL_ON
},
317 { 0x1712, 0x1714, WEAK_NSM
},
318 { 0x1732, 0x1734, WEAK_NSM
},
319 { 0x1752, 0x1753, WEAK_NSM
},
320 { 0x1772, 0x1773, WEAK_NSM
},
321 { 0x17B7, 0x17BD, WEAK_NSM
},
322 { 0x17C6, 0x0000, WEAK_NSM
},
323 { 0x17C9, 0x17D3, WEAK_NSM
},
324 { 0x17DB, 0x0000, WEAK_ET
},
325 { 0x1800, 0x180A, NEUTRAL_ON
},
326 { 0x180B, 0x180D, WEAK_NSM
},
327 { 0x180E, 0x0000, WEAK_BN
},
328 { 0x18A9, 0x0000, WEAK_NSM
},
329 { 0x1FBD, 0x0000, NEUTRAL_ON
},
330 { 0x1FBF, 0x1FC1, NEUTRAL_ON
},
331 { 0x1FCD, 0x1FCF, NEUTRAL_ON
},
332 { 0x1FDD, 0x1FDF, NEUTRAL_ON
},
333 { 0x1FED, 0x1FEF, NEUTRAL_ON
},
334 { 0x1FFD, 0x1FFE, NEUTRAL_ON
},
335 { 0x2000, 0x200A, NEUTRAL_WS
},
336 { 0x200B, 0x200D, WEAK_BN
},
337 { 0x200F, 0x0000, STRONG_R
},
338 { 0x2010, 0x2027, NEUTRAL_ON
},
339 { 0x2028, 0x0000, NEUTRAL_WS
},
340 { 0x2029, 0x0000, NEUTRAL_B
},
341 { 0x202A, 0x0000, LRE
},
342 { 0x202B, 0x0000, RLE
},
343 { 0x202C, 0x0000, PDF
},
344 { 0x202D, 0x0000, LRO
},
345 { 0x202E, 0x0000, RLO
},
346 { 0x202F, 0x0000, NEUTRAL_WS
},
347 { 0x2030, 0x2034, WEAK_ET
},
348 { 0x2035, 0x2057, NEUTRAL_ON
},
349 { 0x205F, 0x0000, NEUTRAL_WS
},
350 { 0x2060, 0x206F, WEAK_BN
},
351 { 0x2070, 0x0000, WEAK_EN
},
352 { 0x2074, 0x2079, WEAK_EN
},
353 { 0x207A, 0x207B, WEAK_ET
},
354 { 0x207C, 0x207E, NEUTRAL_ON
},
355 { 0x2080, 0x2089, WEAK_EN
},
356 { 0x208A, 0x208B, WEAK_ET
},
357 { 0x208C, 0x208E, NEUTRAL_ON
},
358 { 0x20A0, 0x20B1, WEAK_ET
},
359 { 0x20D0, 0x20EA, WEAK_NSM
},
360 { 0x2100, 0x2101, NEUTRAL_ON
},
361 { 0x2103, 0x2106, NEUTRAL_ON
},
362 { 0x2108, 0x2109, NEUTRAL_ON
},
363 { 0x2114, 0x0000, NEUTRAL_ON
},
364 { 0x2116, 0x2118, NEUTRAL_ON
},
365 { 0x211E, 0x2123, NEUTRAL_ON
},
366 { 0x2125, 0x0000, NEUTRAL_ON
},
367 { 0x2127, 0x0000, NEUTRAL_ON
},
368 { 0x2129, 0x0000, NEUTRAL_ON
},
369 { 0x212E, 0x0000, WEAK_ET
},
370 { 0x2132, 0x0000, NEUTRAL_ON
},
371 { 0x213A, 0x0000, NEUTRAL_ON
},
372 { 0x2140, 0x2144, NEUTRAL_ON
},
373 { 0x214A, 0x215F, NEUTRAL_ON
},
374 { 0x2190, 0x2211, NEUTRAL_ON
},
375 { 0x2212, 0x2213, WEAK_ET
},
376 { 0x2214, 0x2335, NEUTRAL_ON
},
377 { 0x237B, 0x2394, NEUTRAL_ON
},
378 { 0x2396, 0x244A, NEUTRAL_ON
},
379 { 0x2460, 0x249B, WEAK_EN
},
380 { 0x24EA, 0x0000, WEAK_EN
},
381 { 0x24EB, 0x2FFB, NEUTRAL_ON
},
382 { 0x3000, 0x0000, NEUTRAL_WS
},
383 { 0x3001, 0x3004, NEUTRAL_ON
},
384 { 0x3008, 0x3020, NEUTRAL_ON
},
385 { 0x302A, 0x302F, WEAK_NSM
},
386 { 0x3030, 0x0000, NEUTRAL_ON
},
387 { 0x3036, 0x3037, NEUTRAL_ON
},
388 { 0x303D, 0x303F, NEUTRAL_ON
},
389 { 0x3099, 0x309A, WEAK_NSM
},
390 { 0x309B, 0x309C, NEUTRAL_ON
},
391 { 0x30A0, 0x0000, NEUTRAL_ON
},
392 { 0x30FB, 0x0000, NEUTRAL_ON
},
393 { 0x3251, 0x325F, NEUTRAL_ON
},
394 { 0x32B1, 0x32BF, NEUTRAL_ON
},
395 { 0xA490, 0xA4C6, NEUTRAL_ON
},
396 { 0xFB1D, 0x0000, STRONG_R
},
397 { 0xFB1E, 0x0000, WEAK_NSM
},
398 { 0xFB1F, 0xFB28, STRONG_R
},
399 { 0xFB29, 0x0000, WEAK_ET
},
400 { 0xFB2A, 0xFB4F, STRONG_R
},
401 { 0xFB50, 0xFD3D, STRONG_AL
},
402 { 0xFD3E, 0xFD3F, NEUTRAL_ON
},
403 { 0xFD50, 0xFDFC, STRONG_AL
},
404 { 0xFE00, 0xFE23, WEAK_NSM
},
405 { 0xFE30, 0xFE4F, NEUTRAL_ON
},
406 { 0xFE50, 0x0000, WEAK_CS
},
407 { 0xFE51, 0x0000, NEUTRAL_ON
},
408 { 0xFE52, 0x0000, WEAK_CS
},
409 { 0xFE54, 0x0000, NEUTRAL_ON
},
410 { 0xFE55, 0x0000, WEAK_CS
},
411 { 0xFE56, 0xFE5E, NEUTRAL_ON
},
412 { 0xFE5F, 0x0000, WEAK_ET
},
413 { 0xFE60, 0xFE61, NEUTRAL_ON
},
414 { 0xFE62, 0xFE63, WEAK_ET
},
415 { 0xFE64, 0xFE68, NEUTRAL_ON
},
416 { 0xFE69, 0xFE6A, WEAK_ET
},
417 { 0xFE6B, 0x0000, NEUTRAL_ON
},
418 { 0xFE70, 0xFEFC, STRONG_AL
},
419 { 0xFEFF, 0x0000, WEAK_BN
},
420 { 0xFF01, 0xFF02, NEUTRAL_ON
},
421 { 0xFF03, 0xFF05, WEAK_ET
},
422 { 0xFF06, 0xFF0A, NEUTRAL_ON
},
423 { 0xFF0B, 0x0000, WEAK_ET
},
424 { 0xFF0C, 0x0000, WEAK_CS
},
425 { 0xFF0D, 0x0000, WEAK_ET
},
426 { 0xFF0E, 0x0000, WEAK_CS
},
427 { 0xFF0F, 0x0000, WEAK_ES
},
428 { 0xFF10, 0xFF19, WEAK_EN
},
429 { 0xFF1A, 0x0000, WEAK_CS
},
430 { 0xFF1B, 0xFF20, NEUTRAL_ON
},
431 { 0xFF3B, 0xFF40, NEUTRAL_ON
},
432 { 0xFF5B, 0xFF65, NEUTRAL_ON
},
433 { 0xFFE0, 0xFFE1, WEAK_ET
},
434 { 0xFFE2, 0xFFE4, NEUTRAL_ON
},
435 { 0xFFE5, 0xFFE6, WEAK_ET
},
436 { 0xFFE8, 0xFFEE, NEUTRAL_ON
},
437 { 0xFFF9, 0xFFFB, WEAK_BN
},
438 { 0xFFFC, 0xFFFD, NEUTRAL_ON
},
439 { 0x1D167, 0x1D169, WEAK_NSM
},
440 { 0x1D173, 0x1D17A, WEAK_BN
},
441 { 0x1D17B, 0x1D182, WEAK_NSM
},
442 { 0x1D185, 0x1D18B, WEAK_NSM
},
443 { 0x1D1AA, 0x1D1AD, WEAK_NSM
},
444 { 0x1D7CE, 0x1D7FF, WEAK_EN
},
445 { 0xE0001, 0xE007F, WEAK_BN
} };
448 bidi_type_table
= Fmake_char_table (Qnil
, make_number (STRONG_L
));
450 for (i
= 0; i
< sizeof bidi_type
/ sizeof bidi_type
[0]; i
++)
451 char_table_set_range (bidi_type_table
, bidi_type
[i
].from
, bidi_type
[i
].to
,
452 make_number (bidi_type
[i
].type
));
453 bidi_initialized
= 1;
457 bidi_is_arabic_number (int ch
)
459 #ifdef TEST_STANDALONE
460 return ch
>= '6' && ch
<= '9';
462 return 0; /* FIXME! */
466 /* Return the bidi type of a character CH. */
468 bidi_get_type (int ch
)
470 return (bidi_type_t
) XINT (CHAR_TABLE_REF (bidi_type_table
, ch
));
473 /* Given a bidi TYPE of a character, return its category. */
475 bidi_get_category (bidi_type_t type
)
489 case PDF
: /* ??? really?? */
508 /* FIXME: exceedingly temporary! Should consult the Unicode database
509 of character properties. */
511 bidi_mirror_char (int c
)
513 static const char mirrored_pairs
[] = "()<>[]{}";
514 const char *p
= strchr (mirrored_pairs
, c
);
518 size_t i
= p
- mirrored_pairs
;
521 return mirrored_pairs
[i
+ 1];
523 return mirrored_pairs
[i
- 1];
528 /* Copy the bidi iterator from FROM to TO. To save cycles, this only
529 copies the part of the level stack that is actually in use. */
531 bidi_copy_it (struct bidi_it
*to
, struct bidi_it
*from
)
535 /* Copy everything except the level stack. */
536 memcpy (to
, from
, ((int)&((struct bidi_it
*)0)->level_stack
[0]));
538 /* Copy the active part of the level stack. */
539 to
->level_stack
[0] = from
->level_stack
[0]; /* level zero is always in use */
540 for (i
= 1; i
<= from
->stack_idx
; i
++)
541 to
->level_stack
[i
] = from
->level_stack
[i
];
544 /* Caching the bidi iterator states. */
546 static struct bidi_it bidi_cache
[1000]; /* FIXME: make this dynamically allocated! */
547 static int bidi_cache_idx
;
548 static int bidi_cache_last_idx
;
551 bidi_cache_reset (void)
554 bidi_cache_last_idx
= -1;
558 bidi_cache_fetch_state (int idx
, struct bidi_it
*bidi_it
)
560 int current_scan_dir
= bidi_it
->scan_dir
;
562 if (idx
< 0 || idx
>= bidi_cache_idx
)
565 bidi_copy_it (bidi_it
, &bidi_cache
[idx
]);
566 bidi_it
->scan_dir
= current_scan_dir
;
567 bidi_cache_last_idx
= idx
;
570 /* Find a cached state with a given CHARPOS and resolved embedding
571 level less or equal to LEVEL. if LEVEL is -1, disregard the
572 resolved levels in cached states. DIR, if non-zero, means search
573 in that direction from the last cache hit. */
575 bidi_cache_search (int charpos
, int level
, int dir
)
581 if (charpos
< bidi_cache
[bidi_cache_last_idx
].charpos
)
583 else if (charpos
> bidi_cache
[bidi_cache_last_idx
].charpos
)
586 i_start
= bidi_cache_last_idx
;
590 i_start
= bidi_cache_idx
- 1;
595 /* Linear search for now; FIXME! */
596 for (i
= i_start
; i
>= 0; i
--)
597 if (bidi_cache
[i
].charpos
== charpos
598 && (level
== -1 || bidi_cache
[i
].resolved_level
<= level
))
603 for (i
= i_start
; i
< bidi_cache_idx
; i
++)
604 if (bidi_cache
[i
].charpos
== charpos
605 && (level
== -1 || bidi_cache
[i
].resolved_level
<= level
))
613 /* Find a cached state where the resolved level changes to a value
614 that is lower than LEVEL, and return its cache slot index. DIR is
615 the direction to search, starting with the last used cache slot.
616 BEFORE, if non-zero, means return the index of the slot that is
617 ``before'' the level change in the search direction. That is,
618 given the cached levels like this:
623 and assuming we are at the position cached at the slot marked with
624 C, searching backwards (DIR = -1) for LEVEL = 2 will return the
625 index of slot B or A, depending whether BEFORE is, respectively,
628 bidi_cache_find_level_change (int level
, int dir
, int before
)
632 int i
= dir
? bidi_cache_last_idx
: bidi_cache_idx
- 1;
633 int incr
= before
? 1 : 0;
644 if (bidi_cache
[i
- incr
].resolved_level
>= 0
645 && bidi_cache
[i
- incr
].resolved_level
< level
)
652 while (i
< bidi_cache_idx
- incr
)
654 if (bidi_cache
[i
+ incr
].resolved_level
>= 0
655 && bidi_cache
[i
+ incr
].resolved_level
< level
)
666 bidi_cache_iterator_state (struct bidi_it
*bidi_it
, int resolved
)
670 /* We should never cache on backward scans. */
671 if (bidi_it
->scan_dir
== -1)
673 idx
= bidi_cache_search (bidi_it
->charpos
, -1, 1);
677 idx
= bidi_cache_idx
;
678 if (idx
> sizeof (bidi_cache
) / sizeof (bidi_cache
[0]) - 1)
680 bidi_copy_it (&bidi_cache
[idx
], bidi_it
);
682 bidi_cache
[idx
].resolved_level
= -1;
686 /* Copy only the members which could have changed, to avoid
687 costly copying of the entire struct. */
688 bidi_cache
[idx
].type
= bidi_it
->type
;
689 bidi_cache
[idx
].orig_type
= bidi_it
->orig_type
;
691 bidi_cache
[idx
].resolved_level
= bidi_it
->resolved_level
;
693 bidi_cache
[idx
].resolved_level
= -1;
694 bidi_cache
[idx
].invalid_levels
= bidi_it
->invalid_levels
;
695 bidi_cache
[idx
].invalid_rl_levels
= bidi_it
->invalid_rl_levels
;
696 bidi_cache
[idx
].next_for_neutral
= bidi_it
->next_for_neutral
;
697 bidi_cache
[idx
].next_for_ws
= bidi_it
->next_for_ws
;
698 bidi_cache
[idx
].ignore_bn_limit
= bidi_it
->ignore_bn_limit
;
701 bidi_cache_last_idx
= idx
;
702 if (idx
>= bidi_cache_idx
)
703 bidi_cache_idx
= idx
+ 1;
706 static inline bidi_type_t
707 bidi_cache_find (int charpos
, int level
, struct bidi_it
*bidi_it
)
709 int i
= bidi_cache_search (charpos
, level
, bidi_it
->scan_dir
);
713 bidi_dir_t current_scan_dir
= bidi_it
->scan_dir
;
715 *bidi_it
= bidi_cache
[i
];
716 bidi_cache_last_idx
= i
;
717 /* Don't let scan direction from from the cached state override
718 the current scan direction. */
719 bidi_it
->scan_dir
= current_scan_dir
;
720 return bidi_it
->type
;
727 bidi_peek_at_next_level (struct bidi_it
*bidi_it
)
729 if (bidi_cache_idx
== 0 || bidi_cache_last_idx
== -1)
731 return bidi_cache
[bidi_cache_last_idx
+ bidi_it
->scan_dir
].resolved_level
;
734 /* Return non-zero if buffer's byte position POS is the last character
735 of a paragraph. THIS_CH is the character preceding the one at POS in
738 bidi_at_paragraph_end (int this_ch
, int pos
)
740 int next_ch
= FETCH_CHAR (pos
);
742 /* FIXME: This should support all Unicode characters that can end a
744 return (this_ch
== '\n' && next_ch
== '\n') || this_ch
== BIDI_EOB
;
747 /* Determine the start-of-run (sor) directional type given the two
748 embedding levels on either side of the run boundary. Also, update
749 the saved info about previously seen characters, since that info is
750 generally valid for a single level run. */
752 bidi_set_sor_type (struct bidi_it
*bidi_it
, int level_before
, int level_after
)
754 int higher_level
= level_before
> level_after
? level_before
: level_after
;
756 /* The prev_was_pdf gork is required for when we have several PDFs
757 in a row. In that case, we want to compute the sor type for the
758 next level run only once: when we see the first PDF. That's
759 because the sor type depends only on the higher of the two levels
760 that we find on the two sides of the level boundary (see UAX#9,
761 clause X10), and so we don't need to know the final embedding
762 level to which we descend after processing all the PDFs. */
763 if (level_before
< level_after
|| !bidi_it
->prev_was_pdf
)
764 /* FIXME: should the default sor direction be user selectable? */
765 bidi_it
->sor
= (higher_level
& 1) != 0 ? R2L
: L2R
;
766 if (level_before
> level_after
)
767 bidi_it
->prev_was_pdf
= 1;
769 bidi_it
->prev
.type
= UNKNOWN_BT
;
770 bidi_it
->last_strong
.type
= bidi_it
->last_strong
.orig_type
=
771 bidi_it
->last_strong
.pristine_type
= UNKNOWN_BT
;
772 bidi_it
->prev_for_neutral
.type
= bidi_it
->sor
== R2L
? STRONG_R
: STRONG_L
;
773 bidi_it
->prev_for_neutral
.charpos
= bidi_it
->charpos
;
774 bidi_it
->prev_for_neutral
.bytepos
= bidi_it
->bytepos
;
775 bidi_it
->next_for_neutral
.type
= bidi_it
->next_for_neutral
.orig_type
=
776 bidi_it
->next_for_neutral
.pristine_type
= UNKNOWN_BT
;
777 bidi_it
->ignore_bn_limit
= 0; /* meaning it's unknown */
781 bidi_paragraph_init (bidi_dir_t dir
, struct bidi_it
*bidi_it
)
783 bidi_it
->level_stack
[0].level
= 0;
785 bidi_it
->level_stack
[0].level
= 1;
786 else if (dir
== NEUTRAL_DIR
) /* P2 */
789 int pos
= bidi_it
->charpos
, bytepos
= bidi_it
->bytepos
;
794 else if (bidi_it
->ch
!= BIDI_EOB
)
797 bytepos
+= bidi_it
->ch_len
;
800 ch
= FETCH_CHAR (bytepos
);
802 bytepos
+= CHAR_BYTES (ch
);
804 /* FIXME: should actually go to where the paragraph begins and
805 start the loop below from there, since UAX#9 says to find the
806 first strong directional character in the paragraph. */
808 for (type
= bidi_get_type (ch
);
809 /* NOTE: UAX#9 says to search only for L, AL, or R types of
810 characters, and ignore RLE, RLO, LRE, and LRO. However,
811 I'm not sure it makes sense to omit those 4; should try
812 with and without that to see the effect. */
813 (bidi_get_category (type
) != STRONG
)
814 || (bidi_ignore_explicit_marks_for_paragraph_level
815 && (type
== RLE
|| type
== RLO
816 || type
== LRE
|| type
== LRO
));
817 type
= bidi_get_type (ch
))
819 if (type
== NEUTRAL_B
|| bidi_at_paragraph_end (ch
, bytepos
))
821 FETCH_CHAR_ADVANCE (ch
, pos
, bytepos
);
823 if (type
== STRONG_R
|| type
== STRONG_AL
) /* P3 */
824 bidi_it
->level_stack
[0].level
= 1;
826 bidi_it
->scan_dir
= 1; /* FIXME: do we need to have control on this? */
827 bidi_it
->resolved_level
= bidi_it
->level_stack
[0].level
;
828 bidi_it
->level_stack
[0].override
= NEUTRAL_DIR
; /* X1 */
829 bidi_it
->invalid_levels
= 0;
830 bidi_it
->invalid_rl_levels
= -1;
831 bidi_it
->new_paragraph
= 0;
832 bidi_it
->next_en_pos
= -1;
833 bidi_it
->next_for_ws
.type
= UNKNOWN_BT
;
834 bidi_set_sor_type (bidi_it
, bidi_it
->level_stack
[0].level
, 0); /* X10 */
839 /* Do whatever UAX#9 clause X8 says should be done at paragraph's end,
840 and set the new paragraph flag in the iterator. */
842 bidi_set_paragraph_end (struct bidi_it
*bidi_it
)
844 bidi_it
->invalid_levels
= 0;
845 bidi_it
->invalid_rl_levels
= -1;
846 bidi_it
->stack_idx
= 0;
847 bidi_it
->resolved_level
= bidi_it
->level_stack
[0].level
;
848 bidi_it
->new_paragraph
= 1;
851 /* Initialize the bidi iterator from buffer position POS for paragraph
852 direction DIR. Return the embedding level at POS. */
854 bidi_init_it (int pos
, bidi_dir_t dir
, struct bidi_it
*bidi_it
)
856 if (! bidi_initialized
)
858 bidi_set_paragraph_end (bidi_it
);
859 bidi_it
->charpos
= pos
;
862 bidi_it
->bytepos
= bidi_it
->charpos
;
863 bidi_it
->ch_len
= 1; /* so that incrementing bytepos works */
867 bidi_it
->bytepos
= CHAR_TO_BYTE (pos
);
869 = MULTIBYTE_FORM_LENGTH (BYTE_POS_ADDR (bidi_it
->bytepos
),
870 MAX_MULTIBYTE_LENGTH
);
872 bidi_it
->ch
= '\x1d'; /* FIXME: should be U+2029 */
873 bidi_it
->type
= NEUTRAL_B
;
874 bidi_it
->orig_type
= UNKNOWN_BT
;
875 bidi_it
->pristine_type
= UNKNOWN_BT
;
876 bidi_it
->prev_was_pdf
= 0;
877 bidi_it
->prev
.type
= bidi_it
->prev
.orig_type
= UNKNOWN_BT
;
878 bidi_it
->last_strong
.type
= bidi_it
->last_strong
.orig_type
=
879 bidi_it
->last_strong
.pristine_type
= UNKNOWN_BT
;
880 bidi_it
->next_for_neutral
.charpos
= -1;
881 bidi_it
->next_for_neutral
.type
=
882 bidi_it
->next_for_neutral
.orig_type
=
883 bidi_it
->next_for_neutral
.pristine_type
= UNKNOWN_BT
;
884 bidi_it
->prev_for_neutral
.charpos
= -1;
885 bidi_it
->prev_for_neutral
.type
=
886 bidi_it
->prev_for_neutral
.orig_type
=
887 bidi_it
->prev_for_neutral
.pristine_type
= UNKNOWN_BT
;
888 bidi_it
->sor
= L2R
; /* FIXME: should it be user-selectable? */
889 bidi_paragraph_init (dir
, bidi_it
);
892 /* Push the current embedding level and override status; reset the
893 current level to LEVEL and the current override status to OVERRIDE. */
895 bidi_push_embedding_level (struct bidi_it
*bidi_it
,
896 int level
, bidi_dir_t override
)
898 bidi_it
->stack_idx
++;
899 if (bidi_it
->stack_idx
>= BIDI_MAXLEVEL
)
901 bidi_it
->level_stack
[bidi_it
->stack_idx
].level
= level
;
902 bidi_it
->level_stack
[bidi_it
->stack_idx
].override
= override
;
905 /* Pop the embedding level and directional override status from the
906 stack, and return the new level. */
908 bidi_pop_embedding_level (struct bidi_it
*bidi_it
)
910 /* UAX#9 says to ignore invalid PDFs. */
911 if (bidi_it
->stack_idx
> 0)
912 bidi_it
->stack_idx
--;
913 return bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
916 /* Record in SAVED_INFO the information about the current character. */
918 bidi_remember_char (struct bidi_saved_info
*saved_info
,
919 struct bidi_it
*bidi_it
)
921 saved_info
->charpos
= bidi_it
->charpos
;
922 saved_info
->bytepos
= bidi_it
->bytepos
;
923 saved_info
->type
= bidi_it
->type
;
924 saved_info
->orig_type
= bidi_it
->orig_type
;
925 saved_info
->pristine_type
= bidi_it
->pristine_type
;
928 /* Resolve the type of a neutral character according to the type of
929 surrounding strong text and the current embedding level. */
930 static inline bidi_type_t
931 bidi_resolve_neutral_1 (bidi_type_t prev_type
, bidi_type_t next_type
, int lev
)
933 /* N1: European and Arabic numbers are treated as though they were R. */
934 if (next_type
== WEAK_EN
|| next_type
== WEAK_AN
)
935 next_type
= STRONG_R
;
936 if (prev_type
== WEAK_EN
|| prev_type
== WEAK_AN
)
937 prev_type
= STRONG_R
;
939 if (next_type
== prev_type
) /* N1 */
941 else if ((lev
& 1) == 0) /* N2 */
948 bidi_explicit_dir_char (int c
)
950 /* FIXME: this should be replaced with a lookup table with suitable
951 bits set, like standard C ctype macros do. */
952 return (c
== LRE_CHAR
|| c
== LRO_CHAR
953 || c
== RLE_CHAR
|| c
== RLO_CHAR
|| c
== PDF_CHAR
);
956 /* A helper function for bidi_resolve_explicit. It advances to the
957 next character in logical order and determines the new embedding
958 level and directional override, but does not take into account
961 bidi_resolve_explicit_1 (struct bidi_it
*bidi_it
)
969 if (bidi_it
->charpos
< 0)
970 bidi_it
->charpos
= bidi_it
->bytepos
= 0;
974 bidi_it
->bytepos
+= bidi_it
->ch_len
;
977 current_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
; /* X1 */
978 override
= bidi_it
->level_stack
[bidi_it
->stack_idx
].override
;
979 new_level
= current_level
;
981 /* in case it is a unibyte character (not yet implemented) */
982 /* _fetch_multibyte_char_len = 1; */
983 curchar
= FETCH_CHAR (bidi_it
->bytepos
);
984 bidi_it
->ch
= curchar
;
985 bidi_it
->ch_len
= CHAR_BYTES (curchar
);
987 type
= bidi_get_type (curchar
);
988 bidi_it
->pristine_type
= type
;
991 bidi_it
->prev_was_pdf
= 0;
993 bidi_it
->orig_type
= UNKNOWN_BT
;
999 bidi_it
->orig_type
= type
;
1000 type
= WEAK_BN
; /* X9/Retaining */
1001 if (bidi_it
->ignore_bn_limit
<= 0)
1003 if (current_level
<= BIDI_MAXLEVEL
- 4)
1005 /* Compute the least odd embedding level greater than
1006 the current level. */
1007 new_level
= ((current_level
+ 1) & ~1) + 1;
1008 if (bidi_it
->orig_type
== RLE
)
1009 override
= NEUTRAL_DIR
;
1012 if (current_level
== BIDI_MAXLEVEL
- 4)
1013 bidi_it
->invalid_rl_levels
= 0;
1014 bidi_push_embedding_level (bidi_it
, new_level
, override
);
1018 bidi_it
->invalid_levels
++;
1019 /* See the commentary about invalid_rl_levels below. */
1020 if (bidi_it
->invalid_rl_levels
< 0)
1021 bidi_it
->invalid_rl_levels
= 0;
1022 bidi_it
->invalid_rl_levels
++;
1025 else if (bidi_it
->prev
.orig_type
== WEAK_EN
/* W5/Retaining */
1026 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1031 bidi_it
->orig_type
= type
;
1032 type
= WEAK_BN
; /* X9/Retaining */
1033 if (bidi_it
->ignore_bn_limit
<= 0)
1035 if (current_level
<= BIDI_MAXLEVEL
- 5)
1037 /* Compute the least even embedding level greater than
1038 the current level. */
1039 new_level
= ((current_level
+ 2) & ~1);
1040 if (bidi_it
->orig_type
== LRE
)
1041 override
= NEUTRAL_DIR
;
1044 bidi_push_embedding_level (bidi_it
, new_level
, override
);
1048 bidi_it
->invalid_levels
++;
1049 /* invalid_rl_levels counts invalid levels encountered
1050 while the embedding level was already too high for
1051 LRE/LRO, but not for RLE/RLO. That is because
1052 there may be exactly one PDF which we should not
1053 ignore even though invalid_levels is non-zero.
1054 invalid_rl_levels helps to know what PDF is
1056 if (bidi_it
->invalid_rl_levels
>= 0)
1057 bidi_it
->invalid_rl_levels
++;
1060 else if (bidi_it
->prev
.orig_type
== WEAK_EN
/* W5/Retaining */
1061 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1065 bidi_it
->orig_type
= type
;
1066 type
= WEAK_BN
; /* X9/Retaining */
1067 if (bidi_it
->ignore_bn_limit
<= 0)
1069 if (!bidi_it
->invalid_rl_levels
)
1071 new_level
= bidi_pop_embedding_level (bidi_it
);
1072 bidi_it
->invalid_rl_levels
= -1;
1073 if (bidi_it
->invalid_levels
)
1074 bidi_it
->invalid_levels
--;
1075 /* else nothing: UAX#9 says to ignore invalid PDFs */
1077 if (!bidi_it
->invalid_levels
)
1078 new_level
= bidi_pop_embedding_level (bidi_it
);
1081 bidi_it
->invalid_levels
--;
1082 bidi_it
->invalid_rl_levels
--;
1085 else if (bidi_it
->prev
.orig_type
== WEAK_EN
/* W5/Retaining */
1086 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1094 bidi_it
->type
= type
;
1099 /* Given an iterator state in BIDI_IT, advance one character position
1100 in the buffer to the next character (in the logical order), resolve
1101 any explicit embeddings and directional overrides, and return the
1102 embedding level of the character after resolving explicit
1103 directives and ignoring empty embeddings. */
1105 bidi_resolve_explicit (struct bidi_it
*bidi_it
)
1107 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1108 int new_level
= bidi_resolve_explicit_1 (bidi_it
);
1110 if (prev_level
< new_level
1111 && bidi_it
->type
== WEAK_BN
1112 && bidi_it
->ignore_bn_limit
== 0 /* only if not already known */
1113 && bidi_explicit_dir_char (FETCH_CHAR (bidi_it
->bytepos
1114 + bidi_it
->ch_len
)))
1116 /* Avoid pushing and popping embedding levels if the level run
1117 is empty, as this breaks level runs where it shouldn't.
1118 UAX#9 removes all the explicit embedding and override codes,
1119 so empty embeddings disappear without a trace. We need to
1120 behave as if we did the same. */
1121 struct bidi_it saved_it
;
1122 int level
= prev_level
;
1124 bidi_copy_it (&saved_it
, bidi_it
);
1126 while (bidi_explicit_dir_char (FETCH_CHAR (bidi_it
->bytepos
1127 + bidi_it
->ch_len
)))
1129 level
= bidi_resolve_explicit_1 (bidi_it
);
1132 if (level
== prev_level
) /* empty embedding */
1133 saved_it
.ignore_bn_limit
= bidi_it
->charpos
+ 1;
1134 else /* this embedding is non-empty */
1135 saved_it
.ignore_bn_limit
= -1;
1137 bidi_copy_it (bidi_it
, &saved_it
);
1138 if (bidi_it
->ignore_bn_limit
> 0)
1140 /* We pushed a level, but we shouldn't have. Undo that. */
1141 if (!bidi_it
->invalid_rl_levels
)
1143 new_level
= bidi_pop_embedding_level (bidi_it
);
1144 bidi_it
->invalid_rl_levels
= -1;
1145 if (bidi_it
->invalid_levels
)
1146 bidi_it
->invalid_levels
--;
1148 if (!bidi_it
->invalid_levels
)
1149 new_level
= bidi_pop_embedding_level (bidi_it
);
1152 bidi_it
->invalid_levels
--;
1153 bidi_it
->invalid_rl_levels
--;
1158 /* For when the paragraph end is defined by anything other than a
1159 special Unicode character (a.k.a. ``higher protocols''). */
1160 if (bidi_it
->type
!= NEUTRAL_B
)
1161 if (bidi_at_paragraph_end (bidi_it
->ch
,
1162 bidi_it
->bytepos
+ bidi_it
->ch_len
))
1163 bidi_it
->type
= NEUTRAL_B
;
1165 if (bidi_it
->type
== NEUTRAL_B
) /* X8 */
1167 bidi_set_paragraph_end (bidi_it
);
1168 bidi_it
->orig_type
= bidi_it
->type
; /* needed below and in L1 */
1174 /* Advance in the buffer, resolve weak types and return the type of
1175 the next character after weak type resolution. */
1177 bidi_resolve_weak (struct bidi_it
*bidi_it
)
1180 bidi_dir_t override
;
1181 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1182 int new_level
= bidi_resolve_explicit (bidi_it
);
1184 bidi_type_t type_of_next
;
1185 struct bidi_it saved_it
;
1187 type
= bidi_it
->type
;
1188 override
= bidi_it
->level_stack
[bidi_it
->stack_idx
].override
;
1190 if (type
== UNKNOWN_BT
1198 if (new_level
!= prev_level
1199 || bidi_it
->type
== NEUTRAL_B
)
1201 /* We've got a new embedding level run, compute the directional
1202 type of sor and initialize per-run variables (UAX#9, clause
1204 bidi_set_sor_type (bidi_it
, prev_level
, new_level
);
1206 else if (type
== NEUTRAL_S
|| type
== NEUTRAL_WS
1207 || type
== WEAK_BN
|| type
== STRONG_AL
)
1208 bidi_it
->orig_type
= type
; /* needed in L1 */
1210 /* Level and directional override status are already recorded in
1211 bidi_it, and do not need any change; see X6. */
1212 if (override
== R2L
) /* X6 */
1214 else if (override
== L2R
)
1216 else if (type
== STRONG_AL
)
1217 type
= STRONG_R
; /* W3 */
1218 else if (type
== WEAK_NSM
) /* W1 */
1220 /* Note that we don't need to consider the case where the prev
1221 character has its type overridden by an RLO or LRO: such
1222 characters are outside the current level run, and thus not
1223 relevant to this NSM. Thus, NSM gets the pristine_type of
1224 the previous character. */
1225 if (bidi_it
->prev
.type
!= UNKNOWN_BT
)
1226 type
= bidi_it
->prev
.pristine_type
;
1227 else if (bidi_it
->sor
== R2L
)
1229 else if (bidi_it
->sor
== L2R
)
1231 else /* shouldn't happen! */
1233 if (type
== WEAK_EN
/* W2 after W1 */
1234 && bidi_it
->last_strong
.orig_type
== STRONG_AL
)
1237 else if (type
== WEAK_EN
/* W2 */
1238 && bidi_it
->last_strong
.orig_type
== STRONG_AL
)
1240 else if ((type
== WEAK_ES
1241 && (bidi_it
->prev
.orig_type
== WEAK_EN
/* W4 */
1242 && (bidi_it
->prev
.pristine_type
== WEAK_EN
1243 || bidi_it
->prev
.pristine_type
== WEAK_NSM
))) /* aft W1 */
1245 && ((bidi_it
->prev
.orig_type
== WEAK_EN
1246 && (bidi_it
->prev
.pristine_type
== WEAK_EN
/* W4 */
1247 || bidi_it
->prev
.pristine_type
== WEAK_NSM
)) /* a/W1 */
1248 || bidi_it
->prev
.orig_type
== WEAK_AN
))) /* W4 */
1250 next_char
= FETCH_CHAR (bidi_it
->bytepos
+ bidi_it
->ch_len
);
1251 type_of_next
= bidi_get_type (next_char
);
1253 if (type_of_next
== WEAK_BN
1254 || bidi_explicit_dir_char (next_char
))
1256 bidi_copy_it (&saved_it
, bidi_it
);
1257 while (bidi_resolve_explicit (bidi_it
) == new_level
1258 && bidi_it
->type
== WEAK_BN
)
1260 type_of_next
= bidi_it
->type
;
1261 bidi_copy_it (bidi_it
, &saved_it
);
1264 /* If the next character is EN, but the last strong-type
1265 character is AL, that next EN will be changed to AN when we
1266 process it in W2 above. So in that case, this ES should not
1267 be changed into EN. */
1269 && type_of_next
== WEAK_EN
1270 && bidi_it
->last_strong
.orig_type
!= STRONG_AL
)
1272 else if (type
== WEAK_CS
)
1274 if (bidi_it
->prev
.orig_type
== WEAK_AN
1275 && (type_of_next
== WEAK_AN
1276 /* If the next character is EN, but the last
1277 strong-type character is AL, EN will be later
1278 changed to AN when we process it in W2 above. So
1279 in that case, this ES should not be changed into
1281 || (type_of_next
== WEAK_EN
1282 && bidi_it
->last_strong
.orig_type
== STRONG_AL
)))
1284 else if (bidi_it
->prev
.orig_type
== WEAK_EN
1285 && type_of_next
== WEAK_EN
1286 && bidi_it
->last_strong
.orig_type
!= STRONG_AL
)
1290 else if (type
== WEAK_ET
/* W5: ET with EN before or after it */
1291 || type
== WEAK_BN
) /* W5/Retaining */
1293 if (bidi_it
->prev
.orig_type
== WEAK_EN
/* ET/BN with EN before it */
1294 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1296 /* W5: ET with EN after it. */
1299 int en_pos
= bidi_it
->charpos
+ 1;
1301 next_char
= FETCH_CHAR (bidi_it
->bytepos
+ bidi_it
->ch_len
);
1302 type_of_next
= bidi_get_type (next_char
);
1304 if (type_of_next
== WEAK_ET
1305 || type_of_next
== WEAK_BN
1306 || bidi_explicit_dir_char (next_char
))
1308 bidi_copy_it (&saved_it
, bidi_it
);
1309 while (bidi_resolve_explicit (bidi_it
) == new_level
1310 && (bidi_it
->type
== WEAK_BN
|| bidi_it
->type
== WEAK_ET
))
1312 type_of_next
= bidi_it
->type
;
1313 en_pos
= bidi_it
->charpos
;
1314 bidi_copy_it (bidi_it
, &saved_it
);
1316 if (type_of_next
== WEAK_EN
)
1318 /* If the last strong character is AL, the EN we've
1319 found will become AN when we get to it (W2). */
1320 if (bidi_it
->last_strong
.orig_type
!= STRONG_AL
)
1323 /* Remember this EN position, to speed up processing
1325 bidi_it
->next_en_pos
= en_pos
;
1327 else if (type
== WEAK_BN
)
1328 type
= NEUTRAL_ON
; /* W6/Retaining */
1333 if (type
== WEAK_ES
|| type
== WEAK_ET
|| type
== WEAK_CS
/* W6 */
1334 || (type
== WEAK_BN
&& (bidi_it
->prev
.orig_type
== WEAK_CS
/* W6/Ret. */
1335 || bidi_it
->prev
.orig_type
== WEAK_ES
1336 || bidi_it
->prev
.orig_type
== WEAK_ET
)))
1339 /* Store the type we've got so far, before we clobber it with strong
1340 types in W7 and while resolving neutral types. But leave alone
1341 the original types that were recorded above, because we will need
1342 them for the L1 clause. */
1343 if (bidi_it
->orig_type
== UNKNOWN_BT
)
1344 bidi_it
->orig_type
= type
;
1346 if (type
== WEAK_EN
) /* W7 */
1348 if ((bidi_it
->last_strong
.orig_type
== STRONG_L
)
1349 || (bidi_it
->last_strong
.type
== UNKNOWN_BT
&& bidi_it
->sor
== L2R
))
1353 bidi_it
->type
= type
;
1358 bidi_resolve_neutral (struct bidi_it
*bidi_it
)
1360 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1361 bidi_type_t type
= bidi_resolve_weak (bidi_it
);
1362 int current_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1364 if (!(type
== STRONG_R
1369 || type
== NEUTRAL_B
1370 || type
== NEUTRAL_S
1371 || type
== NEUTRAL_WS
1372 || type
== NEUTRAL_ON
))
1375 if (bidi_get_category (type
) == NEUTRAL
1376 || (type
== WEAK_BN
&& prev_level
== current_level
))
1378 if (bidi_it
->next_for_neutral
.type
!= UNKNOWN_BT
)
1379 type
= bidi_resolve_neutral_1 (bidi_it
->prev_for_neutral
.type
,
1380 bidi_it
->next_for_neutral
.type
,
1384 /* Arrrgh!! The UAX#9 algorithm is too deeply entrenched in
1385 the assumption of batch-style processing; see clauses W4,
1386 W5, and especially N1, which require to look far forward
1387 (as well as back) in the buffer. May the fleas of a
1388 thousand camels infest the armpits of those who design
1389 supposedly general-purpose algorithms by looking at their
1390 own implementations, and fail to consider other possible
1392 struct bidi_it saved_it
;
1393 bidi_type_t next_type
;
1395 if (bidi_it
->scan_dir
== -1)
1398 bidi_copy_it (&saved_it
, bidi_it
);
1399 /* Scan the text forward until we find the first non-neutral
1400 character, and then use that to resolve the neutral we
1401 are dealing with now. We also cache the scanned iterator
1402 states, to salvage some of the effort later. */
1403 bidi_cache_iterator_state (bidi_it
, 0);
1405 /* Record the info about the previous character, so that
1406 it will be cached below with this state. */
1407 if (bidi_it
->orig_type
!= WEAK_BN
/* W1/Retaining */
1408 && bidi_it
->type
!= WEAK_BN
)
1409 bidi_remember_char (&bidi_it
->prev
, bidi_it
);
1410 type
= bidi_resolve_weak (bidi_it
);
1411 /* Paragraph separators have their levels fully resolved
1412 at this point, so cache them as resolved. */
1413 bidi_cache_iterator_state (bidi_it
, type
== NEUTRAL_B
);
1414 /* FIXME: implement L1 here, by testing for a newline and
1415 resetting the level for any sequence of whitespace
1416 characters adjacent to it. */
1417 } while (!(type
== NEUTRAL_B
1419 && bidi_get_category (type
) != NEUTRAL
)
1420 /* This is all per level run, so stop when we
1421 reach the end of this level run. */
1422 || bidi_it
->level_stack
[bidi_it
->stack_idx
].level
!=
1425 bidi_remember_char (&saved_it
.next_for_neutral
, bidi_it
);
1436 /* N1: ``European and Arabic numbers are treated as
1437 though they were R.'' */
1438 next_type
= STRONG_R
;
1439 saved_it
.next_for_neutral
.type
= STRONG_R
;
1442 if (!bidi_explicit_dir_char (bidi_it
->ch
))
1443 abort (); /* can't happen: BNs are skipped */
1446 /* Marched all the way to the end of this level run.
1447 We need to use the eor type, whose information is
1448 stored by bidi_set_sor_type in the prev_for_neutral
1450 if (saved_it
.type
!= WEAK_BN
1451 || bidi_get_category (bidi_it
->prev
.orig_type
) == NEUTRAL
)
1453 next_type
= bidi_it
->prev_for_neutral
.type
;
1454 saved_it
.next_for_neutral
.type
= next_type
;
1458 /* This is a BN which does not adjoin neutrals.
1459 Leave its type alone. */
1460 bidi_copy_it (bidi_it
, &saved_it
);
1461 return bidi_it
->type
;
1467 type
= bidi_resolve_neutral_1 (saved_it
.prev_for_neutral
.type
,
1468 next_type
, current_level
);
1469 saved_it
.type
= type
;
1470 bidi_copy_it (bidi_it
, &saved_it
);
1476 /* Given an iterator state in BIDI_IT, advance one character position
1477 in the buffer to the next character (in the logical order), resolve
1478 the bidi type of that next character, and return that type. */
1480 bidi_type_of_next_char (struct bidi_it
*bidi_it
)
1484 /* This should always be called during a forward scan. */
1485 if (bidi_it
->scan_dir
!= 1)
1488 /* Reset the limit until which to ignore BNs if we step out of the
1489 area where we found only empty levels. */
1490 if ((bidi_it
->ignore_bn_limit
> 0
1491 && bidi_it
->ignore_bn_limit
<= bidi_it
->charpos
)
1492 || (bidi_it
->ignore_bn_limit
== -1
1493 && !bidi_explicit_dir_char (bidi_it
->ch
)))
1494 bidi_it
->ignore_bn_limit
= 0;
1496 type
= bidi_resolve_neutral (bidi_it
);
1501 /* Given an iterator state BIDI_IT, advance one character position in
1502 the buffer to the next character (in the logical order), resolve
1503 the embedding and implicit levels of that next character, and
1504 return the resulting level. */
1506 bidi_level_of_next_char (struct bidi_it
*bidi_it
)
1509 int level
, prev_level
= -1;
1510 struct bidi_saved_info next_for_neutral
;
1512 if (bidi_it
->scan_dir
== 1)
1514 /* There's no sense in trying to advance if we hit end of text. */
1515 if (bidi_it
->ch
== BIDI_EOB
)
1516 return bidi_it
->resolved_level
;
1518 /* Record the info about the previous character. */
1519 if (bidi_it
->orig_type
!= WEAK_BN
/* W1/Retaining */
1520 && bidi_it
->type
!= WEAK_BN
)
1521 bidi_remember_char (&bidi_it
->prev
, bidi_it
);
1522 if (bidi_it
->orig_type
== STRONG_R
|| bidi_it
->orig_type
== STRONG_L
1523 || bidi_it
->orig_type
== STRONG_AL
)
1524 bidi_remember_char (&bidi_it
->last_strong
, bidi_it
);
1525 /* FIXME: it sounds like we don't need both prev and
1526 prev_for_neutral members, but I'm leaving them both for now. */
1527 if (bidi_it
->type
== STRONG_R
|| bidi_it
->type
== STRONG_L
1528 || bidi_it
->type
== WEAK_EN
|| bidi_it
->type
== WEAK_AN
)
1529 bidi_remember_char (&bidi_it
->prev_for_neutral
, bidi_it
);
1531 /* If we overstepped the characters used for resolving neutrals
1532 and whitespace, invalidate their info in the iterator. */
1533 if (bidi_it
->charpos
>= bidi_it
->next_for_neutral
.charpos
)
1534 bidi_it
->next_for_neutral
.type
= UNKNOWN_BT
;
1535 if (bidi_it
->next_en_pos
>= 0
1536 && bidi_it
->charpos
>= bidi_it
->next_en_pos
)
1537 bidi_it
->next_en_pos
= -1;
1538 if (bidi_it
->next_for_ws
.type
!= UNKNOWN_BT
1539 && bidi_it
->charpos
>= bidi_it
->next_for_ws
.charpos
)
1540 bidi_it
->next_for_ws
.type
= UNKNOWN_BT
;
1542 /* This must be taken before we fill the iterator with the info
1543 about the next char. If we scan backwards, the iterator
1544 state must be already cached, so there's no need to know the
1545 embedding level of the previous character, since we will be
1546 returning to our caller shortly. */
1547 prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1549 next_for_neutral
= bidi_it
->next_for_neutral
;
1551 /* Perhaps it is already cached. */
1552 type
= bidi_cache_find (bidi_it
->charpos
+ bidi_it
->scan_dir
, -1, bidi_it
);
1553 if (type
!= UNKNOWN_BT
)
1555 /* Don't lose the information for resolving neutrals! The
1556 cached states could have been cached before their
1557 next_for_neutral member was computed. If we are on our way
1558 forward, we can simply take the info from the previous
1560 if (bidi_it
->scan_dir
== 1
1561 && bidi_it
->next_for_neutral
.type
== UNKNOWN_BT
)
1562 bidi_it
->next_for_neutral
= next_for_neutral
;
1564 /* If resolved_level is -1, it means this state was cached
1565 before it was completely resolved, so we cannot return
1567 if (bidi_it
->resolved_level
!= -1)
1568 return bidi_it
->resolved_level
;
1570 if (bidi_it
->scan_dir
== -1)
1571 /* If we are going backwards, the iterator state is already cached
1572 from previous scans, and should be fully resolved. */
1575 if (type
== UNKNOWN_BT
)
1576 type
= bidi_type_of_next_char (bidi_it
);
1578 if (type
== NEUTRAL_B
)
1579 return bidi_it
->resolved_level
;
1581 level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1582 if ((bidi_get_category (type
) == NEUTRAL
/* && type != NEUTRAL_B */)
1583 || (type
== WEAK_BN
&& prev_level
== level
))
1585 if (bidi_it
->next_for_neutral
.type
== UNKNOWN_BT
)
1588 /* If the cached state shows a neutral character, it was not
1589 resolved by bidi_resolve_neutral, so do it now. */
1590 type
= bidi_resolve_neutral_1 (bidi_it
->prev_for_neutral
.type
,
1591 bidi_it
->next_for_neutral
.type
,
1595 if (!(type
== STRONG_R
1599 || type
== WEAK_AN
))
1601 bidi_it
->type
= type
;
1603 /* For L1 below, we need to know, for each WS character, whether
1604 it belongs to a sequence of WS characters preceeding a newline
1605 or a TAB or a paragraph separator. */
1606 if (bidi_it
->pristine_type
== NEUTRAL_WS
1607 && bidi_it
->next_for_ws
.type
== UNKNOWN_BT
)
1610 int clen
= bidi_it
->ch_len
;
1611 int bpos
= bidi_it
->bytepos
;
1612 int cpos
= bidi_it
->charpos
;
1616 /*_fetch_multibyte_char_len = 1;*/
1617 ch
= FETCH_CHAR (bpos
+ clen
);
1620 clen
= CHAR_BYTES (ch
);
1621 if (ch
== '\n' /* || ch == LINESEP_CHAR */)
1624 chtype
= bidi_get_type (ch
);
1625 } while (chtype
== NEUTRAL_WS
|| chtype
== WEAK_BN
1626 || bidi_explicit_dir_char (ch
)); /* L1/Retaining */
1627 bidi_it
->next_for_ws
.type
= chtype
;
1628 bidi_it
->next_for_ws
.charpos
= cpos
;
1629 bidi_it
->next_for_ws
.bytepos
= bpos
;
1632 /* Resolve implicit levels, with a twist: PDFs get the embedding
1633 level of the enbedding they terminate. See below for the
1635 if (bidi_it
->pristine_type
== PDF
1636 /* Don't do this if this formatting code didn't change the
1637 embedding level due to invalid or empty embeddings. */
1638 && prev_level
!= level
)
1640 /* Don't look in UAX#9 for the reason for this: it's our own
1641 private quirk. The reason is that we want the formatting
1642 codes to be delivered so that they bracket the text of their
1643 embedding. For example, given the text
1647 we want it to be displayed as
1655 which will result because we bump up the embedding level as
1656 soon as we see the RLO and pop it as soon as we see the PDF,
1657 so RLO itself has the same embedding level as "teST", and
1658 thus would be normally delivered last, just before the PDF.
1659 The switch below fiddles with the level of PDF so that this
1660 ugly side effect does not happen.
1662 (This is, of course, only important if the formatting codes
1663 are actually displayed, but Emacs does display them if the
1667 else if (bidi_it
->pristine_type
== NEUTRAL_B
/* L1 */
1668 || bidi_it
->pristine_type
== NEUTRAL_S
1669 || bidi_it
->ch
== '\n' /* || bidi_it->ch == LINESEP_CHAR */
1670 || (bidi_it
->pristine_type
== NEUTRAL_WS
1671 && (bidi_it
->next_for_ws
.type
== NEUTRAL_B
1672 || bidi_it
->next_for_ws
.type
== NEUTRAL_S
)))
1673 level
= bidi_it
->level_stack
[0].level
;
1674 else if ((level
& 1) == 0) /* I1 */
1676 if (type
== STRONG_R
)
1678 else if (type
== WEAK_EN
|| type
== WEAK_AN
)
1683 if (type
== STRONG_L
|| type
== WEAK_EN
|| type
== WEAK_AN
)
1687 bidi_it
->resolved_level
= level
;
1691 /* Move to the other edge of a level given by LEVEL. If END_FLAG is
1692 non-zero, we are at the end of a level, and we need to prepare to
1693 resume the scan of the lower level.
1695 If this level's other edge is cached, we simply jump to it, filling
1696 the iterator structure with the iterator state on the other edge.
1697 Otherwise, we walk the buffer until we come back to the same level
1700 Note: we are not talking here about a ``level run'' in the UAX#9
1701 sense of the term, but rather about a ``level'' which includes
1702 all the levels higher than it. In other words, given the levels
1705 11111112222222333333334443343222222111111112223322111
1708 and assuming we are at point A scanning left to right, this
1709 function moves to point C, whereas the UAX#9 ``level 2 run'' ends
1712 bidi_find_other_level_edge (struct bidi_it
*bidi_it
, int level
, int end_flag
)
1714 int dir
= end_flag
? -bidi_it
->scan_dir
: bidi_it
->scan_dir
;
1717 /* Try the cache first. */
1718 if ((idx
= bidi_cache_find_level_change (level
, dir
, end_flag
)) >= 0)
1719 bidi_cache_fetch_state (idx
, bidi_it
);
1725 abort (); /* if we are at end of level, its edges must be cached */
1727 bidi_cache_iterator_state (bidi_it
, 1);
1729 new_level
= bidi_level_of_next_char (bidi_it
);
1730 bidi_cache_iterator_state (bidi_it
, 1);
1731 } while (new_level
>= level
);
1736 bidi_get_next_char_visually (struct bidi_it
*bidi_it
)
1738 int old_level
, new_level
, next_level
;
1739 struct bidi_it prev_bidi_it
;
1741 if (bidi_it
->scan_dir
== 0)
1743 bidi_it
->scan_dir
= 1; /* default to logical order */
1746 if (bidi_it
->new_paragraph
)
1747 bidi_paragraph_init (bidi_overriding_paragraph_direction
, bidi_it
);
1748 if (bidi_cache_idx
== 0)
1749 bidi_copy_it (&prev_bidi_it
, bidi_it
);
1751 old_level
= bidi_it
->resolved_level
;
1752 new_level
= bidi_level_of_next_char (bidi_it
);
1753 if (bidi_it
->ch
== BIDI_EOB
)
1756 /* Reordering of resolved levels (clause L2) is implemented by
1757 jumping to the other edge of the level and flipping direction of
1758 scanning the buffer whenever we find a level change. */
1759 if (new_level
!= old_level
)
1761 int ascending
= new_level
> old_level
;
1762 int level_to_search
= ascending
? old_level
+ 1 : old_level
;
1763 int incr
= ascending
? 1 : -1;
1764 int expected_next_level
= old_level
+ incr
;
1766 /* If we don't have anything cached yet, we need to cache the
1767 previous character we've seen, since we'll need it to record
1768 where to jump when the last non-base level is exhausted. */
1769 if (bidi_cache_idx
== 0)
1770 bidi_cache_iterator_state (&prev_bidi_it
, 1);
1771 /* Jump (or walk) to the other edge of this level. */
1772 bidi_find_other_level_edge (bidi_it
, level_to_search
, !ascending
);
1773 /* Switch scan direction and peek at the next character in the
1775 bidi_it
->scan_dir
= -bidi_it
->scan_dir
;
1777 /* The following loop handles the case where the resolved level
1778 jumps by more than one. This is typical for numbers inside a
1779 run of text with left-to-right embedding direction, but can
1780 also happen in other situations. In those cases the decision
1781 where to continue after a level change, and in what direction,
1782 is tricky. For example, given a text like below:
1787 (where the numbers below the text show the resolved levels),
1788 the result of reordering according to UAX#9 should be this:
1792 This is implemented by the loop below which flips direction
1793 and jumps to the other edge of the level each time it finds
1794 the new level not to be the expected one. The expected level
1795 is always one more or one less than the previous one. */
1796 next_level
= bidi_peek_at_next_level (bidi_it
);
1797 while (next_level
!= expected_next_level
)
1799 expected_next_level
+= incr
;
1800 level_to_search
+= incr
;
1801 bidi_find_other_level_edge (bidi_it
, level_to_search
, !ascending
);
1802 bidi_it
->scan_dir
= -bidi_it
->scan_dir
;
1803 next_level
= bidi_peek_at_next_level (bidi_it
);
1806 /* Finally, deliver the next character in the new direction. */
1807 next_level
= bidi_level_of_next_char (bidi_it
);
1810 if (bidi_it
->scan_dir
== 1 && bidi_cache_idx
)
1812 /* If we are at paragraph's base embedding level and beyond the
1813 last cached position, the cache's job is done and we can
1815 if (bidi_it
->resolved_level
== bidi_it
->level_stack
[0].level
1816 && bidi_it
->charpos
> bidi_cache
[bidi_cache_idx
- 1].charpos
)
1817 bidi_cache_reset ();
1818 /* But as long as we are caching during forward scan, we must
1819 cache each state, or else the cache integrity will be
1820 compromised: it assumes cached states correspond to buffer
1823 bidi_cache_iterator_state (bidi_it
, 1);
1827 /* This is meant to be called from within the debugger, whenever you
1828 wish to examine the cache contents. */
1830 bidi_dump_cached_states (void)
1835 if (bidi_cache_idx
== 0)
1837 fprintf (stderr
, "The cache is empty.\n");
1840 fprintf (stderr
, "Total of %d state%s in cache:\n",
1841 bidi_cache_idx
, bidi_cache_idx
== 1 ? "" : "s");
1843 for (i
= bidi_cache
[bidi_cache_idx
- 1].charpos
; i
> 0; i
/= 10)
1845 fputs ("ch ", stderr
);
1846 for (i
= 0; i
< bidi_cache_idx
; i
++)
1847 fprintf (stderr
, "%*c", ndigits
, bidi_cache
[i
].ch
);
1848 fputs ("\n", stderr
);
1849 fputs ("lvl ", stderr
);
1850 for (i
= 0; i
< bidi_cache_idx
; i
++)
1851 fprintf (stderr
, "%*d", ndigits
, bidi_cache
[i
].resolved_level
);
1852 fputs ("\n", stderr
);
1853 fputs ("pos ", stderr
);
1854 for (i
= 0; i
< bidi_cache_idx
; i
++)
1855 fprintf (stderr
, "%*d", ndigits
, bidi_cache
[i
].charpos
);
1856 fputs ("\n", stderr
);
1859 #ifdef TEST_STANDALONE
1861 #include <sys/stat.h>
1864 static char display_line
[80];
1865 static int simulate_display
;
1866 static int incr
= 1;
1869 signal_catcher (int sig
)
1871 if (simulate_display
)
1872 puts (display_line
);
1878 signal (sig
, SIG_DFL
);
1885 if (simulate_display
)
1889 if (p
>= display_line
)
1890 memset (display_line
, ' ', p
- display_line
+ 1);
1894 fputs (display_line
, stdout
);
1900 init_display_direction (bidi_dir_t default_dir
, int base_level
)
1904 /* To which display margin should we flush the lines? */
1905 switch (default_dir
)
1908 if ((base_level
& 1) == 0)
1915 p
= display_line
+ sizeof (display_line
) - 1;
1925 p
= display_line
+ sizeof (display_line
) - 1;
1937 continuation_line (char *p
, int need
)
1941 if (p
< display_line
+ need
)
1945 putc ('\n', stdout
);
1946 memset (display_line
, '>', sizeof(display_line
) - 1);
1947 p
= display_line
+ sizeof (display_line
) - 1;
1953 if (p
> display_line
+ sizeof(display_line
) - need
- 2)
1957 putc ('\n', stdout
);
1958 memset (display_line
, '<', sizeof(display_line
) - 1);
1966 int main (int argc
, char *argv
[])
1968 bidi_dir_t default_dir
= NEUTRAL_DIR
;
1969 char lots_of_equals
[] = "\n===============================================================================\n";
1972 if (argc
> 1 && argv
[1][0] == '-')
1978 simulate_display
= 1;
1983 simulate_display
= 1;
1987 simulate_display
= 1;
1993 bidi_overriding_paragraph_direction
= default_dir
;
1996 for (argv
++; *argv
; argv
++)
1998 FILE *in
= fopen (*argv
, "rb");
1999 struct stat stat_buf
;
2000 struct bidi_it iterator
;
2002 char *p
= display_line
;
2004 unsigned char *s
, *d
, *s_end
;
2006 if (!in
|| stat (*argv
, &stat_buf
))
2012 if (stat_buf
.st_size
> input_buf_size
)
2014 input_buf
= realloc (input_buf
, stat_buf
.st_size
+ 1);
2017 perror ("realloc input buffer");
2020 input_buf_size
= stat_buf
.st_size
;
2022 if (fread (input_buf
, 1, stat_buf
.st_size
, in
) != stat_buf
.st_size
)
2024 perror ("reading input");
2027 input_buf
[stat_buf
.st_size
] = '\0';
2028 for (d
= s
= input_buf
, s_end
= s
+ stat_buf
.st_size
- 1; *s
; s
++)
2030 if (*s
!= '\r' || s
>= s_end
|| s
[1] != '\n')
2033 stat_buf
.st_size
= d
- input_buf
;
2034 input_buf
[stat_buf
.st_size
] = '\0';
2036 /* Done with administrivia, now for some real work... */
2037 signal (SIGABRT
, signal_catcher
);
2038 signal (SIGINT
, signal_catcher
);
2039 bidi_init_it (-1, default_dir
, &iterator
);
2040 if (simulate_display
)
2042 p
= init_display_direction (default_dir
,
2043 iterator
.level_stack
[0].level
);
2046 memset (display_line
, incr
== -1 ? '>' : '<', sizeof (display_line
) - 1);
2047 display_line
[sizeof (display_line
) - 1] = '\0';
2048 base_level
= iterator
.level_stack
[0].level
;
2050 for (i
= 0; i
<= stat_buf
.st_size
; i
++)
2054 bidi_get_next_char_visually (&iterator
);
2057 if (c
== '\n' || c
== BIDI_EOB
)
2059 if (simulate_display
)
2062 /* FIXME: if -R or -L, need to init paragraph here. */
2068 else if (c
>= LRE_CHAR
&& c
<= LRM_CHAR
)
2070 if (simulate_display
)
2072 p
= continuation_line (p
, 5);
2075 memcpy (p
- 4, bidi_name
[c
], 5);
2080 memcpy (p
, bidi_name
[c
], 5);
2085 fputs (bidi_name
[c
], stdout
);
2089 if (simulate_display
)
2091 p
= continuation_line (p
, 2);
2104 printf ("^%c", (c
| 0x40));
2108 int c1
= (iterator
.type
== STRONG_R
) ? bidi_mirror_char (c
) : c
;
2110 if (simulate_display
)
2112 p
= continuation_line (p
, 1);
2120 if (iterator
.ch
== '\n')
2122 if (base_level
!= iterator
.level_stack
[0].level
)
2123 base_level
= iterator
.level_stack
[0].level
;
2124 p
= init_display_direction (default_dir
, base_level
);
2125 memset (display_line
, incr
== -1 ? '>' : '<',
2126 sizeof (display_line
) - 1);
2129 fputs (lots_of_equals
, stdout
);