2 #include "../gfxdevice.h"
3 #include "../gfxsource.h"
4 #include "../gfxtools.h"
5 #include "../devices/pdf.h"
6 #include "../readers/swf.h"
7 #include "../readers/image.h"
8 #include "../pdf/pdf.h"
12 #define RUBY_GFX_VERSION "0.9.0"
15 static VALUE Font, Glyph, Bitmap, Document, DocumentPage, PDFClass, SWFClass, ImageClass, Device;
18 typedef struct doc_internal {
20 gfxsource_t*driver; // filled by alloc
22 gfxfontlist_t*fontlist;
25 typedef struct page_internal {
30 typedef struct image_internal {
35 typedef struct font_internal {
41 typedef struct glyph_internal {
46 static gfxsource_t* pdfdriver = 0;
47 static gfxsource_t* imagedriver = 0;
48 static gfxsource_t* swfdriver = 0;
50 #define Get_Doc(doc,cls) doc_internal_t*doc=0;Data_Get_Struct(cls, doc_internal_t, doc);
51 #define Get_Page(page,cls) page_internal_t*page=0;Data_Get_Struct(cls, page_internal_t, page);
53 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver);
54 static VALUE page_allocate(VALUE cls);
56 // ------------------------ documents ---------------------------------------
58 static VALUE doc_initialize(VALUE cls, VALUE _filename)
60 Check_Type(_filename, T_STRING);
62 const char*filename = StringValuePtr(_filename);
63 doc->fontlist = gfxfontlist_create();
64 doc->doc = pdfdriver->open(pdfdriver, filename);
68 static VALUE doc_num_pages(VALUE cls)
71 return INT2FIX(doc->doc->num_pages);
74 static VALUE doc_get_page(VALUE cls, VALUE _nr)
76 Check_Type(_nr, T_FIXNUM);
77 int nr = FIX2INT(_nr);
80 VALUE v = page_allocate(DocumentPage);
82 page->page = doc->doc->getpage(doc->doc, nr);
85 rb_raise(rb_eArgError, "No page %d in document", nr);
91 static VALUE doc_each_page(VALUE cls)
95 for(t=1;t<=doc->doc->num_pages;t++) {
96 VALUE v = page_allocate(DocumentPage);
98 page->page = doc->doc->getpage(doc->doc, t);
105 static void doc_mark(doc_internal_t*doc)
107 gfxfontlist_t*l = doc->fontlist;
110 rb_gc_mark((VALUE)l->user);
115 static void doc_free(doc_internal_t*doc)
117 gfxfontlist_free(doc->fontlist, 0);
118 doc->doc->destroy(doc->doc);
122 static VALUE doc_allocate(VALUE cls, gfxsource_t*driver)
124 doc_internal_t*doc = 0;
125 VALUE v = Data_Make_Struct(cls, doc_internal_t, doc_mark, doc_free, doc);
127 memset(doc, 0, sizeof(doc_internal_t));
128 doc->driver = driver;
132 static VALUE pdf_allocate(VALUE cls) {return doc_allocate(cls, pdfdriver);}
133 static VALUE swf_allocate(VALUE cls) {return doc_allocate(cls, swfdriver);}
134 static VALUE imgdrv_allocate(VALUE cls) {return doc_allocate(cls, imagedriver);}
136 // ------------------------ doc pages ---------------------------------------
138 static void page_free(page_internal_t*page)
142 page->page->destroy(page->page);
147 static void page_mark(page_internal_t*page)
149 rb_gc_mark(page->doc->self);
151 static VALUE page_allocate(VALUE cls)
153 page_internal_t*page = 0;
154 VALUE v = Data_Make_Struct(cls, page_internal_t, page_mark, page_free, page);
155 memset(page, 0, sizeof(page_internal_t));
158 static VALUE page_nr(VALUE cls)
161 return INT2FIX(page->page->nr);
163 static VALUE page_width(VALUE cls)
166 return INT2FIX(page->page->width);
168 static VALUE page_height(VALUE cls)
171 return INT2FIX(page->page->height);
174 // ------------------------ image -------------------------------------------
176 #define Get_Image(image,cls) image_internal_t*image=0;Data_Get_Struct(cls, image_internal_t, image);
178 static void image_free(image_internal_t*image)
182 static void image_mark(image_internal_t*image)
184 rb_gc_mark(image->doc->self);
186 static VALUE image_allocate(VALUE cls)
188 image_internal_t*image = 0;
189 VALUE v = Data_Make_Struct(cls, image_internal_t, image_mark, image_free, image);
190 memset(image, 0, sizeof(image_internal_t));
193 static VALUE image_width(VALUE cls)
196 return INT2FIX(image->image->width);
198 static VALUE image_height(VALUE cls)
201 return INT2FIX(image->image->height);
203 static VALUE image_save_jpeg(VALUE cls, VALUE _filename, VALUE quality)
206 Check_Type(_filename, T_STRING);
207 Check_Type(quality, T_FIXNUM);
208 const char*filename = StringValuePtr(_filename);
209 int l = strlen(filename);
211 if(l>=4 && !strcmp(&filename[l-4], ".jpg")) jpeg = 1;
212 if(l>=5 && !strcmp(&filename[l-5], ".jpeg")) jpeg = 1;
213 if(l>=4 && !strcmp(&filename[l-4], ".JPG")) jpeg = 1;
214 jpeg_save(image->image->data, image->image->width, image->image->height, FIX2INT(quality), filename);
217 VALUE convert_image(doc_internal_t*doc,gfximage_t*_image)
219 VALUE v = image_allocate(Bitmap);
221 image->image = _image;
225 void invalidate_image(VALUE v)
231 // ------------------------ glyphs ------------------------------------------
233 static VALUE convert_line(gfxline_t*line);
235 #define Get_Glyph(glyph,cls) glyph_internal_t*glyph=0;Data_Get_Struct(cls, glyph_internal_t, glyph);
237 static void glyph_free(glyph_internal_t*glyph)
242 static void glyph_mark(glyph_internal_t*glyph)
244 rb_gc_mark(glyph->font->self);
247 static VALUE glyph_allocate(VALUE cls)
249 glyph_internal_t*glyph = 0;
250 VALUE v = Data_Make_Struct(cls, glyph_internal_t, glyph_mark, glyph_free, glyph);
251 memset(glyph, 0, sizeof(glyph_internal_t));
255 static VALUE glyph_polygon(VALUE cls)
257 Get_Glyph(glyph,cls);
258 return convert_line(glyph->font->font->glyphs[glyph->nr].line);
261 static VALUE glyph_advance(VALUE cls)
263 Get_Glyph(glyph,cls);
264 return rb_float_new(glyph->font->font->glyphs[glyph->nr].advance);
267 static VALUE glyph_unicode(VALUE cls)
269 Get_Glyph(glyph,cls);
270 return INT2FIX(glyph->font->font->glyphs[glyph->nr].unicode);
273 // ------------------------ font --------------------------------------------
275 #define Get_Font(font,cls) font_internal_t*font=0;Data_Get_Struct(cls, font_internal_t, font);
277 static void font_mark(font_internal_t*font)
279 rb_gc_mark(font->glyph_array);
282 static void font_free(font_internal_t*font)
287 static VALUE font_allocate(VALUE cls)
289 font_internal_t*font = 0;
290 VALUE v = Data_Make_Struct(cls, font_internal_t, font_mark, font_free, font);
291 memset(font, 0, sizeof(font_internal_t));
296 static VALUE font_name(VALUE cls)
299 return rb_tainted_str_new2(font->font->id);
302 static VALUE font_glyphs(VALUE cls)
305 return font->glyph_array;
308 // ------------------------ gfx device --------------------------------------
310 typedef struct device_internal {
315 static ID id_setparameter = 0;
316 static ID id_startpage = 0;
317 static ID id_startclip = 0;
318 static ID id_endclip = 0;
319 static ID id_stroke = 0;
320 static ID id_fill = 0;
321 static ID id_fillbitmap = 0;
322 static ID id_fillgradient = 0;
323 static ID id_addfont = 0;
324 static ID id_drawchar = 0;
325 static ID id_drawlink = 0;
326 static ID id_endpage = 0;
327 static ID id_geterror = 0;
328 static ID id_finish = 0;
329 static ID id_butt = 0;
330 static ID id_round = 0;
331 static ID id_square = 0;
332 static ID id_bevel = 0;
333 static ID id_miter = 0;
334 static ID id_move = 0;
335 static ID id_line = 0;
336 static ID id_spline = 0;
337 static ID id_radial = 0;
338 static ID id_linear = 0;
340 static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;}
342 #define forward(v,id,args...) rb_respond_to((v), (id))?rb_funcall((v), (id), args):0
344 VALUE convert_line(gfxline_t*line)
348 while(l) {l=l->next;len++;}
350 volatile VALUE array = rb_ary_new2(len);
356 if(l->type == gfx_moveTo) {
357 e = rb_ary_new3(3, ID2SYM(id_move), Qfalse, Qfalse);
358 rb_ary_store(array, pos, e);
359 rb_ary_store(e, 1, rb_float_new(l->x));
360 rb_ary_store(e, 2, rb_float_new(l->y));
361 } else if(l->type == gfx_lineTo) {
362 e = rb_ary_new3(3, ID2SYM(id_line), Qfalse, Qfalse);
363 rb_ary_store(array, pos, e);
364 rb_ary_store(e, 1, rb_float_new(l->x));
365 rb_ary_store(e, 2, rb_float_new(l->y));
367 e = rb_ary_new3(5, ID2SYM(id_spline), Qfalse, Qfalse, Qfalse, Qfalse);
368 rb_ary_store(array, pos, e);
369 rb_ary_store(e, 1, rb_float_new(l->x));
370 rb_ary_store(e, 2, rb_float_new(l->y));
371 rb_ary_store(e, 3, rb_float_new(l->sx));
372 rb_ary_store(e, 4, rb_float_new(l->sy));
379 VALUE convert_color(gfxcolor_t*color)
381 return rb_ary_new3(4, INT2FIX(color->a), INT2FIX(color->r), INT2FIX(color->g), INT2FIX(color->b));
383 VALUE convert_matrix(gfxmatrix_t*matrix)
385 volatile VALUE array = rb_ary_new2(3);
386 volatile VALUE a = rb_ary_new2(2);
387 rb_ary_store(array, 0, a);
388 rb_ary_store(a, 0, rb_float_new(matrix->m00));
389 rb_ary_store(a, 1, rb_float_new(matrix->m01));
391 rb_ary_store(array, 1, a);
392 rb_ary_store(a, 0, rb_float_new(matrix->m10));
393 rb_ary_store(a, 1, rb_float_new(matrix->m11));
395 rb_ary_store(array, 2, a);
396 rb_ary_store(a, 0, rb_float_new(matrix->tx));
397 rb_ary_store(a, 1, rb_float_new(matrix->ty));
400 static VALUE font_is_cached(device_internal_t*i, gfxfont_t*font)
402 return (VALUE)gfxfontlist_getuserdata(i->doc->fontlist, font->id);
404 static void cache_font(device_internal_t*i, gfxfont_t*font, VALUE v)
406 i->doc->fontlist = gfxfontlist_addfont2(i->doc->fontlist, font, (void*)v);
408 static VALUE convert_font(gfxfont_t*font)
410 volatile VALUE v2 = font_allocate(Font);
413 f->glyph_array = rb_ary_new2(font->num_glyphs);
416 for(t=0;t<font->num_glyphs;t++) {
417 volatile VALUE a = glyph_allocate(Glyph);
418 rb_ary_store(f->glyph_array, t, a);
426 device_internal_t*i = (device_internal_t*)dev->internal; \
428 int rb_setparameter(gfxdevice_t*dev, const char*key, const char*value)
431 volatile VALUE v_key = rb_tainted_str_new2(key);
432 volatile VALUE v_value = rb_tainted_str_new2(value);
433 VALUE ret = forward(v,id_setparameter,2,v_key,v_value);
436 void rb_startpage(gfxdevice_t*dev, int width, int height)
439 VALUE ret = forward(v,id_startpage,2,INT2FIX(width),INT2FIX(height));
441 void rb_startclip(gfxdevice_t*dev, gfxline_t*line)
444 volatile VALUE v_line = convert_line(line);
445 VALUE ret = forward(v,id_startclip,1,v_line);
447 void rb_endclip(gfxdevice_t*dev)
450 VALUE ret = forward(v,id_endclip,0);
452 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)
457 if(cap_style == gfx_capButt) cap = id_butt;
458 else if(cap_style == gfx_capRound) cap = id_round;
459 else if(cap_style == gfx_capSquare) cap = id_square;
462 if(joint_style == gfx_joinRound) joint = id_round;
463 else if(joint_style == gfx_joinMiter) joint = id_miter;
464 else if(joint_style == gfx_joinBevel) joint = id_bevel;
466 volatile VALUE v_line = convert_line(line);
467 volatile VALUE v_width = rb_float_new(width);
468 volatile VALUE v_color = convert_color(color);
469 volatile VALUE v_miter = rb_float_new(miterLimit);
470 forward(v, id_stroke, 6, v_line, v_width, v_color, ID2SYM(cap), ID2SYM(joint), v_miter);
472 void rb_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
476 volatile VALUE v_line = convert_line(line);
477 volatile VALUE v_color = convert_color(color);
478 forward(v, id_fill, 2, v_line, v_color);
480 void rb_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
483 volatile VALUE v_image = convert_image(i->doc, img);
484 volatile VALUE v_line = convert_line(line);
485 volatile VALUE v_matrix = convert_matrix(matrix);
486 forward(v, id_fillbitmap, 4, v_line, v_image, v_matrix, Qnil);
487 invalidate_image(v_image);
489 void rb_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
492 ID typeid = (type == gfxgradient_linear)? id_linear : id_radial;
494 volatile VALUE v_line = convert_line(line);
495 volatile VALUE v_matrix = convert_matrix(matrix);
496 volatile VALUE v_gradient = convert_gradient(gradient);
497 forward(v, id_fillgradient, 4, v_line, v_gradient, ID2SYM(typeid), v_matrix);
499 void rb_addfont(gfxdevice_t*dev, gfxfont_t*font)
503 volatile VALUE f = font_is_cached(i, font);
504 if(!f) {f=convert_font(font);cache_font(i,font,v);}
506 forward(v, id_addfont, 1, f);
508 void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
511 volatile VALUE f = font_is_cached(i, font);
512 if(!f) {f=convert_font(font);cache_font(i,font,v);}
514 volatile VALUE v_color = convert_color(color);
515 volatile VALUE v_matrix = convert_matrix(matrix);
516 forward(v, id_drawchar, 4, f, INT2FIX(glyphnr), v_color, v_matrix);
518 void rb_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
521 volatile VALUE v_line = convert_line(line);
522 volatile VALUE v_action = rb_tainted_str_new2(action);
523 forward(v, id_drawlink, v_line, v_action);
525 void rb_endpage(gfxdevice_t*dev)
528 forward(v, id_endpage, 0);
530 gfxresult_t* rb_finish(gfxdevice_t*dev)
533 VALUE ret = forward(v, id_endpage, 0);
534 gfxresult_t*r = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
535 r->internal = (void*)(ptroff_t)ret;
539 static VALUE page_render(VALUE cls, VALUE device)
541 Check_Type(device, T_OBJECT);
550 dev.setparameter = rb_setparameter;
551 dev.startpage = rb_startpage;
552 dev.startclip = rb_startclip;
553 dev.endclip = rb_endclip;
554 dev.stroke = rb_stroke;
556 dev.fillbitmap = rb_fillbitmap;
557 dev.fillgradient = rb_fillgradient;
558 dev.addfont = rb_addfont;
559 dev.drawchar = rb_drawchar;
560 dev.drawlink = rb_drawlink;
561 dev.endpage = rb_endpage;
562 dev.finish = rb_finish;
564 page->page->render(page->page, &dev);
570 // --------------------------------------------------------------------------
574 initLog(0,0,0,0,0,2);
575 pdfdriver = gfxsource_pdf_create();
576 swfdriver = gfxsource_swf_create();
577 imagedriver = gfxsource_image_create();
579 GFX = rb_define_module("GFX");
581 DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject);
582 rb_define_method(DocumentPage, "width", page_width, 0);
583 rb_define_method(DocumentPage, "height", page_height, 0);
584 rb_define_method(DocumentPage, "nr", page_nr, 0);
585 rb_define_method(DocumentPage, "render", page_render, 1);
587 Document = rb_define_class_under(GFX, "Document", rb_cObject);
588 rb_define_method(Document, "initialize", doc_initialize, 1);
589 rb_define_method(Document, "page", doc_get_page, 1);
590 rb_define_method(Document, "each_page", doc_each_page, 0);
592 Bitmap = rb_define_class_under(GFX, "Bitmap", rb_cObject);
593 rb_define_method(Bitmap, "save_jpeg", image_save_jpeg, 2);
595 Glyph = rb_define_class_under(GFX, "Glyph", rb_cObject);
596 rb_define_method(Glyph, "polygon", glyph_polygon, 0);
597 rb_define_method(Glyph, "unicode", glyph_unicode, 0);
598 rb_define_method(Glyph, "advance", glyph_advance, 0);
600 Font = rb_define_class_under(GFX, "Font", rb_cObject);
601 rb_define_method(Font, "name", font_name, 0);
602 rb_define_method(Font, "glyphs", font_glyphs, 0);
604 Device = rb_define_class_under(GFX, "Device", rb_cObject);
605 rb_define_method(Device, "startpage", noop, -1);
606 rb_define_method(Device, "endpage", noop, -1);
607 rb_define_method(Device, "startclip", noop, -1);
608 rb_define_method(Device, "endclip", noop, -1);
609 rb_define_method(Device, "stroke", noop, -1);
610 rb_define_method(Device, "fill", noop, -1);
611 rb_define_method(Device, "fillbitmap", noop, -1);
612 rb_define_method(Device, "fillgradient", noop, -1);
613 rb_define_method(Device, "addfont", noop, -1);
614 rb_define_method(Device, "drawchar", noop, -1);
615 rb_define_method(Device, "drawlink", noop, -1);
616 rb_define_method(Device, "endpage", noop, -1);
618 PDFClass = rb_define_class_under(GFX, "PDF", Document);
619 rb_define_alloc_func(PDFClass, pdf_allocate);
621 SWFClass = rb_define_class_under(GFX, "SWF", Document);
622 rb_define_alloc_func(SWFClass, swf_allocate);
624 ImageClass = rb_define_class_under(GFX, "ImageRead", Document);
625 rb_define_alloc_func(ImageClass, imgdrv_allocate);
627 id_setparameter = rb_intern("setparameter");
628 id_startpage = rb_intern("startpage") ;
629 id_startclip = rb_intern("startclip") ;
630 id_endclip = rb_intern("endclip") ;
631 id_stroke = rb_intern("stroke") ;
632 id_fill = rb_intern("fill") ;
633 id_fillbitmap = rb_intern("fillbitmap") ;
634 id_fillgradient = rb_intern("fillgradient") ;
635 id_addfont = rb_intern("addfont") ;
636 id_drawchar = rb_intern("drawchar") ;
637 id_drawlink = rb_intern("drawlink") ;
638 id_endpage = rb_intern("endpage") ;
639 id_geterror = rb_intern("geterror") ;
640 id_finish = rb_intern("finish") ;
641 id_butt = rb_intern("butt");
642 id_round = rb_intern("round");
643 id_square = rb_intern("square");
644 id_miter = rb_intern("miter");
645 id_bevel = rb_intern("bevel");
646 id_move = rb_intern("move");
647 id_line = rb_intern("line");
648 id_spline = rb_intern("spline");
649 id_radial = rb_intern("radial");
650 id_linear = rb_intern("linear");