From 6c8f037f0f76ce4b28b80a2133e4150d93012ef7 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Mon, 21 Dec 2009 17:57:05 -0800 Subject: [PATCH] more parameter refactoring --- lib/gfxsource.h | 4 +-- lib/gfxtools.c | 43 ++++++++++++++++++++++++++ lib/gfxtools.h | 13 ++++++++ lib/pdf/pdf.cc | 83 +++++++++++++-------------------------------------- lib/python/gfx.c | 8 ++--- lib/readers/image.c | 8 ++--- lib/readers/swf.c | 8 ++--- lib/ruby/gfx.c | 21 +++++++++++-- lib/ruby/test.rb | 3 +- src/gfx2gfx.c | 6 ++-- src/pdf2pdf.c | 10 +++---- src/pdf2swf.c | 12 ++++---- src/swfrender.c | 4 +-- 13 files changed, 126 insertions(+), 97 deletions(-) diff --git a/lib/gfxsource.h b/lib/gfxsource.h index 17548cc..845eca7 100644 --- a/lib/gfxsource.h +++ b/lib/gfxsource.h @@ -33,7 +33,7 @@ struct _gfxpage; typedef struct _gfxsource { - void (*set_parameter)(struct _gfxsource*src, const char*name, const char*value); + void (*setparameter)(struct _gfxsource*src, const char*name, const char*value); struct _gfxdocument* (*open)(struct _gfxsource*src, const char*filename); void (*destroy)(struct _gfxsource*src); void*internal; @@ -45,7 +45,7 @@ typedef struct _gfxdocument int num_pages; void* (*get)(struct _gfxdocument*gfx, const char*name); void (*destroy)(struct _gfxdocument*gfx); - void (*set_parameter)(struct _gfxdocument*gfx, const char*name, const char*value); + void (*setparameter)(struct _gfxdocument*gfx, const char*name, const char*value); char* (*getinfo)(struct _gfxdocument*gfx, const char*key); struct _gfxpage* (*getpage)(struct _gfxdocument*gfx, int page); void (*prepare)(struct _gfxdocument*gfx, gfxdevice_t*device); diff --git a/lib/gfxtools.c b/lib/gfxtools.c index 55c4925..2e080de 100644 --- a/lib/gfxtools.c +++ b/lib/gfxtools.c @@ -1152,3 +1152,46 @@ void gfximage_save_jpeg(gfximage_t*img, char*filename, int quality) jpeg_save(data, img->width, img->height, quality, filename); } +gfxparams_t* gfxparams_new() +{ + return (gfxparams_t*)rfx_calloc(sizeof(gfxparams_t)); +} + +void gfxparams_store(gfxparams_t*params, const char*key, const char*value) +{ + gfxparam_t*o = params->params; + while(o) { + if(!strcmp(key, o->key)) { + /* overwrite old value */ + free((void*)o->value); + o->value = strdup(value); + return; + } + o = o->next; + } + gfxparam_t*p = (gfxparam_t*)malloc(sizeof(gfxparam_t)); + p->key = strdup(key); + p->value = strdup(value); + p->next = 0; + + if(params->last) { + params->last->next = p; + params->last = p; + } else { + params->params = p; + params->last = p; + } +} + +void gfxparams_free(gfxparams_t*params) +{ + gfxparam_t*p = params->params; + while(p) { + gfxparam_t*next = p->next; + free((void*)p->key); + if(p->value) free((void*)p->value); + free(p); + p = next; + } +} + diff --git a/lib/gfxtools.h b/lib/gfxtools.h index d038e06..430b2e4 100644 --- a/lib/gfxtools.h +++ b/lib/gfxtools.h @@ -101,6 +101,19 @@ gfxline_t*gfxline_makerectangle(double x1, double y1, double x2, double y2); gfxline_t*gfxline_makecircle(double x,double y,double rx, double ry); void gfxline_show(gfxline_t*line, FILE*fi); +typedef struct _gfxparam { + const char*key; + const char*value; + struct _gfxparam* next; +} gfxparam_t; +typedef struct _gfxparams { + gfxparam_t*params; + gfxparam_t*last; +} gfxparams_t; +gfxparams_t* gfxparams_new(); +void gfxparams_store(gfxparams_t*params, const char*name, const char*value); +void gfxparams_free(gfxparams_t*params); + #ifdef __cplusplus } #endif diff --git a/lib/pdf/pdf.cc b/lib/pdf/pdf.cc index 1a6969d..e6bfe14 100644 --- a/lib/pdf/pdf.cc +++ b/lib/pdf/pdf.cc @@ -22,18 +22,6 @@ static char* global_page_range = 0; static int globalparams_count=0; -typedef struct _parameter -{ - struct _parameter *next; - const char*name; - const char*value; -} parameter_t; -typedef struct _parameterlist -{ - parameter_t* device_config; - parameter_t* device_config_next; -} parameterlist_t; - typedef struct _pdf_page_info { int xMin, yMin, xMax, yMax; @@ -49,7 +37,7 @@ typedef struct _pdf_doc_internal char config_bitmap_optimizing; char config_full_bitmap_optimizing; char config_print; - parameterlist_t parameters; + gfxparams_t* parameters; int protect; int nocopy; @@ -82,7 +70,7 @@ typedef struct _dev_output_internal typedef struct _gfxsource_internal { - parameterlist_t parameters; + gfxparams_t* parameters; } gfxsource_internal_t; @@ -95,34 +83,6 @@ static const char* dirseparator() #endif } -static void storeDeviceParameter(parameterlist_t*i, const char*name, const char*value) -{ - parameter_t*o = i->device_config; - while(o) { - if(!strcmp(name, o->name)) { - /* overwrite old value */ - free((void*)o->value); - o->value = strdup(value); - return; - } - o = o->next; - } - parameter_t*p = new parameter_t(); - p->name = strdup(name); - p->value = strdup(value); - p->next = 0; - - if(i->device_config_next) { - i->device_config_next->next = p; - i->device_config_next = p; - } else { - i->device_config = p; - i->device_config_next = p; - } -} - - - void pdfpage_destroy(gfxpage_t*pdf_page) { pdf_page_internal_t*i= (pdf_page_internal_t*)pdf_page->internal; @@ -150,14 +110,14 @@ static void render2(gfxpage_t*page, gfxdevice_t*dev, int x,int y, int x1,int y1, outputDev = (CommonOutputDev*)d; } /* pass global parameters to PDF driver*/ - parameter_t*p = i->parameters.device_config; + gfxparam_t*p = i->parameters->params; while(p) { - outputDev->setParameter(p->name, p->value); + outputDev->setParameter(p->key, p->value); p = p->next; } - p = pi->parameters.device_config; + p = pi->parameters->params; while(p) { - outputDev->setParameter(p->name, p->value); + outputDev->setParameter(p->key, p->value); p = p->next; } @@ -276,7 +236,7 @@ static void add_page_to_map(gfxdocument_t*gfx, int pdfpage, int outputpage) i->pagemap_pos = pdfpage; } -void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value) +void pdf_doc_setparameter(gfxdocument_t*gfx, const char*name, const char*value) { pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal; if(!strcmp(name, "pagemap")) { @@ -290,7 +250,7 @@ void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value) } else if(!strcmp(name, "asprint")) { i->config_print = 1; } else { - storeDeviceParameter(&i->parameters, name, value); + gfxparams_store(i->parameters, name, value); } } @@ -399,9 +359,11 @@ extern int config_addspace; extern int config_fontquality; extern int config_bigchar; -static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value) +static void pdf_setparameter(gfxsource_t*src, const char*name, const char*value) { gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal; + + gfxparams_store(i->parameters, name, value); msg(" setting parameter %s to \"%s\"", name, value); if(!strncmp(name, "fontdir", strlen("fontdir"))) { @@ -454,6 +416,7 @@ static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename) pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t)); memset(i, 0, sizeof(pdf_doc_internal_t)); i->parent = src; + i->parameters = gfxparams_new(); pdf_doc->internal = i; char*userPassword=0; @@ -530,7 +493,7 @@ static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename) pdf_doc->get = 0; pdf_doc->destroy = pdf_doc_destroy; - pdf_doc->set_parameter = pdf_doc_set_parameter; + pdf_doc->setparameter = pdf_doc_setparameter; pdf_doc->getinfo = pdf_doc_getinfo; pdf_doc->getpage = pdf_doc_getpage; pdf_doc->prepare = pdf_doc_prepare; @@ -544,16 +507,9 @@ void pdf_destroy(gfxsource_t*src) if(!src->internal) return; gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal; - - parameter_t*p = i->parameters.device_config; - while(p) { - parameter_t*next = p->next; - if(p->name) free((void*)p->name);p->name = 0; - if(p->value) free((void*)p->value);p->value =0; - p->next = 0;delete p; - p = next; - } - i->parameters.device_config=i->parameters.device_config_next=0; + + gfxparams_free(i->parameters); + i->parameters=0; free(src->internal);src->internal=0; @@ -565,11 +521,12 @@ gfxsource_t*gfxsource_pdf_create() { gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t)); memset(src, 0, sizeof(gfxsource_t)); - src->set_parameter = pdf_set_parameter; + src->setparameter = pdf_setparameter; src->open = pdf_open; src->destroy = pdf_destroy; - src->internal = malloc(sizeof(gfxsource_internal_t)); - memset(src->internal, 0, sizeof(gfxsource_internal_t)); + gfxsource_internal_t*i = (gfxsource_internal_t*)rfx_calloc(sizeof(gfxsource_internal_t)); + src->internal = (void*)i; + i->parameters = gfxparams_new(); if(!globalParams) { globalParams = new GFXGlobalParams(); diff --git a/lib/python/gfx.c b/lib/python/gfx.c index 98cc6cb..f15c1f2 100644 --- a/lib/python/gfx.c +++ b/lib/python/gfx.c @@ -918,7 +918,7 @@ static PyObject* doc_setparameter(PyObject* _self, PyObject* args, PyObject* kwa if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist, &key,&value)) return NULL; - self->doc->set_parameter(self->doc, key, value); + self->doc->setparameter(self->doc, key, value); return PY_NONE; } @@ -1118,7 +1118,7 @@ static PyObject* f_setparameter(PyObject* self, PyObject* args, PyObject* kwargs char*key=0,*value=0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist, &key, &value)) return NULL; - pdfdriver->set_parameter(pdfdriver,key,value); + pdfdriver->setparameter(pdfdriver,key,value); return PY_NONE; } @@ -1158,7 +1158,7 @@ static PyObject* f_addfont(PyObject* self, PyObject* args, PyObject* kwargs) char*filename=0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &filename)) return NULL; - pdfdriver->set_parameter(pdfdriver,"font", filename); + pdfdriver->setparameter(pdfdriver,"font", filename); return PY_NONE; } @@ -1174,7 +1174,7 @@ static PyObject* f_addfontdir(PyObject* self, PyObject* args, PyObject* kwargs) char*filename=0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &filename)) return NULL; - pdfdriver->set_parameter(pdfdriver,"fontdir", filename); + pdfdriver->setparameter(pdfdriver,"fontdir", filename); return PY_NONE; } diff --git a/lib/readers/image.c b/lib/readers/image.c index 261b560..7457a4e 100644 --- a/lib/readers/image.c +++ b/lib/readers/image.c @@ -87,7 +87,7 @@ void image_doc_destroy(gfxdocument_t*gfx) free(gfx);gfx=0; } -void image_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value) +void image_doc_setparameter(gfxdocument_t*gfx, const char*name, const char*value) { image_doc_internal_t*i= (image_doc_internal_t*)gfx->internal; } @@ -113,7 +113,7 @@ gfxpage_t* image_doc_getpage(gfxdocument_t*doc, int page) return image_page; } -static void image_set_parameter(gfxsource_t*src, const char*name, const char*value) +static void image_setparameter(gfxsource_t*src, const char*name, const char*value) { msg(" (gfxsource_image) setting parameter %s to \"%s\"", name, value); } @@ -143,7 +143,7 @@ static gfxdocument_t*image_open(gfxsource_t*src, const char*filename) image_doc->internal = i; image_doc->get = 0; image_doc->destroy = image_doc_destroy; - image_doc->set_parameter = image_doc_set_parameter; + image_doc->setparameter = image_doc_setparameter; image_doc->getpage = image_doc_getpage; return image_doc; @@ -153,7 +153,7 @@ gfxsource_t*gfxsource_image_create() { gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t)); memset(src, 0, sizeof(gfxsource_t)); - src->set_parameter = image_set_parameter; + src->setparameter = image_setparameter; src->open = image_open; return src; } diff --git a/lib/readers/swf.c b/lib/readers/swf.c index 25edb17..5bff2a1 100644 --- a/lib/readers/swf.c +++ b/lib/readers/swf.c @@ -632,7 +632,7 @@ void swf_doc_destroy(gfxdocument_t*gfx) free(gfx);gfx=0; } -void swf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value) +void swf_doc_setparameter(gfxdocument_t*gfx, const char*name, const char*value) { swf_doc_internal_t*i= (swf_doc_internal_t*)gfx->internal; } @@ -660,7 +660,7 @@ gfxpage_t* swf_doc_getpage(gfxdocument_t*doc, int page) return swf_page; } -void swf_set_parameter(gfxsource_t*src, const char*name, const char*value) +void swf_setparameter(gfxsource_t*src, const char*name, const char*value) { msg(" setting parameter %s to \"%s\"", name, value); } @@ -705,7 +705,7 @@ gfxdocument_t*swf_open(gfxsource_t*src, const char*filename) swf_doc->internal = i; swf_doc->get = 0; swf_doc->destroy = swf_doc_destroy; - swf_doc->set_parameter = swf_doc_set_parameter; + swf_doc->setparameter = swf_doc_setparameter; swf_doc->getpage = swf_doc_getpage; return swf_doc; @@ -722,7 +722,7 @@ gfxsource_t*gfxsource_swf_create() { gfxsource_t*src = (gfxsource_t*)malloc(sizeof(gfxsource_t)); memset(src, 0, sizeof(gfxsource_t)); - src->set_parameter = swf_set_parameter; + src->setparameter = swf_setparameter; src->open = swf_open; src->destroy = swf_destroy; return src; diff --git a/lib/ruby/gfx.c b/lib/ruby/gfx.c index ae4b6d8..6a47da7 100644 --- a/lib/ruby/gfx.c +++ b/lib/ruby/gfx.c @@ -501,7 +501,7 @@ void rb_addfont(gfxdevice_t*dev, gfxfont_t*font) HEAD volatile VALUE f = font_is_cached(i, font); - if(!f) {f=convert_font(font);cache_font(i,font,v);} + if(!f) {f=convert_font(font);cache_font(i,font,f);} forward(v, id_addfont, 1, f); } @@ -509,7 +509,7 @@ void rb_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, { HEAD volatile VALUE f = font_is_cached(i, font); - if(!f) {f=convert_font(font);cache_font(i,font,v);} + if(!f) {f=convert_font(font);cache_font(i,font,f);} volatile VALUE v_color = convert_color(color); volatile VALUE v_matrix = convert_matrix(matrix); @@ -561,11 +561,26 @@ static VALUE page_render(VALUE cls, VALUE device) dev.endpage = rb_endpage; dev.finish = rb_finish; + dev.startpage(&dev, page->page->width, page->page->height); page->page->render(page->page, &dev); + dev.endpage(&dev); return cls; } +// ---------------------- global functions ---------------------------------- + +VALUE gfx_setparameter(VALUE module, VALUE _key, VALUE _value) +{ + Check_Type(_key, T_STRING); + Check_Type(_value, T_STRING); + const char*key = StringValuePtr(_key); + const char*value = StringValuePtr(_value); + pdfdriver->setparameter(pdfdriver, key, value); + swfdriver->setparameter(swfdriver, key, value); + imagedriver->setparameter(imagedriver, key, value); + return GFX; +} // -------------------------------------------------------------------------- @@ -578,6 +593,8 @@ void Init_gfx() GFX = rb_define_module("GFX"); + rb_define_module_function(GFX, "setparameter", gfx_setparameter, 2); + DocumentPage = rb_define_class_under(GFX, "DocumentPage", rb_cObject); rb_define_method(DocumentPage, "width", page_width, 0); rb_define_method(DocumentPage, "height", page_height, 0); diff --git a/lib/ruby/test.rb b/lib/ruby/test.rb index 5e6b688..1c0418e 100644 --- a/lib/ruby/test.rb +++ b/lib/ruby/test.rb @@ -1,7 +1,6 @@ require 'gfx' -#pdf = GFX::PDF.new('test.pdf') -pdf = GFX::PDF.new('/home/kramm/paper5.pdf') +pdf = GFX::PDF.new('file.pdf') class TestRender < GFX::Device def startpage(width,height) diff --git a/src/gfx2gfx.c b/src/gfx2gfx.c index 67951a1..6a7596f 100644 --- a/src/gfx2gfx.c +++ b/src/gfx2gfx.c @@ -102,9 +102,9 @@ int args_callback_option(char*name,char*val) { if(c && *c && c[1]) { *c = 0; c++; - driver->set_parameter(driver, s,c); + driver->setparameter(driver, s,c); } else { - driver->set_parameter(driver, s,"1"); + driver->setparameter(driver, s,"1"); } free(s); return 1; @@ -191,7 +191,7 @@ int main(int argn, char *argv[]) } is_in_range(0x7fffffff, pagerange); if(pagerange) - driver->set_parameter(driver, "pages", pagerange); + driver->setparameter(driver, "pages", pagerange); if(!filename) { args_callback_usage(argv[0]); diff --git a/src/pdf2pdf.c b/src/pdf2pdf.c index 1803029..485c6c0 100644 --- a/src/pdf2pdf.c +++ b/src/pdf2pdf.c @@ -95,9 +95,9 @@ int args_callback_option(char*name,char*val) { if(c && *c && c[1]) { *c = 0; c++; - driver->set_parameter(driver, s,c); + driver->setparameter(driver, s,c); } else { - driver->set_parameter(driver, s,"1"); + driver->setparameter(driver, s,"1"); } free(s); return 1; @@ -212,7 +212,7 @@ int main(int argn, char *argv[]) is_in_range(0x7fffffff, pagerange); if(pagerange) - driver->set_parameter(driver, "pages", pagerange); + driver->setparameter(driver, "pages", pagerange); if(!filename) { args_callback_usage(argv[0]); @@ -220,8 +220,8 @@ int main(int argn, char *argv[]) } gfxdocument_t* doc = driver->open(driver, filename); - //doc->set_parameter(doc, "drawonlyshapes", "1"); - doc->set_parameter(doc, "disable_polygon_conversion", "1"); + //doc->setparameter(doc, "drawonlyshapes", "1"); + doc->setparameter(doc, "disable_polygon_conversion", "1"); if(!doc) { msg(" Couldn't open %s", filename); diff --git a/src/pdf2swf.c b/src/pdf2swf.c index c50a3de..4f82dfc 100644 --- a/src/pdf2swf.c +++ b/src/pdf2swf.c @@ -266,7 +266,7 @@ int args_callback_option(char*name,char*val) { } else if(!strcmp(s,"help")) { printf("PDF Parameters:\n"); gfxsource_t*pdf = gfxsource_pdf_create(); - pdf->set_parameter(pdf, "help", ""); + pdf->setparameter(pdf, "help", ""); gfxdevice_t swf; gfxdevice_swf_init(&swf); printf("SWF Parameters:\n"); @@ -630,7 +630,7 @@ int main(int argn, char *argv[]) /* pass global parameters to PDF driver*/ parameter_t*p = device_config; while(p) { - driver->set_parameter(driver, p->name, p->value); + driver->setparameter(driver, p->name, p->value); p = p->next; } @@ -671,11 +671,11 @@ int main(int argn, char *argv[]) } if(pagerange) - driver->set_parameter(driver, "pages", pagerange); + driver->setparameter(driver, "pages", pagerange); /* add fonts */ for(t=0;tset_parameter(driver, "fontdir", fontpaths[t]); + driver->setparameter(driver, "fontdir", fontpaths[t]); } if(info_only) { @@ -713,7 +713,7 @@ int main(int argn, char *argv[]) /* pass global parameters document */ p = device_config; while(p) { - pdf->set_parameter(pdf, p->name, p->value); + pdf->setparameter(pdf, p->name, p->value); p = p->next; } @@ -732,7 +732,7 @@ int main(int argn, char *argv[]) if(is_in_range(pagenr, pagerange)) { char mapping[80]; sprintf(mapping, "%d:%d", pagenr, frame); - pdf->set_parameter(pdf, "pagemap", mapping); + pdf->setparameter(pdf, "pagemap", mapping); pagenum++; } if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) { diff --git a/src/swfrender.c b/src/swfrender.c index 0dcee51..e9df12d 100644 --- a/src/swfrender.c +++ b/src/swfrender.c @@ -151,11 +151,11 @@ int main(int argn, char*argv[]) gfxsource_t*src = gfxsource_swf_create(); for(p=params;p;p=p->next) { - src->set_parameter(src, p->name, p->value); + src->setparameter(src, p->name, p->value); } gfxdocument_t*doc = src->open(src, filename); for(p=params;p;p=p->next) { - doc->set_parameter(doc, p->name, p->value); + doc->setparameter(doc, p->name, p->value); } if(!doc) { fprintf(stderr,"Couldn't open %s\n", filename); -- 1.7.10.4