From 3575c942f81d54c88fd48aee2352ecd29406e711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Mon, 12 Jun 2006 03:07:19 -0400 Subject: [PATCH] Make cairo_output_stream_t an embeddable type. Most internal cairo types are transparent within cairo and have init and fini functions to intialize and finialize them in place. This way they can be easily be embedded in other structs or derived from. Initially, the cairo_output_stream_t type was proposed as a publically visible type and thus kept opaque. However, now it's only used internally and derived from in a number of places so let's make it an embeddable type for consistency and ease of use. The patch keeps _cairo_output_stream_create() and _cairo_output_stream_close() around for (internal) backwards compatibility by deriving a cairo_output_stream_with_closure_t stream type. The patch also moves all cairo_output_stream_t functions out of cairoint.h and into new file cairo-output-stream-private.h, thus chipping away at the monolithic cairoint.h. --- src/cairo-base85-stream.c | 1 + src/cairo-output-stream.c | 76 +++++++++++++++++++++++++---------- src/cairo-pdf-surface.c | 1 + src/cairo-ps-surface.c | 1 + src/cairo-svg-surface.c | 1 + src/cairoint.h | 84 --------------------------------------- 6 files changed, 60 insertions(+), 104 deletions(-) diff --git a/src/cairo-base85-stream.c b/src/cairo-base85-stream.c index d42700de9..2861a4233 100644 --- a/src/cairo-base85-stream.c +++ b/src/cairo-base85-stream.c @@ -35,6 +35,7 @@ */ #include "cairoint.h" +#include "cairo-output-stream-private.h" typedef struct _cairo_base85_stream { cairo_output_stream_t *output; diff --git a/src/cairo-output-stream.c b/src/cairo-output-stream.c index 5ef36e5fd..c71b89fa0 100644 --- a/src/cairo-output-stream.c +++ b/src/cairo-output-stream.c @@ -1,4 +1,4 @@ -/* cairo_output_stream.c: Output stream abstraction +/* cairo-output-stream.c: Output stream abstraction * * Copyright © 2005 Red Hat, Inc * @@ -38,24 +38,35 @@ #include #include #include "cairoint.h" +#include "cairo-output-stream-private.h" #ifdef _MSC_VER #define snprintf _snprintf #endif /* _MSC_VER */ -struct _cairo_output_stream { - cairo_write_func_t write_func; - cairo_close_func_t close_func; - void *closure; - unsigned long position; - cairo_status_t status; - cairo_bool_t closed; -}; + +cairo_private void +_cairo_output_stream_init (cairo_output_stream_t *stream, + cairo_output_stream_write_func_t write_func, + cairo_output_stream_close_func_t close_func) +{ + stream->write_func = write_func; + stream->close_func = close_func; + stream->position = 0; + stream->status = CAIRO_STATUS_SUCCESS; + stream->closed = FALSE; +} + +cairo_private void +_cairo_output_stream_fini (cairo_output_stream_t *stream) +{ + _cairo_output_stream_close (stream); +} + const cairo_output_stream_t cairo_output_stream_nil = { NULL, /* write_func */ NULL, /* close_func */ - NULL, /* closure */ 0, /* position */ CAIRO_STATUS_NO_MEMORY, FALSE /* closed */ @@ -64,31 +75,56 @@ const cairo_output_stream_t cairo_output_stream_nil = { static const cairo_output_stream_t cairo_output_stream_nil_write_error = { NULL, /* write_func */ NULL, /* close_func */ - NULL, /* closure */ 0, /* position */ CAIRO_STATUS_WRITE_ERROR, FALSE /* closed */ }; +typedef struct _cairo_output_stream_with_closure { + cairo_output_stream_t base; + cairo_write_func_t write_func; + cairo_close_func_t close_func; + void *closure; +} cairo_output_stream_with_closure_t; + + +static cairo_status_t +closure_write (cairo_output_stream_t *stream, + const unsigned char *data, unsigned int length) +{ + cairo_output_stream_with_closure_t *stream_with_closure = + (cairo_output_stream_with_closure_t *) stream; + + return stream_with_closure->write_func (stream_with_closure->closure, + data, length); +} + +static cairo_status_t +closure_close (cairo_output_stream_t *stream) +{ + cairo_output_stream_with_closure_t *stream_with_closure = + (cairo_output_stream_with_closure_t *) stream; + + return stream_with_closure->close_func (stream_with_closure->closure); +} + cairo_output_stream_t * _cairo_output_stream_create (cairo_write_func_t write_func, cairo_close_func_t close_func, void *closure) { - cairo_output_stream_t *stream; + cairo_output_stream_with_closure_t *stream; - stream = malloc (sizeof (cairo_output_stream_t)); + stream = malloc (sizeof (cairo_output_stream_with_closure_t)); if (stream == NULL) return (cairo_output_stream_t *) &cairo_output_stream_nil; + _cairo_output_stream_init (&stream->base, closure_write, closure_close); stream->write_func = write_func; stream->close_func = close_func; stream->closure = closure; - stream->position = 0; - stream->status = CAIRO_STATUS_SUCCESS; - stream->closed = FALSE; - return stream; + return &stream->base; } void @@ -106,7 +142,7 @@ _cairo_output_stream_close (cairo_output_stream_t *stream) } if (stream->close_func) { - status = stream->close_func (stream->closure); + status = stream->close_func (stream); if (status) stream->status = status; } @@ -120,7 +156,7 @@ _cairo_output_stream_destroy (cairo_output_stream_t *stream) if (stream == NULL) return; - _cairo_output_stream_close (stream); + _cairo_output_stream_fini (stream); free (stream); } @@ -134,7 +170,7 @@ _cairo_output_stream_write (cairo_output_stream_t *stream, if (stream->status) return; - stream->status = stream->write_func (stream->closure, data, length); + stream->status = stream->write_func (stream, data, length); stream->position += length; } diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c index c0586b9e0..cca02a606 100644 --- a/src/cairo-pdf-surface.c +++ b/src/cairo-pdf-surface.c @@ -43,6 +43,7 @@ #include "cairo-ft-private.h" #include "cairo-paginated-surface-private.h" #include "cairo-path-fixed-private.h" +#include "cairo-output-stream-private.h" #include #include diff --git a/src/cairo-ps-surface.c b/src/cairo-ps-surface.c index a621670e4..53d646825 100644 --- a/src/cairo-ps-surface.c +++ b/src/cairo-ps-surface.c @@ -44,6 +44,7 @@ #include "cairo-paginated-surface-private.h" #include "cairo-meta-surface-private.h" #include "cairo-ft-private.h" +#include "cairo-output-stream-private.h" #include #include diff --git a/src/cairo-svg-surface.c b/src/cairo-svg-surface.c index 6d43119fe..893ef8938 100644 --- a/src/cairo-svg-surface.c +++ b/src/cairo-svg-surface.c @@ -46,6 +46,7 @@ #include "cairo-meta-surface-private.h" #include "cairo-paginated-surface-private.h" #include "cairo-scaled-font-subsets-private.h" +#include "cairo-output-stream-private.h" #include diff --git a/src/cairoint.h b/src/cairoint.h index 78062a798..91e450287 100644 --- a/src/cairoint.h +++ b/src/cairoint.h @@ -2191,90 +2191,6 @@ _cairo_utf8_to_utf16 (const unsigned char *str, uint16_t **result, int *items_written); -/* cairo_output_stream.c */ - -typedef struct _cairo_output_stream cairo_output_stream_t; - -extern const cairo_private cairo_output_stream_t cairo_output_stream_nil; - -/* We already have the following declared in cairo.h: - -typedef cairo_status_t (*cairo_write_func_t) (void *closure, - const unsigned char *data, - unsigned int length); -*/ -typedef cairo_status_t (*cairo_close_func_t) (void *closure); - -/* This function never returns NULL. If an error occurs (NO_MEMORY) - * while trying to create the output stream this function returns a - * valid pointer to a nil output stream. - * - * Note that even with a nil surface, the close_func callback will be - * called by a call to _cairo_output_stream_close or - * _cairo_output_stream_destroy. - */ -cairo_private cairo_output_stream_t * -_cairo_output_stream_create (cairo_write_func_t write_func, - cairo_close_func_t close_func, - void *closure); - -cairo_private void -_cairo_output_stream_close (cairo_output_stream_t *stream); - -cairo_private void -_cairo_output_stream_destroy (cairo_output_stream_t *stream); - -cairo_private void -_cairo_output_stream_write (cairo_output_stream_t *stream, - const void *data, size_t length); - -cairo_private void -_cairo_output_stream_write_hex_string (cairo_output_stream_t *stream, - const char *data, - size_t length); - -cairo_private unsigned char * -_cairo_lzw_compress (unsigned char *data, unsigned long *data_size_in_out); - -cairo_private void -_cairo_output_stream_vprintf (cairo_output_stream_t *stream, - const char *fmt, va_list ap); - -cairo_private void -_cairo_output_stream_printf (cairo_output_stream_t *stream, - const char *fmt, ...) CAIRO_PRINTF_FORMAT(2, 3); - -cairo_private long -_cairo_output_stream_get_position (cairo_output_stream_t *status); - -cairo_private cairo_status_t -_cairo_output_stream_get_status (cairo_output_stream_t *stream); - -/* This function never returns NULL. If an error occurs (NO_MEMORY or - * WRITE_ERROR) while trying to create the output stream this function - * returns a valid pointer to a nil output stream. - * - * NOTE: Even if a nil surface is returned, the caller should still - * call _cairo_output_stream_destroy (or _cairo_output_stream_close at - * least) in order to ensure that everything is properly cleaned up. - */ -cairo_private cairo_output_stream_t * -_cairo_output_stream_create_for_filename (const char *filename); - -/* This function never returns NULL. If an error occurs (NO_MEMORY or - * WRITE_ERROR) while trying to create the output stream this function - * returns a valid pointer to a nil output stream. - * - * The caller still "owns" file and is responsible for calling fclose - * on it when finished. The stream will not do this itself. - */ -cairo_private cairo_output_stream_t * -_cairo_output_stream_create_for_file (FILE *file); - -/* cairo_base85_stream.c */ -cairo_output_stream_t * -_cairo_base85_stream_create (cairo_output_stream_t *output); - cairo_private void _cairo_error (cairo_status_t status);