seed random from ruby interface
[swftools.git] / lib / ruby / gfx.c
index a963bc1..7c48a09 100644 (file)
@@ -1,13 +1,19 @@
 #include <ruby.h>
+#include "../../config.h"
 #include "../gfxdevice.h"
 #include "../gfxsource.h"
 #include "../gfxtools.h"
+#include "../gfximage.h"
+#include "../gfxfont.h"
+#include "../gfxfilter.h"
 #include "../devices/pdf.h"
 #include "../readers/swf.h"
 #include "../readers/image.h"
 #include "../pdf/pdf.h"
 #include "../mem.h"
 #include "../types.h"
+#include "../log.h"
+#include "../args.h"
 
 #define RUBY_GFX_VERSION  "0.9.0"
 
@@ -28,19 +34,21 @@ typedef struct page_internal {
 } page_internal_t;
 
 typedef struct image_internal {
+    doc_internal_t*doc;
     gfximage_t*image;
 } image_internal_t;
 
-typedef struct glyph_internal {
-    gfxfont_t*font;
-    int nr;
-} glyph_internal_t;
-
 typedef struct font_internal {
+    VALUE self;
     VALUE glyph_array;
     gfxfont_t*font;
 } font_internal_t;
 
+typedef struct glyph_internal {
+    font_internal_t*font;
+    int nr;
+} glyph_internal_t;
+
 static gfxsource_t* pdfdriver = 0;
 static gfxsource_t* imagedriver = 0;
 static gfxsource_t* swfdriver = 0;
@@ -60,6 +68,9 @@ static VALUE doc_initialize(VALUE cls, VALUE _filename)
     const char*filename = StringValuePtr(_filename);
     doc->fontlist = gfxfontlist_create();
     doc->doc = pdfdriver->open(pdfdriver, filename);
+    if(!doc->doc) {
+       rb_raise(rb_eIOError, "couldn't open %s", filename);
+    }
     return cls;
 }
 
@@ -81,7 +92,7 @@ static VALUE doc_get_page(VALUE cls, VALUE _nr)
     page->doc = doc;
     if(!page->page) {
        rb_raise(rb_eArgError, "No page %d in document", nr);
-       return;
+       return Qnil;
     }
     return v;
 }
@@ -113,7 +124,10 @@ static void doc_mark(doc_internal_t*doc)
 static void doc_free(doc_internal_t*doc)
 {
     gfxfontlist_free(doc->fontlist, 0);
-    doc->doc->destroy(doc->doc);
+    if(doc->doc) {
+       doc->doc->destroy(doc->doc);
+    }
+    doc->doc = 0;
     free(doc);
 }
 
@@ -177,10 +191,14 @@ static void image_free(image_internal_t*image)
 {
     free(image);
 }
+static void image_mark(image_internal_t*image)
+{
+    rb_gc_mark(image->doc->self);
+}
 static VALUE image_allocate(VALUE cls)
 {
     image_internal_t*image = 0;
-    VALUE v = Data_Make_Struct(cls, image_internal_t, 0, image_free, image);
+    VALUE v = Data_Make_Struct(cls, image_internal_t, image_mark, image_free, image);
     memset(image, 0, sizeof(image_internal_t));
     return v;
 }
@@ -194,25 +212,57 @@ static VALUE image_height(VALUE cls)
     Get_Image(image,cls)
     return INT2FIX(image->image->height);
 }
