diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ChangeLog.bidi | 64 | ||||
| -rw-r--r-- | src/Makefile.in | 2 | ||||
| -rw-r--r-- | src/bidi.c | 2134 | ||||
| -rw-r--r-- | src/buffer.c | 33 | ||||
| -rw-r--r-- | src/buffer.h | 6 | ||||
| -rw-r--r-- | src/dispextern.h | 85 | ||||
| -rw-r--r-- | src/dispnew.c | 8 | ||||
| -rw-r--r-- | src/xdisp.c | 27 |
8 files changed, 2343 insertions, 16 deletions
diff --git a/src/ChangeLog.bidi b/src/ChangeLog.bidi new file mode 100644 index 00000000000..415e6a5c468 --- /dev/null +++ b/src/ChangeLog.bidi | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | 2005-12-03 Eli Zaretskii <eliz@gnu.org> | ||
| 2 | |||
| 3 | * bidi.c: Include stdio.h unconditionally. Fix and elaborate | ||
| 4 | commentary. Add Copyright blurb. | ||
| 5 | |||
| 6 | 2004-03-08 Kenichi Handa <handa@m17n.org> | ||
| 7 | |||
| 8 | * xdisp.c (reseat_1): Call bidi_init_it with a previous position. | ||
| 9 | |||
| 10 | * bidi.c (bidi_init_it): Set bidi_it->ch_len even if POS > 0. | ||
| 11 | |||
| 12 | 2004-03-04 Kenichi Handa <handa@m17n.org> | ||
| 13 | |||
| 14 | The following changes are to support bidirectional text display. | ||
| 15 | |||
| 16 | * Makefile.in (obj): Include bidi.o. | ||
| 17 | (bidi.o): New target. | ||
| 18 | |||
| 19 | * bidi.c: New file. | ||
| 20 | |||
| 21 | * buffer.h (struct buffer): New member bidi_display_reordering. | ||
| 22 | |||
| 23 | * buffer.c (init_buffer_once): Initialize bidi_display_reordering. | ||
| 24 | (syms_of_buffer): Declarations of Lisp variables | ||
| 25 | default-bidi-display-reordering and bidi-display-reordering. | ||
| 26 | |||
| 27 | * dispextern.h (BIDI_MAXLEVEL): New macro. | ||
| 28 | (bidi_type_t, bidi_dir_t): New types. | ||
| 29 | (bidi_saved_info, bidi_stack, bidi_it): New structs. | ||
| 30 | (struct it): New members bidi_p and bidi_it. | ||
| 31 | (bidi_init_it): Extern it. | ||
| 32 | (bidi_get_next_char_visually): Extern it. | ||
| 33 | |||
| 34 | * dispnew.c (direct_output_forward_char): Give up if we need bidi | ||
| 35 | processing or buffer's direction is right-to-left. | ||
| 36 | |||
| 37 | * xdisp.c (init_iterator): Initialize it->bidi_p. | ||
| 38 | (reseat_1): Cal bidi_init_it and bidi_get_next_char_visually if | ||
| 39 | necessary. | ||
| 40 | (set_iterator_to_next): Cal bidi_get_next_char_visually if | ||
| 41 | necessary. | ||
| 42 | |||
| 43 | |||
| 44 | ;; Local Variables: | ||
| 45 | ;; coding: utf-8 | ||
| 46 | ;; add-log-time-zone-rule: t | ||
| 47 | ;; End: | ||
| 48 | |||
| 49 | Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. | ||
| 50 | |||
| 51 | This file is part of GNU Emacs. | ||
| 52 | |||
| 53 | GNU Emacs is free software: you can redistribute it and/or modify | ||
| 54 | it under the terms of the GNU General Public License as published by | ||
| 55 | the Free Software Foundation, either version 3 of the License, or | ||
| 56 | (at your option) any later version. | ||
| 57 | |||
| 58 | GNU Emacs is distributed in the hope that it will be useful, | ||
| 59 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 60 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 61 | GNU General Public License for more details. | ||
| 62 | |||
| 63 | You should have received a copy of the GNU General Public License | ||
| 64 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
diff --git a/src/Makefile.in b/src/Makefile.in index 1e956ae826b..f5b7133a7ae 100644 --- a/src/Makefile.in +++ b/src/Makefile.in | |||
| @@ -560,6 +560,7 @@ FONT_DRIVERS = xfont.o | |||
| 560 | /* lastfile must follow all files | 560 | /* lastfile must follow all files |
| 561 | whose initialized data areas should be dumped as pure by dump-emacs. */ | 561 | whose initialized data areas should be dumped as pure by dump-emacs. */ |
| 562 | obj= dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \ | 562 | obj= dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \ |
| 563 | bidi.o \ | ||
| 563 | charset.o coding.o category.o ccl.o character.o chartab.o \ | 564 | charset.o coding.o category.o ccl.o character.o chartab.o \ |
| 564 | cm.o term.o terminal.o xfaces.o $(XOBJ) $(GTK_OBJ) $(DBUS_OBJ) \ | 565 | cm.o term.o terminal.o xfaces.o $(XOBJ) $(GTK_OBJ) $(DBUS_OBJ) \ |
| 565 | emacs.o keyboard.o macros.o keymap.o sysdep.o \ | 566 | emacs.o keyboard.o macros.o keymap.o sysdep.o \ |
| @@ -1052,6 +1053,7 @@ doc.o: buildobj.h | |||
| 1052 | 1053 | ||
| 1053 | atimer.o: atimer.c atimer.h syssignal.h systime.h lisp.h blockinput.h \ | 1054 | atimer.o: atimer.c atimer.h syssignal.h systime.h lisp.h blockinput.h \ |
| 1054 | $(config_h) | 1055 | $(config_h) |
| 1056 | bidi.o: bidi.c buffer.h character.h | ||
| 1055 | buffer.o: buffer.c buffer.h region-cache.h commands.h window.h \ | 1057 | buffer.o: buffer.c buffer.h region-cache.h commands.h window.h \ |
| 1056 | $(INTERVALS_H) blockinput.h atimer.h systime.h character.h \ | 1058 | $(INTERVALS_H) blockinput.h atimer.h systime.h character.h \ |
| 1057 | indent.h keyboard.h coding.h keymap.h frame.h lisp.h $(config_h) | 1059 | indent.h keyboard.h coding.h keymap.h frame.h lisp.h $(config_h) |
diff --git a/src/bidi.c b/src/bidi.c new file mode 100644 index 00000000000..2a3a9706767 --- /dev/null +++ b/src/bidi.c | |||
| @@ -0,0 +1,2134 @@ | |||
| 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 | |||
| 22 | /* A sequential implementation of the Unicode Bidirectional algorithm, | ||
| 23 | as per UAX#9, a part of the Unicode Standard. | ||
| 24 | |||
| 25 | Unlike the reference and most other implementations, this one is | ||
| 26 | designed to be called once for every character in the buffer. | ||
| 27 | |||
| 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. | ||
| 36 | |||
| 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". */ | ||
| 41 | |||
| 42 | #ifdef HAVE_CONFIG_H | ||
| 43 | #include <config.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #include <stdio.h> | ||
| 47 | |||
| 48 | #ifdef HAVE_STRING_H | ||
| 49 | #include <string.h> | ||
| 50 | #endif | ||
| 51 | |||
| 52 | #include "lisp.h" | ||
| 53 | #include "buffer.h" | ||
| 54 | #include "character.h" | ||
| 55 | #include "dispextern.h" | ||
| 56 | |||
| 57 | static int bidi_initialized = 0; | ||
| 58 | |||
| 59 | static Lisp_Object bidi_type_table; | ||
| 60 | |||
| 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 | ||
| 68 | |||
| 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 | ||
| 73 | |||
| 74 | /* FIXME: need to define wrappers for FETCH_CHAR etc. that return | ||
| 75 | BIDI_EOB when they hit ZV. */ | ||
| 76 | #define BIDI_EOB -1 | ||
| 77 | #define BIDI_BOB -2 | ||
| 78 | |||
| 79 | #ifdef TEST_STANDALONE | ||
| 80 | /* Testing. */ | ||
| 81 | |||
| 82 | static unsigned char *input_buf; | ||
| 83 | static size_t input_buf_size; | ||
| 84 | |||
| 85 | int _fetch_multibyte_char_len, _c_c_; | ||
| 86 | |||
| 87 | #undef FETCH_CHAR_ADVANCE | ||
| 88 | #define FETCH_CHAR_ADVANCE(ch, cp, bp) \ | ||
| 89 | do { \ | ||
| 90 | ch = input_buf[cp]; \ | ||
| 91 | (cp)++; \ | ||
| 92 | (bp)++; \ | ||
| 93 | if (ch == '\0') \ | ||
| 94 | ch = BIDI_EOB; \ | ||
| 95 | } while (0) | ||
| 96 | |||
| 97 | #undef FETCH_CHAR | ||
| 98 | #define FETCH_CHAR(n) ((_c_c_ = input_buf[n]) ? _c_c_ : BIDI_EOB) | ||
| 99 | |||
| 100 | #undef CHAR_CHARSET | ||
| 101 | #define CHAR_CHARSET(c) \ | ||
| 102 | (((c) >= 128 || ((c) < 8 && (c)) || ((c) >= 'A' && (c) < 'X')) \ | ||
| 103 | ? CHARSET_HEBREW \ | ||
| 104 | : ((((c) >= 'X' && (c) <= 'Z') || ((c) >= '6' && (c) <= '9')) \ | ||
| 105 | ? CHARSET_ARABIC \ | ||
| 106 | : CHARSET_ASCII)) | ||
| 107 | |||
| 108 | #undef CHAR_TO_BYTE | ||
| 109 | #define CHAR_TO_BYTE(pos) (pos) | ||
| 110 | |||
| 111 | #define char_bytes(ch) 1 | ||
| 112 | |||
| 113 | #undef LRE_CHAR | ||
| 114 | #undef LRO_CHAR | ||
| 115 | #undef RLE_CHAR | ||
| 116 | #undef RLO_CHAR | ||
| 117 | #undef PDF_CHAR | ||
| 118 | #undef RLM_CHAR | ||
| 119 | #undef LRM_CHAR | ||
| 120 | |||
| 121 | #define LRE_CHAR 1 | ||
| 122 | #define LRO_CHAR 2 | ||
| 123 | #define RLE_CHAR 3 | ||
| 124 | #define RLO_CHAR 4 | ||
| 125 | #define PDF_CHAR 5 | ||
| 126 | #define RLM_CHAR 6 | ||
| 127 | #define LRM_CHAR 7 | ||
| 128 | |||
| 129 | static const char *bidi_name[] = | ||
| 130 | { | ||
| 131 | "[???]", "[LRE]", "[LRO]", "[RLE]", "[RLO]", "[PDF]", "[RLM]", "[LRM]" | ||
| 132 | }; | ||
| 133 | |||
| 134 | #endif /* TEST_STANDALONE */ | ||
| 135 | |||
| 136 | /* Local data structures. (Look in dispextern.h for the rest.) */ | ||
| 137 | |||
| 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 */ | ||
| 144 | }; | ||
| 145 | |||
| 146 | /* Data type for describing the bidirectional character categories. */ | ||
| 147 | typedef enum { | ||
| 148 | UNKNOWN_BC, | ||
| 149 | NEUTRAL, | ||
| 150 | WEAK, | ||
| 151 | STRONG | ||
| 152 | } bidi_category_t; | ||
| 153 | |||
| 154 | int bidi_ignore_explicit_marks_for_paragraph_level = 1; | ||
| 155 | |||
| 156 | bidi_dir_t bidi_overriding_paragraph_direction = NEUTRAL_DIR; | ||
| 157 | |||
| 158 | #define ASCII_BIDI_TYPE_SET(STR, TYPE) \ | ||
| 159 | do { \ | ||
| 160 | unsigned char *p; \ | ||
| 161 | for (p = (STR); *p; p++) \ | ||
| 162 | CHAR_TABLE_SET (bidi_type_table, *p, (TYPE)); \ | ||
| 163 | } while (0) | ||
| 164 | |||
| 165 | static void | ||
| 166 | bidi_initialize () | ||
| 167 | { | ||
| 168 | /* FIXME: This should come from the Unicode Database. */ | ||
| 169 | struct { | ||
| 170 | int from, to; | ||
| 171 | bidi_type_t type; | ||
| 172 | } bidi_type[] = | ||
| 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 } }; | ||
| 446 | int i; | ||
| 447 | |||
| 448 | bidi_type_table = Fmake_char_table (Qnil, make_number (STRONG_L)); | ||
| 449 | |||
| 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; | ||
| 454 | } | ||
| 455 | |||
| 456 | static int | ||
| 457 | bidi_is_arabic_number (int ch) | ||
| 458 | { | ||
| 459 | #ifdef TEST_STANDALONE | ||
| 460 | return ch >= '6' && ch <= '9'; | ||
| 461 | #else | ||
| 462 | return 0; /* FIXME! */ | ||
| 463 | #endif | ||
| 464 | } | ||
| 465 | |||
| 466 | /* Return the bidi type of a character CH. */ | ||
| 467 | bidi_type_t | ||
| 468 | bidi_get_type (int ch) | ||
| 469 | { | ||
| 470 | return (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch)); | ||
| 471 | } | ||
| 472 | |||
| 473 | /* Given a bidi TYPE of a character, return its category. */ | ||
| 474 | bidi_category_t | ||
| 475 | bidi_get_category (bidi_type_t type) | ||
| 476 | { | ||
| 477 | switch (type) | ||
| 478 | { | ||
| 479 | case UNKNOWN_BT: | ||
| 480 | return UNKNOWN_BC; | ||
| 481 | case STRONG_L: | ||
| 482 | case STRONG_R: | ||
| 483 | case STRONG_AL: | ||
| 484 | case LRE: | ||
| 485 | case LRO: | ||
| 486 | case RLE: | ||
| 487 | case RLO: | ||
| 488 | return STRONG; | ||
| 489 | case PDF: /* ??? really?? */ | ||
| 490 | case WEAK_EN: | ||
| 491 | case WEAK_ES: | ||
| 492 | case WEAK_ET: | ||
| 493 | case WEAK_AN: | ||
| 494 | case WEAK_CS: | ||
| 495 | case WEAK_NSM: | ||
| 496 | case WEAK_BN: | ||
| 497 | return WEAK; | ||
| 498 | case NEUTRAL_B: | ||
| 499 | case NEUTRAL_S: | ||
| 500 | case NEUTRAL_WS: | ||
| 501 | case NEUTRAL_ON: | ||
| 502 | return NEUTRAL; | ||
| 503 | default: | ||
| 504 | abort (); | ||
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | /* FIXME: exceedingly temporary! Should consult the Unicode database | ||
| 509 | of character properties. */ | ||
| 510 | int | ||
| 511 | bidi_mirror_char (int c) | ||
| 512 | { | ||
| 513 | static const char mirrored_pairs[] = "()<>[]{}"; | ||
| 514 | const char *p = strchr (mirrored_pairs, c); | ||
| 515 | |||
| 516 | if (p) | ||
| 517 | { | ||
| 518 | size_t i = p - mirrored_pairs; | ||
| 519 | |||
| 520 | if ((i & 1) == 0) | ||
| 521 | return mirrored_pairs[i + 1]; | ||
| 522 | else | ||
| 523 | return mirrored_pairs[i - 1]; | ||
| 524 | } | ||
| 525 | return c; | ||
| 526 | } | ||
| 527 | |||
| 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. */ | ||
| 530 | static inline void | ||
| 531 | bidi_copy_it (struct bidi_it *to, struct bidi_it *from) | ||
| 532 | { | ||
| 533 | int i; | ||
| 534 | |||
| 535 | /* Copy everything except the level stack. */ | ||
| 536 | memcpy (to, from, ((int)&((struct bidi_it *)0)->level_stack[0])); | ||
| 537 | |||
| 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]; | ||
| 542 | } | ||
| 543 | |||
| 544 | /* Caching the bidi iterator states. */ | ||
| 545 | |||
| 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; | ||
| 549 | |||
| 550 | static inline void | ||
| 551 | bidi_cache_reset (void) | ||
| 552 | { | ||
| 553 | bidi_cache_idx = 0; | ||
| 554 | bidi_cache_last_idx = -1; | ||
| 555 | } | ||
| 556 | |||
| 557 | static inline void | ||
| 558 | bidi_cache_fetch_state (int idx, struct bidi_it *bidi_it) | ||
| 559 | { | ||
| 560 | int current_scan_dir = bidi_it->scan_dir; | ||
| 561 | |||
| 562 | if (idx < 0 || idx >= bidi_cache_idx) | ||
| 563 | abort (); | ||
| 564 | |||
| 565 | bidi_copy_it (bidi_it, &bidi_cache[idx]); | ||
| 566 | bidi_it->scan_dir = current_scan_dir; | ||
| 567 | bidi_cache_last_idx = idx; | ||
| 568 | } | ||
| 569 | |||
| 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. */ | ||
| 574 | static inline int | ||
| 575 | bidi_cache_search (int charpos, int level, int dir) | ||
| 576 | { | ||
| 577 | int i, i_start; | ||
| 578 | |||
| 579 | if (bidi_cache_idx) | ||
| 580 | { | ||
| 581 | if (charpos < bidi_cache[bidi_cache_last_idx].charpos) | ||
| 582 | dir = -1; | ||
| 583 | else if (charpos > bidi_cache[bidi_cache_last_idx].charpos) | ||
| 584 | dir = 1; | ||
| 585 | if (dir) | ||
| 586 | i_start = bidi_cache_last_idx; | ||
| 587 | else | ||
| 588 | { | ||
| 589 | dir = -1; | ||
| 590 | i_start = bidi_cache_idx - 1; | ||
| 591 | } | ||
| 592 | |||
| 593 | if (dir < 0) | ||
| 594 | { | ||
| 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)) | ||
| 599 | return i; | ||
| 600 | } | ||
| 601 | else | ||
| 602 | { | ||
| 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)) | ||
| 606 | return i; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | return -1; | ||
| 611 | } | ||
| 612 | |||
| 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: | ||
| 619 | |||
| 620 | 1122333442211 | ||
| 621 | AB C | ||
| 622 | |||
| 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, | ||
| 626 | non-zero or zero. */ | ||
| 627 | static int | ||
| 628 | bidi_cache_find_level_change (int level, int dir, int before) | ||
| 629 | { | ||
| 630 | if (bidi_cache_idx) | ||
| 631 | { | ||
| 632 | int i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1; | ||
| 633 | int incr = before ? 1 : 0; | ||
| 634 | |||
| 635 | if (!dir) | ||
| 636 | dir = -1; | ||
| 637 | else if (!incr) | ||
| 638 | i += dir; | ||
| 639 | |||
| 640 | if (dir < 0) | ||
| 641 | { | ||
| 642 | while (i >= incr) | ||
| 643 | { | ||
| 644 | if (bidi_cache[i - incr].resolved_level >= 0 | ||
| 645 | && bidi_cache[i - incr].resolved_level < level) | ||
| 646 | return i; | ||
| 647 | i--; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | else | ||
| 651 | { | ||
| 652 | while (i < bidi_cache_idx - incr) | ||
| 653 | { | ||
| 654 | if (bidi_cache[i + incr].resolved_level >= 0 | ||
| 655 | && bidi_cache[i + incr].resolved_level < level) | ||
| 656 | return i; | ||
| 657 | i++; | ||
| 658 | } | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | return -1; | ||
| 663 | } | ||
| 664 | |||
| 665 | static inline void | ||
| 666 | bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved) | ||
| 667 | { | ||
| 668 | int idx; | ||
| 669 | |||
| 670 | /* We should never cache on backward scans. */ | ||
| 671 | if (bidi_it->scan_dir == -1) | ||
| 672 | abort (); | ||
| 673 | idx = bidi_cache_search (bidi_it->charpos, -1, 1); | ||
| 674 | |||
| 675 | if (idx < 0) | ||
| 676 | { | ||
| 677 | idx = bidi_cache_idx; | ||
| 678 | if (idx > sizeof (bidi_cache) / sizeof (bidi_cache[0]) - 1) | ||
| 679 | abort (); | ||
| 680 | bidi_copy_it (&bidi_cache[idx], bidi_it); | ||
| 681 | if (!resolved) | ||
| 682 | bidi_cache[idx].resolved_level = -1; | ||
| 683 | } | ||
| 684 | else | ||
| 685 | { | ||
| 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; | ||
| 690 | if (resolved) | ||
| 691 | bidi_cache[idx].resolved_level = bidi_it->resolved_level; | ||
| 692 | else | ||
| 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; | ||
| 699 | } | ||
| 700 | |||
| 701 | bidi_cache_last_idx = idx; | ||
| 702 | if (idx >= bidi_cache_idx) | ||
| 703 | bidi_cache_idx = idx + 1; | ||
| 704 | } | ||
| 705 | |||
| 706 | static inline bidi_type_t | ||
| 707 | bidi_cache_find (int charpos, int level, struct bidi_it *bidi_it) | ||
| 708 | { | ||
| 709 | int i = bidi_cache_search (charpos, level, bidi_it->scan_dir); | ||
| 710 | |||
| 711 | if (i >= 0) | ||
| 712 | { | ||
| 713 | bidi_dir_t current_scan_dir = bidi_it->scan_dir; | ||
| 714 | |||
| 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; | ||
| 721 | } | ||
| 722 | |||
| 723 | return UNKNOWN_BT; | ||
| 724 | } | ||
| 725 | |||
| 726 | static inline int | ||
| 727 | bidi_peek_at_next_level (struct bidi_it *bidi_it) | ||
| 728 | { | ||
| 729 | if (bidi_cache_idx == 0 || bidi_cache_last_idx == -1) | ||
| 730 | abort (); | ||
| 731 | return bidi_cache[bidi_cache_last_idx + bidi_it->scan_dir].resolved_level; | ||
| 732 | } | ||
| 733 | |||
| 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 | ||
| 736 | the buffer. */ | ||
| 737 | int | ||
| 738 | bidi_at_paragraph_end (int this_ch, int pos) | ||
| 739 | { | ||
| 740 | int next_ch = FETCH_CHAR (pos); | ||
| 741 | |||
| 742 | /* FIXME: This should support all Unicode characters that can end a | ||
| 743 | paragraph. */ | ||
| 744 | return (this_ch == '\n' && next_ch == '\n') || this_ch == BIDI_EOB; | ||
| 745 | } | ||
| 746 | |||
| 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. */ | ||
| 751 | static inline void | ||
| 752 | bidi_set_sor_type (struct bidi_it *bidi_it, int level_before, int level_after) | ||
| 753 | { | ||
| 754 | int higher_level = level_before > level_after ? level_before : level_after; | ||
| 755 | |||
| 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; | ||
| 768 | |||
| 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 */ | ||
| 778 | } | ||
| 779 | |||
| 780 | void | ||
| 781 | bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it) | ||
| 782 | { | ||
| 783 | bidi_it->level_stack[0].level = 0; | ||
| 784 | if (dir == R2L) | ||
| 785 | bidi_it->level_stack[0].level = 1; | ||
| 786 | else if (dir == NEUTRAL_DIR) /* P2 */ | ||
| 787 | { | ||
| 788 | bidi_type_t type; | ||
| 789 | int pos = bidi_it->charpos, bytepos = bidi_it->bytepos; | ||
| 790 | int ch; | ||
| 791 | |||
| 792 | if (pos < 0) | ||
| 793 | pos = bytepos = 0; | ||
| 794 | else if (bidi_it->ch != BIDI_EOB) | ||
| 795 | { | ||
| 796 | pos++; | ||
| 797 | bytepos += bidi_it->ch_len; | ||
| 798 | } | ||
| 799 | |||
| 800 | ch = FETCH_CHAR (bytepos); | ||
| 801 | pos++; | ||
| 802 | bytepos += CHAR_BYTES (ch); | ||
| 803 | |||
| 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. */ | ||
| 807 | |||
| 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)) | ||
| 818 | { | ||
| 819 | if (type == NEUTRAL_B || bidi_at_paragraph_end (ch, bytepos)) | ||
| 820 | break; | ||
| 821 | FETCH_CHAR_ADVANCE (ch, pos, bytepos); | ||
| 822 | } | ||
| 823 | if (type == STRONG_R || type == STRONG_AL) /* P3 */ | ||
| 824 | bidi_it->level_stack[0].level = 1; | ||
| 825 | } | ||
| 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 */ | ||
| 835 | |||
| 836 | bidi_cache_reset (); | ||
| 837 | } | ||
| 838 | |||
| 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. */ | ||
| 841 | static inline void | ||
| 842 | bidi_set_paragraph_end (struct bidi_it *bidi_it) | ||
| 843 | { | ||
| 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; | ||
| 849 | } | ||
| 850 | |||
| 851 | /* Initialize the bidi iterator from buffer position POS for paragraph | ||
| 852 | direction DIR. Return the embedding level at POS. */ | ||
| 853 | void | ||
| 854 | bidi_init_it (int pos, bidi_dir_t dir, struct bidi_it *bidi_it) | ||
| 855 | { | ||
| 856 | if (! bidi_initialized) | ||
| 857 | bidi_initialize (); | ||
| 858 | bidi_set_paragraph_end (bidi_it); | ||
| 859 | bidi_it->charpos = pos; | ||
| 860 | if (pos <= 0) | ||
| 861 | { | ||
| 862 | bidi_it->bytepos = bidi_it->charpos; | ||
| 863 | bidi_it->ch_len = 1; /* so that incrementing bytepos works */ | ||
| 864 | } | ||
| 865 | else | ||
| 866 | { | ||
| 867 | bidi_it->bytepos = CHAR_TO_BYTE (pos); | ||
| 868 | bidi_it->ch_len | ||
| 869 | = MULTIBYTE_FORM_LENGTH (BYTE_POS_ADDR (bidi_it->bytepos), | ||
| 870 | MAX_MULTIBYTE_LENGTH); | ||
| 871 | } | ||
| 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); | ||
| 890 | } | ||
| 891 | |||
| 892 | /* Push the current embedding level and override status; reset the | ||
| 893 | current level to LEVEL and the current override status to OVERRIDE. */ | ||
| 894 | static inline void | ||
| 895 | bidi_push_embedding_level (struct bidi_it *bidi_it, | ||
| 896 | int level, bidi_dir_t override) | ||
| 897 | { | ||
| 898 | bidi_it->stack_idx++; | ||
| 899 | if (bidi_it->stack_idx >= BIDI_MAXLEVEL) | ||
| 900 | abort (); | ||
| 901 | bidi_it->level_stack[bidi_it->stack_idx].level = level; | ||
| 902 | bidi_it->level_stack[bidi_it->stack_idx].override = override; | ||
| 903 | } | ||
| 904 | |||
| 905 | /* Pop the embedding level and directional override status from the | ||
| 906 | stack, and return the new level. */ | ||
| 907 | static inline int | ||
| 908 | bidi_pop_embedding_level (struct bidi_it *bidi_it) | ||
| 909 | { | ||
| 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; | ||
| 914 | } | ||
| 915 | |||
| 916 | /* Record in SAVED_INFO the information about the current character. */ | ||
| 917 | static inline void | ||
| 918 | bidi_remember_char (struct bidi_saved_info *saved_info, | ||
| 919 | struct bidi_it *bidi_it) | ||
| 920 | { | ||
| 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; | ||
| 926 | } | ||
| 927 | |||
| 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) | ||
| 932 | { | ||
| 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; | ||
| 938 | |||
| 939 | if (next_type == prev_type) /* N1 */ | ||
| 940 | return next_type; | ||
| 941 | else if ((lev & 1) == 0) /* N2 */ | ||
| 942 | return STRONG_L; | ||
| 943 | else | ||
| 944 | return STRONG_R; | ||
| 945 | } | ||
| 946 | |||
| 947 | static inline int | ||
| 948 | bidi_explicit_dir_char (int c) | ||
| 949 | { | ||
| 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); | ||
| 954 | } | ||
| 955 | |||
| 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 | ||
| 959 | empty embeddings. */ | ||
| 960 | static int | ||
| 961 | bidi_resolve_explicit_1 (struct bidi_it *bidi_it) | ||
| 962 | { | ||
| 963 | int curchar; | ||
| 964 | bidi_type_t type; | ||
| 965 | int current_level; | ||
| 966 | int new_level; | ||
| 967 | bidi_dir_t override; | ||
| 968 | |||
| 969 | if (bidi_it->charpos < 0) | ||
| 970 | bidi_it->charpos = bidi_it->bytepos = 0; | ||
| 971 | else | ||
| 972 | { | ||
| 973 | bidi_it->charpos++; | ||
| 974 | bidi_it->bytepos += bidi_it->ch_len; | ||
| 975 | } | ||
| 976 | |||
| 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; | ||
| 980 | |||
| 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); | ||
| 986 | |||
| 987 | type = bidi_get_type (curchar); | ||
| 988 | bidi_it->pristine_type = type; | ||
| 989 | |||
| 990 | if (type != PDF) | ||
| 991 | bidi_it->prev_was_pdf = 0; | ||
| 992 | |||
| 993 | bidi_it->orig_type = UNKNOWN_BT; | ||
| 994 | |||
| 995 | switch (type) | ||
| 996 | { | ||
| 997 | case RLE: /* X2 */ | ||
| 998 | case RLO: /* X4 */ | ||
| 999 | bidi_it->orig_type = type; | ||
| 1000 | type = WEAK_BN; /* X9/Retaining */ | ||
| 1001 | if (bidi_it->ignore_bn_limit <= 0) | ||
| 1002 | { | ||
| 1003 | if (current_level <= BIDI_MAXLEVEL - 4) | ||
| 1004 | { | ||
| 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; | ||
| 1010 | else | ||
| 1011 | override = R2L; | ||
| 1012 | if (current_level == BIDI_MAXLEVEL - 4) | ||
| 1013 | bidi_it->invalid_rl_levels = 0; | ||
| 1014 | bidi_push_embedding_level (bidi_it, new_level, override); | ||
| 1015 | } | ||
| 1016 | else | ||
| 1017 | { | ||
| 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++; | ||
| 1023 | } | ||
| 1024 | } | ||
| 1025 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | ||
| 1026 | || bidi_it->next_en_pos > bidi_it->charpos) | ||
| 1027 | type = WEAK_EN; | ||
| 1028 | break; | ||
| 1029 | case LRE: /* X3 */ | ||
| 1030 | case LRO: /* X5 */ | ||
| 1031 | bidi_it->orig_type = type; | ||
| 1032 | type = WEAK_BN; /* X9/Retaining */ | ||
| 1033 | if (bidi_it->ignore_bn_limit <= 0) | ||
| 1034 | { | ||
| 1035 | if (current_level <= BIDI_MAXLEVEL - 5) | ||
| 1036 | { | ||
| 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; | ||
| 1042 | else | ||
| 1043 | override = L2R; | ||
| 1044 | bidi_push_embedding_level (bidi_it, new_level, override); | ||
| 1045 | } | ||
| 1046 | else | ||
| 1047 | { | ||
| 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 | ||
| 1055 | that. */ | ||
| 1056 | if (bidi_it->invalid_rl_levels >= 0) | ||
| 1057 | bidi_it->invalid_rl_levels++; | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | ||
| 1061 | || bidi_it->next_en_pos > bidi_it->charpos) | ||
| 1062 | type = WEAK_EN; | ||
| 1063 | break; | ||
| 1064 | case PDF: /* X7 */ | ||
| 1065 | bidi_it->orig_type = type; | ||
| 1066 | type = WEAK_BN; /* X9/Retaining */ | ||
| 1067 | if (bidi_it->ignore_bn_limit <= 0) | ||
| 1068 | { | ||
| 1069 | if (!bidi_it->invalid_rl_levels) | ||
| 1070 | { | ||
| 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 */ | ||
| 1076 | } | ||
| 1077 | if (!bidi_it->invalid_levels) | ||
| 1078 | new_level = bidi_pop_embedding_level (bidi_it); | ||
| 1079 | else | ||
| 1080 | { | ||
| 1081 | bidi_it->invalid_levels--; | ||
| 1082 | bidi_it->invalid_rl_levels--; | ||
| 1083 | } | ||
| 1084 | } | ||
| 1085 | else if (bidi_it->prev.orig_type == WEAK_EN /* W5/Retaining */ | ||
| 1086 | || bidi_it->next_en_pos > bidi_it->charpos) | ||
| 1087 | type = WEAK_EN; | ||
| 1088 | break; | ||
| 1089 | default: | ||
| 1090 | /* Nothing. */ | ||
| 1091 | break; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | bidi_it->type = type; | ||
| 1095 | |||
| 1096 | return new_level; | ||
| 1097 | } | ||
| 1098 | |||
| 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. */ | ||
| 1104 | static int | ||
| 1105 | bidi_resolve_explicit (struct bidi_it *bidi_it) | ||
| 1106 | { | ||
| 1107 | int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level; | ||
| 1108 | int new_level = bidi_resolve_explicit_1 (bidi_it); | ||
| 1109 | |||
| 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))) | ||
| 1115 | { | ||
| 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; | ||
| 1123 | |||
| 1124 | bidi_copy_it (&saved_it, bidi_it); | ||
| 1125 | |||
| 1126 | while (bidi_explicit_dir_char (FETCH_CHAR (bidi_it->bytepos | ||
| 1127 | + bidi_it->ch_len))) | ||
| 1128 | { | ||
| 1129 | level = bidi_resolve_explicit_1 (bidi_it); | ||
| 1130 | } | ||
| 1131 | |||
| 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; | ||
| 1136 | |||
| 1137 | bidi_copy_it (bidi_it, &saved_it); | ||
| 1138 | if (bidi_it->ignore_bn_limit > 0) | ||
| 1139 | { | ||
| 1140 | /* We pushed a level, but we shouldn't have. Undo that. */ | ||
| 1141 | if (!bidi_it->invalid_rl_levels) | ||
| 1142 | { | ||
| 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--; | ||
| 1147 | } | ||
| 1148 | if (!bidi_it->invalid_levels) | ||
| 1149 | new_level = bidi_pop_embedding_level (bidi_it); | ||
| 1150 | else | ||
| 1151 | { | ||
| 1152 | bidi_it->invalid_levels--; | ||
| 1153 | bidi_it->invalid_rl_levels--; | ||
| 1154 | } | ||
| 1155 | } | ||
| 1156 | } | ||
| 1157 | |||
| 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; | ||
| 1164 | |||
| 1165 | if (bidi_it->type == NEUTRAL_B) /* X8 */ | ||
| 1166 | { | ||
| 1167 | bidi_set_paragraph_end (bidi_it); | ||
| 1168 | bidi_it->orig_type = bidi_it->type; /* needed below and in L1 */ | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | return new_level; | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | /* Advance in the buffer, resolve weak types and return the type of | ||
| 1175 | the next character after weak type resolution. */ | ||
| 1176 | bidi_type_t | ||
| 1177 | bidi_resolve_weak (struct bidi_it *bidi_it) | ||
| 1178 | { | ||
| 1179 | bidi_type_t type; | ||
| 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); | ||
| 1183 | int next_char; | ||
| 1184 | bidi_type_t type_of_next; | ||
| 1185 | struct bidi_it saved_it; | ||
| 1186 | |||
| 1187 | type = bidi_it->type; | ||
| 1188 | override = bidi_it->level_stack[bidi_it->stack_idx].override; | ||
| 1189 | |||
| 1190 | if (type == UNKNOWN_BT | ||
| 1191 | || type == LRE | ||
| 1192 | || type == LRO | ||
| 1193 | || type == RLE | ||
| 1194 | || type == RLO | ||
| 1195 | || type == PDF) | ||
| 1196 | abort (); | ||
| 1197 | |||
| 1198 | if (new_level != prev_level | ||
| 1199 | || bidi_it->type == NEUTRAL_B) | ||
| 1200 | { | ||
| 1201 | /* We've got a new embedding level run, compute the directional | ||
| 1202 | type of sor and initialize per-run variables (UAX#9, clause | ||
| 1203 | X10). */ | ||
| 1204 | bidi_set_sor_type (bidi_it, prev_level, new_level); | ||
| 1205 | } | ||
| 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 */ | ||
| 1209 | |||
| 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 */ | ||
| 1213 | type = STRONG_R; | ||
| 1214 | else if (override == L2R) | ||
| 1215 | type = STRONG_L; | ||
| 1216 | else if (type == STRONG_AL) | ||
| 1217 | type = STRONG_R; /* W3 */ | ||
| 1218 | else if (type == WEAK_NSM) /* W1 */ | ||
| 1219 | { | ||
| 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) | ||
| 1228 | type = STRONG_R; | ||
| 1229 | else if (bidi_it->sor == L2R) | ||
| 1230 | type = STRONG_L; | ||
| 1231 | else /* shouldn't happen! */ | ||
| 1232 | abort (); | ||
| 1233 | if (type == WEAK_EN /* W2 after W1 */ | ||
| 1234 | && bidi_it->last_strong.orig_type == STRONG_AL) | ||
| 1235 | type = WEAK_AN; | ||
| 1236 | } | ||
| 1237 | else if (type == WEAK_EN /* W2 */ | ||
| 1238 | && bidi_it->last_strong.orig_type == STRONG_AL) | ||
| 1239 | type = WEAK_AN; | ||
| 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 */ | ||
| 1244 | || (type == WEAK_CS | ||
| 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 */ | ||
| 1249 | { | ||
| 1250 | next_char = FETCH_CHAR (bidi_it->bytepos + bidi_it->ch_len); | ||
| 1251 | type_of_next = bidi_get_type (next_char); | ||
| 1252 | |||
| 1253 | if (type_of_next == WEAK_BN | ||
| 1254 | || bidi_explicit_dir_char (next_char)) | ||
| 1255 | { | ||
| 1256 | bidi_copy_it (&saved_it, bidi_it); | ||
| 1257 | while (bidi_resolve_explicit (bidi_it) == new_level | ||
| 1258 | && bidi_it->type == WEAK_BN) | ||
| 1259 | ; | ||
| 1260 | type_of_next = bidi_it->type; | ||
| 1261 | bidi_copy_it (bidi_it, &saved_it); | ||
| 1262 | } | ||
| 1263 | |||
| 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. */ | ||
| 1268 | if (type == WEAK_ES | ||
| 1269 | && type_of_next == WEAK_EN | ||
| 1270 | && bidi_it->last_strong.orig_type != STRONG_AL) | ||
| 1271 | type = WEAK_EN; | ||
| 1272 | else if (type == WEAK_CS) | ||
| 1273 | { | ||
| 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 | ||
| 1280 | EN. */ | ||
| 1281 | || (type_of_next == WEAK_EN | ||
| 1282 | && bidi_it->last_strong.orig_type == STRONG_AL))) | ||
| 1283 | type = WEAK_AN; | ||
| 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) | ||
| 1287 | type = WEAK_EN; | ||
| 1288 | } | ||
| 1289 | } | ||
| 1290 | else if (type == WEAK_ET /* W5: ET with EN before or after it */ | ||
| 1291 | || type == WEAK_BN) /* W5/Retaining */ | ||
| 1292 | { | ||
| 1293 | if (bidi_it->prev.orig_type == WEAK_EN /* ET/BN with EN before it */ | ||
| 1294 | || bidi_it->next_en_pos > bidi_it->charpos) | ||
| 1295 | type = WEAK_EN; | ||
| 1296 | /* W5: ET with EN after it. */ | ||
| 1297 | else | ||
| 1298 | { | ||
| 1299 | int en_pos = bidi_it->charpos + 1; | ||
| 1300 | |||
| 1301 | next_char = FETCH_CHAR (bidi_it->bytepos + bidi_it->ch_len); | ||
| 1302 | type_of_next = bidi_get_type (next_char); | ||
| 1303 | |||
| 1304 | if (type_of_next == WEAK_ET | ||
| 1305 | || type_of_next == WEAK_BN | ||
| 1306 | || bidi_explicit_dir_char (next_char)) | ||
| 1307 | { | ||
| 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)) | ||
| 1311 | ; | ||
| 1312 | type_of_next = bidi_it->type; | ||
| 1313 | en_pos = bidi_it->charpos; | ||
| 1314 | bidi_copy_it (bidi_it, &saved_it); | ||
| 1315 | } | ||
| 1316 | if (type_of_next == WEAK_EN) | ||
| 1317 | { | ||
| 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) | ||
| 1321 | { | ||
| 1322 | type = WEAK_EN; | ||
| 1323 | /* Remember this EN position, to speed up processing | ||
| 1324 | of the next ETs. */ | ||
| 1325 | bidi_it->next_en_pos = en_pos; | ||
| 1326 | } | ||
| 1327 | else if (type == WEAK_BN) | ||
| 1328 | type = NEUTRAL_ON; /* W6/Retaining */ | ||
| 1329 | } | ||
| 1330 | } | ||
| 1331 | } | ||
| 1332 | |||
| 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))) | ||
| 1337 | type = NEUTRAL_ON; | ||
| 1338 | |||
| 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; | ||
| 1345 | |||
| 1346 | if (type == WEAK_EN) /* W7 */ | ||
| 1347 | { | ||
| 1348 | if ((bidi_it->last_strong.orig_type == STRONG_L) | ||
| 1349 | || (bidi_it->last_strong.type == UNKNOWN_BT && bidi_it->sor == L2R)) | ||
| 1350 | type = STRONG_L; | ||
| 1351 | } | ||
| 1352 | |||
| 1353 | bidi_it->type = type; | ||
| 1354 | return type; | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | bidi_type_t | ||
| 1358 | bidi_resolve_neutral (struct bidi_it *bidi_it) | ||
| 1359 | { | ||
| 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; | ||
| 1363 | |||
| 1364 | if (!(type == STRONG_R | ||
| 1365 | || type == STRONG_L | ||
| 1366 | || type == WEAK_BN | ||
| 1367 | || type == WEAK_EN | ||
| 1368 | || type == WEAK_AN | ||
| 1369 | || type == NEUTRAL_B | ||
| 1370 | || type == NEUTRAL_S | ||
| 1371 | || type == NEUTRAL_WS | ||
| 1372 | || type == NEUTRAL_ON)) | ||
| 1373 | abort (); | ||
| 1374 | |||
| 1375 | if (bidi_get_category (type) == NEUTRAL | ||
| 1376 | || (type == WEAK_BN && prev_level == current_level)) | ||
| 1377 | { | ||
| 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, | ||
| 1381 | current_level); | ||
| 1382 | else | ||
| 1383 | { | ||
| 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 | ||
| 1391 | implementations! */ | ||
| 1392 | struct bidi_it saved_it; | ||
| 1393 | bidi_type_t next_type; | ||
| 1394 | |||
| 1395 | if (bidi_it->scan_dir == -1) | ||
| 1396 | abort (); | ||
| 1397 | |||
| 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); | ||
| 1404 | do { | ||
| 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 | ||
| 1418 | || (type != WEAK_BN | ||
| 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 != | ||
| 1423 | current_level)); | ||
| 1424 | |||
| 1425 | bidi_remember_char (&saved_it.next_for_neutral, bidi_it); | ||
| 1426 | |||
| 1427 | switch (type) | ||
| 1428 | { | ||
| 1429 | case STRONG_L: | ||
| 1430 | case STRONG_R: | ||
| 1431 | case STRONG_AL: | ||
| 1432 | next_type = type; | ||
| 1433 | break; | ||
| 1434 | case WEAK_EN: | ||
| 1435 | case WEAK_AN: | ||
| 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; | ||
| 1440 | break; | ||
| 1441 | case WEAK_BN: | ||
| 1442 | if (!bidi_explicit_dir_char (bidi_it->ch)) | ||
| 1443 | abort (); /* can't happen: BNs are skipped */ | ||
| 1444 | /* FALLTHROUGH */ | ||
| 1445 | case NEUTRAL_B: | ||
| 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 | ||
| 1449 | member. */ | ||
| 1450 | if (saved_it.type != WEAK_BN | ||
| 1451 | || bidi_get_category (bidi_it->prev.orig_type) == NEUTRAL) | ||
| 1452 | { | ||
| 1453 | next_type = bidi_it->prev_for_neutral.type; | ||
| 1454 | saved_it.next_for_neutral.type = next_type; | ||
| 1455 | } | ||
| 1456 | else | ||
| 1457 | { | ||
| 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; | ||
| 1462 | } | ||
| 1463 | break; | ||
| 1464 | default: | ||
| 1465 | abort (); | ||
| 1466 | } | ||
| 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); | ||
| 1471 | } | ||
| 1472 | } | ||
| 1473 | return type; | ||
| 1474 | } | ||
| 1475 | |||
| 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. */ | ||
| 1479 | bidi_type_t | ||
| 1480 | bidi_type_of_next_char (struct bidi_it *bidi_it) | ||
| 1481 | { | ||
| 1482 | bidi_type_t type; | ||
| 1483 | |||
| 1484 | /* This should always be called during a forward scan. */ | ||
| 1485 | if (bidi_it->scan_dir != 1) | ||
| 1486 | abort (); | ||
| 1487 | |||
| 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; | ||
| 1495 | |||
| 1496 | type = bidi_resolve_neutral (bidi_it); | ||
| 1497 | |||
| 1498 | return type; | ||
| 1499 | } | ||
| 1500 | |||
| 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. */ | ||
| 1505 | int | ||
| 1506 | bidi_level_of_next_char (struct bidi_it *bidi_it) | ||
| 1507 | { | ||
| 1508 | bidi_type_t type; | ||
| 1509 | int level, prev_level = -1; | ||
| 1510 | struct bidi_saved_info next_for_neutral; | ||
| 1511 | |||
| 1512 | if (bidi_it->scan_dir == 1) | ||
| 1513 | { | ||
| 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; | ||
| 1517 | |||
| 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); | ||
| 1530 | |||
| 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; | ||
| 1541 | |||
| 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; | ||
| 1548 | } | ||
| 1549 | next_for_neutral = bidi_it->next_for_neutral; | ||
| 1550 | |||
| 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) | ||
| 1554 | { | ||
| 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 | ||
| 1559 | state. */ | ||
| 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; | ||
| 1563 | |||
| 1564 | /* If resolved_level is -1, it means this state was cached | ||
| 1565 | before it was completely resolved, so we cannot return | ||
| 1566 | it. */ | ||
| 1567 | if (bidi_it->resolved_level != -1) | ||
| 1568 | return bidi_it->resolved_level; | ||
| 1569 | } | ||
| 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. */ | ||
| 1573 | abort (); | ||
| 1574 | |||
| 1575 | if (type == UNKNOWN_BT) | ||
| 1576 | type = bidi_type_of_next_char (bidi_it); | ||
| 1577 | |||
| 1578 | if (type == NEUTRAL_B) | ||
| 1579 | return bidi_it->resolved_level; | ||
| 1580 | |||
| 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)) | ||
| 1584 | { | ||
| 1585 | if (bidi_it->next_for_neutral.type == UNKNOWN_BT) | ||
| 1586 | abort (); | ||
| 1587 | |||
| 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, | ||
| 1592 | level); | ||
| 1593 | } | ||
| 1594 | |||
| 1595 | if (!(type == STRONG_R | ||
| 1596 | || type == STRONG_L | ||
| 1597 | || type == WEAK_BN | ||
| 1598 | || type == WEAK_EN | ||
| 1599 | || type == WEAK_AN)) | ||
| 1600 | abort (); | ||
| 1601 | bidi_it->type = type; | ||
| 1602 | |||
| 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) | ||
| 1608 | { | ||
| 1609 | int ch; | ||
| 1610 | int clen = bidi_it->ch_len; | ||
| 1611 | int bpos = bidi_it->bytepos; | ||
| 1612 | int cpos = bidi_it->charpos; | ||
| 1613 | bidi_type_t chtype; | ||
| 1614 | |||
| 1615 | do { | ||
| 1616 | /*_fetch_multibyte_char_len = 1;*/ | ||
| 1617 | ch = FETCH_CHAR (bpos + clen); | ||
| 1618 | bpos += clen; | ||
| 1619 | cpos++; | ||
| 1620 | clen = CHAR_BYTES (ch); | ||
| 1621 | if (ch == '\n' /* || ch == LINESEP_CHAR */) | ||
| 1622 | chtype = NEUTRAL_B; | ||
| 1623 | else | ||
| 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; | ||
| 1630 | } | ||
| 1631 | |||
| 1632 | /* Resolve implicit levels, with a twist: PDFs get the embedding | ||
| 1633 | level of the enbedding they terminate. See below for the | ||
| 1634 | reason. */ | ||
| 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) | ||
| 1639 | { | ||
| 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 | ||
| 1644 | |||
| 1645 | {RLO}teST{PDF} | ||
| 1646 | |||
| 1647 | we want it to be displayed as | ||
| 1648 | |||
| 1649 | {RLO}STet{PDF} | ||
| 1650 | |||
| 1651 | not as | ||
| 1652 | |||
| 1653 | STet{RLO}{PDF} | ||
| 1654 | |||
| 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. | ||
| 1661 | |||
| 1662 | (This is, of course, only important if the formatting codes | ||
| 1663 | are actually displayed, but Emacs does display them if the | ||
| 1664 | user wants to.) */ | ||
| 1665 | level = prev_level; | ||
| 1666 | } | ||
| 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 */ | ||
| 1675 | { | ||
| 1676 | if (type == STRONG_R) | ||
| 1677 | level++; | ||
| 1678 | else if (type == WEAK_EN || type == WEAK_AN) | ||
| 1679 | level += 2; | ||
| 1680 | } | ||
| 1681 | else /* I2 */ | ||
| 1682 | { | ||
| 1683 | if (type == STRONG_L || type == WEAK_EN || type == WEAK_AN) | ||
| 1684 | level++; | ||
| 1685 | } | ||
| 1686 | |||
| 1687 | bidi_it->resolved_level = level; | ||
| 1688 | return level; | ||
| 1689 | } | ||
| 1690 | |||
| 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. | ||
| 1694 | |||
| 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 | ||
| 1698 | as LEVEL. | ||
| 1699 | |||
| 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 | ||
| 1703 | like this: | ||
| 1704 | |||
| 1705 | 11111112222222333333334443343222222111111112223322111 | ||
| 1706 | A B C | ||
| 1707 | |||
| 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 | ||
| 1710 | at point B. */ | ||
| 1711 | static void | ||
| 1712 | bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag) | ||
| 1713 | { | ||
| 1714 | int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir; | ||
| 1715 | int idx; | ||
| 1716 | |||
| 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); | ||
| 1720 | else | ||
| 1721 | { | ||
| 1722 | int new_level; | ||
| 1723 | |||
| 1724 | if (end_flag) | ||
| 1725 | abort (); /* if we are at end of level, its edges must be cached */ | ||
| 1726 | |||
| 1727 | bidi_cache_iterator_state (bidi_it, 1); | ||
| 1728 | do { | ||
| 1729 | new_level = bidi_level_of_next_char (bidi_it); | ||
| 1730 | bidi_cache_iterator_state (bidi_it, 1); | ||
| 1731 | } while (new_level >= level); | ||
| 1732 | } | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | void | ||
| 1736 | bidi_get_next_char_visually (struct bidi_it *bidi_it) | ||
| 1737 | { | ||
| 1738 | int old_level, new_level, next_level; | ||
| 1739 | struct bidi_it prev_bidi_it; | ||
| 1740 | |||
| 1741 | if (bidi_it->scan_dir == 0) | ||
| 1742 | { | ||
| 1743 | bidi_it->scan_dir = 1; /* default to logical order */ | ||
| 1744 | } | ||
| 1745 | |||
| 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); | ||
| 1750 | |||
| 1751 | old_level = bidi_it->resolved_level; | ||
| 1752 | new_level = bidi_level_of_next_char (bidi_it); | ||
| 1753 | if (bidi_it->ch == BIDI_EOB) | ||
| 1754 | return; | ||
| 1755 | |||
| 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) | ||
| 1760 | { | ||
| 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; | ||
| 1765 | |||
| 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 | ||
| 1774 | new direction. */ | ||
| 1775 | bidi_it->scan_dir = -bidi_it->scan_dir; | ||
| 1776 | |||
| 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: | ||
| 1783 | |||
| 1784 | abcdefgh | ||
| 1785 | 11336622 | ||
| 1786 | |||
| 1787 | (where the numbers below the text show the resolved levels), | ||
| 1788 | the result of reordering according to UAX#9 should be this: | ||
| 1789 | |||
| 1790 | efdcghba | ||
| 1791 | |||
| 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) | ||
| 1798 | { | ||
| 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); | ||
| 1804 | } | ||
| 1805 | |||
| 1806 | /* Finally, deliver the next character in the new direction. */ | ||
| 1807 | next_level = bidi_level_of_next_char (bidi_it); | ||
| 1808 | } | ||
| 1809 | |||
| 1810 | if (bidi_it->scan_dir == 1 && bidi_cache_idx) | ||
| 1811 | { | ||
| 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 | ||
| 1814 | discard it. */ | ||
| 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 | ||
| 1821 | positions 1:1. */ | ||
| 1822 | else | ||
| 1823 | bidi_cache_iterator_state (bidi_it, 1); | ||
| 1824 | } | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | /* This is meant to be called from within the debugger, whenever you | ||
| 1828 | wish to examine the cache contents. */ | ||
| 1829 | void | ||
| 1830 | bidi_dump_cached_states (void) | ||
| 1831 | { | ||
| 1832 | int i; | ||
| 1833 | int ndigits = 1; | ||
| 1834 | |||
| 1835 | if (bidi_cache_idx == 0) | ||
| 1836 | { | ||
| 1837 | fprintf (stderr, "The cache is empty.\n"); | ||
| 1838 | return; | ||
| 1839 | } | ||
| 1840 | fprintf (stderr, "Total of %d state%s in cache:\n", | ||
| 1841 | bidi_cache_idx, bidi_cache_idx == 1 ? "" : "s"); | ||
| 1842 | |||
| 1843 | for (i = bidi_cache[bidi_cache_idx - 1].charpos; i > 0; i /= 10) | ||
| 1844 | ndigits++; | ||
| 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); | ||
| 1857 | } | ||
| 1858 | |||
| 1859 | #ifdef TEST_STANDALONE | ||
| 1860 | |||
| 1861 | #include <sys/stat.h> | ||
| 1862 | #include <signal.h> | ||
| 1863 | |||
| 1864 | static char display_line[80]; | ||
| 1865 | static int simulate_display; | ||
| 1866 | static int incr = 1; | ||
| 1867 | |||
| 1868 | void | ||
| 1869 | signal_catcher (int sig) | ||
| 1870 | { | ||
| 1871 | if (simulate_display) | ||
| 1872 | puts (display_line); | ||
| 1873 | else | ||
| 1874 | { | ||
| 1875 | puts ("<"); | ||
| 1876 | fflush (stdout); | ||
| 1877 | } | ||
| 1878 | signal (sig, SIG_DFL); | ||
| 1879 | raise (sig); | ||
| 1880 | } | ||
| 1881 | |||
| 1882 | void | ||
| 1883 | put_line (char *p) | ||
| 1884 | { | ||
| 1885 | if (simulate_display) | ||
| 1886 | { | ||
| 1887 | if (incr == -1) | ||
| 1888 | { | ||
| 1889 | if (p >= display_line) | ||
| 1890 | memset (display_line, ' ', p - display_line + 1); | ||
| 1891 | } | ||
| 1892 | else | ||
| 1893 | *p = '\0'; | ||
| 1894 | fputs (display_line, stdout); | ||
| 1895 | } | ||
| 1896 | fflush (stdout); | ||
| 1897 | } | ||
| 1898 | |||
| 1899 | char * | ||
| 1900 | init_display_direction (bidi_dir_t default_dir, int base_level) | ||
| 1901 | { | ||
| 1902 | char *p; | ||
| 1903 | |||
| 1904 | /* To which display margin should we flush the lines? */ | ||
| 1905 | switch (default_dir) | ||
| 1906 | { | ||
| 1907 | case NEUTRAL_DIR: | ||
| 1908 | if ((base_level & 1) == 0) | ||
| 1909 | { | ||
| 1910 | p = display_line; | ||
| 1911 | incr = 1; | ||
| 1912 | } | ||
| 1913 | else | ||
| 1914 | { | ||
| 1915 | p = display_line + sizeof (display_line) - 1; | ||
| 1916 | *p-- = '\0'; | ||
| 1917 | incr = -1; | ||
| 1918 | } | ||
| 1919 | break; | ||
| 1920 | case L2R: | ||
| 1921 | p = display_line; | ||
| 1922 | incr = 1; | ||
| 1923 | break; | ||
| 1924 | case R2L: | ||
| 1925 | p = display_line + sizeof (display_line) - 1; | ||
| 1926 | *p-- = '\0'; | ||
| 1927 | incr = -1; | ||
| 1928 | break; | ||
| 1929 | default: | ||
| 1930 | abort (); | ||
| 1931 | } | ||
| 1932 | |||
| 1933 | return p; | ||
| 1934 | } | ||
| 1935 | |||
| 1936 | static char * | ||
| 1937 | continuation_line (char *p, int need) | ||
| 1938 | { | ||
| 1939 | if (incr == -1) | ||
| 1940 | { | ||
| 1941 | if (p < display_line + need) | ||
| 1942 | { | ||
| 1943 | *p-- = '/'; | ||
| 1944 | put_line (p); | ||
| 1945 | putc ('\n', stdout); | ||
| 1946 | memset (display_line, '>', sizeof(display_line) - 1); | ||
| 1947 | p = display_line + sizeof (display_line) - 1; | ||
| 1948 | *p-- = '\0'; | ||
| 1949 | } | ||
| 1950 | } | ||
| 1951 | else | ||
| 1952 | { | ||
| 1953 | if (p > display_line + sizeof(display_line) - need - 2) | ||
| 1954 | { | ||
| 1955 | *p++ = '\\'; | ||
| 1956 | put_line (p); | ||
| 1957 | putc ('\n', stdout); | ||
| 1958 | memset (display_line, '<', sizeof(display_line) - 1); | ||
| 1959 | p = display_line; | ||
| 1960 | } | ||
| 1961 | } | ||
| 1962 | |||
| 1963 | return p; | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | int main (int argc, char *argv[]) | ||
| 1967 | { | ||
| 1968 | bidi_dir_t default_dir = NEUTRAL_DIR; | ||
| 1969 | char lots_of_equals[] = "\n===============================================================================\n"; | ||
| 1970 | |||
| 1971 | |||
| 1972 | if (argc > 1 && argv[1][0] == '-') | ||
| 1973 | { | ||
| 1974 | switch (argv[1][1]) | ||
| 1975 | { | ||
| 1976 | case 'R': | ||
| 1977 | default_dir = R2L; | ||
| 1978 | simulate_display = 1; | ||
| 1979 | ++argv; | ||
| 1980 | break; | ||
| 1981 | case 'L': | ||
| 1982 | default_dir = L2R; | ||
| 1983 | simulate_display = 1; | ||
| 1984 | ++argv; | ||
| 1985 | break; | ||
| 1986 | case 'N': | ||
| 1987 | simulate_display = 1; | ||
| 1988 | ++argv; | ||
| 1989 | break; | ||
| 1990 | default: | ||
| 1991 | break; | ||
| 1992 | } | ||
| 1993 | bidi_overriding_paragraph_direction = default_dir; | ||
| 1994 | } | ||
| 1995 | |||
| 1996 | for (argv++; *argv; argv++) | ||
| 1997 | { | ||
| 1998 | FILE *in = fopen (*argv, "rb"); | ||
| 1999 | struct stat stat_buf; | ||
| 2000 | struct bidi_it iterator; | ||
| 2001 | size_t i; | ||
| 2002 | char *p = display_line; | ||
| 2003 | int base_level; | ||
| 2004 | unsigned char *s, *d, *s_end; | ||
| 2005 | |||
| 2006 | if (!in || stat (*argv, &stat_buf)) | ||
| 2007 | { | ||
| 2008 | perror (*argv); | ||
| 2009 | continue; | ||
| 2010 | } | ||
| 2011 | |||
| 2012 | if (stat_buf.st_size > input_buf_size) | ||
| 2013 | { | ||
| 2014 | input_buf = realloc (input_buf, stat_buf.st_size + 1); | ||
| 2015 | if (!input_buf) | ||
| 2016 | { | ||
| 2017 | perror ("realloc input buffer"); | ||
| 2018 | continue; | ||
| 2019 | } | ||
| 2020 | input_buf_size = stat_buf.st_size; | ||
| 2021 | } | ||
| 2022 | if (fread (input_buf, 1, stat_buf.st_size, in) != stat_buf.st_size) | ||
| 2023 | { | ||
| 2024 | perror ("reading input"); | ||
| 2025 | continue; | ||
| 2026 | } | ||
| 2027 | input_buf[stat_buf.st_size] = '\0'; | ||
| 2028 | for (d = s = input_buf, s_end = s + stat_buf.st_size - 1; *s; s++) | ||
| 2029 | { | ||
| 2030 | if (*s != '\r' || s >= s_end || s[1] != '\n') | ||
| 2031 | *d++ = *s; | ||
| 2032 | } | ||
| 2033 | stat_buf.st_size = d - input_buf; | ||
| 2034 | input_buf[stat_buf.st_size] = '\0'; | ||
| 2035 | |||
| 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) | ||
| 2041 | { | ||
| 2042 | p = init_display_direction (default_dir, | ||
| 2043 | iterator.level_stack[0].level); | ||
| 2044 | } | ||
| 2045 | |||
| 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; | ||
| 2049 | |||
| 2050 | for (i = 0; i <= stat_buf.st_size; i++) | ||
| 2051 | { | ||
| 2052 | int c; | ||
| 2053 | |||
| 2054 | bidi_get_next_char_visually (&iterator); | ||
| 2055 | c = iterator.ch; | ||
| 2056 | |||
| 2057 | if (c == '\n' || c == BIDI_EOB) | ||
| 2058 | { | ||
| 2059 | if (simulate_display) | ||
| 2060 | { | ||
| 2061 | put_line (p); | ||
| 2062 | /* FIXME: if -R or -L, need to init paragraph here. */ | ||
| 2063 | } | ||
| 2064 | if (c == BIDI_EOB) | ||
| 2065 | break; | ||
| 2066 | putc (c, stdout); | ||
| 2067 | } | ||
| 2068 | else if (c >= LRE_CHAR && c <= LRM_CHAR) | ||
| 2069 | { | ||
| 2070 | if (simulate_display) | ||
| 2071 | { | ||
| 2072 | p = continuation_line (p, 5); | ||
| 2073 | if (incr == -1) | ||
| 2074 | { | ||
| 2075 | memcpy (p - 4, bidi_name[c], 5); | ||
| 2076 | p -= 5; | ||
| 2077 | } | ||
| 2078 | else | ||
| 2079 | { | ||
| 2080 | memcpy (p, bidi_name[c], 5); | ||
| 2081 | p += 5; | ||
| 2082 | } | ||
| 2083 | } | ||
| 2084 | else | ||
| 2085 | fputs (bidi_name[c], stdout); | ||
| 2086 | } | ||
| 2087 | else if (c < ' ') | ||
| 2088 | { | ||
| 2089 | if (simulate_display) | ||
| 2090 | { | ||
| 2091 | p = continuation_line (p, 2); | ||
| 2092 | if (incr == -1) | ||
| 2093 | { | ||
| 2094 | *p-- = '@' + c; | ||
| 2095 | *p-- = '^'; | ||
| 2096 | } | ||
| 2097 | else | ||
| 2098 | { | ||
| 2099 | *p++ = '^'; | ||
| 2100 | *p++ = '@' + c; | ||
| 2101 | } | ||
| 2102 | } | ||
| 2103 | else | ||
| 2104 | printf ("^%c", (c | 0x40)); | ||
| 2105 | } | ||
| 2106 | else | ||
| 2107 | { | ||
| 2108 | int c1 = (iterator.type == STRONG_R) ? bidi_mirror_char (c) : c; | ||
| 2109 | |||
| 2110 | if (simulate_display) | ||
| 2111 | { | ||
| 2112 | p = continuation_line (p, 1); | ||
| 2113 | *p = c1; | ||
| 2114 | p += incr; | ||
| 2115 | } | ||
| 2116 | else | ||
| 2117 | putc (c1, stdout); | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | if (iterator.ch == '\n') | ||
| 2121 | { | ||
| 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); | ||
| 2127 | } | ||
| 2128 | } | ||
| 2129 | fputs (lots_of_equals, stdout); | ||
| 2130 | fclose (in); | ||
| 2131 | } | ||
| 2132 | return 0; | ||
| 2133 | } | ||
| 2134 | #endif | ||
diff --git a/src/buffer.c b/src/buffer.c index 8657fd8cdff..35e1f4d22ec 100644 --- a/src/buffer.c +++ b/src/buffer.c | |||
| @@ -5186,7 +5186,8 @@ init_buffer_once () | |||
| 5186 | buffer_defaults.truncate_lines = Qnil; | 5186 | buffer_defaults.truncate_lines = Qnil; |
| 5187 | buffer_defaults.word_wrap = Qnil; | 5187 | buffer_defaults.word_wrap = Qnil; |
| 5188 | buffer_defaults.ctl_arrow = Qt; | 5188 | buffer_defaults.ctl_arrow = Qt; |
| 5189 | buffer_defaults.direction_reversed = Qnil; | 5189 | buffer_defaults.enable_bidi_display = Qnil; |
| 5190 | buffer_defaults.orientation_reversed = Qnil; | ||
| 5190 | buffer_defaults.cursor_type = Qt; | 5191 | buffer_defaults.cursor_type = Qt; |
| 5191 | buffer_defaults.extra_line_spacing = Qnil; | 5192 | buffer_defaults.extra_line_spacing = Qnil; |
| 5192 | buffer_defaults.cursor_in_non_selected_windows = Qt; | 5193 | buffer_defaults.cursor_in_non_selected_windows = Qt; |
| @@ -5271,7 +5272,8 @@ init_buffer_once () | |||
| 5271 | XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx; | 5272 | XSETFASTINT (buffer_local_flags.syntax_table, idx); ++idx; |
| 5272 | XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx; | 5273 | XSETFASTINT (buffer_local_flags.cache_long_line_scans, idx); ++idx; |
| 5273 | XSETFASTINT (buffer_local_flags.category_table, idx); ++idx; | 5274 | XSETFASTINT (buffer_local_flags.category_table, idx); ++idx; |
| 5274 | XSETFASTINT (buffer_local_flags.direction_reversed, idx); ++idx; | 5275 | XSETFASTINT (buffer_local_flags.enable_bidi_display, idx); ++idx; |
| 5276 | XSETFASTINT (buffer_local_flags.orientation_reversed, idx); ++idx; | ||
| 5275 | XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx); | 5277 | XSETFASTINT (buffer_local_flags.buffer_file_coding_system, idx); |
| 5276 | /* Make this one a permanent local. */ | 5278 | /* Make this one a permanent local. */ |
| 5277 | buffer_permanent_local_flags[idx++] = 1; | 5279 | buffer_permanent_local_flags[idx++] = 1; |
| @@ -5528,10 +5530,15 @@ This is the same as (default-value 'abbrev-mode). */); | |||
| 5528 | doc: /* Default value of `ctl-arrow' for buffers that do not override it. | 5530 | doc: /* Default value of `ctl-arrow' for buffers that do not override it. |
| 5529 | This is the same as (default-value 'ctl-arrow). */); | 5531 | This is the same as (default-value 'ctl-arrow). */); |
| 5530 | 5532 | ||
| 5531 | DEFVAR_LISP_NOPRO ("default-direction-reversed", | 5533 | DEFVAR_LISP_NOPRO ("default-enable-bidi-display", |
| 5532 | &buffer_defaults.direction_reversed, | 5534 | &buffer_defaults.enable_bidi_display, |
| 5533 | doc: /* Default value of `direction-reversed' for buffers that do not override it. | 5535 | doc: /* *Default value of `enable-bidi-display' for buffers not overriding it. |
| 5534 | This is the same as (default-value 'direction-reversed). */); | 5536 | This is the same as (default-value 'enable-bidi-display). */); |
| 5537 | |||
| 5538 | DEFVAR_LISP_NOPRO ("default-orientation-reversed", | ||
| 5539 | &buffer_defaults.orientation_reversed, | ||
| 5540 | doc: /* *Default value of `orientation-reversed' for buffers that do not override it. | ||
| 5541 | This is the same as (default-value 'orientation-reversed). */); | ||
| 5535 | 5542 | ||
| 5536 | DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters", | 5543 | DEFVAR_LISP_NOPRO ("default-enable-multibyte-characters", |
| 5537 | &buffer_defaults.enable_multibyte_characters, | 5544 | &buffer_defaults.enable_multibyte_characters, |
| @@ -5789,11 +5796,17 @@ The variable `coding-system-for-write', if non-nil, overrides this variable. | |||
| 5789 | 5796 | ||
| 5790 | This variable is never applied to a way of decoding a file while reading it. */); | 5797 | This variable is never applied to a way of decoding a file while reading it. */); |
| 5791 | 5798 | ||
| 5792 | DEFVAR_PER_BUFFER ("direction-reversed", ¤t_buffer->direction_reversed, | 5799 | DEFVAR_PER_BUFFER ("orientation-reversed", |
| 5793 | Qnil, | 5800 | ¤t_buffer->orientation_reversed, Qnil, |
| 5794 | doc: /* *Non-nil means lines in the buffer are displayed right to left. */); | 5801 | doc: /* Non-nil means set beginning of lines at the right end of the window. |
| 5802 | See also the variable `enable-bidi-display'. */); | ||
| 5803 | |||
| 5804 | DEFVAR_PER_BUFFER ("enable-bidi-display", | ||
| 5805 | ¤t_buffer->enable_bidi_display, Qnil, | ||
| 5806 | doc: /*Non-nil means display bidi text in correct visual order. | ||
| 5807 | See also the variable `orientation-reversed'. */); | ||
| 5795 | 5808 | ||
| 5796 | DEFVAR_PER_BUFFER ("truncate-lines", ¤t_buffer->truncate_lines, Qnil, | 5809 | DEFVAR_PER_BUFFER ("truncate-lines", ¤t_buffer->truncate_lines, Qnil, |
| 5797 | doc: /* *Non-nil means do not display continuation lines. | 5810 | doc: /* *Non-nil means do not display continuation lines. |
| 5798 | Instead, give each line of text just one screen line. | 5811 | Instead, give each line of text just one screen line. |
| 5799 | 5812 | ||
diff --git a/src/buffer.h b/src/buffer.h index 9f57a292053..8197ac6328c 100644 --- a/src/buffer.h +++ b/src/buffer.h | |||
| @@ -658,8 +658,10 @@ struct buffer | |||
| 658 | Lisp_Object word_wrap; | 658 | Lisp_Object word_wrap; |
| 659 | /* Non-nil means display ctl chars with uparrow. */ | 659 | /* Non-nil means display ctl chars with uparrow. */ |
| 660 | Lisp_Object ctl_arrow; | 660 | Lisp_Object ctl_arrow; |
| 661 | /* Non-nil means display text from right to left. */ | 661 | /* Non-nil means display bidi text in correct visual order. */ |
| 662 | Lisp_Object direction_reversed; | 662 | Lisp_Object enable_bidi_display; |
| 663 | /* Non-nil means set beginning of lines at the right end of windows. */ | ||
| 664 | Lisp_Object orientation_reversed; | ||
| 663 | /* Non-nil means do selective display; | 665 | /* Non-nil means do selective display; |
| 664 | see doc string in syms_of_buffer (buffer.c) for details. */ | 666 | see doc string in syms_of_buffer (buffer.c) for details. */ |
| 665 | Lisp_Object selective_display; | 667 | Lisp_Object selective_display; |
diff --git a/src/dispextern.h b/src/dispextern.h index f28cae6d8b3..0b3310c8f69 100644 --- a/src/dispextern.h +++ b/src/dispextern.h | |||
| @@ -1702,6 +1702,79 @@ struct face_cache | |||
| 1702 | 1702 | ||
| 1703 | extern int face_change_count; | 1703 | extern int face_change_count; |
| 1704 | 1704 | ||
| 1705 | /* For BIDI */ | ||
| 1706 | #define BIDI_MAXLEVEL 64 | ||
| 1707 | |||
| 1708 | /* Data type for describing the bidirectional character types. */ | ||
| 1709 | typedef enum { | ||
| 1710 | UNKNOWN_BT, | ||
| 1711 | STRONG_L, /* strong left-to-right */ | ||
| 1712 | STRONG_R, /* strong right-to-left */ | ||
| 1713 | STRONG_AL, /* arabic right-to-left letter */ | ||
| 1714 | LRE, /* left-to-right embedding */ | ||
| 1715 | LRO, /* left-to-right override */ | ||
| 1716 | RLE, /* right-to-left embedding */ | ||
| 1717 | RLO, /* right-to-left override */ | ||
| 1718 | PDF, /* pop directional format */ | ||
| 1719 | WEAK_EN, /* european number */ | ||
| 1720 | WEAK_ES, /* european number separator */ | ||
| 1721 | WEAK_ET, /* european number terminator */ | ||
| 1722 | WEAK_AN, /* arabic number */ | ||
| 1723 | WEAK_CS, /* common separator */ | ||
| 1724 | WEAK_NSM, /* non-spacing mark */ | ||
| 1725 | WEAK_BN, /* boundary neutral */ | ||
| 1726 | NEUTRAL_B, /* paragraph separator */ | ||
| 1727 | NEUTRAL_S, /* segment separator */ | ||
| 1728 | NEUTRAL_WS, /* whitespace */ | ||
| 1729 | NEUTRAL_ON /* other neutrals */ | ||
| 1730 | } bidi_type_t; | ||
| 1731 | |||
| 1732 | /* The basic directionality data type. */ | ||
| 1733 | typedef enum { NEUTRAL_DIR, L2R, R2L } bidi_dir_t; | ||
| 1734 | |||
| 1735 | /* Data type for storing information about characters we need to | ||
| 1736 | remember. */ | ||
| 1737 | struct bidi_saved_info { | ||
| 1738 | int bytepos, charpos; /* character's buffer position */ | ||
| 1739 | bidi_type_t type; /* character bidi type */ | ||
| 1740 | bidi_type_t orig_type; /* original type of the character, after W1 */ | ||
| 1741 | bidi_type_t pristine_type; /* type as we found it in the buffer */ | ||
| 1742 | }; | ||
| 1743 | |||
| 1744 | /* Data type for keeping track of saved embedding levels and override | ||
| 1745 | status information. */ | ||
| 1746 | struct bidi_stack { | ||
| 1747 | int level; | ||
| 1748 | bidi_dir_t override; | ||
| 1749 | }; | ||
| 1750 | |||
| 1751 | /* Data type for iterating over bidi text. */ | ||
| 1752 | struct bidi_it { | ||
| 1753 | int bytepos; /* iterator's position in buffer */ | ||
| 1754 | int charpos; | ||
| 1755 | int ch; /* the character itself */ | ||
| 1756 | int ch_len; /* the length of its multibyte sequence */ | ||
| 1757 | bidi_type_t type; /* type of this character */ | ||
| 1758 | bidi_type_t orig_type; /* original type, after overrides and W1 */ | ||
| 1759 | bidi_type_t pristine_type; /* original type, as found in the buffer */ | ||
| 1760 | int resolved_level; /* final resolved level of this character */ | ||
| 1761 | int invalid_levels; /* how many PDFs should we ignore */ | ||
| 1762 | int invalid_rl_levels; /* how many PDFs from RLE/RLO should ignore */ | ||
| 1763 | int new_paragraph; /* if non-zero, a new paragraph begins here */ | ||
| 1764 | int prev_was_pdf; /* if non-zero, prev char was PDF */ | ||
| 1765 | struct bidi_saved_info prev; /* info about the previous character */ | ||
| 1766 | struct bidi_saved_info last_strong; /* last-seen strong directional char */ | ||
| 1767 | struct bidi_saved_info next_for_neutral; /* surrounding characters for... */ | ||
| 1768 | struct bidi_saved_info prev_for_neutral; /* ...resolving neutrals */ | ||
| 1769 | struct bidi_saved_info next_for_ws; /* character after sequence of ws */ | ||
| 1770 | int next_en_pos; /* position of next EN char for ET */ | ||
| 1771 | int ignore_bn_limit; /* position until which we should ignore BNs */ | ||
| 1772 | bidi_dir_t sor; /* direction of start-of-run in effect */ | ||
| 1773 | int scan_dir; /* direction of text scan */ | ||
| 1774 | int stack_idx; /* index of current data on the stack */ | ||
| 1775 | struct bidi_stack level_stack[BIDI_MAXLEVEL]; /* stack of embedding levels */ | ||
| 1776 | }; | ||
| 1777 | |||
| 1705 | 1778 | ||
| 1706 | 1779 | ||
| 1707 | 1780 | ||
| @@ -2233,6 +2306,12 @@ struct it | |||
| 2233 | 2306 | ||
| 2234 | /* Face of the right fringe glyph. */ | 2307 | /* Face of the right fringe glyph. */ |
| 2235 | unsigned right_user_fringe_face_id : FACE_ID_BITS; | 2308 | unsigned right_user_fringe_face_id : FACE_ID_BITS; |
| 2309 | |||
| 2310 | /* 1 means we need bidi processing. */ | ||
| 2311 | int bidi_p; | ||
| 2312 | |||
| 2313 | /* For iterating over bidi text. */ | ||
| 2314 | struct bidi_it bidi_it; | ||
| 2236 | }; | 2315 | }; |
| 2237 | 2316 | ||
| 2238 | 2317 | ||
| @@ -2704,6 +2783,12 @@ extern EMACS_INT tool_bar_button_relief; | |||
| 2704 | Function Prototypes | 2783 | Function Prototypes |
| 2705 | ***********************************************************************/ | 2784 | ***********************************************************************/ |
| 2706 | 2785 | ||
| 2786 | /* Defined in bidi.c */ | ||
| 2787 | |||
| 2788 | extern void bidi_init_it P_ ((int pos, bidi_dir_t dir, | ||
| 2789 | struct bidi_it *bidi_it)); | ||
| 2790 | extern void bidi_get_next_char_visually P_ ((struct bidi_it *bidi_it)); | ||
| 2791 | |||
| 2707 | /* Defined in xdisp.c */ | 2792 | /* Defined in xdisp.c */ |
| 2708 | 2793 | ||
| 2709 | struct glyph_row *row_containing_pos P_ ((struct window *, int, | 2794 | struct glyph_row *row_containing_pos P_ ((struct window *, int, |
diff --git a/src/dispnew.c b/src/dispnew.c index 0039ec0d181..a6984d96a15 100644 --- a/src/dispnew.c +++ b/src/dispnew.c | |||
| @@ -3776,8 +3776,12 @@ direct_output_forward_char (n) | |||
| 3776 | if (!display_completed || cursor_in_echo_area) | 3776 | if (!display_completed || cursor_in_echo_area) |
| 3777 | return 0; | 3777 | return 0; |
| 3778 | 3778 | ||
| 3779 | /* Give up if the buffer's direction is reversed. */ | 3779 | /* Give up if we need bidi display. */ |
| 3780 | if (!NILP (XBUFFER (w->buffer)->direction_reversed)) | 3780 | if (!NILP (XBUFFER (w->buffer)->enable_bidi_display)) |
| 3781 | return 0; | ||
| 3782 | |||
| 3783 | /* Give up if the buffer's orientation is reversed. */ | ||
| 3784 | if (!NILP (XBUFFER (w->buffer)->orientation_reversed)) | ||
| 3781 | return 0; | 3785 | return 0; |
| 3782 | 3786 | ||
| 3783 | /* Can't use direct output if highlighting a region. */ | 3787 | /* Can't use direct output if highlighting a region. */ |
diff --git a/src/xdisp.c b/src/xdisp.c index 435f5dc5334..560ca38e2d0 100644 --- a/src/xdisp.c +++ b/src/xdisp.c | |||
| @@ -2658,6 +2658,9 @@ init_iterator (it, w, charpos, bytepos, row, base_face_id) | |||
| 2658 | /* Are multibyte characters enabled in current_buffer? */ | 2658 | /* Are multibyte characters enabled in current_buffer? */ |
| 2659 | it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters); | 2659 | it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters); |
| 2660 | 2660 | ||
| 2661 | /* Do we need multibyte processing? */ | ||
| 2662 | it->bidi_p = !NILP (current_buffer->enable_bidi_display); | ||
| 2663 | |||
| 2661 | /* Non-zero if we should highlight the region. */ | 2664 | /* Non-zero if we should highlight the region. */ |
| 2662 | highlight_region_p | 2665 | highlight_region_p |
| 2663 | = (!NILP (Vtransient_mark_mode) | 2666 | = (!NILP (Vtransient_mark_mode) |
| @@ -5515,6 +5518,16 @@ reseat_1 (it, pos, set_stop_p) | |||
| 5515 | it->string_from_display_prop_p = 0; | 5518 | it->string_from_display_prop_p = 0; |
| 5516 | it->face_before_selective_p = 0; | 5519 | it->face_before_selective_p = 0; |
| 5517 | 5520 | ||
| 5521 | if (it->bidi_p) | ||
| 5522 | { | ||
| 5523 | bidi_init_it (pos.charpos - 1, L2R, &it->bidi_it); | ||
| 5524 | bidi_get_next_char_visually (&it->bidi_it); | ||
| 5525 | |||
| 5526 | pos.charpos = it->bidi_it.charpos; | ||
| 5527 | pos.bytepos = it->bidi_it.bytepos; | ||
| 5528 | it->current.pos = it->position = pos; | ||
| 5529 | } | ||
| 5530 | |||
| 5518 | if (set_stop_p) | 5531 | if (set_stop_p) |
| 5519 | it->stop_charpos = CHARPOS (pos); | 5532 | it->stop_charpos = CHARPOS (pos); |
| 5520 | } | 5533 | } |
| @@ -6078,8 +6091,18 @@ set_iterator_to_next (it, reseat_p) | |||
| 6078 | else | 6091 | else |
| 6079 | { | 6092 | { |
| 6080 | xassert (it->len != 0); | 6093 | xassert (it->len != 0); |
| 6081 | IT_BYTEPOS (*it) += it->len; | 6094 | |
| 6082 | IT_CHARPOS (*it) += 1; | 6095 | if (! it->bidi_p) |
| 6096 | { | ||
| 6097 | IT_BYTEPOS (*it) += it->len; | ||
| 6098 | IT_CHARPOS (*it) += 1; | ||
| 6099 | } | ||
| 6100 | else | ||
| 6101 | { | ||
| 6102 | bidi_get_next_char_visually (&it->bidi_it); | ||
| 6103 | IT_BYTEPOS (*it) = it->bidi_it.charpos; | ||
| 6104 | IT_CHARPOS (*it) = it->bidi_it.bytepos; | ||
| 6105 | } | ||
| 6083 | xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it))); | 6106 | xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it))); |
| 6084 | } | 6107 | } |
| 6085 | break; | 6108 | break; |