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);
67 rb_raise(rb_eIOError, "couldn't open %s", filename);
72 static VALUE doc_num_pages(VALUE cls)
75 return INT2FIX(doc->doc->num_pages);
78 static VALUE doc_get_page(VALUE cls, VALUE _nr)
80 Check_Type(_nr, T_FIXNUM);
81 int nr = FIX2INT(_nr);
84 VALUE v = page_allocate(DocumentPage);
86 page->page = doc->doc->getpage(doc->doc, nr);
89 rb_raise(rb_eArgError, "No page %d in document", nr);
95 static VALUE doc_each_page(VALUE cls)
99 for(t=1;t<=doc->doc->num_pages;t++) {
100 VALUE v = page_allocate(DocumentPage);
102 page->page = doc->doc->getpage(doc->doc, t);
109 static void doc_mark(doc_internal_t*doc)
111 gfxfontlist_t*l = doc->fontlist;
114 rb_gc_mark((VALUE)l->user);
119 static void doc_free(doc_internal_t*doc)
121 gfxfontlist_free(doc->fontlist, 0);
122 doc->doc->destroy(doc->doc);
126 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver)
128 doc_internal_t*doc = 0;
129 VALUE v = Data_Make_Struct(cls, doc_internal_t, doc_mark, doc_free, doc);
131 memset(doc, 0, sizeof(doc_internal_t));
132 doc->driver = driver;
136 static VALUE pdf_allocate(VALUE cls) {return doc_allocate(cls, pdfdriver);}
137 static VALUE swf_allocate(VALUE cls) {return doc_allocate(cls, swfdriver);}
138 static VALUE imgdrv_allocate(VALUE cls) {return doc_allocate(cls, imagedriver);}
140 // ------------------------ doc pages ---------------------------------------
142 static void page_free(page_internal_t*page)
146 page->page->destroy(page->page);
151 static void page_mark(page_internal_t*page)
153 rb_gc_mark(page->doc->self);
155 static VALUE page_allocate(VALUE cls)
157 page_internal_t*page = 0;
158 VALUE v = Data_Make_Struct(cls, page_internal_t, page_mark, page_free, page);
159 memset(page, 0, sizeof(page_internal_t));
162 static VALUE page_nr(VALUE cls)
165 return INT2FIX(page->page->nr);
167 static VALUE page_width(VALUE cls)
170 return INT2FIX(page->page->width);
172 static VALUE page_height(VALUE cls)
175 return INT2FIX(page->page->height);
178 // ------------------------ image -------------------------------------------
180 #define Get_Image(image,cls) image_internal_t*image=0;Data_Get_Struct(cls, image_internal_t, image);
182 static void image_free(image_internal_t*image)
186 static void image_mark(image_internal_t*image)
188 rb_gc_mark(image->doc->self);
190 static VALUE image_allocate(VALUE cls)
192 image_internal_t*image = 0;
193 VALUE v = Data_Make_Struct(cls, image_internal_t, image_mark, image_free, image);
194 memset(image, 0, sizeof(image_internal_t));
197 static VALUE image_width(VALUE cls)
200 return INT2FIX(image->image->width);
202 static VALUE image_height(VALUE cls)
205 return INT2FIX(image->image->height);
207 static VALUE image_rescale(VALUE cls, VALUE _width, VALUE _height)
210 Check_Type(_width, T_FIXNUM);
211 Check_Type(_height, T_FIXNUM);
212 int width = FIX2INT(_width);
213 int height = FIX2INT(_height);
214 volatile VALUE v_image2 = image_allocate(Bitmap);
215 Get_Image(image2,v_image2)
216 image2->doc = image->doc;
217 image2->image = gfximage_rescale(image->image, width, height);
220 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
223 Check_Type(_filename, T_STRING);
224 Check_Type(quality, T_FIXNUM);
225 const char*filename = StringValuePtr(_filename);
226 gfximage_save_jpeg(image->image, filename, FIX2INT(quality));
229 static VALUE image_save_png(VALUE cls, VALUE _filename)
232 Check_Type(_filename, T_STRING);
233 const char*filename = StringValuePtr(_filename);
234 gfximage_save_png(image->image, filename);
237 VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
239 VALUE v = image_allocate(Bitmap);
241 image->image = _image;
245 void invalidate_image(VALUE v)
251 // ------------------------ glyphs ------------------------------------------
253 static VALUE convert_line(gfxline_t*line);
255 #define Get_Glyph(glyph,cls) glyph_internal_t*glyph=0;Data_Get_Struct(cls, glyph_internal_t, glyph);
257 static void glyph_free(glyph_internal_t*glyph)
262 static void glyph_mark(glyph_internal_t*glyph)
264 rb_gc_mark(glyph->font->self);
267 static VALUE glyph_allocate(VALUE cls)
269 glyph_internal_t*glyph = 0;
270 VALUE v = Data_Make_Struct(cls, glyph_internal_t, glyph_mark, glyph_free, glyph);
271 memset(glyph, 0, sizeof(glyph_internal_t));
275 static VALUE glyph_polygon(VALUE cls)
277 Get_Glyph(glyph,cls);
278 return convert_line(glyph->font->font->glyphs[glyph->nr].line);
281 static VALUE glyph_advance(VALUE cls)
283 Get_Glyph(glyph,cls);
284 return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
287 static VALUE glyph_bbox(VALUE cls)
289 Get_Glyph(glyph,cls);
290 gfxbbox_t bbox = gfxline_getbbox(glyph->font->font->glyphs[glyph->nr].line);
291 return rb_ary_new3(4, rb_float_new(bbox.xmin),
292 rb_float_new(bbox.ymin),
293 rb_float_new(bbox.xmax),
294 rb_float_new(bbox.ymax));
297 static VALUE glyph_unicode(VALUE cls)
299 Get_Glyph(glyph,cls);
300 return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
303 // ------------------------ font --------------------------------------------
305 #define Get_Font(font,cls) font_internal_t*font=0;Data_Get_Struct(cls, font_internal_t, font);
307 static void font_mark(font_internal_t*font)
309 rb_gc_mark(font->glyph_array);
312 static void font_free(font_internal_t*font)
317 static VALUE font_allocate(VALUE cls)
319 font_internal_t*font = 0;
320 VALUE v = Data_Make_Struct(cls, font_internal_t, font_mark, font_free, font);
321 memset(font, 0, sizeof(font_internal_t));
326 static VALUE font_ascent(VALUE cls)
329 return rb_float_new(font->font->ascent);
332 static VALUE font_descent(VALUE cls)
335 return rb_float_new(font->font->descent);
338 static VALUE font_name(VALUE cls)
341 return rb_tainted_str_new2(font->font->id);
344 static VALUE font_glyphs(VALUE cls)
347 return font->glyph_array;
350 static VALUE font_kerning(VALUE cls)
353 gfxkerning_t*kerning = font->font->kerning;
354 int kerning_size = font->font->kerning_size;
355 volatile VALUE a = rb_ary_new2(kerning_size);
357 for(t=0;t<kerning_size;t++) {
358 volatile VALUE tuple = rb_ary_new2(3);
359 rb_ary_store(tuple, 0, INT2FIX(kerning[t].c1));
360 rb_ary_store(tuple, 1, INT2FIX(kerning[t].c2));
361 rb_ary_store(tuple, 2, INT2FIX(kerning[t].advance));
362 rb_ary_store(a, t, tuple);
367 // ------------------------ gfx device --------------------------------------
369 typedef struct device_internal {
374 static ID id_setparameter = 0;
375 static ID id_startpage = 0;
376 static ID id_startclip = 0;
377 static ID id_endclip = 0;
378 static ID id_stroke = 0;
379 static ID id_fill = 0;
380 static ID id_fillbitmap = 0;
381 static ID id_fillgradient = 0;
382 static ID id_addfont = 0;
383 static ID id_drawchar = 0;
384 static ID id_drawlink = 0;
385 static ID id_endpage = 0;
386 static ID id_geterror = 0;
387 static ID id_finish = 0;
388 static ID id_butt = 0;
389 static ID id_round = 0;
390 static ID id_square = 0;
391 static ID id_bevel = 0;
392 static ID id_miter = 0;
393 static ID id_move = 0;
394 static ID id_line = 0;
395 static ID id_spline = 0;
396 static ID id_radial = 0;
397 static ID id_linear = 0;
399 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
401 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
403 VALUE convert_line(gfxline_t*line)
407 while(l) {l=l->next;len++;}
409 volatile VALUE array = rb_ary_new2(len);
415 if(l->type == gfx_moveTo) {
416 e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
417 rb_ary_store(array, pos, e);
418 rb_ary_store(e, 1, rb_float_new(l->x));
419 rb_ary_store(e, 2, rb_float_new(l->y));
420 } else if(l->type == gfx_lineTo) {
421 e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
422 rb_ary_store(array, pos, e);
423 rb_ary_store(e, 1, rb_float_new(l->x));
424 rb_ary_store(e, 2, rb_float_new(l->y));
426 e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
427 rb_ary_store(array, pos, e);
428 rb_ary_store(e, 1, rb_float_new(l->x));
429 rb_ary_store(e, 2, rb_float_new(l->y));
430 rb_ary_store(e, 3, rb_float_new(l->sx));
431 rb_ary_store(e, 4, rb_float_new(l->sy));
438 VALUE convert_color(gfxcolor_t*color)
440 return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
442 VALUE convert_matrix(gfxmatrix_t*matrix)
444 volatile VALUE array = rb_ary_new2(3);
445 volatile VALUE a = rb_ary_new2(2);
446 rb_ary_store(array, 0, a);
447 rb_ary_store(a, 0, rb_float_new(matrix->m00));
448 rb_ary_store(a, 1, rb_float_new(matrix->m01));
450 rb_ary_store(array, 1, a);
451 rb_ary_store(a, 0, rb_float_new(matrix->m10));
452 rb_ary_store(a, 1, rb_float_new(matrix->m11));
454 rb_ary_store(array, 2, a);
455 rb_ary_store(a, 0, rb_float_new(matrix->tx));
456 rb_ary_store(a, 1, rb_float_new(matrix->ty));
459 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
461 return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
463 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
465 i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
467 static VALUE convert_font(gfxfont_t*font)
469 volatile VALUE v2 = font_allocate(Font);
472 f->glyph_array = rb_ary_new2(font->num_glyphs);
475 for(t=0;t<font->num_glyphs;t++) {
476 volatile VALUE a = glyph_allocate(Glyph);
477 rb_ary_store(f->glyph_array, t, a);
485 device_internal_t*i = (device_internal_t*)dev->internal; \
487 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
490 volatile VALUE v_key = rb_tainted_str_new2(key);
491 volatile VALUE v_value = rb_tainted_str_new2(value);
492 VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
495 void rb_startpage(gfxdevice_t*dev, int width, int height)
498 VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
500 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
503 volatile VALUE v_line = convert_line(line);
504 VALUE ret = forward(v,id_startclip,1,v_line);
506 void rb_endclip(gfxdevice_t*dev)
509 VALUE ret = forward(v,id_endclip,0);
511 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)
516 if(cap_style == gfx_capButt) cap = id_butt;
517 else if(cap_style == gfx_capRound) cap = id_round;
518 else if(cap_style == gfx_capSquare) cap = id_square;
521 if(joint_style == gfx_joinRound) joint = id_round;
522 else if(joint_style == gfx_joinMiter) joint = id_miter;
523 else if(joint_style == gfx_joinBevel) joint = id_bevel;
525 volatile VALUE v_line = convert_line(line);
526 volatile VALUE v_width = rb_float_new(width);
527 volatile VALUE v_color = convert_color(color);
528 volatile VALUE v_miter = rb_float_new(miterLimit);
529 forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
531 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
535 volatile VALUE v_line = convert_line(line);
536 volatile VALUE v_color = convert_color(color);
537 forward(v, id_fill, 2, v_line, v_color);
539 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
542 volatile VALUE v_image = convert_image(i->doc, img);
543 volatile VALUE v_line = convert_line(line);
544 volatile VALUE v_matrix = convert_matrix(matrix);
545 forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
546 invalidate_image(v_image);
548 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
551 ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
553 volatile VALUE v_line = convert_line(line);
554 volatile VALUE v_matrix = convert_matrix(matrix);
555 volatile VALUE v_gradient = convert_gradient(gradient);
556 forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
558 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
562 volatile VALUE f = font_is_cached(i, font);
563 if(!f) {f=convert_font(font);cache_font(i,font,f);}
565 forward(v, id_addfont, 1, f);
567 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
570 volatile VALUE f = font_is_cached(i, font);
571 if(!f) {f=convert_font(font);cache_font(i,font,f);}
573 volatile VALUE v_color = convert_color(color);
574 volatile VALUE v_matrix = convert_matrix(matrix);
575 forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
577 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
580 volatile VALUE v_line = convert_line(line);
581 volatile VALUE v_action = rb_tainted_str_new2(action);
582 forward(v, id_drawlink, v_line, v_action);
584 void rb_endpage(gfxdevice_t*dev)
587 forward(v, id_endpage, 0);
589 gfxresult_t* rb_finish(gfxdevice_t*dev)
592 VALUE ret = forward(v, id_endpage, 0);
593 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
594 r->internal = (void*)(ptroff_t)ret;
598 static VALUE page_render(VALUE cls, VALUE device)
600 Check_Type(device, T_OBJECT);
609 dev.setparameter = rb_setparameter;
610 dev.startpage = rb_startpage;
611 dev.startclip = rb_startclip;
612 dev.endclip = rb_endclip;
613 dev.stroke = rb_stroke;
615 dev.fillbitmap = rb_fillbitmap;
616 dev.fillgradient = rb_fillgradient;
617 dev.addfont = rb_addfont;
618 dev.drawchar = rb_drawchar;
619 dev.drawlink = rb_drawlink;
620 dev.endpage = rb_endpage;
621 dev.finish = rb_finish;
623 dev.startpage(&dev, page->page->width, page->page->height);
624 page->page->render(page->page, &dev);
630 // ---------------------- global functions ----------------------------------
632 VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value)
634 Check_Type(_key, T_STRING);
635 Check_Type(_value, T_STRING);
636 const char*key = StringValuePtr(_key);
637 const char*value = StringValuePtr(_value);
638 pdfdriver->setparameter(pdfdriver, key, value);
639 swfdriver->setparameter(swfdriver, key, value);
640 imagedriver->setparameter(imagedriver, key, value);
644 // --------------------------------------------------------------------------
648 initLog(0,0,0,0,0,2);
649 pdfdriver = gfxsource_pdf_create();
650 swfdriver = gfxsource_swf_create();
651 imagedriver = gfxsource_image_create();
653 GFX = rb_define_module("GFX");
655 rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2);
657 DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
658 rb_define_method(DocumentPage, "width", page_width, 0);
659 rb_define_method(DocumentPage, "height", page_height, 0);
660 rb_define_method(DocumentPage, "nr", page_nr, 0);
661 rb_define_method(DocumentPage, "render", page_render, 1);
663 Document = rb_define_class_under(GFX, "Document", rb_cObject);
664 rb_define_method(Document, "initialize", doc_initialize, 1);
665 rb_define_method(Document, "page", doc_get_page, 1);
666 rb_define_method(Document, "each_page", doc_each_page, 0);
668 Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
669 rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
670 rb_define_method(Bitmap, "save_png", image_save_png, 1);
671 rb_define_method(Bitmap, "width", image_width, 0);
672 rb_define_method(Bitmap, "height", image_height, 0);
673 rb_define_method(Bitmap, "rescale", image_rescale, 2);
675 Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
676 rb_define_method(Glyph, "polygon", glyph_polygon, 0);
677 rb_define_method(Glyph, "unicode", glyph_unicode, 0);
678 rb_define_method(Glyph, "advance", glyph_advance, 0);
679 rb_define_method(Glyph, "bbox", glyph_bbox, 0);
681 Font = rb_define_class_under(GFX, "Font", rb_cObject);
682 rb_define_method(Font, "name", font_name, 0);
683 rb_define_method(Font, "ascent", font_ascent, 0);
684 rb_define_method(Font, "descent", font_descent, 0);
685 rb_define_method(Font, "glyphs", font_glyphs, 0);
686 rb_define_method(Font, "kerning", font_kerning, 0);
687 rb_define_method(Font, "get_kerning_table", font_kerning, 0);
689 Device = rb_define_class_under(GFX, "Device", rb_cObject);
690 rb_define_method(Device, "startpage", noop, -1);
691 rb_define_method(Device, "endpage", noop, -1);
692 rb_define_method(Device, "startclip", noop, -1);
693 rb_define_method(Device, "endclip", noop, -1);
694 rb_define_method(Device, "stroke", noop, -1);
695 rb_define_method(Device, "fill", noop, -1);
696 rb_define_method(Device, "fillbitmap", noop, -1);
697 rb_define_method(Device, "fillgradient", noop, -1);
698 rb_define_method(Device, "addfont", noop, -1);
699 rb_define_method(Device, "drawchar", noop, -1);
700 rb_define_method(Device, "drawlink", noop, -1);
701 rb_define_method(Device, "endpage", noop, -1);
703 PDFClass = rb_define_class_under(GFX, "PDF", Document);
704 rb_define_alloc_func(PDFClass, pdf_allocate);
706 SWFClass = rb_define_class_under(GFX, "SWF", Document);
707 rb_define_alloc_func(SWFClass, swf_allocate);
709 ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
710 rb_define_alloc_func(ImageClass, imgdrv_allocate);
712 id_setparameter = rb_intern("setparameter");
713 id_startpage = rb_intern("startpage") ;
714 id_startclip = rb_intern("startclip") ;
715 id_endclip = rb_intern("endclip") ;
716 id_stroke = rb_intern("stroke") ;
717 id_fill = rb_intern("fill") ;
718 id_fillbitmap = rb_intern("fillbitmap") ;
719 id_fillgradient = rb_intern("fillgradient") ;
720 id_addfont = rb_intern("addfont") ;
721 id_drawchar = rb_intern("drawchar") ;
722 id_drawlink = rb_intern("drawlink") ;
723 id_endpage = rb_intern("endpage") ;
724 id_geterror = rb_intern("geterror") ;
725 id_finish = rb_intern("finish") ;
726 id_butt = rb_intern("butt");
727 id_round = rb_intern("round");
728 id_square = rb_intern("square");
729 id_miter = rb_intern("miter");
730 id_bevel = rb_intern("bevel");
731 id_move = rb_intern("move");
732 id_line = rb_intern("line");
733 id_spline = rb_intern("spline");
734 id_radial = rb_intern("radial");
735 id_linear = rb_intern("linear");