1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id: export.c 3478 2009-05-19 12:46:34Z pterjan $ |
---|
8 | * |
---|
9 | * This library is free software. It comes without any warranty, to |
---|
10 | * the extent permitted by applicable law. You can redistribute it |
---|
11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * This file contains various export functions |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #if !defined(__KERNEL__) |
---|
23 | # include <stdlib.h> |
---|
24 | # include <stdio.h> |
---|
25 | # include <string.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "caca.h" |
---|
29 | #include "caca_internals.h" |
---|
30 | |
---|
31 | static inline int sprintu32(char *s, uint32_t x) |
---|
32 | { |
---|
33 | s[0] = (uint8_t)(x >> 24); |
---|
34 | s[1] = (uint8_t)(x >> 16) & 0xff; |
---|
35 | s[2] = (uint8_t)(x >> 8) & 0xff; |
---|
36 | s[3] = (uint8_t)(x ) & 0xff; |
---|
37 | return 4; |
---|
38 | } |
---|
39 | |
---|
40 | static inline int sprintu16(char *s, uint16_t x) |
---|
41 | { |
---|
42 | s[0] = (uint8_t)(x >> 8) & 0xff; |
---|
43 | s[1] = (uint8_t)(x ) & 0xff; |
---|
44 | return 2; |
---|
45 | } |
---|
46 | |
---|
47 | static void *export_caca(caca_canvas_t const *, size_t *); |
---|
48 | static void *export_ansi(caca_canvas_t const *, size_t *); |
---|
49 | static void *export_utf8(caca_canvas_t const *, size_t *, int); |
---|
50 | static void *export_html(caca_canvas_t const *, size_t *); |
---|
51 | static void *export_html3(caca_canvas_t const *, size_t *); |
---|
52 | static void *export_bbfr(caca_canvas_t const *, size_t *); |
---|
53 | static void *export_irc(caca_canvas_t const *, size_t *); |
---|
54 | static void *export_ps(caca_canvas_t const *, size_t *); |
---|
55 | static void *export_svg(caca_canvas_t const *, size_t *); |
---|
56 | static void *export_tga(caca_canvas_t const *, size_t *); |
---|
57 | |
---|
58 | /** \brief Export a canvas into a foreign format. |
---|
59 | * |
---|
60 | * This function exports a libcaca canvas into various foreign formats such |
---|
61 | * as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed |
---|
62 | * to free() to release the allocated storage when it is no longer needed. |
---|
63 | * |
---|
64 | * Valid values for \c format are: |
---|
65 | * - \c "caca": export native libcaca files. |
---|
66 | * - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes). |
---|
67 | * - \c "html": export an HTML page with CSS information. |
---|
68 | * - \c "html3": export an HTML table that should be compatible with |
---|
69 | * most navigators, including textmode ones. |
---|
70 | * - \c "irc": export UTF-8 text with mIRC colour codes. |
---|
71 | * - \c "ps": export a PostScript document. |
---|
72 | * - \c "svg": export an SVG vector image. |
---|
73 | * - \c "tga": export a TGA image. |
---|
74 | * |
---|
75 | * If an error occurs, NULL is returned and \b errno is set accordingly: |
---|
76 | * - \c EINVAL Unsupported format requested. |
---|
77 | * - \c ENOMEM Not enough memory to allocate output buffer. |
---|
78 | * |
---|
79 | * \param cv A libcaca canvas |
---|
80 | * \param format A string describing the requested output format. |
---|
81 | * \param bytes A pointer to a size_t where the number of allocated bytes |
---|
82 | * will be written. |
---|
83 | * \return A pointer to the exported memory area, or NULL in case of error. |
---|
84 | */ |
---|
85 | void *caca_export_memory(caca_canvas_t const *cv, char const *format, |
---|
86 | size_t *bytes) |
---|
87 | { |
---|
88 | if(!strcasecmp("caca", format)) |
---|
89 | return export_caca(cv, bytes); |
---|
90 | |
---|
91 | if(!strcasecmp("ansi", format)) |
---|
92 | return export_ansi(cv, bytes); |
---|
93 | |
---|
94 | if(!strcasecmp("utf8", format)) |
---|
95 | return export_utf8(cv, bytes, 0); |
---|
96 | |
---|
97 | if(!strcasecmp("utf8cr", format)) |
---|
98 | return export_utf8(cv, bytes, 1); |
---|
99 | |
---|
100 | if(!strcasecmp("html", format)) |
---|
101 | return export_html(cv, bytes); |
---|
102 | |
---|
103 | if(!strcasecmp("html3", format)) |
---|
104 | return export_html3(cv, bytes); |
---|
105 | |
---|
106 | if(!strcasecmp("bbfr", format)) |
---|
107 | return export_bbfr(cv, bytes); |
---|
108 | |
---|
109 | if(!strcasecmp("irc", format)) |
---|
110 | return export_irc(cv, bytes); |
---|
111 | |
---|
112 | if(!strcasecmp("ps", format)) |
---|
113 | return export_ps(cv, bytes); |
---|
114 | |
---|
115 | if(!strcasecmp("svg", format)) |
---|
116 | return export_svg(cv, bytes); |
---|
117 | |
---|
118 | if(!strcasecmp("tga", format)) |
---|
119 | return export_tga(cv, bytes); |
---|
120 | |
---|
121 | seterrno(EINVAL); |
---|
122 | return NULL; |
---|
123 | } |
---|
124 | |
---|
125 | /** \brief Get available export formats |
---|
126 | * |
---|
127 | * Return a list of available export formats. The list is a NULL-terminated |
---|
128 | * array of strings, interleaving a string containing the internal value for |
---|
129 | * the export format, to be used with caca_export_memory(), and a string |
---|
130 | * containing the natural language description for that export format. |
---|
131 | * |
---|
132 | * This function never fails. |
---|
133 | * |
---|
134 | * \return An array of strings. |
---|
135 | */ |
---|
136 | char const * const * caca_get_export_list(void) |
---|
137 | { |
---|
138 | static char const * const list[] = |
---|
139 | { |
---|
140 | "caca", "native libcaca format", |
---|
141 | "ansi", "ANSI", |
---|
142 | "utf8", "UTF-8 with ANSI escape codes", |
---|
143 | "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r", |
---|
144 | "html", "HTML", |
---|
145 | "html3", "backwards-compatible HTML", |
---|
146 | "bbfr", "BBCode (French)", |
---|
147 | "irc", "IRC with mIRC colours", |
---|
148 | "ps", "PostScript document", |
---|
149 | "svg", "SVG vector image", |
---|
150 | "tga", "TGA image", |
---|
151 | NULL, NULL |
---|
152 | }; |
---|
153 | |
---|
154 | return list; |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * XXX: the following functions are local. |
---|
159 | */ |
---|
160 | |
---|
161 | /* Generate a native libcaca canvas file. */ |
---|
162 | static void *export_caca(caca_canvas_t const *cv, size_t *bytes) |
---|
163 | { |
---|
164 | char *data, *cur; |
---|
165 | int f, i, n; |
---|
166 | |
---|
167 | /* at least 72 bytes for the header: |
---|
168 | * - 4 bytes for "\xCA\xCA" + "CV" |
---|
169 | * - 20 bytes for the canvas header |
---|
170 | * - 16 bytes for each dirty rectangle info |
---|
171 | * - 32 bytes for each frame info |
---|
172 | * 8 bytes for each character cell */ |
---|
173 | *bytes = 24 + 16 * cv->ndirty + (32 + 8 * cv->width * cv->height) * cv->framecount; |
---|
174 | cur = data = malloc(*bytes); |
---|
175 | |
---|
176 | /* magic */ |
---|
177 | cur += sprintf(cur, "%s", "\xCA\xCA" "CV"); |
---|
178 | |
---|
179 | /* canvas_header */ |
---|
180 | cur += sprintu32(cur, 20 + 16 * cv->ndirty + 32 * cv->framecount); |
---|
181 | cur += sprintu32(cur, cv->width * cv->height * 8 * cv->framecount); |
---|
182 | cur += sprintu16(cur, 0x0002); |
---|
183 | cur += sprintu32(cur, cv->ndirty); |
---|
184 | cur += sprintu32(cur, cv->framecount); |
---|
185 | cur += sprintu16(cur, 0x0000); |
---|
186 | |
---|
187 | /* dirty rectangles info */ |
---|
188 | for(i = 0; i < cv->ndirty; i++) |
---|
189 | { |
---|
190 | cur += sprintu32(cur, cv->dirty_xmin); |
---|
191 | cur += sprintu32(cur, cv->dirty_ymin); |
---|
192 | cur += sprintu32(cur, cv->dirty_xmax); |
---|
193 | cur += sprintu32(cur, cv->dirty_ymax); |
---|
194 | } |
---|
195 | |
---|
196 | /* frame_info */ |
---|
197 | for(f = 0; f < cv->framecount; f++) |
---|
198 | { |
---|
199 | cur += sprintu32(cur, cv->width); |
---|
200 | cur += sprintu32(cur, cv->height); |
---|
201 | cur += sprintu32(cur, 0); |
---|
202 | cur += sprintu32(cur, cv->curattr); |
---|
203 | cur += sprintu32(cur, cv->frames[f].x); |
---|
204 | cur += sprintu32(cur, cv->frames[f].y); |
---|
205 | cur += sprintu32(cur, cv->frames[f].handlex); |
---|
206 | cur += sprintu32(cur, cv->frames[f].handley); |
---|
207 | } |
---|
208 | |
---|
209 | /* canvas_data */ |
---|
210 | for(f = 0; f < cv->framecount; f++) |
---|
211 | { |
---|
212 | uint32_t *attrs = cv->frames[f].attrs; |
---|
213 | uint32_t *chars = cv->frames[f].chars; |
---|
214 | |
---|
215 | for(n = cv->height * cv->width; n--; ) |
---|
216 | { |
---|
217 | cur += sprintu32(cur, *chars++); |
---|
218 | cur += sprintu32(cur, *attrs++); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | return data; |
---|
223 | } |
---|
224 | |
---|
225 | /* Generate UTF-8 representation of current canvas. */ |
---|
226 | static void *export_utf8(caca_canvas_t const *cv, size_t *bytes, |
---|
227 | int cr) |
---|
228 | { |
---|
229 | static uint8_t const palette[] = |
---|
230 | { |
---|
231 | 0, 4, 2, 6, 1, 5, 3, 7, |
---|
232 | 8, 12, 10, 14, 9, 13, 11, 15 |
---|
233 | }; |
---|
234 | |
---|
235 | char *data, *cur; |
---|
236 | int x, y; |
---|
237 | |
---|
238 | /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus |
---|
239 | * 4 max bytes for a UTF-8 character). |
---|
240 | * Add height*9 to that (zeroes color at the end and jump to next line) */ |
---|
241 | *bytes = (cv->height * 9) + (cv->width * cv->height * 23); |
---|
242 | cur = data = malloc(*bytes); |
---|
243 | |
---|
244 | for(y = 0; y < cv->height; y++) |
---|
245 | { |
---|
246 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
247 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
248 | |
---|
249 | uint8_t prevfg = 0x10; |
---|
250 | uint8_t prevbg = 0x10; |
---|
251 | |
---|
252 | for(x = 0; x < cv->width; x++) |
---|
253 | { |
---|
254 | uint32_t attr = lineattr[x]; |
---|
255 | uint32_t ch = linechar[x]; |
---|
256 | uint8_t ansifg, ansibg, fg, bg; |
---|
257 | |
---|
258 | if(ch == CACA_MAGIC_FULLWIDTH) |
---|
259 | continue; |
---|
260 | |
---|
261 | ansifg = caca_attr_to_ansi_fg(attr); |
---|
262 | ansibg = caca_attr_to_ansi_bg(attr); |
---|
263 | |
---|
264 | fg = ansifg < 0x10 ? palette[ansifg] : 0x10; |
---|
265 | bg = ansibg < 0x10 ? palette[ansibg] : 0x10; |
---|
266 | |
---|
267 | /* TODO: the [0 could be omitted in some cases */ |
---|
268 | if(fg != prevfg || bg != prevbg) |
---|
269 | { |
---|
270 | cur += sprintf(cur, "\033[0"); |
---|
271 | |
---|
272 | if(fg < 8) |
---|
273 | cur += sprintf(cur, ";3%d", fg); |
---|
274 | else if(fg < 16) |
---|
275 | cur += sprintf(cur, ";1;3%d;9%d", fg - 8, fg - 8); |
---|
276 | |
---|
277 | if(bg < 8) |
---|
278 | cur += sprintf(cur, ";4%d", bg); |
---|
279 | else if(bg < 16) |
---|
280 | cur += sprintf(cur, ";5;4%d;10%d", bg - 8, bg - 8); |
---|
281 | |
---|
282 | cur += sprintf(cur, "m"); |
---|
283 | } |
---|
284 | |
---|
285 | cur += caca_utf32_to_utf8(cur, ch); |
---|
286 | |
---|
287 | prevfg = fg; |
---|
288 | prevbg = bg; |
---|
289 | } |
---|
290 | |
---|
291 | if(prevfg != 0x10 || prevbg != 0x10) |
---|
292 | cur += sprintf(cur, "\033[0m"); |
---|
293 | |
---|
294 | cur += sprintf(cur, cr ? "\r\n" : "\n"); |
---|
295 | } |
---|
296 | |
---|
297 | /* Crop to really used size */ |
---|
298 | debug("utf8 export: alloc %lu bytes, realloc %lu", |
---|
299 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
300 | *bytes = (uintptr_t)(cur - data); |
---|
301 | data = realloc(data, *bytes); |
---|
302 | |
---|
303 | return data; |
---|
304 | } |
---|
305 | |
---|
306 | /* Generate ANSI representation of current canvas. */ |
---|
307 | static void *export_ansi(caca_canvas_t const *cv, size_t *bytes) |
---|
308 | { |
---|
309 | static uint8_t const palette[] = |
---|
310 | { |
---|
311 | 0, 4, 2, 6, 1, 5, 3, 7, |
---|
312 | 8, 12, 10, 14, 9, 13, 11, 15 |
---|
313 | }; |
---|
314 | |
---|
315 | char *data, *cur; |
---|
316 | int x, y; |
---|
317 | |
---|
318 | uint8_t prevfg = -1; |
---|
319 | uint8_t prevbg = -1; |
---|
320 | |
---|
321 | /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus |
---|
322 | * 1 byte for a CP437 character). |
---|
323 | * Add height*9 to that (zeroes color at the end and jump to next line) */ |
---|
324 | *bytes = (cv->height * 9) + (cv->width * cv->height * 16); |
---|
325 | cur = data = malloc(*bytes); |
---|
326 | |
---|
327 | for(y = 0; y < cv->height; y++) |
---|
328 | { |
---|
329 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
330 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
331 | |
---|
332 | for(x = 0; x < cv->width; x++) |
---|
333 | { |
---|
334 | uint8_t ansifg = caca_attr_to_ansi_fg(lineattr[x]); |
---|
335 | uint8_t ansibg = caca_attr_to_ansi_bg(lineattr[x]); |
---|
336 | uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CACA_LIGHTGRAY; |
---|
337 | uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CACA_BLACK; |
---|
338 | uint32_t ch = linechar[x]; |
---|
339 | |
---|
340 | if(ch == CACA_MAGIC_FULLWIDTH) |
---|
341 | ch = '?'; |
---|
342 | |
---|
343 | if(fg != prevfg || bg != prevbg) |
---|
344 | { |
---|
345 | cur += sprintf(cur, "\033[0;"); |
---|
346 | |
---|
347 | if(fg < 8) |
---|
348 | if(bg < 8) |
---|
349 | cur += sprintf(cur, "3%d;4%dm", fg, bg); |
---|
350 | else |
---|
351 | cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8); |
---|
352 | else |
---|
353 | if(bg < 8) |
---|
354 | cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg); |
---|
355 | else |
---|
356 | cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8); |
---|
357 | } |
---|
358 | |
---|
359 | *cur++ = caca_utf32_to_cp437(ch); |
---|
360 | |
---|
361 | prevfg = fg; |
---|
362 | prevbg = bg; |
---|
363 | } |
---|
364 | |
---|
365 | if(cv->width == 80) |
---|
366 | { |
---|
367 | cur += sprintf(cur, "\033[s\n\033[u"); |
---|
368 | } |
---|
369 | else |
---|
370 | { |
---|
371 | cur += sprintf(cur, "\033[0m\r\n"); |
---|
372 | prevfg = -1; |
---|
373 | prevbg = -1; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | /* Crop to really used size */ |
---|
378 | debug("ansi export: alloc %lu bytes, realloc %lu", |
---|
379 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
380 | *bytes = (uintptr_t)(cur - data); |
---|
381 | data = realloc(data, *bytes); |
---|
382 | |
---|
383 | return data; |
---|
384 | } |
---|
385 | |
---|
386 | /* Generate HTML representation of current canvas. */ |
---|
387 | static void *export_html(caca_canvas_t const *cv, size_t *bytes) |
---|
388 | { |
---|
389 | char *data, *cur; |
---|
390 | int x, y, len; |
---|
391 | |
---|
392 | /* The HTML header: less than 1000 bytes |
---|
393 | * A line: 7 chars for "<br />\n" |
---|
394 | * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">" |
---|
395 | * 83 chars for ";font-weight..." |
---|
396 | * up to 10 chars for "&#xxxxxxx;", far less for pure ASCII |
---|
397 | * 7 chars for "</span>" */ |
---|
398 | *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 10 + 7)); |
---|
399 | cur = data = malloc(*bytes); |
---|
400 | |
---|
401 | /* HTML header */ |
---|
402 | |
---|
403 | cur += sprintf(cur, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"); |
---|
404 | cur += sprintf(cur, " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"); |
---|
405 | cur += sprintf(cur, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">"); |
---|
406 | cur += sprintf(cur, "<head>\n"); |
---|
407 | cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", |
---|
408 | caca_get_version()); |
---|
409 | cur += sprintf(cur, "</head><body>\n"); |
---|
410 | |
---|
411 | cur += sprintf(cur, "<div style=\"%s\">\n", |
---|
412 | "font-family: monospace, fixed; font-weight: bold;"); |
---|
413 | |
---|
414 | for(y = 0; y < cv->height; y++) |
---|
415 | { |
---|
416 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
417 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
418 | |
---|
419 | for(x = 0; x < cv->width; x += len) |
---|
420 | { |
---|
421 | cur += sprintf(cur, "<span style=\""); |
---|
422 | if(caca_attr_to_ansi_fg(lineattr[x]) != CACA_DEFAULT) |
---|
423 | cur += sprintf(cur, ";color:#%.03x", |
---|
424 | caca_attr_to_rgb12_fg(lineattr[x])); |
---|
425 | if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10) |
---|
426 | cur += sprintf(cur, ";background-color:#%.03x", |
---|
427 | caca_attr_to_rgb12_bg(lineattr[x])); |
---|
428 | if(lineattr[x] & CACA_BOLD) |
---|
429 | cur += sprintf(cur, ";font-weight:bold"); |
---|
430 | if(lineattr[x] & CACA_ITALICS) |
---|
431 | cur += sprintf(cur, ";font-style:italic"); |
---|
432 | if(lineattr[x] & CACA_UNDERLINE) |
---|
433 | cur += sprintf(cur, ";text-decoration:underline"); |
---|
434 | if(lineattr[x] & CACA_BLINK) |
---|
435 | cur += sprintf(cur, ";text-decoration:blink"); |
---|
436 | cur += sprintf(cur, "\">"); |
---|
437 | |
---|
438 | for(len = 0; |
---|
439 | x + len < cv->width && lineattr[x + len] == lineattr[x]; |
---|
440 | len++) |
---|
441 | { |
---|
442 | if(linechar[x + len] == CACA_MAGIC_FULLWIDTH) |
---|
443 | ; |
---|
444 | else if((linechar[x + len] <= 0x00000020) |
---|
445 | || |
---|
446 | ((linechar[x + len] >= 0x0000007f) |
---|
447 | && |
---|
448 | (linechar[x + len] <= 0x000000a0))) |
---|
449 | { |
---|
450 | /* Control characters and space converted to |
---|
451 | * U+00A0 NO-BREAK SPACE, a.k.a. " " in HTML, |
---|
452 | * but we use the equivalent numeric character |
---|
453 | * reference   so this will work in plain |
---|
454 | * XHTML with no DTD too. */ |
---|
455 | cur += sprintf(cur, " "); |
---|
456 | } |
---|
457 | else if(linechar[x + len] == '&') |
---|
458 | cur += sprintf(cur, "&"); |
---|
459 | else if(linechar[x + len] == '<') |
---|
460 | cur += sprintf(cur, "<"); |
---|
461 | else if(linechar[x + len] == '>') |
---|
462 | cur += sprintf(cur, ">"); |
---|
463 | else if(linechar[x + len] == '\"') |
---|
464 | cur += sprintf(cur, """); |
---|
465 | else if(linechar[x + len] == '\'') |
---|
466 | cur += sprintf(cur, "'"); |
---|
467 | else if(linechar[x + len] < 0x00000080) |
---|
468 | cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]); |
---|
469 | else if((linechar[x + len] <= 0x0010fffd) |
---|
470 | && |
---|
471 | ((linechar[x + len] & 0x0000fffe) != 0x0000fffe) |
---|
472 | && |
---|
473 | ((linechar[x + len] < 0x0000d800) |
---|
474 | || |
---|
475 | (linechar[x + len] > 0x0000dfff))) |
---|
476 | cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + len]); |
---|
477 | else |
---|
478 | /* non-character codepoints become U+FFFD |
---|
479 | * REPLACEMENT CHARACTER */ |
---|
480 | cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd); |
---|
481 | } |
---|
482 | cur += sprintf(cur, "</span>"); |
---|
483 | } |
---|
484 | /* New line */ |
---|
485 | cur += sprintf(cur, "<br />\n"); |
---|
486 | } |
---|
487 | |
---|
488 | cur += sprintf(cur, "</div></body></html>\n"); |
---|
489 | |
---|
490 | /* Crop to really used size */ |
---|
491 | debug("html export: alloc %lu bytes, realloc %lu", |
---|
492 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
493 | *bytes = (uintptr_t)(cur - data); |
---|
494 | data = realloc(data, *bytes); |
---|
495 | |
---|
496 | return data; |
---|
497 | } |
---|
498 | |
---|
499 | /* Export an HTML3 document. This function is way bigger than export_html(), |
---|
500 | * but permits viewing in old browsers (or limited ones such as links). It |
---|
501 | * will not work under gecko (mozilla rendering engine) unless you set a |
---|
502 | * correct header. */ |
---|
503 | static void *export_html3(caca_canvas_t const *cv, size_t *bytes) |
---|
504 | { |
---|
505 | char *data, *cur; |
---|
506 | int x, y, len; |
---|
507 | int has_multi_cell_row = 0; |
---|
508 | unsigned char *cell_boundary_bitmap; |
---|
509 | |
---|
510 | /* Table */ |
---|
511 | cell_boundary_bitmap = (unsigned char *) malloc((cv->width + 7) / 8); |
---|
512 | if(cell_boundary_bitmap) |
---|
513 | memset((void *) cell_boundary_bitmap, 0, (cv->width + 7) / 8); |
---|
514 | for(y = 0; y < cv->height; y++) |
---|
515 | { |
---|
516 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
517 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
518 | |
---|
519 | for(x = 1; x < cv->width; x++) |
---|
520 | if((! (cell_boundary_bitmap |
---|
521 | ? |
---|
522 | (cell_boundary_bitmap[x / 8] & (1 << (x % 8))) |
---|
523 | : |
---|
524 | has_multi_cell_row)) |
---|
525 | && |
---|
526 | (((linechar[x - 1] == CACA_MAGIC_FULLWIDTH) |
---|
527 | && |
---|
528 | (! caca_utf32_is_fullwidth(linechar[x]))) |
---|
529 | || |
---|
530 | (caca_attr_to_ansi_bg(lineattr[x - 1]) |
---|
531 | != |
---|
532 | caca_attr_to_ansi_bg(lineattr[x])) |
---|
533 | || |
---|
534 | ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10) |
---|
535 | ? |
---|
536 | (_caca_attr_to_rgb24bg(lineattr[x - 1]) |
---|
537 | != |
---|
538 | _caca_attr_to_rgb24bg(lineattr[x])) |
---|
539 | : |
---|
540 | 0))) |
---|
541 | { |
---|
542 | has_multi_cell_row = 1; |
---|
543 | if(cell_boundary_bitmap) |
---|
544 | cell_boundary_bitmap[x / 8] |= 1 << (x % 8); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | /* The HTML table markup: less than 1000 bytes |
---|
549 | * A line: 10 chars for "<tr></tr>\n" |
---|
550 | * A glyph: up to 48 chars for "<td bgcolor=\"#xxxxxx\"><tt><font color=\"#xxxxxx\">" |
---|
551 | * up to 36 chars for "<b><i><u><blink></blink></u></i></b>" |
---|
552 | * up to 10 chars for "&#xxxxxxx;" (far less for pure ASCII) |
---|
553 | * 17 chars for "</font></tt></td>" */ |
---|
554 | *bytes = 1000 + cv->height * (10 + cv->width * (48 + 36 + 10 + 17)); |
---|
555 | cur = data = malloc(*bytes); |
---|
556 | |
---|
557 | cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"[libcaca canvas export]\">\n"); |
---|
558 | |
---|
559 | for(y = 0; y < cv->height; y++) |
---|
560 | { |
---|
561 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
562 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
563 | |
---|
564 | cur += sprintf(cur, "<tr>"); |
---|
565 | |
---|
566 | for(x = 0; x < cv->width; x += len) |
---|
567 | { |
---|
568 | int i, needfont = 0; |
---|
569 | int nonblank = 0; |
---|
570 | |
---|
571 | /* Use colspan option to factor cells with same attributes |
---|
572 | * (see below) */ |
---|
573 | len = 1; |
---|
574 | while((x + len < cv->width) |
---|
575 | && |
---|
576 | ((y |
---|
577 | && |
---|
578 | (linechar[x + len] > 0x00000020) |
---|
579 | && |
---|
580 | ((linechar[x + len] < 0x0000007f) |
---|
581 | || |
---|
582 | (linechar[x + len] > 0x000000a0))) |
---|
583 | || |
---|
584 | (! (cell_boundary_bitmap |
---|
585 | ? |
---|
586 | (cell_boundary_bitmap[(x + len) / 8] & (1 << ((x + len) % 8))) |
---|
587 | : |
---|
588 | has_multi_cell_row)) |
---|
589 | || |
---|
590 | (linechar[x + len] == CACA_MAGIC_FULLWIDTH) |
---|
591 | || |
---|
592 | (cv->height == 1)) |
---|
593 | && |
---|
594 | ((linechar[x + len - 1] != CACA_MAGIC_FULLWIDTH) |
---|
595 | || |
---|
596 | caca_utf32_is_fullwidth(linechar[x + len])) |
---|
597 | && |
---|
598 | (caca_attr_to_ansi_bg(lineattr[x + len]) |
---|
599 | == |
---|
600 | caca_attr_to_ansi_bg(lineattr[x])) |
---|
601 | && |
---|
602 | ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10) |
---|
603 | ? |
---|
604 | (_caca_attr_to_rgb24bg(lineattr[x + len]) |
---|
605 | == |
---|
606 | _caca_attr_to_rgb24bg(lineattr[x])) |
---|
607 | : |
---|
608 | 1)) |
---|
609 | len++; |
---|
610 | |
---|
611 | for(i = 0; i < len; i++) |
---|
612 | if(! ((linechar[x + i] <= 0x00000020) |
---|
613 | || |
---|
614 | ((linechar[x + i] >= 0x0000007f) |
---|
615 | && |
---|
616 | (linechar[x + i] <= 0x000000a0)))) |
---|
617 | nonblank = 1; |
---|
618 | |
---|
619 | cur += sprintf(cur, "<td"); |
---|
620 | |
---|
621 | if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10) |
---|
622 | cur += sprintf(cur, " bgcolor=\"#%.06lx\"", (unsigned long int) |
---|
623 | _caca_attr_to_rgb24bg(lineattr[x])); |
---|
624 | |
---|
625 | if(has_multi_cell_row && (len > 1)) |
---|
626 | { |
---|
627 | int colspan; |
---|
628 | |
---|
629 | colspan = len; |
---|
630 | if(cell_boundary_bitmap) |
---|
631 | for(i = 0; i < len; i ++) |
---|
632 | if(i |
---|
633 | && |
---|
634 | ! (cell_boundary_bitmap[(x + i) / 8] |
---|
635 | & |
---|
636 | (1 << ((x + i) % 8)))) |
---|
637 | colspan --; |
---|
638 | if(colspan > 1) |
---|
639 | cur += sprintf(cur, " colspan=\"%d\"", colspan); |
---|
640 | } |
---|
641 | |
---|
642 | cur += sprintf(cur, ">"); |
---|
643 | |
---|
644 | cur += sprintf(cur, "<tt>"); |
---|
645 | |
---|
646 | for(i = 0; i < len; i++) |
---|
647 | { |
---|
648 | if(nonblank |
---|
649 | && |
---|
650 | ((! i) |
---|
651 | || |
---|
652 | (lineattr[x + i] != lineattr[x + i - 1]))) |
---|
653 | { |
---|
654 | needfont = (caca_attr_to_ansi_fg(lineattr[x + i]) |
---|
655 | != |
---|
656 | CACA_DEFAULT); |
---|
657 | |
---|
658 | if(needfont) |
---|
659 | cur += sprintf(cur, "<font color=\"#%.06lx\">", |
---|
660 | (unsigned long int) |
---|
661 | _caca_attr_to_rgb24fg(lineattr[x + i])); |
---|
662 | |
---|
663 | if(lineattr[x + i] & CACA_BOLD) |
---|
664 | cur += sprintf(cur, "<b>"); |
---|
665 | if(lineattr[x + i] & CACA_ITALICS) |
---|
666 | cur += sprintf(cur, "<i>"); |
---|
667 | if(lineattr[x + i] & CACA_UNDERLINE) |
---|
668 | cur += sprintf(cur, "<u>"); |
---|
669 | if(lineattr[x + i] & CACA_BLINK) |
---|
670 | cur += sprintf(cur, "<blink>"); |
---|
671 | } |
---|
672 | |
---|
673 | if(linechar[x + i] == CACA_MAGIC_FULLWIDTH) |
---|
674 | ; |
---|
675 | else if((linechar[x + i] <= 0x00000020) |
---|
676 | || |
---|
677 | ((linechar[x + i] >= 0x0000007f) |
---|
678 | && |
---|
679 | (linechar[x + i] <= 0x000000a0))) |
---|
680 | { |
---|
681 | /* Control characters and space converted to |
---|
682 | * U+00A0 NO-BREAK SPACE, a.k.a. " " in HTML, |
---|
683 | * but we use the equivalent numeric character |
---|
684 | * reference   so this will work in plain |
---|
685 | * XHTML with no DTD too. */ |
---|
686 | cur += sprintf(cur, " "); |
---|
687 | } |
---|
688 | else if(linechar[x + i] == '&') |
---|
689 | cur += sprintf(cur, "&"); |
---|
690 | else if(linechar[x + i] == '<') |
---|
691 | cur += sprintf(cur, "<"); |
---|
692 | else if(linechar[x + i] == '>') |
---|
693 | cur += sprintf(cur, ">"); |
---|
694 | else if(linechar[x + i] == '\"') |
---|
695 | cur += sprintf(cur, """); |
---|
696 | else if(linechar[x + i] == '\'') |
---|
697 | cur += sprintf(cur, "'"); |
---|
698 | else if(linechar[x + i] < 0x00000080) |
---|
699 | cur += sprintf(cur, "%c", (uint8_t)linechar[x + i]); |
---|
700 | else if((linechar[x + i] <= 0x0010fffd) |
---|
701 | && |
---|
702 | ((linechar[x + i] & 0x0000fffe) != 0x0000fffe) |
---|
703 | && |
---|
704 | ((linechar[x + i] < 0x0000d800) |
---|
705 | || |
---|
706 | (linechar[x + i] > 0x0000dfff))) |
---|
707 | cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]); |
---|
708 | else |
---|
709 | /* non-character codepoints become U+FFFD |
---|
710 | * REPLACEMENT CHARACTER */ |
---|
711 | cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd); |
---|
712 | |
---|
713 | if (nonblank |
---|
714 | && |
---|
715 | (((i + 1) == len) |
---|
716 | || |
---|
717 | (lineattr[x + i + 1] != lineattr[x + i]))) |
---|
718 | { |
---|
719 | if(lineattr[x + i] & CACA_BLINK) |
---|
720 | cur += sprintf(cur, "</blink>"); |
---|
721 | if(lineattr[x + i] & CACA_UNDERLINE) |
---|
722 | cur += sprintf(cur, "</u>"); |
---|
723 | if(lineattr[x + i] & CACA_ITALICS) |
---|
724 | cur += sprintf(cur, "</i>"); |
---|
725 | if(lineattr[x + i] & CACA_BOLD) |
---|
726 | cur += sprintf(cur, "</b>"); |
---|
727 | |
---|
728 | if(needfont) |
---|
729 | cur += sprintf(cur, "</font>"); |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | cur += sprintf(cur, "</tt>"); |
---|
734 | cur += sprintf(cur, "</td>"); |
---|
735 | } |
---|
736 | cur += sprintf(cur, "</tr>\n"); |
---|
737 | } |
---|
738 | |
---|
739 | /* Footer */ |
---|
740 | cur += sprintf(cur, "</table>\n"); |
---|
741 | |
---|
742 | /* Free working memory */ |
---|
743 | if (cell_boundary_bitmap) |
---|
744 | free((void *) cell_boundary_bitmap); |
---|
745 | |
---|
746 | /* Crop to really used size */ |
---|
747 | debug("html3 export: alloc %lu bytes, realloc %lu", |
---|
748 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
749 | *bytes = (uintptr_t)(cur - data); |
---|
750 | data = realloc(data, *bytes); |
---|
751 | |
---|
752 | return data; |
---|
753 | } |
---|
754 | |
---|
755 | static void *export_bbfr(caca_canvas_t const *cv, size_t *bytes) |
---|
756 | { |
---|
757 | char *data, *cur; |
---|
758 | int x, y, len; |
---|
759 | |
---|
760 | /* The font markup: less than 100 bytes |
---|
761 | * A line: 1 char for "\n" |
---|
762 | * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]" |
---|
763 | * up to 21 chars for "[g][i][s][/s][/i][/g]" |
---|
764 | * up to 6 chars for the UTF-8 glyph |
---|
765 | * 8 chars for "[/c][/f]" */ |
---|
766 | *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8)); |
---|
767 | cur = data = malloc(*bytes); |
---|
768 | |
---|
769 | /* Table */ |
---|
770 | cur += sprintf(cur, "[font=Courier New]"); |
---|
771 | |
---|
772 | for(y = 0; y < cv->height; y++) |
---|
773 | { |
---|
774 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
775 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
776 | |
---|
777 | for(x = 0; x < cv->width; x += len) |
---|
778 | { |
---|
779 | int i, needback, needfront; |
---|
780 | |
---|
781 | /* Use colspan option to factor cells with same attributes |
---|
782 | * (see below) */ |
---|
783 | len = 1; |
---|
784 | if(linechar[x] == ' ') |
---|
785 | while(x + len < cv->width && lineattr[x + len] == lineattr[x] |
---|
786 | && linechar[x] == ' ') |
---|
787 | len++; |
---|
788 | else |
---|
789 | while(x + len < cv->width && lineattr[x + len] == lineattr[x] |
---|
790 | && linechar[x] != ' ') |
---|
791 | len++; |
---|
792 | |
---|
793 | needback = caca_attr_to_ansi_bg(lineattr[x]) < 0x10; |
---|
794 | needfront = caca_attr_to_ansi_fg(lineattr[x]) < 0x10; |
---|
795 | |
---|
796 | if(needback) |
---|
797 | cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int) |
---|
798 | _caca_attr_to_rgb24bg(lineattr[x])); |
---|
799 | |
---|
800 | if(linechar[x] == ' ') |
---|
801 | cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int) |
---|
802 | _caca_attr_to_rgb24bg(lineattr[x])); |
---|
803 | else if(needfront) |
---|
804 | cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int) |
---|
805 | _caca_attr_to_rgb24fg(lineattr[x])); |
---|
806 | |
---|
807 | if(lineattr[x] & CACA_BOLD) |
---|
808 | cur += sprintf(cur, "[g]"); |
---|
809 | if(lineattr[x] & CACA_ITALICS) |
---|
810 | cur += sprintf(cur, "[i]"); |
---|
811 | if(lineattr[x] & CACA_UNDERLINE) |
---|
812 | cur += sprintf(cur, "[s]"); |
---|
813 | if(lineattr[x] & CACA_BLINK) |
---|
814 | ; /* FIXME */ |
---|
815 | |
---|
816 | for(i = 0; i < len; i++) |
---|
817 | { |
---|
818 | if(linechar[x + i] == CACA_MAGIC_FULLWIDTH) |
---|
819 | ; |
---|
820 | else if(linechar[x + i] == ' ') |
---|
821 | *cur++ = '_'; |
---|
822 | else |
---|
823 | cur += caca_utf32_to_utf8(cur, linechar[x + i]); |
---|
824 | } |
---|
825 | |
---|
826 | if(lineattr[x] & CACA_BLINK) |
---|
827 | ; /* FIXME */ |
---|
828 | if(lineattr[x] & CACA_UNDERLINE) |
---|
829 | cur += sprintf(cur, "[/s]"); |
---|
830 | if(lineattr[x] & CACA_ITALICS) |
---|
831 | cur += sprintf(cur, "[/i]"); |
---|
832 | if(lineattr[x] & CACA_BOLD) |
---|
833 | cur += sprintf(cur, "[/g]"); |
---|
834 | |
---|
835 | if(linechar[x] == ' ' || needfront) |
---|
836 | cur += sprintf(cur, "[/c]"); |
---|
837 | if(needback) |
---|
838 | cur += sprintf(cur, "[/f]"); |
---|
839 | } |
---|
840 | cur += sprintf(cur, "\n"); |
---|
841 | } |
---|
842 | |
---|
843 | /* Footer */ |
---|
844 | cur += sprintf(cur, "[/font]\n"); |
---|
845 | |
---|
846 | /* Crop to really used size */ |
---|
847 | debug("bbfr export: alloc %lu bytes, realloc %lu", |
---|
848 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
849 | *bytes = (uintptr_t)(cur - data); |
---|
850 | data = realloc(data, *bytes); |
---|
851 | |
---|
852 | return data; |
---|
853 | } |
---|
854 | |
---|
855 | /* Export a text file with IRC colours */ |
---|
856 | static void *export_irc(caca_canvas_t const *cv, size_t *bytes) |
---|
857 | { |
---|
858 | static uint8_t const palette[] = |
---|
859 | { |
---|
860 | 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */ |
---|
861 | 14, 12, 9, 11, 4, 13, 8, 0, /* Light */ |
---|
862 | }; |
---|
863 | |
---|
864 | char *data, *cur; |
---|
865 | int x, y; |
---|
866 | |
---|
867 | /* 14 bytes assumed for max length per pixel. Worst case scenario: |
---|
868 | * ^Cxx,yy 6 bytes |
---|
869 | * ^B^B 2 bytes |
---|
870 | * ch 6 bytes |
---|
871 | * 3 bytes for max length per line. Worst case scenario: |
---|
872 | * <spc> 1 byte (for empty lines) |
---|
873 | * \r\n 2 bytes |
---|
874 | * In real life, the average bytes per pixel value will be around 5. |
---|
875 | */ |
---|
876 | |
---|
877 | *bytes = 2 + cv->height * (3 + cv->width * 14); |
---|
878 | cur = data = malloc(*bytes); |
---|
879 | |
---|
880 | for(y = 0; y < cv->height; y++) |
---|
881 | { |
---|
882 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
883 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
884 | |
---|
885 | uint8_t prevfg = 0x10; |
---|
886 | uint8_t prevbg = 0x10; |
---|
887 | |
---|
888 | for(x = 0; x < cv->width; x++) |
---|
889 | { |
---|
890 | uint32_t attr = lineattr[x]; |
---|
891 | uint32_t ch = linechar[x]; |
---|
892 | uint8_t ansifg, ansibg, fg, bg; |
---|
893 | |
---|
894 | if(ch == CACA_MAGIC_FULLWIDTH) |
---|
895 | continue; |
---|
896 | |
---|
897 | ansifg = caca_attr_to_ansi_fg(attr); |
---|
898 | ansibg = caca_attr_to_ansi_bg(attr); |
---|
899 | |
---|
900 | fg = ansifg < 0x10 ? palette[ansifg] : 0x10; |
---|
901 | bg = ansibg < 0x10 ? palette[ansibg] : 0x10; |
---|
902 | |
---|
903 | /* TODO: optimise series of same fg / same bg |
---|
904 | * don't change fg value if ch == ' ' |
---|
905 | * make sure the \x03,%d trick works everywhere */ |
---|
906 | if(bg != prevbg || fg != prevfg) |
---|
907 | { |
---|
908 | int need_escape = 0; |
---|
909 | |
---|
910 | if(bg == 0x10) |
---|
911 | { |
---|
912 | if(fg == 0x10) |
---|
913 | cur += sprintf(cur, "\x0f"); |
---|
914 | else |
---|
915 | { |
---|
916 | if(prevbg == 0x10) |
---|
917 | cur += sprintf(cur, "\x03%d", fg); |
---|
918 | else |
---|
919 | cur += sprintf(cur, "\x0f\x03%d", fg); |
---|
920 | |
---|
921 | if(ch == (uint32_t)',') |
---|
922 | need_escape = 1; |
---|
923 | } |
---|
924 | } |
---|
925 | else |
---|
926 | { |
---|
927 | if(fg == 0x10) |
---|
928 | cur += sprintf(cur, "\x0f\x03,%d", bg); |
---|
929 | else |
---|
930 | cur += sprintf(cur, "\x03%d,%d", fg, bg); |
---|
931 | } |
---|
932 | |
---|
933 | if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9') |
---|
934 | need_escape = 1; |
---|
935 | |
---|
936 | if(need_escape) |
---|
937 | cur += sprintf(cur, "\x02\x02"); |
---|
938 | } |
---|
939 | |
---|
940 | cur += caca_utf32_to_utf8(cur, ch); |
---|
941 | prevfg = fg; |
---|
942 | prevbg = bg; |
---|
943 | } |
---|
944 | |
---|
945 | /* TODO: do the same the day we optimise whole lines above */ |
---|
946 | if(!cv->width) |
---|
947 | *cur++ = ' '; |
---|
948 | |
---|
949 | *cur++ = '\r'; |
---|
950 | *cur++ = '\n'; |
---|
951 | } |
---|
952 | |
---|
953 | /* Crop to really used size */ |
---|
954 | debug("IRC export: alloc %lu bytes, realloc %lu", |
---|
955 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
956 | *bytes = (uintptr_t)(cur - data); |
---|
957 | data = realloc(data, *bytes); |
---|
958 | |
---|
959 | return data; |
---|
960 | } |
---|
961 | |
---|
962 | /* Export a PostScript document. */ |
---|
963 | static void *export_ps(caca_canvas_t const *cv, size_t *bytes) |
---|
964 | { |
---|
965 | static char const *ps_header = |
---|
966 | "%!\n" |
---|
967 | "%% libcaca PDF export\n" |
---|
968 | "%%LanguageLevel: 2\n" |
---|
969 | "%%Pages: 1\n" |
---|
970 | "%%DocumentData: Clean7Bit\n" |
---|
971 | "/csquare {\n" |
---|
972 | " newpath\n" |
---|
973 | " 0 0 moveto\n" |
---|
974 | " 0 1 rlineto\n" |
---|
975 | " 1 0 rlineto\n" |
---|
976 | " 0 -1 rlineto\n" |
---|
977 | " closepath\n" |
---|
978 | " setrgbcolor\n" |
---|
979 | " fill\n" |
---|
980 | "} def\n" |
---|
981 | "/S {\n" |
---|
982 | " Show\n" |
---|
983 | "} bind def\n" |
---|
984 | "/Courier-Bold findfont\n" |
---|
985 | "8 scalefont\n" |
---|
986 | "setfont\n" |
---|
987 | "gsave\n" |
---|
988 | "6 10 scale\n"; |
---|
989 | |
---|
990 | char *data, *cur; |
---|
991 | int x, y; |
---|
992 | |
---|
993 | /* 200 is arbitrary but should be ok */ |
---|
994 | *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200); |
---|
995 | cur = data = malloc(*bytes); |
---|
996 | |
---|
997 | /* Header */ |
---|
998 | cur += sprintf(cur, "%s", ps_header); |
---|
999 | cur += sprintf(cur, "0 %d translate\n", cv->height); |
---|
1000 | |
---|
1001 | /* Background, drawn using csquare macro defined in header */ |
---|
1002 | for(y = cv->height; y--; ) |
---|
1003 | { |
---|
1004 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
1005 | |
---|
1006 | for(x = 0; x < cv->width; x++) |
---|
1007 | { |
---|
1008 | uint8_t argb[8]; |
---|
1009 | caca_attr_to_argb64(*lineattr++, argb); |
---|
1010 | cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n", |
---|
1011 | (float)argb[1] * (1.0 / 0xf), |
---|
1012 | (float)argb[2] * (1.0 / 0xf), |
---|
1013 | (float)argb[3] * (1.0 / 0xf)); |
---|
1014 | } |
---|
1015 | |
---|
1016 | /* Return to beginning of the line, and jump to the next one */ |
---|
1017 | cur += sprintf(cur, "-%d 1 translate\n", cv->width); |
---|
1018 | } |
---|
1019 | |
---|
1020 | cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */ |
---|
1021 | cur += sprintf(cur, "0 %d translate\n", cv->height*10); |
---|
1022 | |
---|
1023 | for(y = cv->height; y--; ) |
---|
1024 | { |
---|
1025 | uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width; |
---|
1026 | uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width; |
---|
1027 | |
---|
1028 | for(x = 0; x < cv->width; x++) |
---|
1029 | { |
---|
1030 | uint8_t argb[8]; |
---|
1031 | uint32_t ch = *linechar++; |
---|
1032 | |
---|
1033 | caca_attr_to_argb64(*lineattr++, argb); |
---|
1034 | |
---|
1035 | cur += sprintf(cur, "newpath\n"); |
---|
1036 | cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2); |
---|
1037 | cur += sprintf(cur, "%f %f %f setrgbcolor\n", |
---|
1038 | (float)argb[5] * (1.0 / 0xf), |
---|
1039 | (float)argb[6] * (1.0 / 0xf), |
---|
1040 | (float)argb[7] * (1.0 / 0xf)); |
---|
1041 | |
---|
1042 | if(ch < 0x00000020) |
---|
1043 | cur += sprintf(cur, "(?) show\n"); |
---|
1044 | else if(ch >= 0x00000080) |
---|
1045 | cur += sprintf(cur, "(?) show\n"); |
---|
1046 | else switch((uint8_t)(ch & 0x7f)) |
---|
1047 | { |
---|
1048 | case '\\': |
---|
1049 | case '(': |
---|
1050 | case ')': |
---|
1051 | cur += sprintf(cur, "(\\%c) show\n", (uint8_t)ch); |
---|
1052 | break; |
---|
1053 | default: |
---|
1054 | cur += sprintf(cur, "(%c) show\n", (uint8_t)ch); |
---|
1055 | break; |
---|
1056 | } |
---|
1057 | } |
---|
1058 | } |
---|
1059 | |
---|
1060 | cur += sprintf(cur, "showpage\n"); |
---|
1061 | |
---|
1062 | /* Crop to really used size */ |
---|
1063 | debug("PS export: alloc %lu bytes, realloc %lu", |
---|
1064 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
1065 | *bytes = (uintptr_t)(cur - data); |
---|
1066 | data = realloc(data, *bytes); |
---|
1067 | |
---|
1068 | return data; |
---|
1069 | } |
---|
1070 | |
---|
1071 | /* Export an SVG vector image */ |
---|
1072 | static void *export_svg(caca_canvas_t const *cv, size_t *bytes) |
---|
1073 | { |
---|
1074 | static char const svg_header[] = |
---|
1075 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
---|
1076 | "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\"" |
---|
1077 | " xmlns=\"http://www.w3.org/2000/svg\"" |
---|
1078 | " xmlns:xlink=\"http://www.w3.org/1999/xlink\"" |
---|
1079 | " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n"; |
---|
1080 | |
---|
1081 | char *data, *cur; |
---|
1082 | int x, y; |
---|
1083 | |
---|
1084 | /* 200 is arbitrary but should be ok */ |
---|
1085 | *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200; |
---|
1086 | cur = data = malloc(*bytes); |
---|
1087 | |
---|
1088 | /* Header */ |
---|
1089 | cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10, |
---|
1090 | cv->width * 6, cv->height * 10); |
---|
1091 | |
---|
1092 | cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\"" |
---|
1093 | " style=\"font-family: monospace\">\n"); |
---|
1094 | |
---|
1095 | /* Background */ |
---|
1096 | for(y = 0; y < cv->height; y++) |
---|
1097 | { |
---|
1098 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
1099 | |
---|
1100 | for(x = 0; x < cv->width; x++) |
---|
1101 | { |
---|
1102 | cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\"" |
---|
1103 | " width=\"6\" height=\"10\"/>\n", |
---|
1104 | caca_attr_to_rgb12_bg(*lineattr++), |
---|
1105 | x * 6, y * 10); |
---|
1106 | } |
---|
1107 | } |
---|
1108 | |
---|
1109 | /* Text */ |
---|
1110 | for(y = 0; y < cv->height; y++) |
---|
1111 | { |
---|
1112 | uint32_t *lineattr = cv->attrs + y * cv->width; |
---|
1113 | uint32_t *linechar = cv->chars + y * cv->width; |
---|
1114 | |
---|
1115 | for(x = 0; x < cv->width; x++) |
---|
1116 | { |
---|
1117 | uint32_t ch = *linechar++; |
---|
1118 | |
---|
1119 | if(ch == ' ' || ch == CACA_MAGIC_FULLWIDTH) |
---|
1120 | { |
---|
1121 | lineattr++; |
---|
1122 | continue; |
---|
1123 | } |
---|
1124 | |
---|
1125 | cur += sprintf(cur, "<text style=\"fill:#%.03x\" " |
---|
1126 | "x=\"%d\" y=\"%d\">", |
---|
1127 | caca_attr_to_rgb12_fg(*lineattr++), |
---|
1128 | x * 6, (y * 10) + 8); |
---|
1129 | |
---|
1130 | if(ch < 0x00000020) |
---|
1131 | *cur++ = '?'; |
---|
1132 | else if(ch > 0x0000007f) |
---|
1133 | cur += caca_utf32_to_utf8(cur, ch); |
---|
1134 | else switch((uint8_t)ch) |
---|
1135 | { |
---|
1136 | case '>': cur += sprintf(cur, ">"); break; |
---|
1137 | case '<': cur += sprintf(cur, "<"); break; |
---|
1138 | case '&': cur += sprintf(cur, "&"); break; |
---|
1139 | default: *cur++ = (uint8_t)ch; break; |
---|
1140 | } |
---|
1141 | cur += sprintf(cur, "</text>\n"); |
---|
1142 | } |
---|
1143 | } |
---|
1144 | |
---|
1145 | cur += sprintf(cur, " </g>\n"); |
---|
1146 | cur += sprintf(cur, "</svg>\n"); |
---|
1147 | |
---|
1148 | /* Crop to really used size */ |
---|
1149 | debug("SVG export: alloc %lu bytes, realloc %lu", |
---|
1150 | (unsigned long int)*bytes, (unsigned long int)(cur - data)); |
---|
1151 | *bytes = (uintptr_t)(cur - data); |
---|
1152 | data = realloc(data, *bytes); |
---|
1153 | |
---|
1154 | return data; |
---|
1155 | } |
---|
1156 | |
---|
1157 | /* Export a TGA image */ |
---|
1158 | static void *export_tga(caca_canvas_t const *cv, size_t *bytes) |
---|
1159 | { |
---|
1160 | char const * const *fontlist; |
---|
1161 | char *data, *cur; |
---|
1162 | caca_font_t *f; |
---|
1163 | int i, w, h; |
---|
1164 | |
---|
1165 | fontlist = caca_get_font_list(); |
---|
1166 | if(!fontlist[0]) |
---|
1167 | { |
---|
1168 | seterrno(EINVAL); |
---|
1169 | return NULL; |
---|
1170 | } |
---|
1171 | |
---|
1172 | f = caca_load_font(fontlist[0], 0); |
---|
1173 | |
---|
1174 | w = caca_get_canvas_width(cv) * caca_get_font_width(f); |
---|
1175 | h = caca_get_canvas_height(cv) * caca_get_font_height(f); |
---|
1176 | |
---|
1177 | *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */ |
---|
1178 | cur = data = malloc(*bytes); |
---|
1179 | |
---|
1180 | /* ID Length */ |
---|
1181 | cur += sprintf(cur, "%c", 0); |
---|
1182 | /* Color Map Type: no colormap */ |
---|
1183 | cur += sprintf(cur, "%c", 0); |
---|
1184 | /* Image Type: uncompressed truecolor */ |
---|
1185 | cur += sprintf(cur, "%c", 2); |
---|
1186 | /* Color Map Specification: no color map */ |
---|
1187 | memset(cur, 0, 5); cur += 5; |
---|
1188 | |
---|
1189 | /* Image Specification */ |
---|
1190 | cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */ |
---|
1191 | cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */ |
---|
1192 | cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */ |
---|
1193 | cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */ |
---|
1194 | cur += sprintf(cur, "%c", 32); /* Pixel Depth */ |
---|
1195 | cur += sprintf(cur, "%c", 40); /* Image Descriptor */ |
---|
1196 | |
---|
1197 | /* Image ID: no ID */ |
---|
1198 | /* Color Map Data: no colormap */ |
---|
1199 | |
---|
1200 | /* Image Data */ |
---|
1201 | caca_render_canvas(cv, f, cur, w, h, 4 * w); |
---|
1202 | |
---|
1203 | /* Swap bytes. What a waste of time. */ |
---|
1204 | for(i = 0; i < w * h * 4; i += 4) |
---|
1205 | { |
---|
1206 | char c; |
---|
1207 | c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c; |
---|
1208 | c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c; |
---|
1209 | } |
---|
1210 | |
---|
1211 | caca_free_font(f); |
---|
1212 | |
---|
1213 | return data; |
---|
1214 | } |
---|
1215 | |
---|
1216 | /* |
---|
1217 | * XXX: The following functions are aliases. |
---|
1218 | */ |
---|
1219 | |
---|
1220 | void *cucul_export_memory(cucul_canvas_t const *, char const *, |
---|
1221 | size_t *) CACA_ALIAS(caca_export_memory); |
---|
1222 | char const * const * cucul_get_export_list(void) |
---|
1223 | CACA_ALIAS(caca_get_export_list); |
---|
1224 | |
---|