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
9 * The following macro must be defined:
11 * - DUMP_FUNCTION_SPECIALIZED: The name of the function that this
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:
27 DUMP_FUNCTION_SPECIALIZED(struct document
*document
, struct dump_output
*out
)
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
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
63 const unsigned char *utf8_buf
;
64 #else /* !DUMP_CHARSET_UTF8 */
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. */
92 #endif /* DUMP_CHARSET_UTF8 */
94 #ifdef DUMP_COLOR_MODE_16
95 if (color
!= color1
) {
97 if (write_color_16(color
, out
))
101 #elif defined(DUMP_COLOR_MODE_256)
102 if (foreground
!= color1
) {
104 if (write_color_256("38", foreground
, out
))
108 if (background
!= color2
) {
110 if (write_color_256("48", background
, out
))
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
))
121 if (memcmp(background
, new_background
, 3)) {
122 background
= new_background
;
123 if (write_true_color("48", background
, out
))
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
= ' ';
135 if (!isscreensafe(c
)) c
= ' ';
138 #ifdef DUMP_COLOR_MODE_NONE
145 /* Print spaces if any. */
147 if (write_char(' ', out
))
151 #endif /* DUMP_COLOR_MODE_NONE */
153 /* Print normal char. */
154 #ifdef DUMP_CHARSET_UTF8
155 utf8_buf
= encode_utf8(c
);
157 if (write_char(*utf8_buf
++, out
)) return -1;
160 #else /* !DUMP_CHARSET_UTF8 */
161 if (write_char(c
, out
))
163 #endif /* !DUMP_CHARSET_UTF8 */
166 #ifndef DUMP_COLOR_MODE_NONE
167 for (;x
< width
; x
++) {
168 if (write_char(' ', out
))
171 #endif /* !DUMP_COLOR_MODE_NONE */
173 /* Print end of line. */
174 if (write_char('\n', out
))
178 if (dump_output_flush(out
))