1 /* charset.h -- header file for the charset module.
2 Copyright (C) 2003, 2004
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H15PRO112
6 This file is part of the m17n library.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
23 #ifndef _M17N_CHARSET_H_
24 #define _M17N_CHARSET_H_
27 @brief Header for charset handlers.
32 MCHARSET_METHOD_OFFSET,
34 MCHARSET_METHOD_DEFERRED,
35 MCHARSET_METHOD_SUBSET,
36 MCHARSET_METHOD_SUPERSET,
40 /** Structure for charset. */
42 typedef struct MCharset MCharset;
46 /** The value is always 0 because all charsets are static. */
49 /** Symbol indicating the name of the charset. */
52 /** Number of dimensions of the charset. It must be 1, 2, 3, or
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. */
65 /** The minimum code-point calculated from <code_range>. It may be
66 smaller than <min_code>. */
67 int code_range_min_code;
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" -
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];
82 /** Minimum and maximum code-point of the charset. */
83 unsigned min_code, max_code;
85 /** Nonzero means the charset encodes ASCII characters as is. */
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;
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. */
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. */
103 /** Specify how to encode/decode code-point of the charset. It must
104 be Moffset, Mmap, Munify, Msubset, or Msuperset. */
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. */
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. */
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];
127 /* Number of parent charsets. */
130 unsigned subset_min_code, subset_max_code;
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
141 extern MPlist *mcharset__cache;
143 /** Return a charset associated with the symbol CHARSET_SYM. */
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))
154 /** Return index of a character whose code-point in CHARSET is CODE.
155 If CODE is not valid, return -1. */
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)) \
175 /* Return code-point of a character whose index is IDX.
176 The validness of IDX is not checked. IDX may be modified. */
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])) \
186 | (((charset)->code_range[8] \
187 + ((idx) / (charset)->code_range[7] % (charset)->code_range[10])) \
189 | (((charset)->code_range[12] + ((idx) / (charset)->code_range[11])) \
193 /** Return a character whose code-point in CHARSET is CODE. If CODE
194 is invalid, return -1. */
196 #define DECODE_CHAR(charset, code) \
197 (((code) < 128 && (charset)->ascii_compatible) \
199 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
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])
208 /** Return a code-point in CHARSET for character C. If CHARSET
209 does not contain C, return MCHAR_INVALID_CODE. */
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)))
221 extern MCharset *mcharset__ascii;
222 extern MCharset *mcharset__binary;
223 extern MCharset *mcharset__m17n;
224 extern MCharset *mcharset__unicode;
226 #define ISO_MAX_DIMENSION 3
227 #define ISO_MAX_CHARS 2
228 #define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
232 /* Table of ISO-2022 charsets. */
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
240 MCharset *classified[ISO_MAX_DIMENSION][ISO_MAX_CHARS][ISO_MAX_FINAL];
241 } MCharsetISO2022Table;
243 extern MCharsetISO2022Table mcharset__iso_2022_table;
245 #define MCHARSET_ISO_2022(dim, chars, final) \
246 mcharset__iso_2022_table.classified[(dim) - 1][(chars) == 96][(final)]
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 ();
253 #endif /* _M17N_CHARSET_H_ */