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 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
++) {
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 unsigned char *utf8_buf
;
63 #else /* !DUMP_CHARSET_UTF8 */
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
) {
72 if (write_color_16(color
, fd
, buf
, &bptr
))
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
) {
81 if (write_color_256("38", foreground
, fd
, buf
, &bptr
))
85 if (background
!= color2
) {
87 if (write_color_256("48", background
, fd
, buf
, &bptr
))
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
))
100 if (memcmp(background
, new_background
, 3)) {
101 background
= new_background
;
102 if (write_true_color("48", background
, fd
, buf
, &bptr
))
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];
119 /* Print spaces if any. */
121 if (write_char(' ', fd
, buf
, &bptr
))
126 /* Print normal char. */
127 #ifdef DUMP_CHARSET_UTF8
128 utf8_buf
= encode_utf8(c
);
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
))
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
))
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
))
153 if (hard_write(fd
, buf
, bptr
) != bptr
)