+static VALUE image_rescale(VALUE cls, VALUE _width, VALUE _height)
+{
+    Get_Image(image,cls)
+    Check_Type(_width, T_FIXNUM);
+    Check_Type(_height, T_FIXNUM);
+    int width = FIX2INT(_width);
+    int height = FIX2INT(_height);
+    volatile VALUE v_image2 = image_allocate(Bitmap);
+    Get_Image(image2,v_image2)
+    image2->doc = image->doc;
+    image2->image = gfximage_rescale(image->image, width, height);
+    if(!image2->image) {
+       rb_raise(rb_eArgError, "Can't rescale to size %dx%d", width, height);
+    }
+    return v_image2;
+}
+static VALUE image_has_alpha(VALUE cls)
+{
+    Get_Image(image,cls)
+    int size = image->image->width * image->image->height;
+    gfxcolor_t*data = image->image->data;
+    int t;
+    for(t=0;t<size;t++) {
+       if(data[t].a!=255) 
+           return Qtrue;
+    }
+    return Qfalse;
+}
 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
 {
     Get_Image(image,cls)
     Check_Type(_filename, T_STRING);
     Check_Type(quality, T_FIXNUM);
     const char*filename = StringValuePtr(_filename);
-    int l = strlen(filename);
-    char jpeg = 0;
-    if(l>=4 && !strcmp(&filename[l-4], ".jpg")) jpeg = 1;
-    if(l>=5 && !strcmp(&filename[l-5], ".jpeg")) jpeg = 1;
-    if(l>=4 && !strcmp(&filename[l-4], ".JPG")) jpeg = 1;
-    jpeg_save(image->image->data, image->image->width, image->image->height, FIX2INT(quality), filename);
+    gfximage_save_jpeg(image->image, filename, FIX2INT(quality));
+    return cls;
+}
+static VALUE image_save_png(VALUE cls, VALUE _filename)
+{
+    Get_Image(image,cls)
+    Check_Type(_filename, T_STRING);
+    const char*filename = StringValuePtr(_filename);
+    gfximage_save_png(image->image, filename);
     return cls;
 }
-VALUE convert_image(gfximage_t*_image)
+VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
 {
     VALUE v = image_allocate(Bitmap);
     Get_Image(image,v)
     image->image = _image;
+    image->doc = doc;
     return v;
 }
 void invalidate_image(VALUE v)
@@ -232,10 +282,15 @@ static void glyph_free(glyph_internal_t*glyph)
     free(glyph);
 }
 
+static void glyph_mark(glyph_internal_t*glyph)
+{
+    rb_gc_mark(glyph->font->self);
+}
+
 static VALUE glyph_allocate(VALUE cls)
 {
     glyph_internal_t*glyph = 0;
-    VALUE v = Data_Make_Struct(cls, glyph_internal_t, 0, glyph_free, glyph);
+    VALUE v = Data_Make_Struct(cls, glyph_internal_t, glyph_mark, glyph_free, glyph);
     memset(glyph, 0, sizeof(glyph_internal_t));
     return v;
 }
@@ -243,19 +298,29 @@ static VALUE glyph_allocate(VALUE cls)
 static VALUE glyph_polygon(VALUE cls)
 {
     Get_Glyph(glyph,cls);
-    return convert_line(glyph->font->glyphs[glyph->nr].line);
+    return convert_line(glyph->font->font->glyphs[glyph->nr].line);
 }
 
 static VALUE glyph_advance(VALUE cls)
 {
     Get_Glyph(glyph,cls);
-    return rb_float_new(glyph->font->glyphs[glyph->nr].advance);
+    return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
+}
+
+static VALUE glyph_bbox(VALUE cls)
+{
+    Get_Glyph(glyph,cls);
+    gfxbbox_t bbox = gfxline_getbbox(glyph->font->font->glyphs[glyph->nr].line);
+    return rb_ary_new3(4, rb_float_new(bbox.xmin), 
+                         rb_float_new(bbox.ymin), 
+                         rb_float_new(bbox.xmax), 
+                         rb_float_new(bbox.ymax));
 }
 
 static VALUE glyph_unicode(VALUE cls)
 {
     Get_Glyph(glyph,cls);
-    return INT2FIX(glyph->font->glyphs[glyph->nr].unicode);
+    return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
 }
 
 // ------------------------ font --------------------------------------------
@@ -277,9 +342,22 @@ static VALUE font_allocate(VALUE cls)
     font_internal_t*font = 0;
     VALUE v = Data_Make_Struct(cls, font_internal_t, font_mark, font_free, font);
     memset(font, 0, sizeof(font_internal_t));
