static int feed_tiny(context_t *cx, uint32_t ch, uint32_t attr) { switch(ch) { case (uint32_t)'\r': return 0; case (uint32_t)'\n': cx->x = 0; cx->y++; return 0; case (uint32_t)'\t': cx->x = (cx->x & ~7) + 8; return 0; } /* Check whether we reached the end of the screen */ if(cx->x && cx->x + 1 > cx->term_width) { cx->x = 0; cx->y++; } /* Check whether the current canvas is large enough */ if(cx->x + 1 > cx->w) { cx->w = cx->x + 1 < cx->term_width ? cx->x + 1 : cx->term_width; if(cx->w > cx->ew) cx->ew = cx->ew + cx->ew / 2; } if(cx->y + 1 > cx->h) { cx->h = cx->y + 1; if(cx->h > cx->eh) cx->eh = cx->eh + cx->eh / 2; } caca_set_attr(cx->cv, attr); caca_set_canvas_size(cx->cv, cx->ew, cx->eh); caca_put_char(cx->cv, cx->x, cx->y, ch); cx->x++; return 0; }
/** \brief paste a character using the current figfont */ int caca_put_figchar(caca_canvas_t *cv, uint32_t ch) { caca_charfont_t *ff = cv->ff; int c, w, h, x, y, overlap, extra, xleft, xright; if (!ff) return -1; switch(ch) { case (uint32_t)'\r': return 0; case (uint32_t)'\n': ff->x = 0; ff->y += ff->height; return 0; /* FIXME: handle '\t' */ } /* Look whether our glyph is available */ for(c = 0; c < ff->glyphs; c++) if(ff->lookup[c * 2] == ch) break; if(c == ff->glyphs) return 0; w = ff->lookup[c * 2 + 1]; h = ff->height; caca_set_canvas_handle(ff->fontcv, 0, c * ff->height); caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL); /* Check whether we reached the end of the screen */ if(ff->x && ff->x + w > ff->term_width) { ff->x = 0; ff->y += h; } /* Compute how much the next character will overlap */ switch(ff->hmode) { case H_SMUSH: case H_KERN: case H_OVERLAP: extra = (ff->hmode == H_OVERLAP); overlap = w; for(y = 0; y < h; y++) { /* Compute how much spaces we can eat from the new glyph */ for(xright = 0; xright < overlap; xright++) if(caca_get_char(ff->charcv, xright, y) != ' ') break; /* Compute how much spaces we can eat from the previous glyph */ for(xleft = 0; xright + xleft < overlap && xleft < ff->x; xleft++) if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ') break; /* Handle overlapping */ if(ff->hmode == H_OVERLAP && xleft < ff->x) xleft++; /* Handle smushing */ if(ff->hmode == H_SMUSH) { if(xleft < ff->x && hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y), caca_get_char(ff->charcv, xright, y), ff->hsmushrule)) xleft++; } if(xleft + xright < overlap) overlap = xleft + xright; } break; case H_NONE: overlap = 0; break; default: return -1; } /* Check whether the current canvas is large enough */ if(ff->x + w - overlap > ff->w) ff->w = ff->x + w - overlap < ff->term_width ? ff->x + w - overlap : ff->term_width; if(ff->y + h > ff->h) ff->h = ff->y + h; #if 0 /* deactivated for libcaca insertion */ if(attr) caca_set_attr(cv, attr); #endif caca_set_canvas_size(cv, ff->w, ff->h); /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */ for(y = 0; y < h; y++) for(x = 0; x < w; x++) { uint32_t ch1, ch2; uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height); ch2 = caca_get_char(ff->charcv, x, y); if(ch2 == ' ') continue; ch1 = caca_get_char(cv, ff->x + x - overlap, ff->y + y); if(ch1 == ' ' || ff->hmode != H_SMUSH) caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2); else caca_put_char(cv, ff->x + x - overlap, ff->y + y, hsmush(ch1, ch2, ff->hsmushrule)); caca_put_attr(cv, ff->x + x, ff->y + y, tmpat); } /* Advance cursor */ ff->x += w - overlap; return 0; }
int main(int argc, char *argv[]) { caca_canvas_t *cv; caca_dither_t *dither; void *buffer; char *file, *format; char const * const * exports, * const * p; size_t len; int x, y; exports = caca_get_export_list(); if(argc < 2 || argc > 3) { fprintf(stderr, "%s: wrong argument count\n", argv[0]); fprintf(stderr, "usage: %s [file] <format>\n", argv[0]); fprintf(stderr, "where <format> is one of:\n"); for(p = exports; *p; p += 2) fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1)); exit(-1); } if(argc == 2) { file = NULL; format = argv[1]; } else { file = argv[1]; format = argv[2]; } for(p = exports; *p; p += 2) if(!strcasecmp(format, *p)) break; if(!*p) { fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format); fprintf(stderr, "please use one of:\n"); for(p = exports; *p; p += 2) fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1)); exit(-1); } if(file) { cv = caca_create_canvas(0, 0); if(caca_import_canvas_from_file(cv, file, "") < 0) { fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file); exit(-1); } } else { cv = caca_create_canvas(WIDTH, HEIGHT); for(y = 0; y < 256; y++) { for(x = 0; x < 256; x++) { uint32_t r = x; uint32_t g = (255 - y + x) / 2; uint32_t b = y * (255 - x) / 256; pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0); } } dither = caca_create_dither(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); if(!strcmp(format, "ansi") || !strcmp(format, "utf8")) caca_set_dither_charset(dither, "shades"); caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height(cv), dither, pixels); caca_free_dither(dither); caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1); caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2, WIDTH / 4, HEIGHT / 4, ' '); caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK); caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6, " lightgray on black "); caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5, " default on transparent "); caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4, " black on white "); caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]"); caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]"); caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ"); caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>"); caca_set_attr(cv, CACA_BOLD); caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold"); caca_set_attr(cv, CACA_BLINK); caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink"); caca_set_attr(cv, CACA_ITALICS); caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics"); caca_set_attr(cv, CACA_UNDERLINE); caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline"); caca_set_attr(cv, 0); caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE); caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); for(x = 0; x < 16; x++) { caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4)); caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#'); } } buffer = caca_export_canvas_to_memory(cv, format, &len); fwrite(buffer, len, 1, stdout); free(buffer); caca_free_canvas(cv); return 0; }
JNIEXPORT void JNICALL Java_org_zoy_caca_Canvas_setCanvasAttribute(JNIEnv *env, jclass cls, jlong ptr, jint attr) { caca_set_attr((caca_canvas_t *)ptr, attr); }