added prepare() to ruby gfx interface
[swftools.git] / lib / ruby / gfx.c
index 6a47da7..edca2e3 100644 (file)
@@ -2,6 +2,7 @@
 #include "../gfxdevice.h"
 #include "../gfxsource.h"
 #include "../gfxtools.h"
+#include "../gfximage.h"
 #include "../devices/pdf.h"
 #include "../readers/swf.h"
 #include "../readers/image.h"
@@ -62,6 +63,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;
 }
 
@@ -200,18 +204,37 @@ 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_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(doc_internal_t*doc,gfximage_t*_image)
@@ -264,6 +287,16 @@ static VALUE glyph_advance(VALUE cls)
     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);
@@ -293,6 +326,18 @@ static VALUE font_allocate(VALUE cls)
     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);
@@ -305,6 +350,23 @@ static VALUE font_glyphs(VALUE cls)
     return font->glyph_array;
 }
 
+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 {
@@ -568,6 +630,35 @@ static VALUE page_render(VALUE cls, VALUE device)
     return cls;
 }
 
+static VALUE doc_prepare(VALUE cls, VALUE device)
+{
+    Get_Doc(doc,cls);
+
+    gfxdevice_t dev;
+    device_internal_t i;
+    i.v = device;
+    i.doc = 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;
+
+    doc->doc->prepare(doc->doc, &dev);
+    return cls;
+}
+
+
 // ---------------------- global functions ----------------------------------
 
 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
@@ -605,18 +696,28 @@ 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);
     
     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);
     
     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);
     
     Device = rb_define_class_under(GFX, "Device", rb_cObject);
     rb_define_method(Device, "startpage", noop, -1);