From 0d226aa8874ab6d598c8429ab655edee14a72126 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Wed, 16 Dec 2009 15:54:38 -0800 Subject: [PATCH] initial prototype of ruby interface --- lib/Makefile.in | 13 ++-- lib/as3/notes.txt | 3 +- lib/pdf/Makefile.in | 20 ++--- lib/ruby/extconf.rb | 21 ++++++ lib/ruby/gfx.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ruby/test.rb | 15 ++++ src/Makefile.in | 2 +- 7 files changed, 258 insertions(+), 18 deletions(-) create mode 100644 lib/ruby/extconf.rb create mode 100644 lib/ruby/gfx.c create mode 100644 lib/ruby/test.rb diff --git a/lib/Makefile.in b/lib/Makefile.in index b694c81..f075ae4 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -3,7 +3,7 @@ srcdir = @srcdir@ top_srcdir = @top_srcdir@ include ../Makefile.common -all: librfxswf$(A) libpdf$(A) libbase$(A) libgfx$(A) libgfxswf$(A) libocr$(A) +all: librfxswf$(A) libgfxpdf$(A) libbase$(A) libgfx$(A) libgfxswf$(A) libocr$(A) lame_objects = lame/psymodel.$(O) lame/fft.$(O) lame/newmdct.$(O) lame/quantize.$(O) lame/takehiro.$(O) lame/reservoir.$(O) lame/quantize_pvt.$(O) lame/vbrquantize.$(O) lame/encoder.$(O) lame/id3tag.$(O) lame/version.$(O) lame/tables.$(O) lame/util.$(O) lame/bitstream.$(O) lame/set_get.$(O) lame/VbrTag.$(O) lame/lame.$(O) lame_in_source = @lame_in_source@ @@ -18,7 +18,7 @@ gfxpoly_objects = gfxpoly/active.$(O) gfxpoly/convert.$(O) gfxpoly/poly.$(O) gfx rfxswf_modules = modules/swfbits.c modules/swfaction.c modules/swfdump.c modules/swfcgi.c modules/swfbutton.c modules/swftext.c modules/swffont.c modules/swftools.c modules/swfsound.c modules/swfshape.c modules/swfobject.c modules/swfdraw.c modules/swffilter.c modules/swfrender.c h.263/swfvideo.c modules/swfalignzones.c -base_objects=q.$(O) utf8.$(O) png.$(O) jpeg.$(O) wav.$(O) mp3.$(O) os.$(O) bitio.$(O) log.$(O) mem.$(O) +base_objects=q.$(O) utf8.$(O) png.$(O) jpeg.$(O) wav.$(O) mp3.$(O) os.$(O) bitio.$(O) log.$(O) mem.$(O) MD5.$(O) gfx_objects=gfxtools.$(O) gfxfont.$(O) devices/dummy.$(O) devices/file.$(O) devices/render.$(O) devices/text.$(O) devices/record.$(O) devices/ops.$(O) devices/polyops.$(O) devices/bbox.$(O) devices/rescale.$(O) @DEVICE_OPENGL@ @DEVICE_PDF@ rfxswf_objects=modules/swfaction.$(O) modules/swfbits.$(O) modules/swfbutton.$(O) modules/swfcgi.$(O) modules/swfdraw.$(O) modules/swfdump.$(O) modules/swffilter.$(O) modules/swffont.$(O) modules/swfobject.$(O) modules/swfrender.$(O) modules/swfshape.$(O) modules/swfsound.$(O) modules/swftext.$(O) modules/swftools.$(O) modules/swfalignzones.$(O) @@ -96,6 +96,7 @@ MD5.$(O): MD5.c MD5.h $(C) MD5.c -o $@ log.$(O): log.c log.h $(C) log.c -o $@ + rfxswf.$(O): rfxswf.c rfxswf.h drawer.h bitio.h log.h MD5.h $(top_builddir)/config.h $(C) rfxswf.c -o $@ @@ -157,12 +158,12 @@ libgfx$(A): $(gfx_objects) $(gfxpoly_objects) Makefile $(AR) r libgfx$(A) $(gfx_objects) $(gfxpoly_objects) $(RANLIB) libgfx$(A) -librfxswf$(A): Makefile $(rfxswf_objects) rfxswf.$(O) drawer.$(O) MD5.$(O) $(lame_in_source) $(h263_objects) $(as12compiler_in_source) $(as3compiler_objects) Makefile - $(AR) r librfxswf$(A) $(rfxswf_objects) rfxswf.$(O) drawer.$(O) MD5.$(O) $(lame_in_source) $(h263_objects) $(as12compiler_in_source) $(as3compiler_objects) +librfxswf$(A): Makefile $(rfxswf_objects) rfxswf.$(O) drawer.$(O) $(lame_in_source) $(h263_objects) $(as12compiler_in_source) $(as3compiler_objects) Makefile + $(AR) r librfxswf$(A) $(rfxswf_objects) rfxswf.$(O) drawer.$(O) $(lame_in_source) $(h263_objects) $(as12compiler_in_source) $(as3compiler_objects) $(RANLIB) librfxswf$(A) -libpdf$(A): pdf/GFXOutputDev.cc pdf/GFXOutputDev.h pdf/pdf.cc pdf/pdf.h - cd pdf;$(MAKE) libpdf +libgfxpdf$(A): pdf/GFXOutputDev.cc pdf/GFXOutputDev.h pdf/pdf.cc pdf/pdf.h + cd pdf;$(MAKE) libgfxpdf tests: png.test.c $(L) png.test.c -o png.test $(LIBS) diff --git a/lib/as3/notes.txt b/lib/as3/notes.txt index 200f980..5e2850e 100644 --- a/lib/as3/notes.txt +++ b/lib/as3/notes.txt @@ -66,7 +66,8 @@ code needed for this: - [calculate Math.random(100), gives us two stack values] + push a + calculate Math.random(100), push dup2 [a.k.a. setlocal tmp, dup , getlocal tmp, swap, getlocal tmp] getproperty (consumes two stack values) [code for adding 10] diff --git a/lib/pdf/Makefile.in b/lib/pdf/Makefile.in index 55393a2..c316046 100644 --- a/lib/pdf/Makefile.in +++ b/lib/pdf/Makefile.in @@ -4,11 +4,11 @@ srcdir = @srcdir@ top_srcdir = @top_srcdir@ include ../../Makefile.common -all: ../libpdf$(A) pdf2swf$(E) +all: ../libgfxpdf$(A) pdf2swf$(E) -libpdf: ../libpdf$(A) +libgfxpdf: ../libgfxpdf$(A) -libpdf_objects = GFXOutputDev.$(O) InfoOutputDev.$(O) BitmapOutputDev.$(O) FullBitmapOutputDev.$(O) XMLOutputDev.$(O) pdf.$(O) fonts.$(O) +libgfxpdf_objects = GFXOutputDev.$(O) InfoOutputDev.$(O) BitmapOutputDev.$(O) FullBitmapOutputDev.$(O) XMLOutputDev.$(O) pdf.$(O) fonts.$(O) xpdf_in_source = @xpdf_in_source@ @@ -149,9 +149,9 @@ xpdf/%.$(O): xpdf/%.cc $(CC) -I ./ -I xpdf $< -o $@ -../libpdf$(A): $(XPDFOK) $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) - $(AR) r ../libpdf$(A) $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) - $(RANLIB) ../libpdf$(A) +../libgfxpdf$(A): $(XPDFOK) $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) + $(AR) r ../libgfxpdf$(A) $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) + $(RANLIB) ../libgfxpdf$(A) xpdfapp_sources=xpdf/XPDFApp.cc xpdf/XPDFCore.cc xpdf/XPDFTree.cc xpdf/XPDFViewer.cc xpdf/PDFCore.cc xpdf/TextOutputDev.cc xpdf/xpdf.cc xpdf/CoreOutputDev.cc xpdf/UnicodeTypeTable.cc xpdf/PSOutputDev.cc splash_sources=xpdf/Splash.cc xpdf/SplashBitmap.cc xpdf/SplashClip.cc xpdf/SplashFTFont.cc xpdf/SplashFTFontEngine.cc xpdf/SplashFTFontFile.cc xpdf/SplashFont.cc xpdf/SplashFontEngine.cc xpdf/SplashFontFile.cc xpdf/SplashFontFileID.cc xpdf/SplashOutputDev.cc xpdf/SplashPath.cc xpdf/SplashPattern.cc xpdf/SplashScreen.cc xpdf/SplashState.cc xpdf/SplashT1Font.cc xpdf/SplashT1FontEngine.cc xpdf/SplashT1FontFile.cc xpdf/SplashXPath.cc xpdf/SplashXPathScanner.cc @@ -171,10 +171,10 @@ pdfinfo$(E): $(XPDFOK) xpdf/pdfinfo.cc $(xpdf_objects) gfx_objects = ../libgfxswf$(A) ../libgfx$(A) ../librfxswf$(A) ../libbase$(A) gfx_objects2 = $(gfx_objects) ../devices/lrf.$(O) ../libocr$(A) -pdf2swf$(E): $(XPDFOK) ../../src/pdf2swf.c $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) - $(LL) $(CPPFLAGS) -g ../../src/pdf2swf.c $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) -o pdf2swf$(E) $(LIBS) -gfx2gfx$(E): $(XPDFOK) ../../src/gfx2gfx.c $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects2) - $(LL) $(CPPFLAGS) -g ../../src/gfx2gfx.c $(libpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects2) -o gfx2gfx$(E) $(LIBS) +pdf2swf$(E): $(XPDFOK) ../../src/pdf2swf.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) + $(LL) $(CPPFLAGS) -g ../../src/pdf2swf.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects) -o pdf2swf$(E) $(LIBS) +gfx2gfx$(E): $(XPDFOK) ../../src/gfx2gfx.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects2) + $(LL) $(CPPFLAGS) -g ../../src/gfx2gfx.c $(libgfxpdf_objects) $(xpdf_in_source) $(splash_in_source) $(gfx_objects2) -o gfx2gfx$(E) $(LIBS) install: $(mkinstalldirs) $(bindir) diff --git a/lib/ruby/extconf.rb b/lib/ruby/extconf.rb new file mode 100644 index 0000000..5a6d41c --- /dev/null +++ b/lib/ruby/extconf.rb @@ -0,0 +1,21 @@ +require 'mkmf' +require 'rbconfig' + +$LDFLAGS << " -L.. " +$LIBS << " -lstdc++ " + +dir_config 'gfx' + +have_library("z", "deflate") +have_library("m", "sqrt") +have_library("jpeg", "jpeg_write_raw_data") +have_library("fftw3", "fftw_plan_dft_r2c_2d") +have_library("freetype", "FT_Init_FreeType") +have_library("fontconfig", "FcInit") +have_library("base", "ringbuffer_init") +have_library("gfx", "gfxfontlist_create") +have_library("gfxpdf", "gfxsource_pdf_create") +have_library("rfxswf", "swf_ReadSWF") +have_library("gfxswf", "gfxdevice_swf_init") + +create_makefile('gfx') diff --git a/lib/ruby/gfx.c b/lib/ruby/gfx.c new file mode 100644 index 0000000..768277d --- /dev/null +++ b/lib/ruby/gfx.c @@ -0,0 +1,202 @@ +#include +#include "../gfxdevice.h" +#include "../gfxsource.h" +#include "../devices/pdf.h" +#include "../readers/swf.h" +#include "../readers/image.h" +#include "../pdf/pdf.h" +#include "../types.h" + +#define RUBY_GFX_VERSION "0.9.0" + +static VALUE GFX; +static VALUE Document, DocumentPage, PDFClass, SWFClass, ImageClass, Device; +static ID id_doc; + +typedef struct doc_internal { + gfxsource_t*driver; // filled by alloc + gfxdocument_t*doc; +} doc_internal_t; + +typedef struct page_internal { + gfxpage_t*page; +} page_internal_t; + +static gfxsource_t* pdfdriver = 0; +static gfxsource_t* imagedriver = 0; +static gfxsource_t* swfdriver = 0; + +#define Get_Doc(doc,cls) doc_internal_t*doc=0;Data_Get_Struct(cls, doc_internal_t, doc); +#define Get_Page(page,cls) page_internal_t*page=0;Data_Get_Struct(cls, page_internal_t, page); + +static VALUE doc_allocate(VALUE cls, gfxsource_t*driver); +static VALUE page_allocate(VALUE cls); + +// ------------------------ documents --------------------------------------- + +static VALUE doc_initialize(VALUE cls, VALUE _filename) +{ + Check_Type(_filename, T_STRING); + Get_Doc(doc,cls); + const char*filename = StringValuePtr(_filename); + doc->doc = pdfdriver->open(pdfdriver, filename); + return cls; +} + +static VALUE doc_num_pages(VALUE cls) +{ + Get_Doc(doc,cls) + return INT2FIX(doc->doc->num_pages); +} + + +static VALUE doc_get_page(VALUE cls, VALUE _nr) +{ + Check_Type(_nr, T_FIXNUM); + int nr = FIX2INT(_nr); + Get_Doc(doc,cls); + + VALUE v = page_allocate(DocumentPage); + Get_Page(page,v) + page->page = doc->doc->getpage(doc->doc, nr); + return v; +} + +static VALUE doc_each_page(VALUE cls) +{ + Get_Doc(doc,cls); + int t; + for(t=1;t<=doc->doc->num_pages;t++) { + VALUE v = page_allocate(DocumentPage); + Get_Page(page,v) + page->page = doc->doc->getpage(doc->doc, t); + rb_yield(v); + } + return cls; +} + +static void doc_mark(doc_internal_t*doc) +{ + // e.g. rb_gc_mark(z->page); +} + +static void doc_free(doc_internal_t*doc) +{ + doc->doc->destroy(doc->doc); + free(doc); +} + +static VALUE doc_allocate(VALUE cls, gfxsource_t*driver) +{ + doc_internal_t*doc = 0; + VALUE v = Data_Make_Struct(cls, doc_internal_t, 0, doc_free, doc); + memset(doc, 0, sizeof(doc_internal_t)); + doc->driver = driver; + return v; +} + +static VALUE pdf_allocate(VALUE cls) {return doc_allocate(cls, pdfdriver);} +static VALUE swf_allocate(VALUE cls) {return doc_allocate(cls, swfdriver);} +static VALUE image_allocate(VALUE cls) {return doc_allocate(cls, imagedriver);} + +// ------------------------ doc pages --------------------------------------- + +static VALUE page_free(VALUE cls) +{ + Get_Page(page, cls); + page->page->destroy(page->page); + free(page); + return cls; +} + +static VALUE page_allocate(VALUE cls) +{ + page_internal_t*page = 0; + VALUE v = Data_Make_Struct(cls, page_internal_t, 0, page_free, page); + memset(page, 0, sizeof(page_internal_t)); + return v; +} + +static VALUE page_render(VALUE cls, VALUE device) +{ + Check_Type(device, T_CLASS); + Get_Page(page,cls) + + gfxdevice_t dev; + dev.internal = (void*)(ptroff_t)device; + + page->page->render(page->page, 0);//device); + //rb_yield(v); +} + +static VALUE page_nr(VALUE cls) +{ + Get_Page(page,cls) + return INT2FIX(page->page->nr); +} + +static VALUE page_width(VALUE cls) +{ + Get_Page(page,cls) + return INT2FIX(page->page->width); +} + +static VALUE page_height(VALUE cls) +{ + Get_Page(page,cls) + return INT2FIX(page->page->height); +} + +// ------------------------ gfx device -------------------------------------- + +static VALUE noop(int argc, VALUE *argv, VALUE obj) {return obj;} + +// -------------------------------------------------------------------------- + +void Init_gfx() +{ + initLog(0,0,0,0,0,2); + pdfdriver = gfxsource_pdf_create(); + swfdriver = gfxsource_swf_create(); + imagedriver = gfxsource_image_create(); + + GFX = rb_define_module("GFX"); + + DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject); + rb_define_alloc_func(DocumentPage, page_allocate); + rb_define_method(DocumentPage, "width", page_width, 0); + rb_define_method(DocumentPage, "height", page_height, 0); + rb_define_method(DocumentPage, "nr", page_nr, 0); + rb_define_method(DocumentPage, "render", page_render, 0); + + Document = rb_define_class_under(GFX, "Document", rb_cObject); + rb_define_method(Document, "initialize", doc_initialize, 1); + rb_define_method(Document, "page", doc_get_page, 1); + rb_define_method(Document, "each_page", doc_each_page, 0); + + Device = rb_define_class_under(GFX, "Device", rb_cObject); + rb_define_method(Device, "startpage", noop, -1); + rb_define_method(Device, "endpage", noop, -1); + rb_define_method(Device, "startclip", noop, -1); + rb_define_method(Device, "endclip", noop, -1); + rb_define_method(Device, "stroke", noop, -1); + rb_define_method(Device, "fill", noop, -1); + rb_define_method(Device, "fillbitmap", noop, -1); + rb_define_method(Device, "fillgradient", noop, -1); + rb_define_method(Device, "addfont", noop, -1); + rb_define_method(Device, "drawchar", noop, -1); + rb_define_method(Device, "drawlink", noop, -1); + rb_define_method(Device, "endpage", noop, -1); + + PDFClass = rb_define_class_under(GFX, "PDF", Document); + rb_define_alloc_func(PDFClass, pdf_allocate); + + SWFClass = rb_define_class_under(GFX, "SWF", Document); + rb_define_alloc_func(SWFClass, swf_allocate); + + ImageClass = rb_define_class_under(GFX, "Image", Document); + rb_define_alloc_func(ImageClass, image_allocate); + + //id_doc = rb_intern("doc"); +} + diff --git a/lib/ruby/test.rb b/lib/ruby/test.rb new file mode 100644 index 0000000..b235ffc --- /dev/null +++ b/lib/ruby/test.rb @@ -0,0 +1,15 @@ +require 'gfx' + +pdf = GFX::PDF.new('test.pdf') + +pdf.each_page do |page| + puts "#{page.nr} #{page.width}x#{page.height}" +end + +class TestRender < GFX::Device + def stroke(line, width, color, capstyle, jointstyle, miterlimit) + p line + end +end + +pdf.page(3).render(TestRender.new) diff --git a/src/Makefile.in b/src/Makefile.in index 0c1381d..1fbc554 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -56,7 +56,7 @@ parser.$(O): parser.yy.c parser.h ../lib/q.h ../lib/librfxswf$(A): ../lib/modules/swfrender.c ../lib/modules/swfshape.c ../lib/modules/swftext.c ../lib/modules/swffont.c ../lib/modules/swfbits.c ../lib/rfxswf.c ../lib/devices/swf.c ../lib/modules/swfalignzones.c cd ../lib;$(MAKE) librfxswf$(A);cd - -../lib/libpdf$(A): ../lib/pdf/GFXOutputDev.cc +../lib/libgfxpdf$(A): ../lib/pdf/GFXOutputDev.cc cd ../lib;$(MAKE) libpdf$(A);cd - ../lib/libgfx$(A): ../lib/devices/*.c -- 1.7.10.4