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"
14 #define RUBY_GFX_VERSION "0.9.0"
17 static VALUE Font, Glyph, Bitmap, Document, DocumentPage, PDFClass, SWFClass, ImageClass, Device;
20 typedef struct doc_internal {
22 gfxsource_t*driver; // filled by alloc
24 gfxfontlist_t*fontlist;
27 typedef struct page_internal {
32 typedef struct image_internal {
37 typedef struct font_internal {
43 typedef struct glyph_internal {
48 static gfxsource_t* pdfdriver = 0;
49 static gfxsource_t* imagedriver = 0;
50 static gfxsource_t* swfdriver = 0;
52 #define Get_Doc(doc,cls) doc_internal_t*doc=0;Data_Get_Struct(cls, doc_internal_t, doc);
53 #define Get_Page(page,cls) page_internal_t*page=0;Data_Get_Struct(cls, page_internal_t, page);
55 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver);
56 static VALUE page_allocate(VALUE cls);
58 // ------------------------ documents ---------------------------------------
60 static VALUE doc_initialize(VALUE cls, VALUE _filename)
62 Check_Type(_filename, T_STRING);
64 const char*filename = StringValuePtr(_filename);
65 doc->fontlist = gfxfontlist_create();
66 doc->doc = pdfdriver->open(pdfdriver, filename);
68 rb_raise(rb_eIOError, "couldn't open %s", filename);
73 static VALUE doc_num_pages(VALUE cls)
76 return INT2FIX(doc->doc->num_pages);
79 static VALUE doc_get_page(VALUE cls, VALUE _nr)
81 Check_Type(_nr, T_FIXNUM);
82 int nr = FIX2INT(_nr);
85 VALUE v = page_allocate(DocumentPage);
87 page->page = doc->doc->getpage(doc->doc, nr);
90 rb_raise(rb_eArgError, "No page %d in document", nr);
96 static VALUE doc_each_page(VALUE cls)
100 for(t=1;t<=doc->doc->num_pages;t++) {
101 VALUE v = page_allocate(DocumentPage);
103 page->page = doc->doc->getpage(doc->doc, t);
110 static void doc_mark(doc_internal_t*doc)
112 gfxfontlist_t*l = doc->fontlist;
115 rb_gc_mark((VALUE)l->user);
120 static void doc_free(doc_internal_t*doc)
122 gfxfontlist_free(doc->fontlist, 0);
123 doc->doc->destroy(doc->doc);
127 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver)
129 doc_internal_t*doc = 0;
130 VALUE v = Data_Make_Struct(cls, doc_internal_t, doc_mark, doc_free, doc);
132 memset(doc, 0, sizeof(doc_internal_t));
133 doc->driver = driver;
137 static VALUE pdf_allocate(VALUE cls) {return doc_allocate(cls, pdfdriver);}
138 static VALUE swf_allocate(VALUE cls) {return doc_allocate(cls, swfdriver);}
139 static VALUE imgdrv_allocate(VALUE cls) {return doc_allocate(cls, imagedriver);}
141 // ------------------------ doc pages ---------------------------------------
143 static void page_free(page_internal_t*page)
147 page->page->destroy(page->page);
152 static void page_mark(page_internal_t*page)
154 rb_gc_mark(page->doc->self);
156 static VALUE page_allocate(VALUE cls)
158 page_internal_t*page = 0;
159 VALUE v = Data_Make_Struct(cls, page_internal_t, page_mark, page_free, page);
160 memset(page, 0, sizeof(page_internal_t));
163 static VALUE page_nr(VALUE cls)
166 return INT2FIX(page->page->nr);
168 static VALUE page_width(VALUE cls)
171 return INT2FIX(page->page->width);
173 static VALUE page_height(VALUE cls)
176 return INT2FIX(page->page->height);
179 // ------------------------ image -------------------------------------------
181 #define Get_Image(image,cls) image_internal_t*image=0;Data_Get_Struct(cls, image_internal_t, image);
183 static void image_free(image_internal_t*image)
187 static void image_mark(image_internal_t*image)
189 rb_gc_mark(image->doc->self);
191 static VALUE image_allocate(VALUE cls)
193 image_internal_t*image = 0;
194 VALUE v = Data_Make_Struct(cls, image_internal_t, image_mark, image_free, image);
195 memset(image, 0, sizeof(image_internal_t));
198 static VALUE image_width(VALUE cls)
201 return INT2FIX(image->image->width);
203 static VALUE image_height(VALUE cls)
206 return INT2FIX(image->image->height);
208 static VALUE image_rescale(VALUE cls, VALUE _width, VALUE _height)
211 Check_Type(_width, T_FIXNUM);
212 Check_Type(_height, T_FIXNUM);
213 int width = FIX2INT(_width);
214 int height = FIX2INT(_height);
215 volatile VALUE v_image2 = image_allocate(Bitmap);
216 Get_Image(image2,v_image2)
217 image2->doc = image->doc;
218 image2->image = gfximage_rescale(image->image, width, height);
220 rb_raise(rb_eArgError, "Can't rescale to size %dx%d", width, height);
224 static VALUE image_has_alpha(VALUE cls)
227 int size = image->image->width * image->image->height;
228 gfxcolor_t*data = image->image->data;
230 for(t=0;t<size;t++) {
236 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
239 Check_Type(_filename, T_STRING);
240 Check_Type(quality, T_FIXNUM);
241 const char*filename = StringValuePtr(_filename);
242 gfximage_save_jpeg(image->image, filename, FIX2INT(quality));
245 static VALUE image_save_png(VALUE cls, VALUE _filename)
248 Check_Type(_filename, T_STRING);
249 const char*filename = StringValuePtr(_filename);
250 gfximage_save_png(image->image, filename);
253 VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
255 VALUE v = image_allocate(Bitmap);
257 image->image = _image;
261 void invalidate_image(VALUE v)
267 // ------------------------ glyphs ------------------------------------------
269 static VALUE convert_line(gfxline_t*line);
271 #define Get_Glyph(glyph,cls) glyph_internal_t*glyph=0;Data_Get_Struct(cls, glyph_internal_t, glyph);
273 static void glyph_free(glyph_internal_t*glyph)
278 static void glyph_mark(glyph_internal_t*glyph)
280 rb_gc_mark(glyph->font->self);
283 static VALUE glyph_allocate(VALUE cls)
285 glyph_internal_t*glyph = 0;
286 VALUE v = Data_Make_Struct(cls, glyph_internal_t, glyph_mark, glyph_free, glyph);
287 memset(glyph, 0, sizeof(glyph_internal_t));
291 static VALUE glyph_polygon(VALUE cls)
293 Get_Glyph(glyph,cls);
294 return convert_line(glyph->font->font->glyphs[glyph->nr].line);
297 static VALUE glyph_advance(VALUE cls)
299 Get_Glyph(glyph,cls);
300 return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
303 static VALUE glyph_bbox(VALUE cls)
305 Get_Glyph(glyph,cls);
306 gfxbbox_t bbox = gfxline_getbbox(glyph->font->font->glyphs[glyph->nr].line);
307 return rb_ary_new3(4, rb_float_new(bbox.xmin),
308 rb_float_new(bbox.ymin),
309 rb_float_new(bbox.xmax),
310 rb_float_new(bbox.ymax));
313 static VALUE glyph_unicode(VALUE cls)
315 Get_Glyph(glyph,cls);
316 return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
319 // ------------------------ font --------------------------------------------
321 #define Get_Font(font,cls) font_internal_t*font=0;Data_Get_Struct(cls, font_internal_t, font);
323 static void font_mark(font_internal_t*font)
325 rb_gc_mark(font->glyph_array);
328 static void font_free(font_internal_t*font)
333 static VALUE font_allocate(VALUE cls)
335 font_internal_t*font = 0;
336 VALUE v = Data_Make_Struct(cls, font_internal_t, font_mark, font_free, font);
337 memset(font, 0, sizeof(font_internal_t));
342 static VALUE font_ascent(VALUE cls)
345 return rb_float_new(font->font->ascent);
348 static VALUE font_descent(VALUE cls)
351 return rb_float_new(font->font->descent);
354 static VALUE font_name(VALUE cls)
357 return rb_tainted_str_new2(font->font->id);
360 static VALUE font_glyphs(VALUE cls)
363 return font->glyph_array;
366 static VALUE font_kerning(VALUE cls)
369 gfxkerning_t*kerning = font->font->kerning;
370 int kerning_size = font->font->kerning_size;
371 volatile VALUE a = rb_ary_new2(kerning_size);
373 for(t=0;t<kerning_size;t++) {
374 volatile VALUE tuple = rb_ary_new2(3);
375 rb_ary_store(tuple, 0, INT2FIX(kerning[t].c1));
376 rb_ary_store(tuple, 1, INT2FIX(kerning[t].c2));
377 rb_ary_store(tuple, 2, INT2FIX(kerning[t].advance));
378 rb_ary_store(a, t, tuple);
383 // ------------------------ gfx device --------------------------------------
385 typedef struct device_internal {
390 static ID id_setparameter = 0;
391 static ID id_startpage = 0;
392 static ID id_startclip = 0;
393 static ID id_endclip = 0;
394 static ID id_stroke = 0;
395 static ID id_fill = 0;
396 static ID id_fillbitmap = 0;
397 static ID id_fillgradient = 0;
398 static ID id_addfont = 0;
399 static ID id_drawchar = 0;
400 static ID id_drawlink = 0;
401 static ID id_endpage = 0;
402 static ID id_geterror = 0;
403 static ID id_finish = 0;
404 static ID id_butt = 0;
405 static ID id_round = 0;
406 static ID id_square = 0;
407 static ID id_bevel = 0;
408 static ID id_miter = 0;
409 static ID id_move = 0;
410 static ID id_line = 0;
411 static ID id_spline = 0;
412 static ID id_radial = 0;
413 static ID id_linear = 0;
415 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
417 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
419 VALUE convert_line(gfxline_t*line)
423 while(l) {l=l->next;len++;}
425 volatile VALUE array = rb_ary_new2(len);
431 if(l->type == gfx_moveTo) {
432 e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
433 rb_ary_store(array, pos, e);
434 rb_ary_store(e, 1, rb_float_new(l->x));
435 rb_ary_store(e, 2, rb_float_new(l->y));
436 } else if(l->type == gfx_lineTo) {
437 e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
438 rb_ary_store(array, pos, e);
439 rb_ary_store(e, 1, rb_float_new(l->x));
440 rb_ary_store(e, 2, rb_float_new(l->y));
442 e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
443 rb_ary_store(array, pos, e);
444 rb_ary_store(e, 1, rb_float_new(l->x));
445 rb_ary_store(e, 2, rb_float_new(l->y));
446 rb_ary_store(e, 3, rb_float_new(l->sx));
447 rb_ary_store(e, 4, rb_float_new(l->sy));
454 VALUE convert_color(gfxcolor_t*color)
456 return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
458 VALUE convert_matrix(gfxmatrix_t*matrix)
460 volatile VALUE array = rb_ary_new2(3);
461 volatile VALUE a = rb_ary_new2(2);
462 rb_ary_store(array, 0, a);
463 rb_ary_store(a, 0, rb_float_new(matrix->m00));
464 rb_ary_store(a, 1, rb_float_new(matrix->m01));
466 rb_ary_store(array, 1, a);
467 rb_ary_store(a, 0, rb_float_new(matrix->m10));
468 rb_ary_store(a, 1, rb_float_new(matrix->m11));
470 rb_ary_store(array, 2, a);
471 rb_ary_store(a, 0, rb_float_new(matrix->tx));
472 rb_ary_store(a, 1, rb_float_new(matrix->ty));
475 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
477 return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
479 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
481 i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
483 static VALUE convert_font(gfxfont_t*font)
485 volatile VALUE v2 = font_allocate(Font);
488 f->glyph_array = rb_ary_new2(font->num_glyphs);
491 for(t=0;t<font->num_glyphs;t++) {
492 volatile VALUE a = glyph_allocate(Glyph);
493 rb_ary_store(f->glyph_array, t, a);
500 static VALUE convert_gradient(gfxgradient_t*gradient)
505 device_internal_t*i = (device_internal_t*)dev->internal; \
507 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
510 volatile VALUE v_key = rb_tainted_str_new2(key);
511 volatile VALUE v_value = rb_tainted_str_new2(value);
512 VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
515 void rb_startpage(gfxdevice_t*dev, int width, int height)
518 VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
520 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
523 volatile VALUE v_line = convert_line(line);
524 VALUE ret = forward(v,id_startclip,1,v_line);
526 void rb_endclip(gfxdevice_t*dev)
529 VALUE ret = forward(v,id_endclip,0);
531 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)
536 if(cap_style == gfx_capButt) cap = id_butt;
537 else if(cap_style == gfx_capRound) cap = id_round;
538 else if(cap_style == gfx_capSquare) cap = id_square;
541 if(joint_style == gfx_joinRound) joint = id_round;
542 else if(joint_style == gfx_joinMiter) joint = id_miter;
543 else if(joint_style == gfx_joinBevel) joint = id_bevel;
545 volatile VALUE v_line = convert_line(line);
546 volatile VALUE v_width = rb_float_new(width);
547 volatile VALUE v_color = convert_color(color);
548 volatile VALUE v_miter = rb_float_new(miterLimit);
549 forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
551 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
555 volatile VALUE v_line = convert_line(line);
556 volatile VALUE v_color = convert_color(color);
557 forward(v, id_fill, 2, v_line, v_color);
559 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
562 volatile VALUE v_image = convert_image(i->doc, img);
563 volatile VALUE v_line = convert_line(line);
564 volatile VALUE v_matrix = convert_matrix(matrix);
565 forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
566 invalidate_image(v_image);
568 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
571 ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
573 volatile VALUE v_line = convert_line(line);
574 volatile VALUE v_matrix = convert_matrix(matrix);
575 volatile VALUE v_gradient = convert_gradient(gradient);
576 forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
578 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
582 volatile VALUE f = font_is_cached(i, font);
583 if(!f) {f=convert_font(font);cache_font(i,font,f);}
585 forward(v, id_addfont, 1, f);
587 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
590 volatile VALUE f = font_is_cached(i, font);
591 if(!f) {f=convert_font(font);cache_font(i,font,f);}
593 volatile VALUE v_color = convert_color(color);
594 volatile VALUE v_matrix = convert_matrix(matrix);
595 forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
597 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
600 volatile VALUE v_line = convert_line(line);
601 volatile VALUE v_action = rb_tainted_str_new2(action);
602 forward(v, id_drawlink, v_line, v_action);
604 void rb_endpage(gfxdevice_t*dev)
607 forward(v, id_endpage, 0);
609 gfxresult_t* rb_finish(gfxdevice_t*dev)
612 VALUE ret = forward(v, id_endpage, 0);
613 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
614 r->internal = (void*)(ptroff_t)ret;
618 static VALUE page_render(VALUE cls, VALUE device)
620 Check_Type(device, T_OBJECT);
629 dev.setparameter = rb_setparameter;
630 dev.startpage = rb_startpage;
631 dev.startclip = rb_startclip;
632 dev.endclip = rb_endclip;
633 dev.stroke = rb_stroke;
635 dev.fillbitmap = rb_fillbitmap;
636 dev.fillgradient = rb_fillgradient;
637 dev.addfont = rb_addfont;
638 dev.drawchar = rb_drawchar;
639 dev.drawlink = rb_drawlink;
640 dev.endpage = rb_endpage;
641 dev.finish = rb_finish;
643 dev.startpage(&dev, page->page->width, page->page->height);
644 page->page->render(page->page, &dev);
650 static VALUE doc_prepare(VALUE cls, VALUE device)
660 dev.setparameter = rb_setparameter;
661 dev.startpage = rb_startpage;
662 dev.startclip = rb_startclip;
663 dev.endclip = rb_endclip;
664 dev.stroke = rb_stroke;
666 dev.fillbitmap = rb_fillbitmap;
667 dev.fillgradient = rb_fillgradient;
668 dev.addfont = rb_addfont;
669 dev.drawchar = rb_drawchar;
670 dev.drawlink = rb_drawlink;
671 dev.endpage = rb_endpage;
672 dev.finish = rb_finish;
674 doc->doc->prepare(doc->doc, &dev);
679 // ---------------------- global functions ----------------------------------
681 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
683 Check_Type(_key, T_STRING);
684 Check_Type(_value, T_STRING);
685 const char*key = StringValuePtr(_key);
686 const char*value = StringValuePtr(_value);
687 pdfdriver->setparameter(pdfdriver, key, value);
688 swfdriver->setparameter(swfdriver, key, value);
689 imagedriver->setparameter(imagedriver, key, value);
693 // --------------------------------------------------------------------------
697 initLog(0,0,0,0,0,2);
698 pdfdriver = gfxsource_pdf_create();
699 swfdriver = gfxsource_swf_create();
700 imagedriver = gfxsource_image_create();
702 GFX = rb_define_module("GFX");
704 rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
706 DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
707 rb_define_method(DocumentPage, "width", page_width, 0);
708 rb_define_method(DocumentPage, "height", page_height, 0);
709 rb_define_method(DocumentPage, "nr", page_nr, 0);
710 rb_define_method(DocumentPage, "render", page_render, 1);
712 Document = rb_define_class_under(GFX, "Document", rb_cObject);
713 rb_define_method(Document, "initialize", doc_initialize, 1);
714 rb_define_method(Document, "page", doc_get_page, 1);
715 rb_define_method(Document, "each_page", doc_each_page, 0);
716 rb_define_method(Document, "prepare", doc_prepare, 1);
718 Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
719 rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
720 rb_define_method(Bitmap, "save_png", image_save_png, 1);
721 rb_define_method(Bitmap, "width", image_width, 0);
722 rb_define_method(Bitmap, "height", image_height, 0);
723 rb_define_method(Bitmap, "rescale", image_rescale, 2);
724 rb_define_method(Bitmap, "has_alpha", image_has_alpha, 0);
726 Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
727 rb_define_method(Glyph, "polygon", glyph_polygon, 0);
728 rb_define_method(Glyph, "unicode", glyph_unicode, 0);
729 rb_define_method(Glyph, "advance", glyph_advance, 0);
730 rb_define_method(Glyph, "bbox", glyph_bbox, 0);
732 Font = rb_define_class_under(GFX, "Font", rb_cObject);
733 rb_define_method(Font, "name", font_name, 0);
734 rb_define_method(Font, "ascent", font_ascent, 0);
735 rb_define_method(Font, "descent", font_descent, 0);
736 rb_define_method(Font, "glyphs", font_glyphs, 0);
737 rb_define_method(Font, "kerning", font_kerning, 0);
738 rb_define_method(Font, "get_kerning_table", font_kerning, 0);
740 Device = rb_define_class_under(GFX, "Device", rb_cObject);
741 rb_define_method(Device, "startpage", noop, -1);
742 rb_define_method(Device, "endpage", noop, -1);
743 rb_define_method(Device, "startclip", noop, -1);
744 rb_define_method(Device, "endclip", noop, -1);
745 rb_define_method(Device, "stroke", noop, -1);
746 rb_define_method(Device, "fill", noop, -1);
747 rb_define_method(Device, "fillbitmap", noop, -1);
748 rb_define_method(Device, "fillgradient", noop, -1);
749 rb_define_method(Device, "addfont", noop, -1);
750 rb_define_method(Device, "drawchar", noop, -1);
751 rb_define_method(Device, "drawlink", noop, -1);
752 rb_define_method(Device, "endpage", noop, -1);
754 PDFClass = rb_define_class_under(GFX, "PDF", Document);
755 rb_define_alloc_func(PDFClass, pdf_allocate);
757 SWFClass = rb_define_class_under(GFX, "SWF", Document);
758 rb_define_alloc_func(SWFClass, swf_allocate);
760 ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
761 rb_define_alloc_func(ImageClass, imgdrv_allocate);
763 id_setparameter = rb_intern("setparameter");
764 id_startpage = rb_intern("startpage") ;
765 id_startclip = rb_intern("startclip") ;
766 id_endclip = rb_intern("endclip") ;
767 id_stroke = rb_intern("stroke") ;
768 id_fill = rb_intern("fill") ;
769 id_fillbitmap = rb_intern("fillbitmap") ;
770 id_fillgradient = rb_intern("fillgradient") ;
771 id_addfont = rb_intern("addfont") ;
772 id_drawchar = rb_intern("drawchar") ;
773 id_drawlink = rb_intern("drawlink") ;
774 id_endpage = rb_intern("endpage") ;
775 id_geterror = rb_intern("geterror") ;
776 id_finish = rb_intern("finish") ;
777 id_butt = rb_intern("butt");
778 id_round = rb_intern("round");
779 id_square = rb_intern("square");
780 id_miter = rb_intern("miter");
781 id_bevel = rb_intern("bevel");
782 id_move = rb_intern("move");
783 id_line = rb_intern("line");
784 id_spline = rb_intern("spline");
785 id_radial = rb_intern("radial");
786 id_linear = rb_intern("linear");