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

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