dump: Move local variable to reduce nesting
[elinks.git] / src / viewer / dump / dump-specialized.h
blobe7107a555e588fe6652f734780a81eb090d77be8
1 /* Fully specialized functions for dumping to a file.
3 * This include file defines a function that dumps the document to a
4 * file. The function is specialized to one color mode and one kind
5 * of charset. This is supposedly faster than runtime checks. The
6 * file that includes this file must define several macros to select
7 * the specialization.
9 * The following macro must be defined:
11 * - DUMP_FUNCTION_SPECIALIZED: The name of the function that this
12 * file should define.
14 * At most one of the following macros may be defined:
16 * - DUMP_COLOR_MODE_16
17 * - DUMP_COLOR_MODE_256
18 * - DUMP_COLOR_MODE_TRUE
20 * The following macro may be defined:
22 * - DUMP_CHARSET_UTF8
25 static int
26 DUMP_FUNCTION_SPECIALIZED(struct document *document, int fd,
27 unsigned char buf[D_BUF])
29 int y;
30 int bptr = 0;
31 #ifdef DUMP_COLOR_MODE_16
32 unsigned char color = 0;
33 int width = get_opt_int("document.dump.width");
34 #elif defined(DUMP_COLOR_MODE_256)
35 unsigned char foreground = 0;
36 unsigned char background = 0;
37 int width = get_opt_int("document.dump.width");
38 #elif defined(DUMP_COLOR_MODE_TRUE)
39 static unsigned char color[6] = {255, 255, 255, 0, 0, 0};
40 unsigned char *foreground = &color[0];
41 unsigned char *background = &color[3];
42 int width = get_opt_int("document.dump.width");
43 #endif /* DUMP_COLOR_MODE_TRUE */
45 for (y = 0; y < document->height; y++) {
46 int white = 0;
47 int x;
49 #ifdef DUMP_COLOR_MODE_16
50 write_color_16(color, fd, buf, &bptr);
51 #elif defined(DUMP_COLOR_MODE_256)
52 write_color_256("38", foreground, fd, buf, &bptr);
53 write_color_256("48", background, fd, buf, &bptr);
54 #elif defined(DUMP_COLOR_MODE_TRUE)
55 write_true_color("38", foreground, fd, buf, &bptr);
56 write_true_color("48", background, fd, buf, &bptr);
57 #endif /* DUMP_COLOR_MODE_TRUE */
59 for (x = 0; x < document->data[y].length; x++) {
60 #ifdef DUMP_CHARSET_UTF8
61 unicode_val_T c;
62 unsigned char *utf8_buf;
63 #else /* !DUMP_CHARSET_UTF8 */
64 unsigned char c;
65 #endif /* !DUMP_CHARSET_UTF8 */
66 unsigned char attr = document->data[y].chars[x].attr;
67 #ifdef DUMP_COLOR_MODE_16
68 unsigned char color1 = document->data[y].chars[x].color[0];
70 if (color != color1) {
71 color = color1;
72 if (write_color_16(color, fd, buf, &bptr))
73 return -1;
75 #elif defined(DUMP_COLOR_MODE_256)
76 unsigned char color1 = document->data[y].chars[x].color[0];
77 unsigned char color2 = document->data[y].chars[x].color[1];
79 if (foreground != color1) {
80 foreground = color1;
81 if (write_color_256("38", foreground, fd, buf, &bptr))
82 return -1;
85 if (background != color2) {
86 background = color2;
87 if (write_color_256("48", background, fd, buf, &bptr))
88 return -1;
90 #elif defined(DUMP_COLOR_MODE_TRUE)
91 unsigned char *new_foreground = &document->data[y].chars[x].color[0];
92 unsigned char *new_background = &document->data[y].chars[x].color[3];
94 if (memcmp(foreground, new_foreground, 3)) {
95 foreground = new_foreground;
96 if (write_true_color("38", foreground, fd, buf, &bptr))
97 return -1;
100 if (memcmp(background, new_background, 3)) {
101 background = new_background;
102 if (write_true_color("48", background, fd, buf, &bptr))
103 return -1;
105 #endif /* DUMP_COLOR_MODE_TRUE */
107 c = document->data[y].chars[x].data;
109 if ((attr & SCREEN_ATTR_FRAME)
110 && c >= 176 && c < 224)
111 c = frame_dumb[c - 176];
113 if (c <= ' ') {
114 /* Count spaces. */
115 white++;
116 continue;
119 /* Print spaces if any. */
120 while (white) {
121 if (write_char(' ', fd, buf, &bptr))
122 return -1;
123 white--;
126 /* Print normal char. */
127 #ifdef DUMP_CHARSET_UTF8
128 utf8_buf = encode_utf8(c);
129 while (*utf8_buf) {
130 if (write_char(*utf8_buf++,
131 fd, buf, &bptr)) return -1;
134 x += unicode_to_cell(c) - 1;
135 #else /* !DUMP_CHARSET_UTF8 */
136 if (write_char(c, fd, buf, &bptr))
137 return -1;
138 #endif /* !DUMP_CHARSET_UTF8 */
141 #if defined(DUMP_COLOR_MODE_16) || defined(DUMP_COLOR_MODE_256) || defined(DUMP_COLOR_MODE_TRUE)
142 for (;x < width; x++) {
143 if (write_char(' ', fd, buf, &bptr))
144 return -1;
146 #endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */
148 /* Print end of line. */
149 if (write_char('\n', fd, buf, &bptr))
150 return -1;
153 if (hard_write(fd, buf, bptr) != bptr)
154 return -1;
156 return 0;