+    font->self = v;
     return v;
 }
 
+static VALUE font_ascent(VALUE cls)
+{
+    Get_Font(font,cls);
+    return rb_float_new(font->font->ascent);
+}
+
+static VALUE font_descent(VALUE cls)
+{
+    Get_Font(font,cls);
+    return rb_float_new(font->font->descent);
+}
+
 static VALUE font_name(VALUE cls)
 {
     Get_Font(font,cls);
@@ -292,6 +370,41 @@ static VALUE font_glyphs(VALUE cls)
     return font->glyph_array;
 }
 
+static VALUE font_save_ttf(VALUE cls, VALUE _filename)
+{
+    Get_Font(font,cls);
+    Check_Type(_filename, T_STRING);
+    const char*filename = StringValuePtr(_filename);
+    gfxfont_save(font->font, filename);
+    return Qnil;
+}
+
+static VALUE font_save_eot(VALUE cls, VALUE _filename)
+{
+    Get_Font(font,cls);
+    Check_Type(_filename, T_STRING);
+    const char*filename = StringValuePtr(_filename);
+    gfxfont_save_eot(font->font, filename);
+    return Qnil;
+}
+
+static VALUE font_kerning(VALUE cls)
+{
+    Get_Font(font,cls);
+    gfxkerning_t*kerning = font->font->kerning;
+    int kerning_size = font->font->kerning_size;
+    volatile VALUE a = rb_ary_new2(kerning_size);
+    int t;
+    for(t=0;t<kerning_size;t++) {
+       volatile VALUE tuple = rb_ary_new2(3);
+       rb_ary_store(tuple, 0, INT2FIX(kerning[t].c1));
+       rb_ary_store(tuple, 1, INT2FIX(kerning[t].c2));
+       rb_ary_store(tuple, 2, INT2FIX(kerning[t].advance));
+       rb_ary_store(a, t, tuple);
+    }
+    return a;
+}
+
 // ------------------------ gfx device --------------------------------------
 
 typedef struct device_internal {
@@ -323,6 +436,9 @@ static ID id_line = 0;
 static ID id_spline = 0;
 static ID id_radial = 0;
 static ID id_linear = 0;
+static ID id_remove_font_transforms = 0;
+static ID id_maketransparent = 0;
+static ID id_vectors_to_glyphs = 0;
 
 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
 
@@ -334,13 +450,12 @@ VALUE convert_line(gfxline_t*line)
     gfxline_t*l = line;
     while(l) {l=l->next;len++;}
 
-    VALUE array = rb_ary_new2(len);
-    rb_gc_register_address(&array);
+    volatile VALUE array = rb_ary_new2(len);
 
     int pos = 0;
     l = line;
     while(l) {
-       VALUE e;
+       volatile VALUE e;
        if(l->type == gfx_moveTo) {
            e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
            rb_ary_store(array, pos, e);
@@ -362,7 +477,6 @@ VALUE convert_line(gfxline_t*line)
        pos++;
        l=l->next;
     }
-    rb_gc_unregister_address(&array);
     return array;
 }
 VALUE convert_color(gfxcolor_t*color)
@@ -371,9 +485,8 @@ VALUE convert_color(gfxcolor_t*color)
 }
 VALUE convert_matrix(gfxmatrix_t*matrix)
 {
-    VALUE array = rb_ary_new2(3);
-    rb_gc_register_address(&array);
-    VALUE a = rb_ary_new2(2);
+    volatile VALUE array = rb_ary_new2(3);
+    volatile VALUE a = rb_ary_new2(2);
     rb_ary_store(array, 0, a);
     rb_ary_store(a, 0, rb_float_new(matrix->m00));
     rb_ary_store(a, 1, rb_float_new(matrix->m01));
@@ -385,38 +498,46 @@ VALUE convert_matrix(gfxmatrix_t*matrix)
     rb_ary_store(array, 2, a);
     rb_ary_store(a, 0, rb_float_new(matrix->tx));
     rb_ary_store(a, 1, rb_float_new(matrix->ty));
-    rb_gc_unregister_address(&array);
     return array;
 }
