[cairo] C++ bindings

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Sat Jan 1 13:19:24 PST 2005


I've been working on a C++ wrapper for Cairo.  Two issues have come up
so far.  First, in cairo.h, "cairo" is used as an incomplete type.  This
makes it difficult to create the highly desirable namespace "cairo".
Renaming the struct to "struct _cairo" makes it possible.  Since "struct
cairo" is only used to define cairo_t, this is a two-liner that doesn't
significantly affect the library's API.  A patch follows that does this.

Second: To use the PostScript or PNG back-ends requires the client to
provide a FILE*.  In general, it is impossible to get a matching FILE*
from (for example) a std::fstream.  Would it be possible to modify these
functions to accept a callback that performs the write action as needed?
Do the PNG and PS (and future PDF) backends need random access to the
output buffers, or is append support enough?  The C interface can retain
the FILE* versions for convenience to C programmers.

To be absolutely clear, this is the interface that I am proposing:

// Implemented as a convenient front-end for cairo_set_target_ps_raw()
void
cairo_set_target_ps (cairo_t *cr,
     FILE *file,
     double width_inches,
     double height_inches,
     double x_pixels_per_inch,
     double y_pixels_per_inch);

void
cairo_set_target_ps_raw( cairo_t* cr,
    void (*append_output)
        (const void* data, size_t len, void* closure),
    void* closure,
    double width_inches,
    double height_inches,
    double x_pixels_per_inch,
    double y_pixels_per_inch);

void
cairo_set_target_png (cairo_t *cr,
      FILE *file,
      cairo_format_t format,
      int width,
      int height);

void
cairo_set_target_png_raw (cairo_t *cr,
    void (*append_output)( 
        const void* data, size_t len, void* closure),
    void* closure,
    cairo_format_t format,
    int width,
    int height);

Thanks,
Jonathan Brandmeyer


Index: src/cairo.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairo.h,v
retrieving revision 1.62
diff -u -r1.62 cairo.h
--- src/cairo.h 1 Nov 2004 15:58:27 -0000       1.62
+++ src/cairo.h 1 Jan 2005 21:16:21 -0000
@@ -42,7 +42,7 @@
 #include <pixman.h>
 #include <stdio.h>

-typedef struct cairo cairo_t;
+typedef struct _cairo cairo_t;
 typedef struct cairo_surface cairo_surface_t;
 typedef struct cairo_matrix cairo_matrix_t;
 typedef struct cairo_pattern cairo_pattern_t;
Index: src/cairoint.h
===================================================================
RCS file: /cvs/cairo/cairo/src/cairoint.h,v
retrieving revision 1.78
diff -u -r1.78 cairoint.h
--- src/cairoint.h      21 Dec 2004 21:22:44 -0000      1.78
+++ src/cairoint.h      1 Jan 2005 21:16:33 -0000
@@ -766,7 +766,7 @@
     struct cairo_gstate *next;
 } cairo_gstate_t;

-struct cairo {
+struct _cairo {
     unsigned int ref_count;
     cairo_gstate_t *gstate;
     cairo_status_t status;
cvs diff: Diffing test
cvs diff: Diffing util

-- 
Jonathan Brandmeyer <jbrandmeyer at earthlink.net>




More information about the cairo mailing list