Changeset 982


Ignore:
Timestamp:
May 25, 2006, 10:01:10 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Have cucul_utf8_to_utf32 write how many bytes it read.
  • Use that in the SLang driver so that it supports UTF-8 input.
Location:
libcaca/trunk
Files:
8 edited

Legend:

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

    r974 r982  
    212212
    213213#if defined(OPTIMISE_SLANG_PALETTE)
     214            /* If foreground == background, just don't use this colour
     215             * pair, and print a space instead of the real character.
     216             * XXX: disabled, because I can't remember what it was
     217             * here for, and in cases where SLang does not render
     218             * bright backgrounds, it's just fucked up. */
     219#if 0
    214220            uint8_t fgcolor = _cucul_argb32_to_ansi4fg(*attr);
    215221            uint8_t bgcolor = _cucul_argb32_to_ansi4bg(*attr);
    216222
    217             /* If foreground == background, just don't use this colour
    218              * pair, and print a space instead of the real character. */
    219             if(fgcolor != bgcolor)
    220             {
    221                 SLsmg_set_color(slang_assoc[_cucul_argb32_to_ansi8(*attr++)]);
    222                 slang_write_utf32(ch);
    223             }
    224             else
     223            if(fgcolor == bgcolor)
    225224            {
    226225                if(fgcolor == CUCUL_COLOR_BLACK)
     
    235234                attr++;
    236235            }
     236            else
     237#endif
     238            {
     239                SLsmg_set_color(slang_assoc[_cucul_argb32_to_ansi8(*attr++)]);
     240                slang_write_utf32(ch);
     241            }
    237242#else
    238243            SLsmg_set_color(_cucul_argb32_to_ansi8(*attr++));
     
    284289        ev->data.key.utf8[1] = '\0';
    285290        return 1;
     291    }
     292
     293    /* If the key was UTF-8, parse the whole sequence */
     294    if(intkey >= 0x80 && intkey < 0x100)
     295    {
     296        int keys[7]; /* Necessary for ungetkey(); */
     297        char utf8[7];
     298        uint32_t utf32;
     299        unsigned int i, bytes = 0;
     300
     301        keys[0] = intkey;
     302        utf8[0] = intkey;
     303
     304        for(i = 1; i < 6; i++)
     305        {
     306            if(!SLang_input_pending(0))
     307                break;
     308            keys[i] = SLang_getkey();
     309            utf8[i] = (unsigned char)keys[i];
     310        }
     311
     312        utf8[i] = '\0';
     313        utf32 = cucul_utf8_to_utf32(utf8, &bytes);
     314
     315        while(i > bytes)
     316            SLang_ungetkey(keys[--i]);
     317
     318        if(bytes)
     319        {
     320            ev->type = CACA_EVENT_KEY_PRESS;
     321            ev->data.key.ch = 0;
     322            ev->data.key.utf32 = utf32;
     323            strcpy(ev->data.key.utf8, utf8);
     324            return 1;
     325        }
    286326    }
    287327
     
    339379        case SL_KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break;
    340380
    341         default: ev->type = CACA_EVENT_NONE; return 0;
     381        default:
     382            /* Unknown key */
     383            ev->type = CACA_EVENT_NONE; return 0;
    342384    }
    343385
  • libcaca/trunk/cucul/box.c

    r962 r982  
    156156    if(y2 > ymax) y2 = ymax;
    157157
    158     ch = cucul_utf8_to_utf32(str);
     158    ch = cucul_utf8_to_utf32(str, NULL);
    159159
    160160    for(y = y1; y <= y2; y++)
  • libcaca/trunk/cucul/canvas.c

    r962 r982  
    117117    while(len)
    118118    {
    119         *chars++ = cucul_utf8_to_utf32(s);
     119        *chars++ = cucul_utf8_to_utf32(s, NULL);
    120120        *attr++ = (cv->bgcolor << 16) | cv->fgcolor;
    121121
  • libcaca/trunk/cucul/charset.c

    r963 r982  
    9696 *
    9797 *  This function converts a UTF-8 character read from a string and returns
    98  *  its value in the UTF-32 character set.
     98 *  its value in the UTF-32 character set. If the second argument is not null,
     99 *  the total number of read bytes is written in it.
     100 *
     101 *  If a null byte was reached before the expected end of the UTF-8 sequence,
     102 *  this function returns zero and the number of read bytes is set to zero.
    99103 *
    100104 *  This function never fails, but its behaviour with illegal UTF-8 sequences
     
    102106 *
    103107 *  \param s A string containing the UTF-8 character.
    104  *  \return The corresponding UTF-32 character.
    105  */
    106 unsigned long int cucul_utf8_to_utf32(char const *s)
    107 {
    108     int bytes = trailing[(int)(unsigned char)*s];
     108 *  \param read A pointer to an unsigned integer to store the number of
     109 *  bytes in the character, or NULL.
     110 *  \return The corresponding UTF-32 character, or zero if the character
     111 *  is incomplete.
     112 */
     113unsigned long int cucul_utf8_to_utf32(char const *s, unsigned int *read)
     114{
     115    unsigned int bytes = trailing[(int)(unsigned char)*s];
     116    unsigned int i = 0;
    109117    uint32_t ret = 0;
    110118
    111     switch(bytes)
    112     {
    113         /* FIXME: do something for invalid sequences (4 and 5) */
    114         case 3: ret += (uint8_t)*s++; ret <<= 6;
    115         case 2: ret += (uint8_t)*s++; ret <<= 6;
    116         case 1: ret += (uint8_t)*s++; ret <<= 6;
    117         case 0: ret += (uint8_t)*s++;
    118     }
    119 
    120     ret -= offsets[bytes];
    121 
    122     return ret;
     119    for(;;)
     120    {
     121        if(!*s)
     122        {
     123            if(read)
     124                *read = 0;
     125            return 0;
     126        }
     127
     128        ret += ((uint32_t)(unsigned char)*s++) << (6 * (bytes - i));
     129
     130        if(bytes == i++)
     131        {
     132            if(read)
     133                *read = i;
     134            return ret - offsets[bytes];
     135        }
     136    }
    123137}
    124138
  • libcaca/trunk/cucul/conic.c

    r962 r982  
    4444{
    4545    int test, dx, dy;
    46     uint32_t ch = cucul_utf8_to_utf32(str);
     46    uint32_t ch = cucul_utf8_to_utf32(str, NULL);
    4747
    4848    /* Optimized Bresenham. Kick ass. */
     
    139139    int y = b;
    140140    int d1 = b*b - (a*a*b) + (a*a/4);
    141     uint32_t ch = cucul_utf8_to_utf32(str);
     141    uint32_t ch = cucul_utf8_to_utf32(str, NULL);
    142142
    143143    ellipsepoints(cv, xo, yo, x, y, ch);
  • libcaca/trunk/cucul/cucul.h

    r962 r982  
    124124 *
    125125 *  @{ */
    126 extern unsigned long int cucul_utf8_to_utf32(char const *);
     126extern unsigned long int cucul_utf8_to_utf32(char const *, unsigned int *);
    127127extern unsigned int cucul_utf32_to_utf8(char *, unsigned long int);
    128128extern unsigned char cucul_utf32_to_cp437(unsigned long int);
  • libcaca/trunk/cucul/line.c

    r962 r982  
    6262    s.x2 = x2;
    6363    s.y2 = y2;
    64     s.ch = cucul_utf8_to_utf32(str);
     64    s.ch = cucul_utf8_to_utf32(str, NULL);
    6565    s.draw = draw_solid_line;
    6666    clip_line(cv, &s);
     
    9090    int i;
    9191    struct line s;
    92     s.ch = cucul_utf8_to_utf32(str);
     92    s.ch = cucul_utf8_to_utf32(str, NULL);
    9393    s.draw = draw_solid_line;
    9494
  • libcaca/trunk/cucul/triangle.c

    r962 r982  
    110110    ymax = cv->height - 1;
    111111
    112     ch = cucul_utf8_to_utf32(str);
     112    ch = cucul_utf8_to_utf32(str, NULL);
    113113
    114114    /* Rasterize our triangle */
Note: See TracChangeset for help on using the changeset viewer.