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