source: libcaca/trunk/cucul/charset.c @ 1231

Last change on this file since 1231 was 1231, checked in by Sam Hocevar, 13 years ago
  • Removed "This function..." constructs from documentation. Fixed a few documentation errors or imprecisions.
  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: charset.c 1231 2006-10-25 22:06:21Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains functions for converting characters between
16 *  various character sets.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <string.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29/*
30 * UTF-8 handling
31 */
32
33static char const trailing[256] =
34{
35    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
36    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
37    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
39    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
41    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
42    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
43};
44
45static uint32_t const offsets[6] =
46{
47    0x00000000UL, 0x00003080UL, 0x000E2080UL,
48    0x03C82080UL, 0xFA082080UL, 0x82082080UL
49};
50
51/*
52 * CP437 handling
53 */
54
55static uint32_t const cp437_lookup1[] =
56{
57    /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
58            0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
59    0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
60    /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
61    0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
62    0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
63};
64
65static uint32_t const cp437_lookup2[] =
66{
67    /* 0x7f: ⌂ */
68    0x2302,
69    /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
70    0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
71    0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
72    /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
73    0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
74    0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
75    /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
76    0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
77    0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
78    /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
79    0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
80    0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
81    /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
82    0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
83    0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
84    /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
85    0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
86    0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
87    /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
88    0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
89    0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
90    /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
91    0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
92    0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
93};
94
95/** \brief Convert a UTF-8 character to UTF-32.
96 *
97 *  Convert a UTF-8 character read from a string and return its value in
98 *  the UTF-32 character set. If the second argument is not null, the total
99 *  number of read bytes is written in it.
100 *
101 *  If a null byte was reached before the expected end of the UTF-8 sequence,
102 *  this function returns zero and the number of read bytes is set to zero.
103 *
104 *  This function never fails, but its behaviour with illegal UTF-8 sequences
105 *  is undefined.
106 *
107 *  \param s A string containing the UTF-8 character.
108 *  \param read A pointer to an unsigned integer to store the number of
109 *  bytes in the character, or NULL.
110 *  \return The corresponding UTF-32 character, or zero if the character
111 *  is incomplete.
112 */
113unsigned long int cucul_utf8_to_utf32(char const *s, unsigned int *read)
114{
115    unsigned int bytes = trailing[(int)(unsigned char)*s];
116    unsigned int i = 0;
117    uint32_t ret = 0;
118
119    for(;;)
120    {
121        if(!*s)
122        {
123            if(read)
124                *read = 0;
125            return 0;
126        }
127
128        ret += ((uint32_t)(unsigned char)*s++) << (6 * (bytes - i));
129
130        if(bytes == i++)
131        {
132            if(read)
133                *read = i;
134            return ret - offsets[bytes];
135        }
136    }
137}
138
139/** \brief Convert a UTF-32 character to UTF-8.
140 *
141 *  Convert a UTF-32 character read from a string and write its value in
142 *  the UTF-8 character set into the given buffer.
143 *
144 *  This function never fails, but its behaviour with illegal UTF-32 characters
145 *  is undefined.
146 *
147 *  \param buf A pointer to a character buffer where the UTF-8 sequence will
148 *  be written.
149 *  \param ch The UTF-32 character.
150 *  \return The number of bytes written.
151 */
152unsigned int cucul_utf32_to_utf8(char *buf, unsigned long int ch)
153{
154    static const uint8_t mark[7] =
155    {
156        0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
157    };
158
159    char *parser = buf;
160    int bytes;
161
162    if(ch < 0x80)
163    {
164        *parser++ = ch;
165        return 1;
166    }
167
168    bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
169    parser += bytes;
170
171    switch(bytes)
172    {
173        case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
174        case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
175        case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
176    }
177    *--parser = ch | mark[bytes];
178
179    return bytes;
180}
181
182/** \brief Convert a UTF-32 character to CP437.
183 *
184 *  Convert a UTF-32 character read from a string and return its value in
185 *  the CP437 character set, or "?" if the character has no equivalent.
186 *
187 *  This function never fails.
188 *
189 *  \param ch The UTF-32 character.
190 *  \return The corresponding CP437 character, or "?" if not representable.
191 */
192unsigned char cucul_utf32_to_cp437(unsigned long int ch)
193{
194    unsigned int i;
195
196    if(ch < 0x00000020)
197        return '?';
198
199    if(ch < 0x00000080)
200        return ch;
201
202    for(i = 0; i < sizeof(cp437_lookup1) / sizeof(*cp437_lookup1); i++)
203        if(cp437_lookup1[i] == ch)
204            return 0x01 + i;
205
206    for(i = 0; i < sizeof(cp437_lookup2) / sizeof(*cp437_lookup2); i++)
207        if(cp437_lookup2[i] == ch)
208            return 0x7f + i;
209
210    return '?';
211}
212
213/** \brief Convert a CP437 character to UTF-32.
214 *
215 *  Convert a CP437 character read from a string and return its value in
216 *  the UTF-32 character set, or zero if the character is a CP437 control
217 *  character.
218 *
219 *  This function never fails.
220 *
221 *  \param ch The CP437 character.
222 *  \return The corresponding UTF-32 character, or zero if not representable.
223 */
224unsigned long int cucul_cp437_to_utf32(unsigned char ch)
225{
226    if(ch > 0x7f)
227        return cp437_lookup2[ch - 0x7f];
228
229    if(ch >= 0x20)
230        return (uint32_t)ch;
231
232    if(ch > 0)
233        return cp437_lookup1[ch - 0x01];
234
235    return 0x00000000;
236}
237
238/** \brief Tell whether a UTF-32 character is fullwidth.
239 *
240 *  Check whether the given UTF-32 character should be printed at twice
241 *  the normal width (fullwidth characters). If the character is unknown
242 *  or if its status cannot be decided, it is treated as a standard-width
243 *  character.
244 *
245 *  This function never fails.
246 *
247 *  \param ch The UTF-32 character.
248 *  \return 1 if the character is fullwidth, 0 otherwise.
249 */
250int cucul_utf32_is_fullwidth(unsigned long int ch)
251{
252    if(ch < 0x2e80) /* Standard stuff */
253        return 0;
254    if(ch < 0xa700) /* Japanese, Korean, CJK, Yi... */
255        return 1;
256    if(ch < 0xac00) /* Modified Tone Letters, Syloti Nagri */
257        return 0;
258    if(ch < 0xd800) /* Hangul Syllables */
259        return 1;
260    if(ch < 0xf900) /* Misc crap */
261        return 0;
262    if(ch < 0xfb00) /* More CJK */
263        return 1;
264    if(ch < 0xfe20) /* Misc crap */
265        return 0;
266    if(ch < 0xfe70) /* More CJK */
267        return 1;
268    if(ch < 0xff00) /* Misc crap */
269        return 0;
270    if(ch < 0xff61) /* Fullwidth forms */
271        return 1;
272    if(ch < 0xffe0) /* Halfwidth forms */
273        return 0;
274    if(ch < 0xffe8) /* More fullwidth forms */
275        return 1;
276    if(ch < 0x20000) /* Misc crap */
277        return 0;
278    if(ch < 0xe0000) /* More CJK */
279        return 1;
280    return 0;
281}
282
283/*
284 * XXX: The following functions are local.
285 */
286
287unsigned int _cucul_strlen_utf8(char const *s)
288{
289    int len = 0;
290    char const *parser = s;
291
292    while(*parser)
293    {
294        int i;
295        int bytes = 1 + trailing[(int)(unsigned char)*parser];
296
297        for(i = 1; i < bytes; i++)
298            if(!parser[i])
299                return len;
300        parser += bytes;
301        len++;
302    }
303
304    return len;
305}
306
307char const *_cucul_skip_utf8(char const *s, unsigned int x)
308{
309    char const *parser = s;
310
311    while(x)
312    {
313        int i;
314        int bytes = 1 + trailing[(int)(unsigned char)*parser];
315
316        for(i = 1; i < bytes; i++)
317            if(!parser[i])
318                return parser;
319        parser += bytes;
320        x--;
321    }
322
323    return parser;
324}
325
Note: See TracBrowser for help on using the repository browser.