1 /* Copyright (c) 1993-2003
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program 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 3, or (at your option)
11 * This program 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, see
18 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 ****************************************************************
24 #include <sys/types.h>
32 extern unsigned char *null
;
33 extern struct display
*display
, *displays
;
34 extern struct layer
*flayer
;
36 extern char *screenencodings
;
42 static int encmatch
__P((char *, char *));
44 static int recode_char
__P((int, int, int));
45 static int recode_char_to_encoding
__P((int, int));
46 static void comb_tofront
__P((int, int));
48 static int recode_char_dw
__P((int, int *, int, int));
49 static int recode_char_dw_to_encoding
__P((int, int *, int));
63 /* KOI8-R font: 96 ! */
64 /* CP1251 font: 96 ? */
66 struct encoding encodings
[] = {
67 { "C", 0, 0, 0, 0, 0 },
68 { "eucJP", "B\002I\00401", 0, 1, 0, "\002\004I" },
69 { "SJIS", "BIBB01", 0, 1, 1, "\002I" },
70 { "eucKR", "B\003BB01", 0, 1, 0, "\003" },
71 { "eucCN", "B\001BB01", 0, 1, 0, "\001" },
72 { "Big5", "B\030BB01", 0, 1, 0, "\030" },
73 { "KOI8-R", 0, 0x80|'!', 0, 1, 0 },
74 { "CP1251", 0, 0x80|'?', 0, 1, 0 },
75 { "UTF-8", 0, -1, 0, 0, 0 },
76 { "ISO8859-2", 0, 0x80|'B', 0, 0, 0 },
77 { "ISO8859-3", 0, 0x80|'C', 0, 0, 0 },
78 { "ISO8859-4", 0, 0x80|'D', 0, 0, 0 },
79 { "ISO8859-5", 0, 0x80|'L', 0, 0, 0 },
80 { "ISO8859-6", 0, 0x80|'G', 0, 0, 0 },
81 { "ISO8859-7", 0, 0x80|'F', 0, 0, 0 },
82 { "ISO8859-8", 0, 0x80|'H', 0, 0, 0 },
83 { "ISO8859-9", 0, 0x80|'M', 0, 0, 0 },
84 { "ISO8859-10", 0, 0x80|'V', 0, 0, 0 },
85 { "ISO8859-15", 0, 0x80|'b', 0, 0, 0 },
86 { "jis", 0, 0, 0, 0, "\002\004I" },
87 { "GBK", "B\031BB01", 0x80|'b', 1, 1, "\031" }
92 static unsigned short builtin_tabs
[][2] = {
93 { 0x30, 0 }, /* 0: special graphics (line drawing) */
128 { 0x34, 0 }, /* 4: Dutch */
140 { 0x35, 0 }, /* 5: Finnish */
152 { 0x36, 0 }, /* 6: Norwegian/Danish */
165 { 0x37, 0 }, /* 7: Swedish */
178 { 0x3d, 0}, /* =: Swiss */
193 { 0x41, 0}, /* A: UK */
197 { 0x4b, 0}, /* K: German */
208 { 0x51, 0}, /* Q: French Canadian */
221 { 0x52, 0}, /* R: French */
233 { 0x59, 0}, /* Y: Italian */
246 { 0x5a, 0}, /* Z: Spanish */
257 { 0xe2, 0}, /* 96-b: ISO-8859-15 */
268 { 0x4a, 0}, /* J: JIS 0201 Roman */
273 { 0x49, 0}, /* I: halfwidth katakana */
275 { 0x005f|0x8000, 0xff9f },
283 unsigned short (*tab
)[2];
287 #define RECODETAB_ALLOCED 1
288 #define RECODETAB_BUILTIN 2
289 #define RECODETAB_TRIED 4
291 static struct recodetab recodetabs
[256];
296 unsigned short (*p
)[2];
297 for (p
= builtin_tabs
; (*p
)[0]; p
++)
299 recodetabs
[(*p
)[0]].flags
= RECODETAB_BUILTIN
;
300 recodetabs
[(*p
)[0]].tab
= p
+ 1;
308 recode_char(c
, to_utf
, font
)
312 unsigned short (*p
)[2];
320 /* map aliases to keep the table small */
335 p
= recodetabs
[f
].tab
;
336 if (p
== 0 && recodetabs
[f
].flags
== 0)
338 LoadFontTranslation(f
, 0);
339 p
= recodetabs
[f
].tab
;
344 if ((p
[0][0] & 0x8000) && (c
<= (p
[0][0] & 0x7fff)) && c
>= p
[-1][0])
345 return c
- p
[-1][0] + p
[-1][1];
349 return c
& 0xff; /* map to latin1 */
354 return c
; /* latin1 */
355 for (font
= 32; font
< 128; font
++)
357 p
= recodetabs
[font
].tab
;
361 if ((p
[0][0] & 0x8000) && c
<= p
[0][1] && c
>= p
[-1][1])
362 return (c
- p
[-1][1] + p
[-1][0]) | (font
<< 8);
364 return (*p
)[0] | (font
<< 8);
369 if (c
< 128 && (font
& 128) != 0)
373 p
= recodetabs
[font
].tab
;
374 if (p
== 0 && recodetabs
[font
].flags
== 0)
376 LoadFontTranslation(font
, 0);
377 p
= recodetabs
[font
].tab
;
382 if ((p
[0][0] & 0x8000) && c
<= p
[0][1] && c
>= p
[-1][1])
383 return (c
- p
[-1][1] + p
[-1][0]) | (font
& 128 ? 0 : font
<< 8);
385 return (*p
)[0] | (font
& 128 ? 0 : font
<< 8);
394 recode_char_dw(c
, c2p
, to_utf
, font
)
395 int c
, *c2p
, to_utf
, font
;
398 unsigned short (*p
)[2];
403 c
= (c
& 255) << 8 | (*c2p
& 255);
405 p
= recodetabs
[f
].tab
;
406 if (p
== 0 && recodetabs
[f
].flags
== 0)
408 LoadFontTranslation(f
, 0);
409 p
= recodetabs
[f
].tab
;
416 if (!utf8_isdouble((*p
)[1]))
425 for (font
= 0; font
< 030; font
++)
427 p
= recodetabs
[font
].tab
;
432 *c2p
= ((*p
)[0] & 255) | font
<< 8 | 0x8000;
433 return ((*p
)[0] >> 8) | font
<< 8;
441 p
= recodetabs
[font
].tab
;
442 if (p
== 0 && recodetabs
[font
].flags
== 0)
444 LoadFontTranslation(font
, 0);
445 p
= recodetabs
[font
].tab
;
451 *c2p
= ((*p
)[0] & 255) | font
<< 8 | 0x8000;
452 return ((*p
)[0] >> 8) | font
<< 8;
460 recode_char_to_encoding(c
, encoding
)
466 if (encoding
== UTF8
)
467 return recode_char(c
, 1, -1);
468 if ((fp
= encodings
[encoding
].fontlist
) != 0)
470 if ((x
= recode_char(c
, 0, (unsigned char)*fp
++)) != -1)
472 if (encodings
[encoding
].deffont
)
473 if ((x
= recode_char(c
, 0, encodings
[encoding
].deffont
)) != -1)
475 return recode_char(c
, 0, -1);
480 recode_char_dw_to_encoding(c
, c2p
, encoding
)
481 int c
, *c2p
, encoding
;
486 if (encoding
== UTF8
)
487 return recode_char_dw(c
, c2p
, 1, -1);
488 if ((fp
= encodings
[encoding
].fontlist
) != 0)
490 if ((x
= recode_char_dw(c
, c2p
, 0, (unsigned char)*fp
++)) != -1)
492 if (encodings
[encoding
].deffont
)
493 if ((x
= recode_char_dw(c
, c2p
, 0, encodings
[encoding
].deffont
)) != -1)
495 return recode_char_dw(c
, c2p
, 0, -1);
501 recode_mchar(mc
, from
, to
)
505 static struct mchar rmc
;
508 debug3("recode_mchar %02x from %d to %d\n", mc
->image
, from
, to
);
509 if (from
== to
|| (from
!= UTF8
&& to
!= UTF8
))
512 if (rmc
.font
== 0 && from
!= UTF8
)
513 rmc
.font
= encodings
[from
].deffont
;
514 if (rmc
.font
== 0) /* latin1 is the same in unicode */
516 c
= rmc
.image
| (rmc
.font
<< 8);
521 c
= recode_char_dw_to_encoding(c
, &c2
, to
);
526 c
= recode_char_to_encoding(c
, to
);
528 rmc
.font
= c
>> 8 & 255;
533 recode_mline(ml
, w
, from
, to
)
540 static struct mline rml
[2], *rl
;
543 if (from
== to
|| (from
!= UTF8
&& to
!= UTF8
) || w
== 0)
545 if (ml
->font
== null
&& encodings
[from
].deffont
== 0)
549 for (i
= 0; i
< 2; i
++)
551 if (rml
[i
].image
== 0)
552 rml
[i
].image
= malloc(w
);
554 rml
[i
].image
= realloc(rml
[i
].image
, w
);
555 if (rml
[i
].font
== 0)
556 rml
[i
].font
= malloc(w
);
558 rml
[i
].font
= realloc(rml
[i
].font
, w
);
559 if (rml
[i
].image
== 0 || rml
[i
].font
== 0)
562 return ml
; /* sorry */
568 debug("recode_mline: from\n");
569 for (i
= 0; i
< w
; i
++)
570 debug1("%c", "0123456789abcdef"[(ml
->image
[i
] >> 4) & 15]);
572 for (i
= 0; i
< w
; i
++)
573 debug1("%c", "0123456789abcdef"[(ml
->image
[i
] ) & 15]);
575 for (i
= 0; i
< w
; i
++)
576 debug1("%c", "0123456789abcdef"[(ml
->font
[i
] >> 4) & 15]);
578 for (i
= 0; i
< w
; i
++)
579 debug1("%c", "0123456789abcdef"[(ml
->font
[i
] ) & 15]);
585 rl
->color
= ml
->color
;
587 rl
->colorx
= ml
->colorx
;
590 for (i
= 0; i
< w
; i
++)
592 c
= ml
->image
[i
] | (ml
->font
[i
] << 8);
593 if (from
!= UTF8
&& c
< 256)
594 c
|= encodings
[from
].deffont
<< 8;
596 if ((from
!= UTF8
&& (c
& 0x1f00) != 0 && (c
& 0xe000) == 0) || (from
== UTF8
&& utf8_isdouble(c
)))
604 c2
= ml
->image
[i
] | (ml
->font
[i
] << 8);
605 c
= recode_char_dw_to_encoding(c
, &c2
, to
);
606 rl
->font
[i
- 1] = c
>> 8 & 255;
607 rl
->image
[i
- 1] = c
& 255;
613 c
= recode_char_to_encoding(c
, to
);
614 rl
->image
[i
] = c
& 255;
615 rl
->font
[i
] = c
>> 8 & 255;
618 debug("recode_mline: to\n");
619 for (i
= 0; i
< w
; i
++)
620 debug1("%c", "0123456789abcdef"[(rl
->image
[i
] >> 4) & 15]);
622 for (i
= 0; i
< w
; i
++)
623 debug1("%c", "0123456789abcdef"[(rl
->image
[i
] ) & 15]);
625 for (i
= 0; i
< w
; i
++)
626 debug1("%c", "0123456789abcdef"[(rl
->font
[i
] >> 4) & 15]);
628 for (i
= 0; i
< w
; i
++)
629 debug1("%c", "0123456789abcdef"[(rl
->font
[i
] ) & 15]);
640 struct combchar
**combchars
;
646 ASSERT(D_encoding
== UTF8
);
647 if (c
>= 0xd800 && c
< 0xe000 && combchars
&& combchars
[c
- 0xd800])
649 AddUtf8(combchars
[c
- 0xd800]->c1
);
650 c
= combchars
[c
- 0xd800]->c2
;
654 AddChar((c
& 0xf000) >> 12 | 0xe0);
655 c
= (c
& 0x0fff) | 0x1000;
659 AddChar((c
& 0x1fc0) >> 6 ^ 0xc0);
660 c
= (c
& 0x3f) | 0x80;
672 if (c
>= 0xd800 && c
< 0xe000 && combchars
&& combchars
[c
- 0xd800])
674 l
= ToUtf8_comb(p
, combchars
[c
- 0xd800]->c1
);
675 return l
+ ToUtf8(p
? p
+ l
: 0, combchars
[c
- 0xd800]->c2
);
689 *p
++ = (c
& 0xf000) >> 12 | 0xe0;
691 c
= (c
& 0x0fff) | 0x1000;
696 *p
++ = (c
& 0x1fc0) >> 6 ^ 0xc0;
698 c
= (c
& 0x3f) | 0x80;
707 * -1: need more bytes, sequence not finished
708 * -2: corrupt sequence found, redo last char
709 * >= 0: decoded character
712 FromUtf8(c
, utf8charp
)
715 int utf8char
= *utf8charp
;
718 if ((c
& 0xc0) != 0x80)
721 return -2; /* corrupt sequence! */
724 c
= (c
& 0x3f) | (utf8char
<< 6);
725 if (!(utf8char
& 0x40000000))
727 /* check for overlong sequences */
728 if ((c
& 0x820823e0) == 0x80000000)
730 else if ((c
& 0x020821f0) == 0x02000000)
732 else if ((c
& 0x000820f8) == 0x00080000)
734 else if ((c
& 0x0000207c) == 0x00002000)
744 c
= (c
& 0x01) | 0xbffffffc; /* 5 bytes to follow */
746 c
= (c
& 0x03) | 0xbfffff00; /* 4 */
748 c
= (c
& 0x07) | 0xbfffc000; /* 3 */
750 c
= (c
& 0x0f) | 0xbff00000; /* 2 */
752 c
= (c
& 0x1f) | 0xfc000000; /* 1 */
754 c
= 0xfdffffff; /* overlong */
758 *utf8charp
= utf8char
= (c
& 0x80000000) ? c
: 0;
762 c
= UCS_REPL
; /* sorry, only know 16bit Unicode */
763 if (c
>= 0xd800 && (c
<= 0xdfff || c
== 0xfffe || c
== 0xffff))
764 c
= UCS_REPL
; /* illegal code */
770 WinSwitchEncoding(p
, encoding
)
778 struct layer
*oldflayer
;
780 if ((p
->w_encoding
== UTF8
) == (encoding
== UTF8
))
782 p
->w_encoding
= encoding
;
786 for (d
= displays
; d
; d
= d
->d_next
)
787 for (cv
= d
->d_cvlist
; cv
; cv
= cv
->c_next
)
788 if (p
== Layer2Window(cv
->c_layer
))
790 flayer
= cv
->c_layer
;
791 while(flayer
->l_next
)
793 if (oldflayer
== flayer
)
794 oldflayer
= flayer
->l_next
;
799 for (j
= 0; j
< p
->w_height
+ p
->w_histheight
; j
++)
802 ml
= j
< p
->w_height
? &p
->w_mlines
[j
] : &p
->w_hlines
[j
- p
->w_height
];
804 ml
= &p
->w_mlines
[j
];
806 if (ml
->font
== null
&& encodings
[p
->w_encoding
].deffont
== 0)
808 for (i
= 0; i
< p
->w_width
; i
++)
810 c
= ml
->image
[i
] | (ml
->font
[i
] << 8);
811 if (p
->w_encoding
!= UTF8
&& c
< 256)
812 c
|= encodings
[p
->w_encoding
].deffont
<< 8;
815 if (ml
->font
== null
)
817 if ((ml
->font
= (unsigned char *)calloc(p
->w_width
+ 1, 1)) == 0)
824 if ((p
->w_encoding
!= UTF8
&& (c
& 0x1f00) != 0 && (c
& 0xe000) == 0) || (p
->w_encoding
== UTF8
&& utf8_isdouble(c
)))
826 if (i
+ 1 == p
->w_width
)
832 c2
= ml
->image
[i
] | (ml
->font
[i
] << 8);
833 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
834 ml
->font
[i
- 1] = c
>> 8 & 255;
835 ml
->image
[i
- 1] = c
& 255;
841 c
= recode_char_to_encoding(c
, encoding
);
842 ml
->image
[i
] = c
& 255;
843 ml
->font
[i
] = c
>> 8 & 255;
846 p
->w_encoding
= encoding
;
856 /* auxiliary function for binary search in interval table */
857 static int bisearch(int ucs
, const struct interval
*table
, int max
) {
861 if (ucs
< table
[0].first
|| ucs
> table
[max
].last
)
864 mid
= (min
+ max
) / 2;
865 if (ucs
> table
[mid
].last
)
867 else if (ucs
< table
[mid
].first
)
880 /* sorted list of non-overlapping intervals of East Asian Ambiguous
881 * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
882 static const struct interval ambiguous
[] = {
883 { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 },
884 { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 },
885 { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 },
886 { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 },
887 { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED },
888 { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA },
889 { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 },
890 { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B },
891 { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 },
892 { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 },
893 { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 },
894 { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE },
895 { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 },
896 { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA },
897 { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 },
898 { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB },
899 { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB },
900 { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 },
901 { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 },
902 { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 },
903 { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 },
904 { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 },
905 { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 },
906 { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 },
907 { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC },
908 { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 },
909 { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 },
910 { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 },
911 { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 },
912 { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 },
913 { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 },
914 { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B },
915 { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 },
916 { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 },
917 { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E },
918 { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 },
919 { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 },
920 { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F },
921 { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 },
922 { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF },
923 { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B },
924 { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 },
925 { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 },
926 { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 },
927 { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 },
928 { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 },
929 { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 },
930 { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 },
931 { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 },
932 { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F },
933 { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF },
934 { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD }
937 return ((c
>= 0x1100 &&
938 (c
<= 0x115f || /* Hangul Jamo init. consonants */
939 c
== 0x2329 || c
== 0x232a ||
940 (c
>= 0x2e80 && c
<= 0xa4cf &&
941 c
!= 0x303f) || /* CJK ... Yi */
942 (c
>= 0xac00 && c
<= 0xd7a3) || /* Hangul Syllables */
943 (c
>= 0xf900 && c
<= 0xfaff) || /* CJK Compatibility Ideographs */
944 (c
>= 0xfe30 && c
<= 0xfe6f) || /* CJK Compatibility Forms */
945 (c
>= 0xff00 && c
<= 0xff60) || /* Fullwidth Forms */
946 (c
>= 0xffe0 && c
<= 0xffe6) ||
947 (c
>= 0x20000 && c
<= 0x2fffd) ||
948 (c
>= 0x30000 && c
<= 0x3fffd))) ||
950 bisearch(c
, ambiguous
,
951 sizeof(ambiguous
) / sizeof(struct interval
) - 1)));
959 /* taken from Markus Kuhn's wcwidth */
960 static const struct interval combining
[] = {
961 { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
962 { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
963 { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
964 { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
965 { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
966 { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
967 { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
968 { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
969 { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
970 { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
971 { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
972 { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
973 { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
974 { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
975 { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
976 { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
977 { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
978 { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
979 { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
980 { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
981 { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
982 { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
983 { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
984 { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
985 { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
986 { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
987 { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
988 { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
989 { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
990 { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
991 { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
992 { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
993 { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
994 { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
995 { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
996 { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
997 { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
998 { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
999 { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
1000 { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
1001 { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
1002 { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
1003 { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
1004 { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
1005 { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
1006 { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
1007 { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
1008 { 0xE0100, 0xE01EF }
1011 return bisearch(c
, combining
, sizeof(combining
) / sizeof(struct interval
) - 1);
1015 comb_tofront(root
, i
)
1020 debug1("bring to front: %x\n", i
);
1021 combchars
[combchars
[i
]->prev
]->next
= combchars
[i
]->next
;
1022 combchars
[combchars
[i
]->next
]->prev
= combchars
[i
]->prev
;
1023 combchars
[i
]->next
= combchars
[root
]->next
;
1024 combchars
[i
]->prev
= root
;
1025 combchars
[combchars
[root
]->next
]->prev
= i
;
1026 combchars
[root
]->next
= i
;
1027 i
= combchars
[i
]->c1
;
1028 if (i
< 0xd800 || i
>= 0xe000)
1035 utf8_handle_comb(c
, mc
)
1042 c1
= mc
->image
| (mc
->font
<< 8);
1043 isdouble
= c1
>= 0x1100 && utf8_isdouble(c1
);
1046 combchars
= (struct combchar
**)calloc(0x802, sizeof(struct combchar
*));
1049 combchars
[0x800] = (struct combchar
*)malloc(sizeof(struct combchar
));
1050 combchars
[0x801] = (struct combchar
*)malloc(sizeof(struct combchar
));
1051 if (!combchars
[0x800] || !combchars
[0x801])
1053 if (combchars
[0x800])
1054 free(combchars
[0x800]);
1055 if (combchars
[0x801])
1056 free(combchars
[0x801]);
1060 combchars
[0x800]->c1
= 0x000;
1061 combchars
[0x800]->c2
= 0x700;
1062 combchars
[0x800]->next
= 0x800;
1063 combchars
[0x800]->prev
= 0x800;
1064 combchars
[0x801]->c1
= 0x700;
1065 combchars
[0x801]->c2
= 0x800;
1066 combchars
[0x801]->next
= 0x801;
1067 combchars
[0x801]->prev
= 0x801;
1069 root
= isdouble
? 0x801 : 0x800;
1070 for (i
= combchars
[root
]->c1
; i
< combchars
[root
]->c2
; i
++)
1074 if (combchars
[i
]->c1
== c1
&& combchars
[i
]->c2
== c
)
1077 if (i
== combchars
[root
]->c2
)
1079 /* full, recycle old entry */
1080 if (c1
>= 0xd800 && c1
< 0xe000)
1081 comb_tofront(root
, c1
- 0xd800);
1082 i
= combchars
[root
]->prev
;
1083 if (c1
== i
+ 0xd800)
1085 /* completely full, can't recycle */
1086 debug("utf8_handle_comp: completely full!\n");
1091 /* FIXME: delete old char from all buffers */
1093 else if (!combchars
[i
])
1095 combchars
[i
] = (struct combchar
*)malloc(sizeof(struct combchar
));
1098 combchars
[i
]->prev
= i
;
1099 combchars
[i
]->next
= i
;
1101 combchars
[i
]->c1
= c1
;
1102 combchars
[i
]->c2
= c
;
1103 mc
->image
= i
& 0xff;
1104 mc
->font
= (i
>> 8) + 0xd8;
1105 debug3("combinig char %x %x -> %x\n", c1
, c
, i
+ 0xd800);
1106 comb_tofront(root
, i
);
1112 WinSwitchEncoding(p
, encoding
)
1116 p
->w_encoding
= encoding
;
1130 c1
= (unsigned char)*s1
;
1131 if (c1
>= 'A' && c1
<= 'Z')
1133 if (!(c1
>= 'a' && c1
<= 'z') && !(c1
>= '0' && c1
<= '9'))
1138 c2
= (unsigned char)*s2
;
1139 if (c2
>= 'A' && c2
<= 'Z')
1141 if (!(c2
>= 'a' && c2
<= 'z') && !(c2
>= '0' && c2
<= '9'))
1161 debug1("FindEncoding %s\n", name
);
1162 if (name
== 0 || *name
== 0)
1164 if (encmatch(name
, "euc"))
1166 if (encmatch(name
, "off") || encmatch(name
, "iso8859-1"))
1169 if (encmatch(name
, "UTF-8"))
1172 for (encoding
= 0; encoding
< (int)(sizeof(encodings
)/sizeof(*encodings
)); encoding
++)
1173 if (encmatch(name
, encodings
[encoding
].name
))
1176 LoadFontTranslationsForEncoding(encoding
);
1184 EncodingName(encoding
)
1187 if (encoding
>= (int)(sizeof(encodings
)/sizeof(*encodings
)))
1189 return encodings
[encoding
].name
;
1193 EncodingDefFont(encoding
)
1196 return encodings
[encoding
].deffont
;
1204 int encoding
= p
->w_encoding
;
1206 c
= encodings
[encoding
].charsets
;
1210 LoadFontTranslationsForEncoding(encoding
);
1212 if (encodings
[encoding
].usegr
)
1215 p
->w_FontE
= encodings
[encoding
].charsets
[1];
1219 if (encodings
[encoding
].noc1
)
1224 DecodeChar(c
, encoding
, statep
)
1231 debug2("Decoding char %02x for encoding %d\n", c
, encoding
);
1233 if (encoding
== UTF8
)
1234 return FromUtf8(c
, statep
);
1236 if (encoding
== SJIS
)
1240 if ((0x81 <= c
&& c
<= 0x9f) || (0xe0 <= c
&& c
<= 0xef))
1247 return c
| (KANA
<< 16);
1252 if (0x40 <= t
&& t
<= 0xfc && t
!= 0x7f)
1255 c
= (c
- 0x81) * 2 + 0x21;
1257 c
= (c
- 0xc1) * 2 + 0x21;
1264 return (c
<< 8) | t
| (KANJI
<< 16);
1268 if (encoding
== EUC_JP
|| encoding
== EUC_KR
|| encoding
== EUC_CN
)
1282 if (encoding
== EUC_JP
)
1285 return t
| (KANA
<< 16);
1288 *statep
= t
| (KANJI0212
<< 8);
1295 if (encoding
== EUC_KR
)
1296 return c
| (3 << 16);
1297 if (encoding
== EUC_CN
)
1298 return c
| (1 << 16);
1299 if (c
& (KANJI0212
<< 16))
1302 return c
| (KANJI
<< 16);
1304 if (encoding
== BIG5
|| encoding
== GBK
)
1310 if (encoding
== GBK
&& c
== 0x80)
1311 return 0xa4 | (('b'|0x80) << 16);
1321 return c
<< 8 | t
| (encoding
== BIG5
? 030 << 16 : 031 << 16);
1323 return c
| (encodings
[encoding
].deffont
<< 16);
1327 EncodeChar(bp
, c
, encoding
, fontp
)
1335 debug2("Encoding char %02x for encoding %d\n", c
, encoding
);
1336 if (c
== -1 && fontp
)
1351 if (encoding
== UTF8
)
1359 c
= (c
>> 8 & 0xff) | (f
<< 8);
1360 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
1365 c
= (c
& 0xff) | (f
<< 8);
1366 c
= recode_char_to_encoding(c
, encoding
);
1369 return ToUtf8(bp
, c
);
1371 if ((c
& 0xff00) && f
== 0) /* is_utf8? */
1374 if (utf8_isdouble(c
))
1377 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
1378 c
= (c
<< 8) | (c2
& 0xff);
1383 c
= recode_char_to_encoding(c
, encoding
);
1384 c
= ((c
& 0xff00) << 8) | (c
& 0xff);
1386 debug1("Encode: char mapped from utf8 to %x\n", c
);
1390 if (f
& 0x80) /* map special 96-fonts to latin1 */
1393 if (encoding
== SJIS
)
1396 c
= (c
& 0xff) | 0x80;
1397 else if (f
== KANJI
)
1402 c
= (c
>> 8) & 0xff;
1403 t
+= (c
& 1) ? ((t
<= 0x5f) ? 0x1f : 0x20) : 0x7e;
1404 c
= (c
- 0x21) / 2 + ((c
< 0x5f) ? 0x81 : 0xc1);
1410 if (encoding
== EUC
)
1425 *bp
++ = (c
>> 8) | 0x80;
1441 if ((encoding
== EUC_KR
&& f
== 3) || (encoding
== EUC_CN
&& f
== 1))
1445 *bp
++ = (c
>> 8) | 0x80;
1450 if ((encoding
== BIG5
&& f
== 030) || (encoding
== GBK
&& f
== 031))
1454 *bp
++ = (c
>> 8) | 0x80;
1459 if (encoding
== GBK
&& f
== 0 && c
== 0xa4)
1463 if (fontp
&& f
!= *fontp
)
1503 CanEncodeFont(encoding
, f
)
1513 return f
== KANJI
|| f
== KANA
;
1515 return f
== KANJI
|| f
== KANA
|| f
== KANJI0212
;
1532 PrepareEncodedChar(c
)
1539 encoding
= D_encoding
;
1542 if (encoding
== SJIS
)
1546 else if (f
== KANJI
)
1548 t
+= (c
& 1) ? ((t
<= 0x5f) ? 0x1f : 0x20) : 0x7e;
1549 c
= (c
- 0x21) / 2 + ((c
< 0x5f) ? 0x81 : 0xc1);
1554 if (encoding
== EUC
)
1573 if ((encoding
== EUC_KR
&& f
== 3) || (encoding
== EUC_CN
&& f
== 1))
1578 if ((encoding
== BIG5
&& f
== 030) || (encoding
== GBK
&& f
== 031))
1585 RecodeBuf(fbuf
, flen
, fenc
, tenc
, tbuf
)
1586 unsigned char *fbuf
;
1589 unsigned char *tbuf
;
1592 int decstate
= 0, font
= 0;
1594 for (i
= j
= 0; i
< flen
; i
++)
1597 c
= DecodeChar(c
, fenc
, &decstate
);
1602 j
+= EncodeChar(tbuf
? (char *)tbuf
+ j
: 0, c
, tenc
, &font
);
1604 j
+= EncodeChar(tbuf
? (char *)tbuf
+ j
: 0, -1, tenc
, &font
);
1610 ContainsSpecialDeffont(ml
, xs
, xe
, encoding
)
1615 unsigned char *f
, *i
;
1618 if (encoding
== UTF8
|| encodings
[encoding
].deffont
== 0)
1628 x
= recode_char_to_encoding(c
| (encodings
[encoding
].deffont
<< 8), UTF8
);
1631 debug2("ContainsSpecialDeffont: yes %02x != %02x\n", c
, x
);
1635 debug("ContainsSpecialDeffont: no\n");
1641 LoadFontTranslation(font
, file
)
1645 char buf
[1024], *myfile
;
1650 unsigned short (*p
)[2], (*tab
)[2];
1655 if (font
== 0 || screenencodings
== 0)
1657 if (strlen(screenencodings
) > sizeof(buf
) - 10)
1659 sprintf(buf
, "%s/%02x", screenencodings
, font
& 0xff);
1662 debug1("LoadFontTranslation: trying %s\n", myfile
);
1663 if ((f
= secfopen(myfile
, "r")) == 0)
1669 if (getc(f
) != "ScreenI2UTF8"[i
])
1671 if (getc(f
) != 0) /* format */
1673 fo
= getc(f
); /* id */
1676 if (font
!= -1 && font
!= fo
)
1684 while ((x
= getc(f
)) && x
!= EOF
)
1685 getc(f
); /* skip font name (padded to 2 bytes) */
1686 if ((p
= malloc(sizeof(*p
) * (i
+ 1))) == 0)
1692 x
= x
<< 8 | getc(f
);
1705 if (i
|| (tab
[0][0] & 0x8000))
1710 if (recodetabs
[fo
].tab
&& (recodetabs
[fo
].flags
& RECODETAB_ALLOCED
) != 0)
1711 free(recodetabs
[fo
].tab
);
1712 recodetabs
[fo
].tab
= tab
;
1713 recodetabs
[fo
].flags
= RECODETAB_ALLOCED
;
1714 debug1("Successful load of recodetab %02x\n", fo
);
1726 if (font
!= -1 && file
== 0 && recodetabs
[font
].flags
== 0)
1727 recodetabs
[font
].flags
= RECODETAB_TRIED
;
1732 LoadFontTranslationsForEncoding(encoding
)
1738 debug1("LoadFontTranslationsForEncoding: encoding %d\n", encoding
);
1739 if ((c
= encodings
[encoding
].fontlist
) != 0)
1740 while ((f
= (unsigned char)*c
++) != 0)
1741 if (recodetabs
[f
].flags
== 0)
1742 LoadFontTranslation(f
, 0);
1743 f
= encodings
[encoding
].deffont
;
1744 if (f
> 0 && recodetabs
[f
].flags
== 0)
1745 LoadFontTranslation(f
, 0);
1750 #else /* !ENCODINGS */
1752 /* Simple version of EncodeChar to encode font changes for
1756 EncodeChar(bp
, c
, encoding
, fontp
)
1763 f
= (c
== -1) ? 0 : c
>> 16;
1765 if (fontp
&& f
!= *fontp
)
1806 #endif /* ENCODINGS */