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 2, 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, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *)malloc(p
->w_width
+ 1)) == 0)
822 bzero(ml
->font
, p
->w_width
+ 1);
825 if ((p
->w_encoding
!= UTF8
&& (c
& 0x1f00) != 0 && (c
& 0xe000) == 0) || (p
->w_encoding
== UTF8
&& utf8_isdouble(c
)))
827 if (i
+ 1 == p
->w_width
)
833 c2
= ml
->image
[i
] | (ml
->font
[i
] << 8);
834 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
835 ml
->font
[i
- 1] = c
>> 8 & 255;
836 ml
->image
[i
- 1] = c
& 255;
842 c
= recode_char_to_encoding(c
, encoding
);
843 ml
->image
[i
] = c
& 255;
844 ml
->font
[i
] = c
>> 8 & 255;
847 p
->w_encoding
= encoding
;
857 /* auxiliary function for binary search in interval table */
858 static int bisearch(int ucs
, const struct interval
*table
, int max
) {
862 if (ucs
< table
[0].first
|| ucs
> table
[max
].last
)
865 mid
= (min
+ max
) / 2;
866 if (ucs
> table
[mid
].last
)
868 else if (ucs
< table
[mid
].first
)
881 /* sorted list of non-overlapping intervals of East Asian Ambiguous
882 * characters, generated by "uniset +WIDTH-A -cat=Me -cat=Mn -cat=Cf c" */
883 static const struct interval ambiguous
[] = {
884 { 0x00A1, 0x00A1 }, { 0x00A4, 0x00A4 }, { 0x00A7, 0x00A8 },
885 { 0x00AA, 0x00AA }, { 0x00AE, 0x00AE }, { 0x00B0, 0x00B4 },
886 { 0x00B6, 0x00BA }, { 0x00BC, 0x00BF }, { 0x00C6, 0x00C6 },
887 { 0x00D0, 0x00D0 }, { 0x00D7, 0x00D8 }, { 0x00DE, 0x00E1 },
888 { 0x00E6, 0x00E6 }, { 0x00E8, 0x00EA }, { 0x00EC, 0x00ED },
889 { 0x00F0, 0x00F0 }, { 0x00F2, 0x00F3 }, { 0x00F7, 0x00FA },
890 { 0x00FC, 0x00FC }, { 0x00FE, 0x00FE }, { 0x0101, 0x0101 },
891 { 0x0111, 0x0111 }, { 0x0113, 0x0113 }, { 0x011B, 0x011B },
892 { 0x0126, 0x0127 }, { 0x012B, 0x012B }, { 0x0131, 0x0133 },
893 { 0x0138, 0x0138 }, { 0x013F, 0x0142 }, { 0x0144, 0x0144 },
894 { 0x0148, 0x014B }, { 0x014D, 0x014D }, { 0x0152, 0x0153 },
895 { 0x0166, 0x0167 }, { 0x016B, 0x016B }, { 0x01CE, 0x01CE },
896 { 0x01D0, 0x01D0 }, { 0x01D2, 0x01D2 }, { 0x01D4, 0x01D4 },
897 { 0x01D6, 0x01D6 }, { 0x01D8, 0x01D8 }, { 0x01DA, 0x01DA },
898 { 0x01DC, 0x01DC }, { 0x0251, 0x0251 }, { 0x0261, 0x0261 },
899 { 0x02C4, 0x02C4 }, { 0x02C7, 0x02C7 }, { 0x02C9, 0x02CB },
900 { 0x02CD, 0x02CD }, { 0x02D0, 0x02D0 }, { 0x02D8, 0x02DB },
901 { 0x02DD, 0x02DD }, { 0x02DF, 0x02DF }, { 0x0391, 0x03A1 },
902 { 0x03A3, 0x03A9 }, { 0x03B1, 0x03C1 }, { 0x03C3, 0x03C9 },
903 { 0x0401, 0x0401 }, { 0x0410, 0x044F }, { 0x0451, 0x0451 },
904 { 0x2010, 0x2010 }, { 0x2013, 0x2016 }, { 0x2018, 0x2019 },
905 { 0x201C, 0x201D }, { 0x2020, 0x2022 }, { 0x2024, 0x2027 },
906 { 0x2030, 0x2030 }, { 0x2032, 0x2033 }, { 0x2035, 0x2035 },
907 { 0x203B, 0x203B }, { 0x203E, 0x203E }, { 0x2074, 0x2074 },
908 { 0x207F, 0x207F }, { 0x2081, 0x2084 }, { 0x20AC, 0x20AC },
909 { 0x2103, 0x2103 }, { 0x2105, 0x2105 }, { 0x2109, 0x2109 },
910 { 0x2113, 0x2113 }, { 0x2116, 0x2116 }, { 0x2121, 0x2122 },
911 { 0x2126, 0x2126 }, { 0x212B, 0x212B }, { 0x2153, 0x2154 },
912 { 0x215B, 0x215E }, { 0x2160, 0x216B }, { 0x2170, 0x2179 },
913 { 0x2190, 0x2199 }, { 0x21B8, 0x21B9 }, { 0x21D2, 0x21D2 },
914 { 0x21D4, 0x21D4 }, { 0x21E7, 0x21E7 }, { 0x2200, 0x2200 },
915 { 0x2202, 0x2203 }, { 0x2207, 0x2208 }, { 0x220B, 0x220B },
916 { 0x220F, 0x220F }, { 0x2211, 0x2211 }, { 0x2215, 0x2215 },
917 { 0x221A, 0x221A }, { 0x221D, 0x2220 }, { 0x2223, 0x2223 },
918 { 0x2225, 0x2225 }, { 0x2227, 0x222C }, { 0x222E, 0x222E },
919 { 0x2234, 0x2237 }, { 0x223C, 0x223D }, { 0x2248, 0x2248 },
920 { 0x224C, 0x224C }, { 0x2252, 0x2252 }, { 0x2260, 0x2261 },
921 { 0x2264, 0x2267 }, { 0x226A, 0x226B }, { 0x226E, 0x226F },
922 { 0x2282, 0x2283 }, { 0x2286, 0x2287 }, { 0x2295, 0x2295 },
923 { 0x2299, 0x2299 }, { 0x22A5, 0x22A5 }, { 0x22BF, 0x22BF },
924 { 0x2312, 0x2312 }, { 0x2460, 0x24E9 }, { 0x24EB, 0x254B },
925 { 0x2550, 0x2573 }, { 0x2580, 0x258F }, { 0x2592, 0x2595 },
926 { 0x25A0, 0x25A1 }, { 0x25A3, 0x25A9 }, { 0x25B2, 0x25B3 },
927 { 0x25B6, 0x25B7 }, { 0x25BC, 0x25BD }, { 0x25C0, 0x25C1 },
928 { 0x25C6, 0x25C8 }, { 0x25CB, 0x25CB }, { 0x25CE, 0x25D1 },
929 { 0x25E2, 0x25E5 }, { 0x25EF, 0x25EF }, { 0x2605, 0x2606 },
930 { 0x2609, 0x2609 }, { 0x260E, 0x260F }, { 0x2614, 0x2615 },
931 { 0x261C, 0x261C }, { 0x261E, 0x261E }, { 0x2640, 0x2640 },
932 { 0x2642, 0x2642 }, { 0x2660, 0x2661 }, { 0x2663, 0x2665 },
933 { 0x2667, 0x266A }, { 0x266C, 0x266D }, { 0x266F, 0x266F },
934 { 0x273D, 0x273D }, { 0x2776, 0x277F }, { 0xE000, 0xF8FF },
935 { 0xFFFD, 0xFFFD }, { 0xF0000, 0xFFFFD }, { 0x100000, 0x10FFFD }
938 return ((c
>= 0x1100 &&
939 (c
<= 0x115f || /* Hangul Jamo init. consonants */
940 c
== 0x2329 || c
== 0x232a ||
941 (c
>= 0x2e80 && c
<= 0xa4cf &&
942 c
!= 0x303f) || /* CJK ... Yi */
943 (c
>= 0xac00 && c
<= 0xd7a3) || /* Hangul Syllables */
944 (c
>= 0xf900 && c
<= 0xfaff) || /* CJK Compatibility Ideographs */
945 (c
>= 0xfe30 && c
<= 0xfe6f) || /* CJK Compatibility Forms */
946 (c
>= 0xff00 && c
<= 0xff60) || /* Fullwidth Forms */
947 (c
>= 0xffe0 && c
<= 0xffe6) ||
948 (c
>= 0x20000 && c
<= 0x2fffd) ||
949 (c
>= 0x30000 && c
<= 0x3fffd))) ||
951 bisearch(c
, ambiguous
,
952 sizeof(ambiguous
) / sizeof(struct interval
) - 1)));
960 /* taken from Markus Kuhn's wcwidth */
961 static const struct interval combining
[] = {
962 { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
963 { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
964 { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
965 { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
966 { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
967 { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
968 { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
969 { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
970 { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
971 { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
972 { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
973 { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
974 { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
975 { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
976 { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
977 { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
978 { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
979 { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
980 { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
981 { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
982 { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
983 { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
984 { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
985 { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
986 { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
987 { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
988 { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
989 { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
990 { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
991 { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
992 { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
993 { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
994 { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
995 { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
996 { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
997 { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
998 { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
999 { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
1000 { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
1001 { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
1002 { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
1003 { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
1004 { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
1005 { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
1006 { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
1007 { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
1008 { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
1009 { 0xE0100, 0xE01EF }
1012 return bisearch(c
, combining
, sizeof(combining
) / sizeof(struct interval
) - 1);
1016 comb_tofront(root
, i
)
1021 debug1("bring to front: %x\n", i
);
1022 combchars
[combchars
[i
]->prev
]->next
= combchars
[i
]->next
;
1023 combchars
[combchars
[i
]->next
]->prev
= combchars
[i
]->prev
;
1024 combchars
[i
]->next
= combchars
[root
]->next
;
1025 combchars
[i
]->prev
= root
;
1026 combchars
[combchars
[root
]->next
]->prev
= i
;
1027 combchars
[root
]->next
= i
;
1028 i
= combchars
[i
]->c1
;
1029 if (i
< 0xd800 || i
>= 0xe000)
1036 utf8_handle_comb(c
, mc
)
1043 c1
= mc
->image
| (mc
->font
<< 8);
1044 isdouble
= c1
>= 0x1100 && utf8_isdouble(c1
);
1047 combchars
= (struct combchar
**)malloc(sizeof(struct combchar
*) * 0x802);
1050 bzero((char *)combchars
, sizeof(struct combchar
*) * 0x802);
1051 combchars
[0x800] = (struct combchar
*)malloc(sizeof(struct combchar
));
1052 combchars
[0x801] = (struct combchar
*)malloc(sizeof(struct combchar
));
1053 if (!combchars
[0x800] || !combchars
[0x801])
1055 if (combchars
[0x800])
1056 free(combchars
[0x800]);
1057 if (combchars
[0x801])
1058 free(combchars
[0x801]);
1062 combchars
[0x800]->c1
= 0x000;
1063 combchars
[0x800]->c2
= 0x700;
1064 combchars
[0x800]->next
= 0x800;
1065 combchars
[0x800]->prev
= 0x800;
1066 combchars
[0x801]->c1
= 0x700;
1067 combchars
[0x801]->c2
= 0x800;
1068 combchars
[0x801]->next
= 0x801;
1069 combchars
[0x801]->prev
= 0x801;
1071 root
= isdouble
? 0x801 : 0x800;
1072 for (i
= combchars
[root
]->c1
; i
< combchars
[root
]->c2
; i
++)
1076 if (combchars
[i
]->c1
== c1
&& combchars
[i
]->c2
== c
)
1079 if (i
== combchars
[root
]->c2
)
1081 /* full, recycle old entry */
1082 if (c1
>= 0xd800 && c1
< 0xe000)
1083 comb_tofront(root
, c1
- 0xd800);
1084 i
= combchars
[root
]->prev
;
1085 if (c1
== i
+ 0xd800)
1087 /* completely full, can't recycle */
1088 debug("utf8_handle_comp: completely full!\n");
1093 /* FIXME: delete old char from all buffers */
1095 else if (!combchars
[i
])
1097 combchars
[i
] = (struct combchar
*)malloc(sizeof(struct combchar
));
1100 combchars
[i
]->prev
= i
;
1101 combchars
[i
]->next
= i
;
1103 combchars
[i
]->c1
= c1
;
1104 combchars
[i
]->c2
= c
;
1105 mc
->image
= i
& 0xff;
1106 mc
->font
= (i
>> 8) + 0xd8;
1107 debug3("combinig char %x %x -> %x\n", c1
, c
, i
+ 0xd800);
1108 comb_tofront(root
, i
);
1114 WinSwitchEncoding(p
, encoding
)
1118 p
->w_encoding
= encoding
;
1132 c1
= (unsigned char)*s1
;
1133 if (c1
>= 'A' && c1
<= 'Z')
1135 if (!(c1
>= 'a' && c1
<= 'z') && !(c1
>= '0' && c1
<= '9'))
1140 c2
= (unsigned char)*s2
;
1141 if (c2
>= 'A' && c2
<= 'Z')
1143 if (!(c2
>= 'a' && c2
<= 'z') && !(c2
>= '0' && c2
<= '9'))
1163 debug1("FindEncoding %s\n", name
);
1164 if (name
== 0 || *name
== 0)
1166 if (encmatch(name
, "euc"))
1168 if (encmatch(name
, "off") || encmatch(name
, "iso8859-1"))
1171 if (encmatch(name
, "UTF-8"))
1174 for (encoding
= 0; encoding
< (int)(sizeof(encodings
)/sizeof(*encodings
)); encoding
++)
1175 if (encmatch(name
, encodings
[encoding
].name
))
1178 LoadFontTranslationsForEncoding(encoding
);
1186 EncodingName(encoding
)
1189 if (encoding
>= (int)(sizeof(encodings
)/sizeof(*encodings
)))
1191 return encodings
[encoding
].name
;
1195 EncodingDefFont(encoding
)
1198 return encodings
[encoding
].deffont
;
1206 int encoding
= p
->w_encoding
;
1208 c
= encodings
[encoding
].charsets
;
1212 LoadFontTranslationsForEncoding(encoding
);
1214 if (encodings
[encoding
].usegr
)
1217 p
->w_FontE
= encodings
[encoding
].charsets
[1];
1221 if (encodings
[encoding
].noc1
)
1226 DecodeChar(c
, encoding
, statep
)
1233 debug2("Decoding char %02x for encoding %d\n", c
, encoding
);
1235 if (encoding
== UTF8
)
1236 return FromUtf8(c
, statep
);
1238 if (encoding
== SJIS
)
1242 if ((0x81 <= c
&& c
<= 0x9f) || (0xe0 <= c
&& c
<= 0xef))
1249 return c
| (KANA
<< 16);
1254 if (0x40 <= t
&& t
<= 0xfc && t
!= 0x7f)
1257 c
= (c
- 0x81) * 2 + 0x21;
1259 c
= (c
- 0xc1) * 2 + 0x21;
1266 return (c
<< 8) | t
| (KANJI
<< 16);
1270 if (encoding
== EUC_JP
|| encoding
== EUC_KR
|| encoding
== EUC_CN
)
1284 if (encoding
== EUC_JP
)
1287 return t
| (KANA
<< 16);
1290 *statep
= t
| (KANJI0212
<< 8);
1297 if (encoding
== EUC_KR
)
1298 return c
| (3 << 16);
1299 if (encoding
== EUC_CN
)
1300 return c
| (1 << 16);
1301 if (c
& (KANJI0212
<< 16))
1304 return c
| (KANJI
<< 16);
1306 if (encoding
== BIG5
|| encoding
== GBK
)
1312 if (encoding
== GBK
&& c
== 0x80)
1313 return 0xa4 | (('b'|0x80) << 16);
1323 return c
<< 8 | t
| (encoding
== BIG5
? 030 << 16 : 031 << 16);
1325 return c
| (encodings
[encoding
].deffont
<< 16);
1329 EncodeChar(bp
, c
, encoding
, fontp
)
1337 debug2("Encoding char %02x for encoding %d\n", c
, encoding
);
1338 if (c
== -1 && fontp
)
1353 if (encoding
== UTF8
)
1361 c
= (c
>> 8 & 0xff) | (f
<< 8);
1362 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
1367 c
= (c
& 0xff) | (f
<< 8);
1368 c
= recode_char_to_encoding(c
, encoding
);
1371 return ToUtf8(bp
, c
);
1373 if ((c
& 0xff00) && f
== 0) /* is_utf8? */
1376 if (utf8_isdouble(c
))
1379 c
= recode_char_dw_to_encoding(c
, &c2
, encoding
);
1380 c
= (c
<< 8) | (c2
& 0xff);
1385 c
= recode_char_to_encoding(c
, encoding
);
1386 c
= ((c
& 0xff00) << 8) | (c
& 0xff);
1388 debug1("Encode: char mapped from utf8 to %x\n", c
);
1392 if (f
& 0x80) /* map special 96-fonts to latin1 */
1395 if (encoding
== SJIS
)
1398 c
= (c
& 0xff) | 0x80;
1399 else if (f
== KANJI
)
1404 c
= (c
>> 8) & 0xff;
1405 t
+= (c
& 1) ? ((t
<= 0x5f) ? 0x1f : 0x20) : 0x7e;
1406 c
= (c
- 0x21) / 2 + ((c
< 0x5f) ? 0x81 : 0xc1);
1412 if (encoding
== EUC
)
1427 *bp
++ = (c
>> 8) | 0x80;
1443 if ((encoding
== EUC_KR
&& f
== 3) || (encoding
== EUC_CN
&& f
== 1))
1447 *bp
++ = (c
>> 8) | 0x80;
1452 if ((encoding
== BIG5
&& f
== 030) || (encoding
== GBK
&& f
== 031))
1456 *bp
++ = (c
>> 8) | 0x80;
1461 if (encoding
== GBK
&& f
== 0 && c
== 0xa4)
1465 if (fontp
&& f
!= *fontp
)
1505 CanEncodeFont(encoding
, f
)
1515 return f
== KANJI
|| f
== KANA
;
1517 return f
== KANJI
|| f
== KANA
|| f
== KANJI0212
;
1534 PrepareEncodedChar(c
)
1541 encoding
= D_encoding
;
1544 if (encoding
== SJIS
)
1548 else if (f
== KANJI
)
1550 t
+= (c
& 1) ? ((t
<= 0x5f) ? 0x1f : 0x20) : 0x7e;
1551 c
= (c
- 0x21) / 2 + ((c
< 0x5f) ? 0x81 : 0xc1);
1556 if (encoding
== EUC
)
1575 if ((encoding
== EUC_KR
&& f
== 3) || (encoding
== EUC_CN
&& f
== 1))
1580 if ((encoding
== BIG5
&& f
== 030) || (encoding
== GBK
&& f
== 031))
1587 RecodeBuf(fbuf
, flen
, fenc
, tenc
, tbuf
)
1588 unsigned char *fbuf
;
1591 unsigned char *tbuf
;
1594 int decstate
= 0, font
= 0;
1596 for (i
= j
= 0; i
< flen
; i
++)
1599 c
= DecodeChar(c
, fenc
, &decstate
);
1604 j
+= EncodeChar(tbuf
? (char *)tbuf
+ j
: 0, c
, tenc
, &font
);
1606 j
+= EncodeChar(tbuf
? (char *)tbuf
+ j
: 0, -1, tenc
, &font
);
1612 ContainsSpecialDeffont(ml
, xs
, xe
, encoding
)
1617 unsigned char *f
, *i
;
1620 if (encoding
== UTF8
|| encodings
[encoding
].deffont
== 0)
1630 x
= recode_char_to_encoding(c
| (encodings
[encoding
].deffont
<< 8), UTF8
);
1633 debug2("ContainsSpecialDeffont: yes %02x != %02x\n", c
, x
);
1637 debug("ContainsSpecialDeffont: no\n");
1643 LoadFontTranslation(font
, file
)
1647 char buf
[1024], *myfile
;
1652 unsigned short (*p
)[2], (*tab
)[2];
1657 if (font
== 0 || screenencodings
== 0)
1659 if (strlen(screenencodings
) > sizeof(buf
) - 10)
1661 sprintf(buf
, "%s/%02x", screenencodings
, font
& 0xff);
1664 debug1("LoadFontTranslation: trying %s\n", myfile
);
1665 if ((f
= secfopen(myfile
, "r")) == 0)
1671 if (getc(f
) != "ScreenI2UTF8"[i
])
1673 if (getc(f
) != 0) /* format */
1675 fo
= getc(f
); /* id */
1678 if (font
!= -1 && font
!= fo
)
1686 while ((x
= getc(f
)) && x
!= EOF
)
1687 getc(f
); /* skip font name (padded to 2 bytes) */
1688 if ((p
= malloc(sizeof(*p
) * (i
+ 1))) == 0)
1694 x
= x
<< 8 | getc(f
);
1707 if (i
|| (tab
[0][0] & 0x8000))
1712 if (recodetabs
[fo
].tab
&& (recodetabs
[fo
].flags
& RECODETAB_ALLOCED
) != 0)
1713 free(recodetabs
[fo
].tab
);
1714 recodetabs
[fo
].tab
= tab
;
1715 recodetabs
[fo
].flags
= RECODETAB_ALLOCED
;
1716 debug1("Successful load of recodetab %02x\n", fo
);
1728 if (font
!= -1 && file
== 0 && recodetabs
[font
].flags
== 0)
1729 recodetabs
[font
].flags
= RECODETAB_TRIED
;
1734 LoadFontTranslationsForEncoding(encoding
)
1740 debug1("LoadFontTranslationsForEncoding: encoding %d\n", encoding
);
1741 if ((c
= encodings
[encoding
].fontlist
) != 0)
1742 while ((f
= (unsigned char)*c
++) != 0)
1743 if (recodetabs
[f
].flags
== 0)
1744 LoadFontTranslation(f
, 0);
1745 f
= encodings
[encoding
].deffont
;
1746 if (f
> 0 && recodetabs
[f
].flags
== 0)
1747 LoadFontTranslation(f
, 0);
1752 #else /* !ENCODINGS */
1754 /* Simple version of EncodeChar to encode font changes for
1758 EncodeChar(bp
, c
, encoding
, fontp
)
1765 f
= (c
== -1) ? 0 : c
>> 16;
1767 if (fontp
&& f
!= *fontp
)
1808 #endif /* ENCODINGS */