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