fixed record device handling of font->{ascent,descent}
[swftools.git] / lib / devices / record.c
index 9af6530..4cc75dd 100644 (file)
@@ -40,6 +40,7 @@
 typedef struct _internal {
     gfxfontlist_t* fontlist;
     writer_t w;
+    int cliplevel;
 } internal_t;
 
 typedef struct _internal_result {
@@ -240,6 +241,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);
@@ -261,6 +264,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;
@@ -299,6 +304,7 @@ static void record_startclip(struct _gfxdevice*dev, gfxline_t*line)
     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)
@@ -306,6 +312,10 @@ static void record_endclip(struct _gfxdevice*dev)
     internal_t*i = (internal_t*)dev->internal;
     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)
@@ -342,10 +352,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: %08x ADDFONT\n", dev);
-    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,7 +365,6 @@ 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: %08x DRAWCHAR %d\n", glyphnr, dev);
@@ -408,8 +419,7 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, void*data, int length
        unsigned char op = reader_readU8(r);
        switch(op) {
            case OP_END:
-               msg("<trace> replay: END");
-               return;
+               goto finish;
            case OP_SETPARAM: {
                msg("<trace> replay: SETPARAM");
                char*key;
@@ -487,6 +497,7 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, void*data, int length
                gfxline_t* line = readLine(r);
                gfxcxform_t* cxform = readCXForm(r);
                out->fillbitmap(out, line, &img, &matrix, cxform);
+               gfxline_free(line);
                if(cxform)
                    free(cxform);
                free(img.data);img.data=0;
@@ -540,8 +551,13 @@ static void replay(struct _gfxdevice*dev, gfxdevice_t*out, void*data, int length
            }
        }
     }
+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)
 {
@@ -625,7 +641,11 @@ void gfxdevice_record_flush(gfxdevice_t*dev, gfxdevice_t*out)
 static gfxresult_t* record_finish(struct _gfxdevice*dev)
 {
     internal_t*i = (internal_t*)dev->internal;
-    msg("<trace> record: %08x END\n", dev);
+    msg("<trace> record: %08x END", dev);
+
+    if(i->cliplevel) {
+       msg("<error> Warning: unclosed cliplevels");
+    }
     
     writer_writeU8(&i->w, OP_END);
     
@@ -657,6 +677,7 @@ void gfxdevice_record_init(gfxdevice_t*dev)
     
     writer_init_growingmemwriter(&i->w, 1048576);
     i->fontlist = gfxfontlist_create();
+    i->cliplevel = 0;
 
     dev->setparameter = record_setparameter;
     dev->startpage = record_startpage;