Debian Codegraph @

texk/upmendex/sort.c

raw
1 #include "mendex.h"
2 #include "qsort.h"
3
4 #include "exkana.h"
5 #include "exvar.h"
6 #include "exhanzi.h"
7
8 #define RULEBUFSIZE 29652+STYBUFSIZE
9 /*
10 length of collation rule in ICU 76.1
11
12 icu_locale length
13 ja 6410
14 ja@collation=unihan 61
15 ko 12577
16 ko@collation=unihan 51
17 ko@collation=search 782
18 zh (pinin) 27369
19 zh@collation=unihan 82
20 zh@collation=stroke 29651
21 zh@collation=zhuyin 28880
22 */
23
24 int sym,nmbr,ltn,kana,hngl,hnz,cyr,grk,arab,hbrw,brhm[NUM_BRAHMIC];
25
26 static int wcomp(const void *p, const void *q);
27 static int pcomp(const void *p, const void *q);
28 static int ordering(UChar *c);
29 static int get_charset_juncture(UChar *str);
30 static int unescape(const unsigned char *src, UChar *dist);
31
32 /* init ICU collator */
33 void init_icu_collator()
34 {
35 UErrorCode status;
36 UParseError parse_error;
37 UChar rules[RULEBUFSIZE] = {'\0'};
38 int i;
39 int32_t len;
40
41 status = U_ZERO_ERROR;
42 if (strlen(icu_rules)>0) {
43 if (strcmp(icu_locale,"root")!=0) {
44 icu_collator = ucol_open(icu_locale, &status);
45 if (U_FAILURE(status)) {
46 verb_printf(efp, "\n[ICU] Collator creation failed.: %s\n", u_errorName(status));
47 exit(254);
48 }
49 len = ucol_getRulesEx(icu_collator, UCOL_TAILORING_ONLY, rules, RULEBUFSIZE);
50 if (u_strlen(rules)<len) {
51 verb_printf(efp, "\n[ICU] Failed to extract collation rules by locale (%s). Need buffer size %d.\n",
52 icu_locale, len);
53 exit(254);
54 }
55 len = RULEBUFSIZE - u_strlen(rules);
56 if (len<STYBUFSIZE) {
57 warn_printf(efp, "\nWarning: [ICU] Remaining buffer size (%d) is small for custom collation rules with rules by locale (%s).\n",
58 len, icu_locale);
59 }
60 ucol_close(icu_collator);
61 }
62 unescape((unsigned char *)icu_rules, rules);
63 status = U_ZERO_ERROR;
64 icu_collator = ucol_openRules(rules, -1, UCOL_OFF, UCOL_TERTIARY, &parse_error, &status);
65 } else
66 icu_collator = ucol_open(icu_locale, &status);
67 if (U_FAILURE(status)) {
68 verb_printf(efp, "\n[ICU] Collator creation failed.: %s\n", u_errorName(status));
69 exit(254);
70 }
71 if (status == U_USING_DEFAULT_WARNING) {
72 warn_printf(efp, "\nWarning: [ICU] U_USING_DEFAULT_WARNING for locale %s\n",
73 icu_locale);
74 }
75 if (status == U_USING_FALLBACK_WARNING) {
76 warn_printf(efp, "\nWarning: [ICU] U_USING_FALLBACK_WARNING for locale %s\n",
77 icu_locale);
78 }
79 for (i=0;i<UCOL_ATTRIBUTE_COUNT;i++) {
80 if (icu_attributes[i]!=UCOL_DEFAULT) {
81 status = U_ZERO_ERROR;
82 ucol_setAttribute(icu_collator, i, icu_attributes[i], &status);
83 }
84 if (U_FAILURE(status)) {
85 warn_printf(efp, "\nWarning: [ICU] Failed to set attribute (%d): %s\n",
86 i, u_errorName(status));
87 }
88 }
89 }
90
91 /* sort index */
92 void wsort(struct index *ind, int num)
93 {
94 int i,j,order;
95
96 for (order=1,i=0;;i++) {
97 switch (character_order[i]) {
98 case '\0':
99 goto BREAK;
100 break;
101
102 case 'S':
103 sym=order++;
104 break;
105
106 case 'N':
107 nmbr=order++;
108 break;
109
110 case 'L':
111 ltn=order++;
112 break;
113
114 case 'J':
115 kana=order++;
116 break;
117
118 case 'K':
119 hngl=order++;
120 break;
121
122 case 'H':
123 hnz=order++;
124 break;
125
126 case 'C':
127 cyr=order++;
128 break;
129
130 case 'G':
131 grk=order++;
132 break;
133
134 case 'a':
135 arab=order++;
136 break;
137
138 case 'h':
139 hbrw=order++;
140 break;
141
142 case 'D': brhm[BR_DEVA]=order++; break;
143 case 'b': brhm[BR_BENG]=order++; break;
144 case 'p': brhm[BR_GURU]=order++; break;
145 case 'g': brhm[BR_GUJR]=order++; break;
146 case 'o': brhm[BR_ORYA]=order++; break;
147 case 't': brhm[BR_TAML]=order++; break;
148 case 'u': brhm[BR_TELU]=order++; break;
149 case 'k': brhm[BR_KNDA]=order++; break;
150 case 'm': brhm[BR_MLYM]=order++; break;
151 case 's': brhm[BR_SINH]=order++; break;
152 case 'T': brhm[BR_THAI]=order++; break;
153 case 'l': brhm[BR_LAO ]=order++; break;
154
155 case '@':
156 sym=nmbr=ltn=kana=hngl=hnz=cyr=grk=arab=hbrw=order;
157 for(j=0;j<NUM_BRAHMIC;j++) brhm[j]=order;
158 order++;
159 break;
160
161 default:
162 verb_printf(efp,"\nWarning: Illegal input for character_order (%c).",character_order[i]);
163 break;
164 }
165 }
166
167 BREAK:
168
169 if (sym==0) sym=order++;
170 if (nmbr==0) nmbr=order++;
171 if (ltn==0) ltn=order++;
172 if (kana==0) kana=order++;
173 if (hngl==0) hngl=order++;
174 if (hnz==0) hnz=order++;
175 if (cyr==0) cyr=order++;
176 if (grk==0) grk=order++;
177 if (arab==0) arab=order++;
178 if (hbrw==0) hbrw=order++;
179 for(j=0;j<NUM_BRAHMIC;j++)
180 if (brhm[j]==0) brhm[j]=order++;
181
182 qsort(ind,num,sizeof(struct index),wcomp);
183 }
184
185 /* compare for sorting index */
186 static int wcomp(const void *p, const void *q)
187 {
188 int i, j, len1, len2, cmp;
189 const struct index *index1 = p, *index2 = q;
190 UChar ch1, ch2;
191 UChar *str1, *str2;
192 UCollationResult col_result;
193
194 scount++;
195
196 for (j=0;j<MAXDEPTH;j++) {
197
198 /* check level */
199 if (((*index1).words==j)&&((*index2).words!=j)) return -1;
200 else if (((*index1).words!=j)&&((*index2).words==j)) return 1;
201
202 for(i=0;;i+=len1) {
203
204 str1=&((*index1).dic[j][i]);
205 str2=&((*index2).dic[j][i]);
206 ch1=(*index1).dic[j][i];
207 ch2=(*index2).dic[j][i];
208
209 /* even */
210 if ((ch1==L'\0')&&(ch2==L'\0')) break;
211
212 /* index1 is shorter */
213 if (ch1==L'\0') return -1;
214
215 /* index2 is shorter */
216 if (ch2==L'\0') return 1;
217
218 /* priority */
219 if ((priority!=0)&&(i>0)) {
220 if ((is_jpn_kana(str1))&&(!is_jpn_kana(str2)))
221 return -1;
222
223 if ((is_jpn_kana(str2))&&(!is_jpn_kana(str1)))
224 return 1;
225 }
226
227 /* compare group */
228 if (ordering(str1)<ordering(str2))
229 return -1;
230
231 if (ordering(str1)>ordering(str2))
232 return 1;
233
234 /* simple compare */
235 if (priority==0) len1=len2=-1;
236 else {
237 len1=get_charset_juncture(str1);
238 len2=get_charset_juncture(str2);
239 }
240 col_result = ucol_strcoll(icu_collator, str1, len1, str2, len2);
241 if (col_result == UCOL_LESS) return -1;
242 else if (col_result == UCOL_GREATER) return 1;
243
244 if (priority==0) break;
245 }
246
247 /* compare index */
248 str1=&((*index1).idx[j][0]);
249 str2=&((*index2).idx[j][0]);
250 col_result = ucol_strcoll(icu_collator, str1, -1, str2, -1);
251 if (col_result == UCOL_LESS) return -1;
252 else if (col_result == UCOL_GREATER) return 1;
253 cmp=u_strcmp(str1,str2);
254 if (cmp<0) return -1;
255 else if (cmp>0) return 1;
256 }
257 return 0;
258 }
259
260 /* sort page */
261 void pagesort(struct index *ind, int num)
262 {
263 int i,j;
264 struct page *buff;
265
266 for (i=0;i<num;i++) {
267 if (ind[i].num==0) continue;
268
269 buff=xmalloc(sizeof(struct page)*(ind[i].num+1));
270 for (j=0;j<=ind[i].num;j++) {
271 buff[j]=ind[i].p[j];
272 }
273 qsort(buff,ind[i].num+1,sizeof(struct page),pcomp);
274 for (j=0;j<=ind[i].num;j++) {
275 ind[i].p[j]=buff[j];
276 }
277 free(buff);
278 }
279 }
280
281 /* compare for sorting page */
282 static int pcomp(const void *p, const void *q)
283 {
284 int i,j,cc=0,num1,num2;
285 char buff[16],*p0,*p1;
286 const struct page *page1 = p, *page2 = q;
287
288 scount++;
289
290 for (i=0;i<PAGE_COMPOSIT_DEPTH;i++) {
291 if ((page1->attr[i]<0)&&(page2->attr[i]<0)) return 0;
292 else if ((page1->attr[i]<0)&&(page2->attr[i]>=0)) return -1;
293 else if ((page2->attr[i]<0)&&(page1->attr[i]>=0)) return 1;
294
295 if (page1->attr[i]>page2->attr[i]) return 1;
296 if (page1->attr[i]<page2->attr[i]) return -1;
297
298 p0=&page1->page[cc];
299 p1=strstr(p0, page_compositor);
300 j=p1 ? p1-p0 : strlen(p0);
301 strncpy(buff,p0,j);
302 buff[j]='\0';
303 num1=pnumconv(buff,page1->attr[i]);
304
305 p0=&page2->page[cc];
306 p1=strstr(p0, page_compositor);
307 j=p1 ? p1-p0 : strlen(p0);
308 strncpy(buff,p0,j);
309 buff[j]='\0';
310 num2=pnumconv(buff,page2->attr[i]);
311
312 if (num1>num2) return 1;
313 else if (num1<num2) return -1;
314
315 if (page1->enc[0]=='(' || page2->enc[0]==')') return -1;
316 if (page1->enc[0]==')' || page2->enc[0]=='(') return 1;
317
318 if (p1) cc+=j+strlen(page_compositor);
319 else return 0;
320 }
321
322 return 0;
323 }
324
325 static int ordering(UChar *c)
326 {
327 int scr;
328
329 if (*c<0x20) return sym; /* control */
330 else if (*c<0x7F) {
331 if (is_latin(c)) return ltn;
332 else if (is_numeric(c)) return nmbr;
333 else return sym;
334 }
335 else if (*c<0xA0) return sym; /* control */
336 else {
337 if (is_latin(c)) return ltn;
338 else if (is_jpn_kana(c)) return kana;
339 else if (is_kor_hngl(c)) return hngl;
340 else if (is_hanzi(c)) return hnz;
341 else if (is_cyrillic(c)) return cyr;
342 else if (is_greek(c)) return grk;
343 else if (is_numeric(c)) return nmbr;
344 else if (is_thai(c)) return brhm[BR_THAI];
345 else if (is_lao(c)) return brhm[BR_LAO];
346 else if (is_arabic(c)) return arab;
347 else if (is_hebrew(c)) return hbrw;
348 else if ((scr=is_brahmic(c))>0)
349 return brhm[scr-CH_DEVANAGARI];
350 else return sym;
351 }
352 }
353
354 int charset(UChar *c)
355 {
356 int scr;
357
358 if (*c<0x20) return CH_UNKNOWN; /* control */
359 else if (*c<0x7F) {
360 if (is_latin(c)) return CH_LATIN;
361 else if (is_numeric(c)) return CH_NUMERIC;
362 else return CH_SYMBOL;
363 }
364 else if (*c<0xA0) return CH_UNKNOWN; /* control */
365 else {
366 if (is_latin(c)) return CH_LATIN;
367 else if (is_jpn_kana(c)) return CH_KANA;
368 else if (is_kor_hngl(c)) return CH_HANGUL;
369 else if (is_hanzi(c)) return CH_HANZI;
370 else if (is_cyrillic(c)) return CH_CYRILLIC;
371 else if (is_greek(c)) return CH_GREEK;
372 else if (is_numeric(c)) return CH_NUMERIC;
373 else if (is_thai(c)) return CH_THAI;
374 else if (is_lao(c)) return CH_LAO;
375 else if (is_arabic(c)) return CH_ARABIC;
376 else if (is_hebrew(c)) return CH_HEBREW;
377 else if ((scr=is_brahmic(c))>0) return scr;
378 else return CH_SYMBOL;
379 }
380 }
381
382 static int get_charset_juncture(UChar *str)
383 {
384 int k, l, len, chset0, chset_k, chset_l;
385
386 chset0=CH_UNKNOWN;
387 for(k=0;;k++) {
388 if (str[k]==L'\0') {
389 len=k;
390 return len;
391 }
392 if (k==0) continue;
393 if (k>0 && is_surrogate_pair(&str[k-1])) continue;
394 if (k>1 && is_surrogate_pair(&str[k-2])) l = k-2;
395 else l = k-1;
396 chset_l=charset(&str[l]);
397 chset_k=charset(&str[k]);
398 if (chset0==CH_UNKNOWN && is_any_script(chset_l)) {
399 chset0=chset_l;
400 }
401 if (chset0!=CH_UNKNOWN && is_any_script(chset_k)) {
402 if (chset0!=chset_k) {
403 len=k;
404 return len;
405 }
406 }
407 }
408 }
409
410 static int unescape(const unsigned char *src, UChar *dest)
411 {
412 int i,j,k,ret;
413 char tmp[STYBUFSIZE];
414 UErrorCode status;
415
416 for (i=j=0;i<STYBUFSIZE;i++) {
417 if (src[i]=='\0') {
418 return i;
419 }
420 else if (src[i]< 0x80 && (src[i+1]>=0x80 || src[i+1]=='\0')) {
421 strncpy(tmp,(char *)&src[j],i-j+1);
422 tmp[i-j+1]='\0';
423 k=u_strlen(dest);
424 ret=u_unescape(tmp, &dest[k], RULEBUFSIZE-k);
425 if (ret==0) {
426 verb_printf(efp, "\n[ICU] Escape sequence in input seems malformed.\n");
427 exit(254);
428 }
429 j=i+1;
430 }
431 else if (src[i]>=0x80 && (src[i+1]< 0x80 || src[i+1]=='\0')) {
432 strncpy(tmp,(char *)&src[j],i-j+1);
433 tmp[i-j+1]='\0';
434 k=u_strlen(dest);
435 status=U_ZERO_ERROR;
436 u_strFromUTF8(&dest[k], RULEBUFSIZE-k, NULL, tmp, -1, &status);
437 if (U_FAILURE(status)) {
438 verb_printf(efp, "\n[ICU] Input string seems malformed.: %s\n", u_errorName(status));
439 exit(254);
440 }
441 j=i+1;
442 }
443 }
444 return -1;
445 }
446
447 int is_latin(UChar *c)
448 {
449 if (( *c>=L'A' && *c<=L'Z' )||( *c>=L'a' && *c<=L'z' )) return 1;
450 else if ( *c==0x00AA || *c==0x00BA ) return 1; /* Latin-1 Supplement */
451 else if ( *c>=0x00C0 && *c<=0x00D6 ) return 1;
452 else if ( *c>=0x00D8 && *c<=0x00F6 ) return 1;
453 else if ( *c>=0x00F8 && *c<=0x00FF ) return 1;
454 else if ( *c>=0x0100 && *c<=0x024F ) return 1; /* Latin Extended-A,B */
455 else if ( *c>=0x0250 && *c<=0x02AF ) return 1; /* IPA Extensions */
456 else if ( *c>=0x2C60 && *c<=0x2C7F ) return 1; /* Latin Extended-C */
457 else if ( *c>=0xA720 && *c<=0xA7FF ) return 1; /* Latin Extended-D */
458 else if ( *c>=0xAB30 && *c<=0xAB6F ) return 1; /* Latin Extended-E */
459 else if ( *c>=0x1E00 && *c<=0x1EFF ) return 1; /* Latin Extended Additional */
460 else if ( *c>=0xFB00 && *c<=0xFB06 ) return 1; /* Latin ligatures */
461 else if ( *c>=0xFF21 && *c<=0xFF3A ) return 1; /* Fullwidth Latin Capital Letter */
462 else if ( *c>=0xFF41 && *c<=0xFF5A ) return 1; /* Fullwidth Latin Small Letter */
463 /* Property of followings is "Common, So (other symbol)", but seem to be treated as Latin by ICU collator */
464 else if ( *c>=0x24B6 /* CIRCLED LATIN CAPITAL LETTER */
465 && *c<=0x24E9 ) return 1; /* CIRCLED LATIN SMALL LETTER */
466
467 if (is_surrogate_pair(c)) {
468 UChar32 c32;
469 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
470 if ( c32>=0x10780 && c32<=0x107BF ) return 2; /* Latin Extended-F */
471 else if ( c32>=0x1DF00 && c32<=0x1DFFF ) return 2; /* Latin Extended-G */
472 }
473 return 0;
474 }
475
476 int is_numeric(UChar *c)
477 {
478 UChar32 c32;
479
480 if ( *c>=L'0' && *c<=L'9' ) return 1;
481 else if ( *c>=0xFF10 && *c<=0xFF19 ) return 1; /* Fullwidth Digit */
482 /* followings do not seem to be treated as numbers by ICU collator though charType is U_OTHER_NUMBER */
483 else if ( *c>=0x3220 && *c<=0x3229 ) return 0; /* PARENTHESIZED IDEOGRAPH ONE..PARENTHESIZED IDEOGRAPH TEN */
484
485 if (is_surrogate_pair(c))
486 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
487 else c32=*c;
488
489 switch (u_charType(c32)) {
490 case U_DECIMAL_DIGIT_NUMBER:
491 return 1;
492 case U_OTHER_NUMBER:
493 return 2;
494 default:
495 return 0;
496 }
497 }
498
499 int is_jpn_kana(UChar *c)
500 {
501 if ( *c==0x30A0 ) return 0; /* KATAKANA-HIRAGANA DOUBLE HYPHEN */
502 else if ( *c==0x30FB ) return 0; /* KATAKANA MIDDLE DOT */
503 else if ( *c>=0x3040 && *c<=0x30FF ) return 1; /* Hiragana, Katakana */
504 else if ( *c>=0x31F0 && *c<=0x31FF ) return 1; /* Katakana Phonetic Extensions */
505 else if ( *c>=0x32D0 && *c<=0x32FE ) return 1; /* Circled Katakana */
506 else if ( *c>=0xFF66 && *c<=0xFF9F ) return 1; /* Halfwidth Katakana */
507 else if ( *c>=0x3300 && *c<=0x3357 ) return 1; /* Squared Katakana words */
508
509 if (is_surrogate_pair(c)) {
510 UChar32 c32;
511 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
512 if ( c32>=0x1B130 && c32<=0x1B16F ) return 2; /* Small Kana Extensions */
513 else if ( c32==0x1B000 ) return 2; /* KATAKANA LETTER ARCHAIC E */
514 else if ( c32>=0x1B11F /* HIRAGANA LETTER ARCHAIC WU */
515 && c32<=0x1B122 ) return 2; /* KATAKANA LETTER ARCHAIC WU */
516 else if ( c32==0x1F200 ) return 2; /* SQUARE HIRAGANA HOKA */
517 else if ( c32==0x1B001 ) {
518 /* check whether U+1B001 is HIRAGANA LETTER ARCHAIC YE or not.
519 It may be HENTAIGANA LETTER E-1 */
520 if (kana_ye_mode==0) {
521 UCollationResult order;
522 UCollationStrength strgth;
523 UChar strX[4],strZ[4];
524 strgth = ucol_getStrength(icu_collator);
525 ucol_setStrength(icu_collator, UCOL_PRIMARY);
526 strX[0] = 0xD82C; strX[1] = 0xDC01; strX[2] = L'\0'; /* U+1B001 */
527 strZ[0] = 0xD82C; strZ[1] = 0xDD21; strZ[2] = L'\0'; /* U+1B121 */
528 order = ucol_strcoll(icu_collator, strZ, -1, strX, -1);
529 kana_ye_mode = (order==UCOL_EQUAL) ? 2 : 1;
530 ucol_setStrength(icu_collator, strgth);
531 }
532 if (kana_ye_mode==2) return 2;
533 }
534 }
535 return 0;
536 /* ICU 71.1 does not seem to support
537 most of "Kana Supplement" and "Kana Extended-A" yet. (2022/09/11) */
538 }
539
540 int is_kor_hngl(UChar *c)
541 {
542 if ( *c>=0xAC00 && *c<=0xD7AF ) return 1; /* Hangul Syllables */
543 else if ( *c>=0x1100 && *c<=0x11FF ) return 1; /* Hangul Jamo */
544 else if ( *c>=0xA960 && *c<=0xA97F ) return 1; /* Hangul Jamo Extended-A */
545 else if ( *c>=0xD7B0 && *c<=0xD7FF ) return 1; /* Hangul Jamo Extended-B */
546 else if ( *c>=0x3130 && *c<=0x318F ) return 1; /* Hangul Compatibility Jamo */
547 else if ( *c>=0xFFA0 && *c<=0xFFDC ) return 1; /* Hangul Halfwidth Jamo */
548 else if ( *c>=0x3200 && *c<=0x321E ) return 1; /* Enclosed CJK Letters and Months */
549 else if ( *c>=0x3260 && *c<=0x327E ) return 1; /* Enclosed CJK Letters and Months */
550 else return 0;
551 }
552
553 int is_hanzi(UChar *c)
554 {
555 if ( *c>=0x2E80 /* CJK Radicals Supplement */
556 && *c<=0x2FDF ) return 1; /* Kangxi Radicals */
557 else if ( *c>=0x31C0 && *c<=0x31EF ) return 1; /* CJK Strokes */
558 else if ( *c>=0x3300 /* CJK Compatibility */
559 && *c<=0x4DBF ) return 1; /* CJK Unified Ideographs Extension A */
560 else if ( *c>=0x4E00 && *c<=0x9FFF ) return 1; /* CJK Unified Ideographs */
561 else if ( *c>=0xF900 && *c<=0xFAFF ) return 1; /* CJK Compatibility Ideographs */
562
563 if (is_surrogate_pair(c)) {
564 UChar32 c32;
565 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
566 if ( c32>=0x20000 && /* CJK Unified Ideographs Extension B,C,D,E,F,I */
567 /* CJK Compatibility Ideographs Supplement */
568 c32<=0x3347F ) return 2; /* CJK Unified Ideographs Extension G,H,J */
569 }
570 if (*c==0xFDD0) { /* Noncharacter */
571 if (hanzi_mode==HANZI_PINYIN &&
572 *(c+1)>=L'A' && *(c+1)<=L'Z' ) return -1; /* Pinyin Index */
573 if (hanzi_mode==HANZI_ZHUYIN &&
574 *(c+1)>=0x3105 && *(c+1)<=0x3129) return -1; /* Zhuyin Index */
575 }
576 return 0;
577 }
578
579 int is_zhuyin(UChar *c)
580 {
581 if ( *c>=0x3100 && *c<=0x312F ) return 1; /* Bopomofo */
582 else if ( *c>=0x31A0 && *c<=0x31BF ) return 1; /* Bopomofo Extended */
583 else return 0;
584 }
585
586 int is_cyrillic(UChar *c)
587 {
588 if ( *c==0x0482 ) return 0; /* Cyrillic Thousands Sign */
589 else if ( *c>=0x0400 /* Cyrillic */
590 && *c<=0x052F ) return 1; /* Cyrillic Supplement */
591 else if ( *c>=0x1C80 && *c<=0x1C8F ) return 1; /* Cyrillic Extended-C */
592 else if ( *c>=0x2DE0 && *c<=0x2DFF ) return 1; /* Cyrillic Extended-A */
593 else if ( *c>=0xA640 && *c<=0xA69F ) return 1; /* Cyrillic Extended-B */
594
595 if (is_surrogate_pair(c)) {
596 UChar32 c32;
597 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
598 if ( c32>=0x1E030 && c32<=0x1E08F ) return 2; /* Cyrillic Extended-D */
599 }
600 return 0;
601 }
602
603 int is_greek(UChar *c)
604 {
605 if ( *c==0x03F6 ) return 0; /* Greek Reversed Lunate Epsilon Symbol */
606 else if ( *c>=0x0370 && *c<=0x03FF ) return 1; /* Greek */
607 else if ( *c>=0x1F00 && *c<=0x1FFF ) return 1; /* Greek Extended */
608 else return 0;
609 }
610
611 int is_brahmic(UChar *c)
612 {
613 if ( *c< 0x0900 ) return 0;
614
615 /* Attribute Nd, No, Sc, So : return false */
616 else if ( *c>=0x09F2 /* BENGALI RUPEE MARK..BENGALI RUPEE SIGN */
617 /* BENGALI CURRENCY NUMERATOR ONE..BENGALI CURRENCY DENOMINATOR SIXTEEN */
618 && *c<=0x09FB ) return 0; /* .. BENGALI GANDA MARK */
619 else if ( *c==0x0AF1 ) return 0; /* GUJARATI RUPEE SIGN */
620 else if ( *c==0x0B70 ) return 0; /* ORIYA ISSHAR */
621 else if ( *c>=0x0B72 && *c<=0x0B77 ) return 0; /* ORIYA FRACTION ONE QUARTER..ORIYA FRACTION THREE SIXTEENTHS */
622 else if ( *c>=0x0BF0 /* TAMIL NUMBER TEN..TAMIL NUMBER ONE THOUSAND */
623 && *c<=0x0BFA ) return 0; /* .. TAMIL NUMBER SIGN */
624 else if ( *c>=0x0C78 /* TELUGU FRACTION DIGIT ZERO FOR ODD POWERS OF FOUR..TELUGU FRACTION DIGIT THREE FOR EVEN POWERS OF FOUR */
625 && *c<=0x0C7F ) return 0; /* TELUGU SIGN TUUMU */
626 else if ( *c==0x0D4F ) return 0; /* MALAYALAM SIGN PARA */
627 else if ( *c>=0x0D58 && *c<=0x0D5E ) return 0; /* MALAYALAM FRACTION ONE ONE-HUNDRED-AND-SIXTIETH..MALAYALAM FRACTION ONE FIFTH */
628 else if ( *c>=0x0D70 /* MALAYALAM NUMBER TEN..MALAYALAM FRACTION THREE SIXTEENTHS */
629 && *c<=0x0D79 ) return 0; /* MALAYALAM DATE MARK */
630
631 if ( *c>=0xA8E0 && *c<=0xA8FF ) return CH_DEVANAGARI; /* Devanagari Extended */
632 else if ( *c<=0x0DFF ) {
633 /* U+0900..0DFF */
634 /* Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, Malayalam, Sinhala */
635 if ((*c & 0x7F)>=0x64 /* Generic punctuation for scripts of India or Reserved */
636 && (*c & 0x7F)<=0x6F ) return 0; /* .. Digit */
637 return ((*c >> 7) - 0x12 + CH_DEVANAGARI);
638 }
639
640 if (is_surrogate_pair(c)) {
641 UChar32 c32;
642 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
643 if ( c32>=0x11B00 && c32<=0x11B5F ) return CH_DEVANAGARI; /* Devanagari Extended-A */
644 // if ( c32>=0x111E0 && c32<=0x111FF ) return 0; /* Sinhala Archaic Numbers */
645 if ( c32==0x11FFF ) return CH_TAMIL; /* TAMIL PUNCTUATION END OF TEXT */
646 }
647 return 0;
648 }
649
650 int is_thai(UChar *c)
651 {
652 if ( *c==0x0E3F ) return 0; /* Thai Currency Symbol Baht */
653 else if ( *c>=0x0E50 && *c<=0x0E59 ) return 0; /* Thai Digit */
654 else if ( *c>=0x0E00 && *c<=0x0E7F ) return 1; /* Thai */
655 else return 0;
656 }
657
658 int is_lao(UChar *c)
659 {
660 if ( *c>=0x0ED0 && *c<=0x0ED9 ) return 0; /* Lao Digit */
661 else if ( *c>=0x0E80 && *c<=0x0EFF ) return 1; /* Lao */
662 else return 0;
663 }
664
665 int is_arabic(UChar *c)
666 {
667 if ( *c>=0x0600 /* ARABIC NUMBER SIGN..ARABIC SIGN SAMVAT */
668 /* ARABIC NUMBER MARK ABOVE */
669 && *c<=0x0608 ) return 0; /* ARABIC-INDIC CUBE ROOT..ARABIC RAY */
670 else if ( *c==0x060B ) return 0; /* AFGHANI SIGN */
671 else if ( *c==0x060C ) return 0; /* ARABIC COMMA */
672 else if ( *c>=0x060E && *c<=0x060F ) return 0; /* ARABIC POETIC VERSE SIGN..ARABIC SIGN MISRA */
673 else if ( *c>=0x0660 && *c<=0x0669 ) return 0; /* ARABIC-INDIC DIGIT ZERO..ARABIC-INDIC DIGIT NINE */
674 else if ( *c==0x061B ) return 0; /* ARABIC SEMICOLON */
675 else if ( *c==0x061C ) return 0; /* ARABIC LETTER MARK */
676 else if ( *c==0x061F ) return 0; /* ARABIC QUESTION MARK */
677 else if ( *c==0x0640 ) return 0; /* ARABIC TATWEEL */
678 else if ( *c==0x06DD ) return 0; /* ARABIC END OF AYAH */
679 else if ( *c==0x06DE ) return 0; /* ARABIC START OF RUB EL HIZB */
680 else if ( *c==0x06E9 ) return 0; /* ARABIC PLACE OF SAJDAH */
681 else if ( *c>=0x06F0 && *c<=0x06F9 ) return 0; /* EXTENDED ARABIC-INDIC DIGIT ZERO..EXTENDED ARABIC-INDIC DIGIT NINE */
682 else if ( *c>=0x06FD && *c<=0x06FE ) return 0; /* ARABIC SIGN SINDHI AMPERSAND..ARABIC SIGN SINDHI POSTPOSITION MEN */
683 else if ( *c==0x08E2 ) return 0; /* ARABIC DISPUTED END OF AYAH */
684 else if ( *c>=0x0890 && *c<=0x0891 ) return 0; /* ARABIC POUND MARK ABOVE..ARABIC PIASTRE MARK ABOVE */
685 else if ( *c>=0xFD40 && *c<=0xFD4F ) return 0; /* ARABIC LIGATURE RAHIMAHU ALLAAH..ARABIC LIGATURE RAHIMAHUM ALLAAH */
686 else if ( *c==0xFDCF ) return 0; /* ARABIC LIGATURE SALAAMUHU ALAYNAA */
687 else if ( *c==0xFDFC ) return 0; /* RIAL SIGH */
688 else if ( *c>=0xFDFD && *c<=0xFDFF ) return 0; /* ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM..ARABIC LIGATURE AZZA WA JALL */
689
690 else if ( *c>=0x0600 && *c<=0x06FF ) return 1; /* Arabic */
691 else if ( *c>=0x0750 && *c<=0x077F ) return 1; /* Arabic Supplement */
692 else if ( *c>=0x0870 /* Arabic Extended-B */
693 && *c<=0x08FF ) return 1; /* Arabic Extended-A */
694 else if ( *c>=0xFB50 && *c<=0xFDFF ) return 1; /* Arabic Presentation Forms-A */
695 else if ( *c>=0xFE70 && *c<=0xFEFF ) return 1; /* Arabic Presentation Forms-B */
696
697 if (is_surrogate_pair(c)) {
698 UChar32 c32;
699 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
700 if ( c32>=0x10EC0 && c32<=0x10EFF ) return 2; /* Arabic Extended-C */
701 }
702 return 0;
703 }
704
705 int is_hebrew(UChar *c)
706 {
707 if ( *c==0xFB29 ) return 0; /* Hebrew Letter Alternative Plus Sign */
708 else if ( *c>=0x0590 && *c<=0x05FF ) return 1; /* Hebrew */
709 else if ( *c>=0xFB1D && *c<=0xFB4F ) return 1; /* Hebrew presentation forms */
710 else return 0;
711 }
712
713 int is_type_mark_or_punct(UChar *c)
714 {
715 UChar32 c32;
716
717 if (is_surrogate_pair(c))
718 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
719 else c32=*c;
720
721 switch (u_charType(c32)) {
722 case U_MODIFIER_LETTER:
723 case U_DASH_PUNCTUATION: case U_START_PUNCTUATION: case U_END_PUNCTUATION:
724 case U_CONNECTOR_PUNCTUATION: case U_OTHER_PUNCTUATION:
725 case U_INITIAL_PUNCTUATION: case U_FINAL_PUNCTUATION:
726 case U_NON_SPACING_MARK: case U_ENCLOSING_MARK: case U_COMBINING_SPACING_MARK:
727 case U_FORMAT_CHAR:
728 return 1;
729 default:
730 return 0;
731 }
732 }
733
734 int is_type_symbol(UChar *c)
735 {
736 UChar32 c32;
737
738 if (is_surrogate_pair(c))
739 c32=U16_GET_SUPPLEMENTARY(*c,*(c+1));
740 else c32=*c;
741
742 switch (u_charType(c32)) {
743 case U_MODIFIER_SYMBOL:
744 return 1;
745 case U_MATH_SYMBOL: case U_CURRENCY_SYMBOL:
746 case U_OTHER_SYMBOL:
747 return 2;
748 default:
749 return 0;
750 }
751 }
752
753 int chkcontinue(struct page *p, int num)
754 {
755 int i,j,cc=0,num1,num2,k1,k2;
756 char buff1[16],buff2[16],*p0,*p1;
757
758 for (i=0;i<PAGE_COMPOSIT_DEPTH;i++) {
759 if ((p[num].attr[i]<0)&&(p[num+1].attr[i]<0)) return 1;
760 else if (p[num].attr[i]!=p[num+1].attr[i]) return 0;
761
762 p0=&p[num].page[cc];
763 p1=strstr(p0, page_compositor);
764 if (p1) {
765 j=p1-p0;
766 k1=j;
767 } else {
768 j=strlen(p0);
769 k1=0;
770 }
771 strncpy(buff1,p0,j);
772 buff1[j]='\0';
773 num1=pnumconv(buff1,p[num].attr[i]);
774
775 p0=&p[num+1].page[cc];
776 p1=strstr(p0, page_compositor);
777 if (p1) {
778 j=p1-p0;
779 k2=j;
780 } else {
781 j=strlen(p0);
782 k2=0;
783 }
784 strncpy(buff2,p0,j);
785 buff2[j]='\0';
786 num2=pnumconv(buff2,p[num+1].attr[i]);
787
788 if (k1>0 || k2>0) {
789 if (k1!=k2) return 0;
790 if (strcmp(buff1,buff2)) return 0;
791 cc+=k1+strlen(page_compositor);
792 continue;
793 }
794
795 if (num1==num2 || num1+1==num2) return 1;
796 else return 0;
797 }
798
799 return 1;
800 }
801
802 int ss_comp(UChar *s1, UChar *s2)
803 {
804 UCollationResult ret;
805
806 /* compare group */
807 if (ordering(s1)<ordering(s2))
808 return -1;
809 if (ordering(s1)>ordering(s2))
810 return 1;
811
812 /* simple compare */
813 ret = ucol_strcoll(icu_collator, s1, -1, s2, -1);
814 if (ret == UCOL_LESS) return -1;
815 else if (ret == UCOL_GREATER) return 1;
816 return 0;
817 }