Skip to content

Commit

Permalink
Add basic console text selection and clipboard copy support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-drexler committed Nov 26, 2023
1 parent cf37887 commit b9d8a85
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 38 deletions.
87 changes: 81 additions & 6 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4031,6 +4031,31 @@ static const uint32_t qchar_to_unicode[256] =
----------------------------------------------------------------------------------------------------------------------------------
*/};

/*
==================
UTF8_CodePointLength
Returns the number of bytes needed to encode the codepoint
using UTF-8 (max 4), or 0 for an invalid code point
==================
*/
size_t UTF8_CodePointLength (uint32_t codepoint)
{
if (codepoint < 0x80)
return 1;

if (codepoint < 0x800)
return 2;

if (codepoint < 0x10000)
return 3;

if (codepoint < 0x110000)
return 4;

return 0;
}

/*
==================
UTF8_WriteCodePoint
Expand Down Expand Up @@ -4154,14 +4179,30 @@ uint32_t UTF8_ReadCodePoint (const char **src)
UTF8_FromQuake
Converts a string from Quake encoding to UTF-8
Returns the number of written characters (including the NUL terminator)
if a valid output buffer is provided (dst is non-NULL, maxbytes > 0),
or the total amount of space necessary to encode the entire src string
if dst is NULL and maxbytes is 0.
==================
*/
void UTF8_FromQuake (char *dst, size_t maxbytes, const char *src)
size_t UTF8_FromQuake (char *dst, size_t maxbytes, const char *src)
{
size_t i, j, written;

if (!maxbytes)
return;
{
if (dst)
return 0; // error
for (i = 0, j = 0; src[i]; i++)
{
uint32_t codepoint = qchar_to_unicode[(unsigned char) src[i]];
if (codepoint)
j += UTF8_CodePointLength (codepoint);
}
return j + 1; // include terminator
}

--maxbytes;

for (i = 0, j = 0; j < maxbytes && src[i]; i++)
Expand All @@ -4175,7 +4216,9 @@ void UTF8_FromQuake (char *dst, size_t maxbytes, const char *src)
j += written;
}

dst[j] = '\0';
dst[j++] = '\0';

return j;
}

/*
Expand All @@ -4186,11 +4229,16 @@ Transliterates a string from UTF-8 to Quake encoding
Note: only single-character transliterations are used for now,
mainly to remove diacritics
Returns the number of written characters (including the NUL terminator)
if a valid output buffer is provided (dst is non-NULL, maxbytes > 0),
or the total amount of space necessary to encode the entire src string
if dst is NULL and maxbytes is 0.
==================
*/
void UTF8_ToQuake (char *dst, size_t maxbytes, const char *src)
size_t UTF8_ToQuake (char *dst, size_t maxbytes, const char *src)
{
size_t i;
size_t i, j;

if (!unicode_translit_init)
{
Expand All @@ -4214,7 +4262,32 @@ void UTF8_ToQuake (char *dst, size_t maxbytes, const char *src)
}

if (!maxbytes)
return;
{
if (dst)
return 0; // error

// Determine necessary output buffer size
for (i = 0, j = 0; *src; i++)
{
// ASCII fast path
while (*src && (byte)*src < 0x80)
{
src++;
j++;
}

if (!*src)
break;

// Every codepoint maps to a single Quake character
UTF8_ReadCodePoint (&src);

j++;
}

return j + 1; // include terminator
}

--maxbytes;

for (i = 0; i < maxbytes && *src; i++)
Expand Down Expand Up @@ -4242,4 +4315,6 @@ void UTF8_ToQuake (char *dst, size_t maxbytes, const char *src)
}

dst[i++] = '\0';

return i;
}
4 changes: 2 additions & 2 deletions Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ size_t LOC_Format (const char *format, const char* (*getarg_fn)(int idx, void* u
// Unicode
size_t UTF8_WriteCodePoint (char *dst, size_t maxbytes, uint32_t codepoint);
uint32_t UTF8_ReadCodePoint (const char **src);
void UTF8_FromQuake (char *dst, size_t maxbytes, const char *src);
void UTF8_ToQuake (char *dst, size_t maxbytes, const char *src);
size_t UTF8_FromQuake (char *dst, size_t maxbytes, const char *src);
size_t UTF8_ToQuake (char *dst, size_t maxbytes, const char *src);

#define UNICODE_UNKNOWN 0xFFFD
#define UNICODE_MAX 0x10FFFF
Expand Down
Loading

0 comments on commit b9d8a85

Please sign in to comment.