implemented asset resolving
[swftools.git] / lib / python / gfx.c
index bdb6482..0af8b65 100644 (file)
@@ -72,11 +72,11 @@ static char* strf(char*format, ...)
     int l;
     va_list arglist;
     va_start(arglist, format);
-    vsprintf(buf, format, arglist);
+    vsnprintf(buf, sizeof(buf)-1, format, arglist);
     va_end(arglist);
     return strdup(buf);
 }
-#define PY_ERROR(s,args...) (PyErr_SetString(PyExc_Exception, strf(s, ## args)),NULL)
+#define PY_ERROR(s,args...) (PyErr_SetString(PyExc_Exception, strf(s, ## args)),(void*)NULL)
 #define PY_NONE Py_BuildValue("s", 0)
 
 //---------------------------------------------------------------------
@@ -142,7 +142,8 @@ typedef struct {
 static gfximage_t*toImage(PyObject*_bitmap)
 {
     if(!_bitmap || !_bitmap->ob_type->tp_name || strcmp(_bitmap->ob_type->tp_name, "Image")) {
-        return PY_ERROR("Second argument to fillbitmap must be an image");
+        PY_ERROR("Second argument to fillbitmap must be an image");
+        return 0;
     }
     ImageObject*bitmap = (ImageObject*)_bitmap;
     return bitmap->image;
@@ -157,8 +158,9 @@ static gfxline_t*toLine(PyObject*_line)
     gfxline_t*last=&first;
     for(t=0;t<num;t++) {
         PyObject*p= PySequence_GetItem(_line, t);
-        if(!PyTuple_Check(p))
+        if(!PyTuple_Check(p)) {
             return PY_ERROR("each point must be a tuple");
+       }
         PyObject*_type = PyTuple_GetItem(p, 0);
         if(!PyString_Check(_type))
             return PY_ERROR("point tuples must start with a string");
@@ -170,7 +172,7 @@ static gfxline_t*toLine(PyObject*_line)
                 return PY_ERROR("coordinates must be floats");
             }
         }
-        gfxline_t*l = malloc(sizeof(gfxline_t));
+        gfxline_t*l = (gfxline_t*)malloc(sizeof(gfxline_t));
         memset(l, 0, sizeof(gfxline_t));
         last->next = l;
         last = l;
@@ -347,7 +349,7 @@ static PyObject* f_createSWF(PyObject* parent, PyObject* args, PyObject* kwargs)
        return NULL;
     OutputObject*self = PyObject_New(OutputObject, &OutputClass);
     
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     gfxdevice_swf_init(self->output_device);
     return (PyObject*)self;
 }
@@ -367,7 +369,7 @@ static PyObject* f_createOCR(PyObject* parent, PyObject* args, PyObject* kwargs)
        return NULL;
     OutputObject*self = PyObject_New(OutputObject, &OutputClass);
     
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     gfxdevice_ocr_init(self->output_device);
     return (PyObject*)self;
 }
@@ -387,7 +389,7 @@ static PyObject* f_createImageList(PyObject* parent, PyObject* args, PyObject* k
        return NULL;
     OutputObject*self = PyObject_New(OutputObject, &OutputClass);
     
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     gfxdevice_render_init(self->output_device);
     return (PyObject*)self;
 }
@@ -405,7 +407,7 @@ static PyObject* f_createPlainText(PyObject* parent, PyObject* args, PyObject* k
        return NULL;
     OutputObject*self = PyObject_New(OutputObject, &OutputClass);
     
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     gfxdevice_text_init(self->output_device);
     return (PyObject*)self;
 }
@@ -424,18 +426,18 @@ static PyObject* f_createOpenGL(PyObject* parent, PyObject* args, PyObject* kwar
        return NULL;
     OutputObject*self = PyObject_New(OutputObject, &OutputClass);
     
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     gfxdevice_opengl_init(self->output_device);
     return (PyObject*)self;
 }
 #endif
 
-static PyObject*callback_python(char*function, gfxdevice_t*dev, const char*format, ...)
+static char callback_python(char*function, gfxdevice_t*dev, const char*format, ...)
 {
     OutputObject*self = (OutputObject*)dev->internal;
 
     if(!PyObject_HasAttrString(self->pyobj, function))
-        return PY_NONE;
+        return 0;
 
     va_list ap;
     va_start(ap, format);
@@ -450,6 +452,9 @@ static PyObject*callback_python(char*function, gfxdevice_t*dev, const char*forma
         } else if(p=='i') {
             int i = va_arg(ap, int);
             PyTuple_SetItem(tuple, pos, PyInt_FromLong(i));
+        } else if(p=='d') {
+            int i = va_arg(ap, double);
+            PyTuple_SetItem(tuple, pos, PyFloat_FromDouble(i));
         } else if(p=='c') {
             void* ptr = va_arg(ap, void*);
             gfxcolor_t*col = (gfxcolor_t*)ptr;
@@ -510,10 +515,10 @@ static PyObject*callback_python(char*function, gfxdevice_t*dev, const char*forma
     if(!result) { 
         PyErr_Print();
         PyErr_Clear();
-        return 0;
+        return 1;
     } else {
         Py_DECREF(result);
-        return 0;
+        return 1;
     }
 }
     
@@ -550,7 +555,7 @@ static void my_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolo
         joint = "round";
     else if(joint_style == gfx_joinBevel)
         joint = "bevel";
-    callback_python("stroke", dev, "licssi", line, width, color, cap, joint, miterLimit);
+    callback_python("stroke", dev, "ldcssi", line, width, color, cap, joint, miterLimit);
 }
 static void my_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
 {
@@ -568,9 +573,19 @@ static void my_addfont(gfxdevice_t*dev, gfxfont_t*font)
 {
     callback_python("addfont", dev, "f", font);
 }
-static void my_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
+static void my_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
 {
-    callback_python("drawchar", dev, "ficm", font, glyph, color, matrix);
+    if(!callback_python("drawchar", dev, "ficm", font, glyphnr, color, matrix))
+    {
+        if(!font)
+            return;
+        gfxglyph_t*glyph = &font->glyphs[glyphnr];
+        gfxline_t*line2 = gfxline_clone(glyph->line);
+        gfxline_transform(line2, matrix);
+        my_fill(dev, line2, color);
+        gfxline_free(line2);
+        return;
+    }
 }
 static void my_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
 {
@@ -615,7 +630,7 @@ static PyObject* f_createPassThrough(PyObject* parent, PyObject* args, PyObject*
    
     self->pyobj = obj;
     Py_INCREF(obj);
-    self->output_device = malloc(sizeof(gfxdevice_t));
+    self->output_device = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
     memset(self->output_device, 0, sizeof(gfxdevice_t));
     self->output_device->name = strdup("passthrough");
 
@@ -770,7 +785,7 @@ static PyObject* page_asImage(PyObject* _self, PyObject* args, PyObject* kwargs)
     gfxresult_t*result = dev2.finish(&dev2);
     gfximage_t*img = (gfximage_t*)result->get(result,"page0");
     int l = img->width*img->height;
-    unsigned char*data = malloc(img->width*img->height*3);
+    unsigned char*data = (unsigned char*)malloc(img->width*img->height*3);
     int s,t;
     for(t=0,s=0;t<l;s+=3,t++) {
        data[s+0] = img->data[t].r;
@@ -904,7 +919,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;
 }
 
@@ -1104,7 +1119,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;
 }
 
@@ -1144,7 +1159,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;
 }
 
@@ -1160,7 +1175,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;
 }