text.c (libcaca-0.99.beta19) | : | text.c (libcaca-0.99.beta20.tar.bz2) | ||
---|---|---|---|---|
/* | /* | |||
* libcaca Colour ASCII-Art library | * libcaca Colour ASCII-Art library | |||
* Copyright (c) 2002-2014 Sam Hocevar <sam@hocevar.net> | * Copyright © 2002—2021 Sam Hocevar <sam@hocevar.net> | |||
* 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> | * 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> | |||
* All Rights Reserved | * All Rights Reserved | |||
* | * | |||
* This library is free software. It comes without any warranty, to | * This library is free software. It comes without any warranty, to | |||
* the extent permitted by applicable law. You can redistribute it | * the extent permitted by applicable law. You can redistribute it | |||
* and/or modify it under the terms of the Do What the Fuck You Want | * and/or modify it under the terms of the Do What the Fuck You Want | |||
* to Public License, Version 2, as published by Sam Hocevar. See | * to Public License, Version 2, as published by Sam Hocevar. See | |||
* http://www.wtfpl.net/ for more details. | * http://www.wtfpl.net/ for more details. | |||
*/ | */ | |||
/* | /* | |||
* This file contains text import and export functions | * This file contains text import and export functions | |||
skipping to change at line 49 | skipping to change at line 49 | |||
}; | }; | |||
static void ansi_parse_grcm(caca_canvas_t *, struct import *, | static void ansi_parse_grcm(caca_canvas_t *, struct import *, | |||
unsigned int, unsigned int const *); | unsigned int, unsigned int const *); | |||
ssize_t _import_text(caca_canvas_t *cv, void const *data, size_t size) | ssize_t _import_text(caca_canvas_t *cv, void const *data, size_t size) | |||
{ | { | |||
char const *text = (char const *)data; | char const *text = (char const *)data; | |||
unsigned int width = 0, height = 0, x = 0, y = 0, i; | unsigned int width = 0, height = 0, x = 0, y = 0, i; | |||
caca_set_canvas_size(cv, width, height); | caca_set_canvas_size(cv, 0, 0); | |||
for(i = 0; i < size; i++) | for(i = 0; i < size; i++) | |||
{ | { | |||
unsigned char ch = *text++; | unsigned char ch = *text++; | |||
if(ch == '\r') | if(ch == '\r') | |||
continue; | continue; | |||
if(ch == '\n') | if(ch == '\n') | |||
{ | { | |||
skipping to change at line 73 | skipping to change at line 73 | |||
} | } | |||
if(x >= width || y >= height) | if(x >= width || y >= height) | |||
{ | { | |||
if(x >= width) | if(x >= width) | |||
width = x + 1; | width = x + 1; | |||
if(y >= height) | if(y >= height) | |||
height = y + 1; | height = y + 1; | |||
caca_set_canvas_size(cv, width, height); | if (caca_set_canvas_size(cv, width, height) < 0) | |||
return -1; | ||||
} | } | |||
caca_put_char(cv, x, y, ch); | caca_put_char(cv, x, y, ch); | |||
x++; | x++; | |||
} | } | |||
if(y > height) | if (y > height) | |||
caca_set_canvas_size(cv, width, height = y); | { | |||
if (caca_set_canvas_size(cv, width, height = y) < 0) | ||||
return -1; | ||||
} | ||||
return (ssize_t)size; | return (ssize_t)size; | |||
} | } | |||
ssize_t _import_ansi(caca_canvas_t *cv, void const *data, size_t size, int utf8) | ssize_t _import_ansi(caca_canvas_t *cv, void const *data, size_t size, int utf8) | |||
{ | { | |||
struct import im; | struct import im; | |||
unsigned char const *buffer = (unsigned char const*)data; | unsigned char const *buffer = (unsigned char const*)data; | |||
unsigned int i, j, skip, growx = 0, growy = 0, dummy = 0; | unsigned int i, j, skip, growx = 0, growy = 0, dummy = 0; | |||
unsigned int width, height; | unsigned int width, height; | |||
skipping to change at line 305 | skipping to change at line 309 | |||
caca_put_attr(cv, j, y, | caca_put_attr(cv, j, y, | |||
caca_get_attr(cv, j + argv[0], y)); | caca_get_attr(cv, j + argv[0], y)); | |||
} | } | |||
#if 0 | #if 0 | |||
savedattr = caca_get_attr(cv, -1, -1); | savedattr = caca_get_attr(cv, -1, -1); | |||
caca_set_attr(cv, im.clearattr); | caca_set_attr(cv, im.clearattr); | |||
for( ; (unsigned int)j < width; j++) | for( ; (unsigned int)j < width; j++) | |||
caca_put_char(cv, j, y, ' '); | caca_put_char(cv, j, y, ' '); | |||
caca_set_attr(cv, savedattr); | caca_set_attr(cv, savedattr); | |||
#endif | #endif | |||
break; | ||||
case 'X': /* ECH (0x58) - Erase Character */ | case 'X': /* ECH (0x58) - Erase Character */ | |||
if(argc && argv[0]) | if(argc && argv[0]) | |||
{ | { | |||
savedattr = caca_get_attr(cv, -1, -1); | savedattr = caca_get_attr(cv, -1, -1); | |||
caca_set_attr(cv, im.clearattr); | caca_set_attr(cv, im.clearattr); | |||
caca_draw_line(cv, x, y, x + argv[0] - 1, y, ' '); | caca_draw_line(cv, x, y, x + argv[0] - 1, y, ' '); | |||
caca_set_attr(cv, savedattr); | caca_set_attr(cv, savedattr); | |||
} | } | |||
break; | ||||
case 'd': /* VPA (0x64) - Line Position Absolute */ | case 'd': /* VPA (0x64) - Line Position Absolute */ | |||
y = (argc && argv[0] > 0) ? argv[0] - 1 : 0; | y = (argc && argv[0] > 0) ? argv[0] - 1 : 0; | |||
break; | break; | |||
case 'f': /* HVP (0x66) - Character And Line Position */ | case 'f': /* HVP (0x66) - Character And Line Position */ | |||
x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; | x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; | |||
y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; | y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; | |||
break; | break; | |||
case 'h': /* SM (0x68) - FIXME */ | case 'h': /* SM (0x68) - FIXME */ | |||
debug("ansi import: set mode %i", argc ? (int)argv[0] : -1); | debug("ansi import: set mode %i", argc ? (int)argv[0] : -1); | |||
break; | break; | |||
skipping to change at line 385 | skipping to change at line 391 | |||
string = malloc(final - (semicolon + 1) + 1); | string = malloc(final - (semicolon + 1) + 1); | |||
memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1)); | memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1)); | |||
string[final - (semicolon + 1)] = '\0'; | string[final - (semicolon + 1)] = '\0'; | |||
debug("ansi import: got OSC command %i string '%s'", command, | debug("ansi import: got OSC command %i string '%s'", command, | |||
string); | string); | |||
free(string); | free(string); | |||
} | } | |||
/* Form feed means a new frame */ | /* Form feed means a new frame */ | |||
else if(buffer[i] == '\f' && buffer[i + 1] == '\n') | else if (i + 1 < size && buffer[i] == '\f' && buffer[i + 1] == '\n') | |||
{ | { | |||
int f = caca_get_frame_count(cv); | int f = caca_get_frame_count(cv); | |||
caca_create_frame(cv, f); | caca_create_frame(cv, f); | |||
caca_set_frame(cv, f); | caca_set_frame(cv, f); | |||
x = y = 0; | x = y = 0; | |||
skip++; | skip++; | |||
} | } | |||
/* Get the character we’re going to paste */ | /* Get the character we’re going to paste */ | |||
else if(utf8) | else if(utf8) | |||
skipping to change at line 432 | skipping to change at line 438 | |||
wch = 1; | wch = 1; | |||
} | } | |||
/* Wrap long lines or grow horizontally */ | /* Wrap long lines or grow horizontally */ | |||
while((unsigned int)x + wch > width) | while((unsigned int)x + wch > width) | |||
{ | { | |||
if(growx) | if(growx) | |||
{ | { | |||
savedattr = caca_get_attr(cv, -1, -1); | savedattr = caca_get_attr(cv, -1, -1); | |||
caca_set_attr(cv, im.clearattr); | caca_set_attr(cv, im.clearattr); | |||
caca_set_canvas_size(cv, width = x + wch, height); | if (caca_set_canvas_size(cv, width = x + wch, height) < 0) | |||
return -1; | ||||
caca_set_attr(cv, savedattr); | caca_set_attr(cv, savedattr); | |||
} | } | |||
else | else | |||
{ | { | |||
x -= width; | x -= width; | |||
y++; | y++; | |||
} | } | |||
} | } | |||
/* Scroll or grow vertically */ | /* Scroll or grow vertically */ | |||
if((unsigned int)y >= height) | if((unsigned int)y >= height) | |||
{ | { | |||
savedattr = caca_get_attr(cv, -1, -1); | savedattr = caca_get_attr(cv, -1, -1); | |||
caca_set_attr(cv, im.clearattr); | caca_set_attr(cv, im.clearattr); | |||
if(growy) | if(growy) | |||
{ | { | |||
caca_set_canvas_size(cv, width, height = y + 1); | if (caca_set_canvas_size(cv, width, height = y + 1) < 0) | |||
return -1; | ||||
} | } | |||
else | else | |||
{ | { | |||
int lines = (y - height) + 1; | int lines = (y - height) + 1; | |||
for(j = 0; j + lines < height; j++) | for(j = 0; j + lines < height; j++) | |||
{ | { | |||
memcpy(cv->attrs + j * cv->width, | memcpy(cv->attrs + j * cv->width, | |||
cv->attrs + (j + lines) * cv->width, cv->width * 4); | cv->attrs + (j + lines) * cv->width, cv->width * 4); | |||
memcpy(cv->chars + j * cv->width, | memcpy(cv->chars + j * cv->width, | |||
skipping to change at line 481 | skipping to change at line 489 | |||
{ | { | |||
caca_put_char(cv, x, y, ch); | caca_put_char(cv, x, y, ch); | |||
x += wch; | x += wch; | |||
} | } | |||
} | } | |||
if(growy && (unsigned int)y > height) | if(growy && (unsigned int)y > height) | |||
{ | { | |||
savedattr = caca_get_attr(cv, -1, -1); | savedattr = caca_get_attr(cv, -1, -1); | |||
caca_set_attr(cv, im.clearattr); | caca_set_attr(cv, im.clearattr); | |||
caca_set_canvas_size(cv, width, height = y); | if (caca_set_canvas_size(cv, width, height = y)) | |||
return -1; | ||||
caca_set_attr(cv, savedattr); | caca_set_attr(cv, savedattr); | |||
} | } | |||
cv->frames[cv->frame].x = x; | cv->frames[cv->frame].x = x; | |||
cv->frames[cv->frame].y = y; | cv->frames[cv->frame].y = y; | |||
// if(utf8) | // if(utf8) | |||
// caca_set_attr(cv, savedattr); | // caca_set_attr(cv, savedattr); | |||
return i; | return i; | |||
End of changes. 10 change blocks. | ||||
12 lines changed or deleted | 21 lines changed or added |