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

Last change on this file since 2128 was 2128, checked in by Sam Hocevar, 12 years ago
  • Slight improvement to the Unicode -> ASCII conversion, courtesy of Ben Wiley Sittler.
  • Property svn:keywords set to Id
File size: 12.4 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2007 Sam Hocevar <sam@zoy.org>
4 *                2007 Ben Wiley Sittler <bsittler@gmail.com>
5 *                All Rights Reserved
6 *
7 *  $Id: charset.c 2128 2007-12-07 09:33:23Z sam $
8 *
9 *  This library is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16/*
17 *  This file contains functions for converting characters between
18 *  various character sets.
19 */
20
21#include "config.h"
22#include "common.h"
23
24#if !defined(__KERNEL__)
25#   include <string.h>
26#endif
27
28#include "cucul.h"
29#include "cucul_internals.h"
30
31/*
32 * UTF-8 handling
33 */
34
35static char const trailing[256] =
36{
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    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,
42    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,
43    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,
44    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
45};
46
47static uint32_t const offsets[6] =
48{
49    0x00000000UL, 0x00003080UL, 0x000E2080UL,
50    0x03C82080UL, 0xFA082080UL, 0x82082080UL
51};
52
53/*
54 * CP437 handling
55 */
56
57static uint32_t const cp437_lookup1[] =
58{
59    /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
60            0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
61    0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
62    /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
63    0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
64    0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
65};
66
67static uint32_t const cp437_lookup2[] =
68{
69    /* 0x7f: ⌂ */
70    0x2302,
71    /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
72    0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
73    0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
74    /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
75    0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
76    0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
77    /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
78    0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
79    0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
80    /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
81    0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
82    0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
83    /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
84    0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
85    0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
86    /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
87    0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
88    0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
89    /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
90    0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
91    0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
92    /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
93    0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
94    0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
95};
96
97/** \brief Convert a UTF-8 character to UTF-32.
98 *
99 *  Convert a UTF-8 character read from a string and return its value in
100 *  the UTF-32 character set. If the second argument is not null, the total
101 *  number of read bytes is written in it.
102 *
103 *  If a null byte was reached before the expected end of the UTF-8 sequence,
104 *  this function returns zero and the number of read bytes is set to zero.
105 *
106 *  This function never fails, but its behaviour with illegal UTF-8 sequences
107 *  is undefined.
108 *
109 *  \param s A string containing the UTF-8 character.
110 *  \param read A pointer to an unsigned integer to store the number of
111 *  bytes in the character, or NULL.
112 *  \return The corresponding UTF-32 character, or zero if the character
113 *  is incomplete.
114 */
115unsigned long int cucul_utf8_to_utf32(char const *s, unsigned int *read)
116{
117    unsigned int bytes = trailing[(int)(unsigned char)*s];
118    unsigned int i = 0;
119    uint32_t ret = 0;
120
121    for(;;)
122    {
123        if(!*s)
124        {
125            if(read)
126                *read = 0;
127            return 0;
128        }
129
130        ret += ((uint32_t)(unsigned char)*s++) << (6 * (bytes - i));
131
132        if(bytes == i++)
133        {
134            if(read)
135                *read = i;
136            return ret - offsets[bytes];
137        }
138    }
139}
140
141/** \brief Convert a UTF-32 character to UTF-8.
142 *
143 *  Convert a UTF-32 character read from a string and write its value in
144 *  the UTF-8 character set into the given buffer.
145 *
146 *  This function never fails, but its behaviour with illegal UTF-32 characters
147 *  is undefined.
148 *
149 *  \param buf A pointer to a character buffer where the UTF-8 sequence will
150 *  be written.
151 *  \param ch The UTF-32 character.
152 *  \return The number of bytes written.
153 */
154unsigned int cucul_utf32_to_utf8(char *buf, unsigned long int ch)
155{
156    static const uint8_t mark[7] =
157    {
158        0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
159    };
160
161    char *parser = buf;
162    int bytes;
163
164    if(ch < 0x80)
165    {
166        *parser++ = ch;
167        return 1;
168    }
169
170    bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
171    parser += bytes;
172
173    switch(bytes)
174    {
175        case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
176        case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
177        case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
178    }
179    *--parser = ch | mark[bytes];
180
181    return bytes;
182}
183
184/** \brief Convert a UTF-32 character to CP437.
185 *
186 *  Convert a UTF-32 character read from a string and return its value in
187 *  the CP437 character set, or "?" if the character has no equivalent.
188 *
189 *  This function never fails.
190 *
191 *  \param ch The UTF-32 character.
192 *  \return The corresponding CP437 character, or "?" if not representable.
193 */
194unsigned char cucul_utf32_to_cp437(unsigned long int ch)
195{
196    unsigned int i;
197
198    if(ch < 0x00000020)
199        return '?';
200
201    if(ch < 0x00000080)
202        return ch;
203
204    for(i = 0; i < sizeof(cp437_lookup1) / sizeof(*cp437_lookup1); i++)
205        if(cp437_lookup1[i] == ch)
206            return 0x01 + i;
207
208    for(i = 0; i < sizeof(cp437_lookup2) / sizeof(*cp437_lookup2); i++)
209        if(cp437_lookup2[i] == ch)
210            return 0x7f + i;
211
212    return '?';
213}
214
215/** \brief Convert a CP437 character to UTF-32.
216 *
217 *  Convert a CP437 character read from a string and return its value in
218 *  the UTF-32 character set, or zero if the character is a CP437 control
219 *  character.
220 *
221 *  This function never fails.
222 *
223 *  \param ch The CP437 character.
224 *  \return The corresponding UTF-32 character, or zero if not representable.
225 */
226unsigned long int cucul_cp437_to_utf32(unsigned char ch)
227{
228    if(ch > 0x7f)
229        return cp437_lookup2[ch - 0x7f];
230
231    if(ch >= 0x20)
232        return (uint32_t)ch;
233
234    if(ch > 0)
235        return cp437_lookup1[ch - 0x01];
236
237    return 0x00000000;
238}
239
240/** \brief Convert a UTF-32 character to ASCII.
241 *
242 *  Convert a UTF-32 character into an ASCII character. When no equivalent
243 *  exists, a graphically close equivalent is sought.
244 *
245 *  This function never fails, but its behaviour with illegal UTF-32 characters
246 *  is undefined.
247 *
248 *  \param ch The UTF-32 character.
249 *  \return The corresponding ASCII character, or a graphically close
250 *  equivalent if found, or "?" if not representable.
251 */
252char cucul_utf32_to_ascii(unsigned long int ch)
253{
254    /* Standard ASCII */
255    if(ch < 0x80)
256        return ch;
257
258    /* Fullwidth Forms */
259    if(ch > 0x0000ff00 && ch < 0x0000ff5f)
260        return ' ' + (ch - 0x0000ff00);
261
262    switch (ch)
263    {
264    case 0x000000a0: /*   (nbsp) */
265    case 0x00003000: /*   (ideographic space) */
266        return ' ';
267    case 0x000000a3: /* £ */
268        return 'f';
269    case 0x000000b0: /* ° */
270        return '\'';
271    case 0x000000b1: /* ± */
272        return '#';
273    case 0x000000b7: /* · */
274    case 0x00002219: /* ∙ */
275    case 0x000030fb: /* ・ */
276        return '.';
277    case 0x000003c0: /* π */
278        return '*';
279    case 0x00002018: /* ‘ */
280    case 0x00002019: /* ’ */
281        return '\'';
282    case 0x0000201c: /* “ */
283    case 0x0000201d: /* ” */
284        return '"';
285    case 0x00002190: /* ← */
286        return '<';
287    case 0x00002191: /* ↑ */
288        return '^';
289    case 0x00002192: /* → */
290        return '>';
291    case 0x00002193: /* ↓ */
292        return 'v';
293    case 0x00002260: /* ≠ */
294        return '!';
295    case 0x00002261: /* ≡ */
296        return '=';
297    case 0x00002264: /* ≤ */
298        return '<';
299    case 0x00002265: /* ≥ */
300        return '>';
301    case 0x000023ba: /* ⎺ */
302    case 0x000023bb: /* ⎻ */
303    case 0x000023bc: /* ⎼ */
304    case 0x000023bd: /* ⎽ */
305    case 0x00002500: /* ─ */
306    case 0x00002550: /* ═ */
307        return '-';
308    case 0x00002502: /* │ */
309    case 0x00002551: /* ║ */
310        return '|';
311    case 0x0000250c: /* ┌ */
312    case 0x00002552: /* ╒ */
313    case 0x00002553: /* ╓ */
314    case 0x00002554: /* ╔ */
315    case 0x00002514: /* └ */
316    case 0x00002558: /* ╘ */
317    case 0x00002559: /* ╙ */
318    case 0x0000255a: /* ╚ */
319    case 0x0000251c: /* ├ */
320    case 0x0000255e: /* ╞ */
321    case 0x0000255f: /* ╟ */
322    case 0x00002560: /* ╠ */
323    case 0x0000252c: /* ┬ */
324    case 0x00002564: /* ╤ */
325    case 0x00002565: /* ╥ */
326    case 0x00002566: /* ╦ */
327    case 0x00002534: /* ┴ */
328    case 0x00002567: /* ╧ */
329    case 0x00002568: /* ╨ */
330    case 0x00002569: /* ╩ */
331    case 0x0000253c: /* ┼ */
332    case 0x0000256a: /* ╪ */
333    case 0x0000256b: /* ╫ */
334    case 0x0000256c: /* ╬ */
335        return '+';
336    case 0x00002510: /* ┐ */
337    case 0x00002555: /* ╕ */
338    case 0x00002556: /* ╖ */
339    case 0x00002557: /* ╗ */
340    case 0x00002518: /* ┘ */
341    case 0x0000255b: /* ╛ */
342    case 0x0000255c: /* ╜ */
343    case 0x0000255d: /* ╝ */
344    case 0x00002524: /* ┤ */
345    case 0x00002561: /* ╡ */
346    case 0x00002562: /* ╢ */
347    case 0x00002563: /* ╣ */
348        return '+';
349    case 0x00002591: /* ░ */
350    case 0x00002592: /* ▒ */
351    case 0x00002593: /* ▓ */
352    case 0x00002588: /* █ */
353    case 0x0000258c: /* ▌ */
354    case 0x00002590: /* ▐ */
355    case 0x000025a0: /* ■ */
356    case 0x000025ac: /* ▬ */
357    case 0x000025ae: /* ▮ */
358        return '#';
359    case 0x00002580: /* ▀ */
360        return '"';
361    case 0x00002584: /* ▄ */
362        return ',';
363    case 0x000025c6: /* ◆ */
364    case 0x00002666: /* ♦ */
365        return '+';
366    case 0x00002022: /* • */
367    case 0x000025cb: /* ○ */
368    case 0x000025cf: /* ● */
369    case 0x00002603: /* ☃ */
370    case 0x0000263c: /* ☼ */
371        return 'o';
372    case 0x0000301c: /* 〜 */
373        return '~';
374    }
375
376    return '?';
377}
378
379/** \brief Tell whether a UTF-32 character is fullwidth.
380 *
381 *  Check whether the given UTF-32 character should be printed at twice
382 *  the normal width (fullwidth characters). If the character is unknown
383 *  or if its status cannot be decided, it is treated as a standard-width
384 *  character.
385 *
386 *  This function never fails.
387 *
388 *  \param ch The UTF-32 character.
389 *  \return 1 if the character is fullwidth, 0 otherwise.
390 */
391int cucul_utf32_is_fullwidth(unsigned long int ch)
392{
393    if(ch < 0x2e80) /* Standard stuff */
394        return 0;
395    if(ch < 0xa700) /* Japanese, Korean, CJK, Yi... */
396        return 1;
397    if(ch < 0xac00) /* Modified Tone Letters, Syloti Nagri */
398        return 0;
399    if(ch < 0xd800) /* Hangul Syllables */
400        return 1;
401    if(ch < 0xf900) /* Misc crap */
402        return 0;
403    if(ch < 0xfb00) /* More CJK */
404        return 1;
405    if(ch < 0xfe20) /* Misc crap */
406        return 0;
407    if(ch < 0xfe70) /* More CJK */
408        return 1;
409    if(ch < 0xff00) /* Misc crap */
410        return 0;
411    if(ch < 0xff61) /* Fullwidth forms */
412        return 1;
413    if(ch < 0xffe0) /* Halfwidth forms */
414        return 0;
415    if(ch < 0xffe8) /* More fullwidth forms */
416        return 1;
417    if(ch < 0x20000) /* Misc crap */
418        return 0;
419    if(ch < 0xe0000) /* More CJK */
420        return 1;
421    return 0;
422}
423
Note: See TracBrowser for help on using the repository browser.