compacted chars in record device
[swftools.git] / lib / devices / record.c
index b14d51a..d0d7715 100644 (file)
 #include <io.h>
 #endif
 #include <string.h>
+#include <assert.h>
 #include "../gfxdevice.h"
 #include "../gfxtools.h"
 #include "../types.h"
 #include "../bitio.h"
 #include "../log.h"
+#include "../os.h"
 #include "record.h"
 
+typedef struct _state {
+    char*last_string[16];
+    gfxcolor_t last_color[16];
+    gfxmatrix_t last_matrix[16];
+} state_t;
+
 typedef struct _internal {
     gfxfontlist_t* fontlist;
+    state_t state;
+
     writer_t w;
+    int cliplevel;
+    char use_tempfile;
+    char*filename;
 } internal_t;
 
 typedef struct _internal_result {
+    char use_tempfile;
+    char*filename;
     void*data;
     int length;
 } internal_result_t;
@@ -62,14 +77,16 @@ typedef struct _internal_result {
 #define OP_ENDPAGE 0x0c
 #define OP_FINISH 0x0d
 
-#define OP_MOVETO 0x0e
-#define OP_LINETO 0x0f
-#define OP_SPLINETO 0x10
+#define FLAG_SAME_AS_LAST 0x10
+
+#define LINE_MOVETO 0x0e
+#define LINE_LINETO 0x0f
+#define LINE_SPLINETO 0x10
 
 static int record_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: SETPARAM %s %s\n", key, value);
+    msg("<trace> record: %08x SETPARAM %s %s\n", dev, key, value);
     writer_writeU8(&i->w, OP_SETPARAM);
     writer_writeString(&i->w, key);
     writer_writeString(&i->w, value);
@@ -80,15 +97,15 @@ static void dumpLine(writer_t*w, gfxline_t*line)
 {
     while(line) {
        if(line->type == gfx_moveTo) {
-           writer_writeU8(w, OP_MOVETO);
+           writer_writeU8(w, LINE_MOVETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
        } else if(line->type == gfx_lineTo) {
-           writer_writeU8(w, OP_LINETO);
+           writer_writeU8(w, LINE_LINETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
        } else if(line->type == gfx_splineTo) {
-           writer_writeU8(w, OP_SPLINETO);
+           writer_writeU8(w, LINE_SPLINETO);
            writer_writeDouble(w, line->x);
            writer_writeDouble(w, line->y);
            writer_writeDouble(w, line->sx);
@@ -112,15 +129,15 @@ static gfxline_t* readLine(reader_t*r)
            pos->next = line;
            pos = line;
        }
-       if(op == OP_MOVETO) {
+       if(op == LINE_MOVETO) {
            line->type = gfx_moveTo;
            line->x = reader_readDouble(r);
            line->y = reader_readDouble(r);
-       } else if(op == OP_LINETO) {
+       } else if(op == LINE_LINETO) {
            line->type = gfx_lineTo;
            line->x = reader_readDouble(r);
            line->y = reader_readDouble(r);
-       } else if(op == OP_SPLINETO) {
+       } else if(op == LINE_SPLINETO) {
            line->type = gfx_splineTo;
            line->x = reader_readDouble(r);
            line->y = reader_readDouble(r);
@@ -159,6 +176,12 @@ static gfxcolor_t readColor(reader_t*r)
     col.a = reader_readU8(r);
     return col;
 }
+static void readXY(reader_t*r, gfxmatrix_t*m)
+{
+    m->tx = reader_readDouble(r);
+    m->ty = reader_readDouble(r);
+}
+
 static gfxgradient_t* readGradient(reader_t*r)
 {
     gfxgradient_t*start = 0, *pos = 0;
@@ -207,6 +230,11 @@ static void dumpMatrix(writer_t*w, gfxmatrix_t*matrix)
     writer_writeDouble(w, matrix->tx);
     writer_writeDouble(w, matrix->ty);
 }
+static void dumpXY(writer_t*w, gfxmatrix_t*matrix)
+{
+    writer_writeDouble(w, matrix->tx);
+    writer_writeDouble(w, matrix->ty);
+}
 static void dumpGradient(writer_t*w, gfxgradient_t*gradient)
 {
     while(gradient) {
@@ -240,6 +268,8 @@ static void dumpFont(writer_t*w, gfxfont_t*font)
     writer_writeString(w, font->id);
     writer_writeU32(w, font->num_glyphs);
     writer_writeU32(w, font->max_unicode);
+    writer_writeDouble(w, font->ascent);
+    writer_writeDouble(w, font->descent);
     int t;
     for(t=0;t<font->num_glyphs;t++) {
        dumpLine(w, font->glyphs[t].line);
@@ -254,6 +284,12 @@ static void dumpFont(writer_t*w, gfxfont_t*font)
     for(t=0;t<font->max_unicode;t++) {
        writer_writeU32(w, font->unicode2glyph[t]);
     }
+    writer_writeU32(w, font->kerning_size);
+    for(t=0;t<font->kerning_size;t++) {
+       writer_writeU32(w, font->kerning[t].c1);
+       writer_writeU32(w, font->kerning[t].c2);
+       writer_writeU32(w, font->kerning[t].advance);
+    }
 }
 static gfxfont_t*readFont(reader_t*r)
 {
@@ -261,6 +297,8 @@ static gfxfont_t*readFont(reader_t*r)
     font->id = reader_readString(r);
     font->num_glyphs = reader_readU32(r);
     font->max_unicode = reader_readU32(r);
+    font->ascent = reader_readDouble(r);
+    font->descent = reader_readDouble(r);
     font->glyphs = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t)*font->num_glyphs);
     font->unicode2glyph = (int*)rfx_calloc(sizeof(font->unicode2glyph[0])*font->max_unicode);
     int t;
@@ -277,13 +315,22 @@ static gfxfont_t*readFont(reader_t*r)
     for(t=0;t<font->max_unicode;t++) {
        font->unicode2glyph[t] = reader_readU32(r);
     }
+    font->kerning_size = reader_readU32(r);
+    if(font->kerning_size) {
+       font->kerning = malloc(sizeof(gfxkerning_t)*font->kerning_size);
+       for(t=0;t<font->kerning_size;t++) {
+           font->kerning[t].c1 = reader_readU32(r);
+           font->kerning[t].c2 = reader_readU32(r);
+           font->kerning[t].advance = reader_readU32(r);
+       }
+    }
     return font;
 }
 
 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)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: STROKE\n");
+    msg("<trace> record: %08x STROKE\n", dev);
     writer_writeU8(&i->w, OP_STROKE);
     writer_writeDouble(&i->w, width);
     writer_writeDouble(&i->w, miterLimit);
@@ -296,22 +343,27 @@ static void record_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t widt
 static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: STARTCLIP\n");
+    msg("<trace> record: %08x STARTCLIP\n", dev);
     writer_writeU8(&i->w, OP_STARTCLIP);
     dumpLine(&i->w, line);
+    i->cliplevel++;
 }
 
 static void record_endclip(struct _gfxdevice*dev)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: ENDCLIP\n");
+    msg("<trace> record: %08x ENDCLIP\n", dev);
     writer_writeU8(&i->w, OP_ENDCLIP);
+    i->cliplevel--;
+    if(i->cliplevel<0) {
+       msg("<error> record: endclip() without startclip()");
+    }
 }
 
 static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: FILL\n");
+    msg("<trace> record: %08x FILL\n", dev);
     writer_writeU8(&i->w, OP_FILL);
     dumpColor(&i->w, color);
     dumpLine(&i->w, line);
@@ -320,7 +372,7 @@ static void record_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
 static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: FILLBITMAP\n");
+    msg("<trace> record: %08x FILLBITMAP\n", dev);
     writer_writeU8(&i->w, OP_FILLBITMAP);
     dumpImage(&i->w, img);
     dumpMatrix(&i->w, matrix);
@@ -331,7 +383,7 @@ static void record_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*
 static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: FILLGRADIENT\n");
+    msg("<trace> record: %08x FILLGRADIENT %08x\n", dev, gradient);
     writer_writeU8(&i->w, OP_FILLGRADIENT);
     writer_writeU8(&i->w, type);
     dumpGradient(&i->w, gradient);
@@ -342,10 +394,12 @@ static void record_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradie
 static void record_addfont(struct _gfxdevice*dev, gfxfont_t*font)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: ADDFONT\n");
-    writer_writeU8(&i->w, OP_ADDFONT);
-    dumpFont(&i->w, font);
-    i->fontlist = gfxfontlist_addfont(i->fontlist, font);
+    msg("<trace> record: %08x ADDFONT %s\n", dev, font->id);
+    if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
+       writer_writeU8(&i->w, OP_ADDFONT);
+       dumpFont(&i->w, font);
+       i->fontlist = gfxfontlist_addfont(i->fontlist, font);
+    }
 }
 
 static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
@@ -353,24 +407,35 @@ static void record_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr,
     internal_t*i = (internal_t*)dev->internal;
     if(font && !gfxfontlist_hasfont(i->fontlist, font)) {
        record_addfont(dev, font);
-       i->fontlist = gfxfontlist_addfont(i->fontlist, font);
     }
 
-    msg("<trace> record: DRAWCHAR %d\n", glyphnr);
-    writer_writeU8(&i->w, OP_DRAWCHAR);
-    if(font) 
-       writer_writeString(&i->w, font->id);
-    else
-       writer_writeString(&i->w, "*NULL*");
+    msg("<trace> record: %08x DRAWCHAR %d\n", glyphnr, dev);
+    const char*font_id = font->id?font->id:"*NULL*";
+    
+    gfxmatrix_t*l = &i->state.last_matrix[OP_DRAWCHAR];
+
+    char same_font = i->state.last_string[OP_DRAWCHAR] && !strcmp(i->state.last_string[OP_DRAWCHAR], font_id);
+    char same_matrix = (l->m00 == matrix->m00) && (l->m01 == matrix->m01) && (l->m10 == matrix->m10) && (l->m11 == matrix->m11);
+    char same_color = !memcmp(color, &i->state.last_color[OP_DRAWCHAR], sizeof(gfxcolor_t));
+    U8 flags = 0;
+    if(same_font && same_matrix && same_color)
+       flags |= FLAG_SAME_AS_LAST;
+
+    writer_writeU8(&i->w, OP_DRAWCHAR|flags);
     writer_writeU32(&i->w, glyphnr);
-    dumpColor(&i->w, color);
-    dumpMatrix(&i->w, matrix);
+    if(!(flags&FLAG_SAME_AS_LAST)) {
+       writer_writeString(&i->w, font_id);
+       dumpColor(&i->w, color);
+       dumpMatrix(&i->w, matrix);
+    } else {
+       dumpXY(&i->w, matrix);
+    }
 }
 
 static void record_startpage(struct _gfxdevice*dev, int width, int height)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: STARTPAGE\n");
