Changeset 2821 for libcaca/trunk/caca/figfont.c
- Timestamp:
- Sep 27, 2008, 3:12:46 PM (12 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca/figfont.c
r2819 r2821 1 1 /* 2 * libc ucul Canvas for ultrafast compositing of Unicode letters2 * libcaca Colour ASCII-Art library 3 3 * Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org> 4 4 * All Rights Reserved … … 29 29 #endif 30 30 31 #include "c ucul.h"32 #include "c ucul_internals.h"33 34 struct c ucul_figfont31 #include "caca.h" 32 #include "caca_internals.h" 33 34 struct caca_figfont 35 35 { 36 36 int term_width; … … 44 44 int print_direction, full_layout, codetag_count; 45 45 int glyphs; 46 c ucul_canvas_t *fontcv, *charcv;46 caca_canvas_t *fontcv, *charcv; 47 47 int *left, *right; /* Unused yet */ 48 48 uint32_t *lookup; … … 50 50 51 51 static uint32_t hsmush(uint32_t ch1, uint32_t ch2, int rule); 52 static c ucul_figfont_t * open_figfont(char const *);53 static int free_figfont(c ucul_figfont_t *);54 55 int c ucul_canvas_set_figfont(cucul_canvas_t *cv, char const *path)56 { 57 c ucul_figfont_t *ff = NULL;52 static caca_figfont_t * open_figfont(char const *); 53 static int free_figfont(caca_figfont_t *); 54 55 int caca_canvas_set_figfont(caca_canvas_t *cv, char const *path) 56 { 57 caca_figfont_t *ff = NULL; 58 58 59 59 if(path) … … 66 66 if(cv->ff) 67 67 { 68 c ucul_free_canvas(cv->ff->charcv);68 caca_free_canvas(cv->ff->charcv); 69 69 free(cv->ff->left); 70 70 free(cv->ff->right); … … 85 85 ff->w = ff->h = 0; 86 86 ff->lines = 0; 87 c ucul_set_canvas_size(cv, 0, 0); /* XXX */87 caca_set_canvas_size(cv, 0, 0); /* XXX */ 88 88 89 89 /* from TOIlet’s figlet.c */ … … 118 118 } 119 119 120 ff->charcv = c ucul_create_canvas(ff->max_length - 2, ff->height);120 ff->charcv = caca_create_canvas(ff->max_length - 2, ff->height); 121 121 122 122 ff->left = malloc(ff->height * sizeof(int)); … … 128 128 } 129 129 130 int c ucul_put_figchar(cucul_canvas_t *cv, uint32_t ch)131 { 132 c ucul_figfont_t *ff = cv->ff;130 int caca_put_figchar(caca_canvas_t *cv, uint32_t ch) 131 { 132 caca_figfont_t *ff = cv->ff; 133 133 int c, w, h, x, y, overlap, extra, xleft, xright; 134 134 … … 155 155 h = ff->height; 156 156 157 c ucul_set_canvas_handle(ff->fontcv, 0, c * ff->height);158 c ucul_blit(ff->charcv, 0, 0, ff->fontcv, NULL);157 caca_set_canvas_handle(ff->fontcv, 0, c * ff->height); 158 caca_blit(ff->charcv, 0, 0, ff->fontcv, NULL); 159 159 160 160 /* Check whether we reached the end of the screen */ … … 177 177 /* Compute how much spaces we can eat from the new glyph */ 178 178 for(xright = 0; xright < overlap; xright++) 179 if(c ucul_get_char(ff->charcv, xright, y) != ' ')179 if(caca_get_char(ff->charcv, xright, y) != ' ') 180 180 break; 181 181 182 182 /* Compute how much spaces we can eat from the previous glyph */ 183 183 for(xleft = 0; xright + xleft < overlap && xleft < ff->x; xleft++) 184 if(c ucul_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ')184 if(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y) != ' ') 185 185 break; 186 186 … … 193 193 { 194 194 if(xleft < ff->x && 195 hsmush(c ucul_get_char(cv, ff->x - 1 - xleft, ff->y + y),196 c ucul_get_char(ff->charcv, xright, y),195 hsmush(caca_get_char(cv, ff->x - 1 - xleft, ff->y + y), 196 caca_get_char(ff->charcv, xright, y), 197 197 ff->hsmushrule)) 198 198 xleft++; … … 220 220 #if 0 /* deactivated for libcaca insertion */ 221 221 if(attr) 222 c ucul_set_attr(cv, attr);222 caca_set_attr(cv, attr); 223 223 #endif 224 c ucul_set_canvas_size(cv, ff->w, ff->h);225 226 /* Render our char (FIXME: create a rect-aware c ucul_blit_canvas?) */224 caca_set_canvas_size(cv, ff->w, ff->h); 225 226 /* Render our char (FIXME: create a rect-aware caca_blit_canvas?) */ 227 227 for(y = 0; y < h; y++) 228 228 for(x = 0; x < w; x++) 229 229 { 230 230 uint32_t ch1, ch2; 231 //uint32_t tmpat = c ucul_get_attr(ff->fontcv, x, y + c * ff->height);232 ch2 = c ucul_get_char(ff->charcv, x, y);231 //uint32_t tmpat = caca_get_attr(ff->fontcv, x, y + c * ff->height); 232 ch2 = caca_get_char(ff->charcv, x, y); 233 233 if(ch2 == ' ') 234 234 continue; 235 ch1 = c ucul_get_char(cv, ff->x + x - overlap, ff->y + y);236 /* FIXME: this could be changed to c ucul_put_attr() when the237 * function is fixed in libc ucul*/238 //c ucul_set_attr(cv, tmpat);235 ch1 = caca_get_char(cv, ff->x + x - overlap, ff->y + y); 236 /* FIXME: this could be changed to caca_put_attr() when the 237 * function is fixed in libcaca */ 238 //caca_set_attr(cv, tmpat); 239 239 if(ch1 == ' ' || ff->hmode != H_SMUSH) 240 c ucul_put_char(cv, ff->x + x - overlap, ff->y + y, ch2);240 caca_put_char(cv, ff->x + x - overlap, ff->y + y, ch2); 241 241 else 242 c ucul_put_char(cv, ff->x + x - overlap, ff->y + y,242 caca_put_char(cv, ff->x + x - overlap, ff->y + y, 243 243 hsmush(ch1, ch2, ff->hsmushrule)); 244 //c ucul_put_attr(cv, ff->x + x, ff->y + y, tmpat);244 //caca_put_attr(cv, ff->x + x, ff->y + y, tmpat); 245 245 } 246 246 … … 251 251 } 252 252 253 int c ucul_flush_figlet(cucul_canvas_t *cv)254 { 255 c ucul_figfont_t *ff = cv->ff;253 int caca_flush_figlet(caca_canvas_t *cv) 254 { 255 caca_figfont_t *ff = cv->ff; 256 256 int x, y; 257 257 258 258 //ff->torender = cv; 259 //c ucul_set_canvas_size(ff->torender, ff->w, ff->h);260 c ucul_set_canvas_size(cv, ff->w, ff->h);259 //caca_set_canvas_size(ff->torender, ff->w, ff->h); 260 caca_set_canvas_size(cv, ff->w, ff->h); 261 261 262 262 /* FIXME: do this somewhere else, or record hardblank positions */ 263 263 for(y = 0; y < ff->h; y++) 264 264 for(x = 0; x < ff->w; x++) 265 if(c ucul_get_char(cv, x, y) == 0xa0)266 { 267 uint32_t attr = c ucul_get_attr(cv, x, y);268 c ucul_put_char(cv, x, y, ' ');269 c ucul_put_attr(cv, x, y, attr);265 if(caca_get_char(cv, x, y) == 0xa0) 266 { 267 uint32_t attr = caca_get_attr(cv, x, y); 268 caca_put_char(cv, x, y, ' '); 269 caca_put_attr(cv, x, y, attr); 270 270 } 271 271 … … 273 273 ff->w = ff->h = 0; 274 274 275 //cv = c ucul_create_canvas(1, 1); /* XXX */275 //cv = caca_create_canvas(1, 1); /* XXX */ 276 276 277 277 /* from render.c */ 278 ff->lines += c ucul_get_canvas_height(cv);278 ff->lines += caca_get_canvas_height(cv); 279 279 280 280 return 0; … … 284 284 #define EXT_GLYPHS (STD_GLYPHS + 7) 285 285 286 static c ucul_figfont_t * open_figfont(char const *path)286 static caca_figfont_t * open_figfont(char const *path) 287 287 { 288 288 char altpath[2048]; 289 289 char buf[2048]; 290 290 char hardblank[10]; 291 c ucul_figfont_t *ff;291 caca_figfont_t *ff; 292 292 char *data = NULL; 293 c ucul_file_t *f;293 caca_file_t *f; 294 294 int i, j, size, comment_lines; 295 295 296 ff = malloc(sizeof(c ucul_figfont_t));296 ff = malloc(sizeof(caca_figfont_t)); 297 297 if(!ff) 298 298 { … … 302 302 303 303 /* Open font: if not found, try .tlf, then .flf */ 304 f = c ucul_file_open(path, "r");304 f = caca_file_open(path, "r"); 305 305 #if !defined __KERNEL__ && defined HAVE_SNPRINTF 306 306 … … 313 313 snprintf(altpath, 2047, "%s.tlf", path); 314 314 altpath[2047] = '\0'; 315 f = c ucul_file_open(altpath, "r");315 f = caca_file_open(altpath, "r"); 316 316 } 317 317 if(!f) … … 319 319 snprintf(altpath, 2047, "%s.flf", path); 320 320 altpath[2047] = '\0'; 321 f = c ucul_file_open(altpath, "r");321 f = caca_file_open(altpath, "r"); 322 322 } 323 323 #endif … … 333 333 ff->full_layout = 0; 334 334 ff->codetag_count = 0; 335 c ucul_file_gets(f, buf, 2048);335 caca_file_gets(f, buf, 2048); 336 336 if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank, 337 337 &ff->height, &ff->baseline, &ff->max_length, … … 340 340 { 341 341 debug("figfont error: `%s' has invalid header: %s", path, buf); 342 c ucul_file_close(f);342 caca_file_close(f); 343 343 free(ff); 344 344 seterrno(EINVAL); … … 352 352 debug("figfont error: `%s' has invalid layout %i/%u", 353 353 path, ff->old_layout, ff->full_layout); 354 c ucul_file_close(f);354 caca_file_close(f); 355 355 free(ff); 356 356 seterrno(EINVAL); … … 358 358 } 359 359 360 ff->hardblank = c ucul_utf8_to_utf32(hardblank, NULL);360 ff->hardblank = caca_utf8_to_utf32(hardblank, NULL); 361 361 362 362 /* Skip comment lines */ 363 363 for(i = 0; i < comment_lines; i++) 364 c ucul_file_gets(f, buf, 2048);364 caca_file_gets(f, buf, 2048); 365 365 366 366 /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223) … … 369 369 ff->lookup = NULL; 370 370 371 for(i = 0, size = 0; !c ucul_file_eof(f); ff->glyphs++)371 for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++) 372 372 { 373 373 if((ff->glyphs % 2048) == 0) … … 386 386 else 387 387 { 388 if(c ucul_file_gets(f, buf, 2048) == NULL)388 if(caca_file_gets(f, buf, 2048) == NULL) 389 389 break; 390 390 … … 397 397 { 398 398 for(j = 0; j < ff->height; j++) 399 c ucul_file_gets(f, buf, 2048);399 caca_file_gets(f, buf, 2048); 400 400 continue; 401 401 } … … 424 424 data = realloc(data, size += 2048); 425 425 426 c ucul_file_gets(f, data + i, 2048);426 caca_file_gets(f, data + i, 2048); 427 427 i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; 428 428 } 429 429 } 430 430 431 c ucul_file_close(f);431 caca_file_close(f); 432 432 433 433 if(ff->glyphs < EXT_GLYPHS) … … 443 443 444 444 /* Import buffer into canvas */ 445 ff->fontcv = c ucul_create_canvas(0, 0);446 c ucul_import_memory(ff->fontcv, data, i, "utf8");445 ff->fontcv = caca_create_canvas(0, 0); 446 caca_import_memory(ff->fontcv, data, i, "utf8"); 447 447 free(data); 448 448 … … 455 455 for(i = ff->max_length; i--;) 456 456 { 457 ch = c ucul_get_char(ff->fontcv, i, j);457 ch = caca_get_char(ff->fontcv, i, j); 458 458 459 459 /* Replace hardblanks with U+00A0 NO-BREAK SPACE */ 460 460 if(ch == ff->hardblank) 461 c ucul_put_char(ff->fontcv, i, j, ch = 0xa0);461 caca_put_char(ff->fontcv, i, j, ch = 0xa0); 462 462 463 463 if(oldch && ch != oldch) … … 467 467 } 468 468 else if(oldch && ch == oldch) 469 c ucul_put_char(ff->fontcv, i, j, ' ');469 caca_put_char(ff->fontcv, i, j, ' '); 470 470 else if(ch != ' ') 471 471 { 472 472 oldch = ch; 473 c ucul_put_char(ff->fontcv, i, j, ' ');473 caca_put_char(ff->fontcv, i, j, ' '); 474 474 } 475 475 } … … 479 479 } 480 480 481 int free_figfont(c ucul_figfont_t *ff)482 { 483 c ucul_free_canvas(ff->fontcv);481 int free_figfont(caca_figfont_t *ff) 482 { 483 caca_free_canvas(ff->fontcv); 484 484 free(ff->lookup); 485 485 free(ff);
Note: See TracChangeset
for help on using the changeset viewer.