Add missing newline before last line of copied text. Add Unicode support to copied text.

This commit is contained in:
Michael Jumper 2013-05-06 12:18:56 -07:00
parent 3d1ca93b3a
commit 547966b63d
4 changed files with 59 additions and 49 deletions

View File

@ -44,5 +44,11 @@
*/
int guac_terminal_fit_to_range(int value, int min, int max);
/**
* Encodes the given codepoint as UTF-8, storing the result within the
* provided buffer, and returning the number of bytes stored.
*/
int guac_terminal_encode_utf8(int codepoint, char* utf8);
#endif

View File

@ -44,3 +44,49 @@ int guac_terminal_fit_to_range(int value, int min, int max) {
}
int guac_terminal_encode_utf8(int codepoint, char* utf8) {
int i;
int mask, bytes;
/* Determine size and initial byte mask */
if (codepoint <= 0x007F) {
mask = 0x00;
bytes = 1;
}
else if (codepoint <= 0x7FF) {
mask = 0xC0;
bytes = 2;
}
else if (codepoint <= 0xFFFF) {
mask = 0xE0;
bytes = 3;
}
else if (codepoint <= 0x1FFFFF) {
mask = 0xF0;
bytes = 4;
}
/* Otherwise, invalid codepoint */
else {
*(utf8++) = '?';
return 1;
}
/* Offset buffer by size */
utf8 += bytes - 1;
/* Add trailing bytes, if any */
for (i=1; i<bytes; i++) {
*(utf8--) = 0x80 | (codepoint & 0x3F);
codepoint >>= 6;
}
/* Set initial byte */
*utf8 = mask | codepoint;
/* Done */
return bytes;
}

View File

@ -80,52 +80,6 @@ int __guac_terminal_hash_codepoint(int codepoint) {
}
int __guac_terminal_encode_utf8(int codepoint, char* utf8) {
int i;
int mask, bytes;
/* Determine size and initial byte mask */
if (codepoint <= 0x007F) {
mask = 0x00;
bytes = 1;
}
else if (codepoint <= 0x7FF) {
mask = 0xC0;
bytes = 2;
}
else if (codepoint <= 0xFFFF) {
mask = 0xE0;
bytes = 3;
}
else if (codepoint <= 0x1FFFFF) {
mask = 0xF0;
bytes = 4;
}
/* Otherwise, invalid codepoint */
else {
*(utf8++) = '?';
return 1;
}
/* Offset buffer by size */
utf8 += bytes - 1;
/* Add trailing bytes, if any */
for (i=1; i<bytes; i++) {
*(utf8--) = 0x80 | (codepoint & 0x3F);
codepoint >>= 6;
}
/* Set initial byte */
*utf8 = mask | codepoint;
/* Done */
return bytes;
}
/**
* Returns the location of the given character in the glyph cache layer,
* sending it first if necessary. The location returned is in characters,
@ -174,7 +128,7 @@ int __guac_terminal_get_glyph(guac_terminal_display* display, int codepoint) {
location = display->next_glyph++;
/* Convert to UTF-8 */
bytes = __guac_terminal_encode_utf8(codepoint, utf8);
bytes = guac_terminal_encode_utf8(codepoint, utf8);
/* Prepare surface */
surface = cairo_image_surface_create(

View File

@ -49,6 +49,7 @@
#include "types.h"
#include "buffer.h"
#include "common.h"
#include "display.h"
#include "terminal.h"
#include "terminal_handlers.h"
@ -414,8 +415,9 @@ int __guac_terminal_buffer_string(guac_terminal_buffer_row* row, int start, int
int length = 0;
int i;
for (i=start; i<=end; i++) {
*(string++) = (char) row->characters[i].value;
length++;
int bytes = guac_terminal_encode_utf8(row->characters[i].value, string);
string += bytes;
length += bytes;
}
return length;
@ -477,6 +479,8 @@ void guac_terminal_select_end(guac_terminal* terminal, char* string) {
buffer_row = guac_terminal_buffer_get_row(terminal->buffer, end_row, 0);
if (buffer_row->length - 1 < end_col)
end_col = buffer_row->length - 1;
*(string++) = '\n';
string += __guac_terminal_buffer_string(buffer_row, 0, end_col, string);
}