-VALUE convert_font(device_internal_t*i, gfxfont_t*font)
+static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
+{
+    return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
+}
+static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
 {
-    VALUE v = (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
-    if(v) return v;
+    i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
+}
+static VALUE convert_font(gfxfont_t*font)
+{
+    volatile VALUE v2 = font_allocate(Font);
+    Get_Font(f, v2);
+    f->font = font;
+    f->glyph_array = rb_ary_new2(font->num_glyphs);
 
-    VALUE*a = (VALUE*)malloc(sizeof(VALUE)*font->num_glyphs);
     int t;
     for(t=0;t<font->num_glyphs;t++) {
-       a[t] = glyph_allocate(Glyph);
-       Get_Glyph(g, a[t]);
-       g->font = font;
+       volatile VALUE a = glyph_allocate(Glyph);
+       rb_ary_store(f->glyph_array, t, a);
+       Get_Glyph(g, a);
+       g->font = f;
        g->nr = t;
     }
-    VALUE v2 = font_allocate(Font);
-    Get_Font(f, v2);
-
-    f->font = font;
-    f->glyph_array = rb_ary_new4(font->num_glyphs, a);
-
-    i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v2);
     return v2;
 }
+static VALUE convert_gradient(gfxgradient_t*gradient)
+{
+    return Qnil; //TODO
+}
 #define HEAD \
     device_internal_t*i = (device_internal_t*)dev->internal; \
     VALUE v = i->v;
 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
 {
     HEAD
-    VALUE ret = forward(v,id_setparameter,2,rb_tainted_str_new2(key),rb_tainted_str_new2(value));
+    volatile VALUE v_key = rb_tainted_str_new2(key);
+    volatile VALUE v_value = rb_tainted_str_new2(value);
+    VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
     return 0;
 }
 void rb_startpage(gfxdevice_t*dev, int width, int height)
@@ -427,7 +548,8 @@ void rb_startpage(gfxdevice_t*dev, int width, int height)
 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
 {
     HEAD
-    VALUE ret = forward(v,id_startclip,1,convert_line(line));
+    volatile VALUE v_line = convert_line(line);
+    VALUE ret = forward(v,id_startclip,1,v_line);
 }
 void rb_endclip(gfxdevice_t*dev)
 {
@@ -448,49 +570,27 @@ void rb_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*col
     else if(joint_style == gfx_joinMiter) joint = id_miter;
     else if(joint_style == gfx_joinBevel) joint = id_bevel;
 
-    VALUE v_line = convert_line(line);
-    rb_gc_register_address(&v_line);
-    VALUE v_width = rb_float_new(width);
-    rb_gc_register_address(&v_width);
-    VALUE v_color = convert_color(color);
-    rb_gc_register_address(&v_color);
-    VALUE v_miter = rb_float_new(miterLimit);
-    rb_gc_register_address(&v_miter);
-
+    volatile VALUE v_line = convert_line(line);
+    volatile VALUE v_width = rb_float_new(width);
+    volatile VALUE v_color = convert_color(color);
+    volatile VALUE v_miter = rb_float_new(miterLimit);
     forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
-
-    rb_gc_unregister_address(&v_miter);
-    rb_gc_unregister_address(&v_color);
-    rb_gc_unregister_address(&v_width);
-    rb_gc_unregister_address(&v_line);
 }
 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
 {
     HEAD
     
-    VALUE v_line = convert_line(line);
-    rb_gc_register_address(&v_line);
-    VALUE v_color = convert_color(color);
-    rb_gc_register_address(&v_color);
-
+    volatile VALUE v_line = convert_line(line);
+    volatile VALUE v_color = convert_color(color);
     forward(v, id_fill, 2, v_line, v_color);
-    
-    rb_gc_unregister_address(&v_color);
-    rb_gc_unregister_address(&v_line);
 }
 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
 {
     HEAD
-    VALUE v_image = convert_image(img);
-    rb_gc_register_address(&v_image);
-    VALUE v_line = convert_line(line);
-    rb_gc_register_address(&v_line);
-    VALUE v_matrix = convert_matrix(matrix);
-    rb_gc_register_address(&v_matrix);
+    volatile VALUE v_image = convert_image(i->doc, img);
+    volatile VALUE v_line = convert_line(line);
+    volatile VALUE v_matrix = convert_matrix(matrix);
     forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
-    rb_gc_unregister_address(&v_matrix);
-    rb_gc_unregister_address(&v_line);
-    rb_gc_unregister_address(&v_image);
     invalidate_image(v_image);
 }
 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
