exp-txt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "os.h"
  5. #include "export.h"
  6. static int txt_open(struct export *e);
  7. static int txt_option(struct export *e, int opt, char *arg);
  8. static int txt_output(struct export *e, char *name, struct fmt_page *pg);
  9. static char *txt_opts[] = // module options
  10. {
  11. "color", // generate ansi color codes (and attributes)
  12. "gfx-chr=<char>", // substitute <char> for gfx-symbols
  13. "fg=<0-7|none>", // assume term has <x> as foreground color
  14. "bg=<0-7|none>", // assume term has <x> as background color
  15. "lines=<1-25>", // output 24 or 25 lines
  16. 0
  17. };
  18. struct txt_data // private data in struct export
  19. {
  20. u8 color;
  21. u8 gfx_chr;
  22. u8 def_fg;
  23. u8 def_bg;
  24. int endline;
  25. struct fmt_char curr[1];
  26. FILE *fp;
  27. };
  28. struct export_module export_txt = // exported module definition
  29. {
  30. "ascii", // id
  31. "txt", // extension
  32. txt_opts, // options
  33. sizeof(struct txt_data), // data size
  34. txt_open, // open
  35. 0, // close
  36. txt_option, // option
  37. txt_output, // output
  38. };
  39. struct export_module export_ansi = // exported module definition
  40. {
  41. "ansi", // id
  42. "txt", // extension
  43. txt_opts, // options
  44. sizeof(struct txt_data), // data size
  45. txt_open, // open
  46. 0, // close
  47. txt_option, // option
  48. txt_output, // output
  49. };
  50. #define D ((struct txt_data *)e->data)
  51. char * my_stpcpy(char *dst, const char *src)
  52. {
  53. while (*dst = *src++)
  54. dst++;
  55. return dst;
  56. }
  57. static int txt_open(struct export *e)
  58. {
  59. D->gfx_chr = '#';
  60. D->def_fg = -1;
  61. D->def_bg = -1;
  62. D->endline = H;
  63. if (e->mod == &export_ansi)
  64. D->color = 1;
  65. return 0;
  66. }
  67. static int txt_option(struct export *e, int opt, char *arg)
  68. {
  69. switch (opt)
  70. {
  71. case 1: // color
  72. D->color = 1;
  73. break;
  74. case 2: // gfx-chr=
  75. D->gfx_chr = *arg ?: ' ';
  76. break;
  77. case 3: // fg=
  78. D->def_fg = *arg - '0';
  79. break;
  80. case 4: // bg=
  81. D->def_bg = *arg - '0';
  82. break;
  83. case 5: // lines=
  84. D->endline = atoi(arg);
  85. if (D->endline < 1 || D->endline > H)
  86. {
  87. export_error("lines: invalid number");
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. static void put_attr(struct export *e, struct fmt_char *new)
  94. {
  95. char buf[512];
  96. char *p = buf;
  97. int fg, bg, attr;
  98. int reset = 0;
  99. if (D->color)
  100. {
  101. fg = D->curr->fg ^ new->fg;
  102. bg = D->curr->bg ^ new->bg;
  103. attr = (D->curr->attr ^ new->attr) & (EA_BLINK | EA_DOUBLE);
  104. if (fg | bg | attr)
  105. {
  106. if (~new->attr & attr) // reset some attributes -> reset all.
  107. reset = 1;
  108. if (fg && new->fg == D->def_fg) // switch to def fg -> reset all
  109. reset = 1;
  110. if (bg && new->bg == D->def_bg) // switch to def bg -> reset all
  111. reset = 1;
  112. p = my_stpcpy(buf, "\e[");
  113. if (reset)
  114. {
  115. p = my_stpcpy(p, ";"); // "0;" but 0 isn't neccesary
  116. attr = -1; // set all attributes
  117. fg = new->fg ^ D->def_fg; // set fg if != default fg
  118. bg = new->bg ^ D->def_bg; // set bg if != default bg
  119. }
  120. if (attr & new->attr & EA_BLINK)
  121. p = my_stpcpy(p, "5;"); // blink
  122. if (attr & new->attr & EA_DOUBLE)
  123. p = my_stpcpy(p, "1;"); // bold
  124. if (fg)
  125. p += sprintf(p, "%d;", new->fg + 30); // fg-color
  126. if (bg)
  127. p += sprintf(p, "%d;", new->bg + 40); // bg-color
  128. p[-1] = 'm'; // replace last ;
  129. *D->curr = *new;
  130. }
  131. }
  132. *p++ = new->ch;
  133. *p = 0;
  134. fputs(buf, D->fp);
  135. }
  136. static int txt_output(struct export *e, char *name, struct fmt_page *pg)
  137. {
  138. struct fmt_char def_c[1];
  139. struct fmt_char l[W+2];
  140. #define L (l+1)
  141. int x, y;
  142. D->fp = fopen(name, "w");
  143. if (not D->fp)
  144. {
  145. export_error("cannot create file");
  146. return -1;
  147. }
  148. /* initialize default colors. These have to be restored at EOL. */
  149. def_c->ch = '\n';
  150. def_c->fg = D->def_fg;
  151. def_c->bg = D->def_bg;
  152. def_c->attr = E_DEF_ATTR;
  153. *D->curr = *def_c;
  154. L[-1] = L[W] = *def_c;
  155. for (y = 0; y < D->endline; y++)
  156. if (~pg->hid & (1 << y)) // not hidden
  157. {
  158. // character conversion
  159. for (x = 0; x < W; ++x)
  160. {
  161. struct fmt_char c = pg->data[y][x];
  162. switch (c.ch)
  163. {
  164. case 0x00: case 0xa0: c.ch = ' '; break;
  165. case 0x7f: c.ch = '*'; break;
  166. case BAD_CHAR: c.ch = '?'; break;
  167. default:
  168. if (c.attr & EA_GRAPHIC)
  169. c.ch = D->gfx_chr;
  170. break;
  171. }
  172. L[x] = c;
  173. }
  174. if (D->color)
  175. {
  176. // optimize color and attribute changes
  177. // delay fg and attr changes as far as possible
  178. for (x = 0; x < W; ++x)
  179. if (L[x].ch == ' ')
  180. {
  181. L[x].fg = L[x-1].fg;
  182. l[x].attr = L[x-1].attr;
  183. }
  184. // move fg and attr changes to prev bg change point
  185. for (x = W-1; x >= 0; x--)
  186. if (L[x].ch == ' ' && L[x].bg == L[x+1].bg)
  187. {
  188. L[x].fg = L[x+1].fg;
  189. L[x].attr = L[x+1].attr;
  190. }
  191. }
  192. // now emit the whole line (incl EOL)
  193. for (x = 0; x < W+1; ++x)
  194. put_attr(e, L + x);
  195. }
  196. fclose(D->fp);
  197. return 0;
  198. }