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"
13 #define RUBY_GFX_VERSION "0.9.0"
16 static VALUE Font, Glyph, Bitmap, Document, DocumentPage, PDFClass, SWFClass, ImageClass, Device;
19 typedef struct doc_internal {
21 gfxsource_t*driver; // filled by alloc
23 gfxfontlist_t*fontlist;
26 typedef struct page_internal {
31 typedef struct image_internal {
36 typedef struct font_internal {
42 typedef struct glyph_internal {
47 static gfxsource_t* pdfdriver = 0;
48 static gfxsource_t* imagedriver = 0;
49 static gfxsource_t* swfdriver = 0;
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);
54 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver);
55 static VALUE page_allocate(VALUE cls);
57 // ------------------------ documents ---------------------------------------
59 static VALUE doc_initialize(VALUE cls, VALUE _filename)
61 Check_Type(_filename, T_STRING);
63 const char*filename = StringValuePtr(_filename);
64 doc->fontlist = gfxfontlist_create();
65 doc->doc = pdfdriver->open(pdfdriver, filename);
69 static VALUE doc_num_pages(VALUE cls)
72 return INT2FIX(doc->doc->num_pages);
75 static VALUE doc_get_page(VALUE cls, VALUE _nr)
77 Check_Type(_nr, T_FIXNUM);
78 int nr = FIX2INT(_nr);
81 VALUE v = page_allocate(DocumentPage);
83 page->page = doc->doc->getpage(doc->doc, nr);
86 rb_raise(rb_eArgError, "No page %d in document", nr);
92 static VALUE doc_each_page(VALUE cls)
96 for(t=1;t<=doc->doc->num_pages;t++) {
97 VALUE v = page_allocate(DocumentPage);
99 page->page = doc->doc->getpage(doc->doc, t);
106 static void doc_mark(doc_internal_t*doc)
108 gfxfontlist_t*l = doc->fontlist;
111 rb_gc_mark((VALUE)l->user);
116 static void doc_free(doc_internal_t*doc)
118 gfxfontlist_free(doc->fontlist, 0);
119 doc->doc->destroy(doc->doc);
123 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver)
125 doc_internal_t*doc = 0;
126 VALUE v = Data_Make_Struct(cls, doc_internal_t, doc_mark, doc_free, doc);
128 memset(doc, 0, sizeof(doc_internal_t));
129 doc->driver = driver;
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);}
137 // ------------------------ doc pages ---------------------------------------
139 static void page_free(page_internal_t*page)
143 page->page->destroy(page->page);
148 static void page_mark(page_internal_t*page)
150 rb_gc_mark(page->doc->self);
152 static VALUE page_allocate(VALUE cls)
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));
159 static VALUE page_nr(VALUE cls)
162 return INT2FIX(page->page->nr);
164 static VALUE page_width(VALUE cls)
167 return INT2FIX(page->page->width);
169 static VALUE page_height(VALUE cls)
172 return INT2FIX(page->page->height);
175 // ------------------------ image -------------------------------------------
177 #define Get_Image(image,cls) image_internal_t*image=0;Data_Get_Struct(cls, image_internal_t, image);
179 static void image_free(image_internal_t*image)
183 static void image_mark(image_internal_t*image)
185 rb_gc_mark(image->doc->self);
187 static VALUE image_allocate(VALUE cls)
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));
194 static VALUE image_width(VALUE cls)
197 return INT2FIX(image->image->width);
199 static VALUE image_height(VALUE cls)
202 return INT2FIX(image->image->height);
204 static VALUE image_rescale(VALUE cls, VALUE _width, VALUE _height)
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);
217 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
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));
226 static VALUE image_save_png(VALUE cls, VALUE _filename)
229 Check_Type(_filename, T_STRING);
230 const char*filename = StringValuePtr(_filename);
231 gfximage_save_png(image->image, filename);
234 VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
236 VALUE v = image_allocate(Bitmap);
238 image->image = _image;
242 void invalidate_image(VALUE v)
248 // ------------------------ glyphs ------------------------------------------
250 static VALUE convert_line(gfxline_t*line);
252 #define Get_Glyph(glyph,cls) glyph_internal_t*glyph=0;Data_Get_Struct(cls, glyph_internal_t, glyph);
254 static void glyph_free(glyph_internal_t*glyph)
259 static void glyph_mark(glyph_internal_t*glyph)
261 rb_gc_mark(glyph->font->self);
264 static VALUE glyph_allocate(VALUE cls)
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));
272 static VALUE glyph_polygon(VALUE cls)
274 Get_Glyph(glyph,cls);
275 return convert_line(glyph->font->font->glyphs[glyph->nr].line);
278 static VALUE glyph_advance(VALUE cls)
280 Get_Glyph(glyph,cls);
281 return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
284 static VALUE glyph_unicode(VALUE cls)
286 Get_Glyph(glyph,cls);
287 return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
290 // ------------------------ font --------------------------------------------
292 #define Get_Font(font,cls) font_internal_t*font=0;Data_Get_Struct(cls, font_internal_t, font);
294 static void font_mark(font_internal_t*font)
296 rb_gc_mark(font->glyph_array);
299 static void font_free(font_internal_t*font)
304 static VALUE font_allocate(VALUE cls)
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));
313 static VALUE font_name(VALUE cls)
316 return rb_tainted_str_new2(font->font->id);
319 static VALUE font_glyphs(VALUE cls)
322 return font->glyph_array;
325 // ------------------------ gfx device --------------------------------------
327 typedef struct device_internal {
332 static ID id_setparameter = 0;
333 static ID id_startpage = 0;
334 static ID id_startclip = 0;
335 static ID id_endclip = 0;
336 static ID id_stroke = 0;
337 static ID id_fill = 0;
338 static ID id_fillbitmap = 0;
339 static ID id_fillgradient = 0;
340 static ID id_addfont = 0;
341 static ID id_drawchar = 0;
342 static ID id_drawlink = 0;
343 static ID id_endpage = 0;
344 static ID id_geterror = 0;
345 static ID id_finish = 0;
346 static ID id_butt = 0;
347 static ID id_round = 0;
348 static ID id_square = 0;
349 static ID id_bevel = 0;
350 static ID id_miter = 0;
351 static ID id_move = 0;
352 static ID id_line = 0;
353 static ID id_spline = 0;
354 static ID id_radial = 0;
355 static ID id_linear = 0;
357 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
359 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
361 VALUE convert_line(gfxline_t*line)
365 while(l) {l=l->next;len++;}
367 volatile VALUE array = rb_ary_new2(len);
373 if(l->type == gfx_moveTo) {
374 e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
375 rb_ary_store(array, pos, e);
376 rb_ary_store(e, 1, rb_float_new(l->x));
377 rb_ary_store(e, 2, rb_float_new(l->y));
378 } else if(l->type == gfx_lineTo) {
379 e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
380 rb_ary_store(array, pos, e);
381 rb_ary_store(e, 1, rb_float_new(l->x));
382 rb_ary_store(e, 2, rb_float_new(l->y));
384 e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
385 rb_ary_store(array, pos, e);
386 rb_ary_store(e, 1, rb_float_new(l->x));
387 rb_ary_store(e, 2, rb_float_new(l->y));
388 rb_ary_store(e, 3, rb_float_new(l->sx));
389 rb_ary_store(e, 4, rb_float_new(l->sy));
396 VALUE convert_color(gfxcolor_t*color)
398 return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
400 VALUE convert_matrix(gfxmatrix_t*matrix)
402 volatile VALUE array = rb_ary_new2(3);
403 volatile VALUE a = rb_ary_new2(2);
404 rb_ary_store(array, 0, a);
405 rb_ary_store(a, 0, rb_float_new(matrix->m00));
406 rb_ary_store(a, 1, rb_float_new(matrix->m01));
408 rb_ary_store(array, 1, a);
409 rb_ary_store(a, 0, rb_float_new(matrix->m10));
410 rb_ary_store(a, 1, rb_float_new(matrix->m11));
412 rb_ary_store(array, 2, a);
413 rb_ary_store(a, 0, rb_float_new(matrix->tx));
414 rb_ary_store(a, 1, rb_float_new(matrix->ty));
417 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
419 return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
421 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
423 i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
425 static VALUE convert_font(gfxfont_t*font)
427 volatile VALUE v2 = font_allocate(Font);
430 f->glyph_array = rb_ary_new2(font->num_glyphs);
433 for(t=0;t<font->num_glyphs;t++) {
434 volatile VALUE a = glyph_allocate(Glyph);
435 rb_ary_store(f->glyph_array, t, a);
443 device_internal_t*i = (device_internal_t*)dev->internal; \
445 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
448 volatile VALUE v_key = rb_tainted_str_new2(key);
449 volatile VALUE v_value = rb_tainted_str_new2(value);
450 VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
453 void rb_startpage(gfxdevice_t*dev, int width, int height)
456 VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
458 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
461 volatile VALUE v_line = convert_line(line);
462 VALUE ret = forward(v,id_startclip,1,v_line);
464 void rb_endclip(gfxdevice_t*dev)
467 VALUE ret = forward(v,id_endclip,0);
469 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)
474 if(cap_style == gfx_capButt) cap = id_butt;
475 else if(cap_style == gfx_capRound) cap = id_round;
476 else if(cap_style == gfx_capSquare) cap = id_square;
479 if(joint_style == gfx_joinRound) joint = id_round;
480 else if(joint_style == gfx_joinMiter) joint = id_miter;
481 else if(joint_style == gfx_joinBevel) joint = id_bevel;
483 volatile VALUE v_line = convert_line(line);
484 volatile VALUE v_width = rb_float_new(width);
485 volatile VALUE v_color = convert_color(color);
486 volatile VALUE v_miter = rb_float_new(miterLimit);
487 forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
489 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
493 volatile VALUE v_line = convert_line(line);
494 volatile VALUE v_color = convert_color(color);
495 forward(v, id_fill, 2, v_line, v_color);
497 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
500 volatile VALUE v_image = convert_image(i->doc, img);
501 volatile VALUE v_line = convert_line(line);
502 volatile VALUE v_matrix = convert_matrix(matrix);
503 forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
504 invalidate_image(v_image);
506 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
509 ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
511 volatile VALUE v_line = convert_line(line);
512 volatile VALUE v_matrix = convert_matrix(matrix);
513 volatile VALUE v_gradient = convert_gradient(gradient);
514 forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
516 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
520 volatile VALUE f = font_is_cached(i, font);
521 if(!f) {f=convert_font(font);cache_font(i,font,f);}
523 forward(v, id_addfont, 1, f);
525 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
528 volatile VALUE f = font_is_cached(i, font);
529 if(!f) {f=convert_font(font);cache_font(i,font,f);}
531 volatile VALUE v_color = convert_color(color);
532 volatile VALUE v_matrix = convert_matrix(matrix);
533 forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
535 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
538 volatile VALUE v_line = convert_line(line);
539 volatile VALUE v_action = rb_tainted_str_new2(action);
540 forward(v, id_drawlink, v_line, v_action);
542 void rb_endpage(gfxdevice_t*dev)
545 forward(v, id_endpage, 0);
547 gfxresult_t* rb_finish(gfxdevice_t*dev)
550 VALUE ret = forward(v, id_endpage, 0);
551 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
552 r->internal = (void*)(ptroff_t)ret;
556 static VALUE page_render(VALUE cls, VALUE device)
558 Check_Type(device, T_OBJECT);
567 dev.setparameter = rb_setparameter;
568 dev.startpage = rb_startpage;
569 dev.startclip = rb_startclip;
570 dev.endclip = rb_endclip;
571 dev.stroke = rb_stroke;
573 dev.fillbitmap = rb_fillbitmap;
574 dev.fillgradient = rb_fillgradient;
575 dev.addfont = rb_addfont;
576 dev.drawchar = rb_drawchar;
577 dev.drawlink = rb_drawlink;
578 dev.endpage = rb_endpage;
579 dev.finish = rb_finish;
581 dev.startpage(&dev, page->page->width, page->page->height);
582 page->page->render(page->page, &dev);
588 // ---------------------- global functions ----------------------------------
590 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
592 Check_Type(_key, T_STRING);
593 Check_Type(_value, T_STRING);
594 const char*key = StringValuePtr(_key);
595 const char*value = StringValuePtr(_value);
596 pdfdriver->setparameter(pdfdriver, key, value);
597 swfdriver->setparameter(swfdriver, key, value);
598 imagedriver->setparameter(imagedriver, key, value);
602 // --------------------------------------------------------------------------
606 initLog(0,0,0,0,0,2);
607 pdfdriver = gfxsource_pdf_create();
608 swfdriver = gfxsource_swf_create();
609 imagedriver = gfxsource_image_create();
611 GFX = rb_define_module("GFX");
613 rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
615 DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
616 rb_define_method(DocumentPage, "width", page_width, 0);
617 rb_define_method(DocumentPage, "height", page_height, 0);
618 rb_define_method(DocumentPage, "nr", page_nr, 0);
619 rb_define_method(DocumentPage, "render", page_render, 1);
621 Document = rb_define_class_under(GFX, "Document", rb_cObject);
622 rb_define_method(Document, "initialize", doc_initialize, 1);
623 rb_define_method(Document, "page", doc_get_page, 1);
624 rb_define_method(Document, "each_page", doc_each_page, 0);
626 Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
627 rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
628 rb_define_method(Bitmap, "save_png", image_save_png, 1);
629 rb_define_method(Bitmap, "width", image_width, 0);
630 rb_define_method(Bitmap, "height", image_height, 0);
631 rb_define_method(Bitmap, "rescale", image_rescale, 2);
633 Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
634 rb_define_method(Glyph, "polygon", glyph_polygon, 0);
635 rb_define_method(Glyph, "unicode", glyph_unicode, 0);
636 rb_define_method(Glyph, "advance", glyph_advance, 0);
638 Font = rb_define_class_under(GFX, "Font", rb_cObject);
639 rb_define_method(Font, "name", font_name, 0);
640 rb_define_method(Font, "glyphs", font_glyphs, 0);
642 Device = rb_define_class_under(GFX, "Device", rb_cObject);
643 rb_define_method(Device, "startpage", noop, -1);
644 rb_define_method(Device, "endpage", noop, -1);
645 rb_define_method(Device, "startclip", noop, -1);
646 rb_define_method(Device, "endclip", noop, -1);
647 rb_define_method(Device, "stroke", noop, -1);
648 rb_define_method(Device, "fill", noop, -1);
649 rb_define_method(Device, "fillbitmap", noop, -1);
650 rb_define_method(Device, "fillgradient", noop, -1);
651 rb_define_method(Device, "addfont", noop, -1);
652 rb_define_method(Device, "drawchar", noop, -1);
653 rb_define_method(Device, "drawlink", noop, -1);
654 rb_define_method(Device, "endpage", noop, -1);
656 PDFClass = rb_define_class_under(GFX, "PDF", Document);
657 rb_define_alloc_func(PDFClass, pdf_allocate);
659 SWFClass = rb_define_class_under(GFX, "SWF", Document);
660 rb_define_alloc_func(SWFClass, swf_allocate);
662 ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
663 rb_define_alloc_func(ImageClass, imgdrv_allocate);
665 id_setparameter = rb_intern("setparameter");
666 id_startpage = rb_intern("startpage") ;
667 id_startclip = rb_intern("startclip") ;
668 id_endclip = rb_intern("endclip") ;
669 id_stroke = rb_intern("stroke") ;
670 id_fill = rb_intern("fill") ;
671 id_fillbitmap = rb_intern("fillbitmap") ;
672 id_fillgradient = rb_intern("fillgradient") ;
673 id_addfont = rb_intern("addfont") ;
674 id_drawchar = rb_intern("drawchar") ;
675 id_drawlink = rb_intern("drawlink") ;
676 id_endpage = rb_intern("endpage") ;
677 id_geterror = rb_intern("geterror") ;
678 id_finish = rb_intern("finish") ;
679 id_butt = rb_intern("butt");
680 id_round = rb_intern("round");
681 id_square = rb_intern("square");
682 id_miter = rb_intern("miter");
683 id_bevel = rb_intern("bevel");
684 id_move = rb_intern("move");
685 id_line = rb_intern("line");
686 id_spline = rb_intern("spline");
687 id_radial = rb_intern("radial");
688 id_linear = rb_intern("linear");