2 * Copyright 1999 Computing Research Labs, New Mexico State University
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
20 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 static char rcsid[] __attribute__ ((unused)) = "$Id: ucpgba.c,v 1.4 1999/11/29 16:41:06 mleisher Exp $";
26 static char rcsid[] = "$Id: ucpgba.c,v 1.4 1999/11/29 16:41:06 mleisher Exp $";
39 * These macros are used while reordering of RTL runs of text for the
40 * special case of non-spacing characters being in runs of weakly
41 * directional text. They check for weak and non-spacing, and digits and
44 #define ISWEAKSPECIAL(cc) ucisprop(cc, UC_EN|UC_ES|UC_MN, UC_ET|UC_AN|UC_CS)
45 #define ISDIGITSPECIAL(cc) ucisprop(cc, UC_ND|UC_MN, 0)
48 * These macros are used while breaking a string into runs of text in
49 * different directions. Descriptions:
51 * ISLTR_LTR - Test for members of an LTR run in an LTR context. This looks
52 * for characters with ltr, non-spacing, weak, and neutral
55 * ISRTL_RTL - Test for members of an RTL run in an RTL context. This looks
56 * for characters with rtl, non-spacing, weak, and neutral
59 * ISRTL_NEUTRAL - Test for RTL or neutral characters.
61 * ISWEAK_NEUTRAL - Test for weak or neutral characters.
63 #define ISLTR_LTR(cc) ucisprop(cc, UC_L|UC_MN|UC_EN|UC_ES,\
64 UC_ET|UC_AN|UC_CS|UC_B|UC_S|UC_WS|UC_ON)
66 #define ISRTL_RTL(cc) ucisprop(cc, UC_R|UC_MN|UC_EN|UC_ES,\
67 UC_ET|UC_AN|UC_CS|UC_B|UC_S|UC_WS|UC_ON)
69 #define ISRTL_NEUTRAL(cc) ucisprop(cc, UC_R, UC_B|UC_S|UC_WS|UC_ON)
70 #define ISWEAK_NEUTRAL(cc) ucisprop(cc, UC_EN|UC_ES, \
71 UC_B|UC_S|UC_WS|UC_ON|UC_ET|UC_AN|UC_CS)
74 * This table is temporarily hard-coded here until it can be constructed
75 * automatically somehow.
77 static unsigned long _symmetric_pairs[] = {
78 0x0028, 0x0029, 0x0029, 0x0028, 0x003C, 0x003E, 0x003E, 0x003C,
79 0x005B, 0x005D, 0x005D, 0x005B, 0x007B, 0x007D, 0x007D, 0x007B,
80 0x2045, 0x2046, 0x2046, 0x2045, 0x207D, 0x207E, 0x207E, 0x207D,
81 0x208D, 0x208E, 0x208E, 0x208D, 0x3008, 0x3009, 0x3009, 0x3008,
82 0x300A, 0x300B, 0x300B, 0x300A, 0x300C, 0x300D, 0x300D, 0x300C,
83 0x300E, 0x300F, 0x300F, 0x300E, 0x3010, 0x3011, 0x3011, 0x3010,
84 0x3014, 0x3015, 0x3015, 0x3014, 0x3016, 0x3017, 0x3017, 0x3016,
85 0x3018, 0x3019, 0x3019, 0x3018, 0x301A, 0x301B, 0x301B, 0x301A,
86 0xFD3E, 0xFD3F, 0xFD3F, 0xFD3E, 0xFE59, 0xFE5A, 0xFE5A, 0xFE59,
87 0xFE5B, 0xFE5C, 0xFE5C, 0xFE5B, 0xFE5D, 0xFE5E, 0xFE5E, 0xFE5D,
88 0xFF08, 0xFF09, 0xFF09, 0xFF08, 0xFF3B, 0xFF3D, 0xFF3D, 0xFF3B,
89 0xFF5B, 0xFF5D, 0xFF5D, 0xFF5B, 0xFF62, 0xFF63, 0xFF63, 0xFF62,
92 static int _symmetric_pairs_size =
93 sizeof(_symmetric_pairs)/sizeof(_symmetric_pairs[0]);
96 * This routine looks up the other form of a symmetric pair.
99 _ucsymmetric_pair(unsigned long c)
103 for (i = 0; i < _symmetric_pairs_size; i += 2) {
104 if (_symmetric_pairs[i] == c)
105 return _symmetric_pairs[i+1];
111 * This routine creates a new run, copies the text into it, links it into the
112 * logical text order chain and returns it to the caller to be linked into
113 * the visual text order chain.
116 _add_run(ucstring_t *str, unsigned long *src,
117 unsigned long start, unsigned long end, int direction)
122 run = (ucrun_t *) malloc(sizeof(ucrun_t));
123 run->visual_next = run->visual_prev = 0;
124 run->direction = direction;
128 run->chars = (unsigned long *)
129 malloc(sizeof(unsigned long) * ((end - start) << 1));
130 run->positions = run->chars + (end - start);
136 if (direction == UCPGBA_RTL) {
138 * Copy the source text into the run in reverse order and select
139 * replacements for the pairwise punctuation and the <> characters.
141 for (i = 0, t = end - 1; start < end; start++, t--, i++) {
142 run->positions[i] = t;
143 if (ucissymmetric(src[t]) || src[t] == '<' || src[t] == '>')
144 run->chars[i] = _ucsymmetric_pair(src[t]);
146 run->chars[i] = src[t];
150 * Copy the source text into the run directly.
152 for (i = start; i < end; i++) {
153 run->positions[i - start] = i;
154 run->chars[i - start] = src[i];
159 * Add the run to the logical list for cursor traversal.
161 if (str->logical_first == 0)
162 str->logical_first = str->logical_last = run;
164 run->logical_prev = str->logical_last;
165 str->logical_last->logical_next = run;
166 str->logical_last = run;
173 _ucadd_rtl_segment(ucstring_t *str, unsigned long *source, unsigned long start,
180 * This is used to splice runs into strings with overall LTR direction.
181 * The `lrun' variable will never be NULL because at least one LTR run was
182 * added before this RTL run.
184 lrun = str->visual_last;
186 for (e = s = start; s < end;) {
187 for (; e < end && ISRTL_NEUTRAL(source[e]); e++) ;
190 run = _add_run(str, source, s, e, UCPGBA_RTL);
193 * Add the run to the visual list for cursor traversal.
195 if (str->visual_first != 0) {
196 if (str->direction == UCPGBA_LTR) {
197 run->visual_prev = lrun;
198 run->visual_next = lrun->visual_next;
199 if (lrun->visual_next != 0)
200 lrun->visual_next->visual_prev = run;
201 lrun->visual_next = run;
202 if (lrun == str->visual_last)
203 str->visual_last = run;
205 run->visual_next = str->visual_first;
206 str->visual_first->visual_prev = run;
207 str->visual_first = run;
210 str->visual_first = str->visual_last = run;
214 * Now handle the weak sequences such that multiple non-digit groups
215 * are kept together appropriately and added as RTL sequences.
217 for (s = e; e < end && ISWEAKSPECIAL(source[e]); e++) {
218 if (!ISDIGITSPECIAL(source[e]) &&
219 (e + 1 == end || !ISDIGITSPECIAL(source[e + 1])))
224 run = _add_run(str, source, s, e, UCPGBA_LTR);
227 * Add the run to the visual list for cursor traversal.
229 if (str->visual_first != 0) {
230 if (str->direction == UCPGBA_LTR) {
231 run->visual_prev = lrun;
232 run->visual_next = lrun->visual_next;
233 if (lrun->visual_next != 0)
234 lrun->visual_next->visual_prev = run;
235 lrun->visual_next = run;
236 if (lrun == str->visual_last)
237 str->visual_last = run;
239 run->visual_next = str->visual_first;
240 str->visual_first->visual_prev = run;
241 str->visual_first = run;
244 str->visual_first = str->visual_last = run;
248 * Collect all weak non-digit sequences for an RTL segment. These
249 * will appear as part of the next RTL segment or will be added as
250 * an RTL segment by themselves.
252 for (s = e; e < end && ucisweak(source[e]) && !ucisdigit(source[e]);
257 * Capture any weak non-digit sequences that occur at the end of the RTL
261 run = _add_run(str, source, s, e, UCPGBA_RTL);
264 * Add the run to the visual list for cursor traversal.
266 if (str->visual_first != 0) {
267 if (str->direction == UCPGBA_LTR) {
268 run->visual_prev = lrun;
269 run->visual_next = lrun->visual_next;
270 if (lrun->visual_next != 0)
271 lrun->visual_next->visual_prev = run;
272 lrun->visual_next = run;
273 if (lrun == str->visual_last)
274 str->visual_last = run;
276 run->visual_next = str->visual_first;
277 str->visual_first->visual_prev = run;
278 str->visual_first = run;
281 str->visual_first = str->visual_last = run;
286 _ucadd_ltr_segment(ucstring_t *str, unsigned long *source, unsigned long start,
291 run = _add_run(str, source, start, end, UCPGBA_LTR);
294 * Add the run to the visual list for cursor traversal.
296 if (str->visual_first != 0) {
297 if (str->direction == UCPGBA_LTR) {
298 run->visual_prev = str->visual_last;
299 str->visual_last->visual_next = run;
300 str->visual_last = run;
302 run->visual_next = str->visual_first;
303 str->visual_first->visual_prev = run;
304 str->visual_first = run;
307 str->visual_first = str->visual_last = run;
311 ucstring_create(unsigned long *source, unsigned long start, unsigned long end,
312 int default_direction, int cursor_motion)
318 str = (ucstring_t *) malloc(sizeof(ucstring_t));
321 * Set the initial values.
323 str->cursor_motion = cursor_motion;
324 str->logical_first = str->logical_last = 0;
325 str->visual_first = str->visual_last = str->cursor = 0;
326 str->source = source;
331 * If the length of the string is 0, then just return it at this point.
337 * This flag indicates whether the collection loop for RTL is called
338 * before the LTR loop the first time.
343 * Look for the first character in the string that has strong
346 for (s = start; s < end && !ucisstrong(source[s]); s++) ;
350 * If the string contains no characters with strong directionality, use
351 * the default direction.
353 str->direction = default_direction;
355 str->direction = ucisrtl(source[s]) ? UCPGBA_RTL : UCPGBA_LTR;
357 if (str->direction == UCPGBA_RTL)
359 * Set the flag that causes the RTL collection loop to run first.
364 * This loop now separates the string into runs based on directionality.
366 for (s = e = 0; s < end; s = e) {
369 * Determine the next run of LTR text.
372 while (e < end && ISLTR_LTR(source[e]))
374 if (str->direction != UCPGBA_LTR) {
375 while (e > s && ISWEAK_NEUTRAL(source[e - 1]))
380 * Add the LTR segment to the string.
383 _ucadd_ltr_segment(str, source, s, e);
387 * Determine the next run of RTL text.
390 while (e < end && ISRTL_RTL(source[e]))
392 if (str->direction != UCPGBA_RTL) {
393 while (e > s && ISWEAK_NEUTRAL(source[e - 1]))
398 * Add the RTL segment to the string.
401 _ucadd_rtl_segment(str, source, s, e);
404 * Clear the flag that allowed the RTL collection loop to run first
405 * for strings with overall RTL directionality.
411 * Set up the initial cursor run.
413 str->cursor = str->logical_first;
415 str->cursor->cursor = (str->cursor->direction == UCPGBA_RTL) ?
416 str->cursor->end - str->cursor->start : 0;
422 ucstring_free(ucstring_t *s)
429 for (l = 0, r = s->visual_first; r != 0; r = r->visual_next) {
430 if (r->end > r->start)
431 free((char *) r->chars);
443 ucstring_set_cursor_motion(ucstring_t *str, int cursor_motion)
450 n = str->cursor_motion;
451 str->cursor_motion = cursor_motion;
456 _ucstring_visual_cursor_right(ucstring_t *str, int count)
465 cursor = str->cursor;
467 size = cursor->end - cursor->start;
468 if ((cursor->direction == UCPGBA_RTL && cursor->cursor + 1 == size) ||
469 cursor->cursor + 1 > size) {
471 * If the next run is NULL, then the cursor is already on the
472 * far right end already.
474 if (cursor->visual_next == 0)
476 * If movement occured, then report it.
478 return (cnt != count);
481 * Move to the next run.
483 str->cursor = cursor = cursor->visual_next;
484 cursor->cursor = (cursor->direction == UCPGBA_RTL) ? -1 : 0;
485 size = cursor->end - cursor->start;
494 _ucstring_logical_cursor_right(ucstring_t *str, int count)
503 cursor = str->cursor;
505 size = cursor->end - cursor->start;
506 if (str->direction == UCPGBA_RTL) {
507 if (cursor->direction == UCPGBA_RTL) {
508 if (cursor->cursor + 1 == size) {
509 if (cursor == str->logical_first)
511 * Already at the beginning of the string.
513 return (cnt != count);
515 str->cursor = cursor = cursor->logical_prev;
516 size = cursor->end - cursor->start;
517 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
522 if (cursor->cursor == 0) {
523 if (cursor == str->logical_first)
525 * At the beginning of the string already.
527 return (cnt != count);
529 str->cursor = cursor = cursor->logical_prev;
530 size = cursor->end - cursor->start;
531 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
537 if (cursor->direction == UCPGBA_RTL) {
538 if (cursor->cursor == 0) {
539 if (cursor == str->logical_last)
541 * Already at the end of the string.
543 return (cnt != count);
545 str->cursor = cursor = cursor->logical_next;
546 size = cursor->end - cursor->start;
547 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
552 if (cursor->cursor + 1 > size) {
553 if (cursor == str->logical_last)
555 * Already at the end of the string.
557 return (cnt != count);
559 str->cursor = cursor = cursor->logical_next;
560 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
572 ucstring_cursor_right(ucstring_t *str, int count)
576 return (str->cursor_motion == UCPGBA_CURSOR_VISUAL) ?
577 _ucstring_visual_cursor_right(str, count) :
578 _ucstring_logical_cursor_right(str, count);
582 _ucstring_visual_cursor_left(ucstring_t *str, int count)
591 cursor = str->cursor;
593 size = cursor->end - cursor->start;
594 if ((cursor->direction == UCPGBA_LTR && cursor->cursor == 0) ||
595 cursor->cursor - 1 < -1) {
597 * If the preceding run is NULL, then the cursor is already on the
598 * far left end already.
600 if (cursor->visual_prev == 0)
602 * If movement occured, then report it.
604 return (cnt != count);
607 * Move to the previous run.
609 str->cursor = cursor = cursor->visual_prev;
610 size = cursor->end - cursor->start;
611 cursor->cursor = (cursor->direction == UCPGBA_RTL) ?
621 _ucstring_logical_cursor_left(ucstring_t *str, int count)
630 cursor = str->cursor;
632 size = cursor->end - cursor->start;
633 if (str->direction == UCPGBA_RTL) {
634 if (cursor->direction == UCPGBA_RTL) {
635 if (cursor->cursor == -1) {
636 if (cursor == str->logical_last)
638 * Already at the end of the string.
640 return (cnt != count);
642 str->cursor = cursor = cursor->logical_next;
643 size = cursor->end - cursor->start;
644 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
649 if (cursor->cursor + 1 > size) {
650 if (cursor == str->logical_last)
652 * At the end of the string already.
654 return (cnt != count);
656 str->cursor = cursor = cursor->logical_next;
657 size = cursor->end - cursor->start;
658 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
664 if (cursor->direction == UCPGBA_RTL) {
665 if (cursor->cursor + 1 == size) {
666 if (cursor == str->logical_first)
668 * Already at the beginning of the string.
670 return (cnt != count);
672 str->cursor = cursor = cursor->logical_prev;
673 size = cursor->end - cursor->start;
674 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
679 if (cursor->cursor == 0) {
680 if (cursor == str->logical_first)
682 * Already at the beginning of the string.
684 return (cnt != count);
686 str->cursor = cursor = cursor->logical_prev;
687 cursor->cursor = (cursor->direction == UCPGBA_LTR) ?
699 ucstring_cursor_left(ucstring_t *str, int count)
703 return (str->cursor_motion == UCPGBA_CURSOR_VISUAL) ?
704 _ucstring_visual_cursor_left(str, count) :
705 _ucstring_logical_cursor_left(str, count);
709 ucstring_cursor_info(ucstring_t *str, int *direction, unsigned long *position)
715 if (str == 0 || direction == 0 || position == 0)
718 cursor = str->cursor;
720 *direction = cursor->direction;
723 size = cursor->end - cursor->start;
726 *position = (cursor->direction == UCPGBA_RTL) ?
727 cursor->start : cursor->positions[c - 1];
729 *position = (cursor->direction == UCPGBA_RTL) ?
730 cursor->end : cursor->start;
732 *position = cursor->positions[c];