source: libcaca/trunk/caca/charset.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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