+    msg("<trace> record: %08x STARTPAGE\n", dev);
     writer_writeU8(&i->w, OP_STARTPAGE);
     writer_writeU16(&i->w, width);
     writer_writeU16(&i->w, height);
@@ -379,44 +444,81 @@ static void record_startpage(struct _gfxdevice*dev, int width, int height)
 static void record_endpage(struct _gfxdevice*dev)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: ENDPAGE\n");
+    msg("<trace> record: %08x ENDPAGE\n", dev);
     writer_writeU8(&i->w, OP_ENDPAGE);
 }
 
 static void record_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: DRAWLINK\n");
+    msg("<trace> record: %08x DRAWLINK\n", dev);
     writer_writeU8(&i->w, OP_DRAWLINK);
     dumpLine(&i->w, line);
     writer_writeString(&i->w, action);
 }
 
-static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, int length)
+static char* read_string(reader_t*r, state_t*state, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST) {
+       assert(state->last_string[id]);
+       return state->last_string[id];
+    }
+    char*s = reader_readString(r);
+    state->last_string[id] = strdup(s);
+    return s;
+}
+static gfxcolor_t read_color(reader_t*r, state_t*s, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST)
+       return s->last_color[id];
+    gfxcolor_t c = readColor(r);
+    s->last_color[id] = c;
+    return c;
+}
+static gfxmatrix_t read_matrix(reader_t*r, state_t*s, U8 id, U8 flags)
+{
+    assert(id>=0 && id<16);
+    if(flags&FLAG_SAME_AS_LAST) {
+       gfxmatrix_t m = s->last_matrix[id];
+       readXY(r, &m);
+       return m;
+    }
+    gfxmatrix_t m = readMatrix(r);
+    s->last_matrix[id] = m;
+    return m;
+}
+
+static void replay(struct _gfxdevice*dev, gfxdevice_t*out, reader_t*r)
 {
     internal_t*i = 0;
-    if(recorddev) {
-       i = (internal_t*)recorddev->internal;
+    if(dev) {
+       i = (internal_t*)dev->internal;
     }
 
-    reader_t r2;
-    reader_t*r = &r2;
-    reader_init_memreader(r, data, length);
+    state_t state;
+    memset(&state, 0, sizeof(state));
+
     gfxfontlist_t* fontlist = gfxfontlist_create();
 
-    while(r->pos < length) {
-       unsigned char op = reader_readU8(r);
+    while(1) {
+       unsigned char op;
+       if(r->read(r, &op, 1)!=1)
+           break;
+       unsigned char flags = op&0xf0;
+       op&=0x0f;
+
        switch(op) {
            case OP_END:
-               msg("<trace> replay: END");
-               return;
+               goto finish;
            case OP_SETPARAM: {
                msg("<trace> replay: SETPARAM");
                char*key;
                char*value;
                key = reader_readString(r);
                value = reader_readString(r);
-               device->setparameter(device, key, value);
+               out->setparameter(out, key, value);
                free(key);
                free(value);
                break;
@@ -425,11 +527,12 @@ static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, i
                msg("<trace> replay: STARTPAGE");
                U16 width = reader_readU16(r);
                U16 height = reader_readU16(r);
-               device->startpage(device, width, height);
+               out->startpage(out, width, height);
                break;
            }
            case OP_ENDPAGE: {
                msg("<trace> replay: ENDPAGE");
+               out->endpage(out);
                break;
            }
            case OP_FINISH: {
@@ -456,27 +559,27 @@ static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, i
                    case 2: jointtype = gfx_joinBevel; break;
                }
                gfxline_t* line = readLine(r);
-               device->stroke(device, line, width, &color, captype, jointtype,miterlimit);
+               out->stroke(out, line, width, &color, captype, jointtype,miterlimit);
                gfxline_free(line);
                break;
            }
            case OP_STARTCLIP: {
                msg("<trace> replay: STARTCLIP");
                gfxline_t* line = readLine(r);
-               device->startclip(device, line);
+               out->startclip(out, line);
                gfxline_free(line);
                break;
            }
            case OP_ENDCLIP: {
                msg("<trace> replay: ENDCLIP");
-               device->endclip(device);
+               out->endclip(out);
                break;
            }
            case OP_FILL: {
                msg("<trace> replay: FILL");
                gfxcolor_t color = readColor(r);
                gfxline_t* line = readLine(r);
-               device->fill(device, line, &color);
+               out->fill(out, line, &color);
                gfxline_free(line);
                break;
            }
@@ -486,9 +589,11 @@ static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, i
                gfxmatrix_t matrix = readMatrix(r);
                gfxline_t* line = readLine(r);
                gfxcxform_t* cxform = readCXForm(r);
-               device->fillbitmap(device, line, &img, &matrix, cxform);
+               out->fillbitmap(out, line, &img, &matrix, cxform);
+               gfxline_free(line);
                if(cxform)
                    free(cxform);
+               free(img.data);img.data=0;
                break;
            }
            case OP_FILLGRADIENT: {
@@ -504,65 +609,86 @@ static void replay(struct _gfxdevice*recorddev, gfxdevice_t*device, void*data, i
                gfxgradient_t*gradient = readGradient(r);
                gfxmatrix_t matrix = readMatrix(r);
                gfxline_t* line = readLine(r);
-               device->fillgradient(device, line, gradient, type, &matrix);
+               out->fillgradient(out, line, gradient, type, &matrix);
                break;
            }
            case OP_DRAWLINK: {
                msg("<trace> replay: DRAWLINK");
                gfxline_t* line = readLine(r);
                char* s = reader_readString(r);
-               device->drawlink(device,line,s);
+               out->drawlink(out,line,s);
                gfxline_free(line);
                free(s);
                break;
            }
            case OP_ADDFONT: {
-               msg("<trace> replay: ADDFONT");
+               msg("<trace> replay: ADDFONT out=%08x(%s)", out, out->name);
                gfxfont_t*font = readFont(r);
                fontlist = gfxfontlist_addfont(fontlist, font);
-               device->addfont(device, font);
+               out->addfont(out, font);
                break;
            }
            case OP_DRAWCHAR: {
-               char* id = reader_readString(r);
+               U32 glyph = reader_readU32(r);
+               gfxmatrix_t m = {1,0,0, 0,1,0};
+               char* id = read_string(r, &state, op, flags);
+               gfxcolor_t color = read_color(r, &state, op, flags);
+               gfxmatrix_t matrix = read_matrix(r, &state, op, flags);
+
                gfxfont_t*font = id?gfxfontlist_findfont(fontlist, id):0;
                if(i && !font) {
                    font = gfxfontlist_findfont(i->fontlist, id);
                }
-               U32 glyph = reader_readU32(r);
                msg("<trace> replay: DRAWCHAR font=%s glyph=%d", id, glyph);
-               gfxcolor_t color = readColor(r);
-               gfxmatrix_t matrix = readMatrix(r);
-               device->drawchar(device, font, glyph, &color, &matrix);
+               out->drawchar(out, font, glyph, &color, &matrix);
                free(id);
                break;
            }
        }
     }
+finish:
     r->dealloc(r);
-    gfxfontlist_free(fontlist, 1);
+    /* problem: if we just replayed into a device which stores the
+       font for later use (the record device itself is a nice example),
+       then we can't free it yet */
+    //gfxfontlist_free(fontlist, 1);
+    gfxfontlist_free(fontlist, 0);
 }
 void gfxresult_record_replay(gfxresult_t*result, gfxdevice_t*device)
 {
     internal_result_t*i = (internal_result_t*)result->internal;
-    replay(0, device, i->data, i->length);
+    
+    reader_t r;
+    if(i->use_tempfile) {
+       reader_init_filereader2(&r, i->filename);
+    } else {
+       reader_init_memreader(&r, i->data, i->length);
+    }
+
+    replay(0, device, &r);
 }
 
 static void record_result_write(gfxresult_t*r, int filedesc)
 {
     internal_result_t*i = (internal_result_t*)r->internal;
-    write(filedesc, i->data, i->length);
+    if(i->data) {
+       write(filedesc, i->data, i->length);
+    }
 }
 static int record_result_save(gfxresult_t*r, const char*filename)
 {
     internal_result_t*i = (internal_result_t*)r->internal;
-    FILE*fi = fopen(filename, "wb");
-    if(!fi) {
-       fprintf(stderr, "Couldn't open file %s for writing\n", filename);
-       return -1;
+    if(i->use_tempfile) {
+       move_file(i->filename, filename);
+    } else {
+       FILE*fi = fopen(filename, "wb");
+       if(!fi) {
+           fprintf(stderr, "Couldn't open file %s for writing\n", filename);
+           return -1;
+       }
+       fwrite(i->data, i->length, 1, fi);
+       fclose(fi);
     }
-    fwrite(i->data, i->length, 1, fi);
-    fclose(fi);
     return 0;
 }
 static void*record_result_get(gfxresult_t*r, const char*name)
@@ -581,6 +707,10 @@ static void record_result_destroy(gfxresult_t*r)
     if(i->data) {
        free(i->data);i->data = 0;
     }
+    if(i->filename) {
+       unlink(i->filename);
+       free(i->filename);
+    }
     free(r->internal);r->internal = 0;
     free(r);
 }
@@ -614,25 +744,42 @@ void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
 {
     internal_t*i = (internal_t*)dev->internal;
     if(out) {
-       int len=0;
-       void*data = writer_growmemwrite_memptr(&i->w, &len);
-       replay(dev, out, data, len);
+       if(!i->use_tempfile) {
+           int len=0;
+           void*data = writer_growmemwrite_memptr(&i->w, &len);
+           reader_t r;
+           reader_init_memreader(&r, data, len);
+           replay(dev, out, &r);
+           writer_growmemwrite_reset(&i->w);
+       } else {
+           msg("<fatal> Flushing not supported for file based record device");
+           exit(1);
+       }
     }
-    writer_growmemwrite_reset(&i->w);
 }
 
 static gfxresult_t* record_finish(struct _gfxdevice*dev)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: END\n");
+    msg("<trace> record: %08x END", dev);
+
+    if(i->cliplevel) {
+       msg("<error> Warning: unclosed cliplevels");
+    }
     
     writer_writeU8(&i->w, OP_END);
     
     gfxfontlist_free(i->fontlist, 0);
    
     internal_result_t*ir = (internal_result_t*)rfx_calloc(sizeof(gfxresult_t));
-    ir->data = writer_growmemwrite_getmem(&i->w);
-    ir->length = i->w.pos;
+   
+    ir->use_tempfile = i->use_tempfile;
+    if(i->use_tempfile) {
+       ir->filename = i->filename;
+    } else {
+       ir->data = writer_growmemwrite_getmem(&i->w);
+       ir->length = i->w.pos;
+    }
     i->w.finish(&i->w);
 
     gfxresult_t*result= (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
@@ -645,7 +792,8 @@ static gfxresult_t* record_finish(struct _gfxdevice*dev)
     
     return result;
 }
-void gfxdevice_record_init(gfxdevice_t*dev)
+
+void gfxdevice_record_init(gfxdevice_t*dev, char use_tempfile)
 {
     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
     memset(dev, 0, sizeof(gfxdevice_t));
@@ -653,9 +801,17 @@ void gfxdevice_record_init(gfxdevice_t*dev)
     dev->name = "record";
 
     dev->internal = i;
-    
-    writer_init_growingmemwriter(&i->w, 1048576);
+  
+    i->use_tempfile = use_tempfile;
+    if(!use_tempfile) {
+       writer_init_growingmemwriter(&i->w, 1048576);
+    } else {
+       char buffer[128];
+       i->filename = strdup(mktempname(buffer, "gfx"));
+       writer_init_filewriter2(&i->w, i->filename);
+    }
     i->fontlist = gfxfontlist_create();
+    i->cliplevel = 0;
 
     dev->setparameter = record_setparameter;
     dev->startpage = record_startpage;