int main(int argc, char *argv[]) { caca_canvas_t *cv, *pig; void *buffer; size_t len; int i, j; pig = caca_create_canvas(0, 0); caca_import_canvas_from_memory(pig, STRING, strlen(STRING), "text"); cv = caca_create_canvas(caca_get_canvas_width(pig) * 2, caca_get_canvas_height(pig) * 2); if(cv == NULL || pig == NULL) { printf("Can't created canvas\n"); return -1; } caca_blit(cv, 0, 0, pig, NULL); caca_flip(pig); caca_blit(cv, caca_get_canvas_width(pig), 0, pig, NULL); caca_flip(pig); caca_flop(pig); caca_blit(cv, 0, caca_get_canvas_height(pig), pig, NULL); caca_flop(pig); caca_rotate_180(pig); caca_blit(cv, caca_get_canvas_width(pig), caca_get_canvas_height(pig), pig, NULL); for(j = 0; j < caca_get_canvas_height(cv); j++) { for(i = 0; i < caca_get_canvas_width(cv); i += 2) { unsigned long int a; caca_set_color_ansi(cv, CACA_LIGHTBLUE + (i + j) % 6, CACA_DEFAULT); a = caca_get_attr(cv, -1, -1); caca_put_attr(cv, i, j, a); caca_put_attr(cv, i + 1, j, a); } } buffer = caca_export_canvas_to_memory(cv, "utf8", &len); fwrite(buffer, len, 1, stdout); free(buffer); caca_rotate_left(cv); buffer = caca_export_canvas_to_memory(cv, "utf8", &len); fwrite(buffer, len, 1, stdout); free(buffer); caca_free_canvas(pig); caca_free_canvas(cv); return 0; }
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buf, char const *format) { caca_canvas_t *cv = caca_create_canvas(0, 0); int ret = caca_import_canvas_from_memory(cv, (unsigned char const *)buf->data, buf->size, format); if(ret < 0) { caca_free_canvas(cv); return NULL; } return cv; }
int main(int argc, char *argv[]) { caca_canvas_t *cv, *sprite; caca_display_t *dp; cv = caca_create_canvas(80, 24); if(cv == NULL) { printf("Failed to create canvas\n"); return 1; } dp = caca_create_display(cv); if(dp == NULL) { printf("Failed to create display\n"); return 1; } sprite = caca_create_canvas(0, 0); caca_set_color_ansi(sprite, CACA_LIGHTRED, CACA_BLACK); caca_import_canvas_from_memory(sprite, pig, strlen(pig), "text"); caca_set_canvas_handle(sprite, caca_get_canvas_width(sprite) / 2, caca_get_canvas_height(sprite) / 2); caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); caca_put_str(cv, 0, 0, "Centered sprite"); caca_blit(cv, caca_get_canvas_width(cv) / 2, caca_get_canvas_height(cv) / 2, sprite, NULL); caca_refresh_display(dp); caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); caca_free_display(dp); caca_free_canvas(sprite); caca_free_canvas(cv); return 0; }
int main(int argc, char *argv[]) { caca_canvas_t *cv, *image, *tmp, *sprite; caca_display_t *dp; cv = caca_create_canvas(0, 0); if(cv == NULL) { printf("Can't created canvas\n"); return -1; } dp = caca_create_display(cv); if(dp == NULL) { printf("Can't create display\n"); return -1; } image = caca_create_canvas(70, 6); tmp = caca_create_canvas(70, 6); sprite = caca_create_canvas(0, 0); caca_set_color_ansi(sprite, CACA_LIGHTMAGENTA, CACA_BLACK); caca_import_canvas_from_memory(sprite, pig, strlen(pig), "text"); caca_blit(image, 55, 0, sprite, NULL); caca_set_color_ansi(sprite, CACA_LIGHTGREEN, CACA_BLACK); caca_import_canvas_from_memory(sprite, duck, strlen(duck), "text"); caca_blit(image, 30, 1, sprite, NULL); caca_set_color_ansi(image, CACA_LIGHTCYAN, CACA_BLACK); caca_put_str(image, 1, 1, "hahaha mais vieux porc immonde !! [⽼ ⾗]"); caca_set_color_ansi(image, CACA_LIGHTRED, CACA_BLACK); caca_put_char(image, 38, 1, '|'); caca_set_color_ansi(image, CACA_YELLOW, CACA_BLACK); caca_put_str(image, 4, 2, "\\o\\ \\o| _o/ \\o_ |o/ /o/"); caca_set_color_ansi(image, CACA_WHITE, CACA_LIGHTRED); caca_put_str(image, 7, 3, "▙▘▌▙▘▞▖▞▖▌ ▞▖▌ ▌▌"); caca_put_str(image, 7, 4, "▛▖▌▛▖▚▘▚▘▚▖▚▘▚▖▖▖"); caca_set_color_ansi(image, CACA_BLACK, CACA_LIGHTRED); caca_put_str(image, 4, 3, "▓▒░"); caca_put_str(image, 4, 4, "▓▒░"); caca_put_str(image, 24, 3, "░▒▓"); caca_put_str(image, 24, 4, "░▒▓"); /* Blit the transformed canvas onto the main canvas */ caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE); caca_put_str(cv, 0, 0, "normal"); caca_blit(cv, 10, 0, image, NULL); caca_put_str(cv, 0, 6, "flip"); caca_blit(tmp, 0, 0, image, NULL); caca_flip(tmp); caca_blit(cv, 10, 6, tmp, NULL); caca_put_str(cv, 0, 12, "flop"); caca_blit(tmp, 0, 0, image, NULL); caca_flop(tmp); caca_blit(cv, 10, 12, tmp, NULL); caca_put_str(cv, 0, 18, "rotate"); caca_blit(tmp, 0, 0, image, NULL); caca_rotate_180(tmp); caca_blit(cv, 10, 18, tmp, NULL); caca_refresh_display(dp); caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1); caca_free_display(dp); caca_free_canvas(tmp); caca_free_canvas(sprite); caca_free_canvas(image); caca_free_canvas(cv); return 0; }
static caca_charfont_t * open_charfont(char const *path) { char buf[2048]; char hardblank[10]; caca_charfont_t *ff; char *data = NULL; caca_file_t *f; #if !defined __KERNEL__ && (defined HAVE_SNPRINTF || defined HAVE_SPRINTF_S) int const pathlen = 2048; char *altpath = NULL; #endif int i, j, size, comment_lines; ff = malloc(sizeof(caca_charfont_t)); if(!ff) { seterrno(ENOMEM); return NULL; } /* Open font: if not found, try .tlf, then .flf */ f = caca_file_open(path, "r"); #if !defined __KERNEL__ && (defined HAVE_SNPRINTF || defined HAVE_SPRINTF_S) if(!f) altpath = malloc(pathlen); if(!f) { #if defined HAVE_SPRINTF_S sprintf_s(altpath, pathlen - 1, "%s.tlf", path); #else snprintf(altpath, pathlen - 1, "%s.tlf", path); #endif altpath[pathlen - 1] = '\0'; f = caca_file_open(altpath, "r"); } if(!f) { #if defined HAVE_SPRINTF_S sprintf_s(altpath, pathlen - 1, "%s.flf", path); #else snprintf(altpath, pathlen - 1, "%s.flf", path); #endif altpath[pathlen - 1] = '\0'; f = caca_file_open(altpath, "r"); } if (altpath) free(altpath); #endif if(!f) { free(ff); seterrno(ENOENT); return NULL; } /* Read header */ ff->print_direction = 0; ff->full_layout = 0; ff->codetag_count = 0; caca_file_gets(f, buf, 2048); if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank, &ff->height, &ff->baseline, &ff->max_length, &ff->old_layout, &comment_lines, &ff->print_direction, &ff->full_layout, &ff->codetag_count) < 6) { debug("figfont error: `%s' has invalid header: %s", path, buf); caca_file_close(f); free(ff); seterrno(EINVAL); return NULL; } if(ff->old_layout < -1 || ff->old_layout > 63 || ff->full_layout > 32767 || ((ff->full_layout & 0x80) && (ff->full_layout & 0x3f) == 0 && ff->old_layout)) { debug("figfont error: `%s' has invalid layout %i/%u", path, ff->old_layout, ff->full_layout); caca_file_close(f); free(ff); seterrno(EINVAL); return NULL; } ff->hardblank = caca_utf8_to_utf32(hardblank, NULL); /* Skip comment lines */ for(i = 0; i < comment_lines; i++) caca_file_gets(f, buf, 2048); /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223) * then read additional characters. */ ff->glyphs = 0; ff->lookup = NULL; for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++) { if((ff->glyphs % 2048) == 0) ff->lookup = realloc(ff->lookup, (ff->glyphs + 2048) * 2 * sizeof(int)); if(ff->glyphs < STD_GLYPHS) { ff->lookup[ff->glyphs * 2] = 32 + ff->glyphs; } else if(ff->glyphs < EXT_GLYPHS) { static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 }; ff->lookup[ff->glyphs * 2] = tab[ff->glyphs - STD_GLYPHS]; } else { unsigned int tmp; if(caca_file_gets(f, buf, 2048) == NULL) break; /* Ignore blank lines, as in jacky.flf */ if(buf[0] == '\n' || buf[0] == '\r') continue; /* Ignore negative indices for now, as in ivrit.flf */ if(buf[0] == '-') { for(j = 0; j < ff->height; j++) caca_file_gets(f, buf, 2048); continue; } if(!buf[0] || buf[0] < '0' || buf[0] > '9') { debug("figfont error: glyph #%u in `%s'", ff->glyphs, path); free(data); free(ff->lookup); free(ff); seterrno(EINVAL); return NULL; } sscanf(buf, buf[1] == 'x' ? "%x" : "%u", &tmp); ff->lookup[ff->glyphs * 2] = tmp; } ff->lookup[ff->glyphs * 2 + 1] = 0; for(j = 0; j < ff->height; j++) { if(i + 2048 >= size) data = realloc(data, size += 2048); caca_file_gets(f, data + i, 2048); i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; } } caca_file_close(f); if(ff->glyphs < EXT_GLYPHS) { debug("figfont error: only %u glyphs in `%s', expected at least %u", ff->glyphs, path, EXT_GLYPHS); free(data); free(ff->lookup); free(ff); seterrno(EINVAL); return NULL; } /* Remaining initialisation */ ff->charcv = NULL; ff->left = NULL; ff->right = NULL; /* Import buffer into canvas */ ff->fontcv = caca_create_canvas(0, 0); caca_import_canvas_from_memory(ff->fontcv, data, i, "utf8"); free(data); /* Remove EOL characters. For now we ignore hardblanks, don’t do any * smushing, nor any kind of error checking. */ for(j = 0; j < ff->height * ff->glyphs; j++) { uint32_t ch, oldch = 0; for(i = ff->max_length; i--;) { ch = caca_get_char(ff->fontcv, i, j); /* Replace hardblanks with U+00A0 NO-BREAK SPACE */ if(ch == ff->hardblank) caca_put_char(ff->fontcv, i, j, ch = 0xa0); if(oldch && ch != oldch) { if(!ff->lookup[j / ff->height * 2 + 1]) ff->lookup[j / ff->height * 2 + 1] = i + 1; } else if(oldch && ch == oldch) caca_put_char(ff->fontcv, i, j, ' '); else if(ch != ' ') { oldch = ch; caca_put_char(ff->fontcv, i, j, ' '); } } } return ff; }