added has_alpha function to ruby bitmap object
[swftools.git] / lib / ruby / gfx.c
1 #include <ruby.h>
2 #include "../gfxdevice.h"
3 #include "../gfxsource.h"
4 #include "../gfxtools.h"
5 #include "../gfximage.h"
6 #include "../devices/pdf.h"
7 #include "../readers/swf.h"
8 #include "../readers/image.h"
9 #include "../pdf/pdf.h"
10 #include "../mem.h"
11 #include "../types.h"
12
13 #define RUBY_GFX_VERSION  "0.9.0"
14
15 static VALUE GFX;
16 static VALUE Font, Glyph, Bitmap, Document, DocumentPage, PDFClass, SWFClass, ImageClass, Device;
17 static ID id_doc;
18
19 typedef struct doc_internal {
20     VALUE self;
21     gfxsource_t*driver; // filled by alloc
22     gfxdocument_t*doc;
23     gfxfontlist_t*fontlist;
24 } doc_internal_t;
25
26 typedef struct page_internal {
27     doc_internal_t*doc;
28     gfxpage_t*page;
29 } page_internal_t;
30
31 typedef struct image_internal {
32     doc_internal_t*doc;
33     gfximage_t*image;
34 } image_internal_t;
35
36 typedef struct font_internal {
37     VALUE self;
38     VALUE glyph_array;
39     gfxfont_t*font;
40 } font_internal_t;
41
42 typedef struct glyph_internal {
43     font_internal_t*font;
44     int nr;
45 } glyph_internal_t;
46
47 static gfxsource_t* pdfdriver = 0;
48 static gfxsource_t* imagedriver = 0;
49 static gfxsource_t* swfdriver = 0;
50
51 #define Get_Doc(doc,cls) doc_internal_t*doc=0;Data_Get_Struct(cls, doc_internal_t, doc);
52 #define Get_Page(page,cls) page_internal_t*page=0;Data_Get_Struct(cls, page_internal_t, page);
53
54 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver);
55 static VALUE page_allocate(VALUE cls);
56
57 // ------------------------ documents ---------------------------------------
58
59 static VALUE doc_initialize(VALUE cls, VALUE _filename)
60 {
61     Check_Type(_filename, T_STRING);
62     Get_Doc(doc,cls);
63     const char*filename = StringValuePtr(_filename);
64     doc->fontlist = gfxfontlist_create();
65     doc->doc = pdfdriver->open(pdfdriver, filename);
66     if(!doc->doc) {
67         rb_raise(rb_eIOError, "couldn't open %s", filename);
68     }
69     return cls;
70 }
71
72 static VALUE doc_num_pages(VALUE cls)
73 {
74     Get_Doc(doc,cls)
75     return INT2FIX(doc->doc->num_pages);
76 }
77
78 static VALUE doc_get_page(VALUE cls, VALUE _nr)
79 {
80     Check_Type(_nr, T_FIXNUM);
81     int nr = FIX2INT(_nr);
82     Get_Doc(doc,cls);
83
84     VALUE v = page_allocate(DocumentPage);
85     Get_Page(page,v)
86     page->page = doc->doc->getpage(doc->doc, nr);
87     page->doc = doc;
88     if(!page->page) {
89         rb_raise(rb_eArgError, "No page %d in document", nr);
90         return;
91     }
92     return v;
93 }
94
95 static VALUE doc_each_page(VALUE cls)
96 {
97     Get_Doc(doc,cls);
98     int t;
99     for(t=1;t<=doc->doc->num_pages;t++) {
100         VALUE v = page_allocate(DocumentPage);
101         Get_Page(page,v)
102         page->page = doc->doc->getpage(doc->doc, t);
103         page->doc = doc;
104         rb_yield(v);
105     }
106     return cls;
107 }
108
109 static void doc_mark(doc_internal_t*doc)
110 {
111     gfxfontlist_t*l = doc->fontlist;
112     while(l) {
113         if(l->user) 
114             rb_gc_mark((VALUE)l->user);
115         l = l->next;
116     }
117 }
118
119 static void doc_free(doc_internal_t*doc)
120 {
121     gfxfontlist_free(doc->fontlist, 0);
122     doc->doc->destroy(doc->doc);
123     free(doc);
124 }
125
126 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver)
127 {
128     doc_internal_t*doc = 0;
129     VALUE v = Data_Make_Struct(cls, doc_internal_t, doc_mark, doc_free, doc);
130     doc->self = v;
131     memset(doc, 0, sizeof(doc_internal_t));
132     doc->driver = driver;
133     return v;
134 }
135
136 static VALUE pdf_allocate(VALUE cls) {return doc_allocate(cls, pdfdriver);}
137 static VALUE swf_allocate(VALUE cls) {return doc_allocate(cls, swfdriver);}
138 static VALUE imgdrv_allocate(VALUE cls) {return doc_allocate(cls, imagedriver);}
139
140 // ------------------------ doc pages ---------------------------------------
141
142 static void page_free(page_internal_t*page)
143 {
144     if(!page) return;
145     if(page->page) {
146         page->page->destroy(page->page);
147         page->page = 0;
148     }
149     free(page);
150 }
151 static void page_mark(page_internal_t*page)
152 {
153     rb_gc_mark(page->doc->self);
154 }
155 static VALUE page_allocate(VALUE cls)
156 {
157     page_internal_t*page = 0;
158     VALUE v = Data_Make_Struct(cls, page_internal_t, page_mark, page_free, page);
159     memset(page, 0, sizeof(page_internal_t));
160     return v;
161 }
162 static VALUE page_nr(VALUE cls)
163 {
164     Get_Page(page,cls)
165     return INT2FIX(page->page->nr);
166 }
167 static VALUE page_width(VALUE cls)
168 {
169     Get_Page(page,cls)
170     return INT2FIX(page->page->width);
171 }
172 static VALUE page_height(VALUE cls)
173 {
174     Get_Page(page,cls)
175     return INT2FIX(page->page->height);
176 }
177
178 // ------------------------ image -------------------------------------------
179
180 #define Get_Image(image,cls) image_internal_t*image=0;Data_Get_Struct(cls, image_internal_t, image);
181
182 static void image_free(image_internal_t*image)
183 {
184     free(image);
185 }
186 static void image_mark(image_internal_t*image)
187 {
188     rb_gc_mark(image->doc->self);
189 }
190 static VALUE image_allocate(VALUE cls)
191 {
192     image_internal_t*image = 0;
193     VALUE v = Data_Make_Struct(cls, image_internal_t, image_mark, image_free, image);
194     memset(image, 0, sizeof(image_internal_t));
195     return v;
196 }
197 static VALUE image_width(VALUE cls)
198 {
199     Get_Image(image,cls)
200     return INT2FIX(image->image->width);
201 }
202 static VALUE image_height(VALUE cls)
203 {
204     Get_Image(image,cls)
205     return INT2FIX(image->image->height);
206 }
207 static VALUE image_rescale(VALUE cls, VALUE _width, VALUE _height)
208 {
209     Get_Image(image,cls)
210     Check_Type(_width, T_FIXNUM);
211     Check_Type(_height, T_FIXNUM);
212     int width = FIX2INT(_width);
213     int height = FIX2INT(_height);
214     volatile VALUE v_image2 = image_allocate(Bitmap);
215     Get_Image(image2,v_image2)
216     image2->doc = image->doc;
217     image2->image = gfximage_rescale(image->image, width, height);
218     if(!image2->image) {
219         rb_raise(rb_eArgError, "Can't rescale to size %dx%d", width, height);
220     }
221     return v_image2;
222 }
223 static VALUE image_has_alpha(VALUE cls)
224 {
225     Get_Image(image,cls)
226     int size = image->image->width * image->image->height;
227     gfxcolor_t*data = image->image->data;
228     int t;
229     for(t=0;t<size;t++) {
230         if(data->a!=255) 
231             return Qtrue;
232     }
233     return Qfalse;
234 }
235 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
236 {
237     Get_Image(image,cls)
238     Check_Type(_filename, T_STRING);
239     Check_Type(quality, T_FIXNUM);
240     const char*filename = StringValuePtr(_filename);
241     gfximage_save_jpeg(image->image, filename, FIX2INT(quality));
242     return cls;
243 }
244 static VALUE image_save_png(VALUE cls, VALUE _filename)
245 {
246     Get_Image(image,cls)
247     Check_Type(_filename, T_STRING);
248     const char*filename = StringValuePtr(_filename);
249     gfximage_save_png(image->image, filename);
250     return cls;
251 }
252 VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
253 {
254     VALUE v = image_allocate(Bitmap);
255     Get_Image(image,v)
256     image->image = _image;
257     image->doc = doc;
258     return v;
259 }
260 void invalidate_image(VALUE v)
261 {
262     Get_Image(image,v)
263     image->image = 0;
264 }
265
266 // ------------------------ glyphs ------------------------------------------
267
268 static VALUE convert_line(gfxline_t*line);
269
270 #define Get_Glyph(glyph,cls) glyph_internal_t*glyph=0;Data_Get_Struct(cls, glyph_internal_t, glyph);
271
272 static void glyph_free(glyph_internal_t*glyph)
273 {
274     free(glyph);
275 }
276
277 static void glyph_mark(glyph_internal_t*glyph)
278 {
279     rb_gc_mark(glyph->font->self);
280 }
281
282 static VALUE glyph_allocate(VALUE cls)
283 {
284     glyph_internal_t*glyph = 0;
285     VALUE v = Data_Make_Struct(cls, glyph_internal_t, glyph_mark, glyph_free, glyph);
286     memset(glyph, 0, sizeof(glyph_internal_t));
287     return v;
288 }
289
290 static VALUE glyph_polygon(VALUE cls)
291 {
292     Get_Glyph(glyph,cls);
293     return convert_line(glyph->font->font->glyphs[glyph->nr].line);
294 }
295
296 static VALUE glyph_advance(VALUE cls)
297 {
298     Get_Glyph(glyph,cls);
299     return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
300 }
301
302 static VALUE glyph_bbox(VALUE cls)
303 {
304     Get_Glyph(glyph,cls);
305     gfxbbox_t bbox = gfxline_getbbox(glyph->font->font->glyphs[glyph->nr].line);
306     return rb_ary_new3(4, rb_float_new(bbox.xmin), 
307                           rb_float_new(bbox.ymin), 
308                           rb_float_new(bbox.xmax), 
309                           rb_float_new(bbox.ymax));
310 }
311
312 static VALUE glyph_unicode(VALUE cls)
313 {
314     Get_Glyph(glyph,cls);
315     return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
316 }
317
318 // ------------------------ font --------------------------------------------
319
320 #define Get_Font(font,cls) font_internal_t*font=0;Data_Get_Struct(cls, font_internal_t, font);
321
322 static void font_mark(font_internal_t*font)
323 {
324     rb_gc_mark(font->glyph_array);
325 }
326
327 static void font_free(font_internal_t*font)
328 {
329     free(font);
330 }
331
332 static VALUE font_allocate(VALUE cls)
333 {
334     font_internal_t*font = 0;
335     VALUE v = Data_Make_Struct(cls, font_internal_t, font_mark, font_free, font);
336     memset(font, 0, sizeof(font_internal_t));
337     font->self = v;
338     return v;
339 }
340
341 static VALUE font_ascent(VALUE cls)
342 {
343     Get_Font(font,cls);
344     return rb_float_new(font->font->ascent);
345 }
346
347 static VALUE font_descent(VALUE cls)
348 {
349     Get_Font(font,cls);
350     return rb_float_new(font->font->descent);
351 }
352
353 static VALUE font_name(VALUE cls)
354 {
355     Get_Font(font,cls);
356     return rb_tainted_str_new2(font->font->id);
357 }
358
359 static VALUE font_glyphs(VALUE cls)
360 {
361     Get_Font(font,cls);
362     return font->glyph_array;
363 }
364
365 static VALUE font_kerning(VALUE cls)
366 {
367     Get_Font(font,cls);
368     gfxkerning_t*kerning = font->font->kerning;
369     int kerning_size = font->font->kerning_size;
370     volatile VALUE a = rb_ary_new2(kerning_size);
371     int t;
372     for(t=0;t<kerning_size;t++) {
373         volatile VALUE tuple = rb_ary_new2(3);
374         rb_ary_store(tuple, 0, INT2FIX(kerning[t].c1));
375         rb_ary_store(tuple, 1, INT2FIX(kerning[t].c2));
376         rb_ary_store(tuple, 2, INT2FIX(kerning[t].advance));
377         rb_ary_store(a, t, tuple);
378     }
379     return a;
380 }
381
382 // ------------------------ gfx device --------------------------------------
383
384 typedef struct device_internal {
385     doc_internal_t*doc;
386     VALUE v;
387 } device_internal_t;
388
389 static ID id_setparameter = 0;
390 static ID id_startpage = 0;
391 static ID id_startclip = 0;
392 static ID id_endclip = 0;
393 static ID id_stroke = 0;
394 static ID id_fill = 0;
395 static ID id_fillbitmap = 0;
396 static ID id_fillgradient = 0;
397 static ID id_addfont = 0;
398 static ID id_drawchar = 0;
399 static ID id_drawlink = 0;
400 static ID id_endpage = 0;
401 static ID id_geterror = 0;
402 static ID id_finish = 0;
403 static ID id_butt = 0;
404 static ID id_round = 0;
405 static ID id_square = 0;
406 static ID id_bevel = 0;
407 static ID id_miter = 0;
408 static ID id_move = 0;
409 static ID id_line = 0;
410 static ID id_spline = 0;
411 static ID id_radial = 0;
412 static ID id_linear = 0;
413
414 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
415
416 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
417
418 VALUE convert_line(gfxline_t*line)
419 {
420     int len = 0;
421     gfxline_t*l = line;
422     while(l) {l=l->next;len++;}
423
424     volatile VALUE array = rb_ary_new2(len);
425
426     int pos = 0;
427     l = line;
428     while(l) {
429         volatile VALUE e;
430         if(l->type == gfx_moveTo) {
431             e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
432             rb_ary_store(array, pos, e);
433             rb_ary_store(e, 1, rb_float_new(l->x));
434             rb_ary_store(e, 2, rb_float_new(l->y));
435         } else if(l->type == gfx_lineTo) {
436             e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
437             rb_ary_store(array, pos, e);
438             rb_ary_store(e, 1, rb_float_new(l->x));
439             rb_ary_store(e, 2, rb_float_new(l->y));
440         } else {
441             e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
442             rb_ary_store(array, pos, e);
443             rb_ary_store(e, 1, rb_float_new(l->x));
444             rb_ary_store(e, 2, rb_float_new(l->y));
445             rb_ary_store(e, 3, rb_float_new(l->sx));
446             rb_ary_store(e, 4, rb_float_new(l->sy));
447         }
448         pos++;
449         l=l->next;
450     }
451     return array;
452 }
453 VALUE convert_color(gfxcolor_t*color)
454 {
455     return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
456 }
457 VALUE convert_matrix(gfxmatrix_t*matrix)
458 {
459     volatile VALUE array = rb_ary_new2(3);
460     volatile VALUE a = rb_ary_new2(2);
461     rb_ary_store(array, 0, a);
462     rb_ary_store(a, 0, rb_float_new(matrix->m00));
463     rb_ary_store(a, 1, rb_float_new(matrix->m01));
464     a = rb_ary_new2(2);
465     rb_ary_store(array, 1, a);
466     rb_ary_store(a, 0, rb_float_new(matrix->m10));
467     rb_ary_store(a, 1, rb_float_new(matrix->m11));
468     a = rb_ary_new2(2);
469     rb_ary_store(array, 2, a);
470     rb_ary_store(a, 0, rb_float_new(matrix->tx));
471     rb_ary_store(a, 1, rb_float_new(matrix->ty));
472     return array;
473 }
474 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
475 {
476     return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
477 }
478 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
479 {
480     i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
481 }
482 static VALUE convert_font(gfxfont_t*font)
483 {
484     volatile VALUE v2 = font_allocate(Font);
485     Get_Font(f, v2);
486     f->font = font;
487     f->glyph_array = rb_ary_new2(font->num_glyphs);
488
489     int t;
490     for(t=0;t<font->num_glyphs;t++) {
491         volatile VALUE a = glyph_allocate(Glyph);
492         rb_ary_store(f->glyph_array, t, a);
493         Get_Glyph(g, a);
494         g->font = f;
495         g->nr = t;
496     }
497     return v2;
498 }
499 #define HEAD \
500     device_internal_t*i = (device_internal_t*)dev->internal; \
501     VALUE v = i->v;
502 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
503 {
504     HEAD
505     volatile VALUE v_key = rb_tainted_str_new2(key);
506     volatile VALUE v_value = rb_tainted_str_new2(value);
507     VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
508     return 0;
509 }
510 void rb_startpage(gfxdevice_t*dev, int width, int height)
511 {
512     HEAD
513     VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
514 }
515 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
516 {
517     HEAD
518     volatile VALUE v_line = convert_line(line);
519     VALUE ret = forward(v,id_startclip,1,v_line);
520 }
521 void rb_endclip(gfxdevice_t*dev)
522 {
523     HEAD
524     VALUE ret = forward(v,id_endclip,0);
525 }
526 void rb_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
527 {
528     HEAD
529     
530     ID cap = 0;
531     if(cap_style == gfx_capButt) cap = id_butt;
532     else if(cap_style == gfx_capRound) cap = id_round;
533     else if(cap_style == gfx_capSquare) cap = id_square;
534     
535     ID joint = 0;
536     if(joint_style == gfx_joinRound) joint = id_round;
537     else if(joint_style == gfx_joinMiter) joint = id_miter;
538     else if(joint_style == gfx_joinBevel) joint = id_bevel;
539
540     volatile VALUE v_line = convert_line(line);
541     volatile VALUE v_width = rb_float_new(width);
542     volatile VALUE v_color = convert_color(color);
543     volatile VALUE v_miter = rb_float_new(miterLimit);
544     forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
545 }
546 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
547 {
548     HEAD
549     
550     volatile VALUE v_line = convert_line(line);
551     volatile VALUE v_color = convert_color(color);
552     forward(v, id_fill, 2, v_line, v_color);
553 }
554 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
555 {
556     HEAD
557     volatile VALUE v_image = convert_image(i->doc, img);
558     volatile VALUE v_line = convert_line(line);
559     volatile VALUE v_matrix = convert_matrix(matrix);
560     forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
561     invalidate_image(v_image);
562 }
563 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
564 {
565     HEAD
566     ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
567     
568     volatile VALUE v_line = convert_line(line);
569     volatile VALUE v_matrix = convert_matrix(matrix);
570     volatile VALUE v_gradient = convert_gradient(gradient);
571     forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
572 }
573 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
574 {
575     HEAD
576
577     volatile VALUE f = font_is_cached(i, font);
578     if(!f) {f=convert_font(font);cache_font(i,font,f);}
579
580     forward(v, id_addfont, 1, f);
581 }
582 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
583 {
584     HEAD
585     volatile VALUE f = font_is_cached(i, font);
586     if(!f) {f=convert_font(font);cache_font(i,font,f);}
587
588     volatile VALUE v_color = convert_color(color);
589     volatile VALUE v_matrix = convert_matrix(matrix);
590     forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
591 }
592 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
593 {
594     HEAD
595     volatile VALUE v_line = convert_line(line);
596     volatile VALUE v_action = rb_tainted_str_new2(action);
597     forward(v, id_drawlink, v_line, v_action);
598 }
599 void rb_endpage(gfxdevice_t*dev)
600 {
601     HEAD
602     forward(v, id_endpage, 0);
603 }
604 gfxresult_t* rb_finish(gfxdevice_t*dev)
605 {
606     HEAD
607     VALUE ret = forward(v, id_endpage, 0);
608     gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
609     r->internal = (void*)(ptroff_t)ret;
610     return r;
611 }
612
613 static VALUE page_render(VALUE cls, VALUE device)
614 {
615     Check_Type(device, T_OBJECT);
616     Get_Page(page,cls)
617
618     gfxdevice_t dev;
619     device_internal_t i;
620     i.v = device;
621     i.doc = page->doc;
622
623     dev.internal = &i;
624     dev.setparameter = rb_setparameter;
625     dev.startpage = rb_startpage;
626     dev.startclip = rb_startclip;
627     dev.endclip = rb_endclip;
628     dev.stroke = rb_stroke;
629     dev.fill = rb_fill;
630     dev.fillbitmap = rb_fillbitmap;
631     dev.fillgradient = rb_fillgradient;
632     dev.addfont = rb_addfont;
633     dev.drawchar = rb_drawchar;
634     dev.drawlink = rb_drawlink;
635     dev.endpage = rb_endpage;
636     dev.finish = rb_finish;
637
638     dev.startpage(&dev, page->page->width, page->page->height);
639     page->page->render(page->page, &dev);
640     dev.endpage(&dev);
641
642     return cls;
643 }
644
645 static VALUE doc_prepare(VALUE cls, VALUE device)
646 {
647     Get_Doc(doc,cls);
648
649     gfxdevice_t dev;
650     device_internal_t i;
651     i.v = device;
652     i.doc = doc;
653
654     dev.internal = &i;
655     dev.setparameter = rb_setparameter;
656     dev.startpage = rb_startpage;
657     dev.startclip = rb_startclip;
658     dev.endclip = rb_endclip;
659     dev.stroke = rb_stroke;
660     dev.fill = rb_fill;
661     dev.fillbitmap = rb_fillbitmap;
662     dev.fillgradient = rb_fillgradient;
663     dev.addfont = rb_addfont;
664     dev.drawchar = rb_drawchar;
665     dev.drawlink = rb_drawlink;
666     dev.endpage = rb_endpage;
667     dev.finish = rb_finish;
668
669     doc->doc->prepare(doc->doc, &dev);
670     return cls;
671 }
672
673
674 // ---------------------- global functions ----------------------------------
675
676 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
677 {
678     Check_Type(_key, T_STRING);
679     Check_Type(_value, T_STRING);
680     const char*key = StringValuePtr(_key);
681     const char*value = StringValuePtr(_value);
682     pdfdriver->setparameter(pdfdriver, key, value);
683     swfdriver->setparameter(swfdriver, key, value);
684     imagedriver->setparameter(imagedriver, key, value);
685     return GFX;
686 }
687
688 // --------------------------------------------------------------------------
689
690 void Init_gfx()
691 {
692     initLog(0,0,0,0,0,2);
693     pdfdriver = gfxsource_pdf_create();
694     swfdriver = gfxsource_swf_create();
695     imagedriver = gfxsource_image_create();
696
697     GFX = rb_define_module("GFX");
698     
699     rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
700     
701     DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
702     rb_define_method(DocumentPage, "width", page_width, 0);
703     rb_define_method(DocumentPage, "height", page_height, 0);
704     rb_define_method(DocumentPage, "nr", page_nr, 0);
705     rb_define_method(DocumentPage, "render", page_render, 1);
706
707     Document = rb_define_class_under(GFX, "Document", rb_cObject);
708     rb_define_method(Document, "initialize", doc_initialize, 1);
709     rb_define_method(Document, "page", doc_get_page, 1);
710     rb_define_method(Document, "each_page", doc_each_page, 0);
711     rb_define_method(Document, "prepare", doc_prepare, 1);
712     
713     Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
714     rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
715     rb_define_method(Bitmap, "save_png", image_save_png, 1);
716     rb_define_method(Bitmap, "width", image_width, 0);
717     rb_define_method(Bitmap, "height", image_height, 0);
718     rb_define_method(Bitmap, "rescale", image_rescale, 2);
719     rb_define_method(Bitmap, "has_alpha", image_has_alpha, 0);
720     
721     Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
722     rb_define_method(Glyph, "polygon", glyph_polygon, 0);
723     rb_define_method(Glyph, "unicode", glyph_unicode, 0);
724     rb_define_method(Glyph, "advance", glyph_advance, 0);
725     rb_define_method(Glyph, "bbox", glyph_bbox, 0);
726     
727     Font = rb_define_class_under(GFX, "Font", rb_cObject);
728     rb_define_method(Font, "name", font_name, 0);
729     rb_define_method(Font, "ascent", font_ascent, 0);
730     rb_define_method(Font, "descent", font_descent, 0);
731     rb_define_method(Font, "glyphs", font_glyphs, 0);
732     rb_define_method(Font, "kerning", font_kerning, 0);
733     rb_define_method(Font, "get_kerning_table", font_kerning, 0);
734     
735     Device = rb_define_class_under(GFX, "Device", rb_cObject);
736     rb_define_method(Device, "startpage", noop, -1);
737     rb_define_method(Device, "endpage", noop, -1);
738     rb_define_method(Device, "startclip", noop, -1);
739     rb_define_method(Device, "endclip", noop, -1);
740     rb_define_method(Device, "stroke", noop, -1);
741     rb_define_method(Device, "fill", noop, -1);
742     rb_define_method(Device, "fillbitmap", noop, -1);
743     rb_define_method(Device, "fillgradient", noop, -1);
744     rb_define_method(Device, "addfont", noop, -1);
745     rb_define_method(Device, "drawchar", noop, -1);
746     rb_define_method(Device, "drawlink", noop, -1);
747     rb_define_method(Device, "endpage", noop, -1);
748
749     PDFClass = rb_define_class_under(GFX, "PDF", Document);
750     rb_define_alloc_func(PDFClass, pdf_allocate);
751     
752     SWFClass = rb_define_class_under(GFX, "SWF", Document);
753     rb_define_alloc_func(SWFClass, swf_allocate);
754     
755     ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
756     rb_define_alloc_func(ImageClass, imgdrv_allocate);
757
758     id_setparameter = rb_intern("setparameter");
759     id_startpage = rb_intern("startpage") ;
760     id_startclip = rb_intern("startclip") ;
761     id_endclip = rb_intern("endclip") ;
762     id_stroke = rb_intern("stroke") ;
763     id_fill = rb_intern("fill") ;
764     id_fillbitmap = rb_intern("fillbitmap") ;
765     id_fillgradient = rb_intern("fillgradient") ;
766     id_addfont = rb_intern("addfont") ;
767     id_drawchar = rb_intern("drawchar") ;
768     id_drawlink = rb_intern("drawlink") ;
769     id_endpage = rb_intern("endpage") ;
770     id_geterror = rb_intern("geterror") ;
771     id_finish = rb_intern("finish") ;
772     id_butt = rb_intern("butt");
773     id_round = rb_intern("round");
774     id_square = rb_intern("square");
775     id_miter = rb_intern("miter");
776     id_bevel = rb_intern("bevel");
777     id_move = rb_intern("move");
778     id_line = rb_intern("line");
779     id_spline = rb_intern("spline");
780     id_radial = rb_intern("radial");
781     id_linear = rb_intern("linear");
782 }
783