big dialogs: set_curosr2 -> set_dlg_cursor.
[elinks.git] / src / intl / charsets.h
blobd87e2ee454ec3fc6844aa2387414f38f5a176c16
1 #ifndef EL__INTL_CHARSETS_H
2 #define EL__INTL_CHARSETS_H
4 struct hash;
5 typedef uint32_t unicode_val_T;
7 /* U+0020 SPACE. Normally the same as ' ' or L' ' but perhaps ELinks
8 * shouldn't rely on that. */
9 #define UCS_SPACE ((unicode_val_T) 0x0020)
11 /* U+00A0 NO-BREAK SPACE. */
12 #define UCS_NO_BREAK_SPACE ((unicode_val_T) 0x00A0)
14 /* U+00AD SOFT HYPHEN. */
15 #define UCS_SOFT_HYPHEN ((unicode_val_T) 0x00AD)
17 /* U+FFFD REPLACEMENT CHARACTER. Used when no Unicode mapping is
18 * known for a byte in a codepage, or when invalid UTF-8 is received
19 * from a terminal. After generating the character, ELinks then
20 * treats it like any other Unicode character. The user can also type
21 * this character directly, and it can occur in documents. */
22 #define UCS_REPLACEMENT_CHARACTER ((unicode_val_T) 0xFFFD)
24 /* A special value that fits in unicode_val_T but is outside the range
25 * of Unicode characters. utf8_to_unicode and cp_to_unicode return
26 * this if the input is too short. This is also used as a placeholder
27 * for the second cell of a double-cell character. */
28 #define UCS_NO_CHAR ((unicode_val_T) 0xFFFFFFFD)
30 #ifdef CONFIG_COMBINE
31 #define UCS_END_COMBINED ((unicode_val_T) 0xFFFFFFFC)
33 #define UCS_BEGIN_COMBINED ((unicode_val_T) (UCS_END_COMBINED - (unicode_val_T) 10000))
35 /* Base character and up to 5 combining characters. */
36 #define UCS_MAX_LENGTH_COMBINED 6
37 #endif /* CONFIG_COMBINE */
39 /* If ELinks should display a double-cell character but there is only
40 * one cell available, it displays this character instead. This must
41 * be a single-cell character but need not be unique. Possible values
42 * might be U+0020 SPACE or U+303F IDEOGRAPHIC HALF FILL SPACE.
44 * Some BFU widgets (at least input fields and list boxes) currently
45 * ignore this setting and use U+0020 instead. (They first draw spaces
46 * and then overwrite with text; look for utf8_cells2bytes calls.)
47 * We should fix that if we ever change the value. */
48 #define UCS_ORPHAN_CELL ((unicode_val_T) 0x20)
50 /*   replacement character. See u2cp().
51 * UTF-8 strings should use the encoding of U+00A0 instead. */
52 #define NBSP_CHAR ((unsigned char) 1)
53 #define NBSP_CHAR_STRING "\001"
55 /* How to convert a byte from a source charset. This is used in an
56 * array (struct conv_table[256]) indexed by the byte value. */
57 struct conv_table {
58 /* 0 if this is the final byte of a character, or 1 if more
59 * bytes are needed. */
60 int t;
61 union {
62 /* If @t==0: a null-terminated string that is the
63 * corresponding character in the target charset.
64 * Normally, the string is statically allocated.
65 * However, if the conversion table is to UTF-8, then
66 * the strings in elements 0x80 to 0xFF are allocated
67 * with @mem_alloc and owned by the table. */
68 const unsigned char *str;
69 /* If @t==1: a pointer to a nested conversion table
70 * (with 256 elements) that describes how to convert
71 * each possible subsequent byte. The conversion
72 * table owns the nested conversion table. */
73 struct conv_table *tbl;
74 } u;
77 enum convert_string_mode {
78 CSM_DEFAULT, /* Convert any char. */
79 CSM_QUERY, /* Special handling of '&' and '=' chars. */
80 CSM_FORM, /* Special handling of '&' and '=' chars in forms. */
81 CSM_NONE, /* Convert nothing. */
84 /* How to translate U+00A0 NO-BREAK SPACE. If u2cp_ is converting to
85 * UTF-8, it ignores this choice and just encodes the U+00A0. */
86 enum nbsp_mode {
87 /* Convert to NBSP_CHAR. This lets the HTML renderer
88 * recognize nbsp even if the codepage doesn't support
89 * nbsp. (VISCII doesn't.) */
90 NBSP_MODE_HACK = 0,
92 /* Convert to normal ASCII space. */
93 NBSP_MODE_ASCII = 1
96 struct conv_table *get_translation_table(int, int);
97 const unsigned char *get_entity_string(const unsigned char *str,
98 const int strlen, int encoding);
100 /* The convert_string() name is also used by Samba (version 3.0.3), which
101 * provides libnss_wins.so.2, which is called somewhere inside
102 * _nss_wins_gethostbyname_r(). This name clash causes the elinks hostname
103 * lookup thread to crash so we need to rename the symbol. */
104 /* Reported by Derek Poon and filed as bug 453 */
105 #undef convert_string
106 #define convert_string convert_string_elinks
108 /* This routine converts a string from one charset to another according to the
109 * passed @convert_table, potentially also decoding SGML (HTML) entities along
110 * the way (according to @mode). It either returns dynamically allocated
111 * converted string of length @length, or if the @callback is non-NULL it calls
112 * it each few bytes instead and always returns NULL (@length is undefined).
113 * Note that it's ok not to care and pass NULL as @length. */
114 unsigned char *convert_string(struct conv_table *convert_table,
115 unsigned char *chars, int charslen, int cp,
116 enum convert_string_mode mode, int *length,
117 void (*callback)(void *data, unsigned char *buf, int buflen),
118 void *callback_data);
120 int get_cp_index(const unsigned char *);
121 unsigned char *get_cp_name(int);
122 unsigned char *get_cp_config_name(int);
123 unsigned char *get_cp_mime_name(int);
124 int is_cp_utf8(int);
125 void free_conv_table(void);
126 #ifdef CONFIG_UTF8
127 inline unsigned char *encode_utf8(unicode_val_T);
128 inline unsigned char *utf8_prevchar(unsigned char *, int, unsigned char *);
129 inline int utf8charlen(const unsigned char *);
130 int utf8_char2cells(unsigned char *, unsigned char *);
131 int utf8_ptr2cells(unsigned char *, unsigned char *);
132 int utf8_ptr2chars(unsigned char *, unsigned char *);
133 int utf8_cells2bytes(unsigned char *, int, unsigned char *);
134 /* How utf8_step_forward and utf8_step_backward count steps. */
135 enum utf8_step {
136 /* Each step is one character, even if it is a combining or
137 * double-width character. */
138 UTF8_STEP_CHARACTERS,
140 /* Each step is one cell. If the specified number of steps
141 * would end in the middle of a double-width character, do not
142 * include the character. */
143 UTF8_STEP_CELLS_FEWER,
145 /* Each step is one cell. If the specified number of steps
146 * would end in the middle of a double-width character,
147 * include the whole character. */
148 UTF8_STEP_CELLS_MORE
150 unsigned char *utf8_step_forward(unsigned char *, unsigned char *,
151 int, enum utf8_step, int *);
152 unsigned char *utf8_step_backward(unsigned char *, unsigned char *,
153 int, enum utf8_step, int *);
154 inline int unicode_to_cell(unicode_val_T);
155 unicode_val_T unicode_fold_label_case(unicode_val_T);
156 inline int strlen_utf8(unsigned char **);
157 inline unicode_val_T utf8_to_unicode(unsigned char **, const unsigned char *);
158 unicode_val_T cp_to_unicode(int, unsigned char **, unsigned char *);
159 #endif /* CONFIG_UTF8 */
161 #ifdef CONFIG_COMBINE
162 extern unicode_val_T last_combined;
163 extern unicode_val_T **combined;
164 extern struct hash *combined_hash;
165 unicode_val_T get_combined(unicode_val_T *, int);
166 void free_combined();
167 #endif /* CONFIG_COMBINE */
169 unicode_val_T cp2u(int, unsigned char);
170 const unsigned char *cp2utf8(int, int);
172 const unsigned char *u2cp_(unicode_val_T, int, enum nbsp_mode);
173 #define u2cp(u, to) u2cp_(u, to, NBSP_MODE_HACK)
174 #define u2cp_no_nbsp(u, to) u2cp_(u, to, NBSP_MODE_ASCII)
176 void init_charsets_lookup(void);
177 void free_charsets_lookup(void);
179 /* UTF-16 encodes each Unicode character U+0000...U+FFFF as a single
180 * 16-bit code unit, and each character U+10000...U+10FFFF as a pair
181 * of two code units: a high surrogate followed by a low surrogate.
182 * The range U+D800...U+DFFF is reserved for these surrogates. */
183 #define is_utf16_surrogate(u) (((u) & 0xFFFFF800) == 0xD800)
184 #define is_utf16_high_surrogate(u) (((u) & 0xFFFFFC00) == 0xD800)
185 #define is_utf16_low_surrogate(u) (((u) & 0xFFFFFC00) == 0xDC00)
186 #define join_utf16_surrogates(high,low) (0x10000 + (((high) - 0xD800L) << 10) + ((low) - 0xDC00))
187 #define needs_utf16_surrogates(u) ((uint32_t) ((u) - 0x10000) < 0x100000)
188 #define get_utf16_high_surrogate(u) (0xD800 + (((u) - 0x10000) >> 10))
189 #define get_utf16_low_surrogate(u) (0xDC00 + ((u) & 0x3FF))
191 #endif