Update copyright years
[m17n/m17n-lib.git] / src / charset.h
1 /* charset.h -- header file for the charset module.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    02111-1307, USA.  */
22
23 #ifndef _M17N_CHARSET_H_
24 #define _M17N_CHARSET_H_
25
26 /** @file charset.h
27     @brief Header for charset handlers.
28 */
29
30 enum mcharset_method
31   {
32     MCHARSET_METHOD_OFFSET,
33     MCHARSET_METHOD_MAP,
34     MCHARSET_METHOD_DEFERRED,
35     MCHARSET_METHOD_SUBSET,
36     MCHARSET_METHOD_SUPERSET,
37     MCHARSET_METHOD_MAX
38   };
39
40 /** Structure for charset.  */
41
42 typedef struct MCharset MCharset;
43
44 struct MCharset
45 {
46   /** The value is always 0 because all charsets are static.  */
47   unsigned ref_count;
48
49   /** Symbol indicating the name of the charset.  */
50   MSymbol name;
51
52   /** Number of dimensions of the charset.  It must be 1, 2, 3, or
53       4.  */
54   int dimension;
55
56   /** Byte code range of each dimension.  <code_range>[4N] is a
57       minimum byte code of the (N+1)th dimension, <code_range>[4N+1]
58       is a maximum byte code of the (N+1)th dimension,
59       <code_range>[4N+2] is (<code_range>[4N+1] - <code_range>[4N] +
60       1), <code_range>[4N+3] is a number of characters contained in the
61       first to (N+1)th dimensions.  We get "char-index" of a
62       "code-point" from this information.  */
63   int code_range[16];
64
65   /** The minimum code-point calculated from <code_range>.  It may be
66       smaller than <min_code>.  */
67   int code_range_min_code;
68
69   /** Nonzero means there is no gap in code points of the charset.  If
70       <dimension> is 1, <no_code_gap> is always 1.  Otherwise,
71       <no_code_gap> is 1 iff <code_range>[4N] is zero and
72       <code_range>[4N+1] is 256 for N = 0..<dimension>-2.  If
73       <no_code_gap> is nonzero, "char-index" is "code-point" -
74       <min_code>.  */
75   int no_code_gap;
76
77   /** If the byte code B is valid in the (N+1)th dimension,
78       (<code_range_mask>[B] & (1 << N)) is 1.  Otherwise,
79       (<code_range_mask>[B] & (1 << N)) is 0.  */
80   unsigned char code_range_mask[256];
81
82   /** Minimum and maximum code-point of the charset.  */
83   unsigned min_code, max_code;
84
85   /** Nonzero means the charset encodes ASCII characters as is.  */
86   int ascii_compatible;
87
88   /** Minimum and maximum character of the charset.  If
89       <ascii_compatible> is nonzero, <min_char> is actually the
90       minimum non-ASCII character of the charset.  */
91   int min_char, max_char;
92
93   /** ISO 2022 final byte of the charset.  It must be in the range
94       48..127, or -1.  The value -1 means that the charset is not
95       encodable by ISO 2022 based coding systems.  */
96   int final_byte;
97
98   /** ISO 2022 revision number of the charset, or -1.  The value -1
99       means that the charset has no revision number.  Used only when
100       <final_byte> is not -1.  */
101   int revision;
102
103   /** Specify how to encode/decode code-point of the charset.  It must
104       be Moffset, Mmap, Munify, Msubset, or Msuperset.  */
105   MSymbol method;
106
107   /** Array of integers to decode a code-point of the charset.  It is
108       indexed by a "char-index" of the code-point, and the
109       corresponding element is a character of the charset, or -1 if
110       the code point is not valid in the charset.  Used only when
111       <method> is Mmap or Munify.  */
112   int *decoder;
113
114   /** Char-table to encode a character of the charset.  It is indexed
115       by a character code, and the corresponding element is a code
116       point of the character in the charset, or
117       MCHAR_INVALID_CODE if the character is not included in the
118       charset.  Used only when <method> is Mmap or Munify.  */
119   MCharTable *encoder;
120
121   int unified_max;
122
123   /** Array of pointers to parent charsets.  Used only when <method>
124       is Msubset or Msuperset.  Atmost 8 parents are supported.  */
125   MCharset *parents[8];
126
127   /* Number of parent charsets.  */
128   int nparents;
129
130   unsigned subset_min_code, subset_max_code;
131   int subset_offset;
132
133   int simple;
134
135   /** If the charset is fully loaded (i.e. all the above member are
136       set to correct values), the value is 1.  Otherwise, the value is
137       0.  */
138   int fully_loaded;
139 };
140
141 extern MPlist *mcharset__cache;
142
143 /** Return a charset associated with the symbol CHARSET_SYM.  */
144
145 #define MCHARSET(charset_sym)                                   \
146   (((charset_sym) == MPLIST_KEY (mcharset__cache)               \
147     || (MPLIST_KEY (mcharset__cache) = (charset_sym),           \
148         MPLIST_VAL (mcharset__cache)                            \
149         = (MCharset *) msymbol_get ((charset_sym), Mcharset)))  \
150    ? MPLIST_VAL (mcharset__cache)                               \
151    : mcharset__find (charset_sym))
152
153
154 /** Return index of a character whose code-point in CHARSET is CODE.
155     If CODE is not valid, return -1.  */
156
157 #define CODE_POINT_TO_INDEX(charset, code)                              \
158   ((charset)->no_code_gap                                               \
159    ? (code) - (charset)->min_code                                       \
160    : (((charset)->code_range_mask[(code) >> 24] & 0x8)                  \
161       && ((charset)->code_range_mask[((code) >> 16) & 0xFF] & 0x4)      \
162       && ((charset)->code_range_mask[((code) >> 8) & 0xFF] & 0x2)       \
163       && ((charset)->code_range_mask[(code) & 0xFF] & 0x1))             \
164    ? (((((code) >> 24) - (charset)->code_range[12])                     \
165        * (charset)->code_range[11])                                     \
166       + (((((code) >> 16) & 0xFF) - (charset)->code_range[8])           \
167          * (charset)->code_range[7])                                    \
168       + (((((code) >> 8) & 0xFF) - (charset)->code_range[4])            \
169          * (charset)->code_range[3])                                    \
170       + (((code) & 0xFF) - (charset)->code_range[0])                    \
171       - ((charset)->min_code - (charset)->code_range_min_code))         \
172    : -1)
173
174
175 /* Return code-point of a character whose index is IDX.  
176    The validness of IDX is not checked.  IDX may be modified.  */
177
178 #define INDEX_TO_CODE_POINT(charset, idx)                                    \
179   ((charset)->no_code_gap                                                    \
180    ? (idx) + (charset)->min_code                                             \
181    : (idx += (charset)->min_code - (charset)->code_range_min_code,           \
182       (((charset)->code_range[0] + (idx) % (charset)->code_range[2])         \
183        | (((charset)->code_range[4]                                          \
184            + ((idx) / (charset)->code_range[3] % (charset)->code_range[6]))  \
185           << 8)                                                              \
186        | (((charset)->code_range[8]                                          \
187            + ((idx) / (charset)->code_range[7] % (charset)->code_range[10])) \
188           << 16)                                                             \
189        | (((charset)->code_range[12] + ((idx) / (charset)->code_range[11]))  \
190           << 24))))
191
192
193 /** Return a character whose code-point in CHARSET is CODE.  If CODE
194     is invalid, return -1.  */
195
196 #define DECODE_CHAR(charset, code)                                      \
197   (((code) < 128 && (charset)->ascii_compatible)                        \
198    ? (int) (code)                                                       \
199    : ((code) < (charset)->min_code || (code) > (charset)->max_code)     \
200    ? -1                                                                 \
201    : ! (charset)->simple                                                \
202    ? mcharset__decode_char ((charset), (code))                          \
203    : (charset)->method == Moffset                                       \
204    ? (code) - (charset)->min_code + (charset)->min_char                 \
205    : (charset)->decoder[(code) - (charset)->min_code])
206
207
208 /** Return a code-point in CHARSET for character C.  If CHARSET
209     does not contain C, return MCHAR_INVALID_CODE.  */
210
211 #define ENCODE_CHAR(charset, c)                                 \
212   (! (charset)->simple                                          \
213    ? mcharset__encode_char ((charset), (c))                     \
214    : ((c) < (charset)->min_char || (c) > (charset)->max_char)   \
215    ? MCHAR_INVALID_CODE                                         \
216    : (charset)->method == Moffset                               \
217    ? (c) - (charset)->min_char + (charset)->min_code            \
218    : (unsigned) mchartable_lookup ((charset)->encoder, (c)))
219
220
221 extern MCharset *mcharset__ascii;
222 extern MCharset *mcharset__binary;
223 extern MCharset *mcharset__m17n;
224 extern MCharset *mcharset__unicode;
225
226 #define ISO_MAX_DIMENSION 3
227 #define ISO_MAX_CHARS 2
228 #define ISO_MAX_FINAL 0x80      /* only 0x30..0xFF are used */
229
230 typedef struct
231 {
232   /* Table of ISO-2022 charsets.  */
233   int size, inc, used;
234   MCharset **charsets;
235
236   /** A 3-dimensional table indexed by "dimension", "chars", and
237       "final byte" of an ISO-2022 charset to get the correponding
238       charset.  A charset that has a revision number is not stored in
239       this table.  */
240   MCharset *classified[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
241 } MCharsetISO2022Table;
242
243 extern MCharsetISO2022Table mcharset__iso_2022_table;
244
245 #define MCHARSET_ISO_2022(dim, chars, final) \
246   mcharset__iso_2022_table.classified[(dim) - 1][(chars) == 96][(final)]
247
248 extern MCharset *mcharset__find (MSymbol name);
249 extern int mcharset__decode_char (MCharset *charset, unsigned code);
250 extern unsigned mcharset__encode_char (MCharset *charset, int c);
251 extern int mcharset__load_from_database ();
252
253 #endif /* _M17N_CHARSET_H_ */