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 * 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:
26 DUMP_FUNCTION_SPECIALIZED(struct document
*document
, int fd
,
27 unsigned char buf
[D_BUF
])
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
++) {
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
62 const unsigned char *utf8_buf
;
63 #else /* !DUMP_CHARSET_UTF8 */
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
) {
86 if (write_color_16(color
, fd
, buf
, &bptr
))
90 #elif defined(DUMP_COLOR_MODE_256)
91 if (foreground
!= color1
) {
93 if (write_color_256("38", foreground
, fd
, buf
, &bptr
))
97 if (background
!= color2
) {
99 if (write_color_256("48", background
, fd
, buf
, &bptr
))
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
))
110 if (memcmp(background
, new_background
, 3)) {
111 background
= new_background
;
112 if (write_true_color("48", background
, fd
, buf
, &bptr
))
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];
129 /* Print spaces if any. */
131 if (write_char(' ', fd
, buf
, &bptr
))
136 /* Print normal char. */
137 #ifdef DUMP_CHARSET_UTF8
138 utf8_buf
= encode_utf8(c
);
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
))
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
))
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
))
163 if (hard_write(fd
, buf
, bptr
) != bptr
)