]> git.lyx.org Git - lyx.git/blob - 3rdparty/libiconv/1.15/lib/big5hkscs1999.h
Implement auto-nesting.
[lyx.git] / 3rdparty / libiconv / 1.15 / lib / big5hkscs1999.h
1 /*
2  * Copyright (C) 1999-2002, 2006, 2016 Free Software Foundation, Inc.
3  * This file is part of the GNU LIBICONV Library.
4  *
5  * The GNU LIBICONV Library is free software; you can redistribute it
6  * and/or modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * The GNU LIBICONV Library is distributed in the hope that it will be
11  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17  * If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * BIG5-HKSCS:1999
22  */
23
24 /*
25  * BIG5-HKSCS:1999 can be downloaded from
26  *   http://www.info.gov.hk/digital21/eng/hkscs/download.html
27  *   http://www.info.gov.hk/digital21/eng/hkscs/index.html
28  *
29  * It extends BIG5 (without the rows 0xC6..0xC7) through the ranges
30  *
31  *   0x{88..8D}{40..7E,A1..FE}      641 characters
32  *   0x{8E..A0}{40..7E,A1..FE}     2898 characters
33  *   0x{C6..C8}{40..7E,A1..FE}      359 characters
34  *   0xF9{D6..FE}                    41 characters
35  *   0x{FA..FE}{40..7E,A1..FE}      763 characters
36  *
37  * Note that some HKSCS characters are not contained in Unicode 3.2
38  * and are therefore best represented as sequences of Unicode characters:
39  *   0x8862  U+00CA U+0304  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND MACRON
40  *   0x8864  U+00CA U+030C  LATIN CAPITAL LETTER E WITH CIRCUMFLEX AND CARON
41  *   0x88A3  U+00EA U+0304  LATIN SMALL LETTER E WITH CIRCUMFLEX AND MACRON
42  *   0x88A5  U+00EA U+030C  LATIN SMALL LETTER E WITH CIRCUMFLEX AND CARON
43  */
44
45 #include "hkscs1999.h"
46 #include "flushwc.h"
47
48 static int
49 big5hkscs1999_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
50 {
51   ucs4_t last_wc = conv->istate;
52   if (last_wc) {
53     /* Output the buffered character. */
54     conv->istate = 0;
55     *pwc = last_wc;
56     return 0; /* Don't advance the input pointer. */
57   } else {
58     unsigned char c = *s;
59     /* Code set 0 (ASCII) */
60     if (c < 0x80)
61       return ascii_mbtowc(conv,pwc,s,n);
62     /* Code set 1 (BIG5 extended) */
63     if (c >= 0xa1 && c < 0xff) {
64       if (n < 2)
65         return RET_TOOFEW(0);
66       {
67         unsigned char c2 = s[1];
68         if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
69           if (!((c == 0xc6 && c2 >= 0xa1) || c == 0xc7)) {
70             int ret = big5_mbtowc(conv,pwc,s,2);
71             if (ret != RET_ILSEQ)
72               return ret;
73           }
74         }
75       }
76     }
77     {
78       int ret = hkscs1999_mbtowc(conv,pwc,s,n);
79       if (ret != RET_ILSEQ)
80         return ret;
81     }
82     if (c == 0x88) {
83       if (n < 2)
84         return RET_TOOFEW(0);
85       {
86         unsigned char c2 = s[1];
87         if (c2 == 0x62 || c2 == 0x64 || c2 == 0xa3 || c2 == 0xa5) {
88           /* It's a composed character. */
89           ucs4_t wc1 = ((c2 >> 3) << 2) + 0x009a; /* = 0x00ca or 0x00ea */
90           ucs4_t wc2 = ((c2 & 6) << 2) + 0x02fc; /* = 0x0304 or 0x030c */
91           /* We cannot output two Unicode characters at once. So,
92              output the first character and buffer the second one. */
93           *pwc = wc1;
94           conv->istate = wc2;
95           return 2;
96         }
97       }
98     }
99     return RET_ILSEQ;
100   }
101 }
102
103 #define big5hkscs1999_flushwc normal_flushwc
104
105 static int
106 big5hkscs1999_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
107 {
108   int count = 0;
109   unsigned char last = conv->ostate;
110
111   if (last) {
112     /* last is = 0x66 or = 0xa7. */
113     if (wc == 0x0304 || wc == 0x030c) {
114       /* Output the combined character. */
115       if (n >= 2) {
116         r[0] = 0x88;
117         r[1] = last + ((wc & 24) >> 2) - 4; /* = 0x62 or 0x64 or 0xa3 or 0xa5 */
118         conv->ostate = 0;
119         return 2;
120       } else
121         return RET_TOOSMALL;
122     }
123
124     /* Output the buffered character. */
125     if (n < 2)
126       return RET_TOOSMALL;
127     r[0] = 0x88;
128     r[1] = last;
129     r += 2;
130     count = 2;
131   }
132
133   /* Code set 0 (ASCII) */
134   if (wc < 0x0080) {
135     /* Plain ASCII character. */
136     if (n > count) {
137       r[0] = (unsigned char) wc;
138       conv->ostate = 0;
139       return count+1;
140     } else
141       return RET_TOOSMALL;
142   } else {
143     unsigned char buf[2];
144     int ret;
145
146     /* Code set 1 (BIG5 extended) */
147     ret = big5_wctomb(conv,buf,wc,2);
148     if (ret != RET_ILUNI) {
149       if (ret != 2) abort();
150       if (!((buf[0] == 0xc6 && buf[1] >= 0xa1) || buf[0] == 0xc7)) {
151         if (n >= count+2) {
152           r[0] = buf[0];
153           r[1] = buf[1];
154           conv->ostate = 0;
155           return count+2;
156         } else
157           return RET_TOOSMALL;
158       }
159     }
160     ret = hkscs1999_wctomb(conv,buf,wc,2);
161     if (ret != RET_ILUNI) {
162       if (ret != 2) abort();
163       if ((wc & ~0x0020) == 0x00ca) {
164         /* A possible first character of a multi-character sequence. We have to
165            buffer it. */
166         if (!(buf[0] == 0x88 && (buf[1] == 0x66 || buf[1] == 0xa7))) abort();
167         conv->ostate = buf[1]; /* = 0x66 or = 0xa7 */
168         return count+0;
169       }
170       if (n >= count+2) {
171         r[0] = buf[0];
172         r[1] = buf[1];
173         conv->ostate = 0;
174         return count+2;
175       } else
176         return RET_TOOSMALL;
177     }
178     return RET_ILUNI;
179   }
180 }
181
182 static int
183 big5hkscs1999_reset (conv_t conv, unsigned char *r, size_t n)
184 {
185   unsigned char last = conv->ostate;
186
187   if (last) {
188     if (n < 2)
189       return RET_TOOSMALL;
190     r[0] = 0x88;
191     r[1] = last;
192     /* conv->ostate = 0; will be done by the caller */
193     return 2;
194   } else
195     return 0;
196 }