Changeset 3235
- Timestamp:
- Nov 3, 2008, 5:55:00 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca/codec/export.c
r3227 r3235 460 460 char *data, *cur; 461 461 int x, y, len; 462 int maxcols;463 462 int has_multi_cell_row = 0; 463 unsigned char *cell_boundary_bitmap; 464 464 465 465 /* Table */ 466 maxcols = 0; 466 cell_boundary_bitmap = (unsigned char *) malloc((cv->width + 7) / 8); 467 if(cell_boundary_bitmap) 468 memset((void *) cell_boundary_bitmap, 0, (cv->width + 7) / 8); 467 469 for(y = 0; y < cv->height; y++) 468 470 { 469 471 uint32_t *lineattr = cv->attrs + y * cv->width; 470 uint32_t *linechar = cv->chars + y * cv->width; 471 int cols = 0; 472 473 for(x = 0; x < cv->width; x++) 474 { 475 if((! has_multi_cell_row) 472 473 for(x = 1; x < cv->width; x++) 474 if((! (cell_boundary_bitmap 475 ? 476 (cell_boundary_bitmap[x / 8] & (1 << (x % 8))) 477 : 478 has_multi_cell_row)) 476 479 && 477 (x > 1) 478 && 479 (caca_attr_to_ansi_bg(lineattr[x - 1]) 480 != 481 caca_attr_to_ansi_bg(lineattr[x])) 482 && 483 ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10) 484 ? 485 (_caca_attr_to_rgb24bg(lineattr[x - 1]) 480 ((caca_attr_to_ansi_bg(lineattr[x - 1]) 486 481 != 487 _caca_attr_to_rgb24bg(lineattr[x])) 488 : 489 0)) 482 caca_attr_to_ansi_bg(lineattr[x])) 483 || 484 ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10) 485 ? 486 (_caca_attr_to_rgb24bg(lineattr[x - 1]) 487 != 488 _caca_attr_to_rgb24bg(lineattr[x])) 489 : 490 0))) 490 491 { 491 492 has_multi_cell_row = 1; 493 if(cell_boundary_bitmap) 494 cell_boundary_bitmap[x / 8] |= 1 << (x % 8); 492 495 } 493 if(linechar[x] == 0x00000009)494 while((cols + 1) % 8)495 cols ++;496 cols ++;497 }498 if (cols > maxcols)499 maxcols = cols;500 496 } 501 497 … … 506 502 * up to 10 chars for "&#xxxxxxx;" (far less for pure ASCII) 507 503 * 17 chars for "</font></tt></td>" */ 508 *bytes = 1000 + cv->height * (10 + maxcols* (48 + 36 + 10 + 17));504 *bytes = 1000 + cv->height * (10 + cv->width * (48 + 36 + 10 + 17)); 509 505 cur = data = malloc(*bytes); 510 506 511 cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"[libcaca canvas html3export]\">\n");507 cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"[libcaca canvas export]\">\n"); 512 508 513 509 for(y = 0; y < cv->height; y++) … … 515 511 uint32_t *lineattr = cv->attrs + y * cv->width; 516 512 uint32_t *linechar = cv->chars + y * cv->width; 517 int taboff = 0;518 513 519 514 cur += sprintf(cur, "<tr>"); … … 522 517 { 523 518 int i, needfont = 0; 524 int thistab= 0;519 int nonblank = 0; 525 520 526 521 /* Use colspan option to factor cells with same attributes 527 522 * (see below) */ 528 523 len = 1; 529 while(( y || (! has_multi_cell_row) || (cv->height == 1))524 while((x + len < cv->width) 530 525 && 531 (x + len < cv->width) 526 (y 527 || 528 (! (cell_boundary_bitmap 529 ? 530 (cell_boundary_bitmap[(x + len) / 8] & (1 << ((x + len) % 8))) 531 : 532 has_multi_cell_row)) 533 || 534 (cv->height == 1)) 532 535 && 533 536 (caca_attr_to_ansi_bg(lineattr[x + len]) … … 545 548 546 549 for(i = 0; i < len; i++) 547 if(linechar[x + i] == 0x00000009) 548 while((x + i + taboff + thistab + 1) % 8) 549 thistab ++; 550 if(! ((linechar[x + i] <= 0x00000020) 551 || 552 ((linechar[x + i] >= 0x0000007f) 553 && 554 (linechar[x + i] <= 0x000000a0)))) 555 nonblank = 1; 550 556 551 557 cur += sprintf(cur, "<td"); … … 555 561 _caca_attr_to_rgb24bg(lineattr[x])); 556 562 557 if((len + thistab) > 1) 558 cur += sprintf(cur, " colspan=\"%d\"", len + thistab); 563 if(has_multi_cell_row && (len > 1)) 564 { 565 int colspan; 566 567 colspan = len; 568 if(cell_boundary_bitmap) 569 for(i = 0; i < len; i ++) 570 if(i 571 && 572 ! (cell_boundary_bitmap[(x + i) / 8] 573 & 574 (1 << ((x + i) % 8)))) 575 colspan --; 576 if(colspan > 1) 577 cur += sprintf(cur, " colspan=\"%d\"", colspan); 578 } 559 579 560 580 cur += sprintf(cur, ">"); … … 564 584 for(i = 0; i < len; i++) 565 585 { 566 if((! i) || (lineattr[x + i] != lineattr[x + i - 1])) 586 if(nonblank 587 && 588 ((! i) 589 || 590 (lineattr[x + i] != lineattr[x + i - 1]))) 567 591 { 568 needfont = (caca_attr_to_ansi_fg(lineattr[x + i]) != CACA_DEFAULT); 592 needfont = (caca_attr_to_ansi_fg(lineattr[x + i]) 593 != 594 CACA_DEFAULT); 569 595 570 596 if(needfont) 571 cur += sprintf(cur, "<font color=\"#%.06lx\">", (unsigned long int) 597 cur += sprintf(cur, "<font color=\"#%.06lx\">", 598 (unsigned long int) 572 599 _caca_attr_to_rgb24fg(lineattr[x + i])); 573 600 … … 588 615 ((linechar[x + i] >= 0x0000007f) 589 616 && 590 (linechar[x + i] <= 0x000000 9f)))617 (linechar[x + i] <= 0x000000a0))) 591 618 { 592 619 /* Control characters and space converted to … … 594 621 * but we use the equivalent numeric character 595 622 * reference   so this will work in plain 596 * XHTML with no DTD too. We also expand tabs 597 * here, since they are not honored in HTML. */ 598 if(linechar[x + i] == 0x00000009) 599 { 600 while((x + i + taboff + 1) % 8) 601 { 602 cur += sprintf(cur, " "); 603 taboff ++; 604 } 605 } 623 * XHTML with no DTD too. */ 606 624 cur += sprintf(cur, " "); 607 625 } … … 631 649 cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd); 632 650 633 if (((i + 1) == len) || (lineattr[x + i + 1] != lineattr[x + i])) 651 if (nonblank 652 && 653 (((i + 1) == len) 654 || 655 (lineattr[x + i + 1] != lineattr[x + i]))) 634 656 { 635 657 if(lineattr[x + i] & CACA_BLINK) … … 655 677 /* Footer */ 656 678 cur += sprintf(cur, "</table>\n"); 679 680 /* Free working memory */ 681 if (cell_boundary_bitmap) 682 free((void *) cell_boundary_bitmap); 657 683 658 684 /* Crop to really used size */
Note: See TracChangeset
for help on using the changeset viewer.