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