@@ -498,87 +598,190 @@ void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gf
     HEAD
     ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
     
-    VALUE v_line = convert_line(line);
-    rb_gc_register_address(&v_line);
-    VALUE v_matrix = convert_matrix(matrix);
-    rb_gc_register_address(&v_matrix);
-    VALUE v_gradient = convert_gradient(gradient);
-    rb_gc_register_address(&v_gradient);
-    forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), convert_matrix(matrix));
-    rb_gc_unregister_address(&v_gradient);
-    rb_gc_unregister_address(&v_matrix);
-    rb_gc_unregister_address(&v_line);
+    volatile VALUE v_line = convert_line(line);
+    volatile VALUE v_matrix = convert_matrix(matrix);
+    volatile VALUE v_gradient = convert_gradient(gradient);
+    forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
 }
 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
 {
     HEAD
-    forward(v, id_addfont, 1, convert_font(i, font));
+
+    volatile VALUE f = font_is_cached(i, font);
+    if(!f) {f=convert_font(font);cache_font(i,font,f);}
+
+    forward(v, id_addfont, 1, f);
 }
 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
 {
     HEAD
-    forward(v, id_drawchar, 4, convert_font(i, font), INT2FIX(glyphnr), convert_color(color), convert_matrix(matrix));
+    volatile VALUE f = font_is_cached(i, font);
+    if(!f) {f=convert_font(font);cache_font(i,font,f);}
+
+    volatile VALUE v_color = convert_color(color);
+    volatile VALUE v_matrix = convert_matrix(matrix);
+    forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
 }
 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
 {
     HEAD
-    forward(v, id_drawlink, convert_line(line), rb_tainted_str_new2(action));
+    volatile VALUE v_line = convert_line(line);
+    volatile VALUE v_action = rb_tainted_str_new2(action);
+
+    forward(v, id_drawlink, 2, v_line, v_action);
 }
 void rb_endpage(gfxdevice_t*dev)
 {
     HEAD
     forward(v, id_endpage, 0);
 }
+void gfxresult_rb_destroy(gfxresult_t*r)
+{
+    free(r);
+}
 gfxresult_t* rb_finish(gfxdevice_t*dev)
 {
     HEAD
-    VALUE ret = forward(v, id_endpage, 0);
+    VALUE ret = forward(v, id_finish, 0);
     gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
+    r->destroy = gfxresult_rb_destroy;
     r->internal = (void*)(ptroff_t)ret;
     return r;
 }
 
