Changeset 643


Ignore:
Timestamp:
Mar 17, 2006, 2:32:58 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Factorised some charset handling code into a new cucul/charset.c file.
Location:
libcaca/trunk
Files:
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/driver_conio.c

    r631 r643  
    3434#include "cucul.h"
    3535#include "cucul_internals.h"
    36 
    37 static uint8_t conio_utf32_to_cp437(uint32_t);
    3836
    3937struct driver_private
     
    107105    for(n = kk->qq->height * kk->qq->width; n--; )
    108106    {
    109         *screen++ = conio_utf32_to_cp437(*chars++);
     107        *screen++ = _cucul_utf32_to_cp437(*chars++);
    110108        *screen++ = *attr++;
    111109    }
     
    137135
    138136/*
    139  * XXX: following functions are local
    140  */
    141 
    142 static uint8_t conio_utf32_to_cp437(uint32_t c)
    143 {
    144     static uint32_t const lookup1[] =
    145     {
    146         /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
    147                 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
    148         0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
    149         /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
    150         0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
    151         0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
    152     };
    153 
    154     static uint32_t const lookup2[] =
    155     {
    156         /* 0x7f: ⌂ */
    157         0x2302,
    158         /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
    159         0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
    160         0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
    161         /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
    162         0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
    163         0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
    164         /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
    165         0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
    166         0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
    167         /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
    168         0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
    169         0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
    170         /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
    171         0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
    172         0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
    173         /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
    174         0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
    175         0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
    176         /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
    177         0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
    178         0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
    179         /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
    180         0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
    181         0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
    182     };
    183 
    184     unsigned int i;
    185  
    186     if(c < 0x00000020)
    187         return '?';
    188 
    189     if(c < 0x00000080)
    190         return c;
    191 
    192     for(i = 0; i < sizeof(lookup1) / sizeof(*lookup1); i++)
    193         if(lookup1[i] == c)
    194             return 0x01 + i;
    195 
    196     for(i = 0; i < sizeof(lookup2) / sizeof(*lookup2); i++)
    197         if(lookup2[i] == c)
    198             return 0x7f + i;
    199 
    200     return '?';
    201 }
    202 
    203 /*
    204137 * Driver initialisation
    205138 */
  • libcaca/trunk/caca/driver_vga.c

    r631 r643  
    2626#include "cucul.h"
    2727#include "cucul_internals.h"
    28 
    29 static uint8_t vga_utf32_to_cp437(uint32_t);
    3028
    3129/* Address of the VGA screen */
     
    125123    for(n = kk->qq->height * kk->qq->width; n--; )
    126124    {
    127         *screen++ = vga_utf32_to_cp437(*chars++);
     125        *screen++ = _cucul_utf32_to_cp437(*chars++);
    128126        *screen++ = *attr++;
    129127    }
     
    141139    /* FIXME */
    142140    return CACA_EVENT_NONE;
    143 }
    144 
    145 /*
    146  * XXX: following functions are local
    147  */
    148 
    149 static uint8_t vga_utf32_to_cp437(uint32_t c)
    150 {
    151     static uint32_t const lookup1[] =
    152     {
    153         /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
    154                 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
    155         0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
    156         /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
    157         0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
    158         0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
    159     };
    160 
    161     static uint32_t const lookup2[] =
    162     {
    163         /* 0x7f: ⌂ */
    164         0x2302,
    165         /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
    166         0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
    167         0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
    168         /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
    169         0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
    170         0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
    171         /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
    172         0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
    173         0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
    174         /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
    175         0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
    176         0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
    177         /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
    178         0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
    179         0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
    180         /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
    181         0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
    182         0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
    183         /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
    184         0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
    185         0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
    186         /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
    187         0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
    188         0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
    189     };
    190 
    191     unsigned int i;
    192 
    193     if(c < 0x00000020)
    194         return '?';
    195 
    196     if(c < 0x00000080)
    197         return c;
    198 
    199     for(i = 0; i < sizeof(lookup1) / sizeof(*lookup1); i++)
    200         if(lookup1[i] == c)
    201             return 0x01 + i;
    202 
    203     for(i = 0; i < sizeof(lookup2) / sizeof(*lookup2); i++)
    204         if(lookup2[i] == c)
    205             return 0x7f + i;
    206 
    207     return '?';
    208141}
    209142
  • libcaca/trunk/cucul/Makefile.am

    r620 r643  
    1010        cucul_internals.h \
    1111        char.c \
     12        charset.c \
    1213        math.c \
    1314        line.c \
  • libcaca/trunk/cucul/char.c

    r583 r643  
    3939#include "cucul_internals.h"
    4040
    41 size_t utf8_strlen(const char *s);
    42 const char *utf8_skip(const char *s, size_t x);
    43 uint32_t utf8_to_utf32(const char *s);
    44 
    4541/** \brief Set the default colour pair.
    4642 *
     
    158154        return;
    159155
    160     len = utf8_strlen(s);
     156    len = _cucul_strlen_utf8(s);
    161157
    162158    if(x < 0)
     
    165161            return;
    166162        len -= -x;
    167         s = utf8_skip(s, -x);
     163        s = _cucul_skip_utf8(s, -x);
    168164        x = 0;
    169165    }
     
    177173    while(len)
    178174    {
    179         *chars++ = utf8_to_utf32(s);
     175        *chars++ = _cucul_utf8_to_utf32(s);
    180176        *attr++ = (qq->bgcolor << 4) | qq->fgcolor;
    181177
    182         s = utf8_skip(s, 1);
     178        s = _cucul_skip_utf8(s, 1);
    183179        len--;
    184180    }
     
    262258}
    263259
    264 /*
    265  * XXX: The following functions are local.
    266  */
    267 
    268 static const char trailing[256] =
    269 {
    270     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,
    271     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,
    272     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,
    273     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,
    274     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,
    275     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,
    276     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,
    277     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
    278 };
    279 
    280 static const uint32_t offsets[6] =
    281 {
    282     0x00000000UL, 0x00003080UL, 0x000E2080UL,
    283     0x03C82080UL, 0xFA082080UL, 0x82082080UL
    284 };
    285 
    286 size_t utf8_strlen(const char *s)
    287 {
    288     int len = 0;
    289     const char *parser = s;
    290 
    291     while(*parser)
    292     {
    293         int i;
    294         int bytes = 1 + trailing[(int)(unsigned char)*parser];
    295 
    296         for(i = 1; i < bytes; i++)
    297             if(!parser[i])
    298                 return len;
    299         parser += bytes;
    300         len++;
    301     }
    302 
    303     return len;
    304 }
    305 
    306 const char *utf8_skip(const char *s, size_t x)
    307 {
    308     const char *parser = s;
    309 
    310     while(x)
    311     {
    312         int i;
    313         int bytes = 1 + trailing[(int)(unsigned char)*parser];
    314 
    315         for(i = 1; i < bytes; i++)
    316             if(!parser[i])
    317                 return parser;
    318         parser += bytes;
    319         x--;
    320     }
    321 
    322     return parser;
    323 }
    324 
    325 uint32_t utf8_to_utf32(const char *s)
    326 {
    327     int bytes = trailing[(int)(unsigned char)*s];
    328     uint32_t ret = 0;
    329 
    330     switch(bytes)
    331     {
    332         /* FIXME: do something for invalid sequences (4 and 5) */
    333         case 3: ret += (uint8_t)*s++; ret <<= 6;
    334         case 2: ret += (uint8_t)*s++; ret <<= 6;
    335         case 1: ret += (uint8_t)*s++; ret <<= 6;
    336         case 0: ret += (uint8_t)*s++;
    337     }
    338 
    339     ret -= offsets[bytes];
    340 
    341     return ret;
    342 }
    343 
  • libcaca/trunk/cucul/charset.c

    r639 r643  
    1010 */
    1111
    12 /** \file char.c
     12/** \file charset.c
    1313 *  \version \$Id$
    1414 *  \author Sam Hocevar <sam@zoy.org>
    15  *  \brief Character drawing
     15 *  \brief Character set conversions
    1616 *
    17  *  This file contains character and string drawing functions.
     17 *  This file contains functions for converting characters between
     18 *  various character sets.
    1819 */
    1920
     
    2122
    2223#if !defined(__KERNEL__)
    23 #   include <stdio.h> /* BUFSIZ */
    2424#   include <string.h>
    25 #   include <stdlib.h>
    26 #   include <stdarg.h>
    27 #   if defined(HAVE_UNISTD_H)
    28 #       include <unistd.h>
    29 #   endif
    30 #   if defined(HAVE_SIGNAL_H)
    31 #       include <signal.h>
    32 #   endif
    33 #   if defined(HAVE_SYS_IOCTL_H)
    34 #       include <sys/ioctl.h>
    35 #   endif
    3625#endif
    3726
     
    3928#include "cucul_internals.h"
    4029
    41 size_t utf8_strlen(const char *s);
    42 const char *utf8_skip(const char *s, size_t x);
    43 uint32_t utf8_to_utf32(const char *s);
    44 
    45 /** \brief Set the default colour pair.
    46  *
    47  *  This function sets the default colour pair. String functions such as
    48  *  caca_printf() and graphical primitive functions such as caca_draw_line()
    49  *  will use these colour pairs.
    50  *
    51  *  \param fgcolor The requested foreground colour.
    52  *  \param bgcolor The requested background colour.
    53  */
    54 void cucul_set_color(cucul_t *qq, enum cucul_color fgcolor, enum cucul_color bgcolor)
    55 {
    56     if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15)
    57         return;
    58 
    59     qq->fgcolor = fgcolor;
    60     qq->bgcolor = bgcolor;
    61 }
    62 
    63 /** \brief Get the current foreground colour.
    64  *
    65  *  This function returns the current foreground colour that was set with
    66  *  cucul_set_color().
    67  *
    68  *  \return The current foreground colour.
    69  */
    70 enum cucul_color cucul_get_fg_color(cucul_t *qq)
    71 {
    72     return qq->fgcolor;
    73 }
    74 
    75 /** \brief Get the current background colour.
    76  *
    77  *  This function returns the current background colour that was set with
    78  *  cucul_set_color().
    79  *
    80  *  \return The current background colour.
    81  */
    82 enum cucul_color cucul_get_bg_color(cucul_t *qq)
    83 {
    84     return qq->bgcolor;
    85 }
    86 
    87 /** \brief Print an ASCII character.
    88  *
    89  *  This function prints an ASCII character at the given coordinates, using
    90  *  the default foreground and background values. If the coordinates are
    91  *  outside the screen boundaries, nothing is printed. If the character
    92  *  value is a non-printable character or is outside the ASCII range, it is
    93  *  replaced with a space. To print a sequence of bytes forming an UTF-8
    94  *  character, use cucul_putstr() instead.
    95  *
    96  *  \param x X coordinate.
    97  *  \param y Y coordinate.
    98  *  \param c The character to print.
    99  */
    100 void cucul_putchar(cucul_t *qq, int x, int y, char c)
    101 {
    102     if(x < 0 || x >= (int)qq->width ||
    103        y < 0 || y >= (int)qq->height)
    104         return;
    105 
    106     if((unsigned char)c < 0x20 || (unsigned char)c > 0x7f)
    107         c = 0x20;
    108 
    109     qq->chars[x + y * qq->width] = c;
    110     qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
    111 }
    112 
    113 /** \brief Print a Unicode character.
    114  *
    115  *  FIXME: do we really want this function?
    116  *
    117  *  This function prints a Unicode character (native-endian, 32 bits UCS-4,
    118  *  also known as UTF-32) at the given coordinates, using the default
    119  *  foreground and background values. If the coordinates are outside the
    120  *  screen boundaries, nothing is printed. If the character is an invalid
    121  *  Unicode character, it is replaced with a space.
    122  *
    123  *  \param x X coordinate.
    124  *  \param y Y coordinate.
    125  *  \param c The character to print.
    126  */
    127 void cucul_putchar32(cucul_t *qq, int x, int y, unsigned long int c)
    128 {
    129     if(x < 0 || x >= (int)qq->width ||
    130        y < 0 || y >= (int)qq->height)
    131         return;
    132 
    133     if(c < 0x20 || c > 0x7f)
    134         c = 0x20;
    135 
    136     qq->chars[x + y * qq->width] = c;
    137     qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
    138 }
    139 
    140 /** \brief Print a string.
    141  *
    142  *  This function prints an UTF-8 string at the given coordinates, using the
    143  *  default foreground and background values. The coordinates may be outside
    144  *  the screen boundaries (eg. a negative Y coordinate) and the string will
    145  *  be cropped accordingly if it is too long.
    146  *
    147  *  \param x X coordinate.
    148  *  \param y Y coordinate.
    149  *  \param s The string to print.
    150  */
    151 void cucul_putstr(cucul_t *qq, int x, int y, char const *s)
    152 {
    153     uint32_t *chars;
    154     uint8_t *attr;
    155     unsigned int len;
    156 
    157     if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
    158         return;
    159 
    160     len = utf8_strlen(s);
    161 
    162     if(x < 0)
    163     {
    164         if(len < (unsigned int)-x)
    165             return;
    166         len -= -x;
    167         s = utf8_skip(s, -x);
    168         x = 0;
    169     }
    170 
    171     chars = qq->chars + x + y * qq->width;
    172     attr = qq->attr + x + y * qq->width;
    173 
    174     if(x + len >= qq->width)
    175         len = qq->width - x;
    176 
    177     while(len)
    178     {
    179         *chars++ = utf8_to_utf32(s);
    180         *attr++ = (qq->bgcolor << 4) | qq->fgcolor;
    181 
    182         s = utf8_skip(s, 1);
    183         len--;
    184     }
    185 }
    186 
    187 /** \brief Format a string.
    188  *
    189  *  This function formats a string at the given coordinates, using the
    190  *  default foreground and background values. The coordinates may be outside
    191  *  the screen boundaries (eg. a negative Y coordinate) and the string will
    192  *  be cropped accordingly if it is too long. The syntax of the format
    193  *  string is the same as for the C printf() function.
    194  *
    195  *  \param x X coordinate.
    196  *  \param y Y coordinate.
    197  *  \param format The format string to print.
    198  *  \param ... Arguments to the format string.
    199  */
    200 void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...)
    201 {
    202     char tmp[BUFSIZ];
    203     char *buf = tmp;
    204     va_list args;
    205 
    206     if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
    207         return;
    208 
    209     if(qq->width - x + 1 > BUFSIZ)
    210         buf = malloc(qq->width - x + 1);
    211 
    212     va_start(args, format);
    213 #if defined(HAVE_VSNPRINTF)
    214     vsnprintf(buf, qq->width - x + 1, format, args);
    215 #else
    216     vsprintf(buf, format, args);
    217 #endif
    218     buf[qq->width - x] = '\0';
    219     va_end(args);
    220 
    221     cucul_putstr(qq, x, y, buf);
    222 
    223     if(buf != tmp)
    224         free(buf);
    225 }
    226 
    227 /** \brief Get the screen.
    228  *
    229  *  This function fills a byte array with the character values.
    230  */
    231 void cucul_get_screen(cucul_t *qq, char *buffer)
    232 {
    233     unsigned int x, y;
    234 
    235     for(y = 0; y < qq->height; y++)
    236     {
    237         for(x = 0; x < qq->width; x++)
    238         {
    239             *buffer++ = qq->attr[x + y * qq->width];
    240             *buffer++ = qq->chars[x + y * qq->width] & 0x7f; /* FIXME: ASCII */
    241         }
    242     }
    243 }
    244 
    245 /** \brief Clear the screen.
    246  *
    247  *  This function clears the screen using a black background.
    248  */
    249 void cucul_clear(cucul_t *qq)
    250 {
    251     enum cucul_color oldfg = cucul_get_fg_color(qq);
    252     enum cucul_color oldbg = cucul_get_bg_color(qq);
    253     int y = qq->height;
    254 
    255     cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
    256 
    257     /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
    258     while(y--)
    259         cucul_putstr(qq, 0, y, qq->empty_line);
    260 
    261     cucul_set_color(qq, oldfg, oldbg);
    262 }
    263 
    264 /*
    265  * XXX: The following functions are local.
    266  */
    267 
    268 static const char trailing[256] =
     30static char const trailing[256] =
    26931{
    27032    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,
     
    27840};
    27941
    280 static const uint32_t offsets[6] =
     42static uint32_t const offsets[6] =
    28143{
    28244    0x00000000UL, 0x00003080UL, 0x000E2080UL,
     
    28446};
    28547
    286 size_t utf8_strlen(const char *s)
     48unsigned int _cucul_strlen_utf8(char const *s)
    28749{
    28850    int len = 0;
    289     const char *parser = s;
     51    char const *parser = s;
    29052
    29153    while(*parser)
     
    30466}
    30567
    306 const char *utf8_skip(const char *s, size_t x)
     68char const *_cucul_skip_utf8(char const *s, unsigned int x)
    30769{
    308     const char *parser = s;
     70    char const *parser = s;
    30971
    31072    while(x)
     
    32385}
    32486
    325 uint32_t utf8_to_utf32(const char *s)
     87uint32_t _cucul_utf8_to_utf32(char const *s)
    32688{
    32789    int bytes = trailing[(int)(unsigned char)*s];
     
    342104}
    343105
     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
  • libcaca/trunk/cucul/cucul_internals.h

    r609 r643  
    5555void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height);
    5656
     57/* Charset functions */
     58unsigned int _cucul_strlen_utf8(char const *s);
     59char const *_cucul_skip_utf8(char const *s, unsigned int x);
     60uint32_t _cucul_utf8_to_utf32(char const *s);
     61uint8_t _cucul_utf32_to_cp437(uint32_t c);
     62uint32_t _cucul_cp437_to_utf32(uint8_t c);
     63
     64/* Export functions */
     65void _cucul_get_ansi(cucul_t *qq, struct cucul_buffer *ex);
     66void _cucul_get_html(cucul_t *qq, struct cucul_buffer *ex);
     67void _cucul_get_html3(cucul_t *qq, struct cucul_buffer *ex);
     68void _cucul_get_irc(cucul_t *qq, struct cucul_buffer *ex);
     69void _cucul_get_ps(cucul_t *qq, struct cucul_buffer *ex);
     70void _cucul_get_svg(cucul_t *qq, struct cucul_buffer *ex);
    5771
    5872#endif /* __CUCUL_INTERNALS_H__ */
Note: See TracChangeset for help on using the changeset viewer.