dump: Trim spaces only in color mode 0 or -1
[elinks.git] / src / viewer / dump / dump-specialized.h
blob3f91f9aa1ab515d4ce566178564906e6f2fb9273
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 * One of the following macros must be defined:
16 * - DUMP_COLOR_MODE_NONE
17 * - DUMP_COLOR_MODE_16
18 * - DUMP_COLOR_MODE_256
19 * - DUMP_COLOR_MODE_TRUE
21 * The following macro may be defined:
23 * - DUMP_CHARSET_UTF8
26 static int
27 DUMP_FUNCTION_SPECIALIZED(struct document *document, struct dump_output *out)
29 int y;
30 #ifdef DUMP_COLOR_MODE_16
31 unsigned char color = 0;
32 const int width = get_opt_int("document.dump.width");
33 #elif defined(DUMP_COLOR_MODE_256)
34 unsigned char foreground = 0;
35 unsigned char background = 0;
36 const int width = get_opt_int("document.dump.width");
37 #elif defined(DUMP_COLOR_MODE_TRUE)
38 static const unsigned char color[6] = {255, 255, 255, 0, 0, 0};
39 const unsigned char *foreground = &color[0];
40 const unsigned char *background = &color[3];
41 const int width = get_opt_int("document.dump.width");
42 #endif /* DUMP_COLOR_MODE_TRUE */
44 for (y = 0; y < document->height; y++) {
45 #ifdef DUMP_COLOR_MODE_NONE
46 int white = 0;
47 #endif
48 int x;
50 #ifdef DUMP_COLOR_MODE_16
51 write_color_16(color, out);
52 #elif defined(DUMP_COLOR_MODE_256)
53 write_color_256("38", foreground, out);
54 write_color_256("48", background, out);
55 #elif defined(DUMP_COLOR_MODE_TRUE)
56 write_true_color("38", foreground, out);
57 write_true_color("48", background, out);
58 #endif /* DUMP_COLOR_MODE_TRUE */
60 for (x = 0; x < document->data[y].length; x++) {
61 #ifdef DUMP_CHARSET_UTF8
62 unicode_val_T c;
63 const unsigned char *utf8_buf;
64 #else /* !DUMP_CHARSET_UTF8 */
65 unsigned char c;
66 #endif /* !DUMP_CHARSET_UTF8 */
67 const unsigned char attr
68 = document->data[y].chars[x].attr;
69 #ifdef DUMP_COLOR_MODE_16
70 const unsigned char color1
71 = document->data[y].chars[x].color[0];
72 #elif defined(DUMP_COLOR_MODE_256)
73 const unsigned char color1
74 = document->data[y].chars[x].color[0];
75 const unsigned char color2
76 = document->data[y].chars[x].color[1];
77 #elif defined(DUMP_COLOR_MODE_TRUE)
78 const unsigned char *const new_foreground
79 = &document->data[y].chars[x].color[0];
80 const unsigned char *const new_background
81 = &document->data[y].chars[x].color[3];
82 #endif /* DUMP_COLOR_MODE_TRUE */
84 c = document->data[y].chars[x].data;
86 #ifdef DUMP_CHARSET_UTF8
87 if (c == UCS_NO_CHAR) {
88 /* This is the second cell of
89 * a double-cell character. */
90 continue;
92 #endif /* DUMP_CHARSET_UTF8 */
94 #ifdef DUMP_COLOR_MODE_16
95 if (color != color1) {
96 color = color1;
97 if (write_color_16(color, out))
98 return -1;
101 #elif defined(DUMP_COLOR_MODE_256)
102 if (foreground != color1) {
103 foreground = color1;
104 if (write_color_256("38", foreground, out))
105 return -1;
108 if (background != color2) {
109 background = color2;
110 if (write_color_256("48", background, out))
111 return -1;
114 #elif defined(DUMP_COLOR_MODE_TRUE)
115 if (memcmp(foreground, new_foreground, 3)) {
116 foreground = new_foreground;
117 if (write_true_color("38", foreground, out))
118 return -1;
121 if (memcmp(background, new_background, 3)) {
122 background = new_background;
123 if (write_true_color("48", background, out))
124 return -1;
126 #endif /* DUMP_COLOR_MODE_TRUE */
128 if ((attr & SCREEN_ATTR_FRAME)
129 && c >= 176 && c < 224)
130 c = frame_dumb[c - 176];
132 #ifdef DUMP_CHARSET_UTF8
133 if (!isscreensafe_ucs(c)) c = ' ';
134 #else
135 if (!isscreensafe(c)) c = ' ';
136 #endif
138 #ifdef DUMP_COLOR_MODE_NONE
139 if (c == ' ') {
140 /* Count spaces. */
141 white++;
142 continue;
145 /* Print spaces if any. */
146 while (white) {
147 if (write_char(' ', out))
148 return -1;
149 white--;
151 #endif /* DUMP_COLOR_MODE_NONE */
153 /* Print normal char. */
154 #ifdef DUMP_CHARSET_UTF8
155 utf8_buf = encode_utf8(c);
156 while (*utf8_buf) {
157 if (write_char(*utf8_buf++, out)) return -1;
160 #else /* !DUMP_CHARSET_UTF8 */
161 if (write_char(c, out))
162 return -1;
163 #endif /* !DUMP_CHARSET_UTF8 */
166 #ifndef DUMP_COLOR_MODE_NONE
167 for (;x < width; x++) {
168 if (write_char(' ', out))
169 return -1;
171 #endif /* !DUMP_COLOR_MODE_NONE */
173 /* Print end of line. */
174 if (write_char('\n', out))
175 return -1;
178 if (dump_output_flush(out))
179 return -1;
181 return 0;