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