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