+#define make_device(dev, idoc, device) \
+    gfxdevice_t dev; \
+    device_internal_t i; \
+    i.v = device; \
+    i.doc = idoc; \
+    dev.internal = &i; \
+    dev.setparameter = rb_setparameter; \
+    dev.startpage = rb_startpage; \
+    dev.startclip = rb_startclip; \
+    dev.endclip = rb_endclip; \
+    dev.stroke = rb_stroke; \
+    dev.fill = rb_fill; \
+    dev.fillbitmap = rb_fillbitmap; \
+    dev.fillgradient = rb_fillgradient; \
+    dev.addfont = rb_addfont; \
+    dev.drawchar = rb_drawchar; \
+    dev.drawlink = rb_drawlink; \
+    dev.endpage = rb_endpage; \
+    dev.finish = rb_finish;
+
 static VALUE page_render(VALUE cls, VALUE device)
 {
     Check_Type(device, T_OBJECT);
     Get_Page(page,cls)
 
-    gfxdevice_t dev;
-    device_internal_t i;
-    i.v = device;
-    i.doc = page->doc;
-
-    dev.internal = &i;
-    dev.setparameter = rb_setparameter;
-    dev.startpage = rb_startpage;
-    dev.startclip = rb_startclip;
-    dev.endclip = rb_endclip;
-    dev.stroke = rb_stroke;
-    dev.fill = rb_fill;
-    dev.fillbitmap = rb_fillbitmap;
-    dev.fillgradient = rb_fillgradient;
-    dev.addfont = rb_addfont;
-    dev.drawchar = rb_drawchar;
-    dev.drawlink = rb_drawlink;
-    dev.endpage = rb_endpage;
-    dev.finish = rb_finish;
+    make_device(dev, page->doc, device);
 
+    dev.startpage(&dev, page->page->width, page->page->height);
     page->page->render(page->page, &dev);
+    dev.endpage(&dev);
+
+    return cls;
+}
+
+static VALUE doc_render(VALUE cls, VALUE device, VALUE _range, VALUE filters)
+{
+    const char*range = 0;
+    if(!NIL_P(_range)) {
+       Check_Type(_range, T_STRING);
+       range = StringValuePtr(_range);
+    }
+    Get_Doc(doc,cls);
+
+    make_device(_dev, doc, device);
+    gfxdevice_t*dev = &_dev;
+
+    if(!NIL_P(filters)) {
+       if(TYPE(filters) != T_ARRAY)
+           rb_raise(rb_eArgError, "third argument of doc->render must be an array of symbols");
+
+       int len = RARRAY(filters)->len;
+       int t=0;
+       while(t<len) {
+           VALUE filter = RARRAY(filters)->ptr[t++];
+           Check_Type(filter, T_SYMBOL);
+           ID id = SYM2ID(filter);
+#           define PARAM(x) VALUE x;if(t==len) rb_raise(rb_eArgError, "End of array while parsing arguments for filter %s", rb_id2name(id)); \
+                           else x = RARRAY(filters)->ptr[t++];
+           if(id == id_remove_font_transforms) {
+               wrap_filter2(dev, remove_font_transforms);
+           } else if(id == id_vectors_to_glyphs) {
+               wrap_filter2(dev, vectors_to_glyphs);
+           } else if(id == id_maketransparent) {
+               PARAM(alpha);
+               wrap_filter(dev, maketransparent, FIX2INT(alpha));
+           } else {
+               rb_raise(rb_eArgError, "unknown filter %s", rb_id2name(id));
+           }
+       }
+    }
+
+    int pagenr;
+    for(pagenr=1;pagenr<=doc->doc->num_pages;pagenr++) {
+       if(is_in_range(pagenr, (char*)range)) {
+           gfxpage_t*page = doc->doc->getpage(doc->doc, pagenr);
+           dev->startpage(dev, page->width, page->height);
+           page->render(page, dev);
+           dev->endpage(dev);
+           page->destroy(page);
+       }
+    }
+   
 
+    gfxresult_t*r = dev->finish(dev);
+    r->destroy(r);
+
+    return Qnil;
+}
+
+static VALUE doc_prepare(VALUE cls, VALUE device)
+{
+    Get_Doc(doc,cls);
+    make_device(dev, doc, device);
+    doc->doc->prepare(doc->doc, &dev);
     return cls;
 }
 
 
