Make the charsets code useable in kittens

This commit is contained in:
Kovid Goyal 2019-01-23 11:33:45 +05:30
parent 6e94dae9cd
commit de130787f6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
8 changed files with 44 additions and 3 deletions

26
kitty/charsets.c generated
View File

@ -5,9 +5,13 @@
* Distributed under terms of the GPL3 license.
*/
#include "data-types.h"
// Taken from consolemap.c in the linux vt driver sourcecode
#include <stddef.h>
#include <stdint.h>
#define UTF8_ACCEPT 0
#define UTF8_REJECT 1
static uint32_t charset_translations[5][256] = {
/* 8-bit Latin-1 mapped to Unicode -- trivial mapping */
{
@ -238,6 +242,26 @@ decode_utf8(uint32_t* state, uint32_t* codep, uint8_t byte) {
return *state;
}
size_t
decode_utf8_string(char *src, size_t sz, uint32_t *dest) {
// dest must be a zeroed array of size at least sz
uint32_t codep = 0, state = 0, prev = UTF8_ACCEPT;
size_t i, d;
for (i = 0, d = 0; i < sz; i++) {
switch(decode_utf8(&state, &codep, src[i])) {
case UTF8_ACCEPT:
dest[d++] = codep;
break;
case UTF8_REJECT:
state = UTF8_ACCEPT;
if (prev != UTF8_ACCEPT && i > 0) i--;
break;
}
prev = state;
}
return d;
}
unsigned int
encode_utf8(uint32_t ch, char* dest) {
if (ch < 0x80) {

14
kitty/charsets.h Normal file
View File

@ -0,0 +1,14 @@
/*
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#pragma once
#include <stdint.h>
#include <stddef.h>
uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte);
size_t decode_utf8_string(char *src, size_t sz, uint32_t *dest);
unsigned int encode_utf8(uint32_t ch, char* dest);

View File

@ -9,6 +9,7 @@
#include "threading.h"
#include "screen.h"
#include "fonts.h"
#include "charsets.h"
#include <termios.h>
#include <unistd.h>
#include <float.h>

View File

@ -268,8 +268,6 @@ ColorProfile* alloc_color_profile();
PyObject* create_256_color_table();
PyObject* parse_bytes_dump(PyObject UNUSED *, PyObject *);
PyObject* parse_bytes(PyObject UNUSED *, PyObject *);
uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte);
unsigned int encode_utf8(uint32_t ch, char* dest);
void cursor_reset(Cursor*);
Cursor* cursor_copy(Cursor*);
void cursor_copy_to(Cursor *src, Cursor *dest);

View File

@ -8,6 +8,7 @@
#include "data-types.h"
#include "unicode-data.h"
#include "lineops.h"
#include "charsets.h"
extern PyTypeObject Cursor_Type;

View File

@ -8,6 +8,7 @@
#include "state.h"
#include "screen.h"
#include "lineops.h"
#include "charsets.h"
#include <limits.h>
#include <math.h>
#include "glfw-wrapper.h"

View File

@ -8,6 +8,7 @@
#include "control-codes.h"
#include "screen.h"
#include "graphics.h"
#include "charsets.h"
#include <time.h>
extern PyTypeObject Screen_Type;

View File

@ -23,6 +23,7 @@
#include "modes.h"
#include "wcwidth-std.h"
#include "control-codes.h"
#include "charsets.h"
static const ScreenModes empty_modes = {0, .mDECAWM=true, .mDECTCEM=true, .mDECARM=true};
static Selection EMPTY_SELECTION = {0};