diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2010-01-18 23:11:19 +0000 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2010-01-22 23:01:50 +0000 |
commit | 3acd520c9dec89e72d7ff61adb1ae30bab12e256 (patch) | |
tree | 1b513d37583f3cb9e9ae6090e6b290367d3258b4 /src/cairo-xml-surface.c | |
parent | 49ab86772a44d7a044464d875324bd0af96af728 (diff) |
xml: Port to cairo_device_t
Diffstat (limited to 'src/cairo-xml-surface.c')
-rw-r--r-- | src/cairo-xml-surface.c | 210 |
1 files changed, 107 insertions, 103 deletions
diff --git a/src/cairo-xml-surface.c b/src/cairo-xml-surface.c index 1677c4c0b..53247a02e 100644 --- a/src/cairo-xml-surface.c +++ b/src/cairo-xml-surface.c @@ -45,6 +45,7 @@ #include "cairo-xml.h" #include "cairo-clip-private.h" +#include "cairo-device-private.h" #include "cairo-error-private.h" #include "cairo-output-stream-private.h" #include "cairo-recording-surface-private.h" @@ -53,30 +54,21 @@ typedef struct _cairo_xml_surface cairo_xml_surface_t; -struct _cairo_xml { - cairo_status_t status; - - int ref; +typedef struct _cairo_xml { + cairo_device_t base; cairo_output_stream_t *stream; int indent; -}; +} cairo_xml_t; struct _cairo_xml_surface { cairo_surface_t base; - cairo_xml_t *xml; - double width, height; }; slim_hidden_proto (cairo_xml_for_recording_surface); -static const cairo_xml_t _nil_xml = { - CAIRO_STATUS_NO_MEMORY, - -1 -}; - static const cairo_surface_backend_t _cairo_xml_surface_backend; static const char * @@ -232,25 +224,53 @@ _format_to_string (cairo_format_t format) return names[format]; } -static cairo_xml_t * +static void +_device_flush (void *abstract_device) +{ + cairo_xml_t *xml = abstract_device; + cairo_status_t status; + + status = _cairo_output_stream_flush (xml->stream); +} + +static void +_device_destroy (void *abstract_device) +{ + cairo_xml_t *xml = abstract_device; + cairo_status_t status; + + status = _cairo_output_stream_destroy (xml->stream); + + free (xml); +} + +static const cairo_device_backend_t _cairo_xml_device_backend = { + CAIRO_DEVICE_TYPE_XML, + + NULL, NULL, /* lock, unlock */ + + _device_flush, + NULL, /* finish */ + _device_destroy +}; + +static cairo_device_t * _cairo_xml_create_internal (cairo_output_stream_t *stream) { cairo_xml_t *xml; xml = malloc (sizeof (cairo_xml_t)); - if (unlikely (xml == NULL)) { - _cairo_error_throw (CAIRO_STATUS_NO_MEMORY); - return (cairo_xml_t *) &_nil_xml; - } + if (unlikely (xml == NULL)) + return _cairo_device_create_in_error (CAIRO_STATUS_NO_MEMORY); memset (xml, 0, sizeof (cairo_xml_t)); - xml->status = CAIRO_STATUS_SUCCESS; - xml->ref = 1; - xml->indent = 0; + _cairo_device_init (&xml->base, &_cairo_xml_device_backend); + + xml->indent = 0; xml->stream = stream; - return xml; + return &xml->base; } static void @@ -321,22 +341,6 @@ _cairo_xml_printf_end (cairo_xml_t *xml, const char *fmt, ...) _cairo_output_stream_write (xml->stream, "\n", 1); } -static cairo_status_t -_cairo_xml_destroy_internal (cairo_xml_t *xml) -{ - cairo_status_t status; - - assert (xml->ref > 0); - if (--xml->ref) - return _cairo_output_stream_flush (xml->stream); - - status = _cairo_output_stream_destroy (xml->stream); - - free (xml); - - return status; -} - static cairo_surface_t * _cairo_xml_surface_create_similar (void *abstract_surface, cairo_content_t content, @@ -352,14 +356,6 @@ _cairo_xml_surface_create_similar (void *abstract_surface, return cairo_recording_surface_create (content, &extents); } -static cairo_status_t -_cairo_xml_surface_finish (void *abstract_surface) -{ - cairo_xml_surface_t *surface = abstract_surface; - - return _cairo_xml_destroy_internal (surface->xml); -} - static cairo_bool_t _cairo_xml_surface_get_extents (void *abstract_surface, cairo_rectangle_int_t *rectangle) @@ -382,8 +378,8 @@ _cairo_xml_move_to (void *closure, const cairo_point_t *p1) { _cairo_xml_printf_continue (closure, " %f %f m", - _cairo_fixed_to_double (p1->x), - _cairo_fixed_to_double (p1->y)); + _cairo_fixed_to_double (p1->x), + _cairo_fixed_to_double (p1->y)); return CAIRO_STATUS_SUCCESS; } @@ -393,8 +389,8 @@ _cairo_xml_line_to (void *closure, const cairo_point_t *p1) { _cairo_xml_printf_continue (closure, " %f %f l", - _cairo_fixed_to_double (p1->x), - _cairo_fixed_to_double (p1->y)); + _cairo_fixed_to_double (p1->x), + _cairo_fixed_to_double (p1->y)); return CAIRO_STATUS_SUCCESS; } @@ -406,12 +402,13 @@ _cairo_xml_curve_to (void *closure, const cairo_point_t *p3) { _cairo_xml_printf_continue (closure, " %f %f %f %f %f %f c", - _cairo_fixed_to_double (p1->x), - _cairo_fixed_to_double (p1->y), - _cairo_fixed_to_double (p2->x), - _cairo_fixed_to_double (p2->y), - _cairo_fixed_to_double (p3->x), - _cairo_fixed_to_double (p3->y)); + _cairo_fixed_to_double (p1->x), + _cairo_fixed_to_double (p1->y), + _cairo_fixed_to_double (p2->x), + _cairo_fixed_to_double (p2->y), + _cairo_fixed_to_double (p3->x), + _cairo_fixed_to_double (p3->y)); + return CAIRO_STATUS_SUCCESS; } @@ -419,6 +416,7 @@ static cairo_status_t _cairo_xml_close_path (void *closure) { _cairo_xml_printf_continue (closure, " h"); + return CAIRO_STATUS_SUCCESS; } @@ -456,12 +454,19 @@ _cairo_xml_emit_double (cairo_xml_t *xml, _cairo_xml_printf (xml, "<%s>%f</%s>", node, data, node); } +static cairo_xml_t * +to_xml (cairo_xml_surface_t *surface) +{ + return (cairo_xml_t *) surface->base.device; +} + static cairo_status_t _cairo_xml_surface_emit_clip_path (cairo_xml_surface_t *surface, cairo_clip_path_t *clip_path) { cairo_box_t box; cairo_status_t status; + cairo_xml_t *xml; if (clip_path->prev != NULL) { status = _cairo_xml_surface_emit_clip_path (surface, clip_path->prev); @@ -482,18 +487,20 @@ _cairo_xml_surface_emit_clip_path (cairo_xml_surface_t *surface, } } - _cairo_xml_printf_start (surface->xml, "<clip>"); - _cairo_xml_indent (surface->xml, 2); + xml = to_xml (surface); - _cairo_xml_emit_path (surface->xml, &clip_path->path); - _cairo_xml_emit_double (surface->xml, "tolerance", clip_path->tolerance); - _cairo_xml_emit_string (surface->xml, "antialias", + _cairo_xml_printf_start (xml, "<clip>"); + _cairo_xml_indent (xml, 2); + + _cairo_xml_emit_path (xml, &clip_path->path); + _cairo_xml_emit_double (xml, "tolerance", clip_path->tolerance); + _cairo_xml_emit_string (xml, "antialias", _antialias_to_string (clip_path->antialias)); - _cairo_xml_emit_string (surface->xml, "fill-rule", + _cairo_xml_emit_string (xml, "fill-rule", _fill_rule_to_string (clip_path->fill_rule)); - _cairo_xml_indent (surface->xml, -2); - _cairo_xml_printf_end (surface->xml, "</clip>"); + _cairo_xml_indent (xml, -2); + _cairo_xml_printf_end (xml, "</clip>"); return CAIRO_STATUS_SUCCESS; } @@ -625,7 +632,7 @@ _cairo_xml_emit_surface (cairo_xml_t *xml, cairo_status_t status; if (_cairo_surface_is_recording (source)) { - status = cairo_xml_for_recording_surface (xml, source); + status = cairo_xml_for_recording_surface (&xml->base, source); } else { cairo_image_surface_t *image; void *image_extra; @@ -695,7 +702,7 @@ _cairo_xml_surface_paint (void *abstract_surface, cairo_clip_t *clip) { cairo_xml_surface_t *surface = abstract_surface; - cairo_xml_t *xml = surface->xml; + cairo_xml_t *xml = to_xml (surface); cairo_status_t status; _cairo_xml_printf (xml, "<paint>"); @@ -725,7 +732,7 @@ _cairo_xml_surface_mask (void *abstract_surface, cairo_clip_t *clip) { cairo_xml_surface_t *surface = abstract_surface; - cairo_xml_t *xml = surface->xml; + cairo_xml_t *xml = to_xml (surface); cairo_status_t status; _cairo_xml_printf (xml, "<mask>"); @@ -764,7 +771,7 @@ _cairo_xml_surface_stroke (void *abstract_surface, cairo_clip_t *clip) { cairo_xml_surface_t *surface = abstract_surface; - cairo_xml_t *xml = surface->xml; + cairo_xml_t *xml = to_xml (surface); cairo_status_t status; _cairo_xml_printf (xml, "<stroke>"); @@ -795,7 +802,7 @@ _cairo_xml_surface_stroke (void *abstract_surface, _cairo_xml_printf_end (xml, "</dash>"); } - _cairo_xml_emit_path (surface->xml, path); + _cairo_xml_emit_path (xml, path); _cairo_xml_emit_double (xml, "tolerance", tolerance); _cairo_xml_emit_string (xml, "antialias", _antialias_to_string (antialias)); @@ -818,7 +825,7 @@ _cairo_xml_surface_fill (void *abstract_surface, cairo_clip_t *clip) { cairo_xml_surface_t *surface = abstract_surface; - cairo_xml_t *xml = surface->xml; + cairo_xml_t *xml = to_xml (surface); cairo_status_t status; _cairo_xml_printf (xml, "<fill>"); @@ -834,7 +841,7 @@ _cairo_xml_surface_fill (void *abstract_surface, if (unlikely (status)) return status; - _cairo_xml_emit_path (surface->xml, path); + _cairo_xml_emit_path (xml, path); _cairo_xml_emit_double (xml, "tolerance", tolerance); _cairo_xml_emit_string (xml, "antialias", _antialias_to_string (antialias)); _cairo_xml_emit_string (xml, "fill-rule", _fill_rule_to_string (fill_rule)); @@ -958,7 +965,7 @@ _cairo_xml_surface_glyphs (void *abstract_surface, int *remaining_glyphs) { cairo_xml_surface_t *surface = abstract_surface; - cairo_xml_t *xml = surface->xml; + cairo_xml_t *xml = to_xml (surface); cairo_status_t status; int i; @@ -997,7 +1004,7 @@ static const cairo_surface_backend_t _cairo_xml_surface_backend = { CAIRO_SURFACE_TYPE_XML, _cairo_xml_surface_create_similar, - _cairo_xml_surface_finish, + NULL, NULL, NULL, /* source image */ NULL, NULL, /* dst image */ NULL, /* clone_similar */ @@ -1034,82 +1041,88 @@ _cairo_xml_surface_backend = { }; static cairo_surface_t * -_cairo_xml_surface_create_internal (cairo_xml_t *xml, +_cairo_xml_surface_create_internal (cairo_device_t *device, cairo_content_t content, double width, double height) { cairo_xml_surface_t *surface; - if (unlikely (xml == NULL)) - return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NULL_POINTER)); - surface = malloc (sizeof (cairo_xml_surface_t)); if (unlikely (surface == NULL)) return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY)); _cairo_surface_init (&surface->base, &_cairo_xml_surface_backend, - NULL, /* device */ + device, content); - surface->xml = xml; - xml->ref++; - surface->width = width; surface->height = height; return &surface->base; } -cairo_xml_t * +cairo_device_t * cairo_xml_create (const char *filename) { cairo_output_stream_t *stream; + cairo_status_t status; stream = _cairo_output_stream_create_for_filename (filename); - if (_cairo_output_stream_get_status (stream)) - return (cairo_xml_t *) &_nil_xml; + if ((status = _cairo_output_stream_get_status (stream))) + return _cairo_device_create_in_error (status); return _cairo_xml_create_internal (stream); } -cairo_xml_t * +cairo_device_t * cairo_xml_create_for_stream (cairo_write_func_t write_func, void *closure) { cairo_output_stream_t *stream; + cairo_status_t status; stream = _cairo_output_stream_create (write_func, NULL, closure); - if (_cairo_output_stream_get_status (stream)) - return (cairo_xml_t *) &_nil_xml; + if ((status = _cairo_output_stream_get_status (stream))) + return _cairo_device_create_in_error (status); return _cairo_xml_create_internal (stream); } cairo_surface_t * -cairo_xml_surface_create (cairo_xml_t *xml, +cairo_xml_surface_create (cairo_device_t *device, cairo_content_t content, double width, double height) { - return _cairo_xml_surface_create_internal (xml, content, width, height); + if (unlikely (device->backend->type != CAIRO_DEVICE_TYPE_XML)) + return _cairo_surface_create_in_error (CAIRO_STATUS_DEVICE_TYPE_MISMATCH); + + if (unlikely (device->status)) + return _cairo_surface_create_in_error (device->status); + + return _cairo_xml_surface_create_internal (device, content, width, height); } cairo_status_t -cairo_xml_for_recording_surface (cairo_xml_t *xml, +cairo_xml_for_recording_surface (cairo_device_t *device, cairo_surface_t *recording_surface) { cairo_box_t bbox; cairo_rectangle_int_t extents; cairo_surface_t *surface; + cairo_xml_t *xml; cairo_status_t status; - if (unlikely (xml->status)) - return xml->status; + if (unlikely (device->status)) + return device->status; if (unlikely (recording_surface->status)) return recording_surface->status; + if (unlikely (device->backend->type != CAIRO_DEVICE_TYPE_XML)) + return _cairo_error (CAIRO_STATUS_DEVICE_TYPE_MISMATCH); + if (unlikely (! _cairo_surface_is_recording (recording_surface))) return _cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH); @@ -1119,13 +1132,15 @@ cairo_xml_for_recording_surface (cairo_xml_t *xml, return status; _cairo_box_round_to_rectangle (&bbox, &extents); - surface = _cairo_xml_surface_create_internal (xml, + surface = _cairo_xml_surface_create_internal (device, recording_surface->content, extents.width, extents.height); if (unlikely (surface->status)) return surface->status; + xml = (cairo_xml_t *) device; + _cairo_xml_printf (xml, "<surface content='%s' width='%d' height='%d'>", _content_to_string (recording_surface->content), @@ -1142,14 +1157,3 @@ cairo_xml_for_recording_surface (cairo_xml_t *xml, return status; } slim_hidden_def (cairo_xml_for_recording_surface); - -void -cairo_xml_destroy (cairo_xml_t *xml) -{ - cairo_status_t status_ignored; - - if (xml == NULL || xml->ref < 0) - return; - - status_ignored = _cairo_xml_destroy_internal (xml); -} |