1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
Copyright (c) 2003 Bruno T. C. de Oliveira
LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira
INFORMAES DE LICENA:
Este programa um software de livre distribuio; voc pode
redistribu-lo e/ou modific-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela verso 2 da licena ou qualquer verso posterior.
Este programa distribudo na esperana de que ele ser til
aos seus usurios, porm, SEM QUAISQUER GARANTIAS; sem sequer
a garantia implcita de COMERCIABILIDADE ou DE ADEQUAO A
QUALQUER FINALIDADE ESPECFICA. Consulte a GNU General Public
License para obter mais detalhes (uma cpia acompanha este
programa, armazenada no arquivo COPYING).
*/
#include "document.h"
#include "vlayer.h"
#include "export.h"
#include <stdbool.h>
#include "bores/bores.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
bool clear_screen = false;
bool suppress_newlines = false;
/* whether to suppress newlines on output. */
bool use_color = true;
char *outputfile;
FILE *f;
Document *doc; /* the document we are outputting */
int layer_number; /* the layer we will output; -1 means composite */
Layer *lyr; /* the layer we are printing; NULL if printing composite */
int width, height;
struct html_output_state_t {
bool font_tag_open, blink_tag_open; /* which html tags are open */
int color; /* The color we are currently outputting text in.
* This is a full 0-15 color code.*/
int bgcolor; /* The color we are currently putting text onto.
* This is a full 0-15 color code.*/
};
/* Extracts a cell from the appropriate layer (or build one,
* if we are dealing with a composite). Returns it. */
void get_decoded_cell(int x, int y, DecodedCell *dec) {
if (lyr) decode_cell(&lyr->cells[x][y], dec);
else {
Cell c = document_calc_effective_cell(doc, x, y);
decode_cell(&c, dec);
}
}
void output_text(void) {
VirtualLayer *vl;
if (lyr) vl = vlayer_create_from_layer(lyr);
else vl = vlayer_create_from_composite(doc);
if (!vl) {
fprintf(stderr, "Error creating virtual layer. Is the document empty?\n");
exit(2);
}
if (clear_screen) fputs("\x1B[2J\x1B[H", f);
if (!export_vlayer_to_ansi(vl, use_color, !suppress_newlines, false, f)) {
fprintf(stderr, "Error exporting virtual layer to ansi.\n");
exit(2);
}
vlayer_destroy(vl);
}
#define COL_BLACK 0
#define COL_RED 1
#define COL_GREEN 2
#define COL_YELLOW 3
#define COL_BLUE 4
#define COL_MAGENTA 5
#define COL_CYAN 6
#define COL_WHITE 7
static char *hex_codes[16] = {
"000000", "990000", "009900", "999900",
"000099", "990099", "009999", "999999",
"404040", "ff0000", "00ff00", "ffff00",
"0000ff", "ff00ff", "00ffff", "ffffff" };
void output_html(void) {
int x, y;
struct html_output_state_t cur_state;
DecodedCell dec;
fputs("<html><body style=\"color:#ffffff;background-color:#000000;\">"
"<pre>", f);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
get_decoded_cell(x, y, &dec);
/* first set up the blink tag */
if (use_color && dec.blink != cur_state.blink_tag_open) {
fprintf(f, "<%sblink>", dec.blink ? "" : "/");
cur_state.blink_tag_open = dec.blink;
}
/* set up color */
if (use_color && (dec.fg != cur_state.color ||
dec.bg != cur_state.bgcolor)) {
if (cur_state.font_tag_open) fputs("</span>", f);
fprintf(f, "<span style=\"color:#%s;background-color:#%s;\">",
hex_codes[dec.fg], hex_codes[dec.bg]);
cur_state.color = dec.fg;
cur_state.bgcolor = dec.bg;
cur_state.font_tag_open = true;
}
/* now render the character */
if (dec.ch >= 0 && dec.ch <= 32) fputs(" ", f);
else if (dec.ch == '&') fputs("&", f);
else if (dec.ch == '<') fputs("<", f);
else if (dec.ch == '>') fputs(">", f);
else fputc(dec.ch, f);
}
fputs("\n", f);
}
/* close tags as needed and call it a day */
if (cur_state.font_tag_open) fputs("</span>", f);
if (cur_state.blink_tag_open) fputs("</blink>", f);
fputs("</pre></body></html>", f);
}
void output_comment(void) {
printf ("%s\n", doc->metainfo);
}
void output_layercount(void) {
printf ("%d\n", doc->layer_count);
}
#define MYSYNTAX \
"Syntax: aecat [-b] [-c] [-{n|N}] [{-p | -l <layer_num>}]\n" \
" [-f <format>] [-o <output_file>] inputfile\n" \
"\n" \
" -f : specifies output format - can be text, html or comment\n" \
" The 'comment' format extracts document metadata.\n" \
" -c : prepend a 'clear screen' escape sequence (only valid when\n" \
" outputting text)\n" \
" -o : writes output to specified file rather than stdout\n" \
" -b : disables output of color (only characters will be\n" \
" printed).\n" \
" -l : specifies which layer of the document is to be used.\n" \
" (must be an index, not a layer name). By default,\n" \
" layer 0 will be used.\n" \
" -L : displays how many layers the document has.\n" \
" -p : exports a composite, that is, overlays all visible layers,\n" \
" paying attention to layer transparency, etc. The size of the\n" \
" composite will be the size of the first layer\n" \
" -n : suppress output of newlines\n" \
" -h : prints this help text\n"
void rtfm() { /* rtfm = Read the FAQ and Manual, of course :-) */
fprintf(stderr, MYSYNTAX);
exit(1);
}
#define FMT_TEXT 1
#define FMT_HTML 2
#define FMT_COMMENT 3
#define FMT_LAYERS 4
int main(int argc, char **argv) {
static char optstring[] = "o:chl:Lbpf:n";
char *inputfile;
int ch;
int out_fmt = 0; /* In what format to output. */
while (0 < (ch = getopt(argc, argv, optstring)) ) {
switch (ch) {
case 'c': clear_screen = true; break;
case 'o': outputfile = strdup(optarg); break;
case 'l': layer_number = atoi(optarg); break;
case 'L': out_fmt = FMT_LAYERS; break;
case 'p': layer_number = -1; break;
case 'b': use_color = false; break;
case 'n': suppress_newlines = true; break;
case 'f':
if (strcmp(optarg, "text") == 0)
out_fmt = FMT_TEXT;
else if (strcmp(optarg, "html") == 0)
out_fmt = FMT_HTML;
else if (strcmp(optarg, "comment") == 0)
out_fmt = FMT_COMMENT;
else {
fprintf(stderr, "Invalid format \"%s\"\n", optarg);
exit(1);
}
break;
default : rtfm();
}
}
if (!out_fmt)
out_fmt = FMT_TEXT;
if (out_fmt != FMT_TEXT && clear_screen)
fprintf(stderr, "Warning: -c option only valid when outputting text\n");
if (out_fmt != FMT_TEXT && suppress_newlines)
fprintf(stderr, "Warning: -n option only valid when "
"outputting text\n");
if (optind >= argc) rtfm();
inputfile = strdup(argv[optind]);
if (! (doc = document_load_from(inputfile)) ) {
fprintf(stderr, "Error loading document from %s (bad format?).\n",
inputfile);
fprintf(stderr, "Error description:\n %s\n", aeff_get_error());
exit(1);
}
if (layer_number >= doc->layer_count) {
if (layer_number)
fprintf(stderr, "Document %s does not have the specified layer: %d\n",
inputfile, layer_number);
else
fprintf(stderr, "Document %s has no layers.\n", inputfile);
exit(1);
}
width = doc->layers[0]->width;
height = doc->layers[0]->height;
lyr = layer_number >= 0 ? doc->layers[layer_number] : NULL;
if (outputfile && !(f = fopen(outputfile, "w"))) {
fprintf(stderr, "Error opening %s for writing.\n", outputfile);
exit(1);
}
else f = stdout; /* print to standard output if no output file specified */
switch(out_fmt) {
case FMT_TEXT: output_text(); break;
case FMT_HTML: output_html(); break;
case FMT_COMMENT: output_comment(); break;
case FMT_LAYERS: output_layercount(); break;
}
if (f != stdout) fclose(f);
return 0;
}
|