abace9b8e1cc947131fbdc67d9aea7f7287be778
[swftools.git] / lib / devices / record.c
1 /* gfxdevice_record.cc
2
3    Part of the swftools package.
4
5    Copyright (c) 2005 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include "../../config.h"
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <memory.h>
29 #ifdef HAVE_IO_H
30 #include <io.h>
31 #endif
32 #include <string.h>
33 #include "../gfxdevice.h"
34 #include "../gfxtools.h"
35 #include "../types.h"
36 #include "../bitio.h"
37 #include "../log.h"
38 #include "../os.h"
39 #include "record.h"
40
41 typedef struct _internal {
42     gfxfontlist_t* fontlist;
43     writer_t w;
44     int cliplevel;
45     char use_tempfile;
46     char*filename;
47 } internal_t;
48
49 typedef struct _internal_result {
50     char use_tempfile;
51     char*filename;
52     void*data;
53     int length;
54 } internal_result_t;
55
56 #define OP_END 0x00
57 #define OP_SETPARAM 0x01
58 #define OP_STROKE 0x02
59 #define OP_STARTCLIP 0x03
60 #define OP_ENDCLIP 0x04
61 #define OP_FILL 0x05
62 #define OP_FILLBITMAP 0x06
63 #define OP_FILLGRADIENT 0x07
64 #define OP_ADDFONT 0x08
65 #define OP_DRAWCHAR 0x09
66 #define OP_DRAWLINK 0x0a
67 #define OP_STARTPAGE 0x0b
68 #define OP_ENDPAGE 0x0c
69 #define OP_FINISH 0x0d
70
71 #define OP_MOVETO 0x0e
72 #define OP_LINETO 0x0f
73 #define OP_SPLINETO 0x10
74
75 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
76 {
77     internal_t*i = (internal_t*)dev->internal;
78     msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
79     writer_writeU8(&i->w, OP_SETPARAM);
80     writer_writeString(&i->w, key);
81     writer_writeString(&i->w, value);
82     return 1;
83 }
84
85 static void dumpLine(writer_t*w, gfxline_t*line)
86 {
87     while(line) {
88         if(line->type == gfx_moveTo) {
89             writer_writeU8(w, OP_MOVETO);
90             writer_writeDouble(w, line->x);
91             writer_writeDouble(w, line->y);
92         } else if(line->type == gfx_lineTo) {
93             writer_writeU8(w, OP_LINETO);
94             writer_writeDouble(w, line->x);
95             writer_writeDouble(w, line->y);
96         } else if(line->type == gfx_splineTo) {
97             writer_writeU8(w, OP_SPLINETO);
98             writer_writeDouble(w, line->x);
99             writer_writeDouble(w, line->y);
100             writer_writeDouble(w, line->sx);
101             writer_writeDouble(w, line->sy);
102         }
103         line = line->next;
104     }
105     writer_writeU8(w, OP_END);
106 }
107 static gfxline_t* readLine(reader_t*r)
108 {
109     gfxline_t*start = 0, *pos = 0;
110     while(1) {
111         unsigned char op = reader_readU8(r);
112         if(op == OP_END)
113             break;
114         gfxline_t*line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t));
115         if(!start) {
116             start = pos = line;
117         } else {
118             pos->next = line;
119             pos = line;
120         }
121         if(op == OP_MOVETO) {
122             line->type = gfx_moveTo;
123             line->x = reader_readDouble(r);
124             line->y = reader_readDouble(r);
125         } else if(op == OP_LINETO) {
126             line->type = gfx_lineTo;
127             line->x = reader_readDouble(r);
128             line->y = reader_readDouble(r);
129         } else if(op == OP_SPLINETO) {
130             line->type = gfx_splineTo;
131             line->x = reader_readDouble(r);
132             line->y = reader_readDouble(r);
133             line->sx = reader_readDouble(r);
134             line->sy = reader_readDouble(r);
135         }
136     }
137     return start;
138 }
139 static gfximage_t readImage(reader_t*r)
140 {
141     gfximage_t img;
142     img.width = reader_readU16(r);
143     img.height = reader_readU16(r);
144     img.data = (gfxcolor_t*)rfx_alloc(img.width*img.height*4);
145     r->read(r, img.data, img.width*img.height*4);
146     return img;
147 }
148 static gfxmatrix_t readMatrix(reader_t*r)
149 {
150     gfxmatrix_t matrix;
151     matrix.m00 = reader_readDouble(r);
152     matrix.m01 = reader_readDouble(r);
153     matrix.m10 = reader_readDouble(r);
154     matrix.m11 = reader_readDouble(r);
155     matrix.tx = reader_readDouble(r);
156     matrix.ty = reader_readDouble(r);
157     return matrix;
158 }
159 static gfxcolor_t readColor(reader_t*r)
160 {
161     gfxcolor_t col;
162     col.r = reader_readU8(r);
163     col.g = reader_readU8(r);
164     col.b = reader_readU8(r);
165     col.a = reader_readU8(r);
166     return col;
167 }
168 static gfxgradient_t* readGradient(reader_t*r)
169 {
170     gfxgradient_t*start = 0, *pos = 0;
171     while(1) {
172         U8 op = reader_readU8(r);
173         if(!op)
174             break;
175         gfxgradient_t*g = (gfxgradient_t*)rfx_calloc(sizeof(gfxgradient_t));
176         if(!start) {
177             start = pos = g;
178         } else {
179             pos->next = g;
180             pos = g;
181         }
182         g->color = readColor(r);
183         g->pos = reader_readFloat(r);
184     }
185     return start;
186 }
187 static gfxcxform_t* readCXForm(reader_t*r)
188 {
189     U8 type = reader_readU8(r);
190     if(!type)
191         return 0;
192     gfxcxform_t* c = (gfxcxform_t*)rfx_calloc(sizeof(gfxcxform_t));
193     c->rr = reader_readFloat(r); c->rg = reader_readFloat(r); c->rb = reader_readFloat(r); c->ra = reader_readFloat(r);
194     c->gr = reader_readFloat(r); c->gg = reader_readFloat(r); c->gb = reader_readFloat(r); c->ga = reader_readFloat(r);
195     c->br = reader_readFloat(r); c->bg = reader_readFloat(r); c->bb = reader_readFloat(r); c->ba = reader_readFloat(r);
196     c->ar = reader_readFloat(r); c->ag = reader_readFloat(r); c->ab = reader_readFloat(r); c->aa = reader_readFloat(r);
197     return c;
198 }
199
200 static void dumpColor(writer_t*w, gfxcolor_t*color)
201 {
202     writer_writeU8(w, color->r);
203     writer_writeU8(w, color->g);
204     writer_writeU8(w, color->b);
205     writer_writeU8(w, color->a);
206 }
207 static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
208 {
209     writer_writeDouble(w, matrix->m00);
210     writer_writeDouble(w, matrix->m01);
211     writer_writeDouble(w, matrix->m10);
212     writer_writeDouble(w, matrix->m11);
213     writer_writeDouble(w, matrix->tx);
214     writer_writeDouble(w, matrix->ty);
215 }
216 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
217 {
218     while(gradient) {
219         writer_writeU8(w, 1);
220         dumpColor(w, &gradient->color);
221         writer_writeFloat(w, gradient->pos);
222         gradient = gradient->next;
223     }
224     writer_writeU8(w, 0);
225 }
226 static void dumpImage(writer_t*w, gfximage_t*image)
227 {
228     writer_writeU16(w, image->width);
229     writer_writeU16(w, image->height);
230     w->write(w, image->data, image->width*image->height*4);
231 }
232 static void dumpCXForm(writer_t*w, gfxcxform_t*c)
233 {
234     if(!c) {
235         writer_writeU8(w, 0);
236     } else {
237         writer_writeU8(w, 1);
238         writer_writeFloat(w, c->rr); writer_writeFloat(w, c->rg); writer_writeFloat(w, c->rb); writer_writeFloat(w, c->ra);
239         writer_writeFloat(w, c->gr); writer_writeFloat(w, c->gg); writer_writeFloat(w, c->gb); writer_writeFloat(w, c->ga);
240         writer_writeFloat(w, c->br); writer_writeFloat(w, c->bg); writer_writeFloat(w, c->bb); writer_writeFloat(w, c->ba);
241         writer_writeFloat(w, c->ar); writer_writeFloat(w, c->ag); writer_writeFloat(w, c->ab); writer_writeFloat(w, c->aa);
242     }
243 }
244 static void dumpFont(writer_t*w, gfxfont_t*font)
245 {
246     writer_writeString(w, font->id);
247     writer_writeU32(w, font->num_glyphs);
248     writer_writeU32(w, font->max_unicode);
249     writer_writeDouble(w, font->ascent);
250     writer_writeDouble(w, font->descent);
251     int t;
252     for(t=0;t<font->num_glyphs;t++) {
253         dumpLine(w, font->glyphs[t].line);
254         writer_writeDouble(w, font->glyphs[t].advance);
255         writer_writeU32(w, font->glyphs[t].unicode);
256         if(font->glyphs[t].name) {
257             writer_writeString(w,font->glyphs[t].name);
258         } else {
259             writer_writeU8(w,0);
260         }
261     }
262     for(t=0;t<font->max_unicode;t++) {
263         writer_writeU32(w, font->unicode2glyph[t]);
264     }
265     writer_writeU32(w, font->kerning_size);
266     for(t=0;t<font->kerning_size;t++) {
267         writer_writeU32(w, font->kerning[t].c1);
268         writer_writeU32(w, font->kerning[t].c2);
269         writer_writeU32(w, font->kerning[t].advance);
270     }
271 }
272 static gfxfont_t*readFont(reader_t*r)
273 {
274     gfxfont_t* font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
275     font->id = reader_readString(r);
276     font->num_glyphs = reader_readU32(r);
277     font->max_unicode = reader_readU32(r);
278     font->ascent = reader_readDouble(r);
279     font->descent = reader_readDouble(r);
280     font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
281     font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
282     int t;
283     for(t=0;t<font->num_glyphs;t++) {
284         font->glyphs[t].line = readLine(r);
285         font->glyphs[t].advance = reader_readDouble(r);
286         font->glyphs[t].unicode = reader_readU32(r);
287         font->glyphs[t].name = reader_readString(r);
288         if(!font->glyphs[t].name[0]) {
289             free((void*)(font->glyphs[t].name));
290             font->glyphs[t].name = 0;
291         }
292     }
293     for(t=0;t<font->max_unicode;t++) {
294         font->unicode2glyph[t] = reader_readU32(r);
295     }
296     font->kerning_size = reader_readU32(r);
297     if(font->kerning_size) {
298         font->kerning = malloc(sizeof(gfxkerning_t)*font->kerning_size);
299         for(t=0;t<font->kerning_size;t++) {
300             font->kerning[t].c1 = reader_readU32(r);
301             font->kerning[t].c2 = reader_readU32(r);
302             font->kerning[t].advance = reader_readU32(r);
303         }
304     }
305     return font;
306 }
307
308 static void record_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
309 {
310     internal_t*i = (internal_t*)dev->internal;
311     msg("<trace> record: %08x STROKE\n", dev);
312     writer_writeU8(&i->w, OP_STROKE);
313     writer_writeDouble(&i->w, width);
314     writer_writeDouble(&i->w, miterLimit);
315     dumpColor(&i->w, color);
316     writer_writeU8(&i->w, cap_style);
317     writer_writeU8(&i->w, joint_style);
318     dumpLine(&i->w, line);
319 }
320
321 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
322 {
323     internal_t*i = (internal_t*)dev->internal;
324     msg("<trace> record: %08x STARTCLIP\n", dev);
325     writer_writeU8(&i->w, OP_STARTCLIP);
326     dumpLine(&i->w, line);
327     i->cliplevel++;
328 }
329
330 static void record_endclip(struct _gfxdevice*dev)
331 {
332     internal_t*i = (internal_t*)dev->internal;
333     msg("<trace> record: %08x ENDCLIP\n", dev);
334     writer_writeU8(&i->w, OP_ENDCLIP);
335     i->cliplevel--;
336     if(i->cliplevel<0) {
337         msg("<error> record: endclip() without startclip()");
338     }
339 }
340
341 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
342 {
343     internal_t*i = (internal_t*)dev->internal;
344     msg("<trace> record: %08x FILL\n", dev);
345     writer_writeU8(&i->w, OP_FILL);
346     dumpColor(&i->w, color);
347     dumpLine(&i->w, line);
348 }
349
350 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
351 {
352     internal_t*i = (internal_t*)dev->internal;
353     msg("<trace> record: %08x FILLBITMAP\n", dev);
354     writer_writeU8(&i->w, OP_FILLBITMAP);
355     dumpImage(&i->w, img);
356     dumpMatrix(&i->w, matrix);
357     dumpLine(&i->w, line);
358     dumpCXForm(&i->w, cxform);
359 }
360
361 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
362 {
363     internal_t*i = (internal_t*)dev->internal;
364     msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
365     writer_writeU8(&i->w, OP_FILLGRADIENT);
366     writer_writeU8(&i->w, type);
367     dumpGradient(&i->w, gradient);
368     dumpMatrix(&i->w, matrix);
369     dumpLine(&i->w, line);
370 }
371
372 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
373 {
374     internal_t*i = (internal_t*)dev->internal;
375     msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
376     if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
377         writer_writeU8(&i->w, OP_ADDFONT);
378         dumpFont(&i->w, font);
379         i->fontlist = gfxfontlist_addfont(i->fontlist, font);
380     }
381 }
382
383 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
384 {
385     internal_t*i = (internal_t*)dev->internal;
386     if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
387         record_addfont(dev, font);
388     }
389
390     msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
391     writer_writeU8(&i->w, OP_DRAWCHAR);
392     if(font) 
393         writer_writeString(&i->w, font->id);
394     else
395         writer_writeString(&i->w, "*NULL*");
396     writer_writeU32(&i->w, glyphnr);
397     dumpColor(&i->w, color);
398     dumpMatrix(&i->w, matrix);
399 }
400
401 static void record_startpage(struct _gfxdevice*dev, int width, int height)
402 {
403     internal_t*i = (internal_t*)dev->internal;
404     msg("<trace> record: %08x STARTPAGE\n", dev);
405     writer_writeU8(&i->w, OP_STARTPAGE);
406     writer_writeU16(&i->w, width);
407     writer_writeU16(&i->w, height);
408 }
409
410 static void record_endpage(struct _gfxdevice*dev)
411 {
412     internal_t*i = (internal_t*)dev->internal;
413     msg("<trace> record: %08x ENDPAGE\n", dev);
414     writer_writeU8(&i->w, OP_ENDPAGE);
415 }
416
417 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
418 {
419     internal_t*i = (internal_t*)dev->internal;
420     msg("<trace> record: %08x DRAWLINK\n", dev);
421     writer_writeU8(&i->w, OP_DRAWLINK);
422     dumpLine(&i->w, line);
423     writer_writeString(&i->w, action);
424 }
425
426 static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
427 {
428     internal_t*i = 0;
429     if(dev) {
430         i = (internal_t*)dev->internal;
431     }
432
433     gfxfontlist_t* fontlist = gfxfontlist_create();
434
435     while(1) {
436         unsigned char op;
437         if(r->read(r, &op, 1)!=1)
438             break;
439
440         switch(op) {
441             case OP_END:
442                 goto finish;
443             case OP_SETPARAM: {
444                 msg("<trace> replay: SETPARAM");
445                 char*key;
446                 char*value;
447                 key = reader_readString(r);
448                 value = reader_readString(r);
449                 out->setparameter(out, key, value);
450                 free(key);
451                 free(value);
452                 break;
453             }
454             case OP_STARTPAGE: {
455                 msg("<trace> replay: STARTPAGE");
456                 U16 width = reader_readU16(r);
457                 U16 height = reader_readU16(r);
458                 out->startpage(out, width, height);
459                 break;
460             }
461             case OP_ENDPAGE: {
462                 msg("<trace> replay: ENDPAGE");
463                 out->endpage(out);
464                 break;
465             }
466             case OP_FINISH: {
467                 msg("<trace> replay: FINISH");
468                 break;
469             }
470             case OP_STROKE: {
471                 msg("<trace> replay: STROKE");
472                 double width = reader_readDouble(r);
473                 double miterlimit = reader_readDouble(r);
474                 gfxcolor_t color = readColor(r);
475                 gfx_capType captype;
476                 int v = reader_readU8(r);
477                 switch (v) {
478                     case 0: captype = gfx_capButt; break;
479                     case 1: captype = gfx_capRound; break;
480                     case 2: captype = gfx_capSquare; break;
481                 }
482                 gfx_joinType jointtype;
483                 v = reader_readU8(r);
484                 switch (v) {
485                     case 0: jointtype = gfx_joinMiter; break;
486                     case 1: jointtype = gfx_joinRound; break;
487                     case 2: jointtype = gfx_joinBevel; break;
488                 }
489                 gfxline_t* line = readLine(r);
490                 out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
491                 gfxline_free(line);
492                 break;
493             }
494             case OP_STARTCLIP: {
495                 msg("<trace> replay: STARTCLIP");
496                 gfxline_t* line = readLine(r);
497                 out->startclip(out, line);
498                 gfxline_free(line);
499                 break;
500             }
501             case OP_ENDCLIP: {
502                 msg("<trace> replay: ENDCLIP");
503                 out->endclip(out);
504                 break;
505             }
506             case OP_FILL: {
507                 msg("<trace> replay: FILL");
508                 gfxcolor_t color = readColor(r);
509                 gfxline_t* line = readLine(r);
510                 out->fill(out, line, &color);
511                 gfxline_free(line);
512                 break;
513             }
514             case OP_FILLBITMAP: {
515                 msg("<trace> replay: FILLBITMAP");
516                 gfximage_t img = readImage(r);
517                 gfxmatrix_t matrix = readMatrix(r);
518                 gfxline_t* line = readLine(r);
519                 gfxcxform_t* cxform = readCXForm(r);
520                 out->fillbitmap(out, line, &img, &matrix, cxform);
521                 gfxline_free(line);
522                 if(cxform)
523                     free(cxform);
524                 free(img.data);img.data=0;
525                 break;
526             }
527             case OP_FILLGRADIENT: {
528                 msg("<trace> replay: FILLGRADIENT");
529                 gfxgradienttype_t type;
530                 int v = reader_readU8(r);
531                 switch (v) {
532                     case 0: 
533                       type = gfxgradient_radial; break;
534                     case 1:
535                       type = gfxgradient_linear; break;
536                 }  
537                 gfxgradient_t*gradient = readGradient(r);
538                 gfxmatrix_t matrix = readMatrix(r);
539                 gfxline_t* line = readLine(r);
540                 out->fillgradient(out, line, gradient, type, &matrix);
541                 break;
542             }
543             case OP_DRAWLINK: {
544                 msg("<trace> replay: DRAWLINK");
545                 gfxline_t* line = readLine(r);
546                 char* s = reader_readString(r);
547                 out->drawlink(out,line,s);
548                 gfxline_free(line);
549                 free(s);
550                 break;
551             }
552             case OP_ADDFONT: {
553                 msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
554                 gfxfont_t*font = readFont(r);
555                 fontlist = gfxfontlist_addfont(fontlist, font);
556                 out->addfont(out, font);
557                 break;
558             }
559             case OP_DRAWCHAR: {
560                 char* id = reader_readString(r);
561                 gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
562                 if(i && !font) {
563                     font = gfxfontlist_findfont(i->fontlist, id);
564                 }
565                 U32 glyph = reader_readU32(r);
566                 msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
567                 gfxcolor_t color = readColor(r);
568                 gfxmatrix_t matrix = readMatrix(r);
569                 out->drawchar(out, font, glyph, &color, &matrix);
570                 free(id);
571                 break;
572             }
573         }
574     }
575 finish:
576     r->dealloc(r);
577     /* problem: if we just replayed into a device which stores the
578        font for later use (the record device itself is a nice example),
579        then we can't free it yet */
580     //gfxfontlist_free(fontlist, 1);
581     gfxfontlist_free(fontlist, 0);
582 }
583 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
584 {
585     internal_result_t*i = (internal_result_t*)result->internal;
586     
587     reader_t r;
588     if(i->use_tempfile) {
589         reader_init_filereader2(&r, i->filename);
590     } else {
591         reader_init_memreader(&r, i->data, i->length);
592     }
593
594     replay(0, device, &r);
595 }
596
597 static void record_result_write(gfxresult_t*r, int filedesc)
598 {
599     internal_result_t*i = (internal_result_t*)r->internal;
600     if(i->data) {
601         write(filedesc, i->data, i->length);
602     }
603 }
604 static int record_result_save(gfxresult_t*r, const char*filename)
605 {
606     internal_result_t*i = (internal_result_t*)r->internal;
607     if(i->use_tempfile) {
608         move_file(i->filename, filename);
609     } else {
610         FILE*fi = fopen(filename, "wb");
611         if(!fi) {
612             fprintf(stderr, "Couldn't open file %s for writing\n", filename);
613             return -1;
614         }
615         fwrite(i->data, i->length, 1, fi);
616         fclose(fi);
617     }
618     return 0;
619 }
620 static void*record_result_get(gfxresult_t*r, const char*name)
621 {
622     internal_result_t*i = (internal_result_t*)r->internal;
623     if(!strcmp(name, "data")) {
624         return i->data;
625     } else if(!strcmp(name, "length")) {
626         return &i->length;
627     }
628     return 0;
629 }
630 static void record_result_destroy(gfxresult_t*r)
631 {
632     internal_result_t*i = (internal_result_t*)r->internal;
633     if(i->data) {
634         free(i->data);i->data = 0;
635     }
636     if(i->filename) {
637         unlink(i->filename);
638         free(i->filename);
639     }
640     free(r->internal);r->internal = 0;
641     free(r);
642 }
643
644 static unsigned char printable(unsigned char a)
645 {
646     if(a<32 || a==127) return '.';
647     else return a;
648 }
649
650 static void hexdumpMem(unsigned char*data, int len)
651 {
652     int t;
653     char ascii[32];
654     for(t=0;t<len;t++) {
655         printf("%02x ", data[t]);
656         ascii[t&15] = printable(data[t]);
657         if((t && ((t&15)==15)) || (t==len-1))
658         {
659             int s,p=((t)&15)+1;
660             ascii[p] = 0;
661             for(s=p-1;s<16;s++) {
662                 printf("   ");
663             }
664             printf(" %s\n", ascii);
665         }
666     }
667 }
668
669 void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
670 {
671     internal_t*i = (internal_t*)dev->internal;
672     if(out) {
673         if(!i->use_tempfile) {
674             int len=0;
675             void*data = writer_growmemwrite_memptr(&i->w, &len);
676             reader_t r;
677             reader_init_memreader(&r, data, len);
678             replay(dev, out, &r);
679             writer_growmemwrite_reset(&i->w);
680         } else {
681             msg("<fatal> Flushing not supported for file based record device");
682             exit(1);
683         }
684     }
685 }
686
687 static gfxresult_t* record_finish(struct _gfxdevice*dev)
688 {
689     internal_t*i = (internal_t*)dev->internal;
690     msg("<trace> record: %08x END", dev);
691
692     if(i->cliplevel) {
693         msg("<error> Warning: unclosed cliplevels");
694     }
695     
696     writer_writeU8(&i->w, OP_END);
697     
698     gfxfontlist_free(i->fontlist, 0);
699    
700     internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
701    
702     ir->use_tempfile = i->use_tempfile;
703     if(i->use_tempfile) {
704         ir->filename = i->filename;
705     } else {
706         ir->data = writer_growmemwrite_getmem(&i->w);
707         ir->length = i->w.pos;
708     }
709     i->w.finish(&i->w);
710
711     gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
712     result->save = record_result_save;
713     result->get = record_result_get;
714     result->destroy = record_result_destroy;
715     result->internal = ir;
716
717     free(dev->internal);memset(dev, 0, sizeof(gfxdevice_t));
718     
719     return result;
720 }
721
722 void gfxdevice_record_init(gfxdevice_t*dev, char use_tempfile)
723 {
724     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
725     memset(dev, 0, sizeof(gfxdevice_t));
726     
727     dev->name = "record";
728
729     dev->internal = i;
730   
731     i->use_tempfile = use_tempfile;
732     if(!use_tempfile) {
733         writer_init_growingmemwriter(&i->w, 1048576);
734     } else {
735         char buffer[128];
736         i->filename = strdup(mktempname(buffer, "gfx"));
737         writer_init_filewriter2(&i->w, i->filename);
738     }
739     i->fontlist = gfxfontlist_create();
740     i->cliplevel = 0;
741
742     dev->setparameter = record_setparameter;
743     dev->startpage = record_startpage;
744     dev->startclip = record_startclip;
745     dev->endclip = record_endclip;
746     dev->stroke = record_stroke;
747     dev->fill = record_fill;
748     dev->fillbitmap = record_fillbitmap;
749     dev->fillgradient = record_fillgradient;
750     dev->addfont = record_addfont;
751     dev->drawchar = record_drawchar;
752     dev->drawlink = record_drawlink;
753     dev->endpage = record_endpage;
754     dev->finish = record_finish;
755 }
756