dump: One #if for declarations and another for statements
[elinks.git] / src / viewer / dump / dump-specialized.h
blobc1e3433afc557a272763c1dc2e5585eca4a6501c
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 const 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 const int width = get_opt_int("document.dump.width");
38 #elif defined(DUMP_COLOR_MODE_TRUE)
39 static const unsigned char color[6] = {255, 255, 255, 0, 0, 0};
40 const unsigned char *foreground = &color[0];
41 const unsigned char *background = &color[3];
42 const 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 const unsigned char *utf8_buf;
63 #else /* !DUMP_CHARSET_UTF8 */
64 unsigned char c;
65 #endif /* !DUMP_CHARSET_UTF8 */
66 const unsigned char attr
67 = document->data[y].chars[x].attr;
68 #ifdef DUMP_COLOR_MODE_16
69 const unsigned char color1
70 = document->data[y].chars[x].color[0];
71 #elif defined(DUMP_COLOR_MODE_256)
72 const unsigned char color1
73 = document->data[y].chars[x].color[0];
74 const unsigned char color2
75 = document->data[y].chars[x].color[1];
76 #elif defined(DUMP_COLOR_MODE_TRUE)
77 const unsigned char *const new_foreground
78 = &document->data[y].chars[x].color[0];
79 const unsigned char *const new_background
80 = &document->data[y].chars[x].color[3];
81 #endif /* DUMP_COLOR_MODE_TRUE */
83 #ifdef DUMP_COLOR_MODE_16
84 if (color != color1) {
85 color = color1;
86 if (write_color_16(color, fd, buf, &bptr))
87 return -1;
90 #elif defined(DUMP_COLOR_MODE_256)
91 if (foreground != color1) {
92 foreground = color1;
93 if (write_color_256("38", foreground, fd, buf, &bptr))
94 return -1;
97 if (background != color2) {
98 background = color2;
99 if (write_color_256("48", background, fd, buf, &bptr))
100 return -1;
103 #elif defined(DUMP_COLOR_MODE_TRUE)
104 if (memcmp(foreground, new_foreground, 3)) {
105 foreground = new_foreground;
106 if (write_true_color("38", foreground, fd, buf, &bptr))
107 return -1;
110 if (memcmp(background, new_background, 3)) {
111 background = new_background;
112 if (write_true_color("48", background, fd, buf, &bptr))
113 return -1;
115 #endif /* DUMP_COLOR_MODE_TRUE */
117 c = document->data[y].chars[x].data;
119 if ((attr & SCREEN_ATTR_FRAME)
120 && c >= 176 && c < 224)
121 c = frame_dumb[c - 176];
123 if (c <= ' ') {
124 /* Count spaces. */
125 white++;
126 continue;
129 /* Print spaces if any. */
130 while (white) {
131 if (write_char(' ', fd, buf, &bptr))
132 return -1;
133 white--;
136 /* Print normal char. */
137 #ifdef DUMP_CHARSET_UTF8
138 utf8_buf = encode_utf8(c);
139 while (*utf8_buf) {
140 if (write_char(*utf8_buf++,
141 fd, buf, &bptr)) return -1;
144 x += unicode_to_cell(c) - 1;
145 #else /* !DUMP_CHARSET_UTF8 */
146 if (write_char(c, fd, buf, &bptr))
147 return -1;
148 #endif /* !DUMP_CHARSET_UTF8 */
151 #if defined(DUMP_COLOR_MODE_16) || defined(DUMP_COLOR_MODE_256) || defined(DUMP_COLOR_MODE_TRUE)
152 for (;x < width; x++) {
153 if (write_char(' ', fd, buf, &bptr))
154 return -1;
156 #endif /* DUMP_COLOR_MODE_16 || DUMP_COLOR_MODE_256 || DUMP_COLOR_MODE_TRUE */
158 /* Print end of line. */
159 if (write_char('\n', fd, buf, &bptr))
160 return -1;
163 if (hard_write(fd, buf, bptr) != bptr)
164 return -1;
166 return 0;