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