minor bugfixes
[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[t].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_save_eot(VALUE cls, VALUE _filename)
382 {
383     Get_Font(font,cls);
384     Check_Type(_filename, T_STRING);
385     const char*filename = StringValuePtr(_filename);
386     gfxfont_save_eot(font->font, filename);
387     return Qnil;
388 }
389
390 static VALUE font_kerning(VALUE cls)
391 {
392     Get_Font(font,cls);
393     gfxkerning_t*kerning = font->font->kerning;
394     int kerning_size = font->font->kerning_size;
395     volatile VALUE a = rb_ary_new2(kerning_size);
396     int t;
397     for(t=0;t<kerning_size;t++) {
398         volatile VALUE tuple = rb_ary_new2(3);
399         rb_ary_store(tuple, 0, INT2FIX(kerning[t].c1));
400         rb_ary_store(tuple, 1, INT2FIX(kerning[t].c2));
401         rb_ary_store(tuple, 2, INT2FIX(kerning[t].advance));
402         rb_ary_store(a, t, tuple);
403     }
404     return a;
405 }
406
407 // ------------------------ gfx device --------------------------------------
408
409 typedef struct device_internal {
410     doc_internal_t*doc;
411     VALUE v;
412 } device_internal_t;
413
414 static ID id_setparameter = 0;
415 static ID id_startpage = 0;
416 static ID id_startclip = 0;
417 static ID id_endclip = 0;
418 static ID id_stroke = 0;
419 static ID id_fill = 0;
420 static ID id_fillbitmap = 0;
421 static ID id_fillgradient = 0;
422 static ID id_addfont = 0;
423 static ID id_drawchar = 0;
424 static ID id_drawlink = 0;
425 static ID id_endpage = 0;
426 static ID id_geterror = 0;
427 static ID id_finish = 0;
428 static ID id_butt = 0;
429 static ID id_round = 0;
430 static ID id_square = 0;
431 static ID id_bevel = 0;
432 static ID id_miter = 0;
433 static ID id_move = 0;
434 static ID id_line = 0;
435 static ID id_spline = 0;
436 static ID id_radial = 0;
437 static ID id_linear = 0;
438 static ID id_remove_font_transforms = 0;
439 static ID id_maketransparent = 0;
440
441 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
442
443 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
444
445 VALUE convert_line(gfxline_t*line)
446 {
447     int len = 0;
448     gfxline_t*l = line;
449     while(l) {l=l->next;len++;}
450
451     volatile VALUE array = rb_ary_new2(len);
452
453     int pos = 0;
454     l = line;
455     while(l) {
456         volatile VALUE e;
457         if(l->type == gfx_moveTo) {
458             e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
459             rb_ary_store(array, pos, e);
460             rb_ary_store(e, 1, rb_float_new(l->x));
461             rb_ary_store(e, 2, rb_float_new(l->y));
462         } else if(l->type == gfx_lineTo) {
463             e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
464             rb_ary_store(array, pos, e);
465             rb_ary_store(e, 1, rb_float_new(l->x));
466             rb_ary_store(e, 2, rb_float_new(l->y));
467         } else {
468             e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
469             rb_ary_store(array, pos, e);
470             rb_ary_store(e, 1, rb_float_new(l->x));
471             rb_ary_store(e, 2, rb_float_new(l->y));
472             rb_ary_store(e, 3, rb_float_new(l->sx));
473             rb_ary_store(e, 4, rb_float_new(l->sy));
474         }
475         pos++;
476         l=l->next;
477     }
478     return array;
479 }
480 VALUE convert_color(gfxcolor_t*color)
481 {
482     return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
483 }
484 VALUE convert_matrix(gfxmatrix_t*matrix)
485 {
486     volatile VALUE array = rb_ary_new2(3);
487     volatile VALUE a = rb_ary_new2(2);
488     rb_ary_store(array, 0, a);
489     rb_ary_store(a, 0, rb_float_new(matrix->m00));
490     rb_ary_store(a, 1, rb_float_new(matrix->m01));
491     a = rb_ary_new2(2);
492     rb_ary_store(array, 1, a);
493     rb_ary_store(a, 0, rb_float_new(matrix->m10));
494     rb_ary_store(a, 1, rb_float_new(matrix->m11));
495     a = rb_ary_new2(2);
496     rb_ary_store(array, 2, a);
497     rb_ary_store(a, 0, rb_float_new(matrix->tx));
498     rb_ary_store(a, 1, rb_float_new(matrix->ty));
499     return array;
500 }
501 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
502 {
503     return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
504 }
505 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
506 {
507     i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
508 }
509 static VALUE convert_font(gfxfont_t*font)
510 {
511     volatile VALUE v2 = font_allocate(Font);
512     Get_Font(f, v2);
513     f->font = font;
514     f->glyph_array = rb_ary_new2(font->num_glyphs);
515
516     int t;
517     for(t=0;t<font->num_glyphs;t++) {
518         volatile VALUE a = glyph_allocate(Glyph);
519         rb_ary_store(f->glyph_array, t, a);
520         Get_Glyph(g, a);
521         g->font = f;
522         g->nr = t;
523     }
524     return v2;
525 }
526 static VALUE convert_gradient(gfxgradient_t*gradient)
527 {
528     return Qnil; //TODO
529 }
530 #define HEAD \
531     device_internal_t*i = (device_internal_t*)dev->internal; \
532     VALUE v = i->v;
533 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
534 {
535     HEAD
536     volatile VALUE v_key = rb_tainted_str_new2(key);
537     volatile VALUE v_value = rb_tainted_str_new2(value);
538     VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
539     return 0;
540 }
541 void rb_startpage(gfxdevice_t*dev, int width, int height)
542 {
543     HEAD
544     VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
545 }
546 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
547 {
548     HEAD
549     volatile VALUE v_line = convert_line(line);
550     VALUE ret = forward(v,id_startclip,1,v_line);
551 }
552 void rb_endclip(gfxdevice_t*dev)
553 {
554     HEAD
555     VALUE ret = forward(v,id_endclip,0);
556 }
557 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)
558 {
559     HEAD
560     
561     ID cap = 0;
562     if(cap_style == gfx_capButt) cap = id_butt;
563     else if(cap_style == gfx_capRound) cap = id_round;
564     else if(cap_style == gfx_capSquare) cap = id_square;
565     
566     ID joint = 0;
567     if(joint_style == gfx_joinRound) joint = id_round;
568     else if(joint_style == gfx_joinMiter) joint = id_miter;
569     else if(joint_style == gfx_joinBevel) joint = id_bevel;
570
571     volatile VALUE v_line = convert_line(line);
572     volatile VALUE v_width = rb_float_new(width);
573     volatile VALUE v_color = convert_color(color);
574     volatile VALUE v_miter = rb_float_new(miterLimit);
575     forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
576 }
577 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
578 {
579     HEAD
580     
581     volatile VALUE v_line = convert_line(line);
582     volatile VALUE v_color = convert_color(color);
583     forward(v, id_fill, 2, v_line, v_color);
584 }
585 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
586 {
587     HEAD
588     volatile VALUE v_image = convert_image(i->doc, img);
589     volatile VALUE v_line = convert_line(line);
590     volatile VALUE v_matrix = convert_matrix(matrix);
591     forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
592     invalidate_image(v_image);
593 }
594 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
595 {
596     HEAD
597     ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
598     
599     volatile VALUE v_line = convert_line(line);
600     volatile VALUE v_matrix = convert_matrix(matrix);
601     volatile VALUE v_gradient = convert_gradient(gradient);
602     forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
603 }
604 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
605 {
606     HEAD
607
608     volatile VALUE f = font_is_cached(i, font);
609     if(!f) {f=convert_font(font);cache_font(i,font,f);}
610
611     forward(v, id_addfont, 1, f);
612 }
613 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
614 {
615     HEAD
616     volatile VALUE f = font_is_cached(i, font);
617     if(!f) {f=convert_font(font);cache_font(i,font,f);}
618
619     volatile VALUE v_color = convert_color(color);
620     volatile VALUE v_matrix = convert_matrix(matrix);
621     forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
622 }
623 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
624 {
625     HEAD
626     volatile VALUE v_line = convert_line(line);
627     volatile VALUE v_action = rb_tainted_str_new2(action);
628
629     forward(v, id_drawlink, 2, v_line, v_action);
630 }
631 void rb_endpage(gfxdevice_t*dev)
632 {
633     HEAD
634     forward(v, id_endpage, 0);
635 }
636 void gfxresult_rb_destroy(gfxresult_t*r)
637 {
638     free(r);
639 }
640 gfxresult_t* rb_finish(gfxdevice_t*dev)
641 {
642     HEAD
643     VALUE ret = forward(v, id_finish, 0);
644     gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
645     r->destroy = gfxresult_rb_destroy;
646     r->internal = (void*)(ptroff_t)ret;
647     return r;
648 }
649
650 #define make_device(dev, idoc, device) \
651     gfxdevice_t dev; \
652     device_internal_t i; \
653     i.v = device; \
654     i.doc = idoc; \
655     dev.internal = &i; \
656     dev.setparameter = rb_setparameter; \
657     dev.startpage = rb_startpage; \
658     dev.startclip = rb_startclip; \
659     dev.endclip = rb_endclip; \
660     dev.stroke = rb_stroke; \
661     dev.fill = rb_fill; \
662     dev.fillbitmap = rb_fillbitmap; \
663     dev.fillgradient = rb_fillgradient; \
664     dev.addfont = rb_addfont; \
665     dev.drawchar = rb_drawchar; \
666     dev.drawlink = rb_drawlink; \
667     dev.endpage = rb_endpage; \
668     dev.finish = rb_finish;
669
670 static VALUE page_render(VALUE cls, VALUE device)
671 {
672     Check_Type(device, T_OBJECT);
673     Get_Page(page,cls)
674
675     make_device(dev, page->doc, device);
676
677     dev.startpage(&dev, page->page->width, page->page->height);
678     page->page->render(page->page, &dev);
679     dev.endpage(&dev);
680
681     return cls;
682 }
683
684 static VALUE doc_render(VALUE cls, VALUE device, VALUE _range, VALUE filters)
685 {
686     const char*range = 0;
687     if(!NIL_P(_range)) {
688         Check_Type(_range, T_STRING);
689         range = StringValuePtr(_range);
690     }
691     Get_Doc(doc,cls);
692
693     make_device(_dev, doc, device);
694     gfxdevice_t*dev = &_dev;
695
696     if(!NIL_P(filters)) {
697         if(TYPE(filters) != T_ARRAY)
698             rb_raise(rb_eArgError, "third argument of doc->render must be an array of symbols");
699
700         int len = RARRAY(filters)->len;
701         int t=0;
702         while(t<len) {
703             VALUE filter = RARRAY(filters)->ptr[t++];
704             Check_Type(filter, T_SYMBOL);
705             ID id = SYM2ID(filter);
706 #           define PARAM(x) VALUE x;if(t==len) rb_raise(rb_eArgError, "End of array while parsing arguments for filter %s", rb_id2name(id)); \
707                             else x = RARRAY(filters)->ptr[t++];
708             if(id == id_remove_font_transforms) {
709                 wrap_filter2(dev, remove_font_transforms);
710             } else if(id == id_maketransparent) {
711                 PARAM(alpha);
712                 wrap_filter(dev, maketransparent, FIX2INT(alpha));
713             } else {
714                 rb_raise(rb_eArgError, "unknown filter %s", rb_id2name(id));
715             }
716         }
717     }
718
719     int pagenr;
720     for(pagenr=1;pagenr<=doc->doc->num_pages;pagenr++) {
721         if(is_in_range(pagenr, (char*)range)) {
722             gfxpage_t*page = doc->doc->getpage(doc->doc, pagenr);
723             dev->startpage(dev, page->width, page->height);
724             page->render(page, dev);
725             dev->endpage(dev);
726             page->destroy(page);
727         }
728     }
729    
730
731     gfxresult_t*r = dev->finish(dev);
732     r->destroy(r);
733
734     return Qnil;
735 }
736
737 static VALUE doc_prepare(VALUE cls, VALUE device)
738 {
739     Get_Doc(doc,cls);
740     make_device(dev, doc, device);
741     doc->doc->prepare(doc->doc, &dev);
742     return cls;
743 }
744
745
746 // ---------------------- global functions ----------------------------------
747
748 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
749 {
750     Check_Type(_key, T_STRING);
751     Check_Type(_value, T_STRING);
752     const char*key = StringValuePtr(_key);
753     const char*value = StringValuePtr(_value);
754     pdfdriver->setparameter(pdfdriver, key, value);
755     swfdriver->setparameter(swfdriver, key, value);
756     imagedriver->setparameter(imagedriver, key, value);
757     return GFX;
758 }
759
760 // --------------------------------------------------------------------------
761
762 void Init_gfx()
763 {
764     initLog(0,0,0,0,0,2);
765     pdfdriver = gfxsource_pdf_create();
766     swfdriver = gfxsource_swf_create();
767     imagedriver = gfxsource_image_create();
768
769     GFX = rb_define_module("GFX");
770     rb_define_const(GFX, "VERSION", INT2FIX(20100309));
771     
772     rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
773     
774     DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
775     rb_define_method(DocumentPage, "width", page_width, 0);
776     rb_define_method(DocumentPage, "height", page_height, 0);
777     rb_define_method(DocumentPage, "nr", page_nr, 0);
778     rb_define_method(DocumentPage, "render", page_render, 1);
779
780     Document = rb_define_class_under(GFX, "Document", rb_cObject);
781     rb_define_method(Document, "initialize", doc_initialize, 1);
782     rb_define_method(Document, "page", doc_get_page, 1);
783     rb_define_method(Document, "each_page", doc_each_page, 0);
784     rb_define_method(Document, "prepare", doc_prepare, 1);
785     rb_define_method(Document, "render", doc_render, 3);
786     
787     Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
788     rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
789     rb_define_method(Bitmap, "save_png", image_save_png, 1);
790     rb_define_method(Bitmap, "width", image_width, 0);
791     rb_define_method(Bitmap, "height", image_height, 0);
792     rb_define_method(Bitmap, "rescale", image_rescale, 2);
793     rb_define_method(Bitmap, "has_alpha", image_has_alpha, 0);
794     
795     Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
796     rb_define_method(Glyph, "polygon", glyph_polygon, 0);
797     rb_define_method(Glyph, "unicode", glyph_unicode, 0);
798     rb_define_method(Glyph, "advance", glyph_advance, 0);
799     rb_define_method(Glyph, "bbox", glyph_bbox, 0);
800     
801     Font = rb_define_class_under(GFX, "Font", rb_cObject);
802     rb_define_method(Font, "name", font_name, 0);
803     rb_define_method(Font, "ascent", font_ascent, 0);
804     rb_define_method(Font, "descent", font_descent, 0);
805     rb_define_method(Font, "glyphs", font_glyphs, 0);
806     rb_define_method(Font, "kerning", font_kerning, 0);
807     rb_define_method(Font, "get_kerning_table", font_kerning, 0);
808     rb_define_method(Font, "save_ttf", font_save_ttf, 1);
809     rb_define_method(Font, "save_eot", font_save_eot, 1);
810     
811     Device = rb_define_class_under(GFX, "Device", rb_cObject);
812     rb_define_method(Device, "startpage", noop, -1);
813     rb_define_method(Device, "endpage", noop, -1);
814     rb_define_method(Device, "startclip", noop, -1);
815     rb_define_method(Device, "endclip", noop, -1);
816     rb_define_method(Device, "stroke", noop, -1);
817     rb_define_method(Device, "fill", noop, -1);
818     rb_define_method(Device, "fillbitmap", noop, -1);
819     rb_define_method(Device, "fillgradient", noop, -1);
820     rb_define_method(Device, "addfont", noop, -1);
821     rb_define_method(Device, "drawchar", noop, -1);
822     rb_define_method(Device, "drawlink", noop, -1);
823
824     PDFClass = rb_define_class_under(GFX, "PDF", Document);
825     rb_define_alloc_func(PDFClass, pdf_allocate);
826     
827     SWFClass = rb_define_class_under(GFX, "SWF", Document);
828     rb_define_alloc_func(SWFClass, swf_allocate);
829     
830     ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
831     rb_define_alloc_func(ImageClass, imgdrv_allocate);
832     
833     id_setparameter = rb_intern("setparameter");
834     id_startpage = rb_intern("startpage") ;
835     id_startclip = rb_intern("startclip") ;
836     id_endclip = rb_intern("endclip") ;
837     id_stroke = rb_intern("stroke") ;
838     id_fill = rb_intern("fill") ;
839     id_fillbitmap = rb_intern("fillbitmap") ;
840     id_fillgradient = rb_intern("fillgradient") ;
841     id_addfont = rb_intern("addfont") ;
842     id_drawchar = rb_intern("drawchar") ;
843     id_drawlink = rb_intern("drawlink") ;
844     id_endpage = rb_intern("endpage") ;
845     id_geterror = rb_intern("geterror") ;
846     id_finish = rb_intern("finish") ;
847     id_butt = rb_intern("butt");
848     id_round = rb_intern("round");
849     id_square = rb_intern("square");
850     id_miter = rb_intern("miter");
851     id_bevel = rb_intern("bevel");
852     id_move = rb_intern("move");
853     id_line = rb_intern("line");
854     id_spline = rb_intern("spline");
855     id_radial = rb_intern("radial");
856     id_linear = rb_intern("linear");
857     id_remove_font_transforms = rb_intern("remove_font_transforms");
858     id_maketransparent = rb_intern("maketransparent");
859 }
860