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

Last change on this file since 672 was 672, checked in by Sam Hocevar, 15 years ago
  • Slightly changed header comments.
  • Property svn:keywords set to Id
File size: 5.4 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 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file charset.c
13 *  \version \$Id: charset.c 672 2006-03-22 21:15:57Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Character set conversions
16 *
17 *  This file contains functions for converting characters between
18 *  various character sets.
19 */
20
21#include "config.h"
22
23#if !defined(__KERNEL__)
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30static char const trailing[256] =
31{
32    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,
33    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,
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    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,
39    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
40};
41
42static uint32_t const offsets[6] =
43{
44    0x00000000UL, 0x00003080UL, 0x000E2080UL,
45    0x03C82080UL, 0xFA082080UL, 0x82082080UL
46};
47
48unsigned int _cucul_strlen_utf8(char const *s)
49{
50    int len = 0;
51    char const *parser = s;
52
53    while(*parser)
54    {
55        int i;
56        int bytes = 1 + trailing[(int)(unsigned char)*parser];
57
58        for(i = 1; i < bytes; i++)
59            if(!parser[i])
60                return len;
61        parser += bytes;
62        len++;
63    }
64
65    return len;
66}
67
68char const *_cucul_skip_utf8(char const *s, unsigned int x)
69{
70    char const *parser = s;
71
72    while(x)
73    {
74        int i;
75        int bytes = 1 + trailing[(int)(unsigned char)*parser];
76
77        for(i = 1; i < bytes; i++)
78            if(!parser[i])
79                return parser;
80        parser += bytes;
81        x--;
82    }
83
84    return parser;
85}
86
87uint32_t _cucul_utf8_to_utf32(char const *s)
88{
89    int bytes = trailing[(int)(unsigned char)*s];
90    uint32_t ret = 0;
91
92    switch(bytes)
93    {
94        /* FIXME: do something for invalid sequences (4 and 5) */
95        case 3: ret += (uint8_t)*s++; ret <<= 6;
96        case 2: ret += (uint8_t)*s++; ret <<= 6;
97        case 1: ret += (uint8_t)*s++; ret <<= 6;
98        case 0: ret += (uint8_t)*s++;
99    }
100
101    ret -= offsets[bytes];
102
103    return ret;
104}
105
106/*
107 * CP437 handling
108 */
109
110static uint32_t const cp437_lookup1[] =
111{
112    /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
113            0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
114    0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
115    /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
116    0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
117    0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
118};
119
120static uint32_t const cp437_lookup2[] =
121{
122    /* 0x7f: ⌂ */
123    0x2302,
124    /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
125    0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
126    0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
127    /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
128    0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
129    0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
130    /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
131    0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
132    0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
133    /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
134    0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
135    0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
136    /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
137    0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
138    0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
139    /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
140    0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
141    0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
142    /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
143    0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
144    0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
145    /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
146    0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
147    0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
148};
149
150uint8_t _cucul_utf32_to_cp437(uint32_t c)
151{
152    unsigned int i;
153
154    if(c < 0x00000020)
155        return '?';
156
157    if(c < 0x00000080)
158        return c;
159
160    for(i = 0; i < sizeof(cp437_lookup1) / sizeof(*cp437_lookup1); i++)
161        if(cp437_lookup1[i] == c)
162            return 0x01 + i;
163
164    for(i = 0; i < sizeof(cp437_lookup2) / sizeof(*cp437_lookup2); i++)
165        if(cp437_lookup2[i] == c)
166            return 0x7f + i;
167
168    return '?';
169}
170
171uint32_t _cucul_cp437_to_utf32(uint8_t c)
172{
173    if(c > 0x7f)
174        return cp437_lookup2[c - 0x7f];
175
176    if(c >= 0x20)
177        return (uint32_t)c;
178
179    if(c > 0)
180        return cp437_lookup1[c - 0x01];
181
182    return 0x00000000;
183}
184
Note: See TracBrowser for help on using the repository browser.