+// ---------------------- global functions ----------------------------------
+
+VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
+{
+    Check_Type(_key, T_STRING);
+    Check_Type(_value, T_STRING);
+    const char*key = StringValuePtr(_key);
+    const char*value = StringValuePtr(_value);
+    pdfdriver->setparameter(pdfdriver, key, value);
+    swfdriver->setparameter(swfdriver, key, value);
+    imagedriver->setparameter(imagedriver, key, value);
+    return GFX;
+}
+
 // --------------------------------------------------------------------------
 
 void Init_gfx()
 {
     initLog(0,0,0,0,0,2);
+#ifdef HAVE_SRAND48
+    srand48(time(0));
+#else
+#ifdef HAVE_SRAND
+    srand(time(0));
+#endif
+#endif
+
     pdfdriver = gfxsource_pdf_create();
     swfdriver = gfxsource_swf_create();
     imagedriver = gfxsource_image_create();
 
     GFX = rb_define_module("GFX");
+    rb_define_const(GFX, "VERSION", INT2FIX(20100309));
+    
+    rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
     
     DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
     rb_define_method(DocumentPage, "width", page_width, 0);
@@ -590,18 +793,32 @@ void Init_gfx()
     rb_define_method(Document, "initialize", doc_initialize, 1);
     rb_define_method(Document, "page", doc_get_page, 1);
     rb_define_method(Document, "each_page", doc_each_page, 0);
+    rb_define_method(Document, "prepare", doc_prepare, 1);
+    rb_define_method(Document, "render", doc_render, 3);
     
     Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
     rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
+    rb_define_method(Bitmap, "save_png", image_save_png, 1);
+    rb_define_method(Bitmap, "width", image_width, 0);
+    rb_define_method(Bitmap, "height", image_height, 0);
+    rb_define_method(Bitmap, "rescale", image_rescale, 2);
+    rb_define_method(Bitmap, "has_alpha", image_has_alpha, 0);
     
     Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
     rb_define_method(Glyph, "polygon", glyph_polygon, 0);
     rb_define_method(Glyph, "unicode", glyph_unicode, 0);
     rb_define_method(Glyph, "advance", glyph_advance, 0);
+    rb_define_method(Glyph, "bbox", glyph_bbox, 0);
     
     Font = rb_define_class_under(GFX, "Font", rb_cObject);
     rb_define_method(Font, "name", font_name, 0);
+    rb_define_method(Font, "ascent", font_ascent, 0);
+    rb_define_method(Font, "descent", font_descent, 0);
     rb_define_method(Font, "glyphs", font_glyphs, 0);
+    rb_define_method(Font, "kerning", font_kerning, 0);
+    rb_define_method(Font, "get_kerning_table", font_kerning, 0);
+    rb_define_method(Font, "save_ttf", font_save_ttf, 1);
+    rb_define_method(Font, "save_eot", font_save_eot, 1);
     
     Device = rb_define_class_under(GFX, "Device", rb_cObject);
     rb_define_method(Device, "startpage", noop, -1);
@@ -615,7 +832,6 @@ void Init_gfx()
     rb_define_method(Device, "addfont", noop, -1);
     rb_define_method(Device, "drawchar", noop, -1);
     rb_define_method(Device, "drawlink", noop, -1);
-    rb_define_method(Device, "endpage", noop, -1);
 
     PDFClass = rb_define_class_under(GFX, "PDF", Document);
     rb_define_alloc_func(PDFClass, pdf_allocate);
@@ -625,7 +841,7 @@ void Init_gfx()
     
     ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
     rb_define_alloc_func(ImageClass, imgdrv_allocate);
-
+    
     id_setparameter = rb_intern("setparameter");
     id_startpage = rb_intern("startpage") ;
     id_startclip = rb_intern("startclip") ;
@@ -650,5 +866,8 @@ void Init_gfx()
     id_spline = rb_intern("spline");
     id_radial = rb_intern("radial");
     id_linear = rb_intern("linear");
+    id_remove_font_transforms = rb_intern("remove_font_transforms");
+    id_maketransparent = rb_intern("maketransparent");
+    id_vectors_to_glyphs = rb_intern("vectors_to_glyphs");
 }