summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2005-05-17 05:58:01 +0000
committerCarl Worth <cworth@cworth.org>2005-05-17 05:58:01 +0000
commit84bc5a32d6b3ad366698c14a01f7a4898f1cd25c (patch)
tree57c82dad0f156994cbdd103341184c36d9b79434
parent9bf26e8e73e0bafd7361ae38d485c9b9b17794b1 (diff)
Remove destroy_closure from cairo_output_stream_t interface.
Remove destroy_closure argument from cairo_pdf_surface_create_for_stream. Rename width,height to width_in_points, height_in_points for better clarity. Brush a bunch of dust off of the PS backend and bring it up to date with the latest API conventions from the PDF backend. These include: accepting a filename rather than a FILE in the primary constructor, providing a stream-based interface for more flexibility, and accepting a surface size in device-space units (points) rather than inches. Make it a little more clear that the width and height being passed around are in units of points. Update to the latest cairo-ps.h changes as described above. Notice how much more sane things become now that the surface size is described in device-space units.
-rw-r--r--ChangeLog27
-rw-r--r--src/cairo-output-stream.c23
-rw-r--r--src/cairo-pdf-surface.c13
-rw-r--r--src/cairo-pdf.h17
-rw-r--r--src/cairo-ps-surface.c153
-rw-r--r--src/cairo-ps.h19
-rw-r--r--src/cairoint.h1
-rw-r--r--test/pdf-surface.c11
-rw-r--r--test/ps-surface.c13
9 files changed, 164 insertions, 113 deletions
diff --git a/ChangeLog b/ChangeLog
index be7dba04f..1da625d47 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,32 @@
2005-05-17 Carl Worth <cworth@cworth.org>
+ * src/cairoint.h:
+ * src/cairo-output-stream.c: Remove destroy_closure from
+ cairo_output_stream_t interface.
+
+ * src/cairo-pdf.h:
+ * src/cairo-pdf-surface.c: Remove destroy_closure argument from
+ cairo_pdf_surface_create_for_stream. Rename width,height to
+ width_in_points, height_in_points for better clarity.
+
+ * src/cairo-ps.h:
+ * src/cairo-ps-surface.c: Brush a bunch of dust off of the PS
+ backend and bring it up to date with the latest API conventions
+ from the PDF backend. These include: accepting a filename rather
+ than a FILE in the primary constructor, providing a stream-based
+ interface for more flexibility, and accepting a surface size in
+ device-space units (points) rather than inches.
+
+ * test/pdf-surface.c: (main): Make it a little more clear that the
+ width and height being passed around are in units of points.
+
+ * test/ps-surface.c: (main): Update to the latest cairo-ps.h
+ changes as described above. Notice how much more sane things
+ become now that the surface size is described in device-space
+ units.
+
+2005-05-17 Carl Worth <cworth@cworth.org>
+
* test/.cvsignore:
* test/Makefile.am:
* test/ps-surface.c: (draw), (main): Add simple test for ps
diff --git a/src/cairo-output-stream.c b/src/cairo-output-stream.c
index e0b8e62d0..31473d1d8 100644
--- a/src/cairo-output-stream.c
+++ b/src/cairo-output-stream.c
@@ -49,7 +49,6 @@ struct _cairo_output_stream {
cairo_output_stream_t *
_cairo_output_stream_create (cairo_write_func_t write_data,
- cairo_destroy_func_t destroy_closure,
void *closure)
{
cairo_output_stream_t *stream;
@@ -59,7 +58,6 @@ _cairo_output_stream_create (cairo_write_func_t write_data,
return NULL;
stream->write_data = write_data;
- stream->destroy_closure = destroy_closure;
stream->closure = closure;
stream->position = 0;
stream->status = CAIRO_STATUS_SUCCESS;
@@ -70,7 +68,6 @@ _cairo_output_stream_create (cairo_write_func_t write_data,
void
_cairo_output_stream_destroy (cairo_output_stream_t *stream)
{
- stream->destroy_closure (stream->closure);
free (stream);
}
@@ -255,20 +252,12 @@ _cairo_output_stream_get_status (cairo_output_stream_t *stream)
static cairo_status_t
stdio_write (void *closure, const unsigned char *data, unsigned int length)
{
- FILE *fp = closure;
+ FILE *fp = closure;
- if (fwrite (data, 1, length, fp) == length)
- return CAIRO_STATUS_SUCCESS;
-
- return CAIRO_STATUS_WRITE_ERROR;
-}
-
-static void
-stdio_destroy_closure (void *closure)
-{
- FILE *fp = closure;
+ if (fwrite (data, 1, length, fp) == length)
+ return CAIRO_STATUS_SUCCESS;
- fclose (fp);
+ return CAIRO_STATUS_WRITE_ERROR;
}
cairo_output_stream_t *
@@ -281,11 +270,9 @@ _cairo_output_stream_create_for_file (const char *filename)
if (fp == NULL)
return NULL;
- stream = _cairo_output_stream_create (stdio_write,
- stdio_destroy_closure, fp);
+ stream = _cairo_output_stream_create (stdio_write, fp);
if (stream == NULL)
fclose (fp);
return stream;
}
-
diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index b09bfd81a..1a75772a4 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -903,9 +903,9 @@ _cairo_pdf_surface_add_font (cairo_pdf_surface_t *surface, unsigned int id)
}
static cairo_surface_t *
-_cairo_pdf_surface_create_for_stream (cairo_output_stream_t *stream,
- double width,
- double height)
+_cairo_pdf_surface_create_for_stream_internal (cairo_output_stream_t *stream,
+ double width,
+ double height)
{
cairo_pdf_document_t *document;
cairo_surface_t *surface;
@@ -924,18 +924,17 @@ _cairo_pdf_surface_create_for_stream (cairo_output_stream_t *stream,
cairo_surface_t *
cairo_pdf_surface_create_for_stream (cairo_write_func_t write,
- cairo_destroy_func_t destroy_closure,
void *closure,
double width,
double height)
{
cairo_output_stream_t *stream;
- stream = _cairo_output_stream_create (write, destroy_closure, closure);
+ stream = _cairo_output_stream_create (write, closure);
if (stream == NULL)
return NULL;
- return _cairo_pdf_surface_create_for_stream (stream, width, height);
+ return _cairo_pdf_surface_create_for_stream_internal (stream, width, height);
}
cairo_surface_t *
@@ -949,7 +948,7 @@ cairo_pdf_surface_create (const char *filename,
if (stream == NULL)
return NULL;
- return _cairo_pdf_surface_create_for_stream (stream, width, height);
+ return _cairo_pdf_surface_create_for_stream_internal (stream, width, height);
}
void
diff --git a/src/cairo-pdf.h b/src/cairo-pdf.h
index 050e4aa70..61bf7a841 100644
--- a/src/cairo-pdf.h
+++ b/src/cairo-pdf.h
@@ -44,19 +44,18 @@
CAIRO_BEGIN_DECLS
cairo_surface_t *
-cairo_pdf_surface_create (const char *filename,
- double width,
- double height);
+cairo_pdf_surface_create (const char *filename,
+ double width_in_points,
+ double height_in_points);
cairo_surface_t *
-cairo_pdf_surface_create_for_stream (cairo_write_func_t write_func,
- cairo_destroy_func_t destroy_closure_func,
- void *closure,
- double width,
- double height);
+cairo_pdf_surface_create_for_stream (cairo_write_func_t write_func,
+ void *closure,
+ double width_in_points,
+ double height_in_points);
void
-cairo_pdf_surface_set_dpi (cairo_surface_t *surface,
+cairo_pdf_surface_set_dpi (cairo_surface_t *surface,
double x_dpi,
double y_dpi);
diff --git a/src/cairo-ps-surface.c b/src/cairo-ps-surface.c
index 3bac00e8d..a79703e1d 100644
--- a/src/cairo-ps-surface.c
+++ b/src/cairo-ps-surface.c
@@ -46,30 +46,30 @@ typedef struct cairo_ps_surface {
cairo_surface_t base;
/* PS-specific fields */
- FILE *file;
+ cairo_output_stream_t *stream;
- double width_inches;
- double height_inches;
- double x_ppi;
- double y_ppi;
+ double width_in_points;
+ double height_in_points;
+ double x_dpi;
+ double y_dpi;
int pages;
cairo_image_surface_t *image;
} cairo_ps_surface_t;
+#define PS_SURFACE_DPI_DEFAULT 300.0
+
static void
_cairo_ps_surface_erase (cairo_ps_surface_t *surface);
-cairo_surface_t *
-cairo_ps_surface_create (FILE *file,
- double width_inches,
- double height_inches,
- double x_pixels_per_inch,
- double y_pixels_per_inch)
+static cairo_surface_t *
+_cairo_ps_surface_create_for_stream_internal (cairo_output_stream_t *stream,
+ double width_in_points,
+ double height_in_points)
{
cairo_ps_surface_t *surface;
- int width, height;
+ int width_in_pixels, height_in_pixels;
time_t now = time (0);
surface = malloc (sizeof (cairo_ps_surface_t));
@@ -78,20 +78,21 @@ cairo_ps_surface_create (FILE *file,
_cairo_surface_init (&surface->base, &cairo_ps_surface_backend);
- surface->file = file;
+ surface->stream = stream;
- surface->width_inches = width_inches;
- surface->height_inches = height_inches;
- surface->x_ppi = x_pixels_per_inch;
- surface->y_ppi = x_pixels_per_inch;
+ surface->width_in_points = width_in_points;
+ surface->height_in_points = height_in_points;
+ surface->x_dpi = PS_SURFACE_DPI_DEFAULT;
+ surface->y_dpi = PS_SURFACE_DPI_DEFAULT;
surface->pages = 0;
- width = (int) (x_pixels_per_inch * width_inches + 1.0);
- height = (int) (y_pixels_per_inch * height_inches + 1.0);
+ width_in_pixels = (int) (surface->x_dpi * width_in_points / 72.0 + 1.0);
+ height_in_pixels = (int) (surface->y_dpi * height_in_points / 72.0 + 1.0);
surface->image = (cairo_image_surface_t *)
- cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+ cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+ width_in_pixels, height_in_pixels);
if (surface->image == NULL) {
free (surface);
return NULL;
@@ -100,26 +101,61 @@ cairo_ps_surface_create (FILE *file,
_cairo_ps_surface_erase (surface);
/* Document header */
- fprintf (file,
- "%%!PS-Adobe-3.0\n"
- "%%%%Creator: Cairo (http://cairographics.org)\n");
- fprintf (file,
- "%%%%CreationDate: %s",
- ctime (&now));
- fprintf (file,
- "%%%%BoundingBox: %d %d %d %d\n",
- 0, 0, (int) (surface->width_inches * 72.0), (int) (surface->height_inches * 72.0));
+ _cairo_output_stream_printf (surface->stream,
+ "%%!PS-Adobe-3.0\n"
+ "%%%%Creator: Cairo (http://cairographics.org)\n");
+ _cairo_output_stream_printf (surface->stream,
+ "%%%%CreationDate: %s",
+ ctime (&now));
+ _cairo_output_stream_printf (surface->stream,
+ "%%%%BoundingBox: %f %f %f %f\n",
+ 0.0, 0.0,
+ surface->width_in_points,
+ surface->height_in_points);
/* The "/FlateDecode filter" currently used is a feature of LanguageLevel 3 */
- fprintf (file,
- "%%%%DocumentData: Clean7Bit\n"
- "%%%%LanguageLevel: 3\n");
- fprintf (file,
- "%%%%Orientation: Portrait\n"
- "%%%%EndComments\n");
+ _cairo_output_stream_printf (surface->stream,
+ "%%%%DocumentData: Clean7Bit\n"
+ "%%%%LanguageLevel: 3\n");
+ _cairo_output_stream_printf (surface->stream,
+ "%%%%Orientation: Portrait\n"
+ "%%%%EndComments\n");
return &surface->base;
}
+cairo_surface_t *
+cairo_ps_surface_create (const char *filename,
+ double width_in_points,
+ double height_in_points)
+{
+ cairo_output_stream_t *stream;
+
+ stream = _cairo_output_stream_create_for_file (filename);
+ if (stream == NULL)
+ return NULL;
+
+ return _cairo_ps_surface_create_for_stream_internal (stream,
+ width_in_points,
+ height_in_points);
+}
+
+cairo_surface_t *
+cairo_ps_surface_create_for_stream (cairo_write_func_t write_func,
+ void *closure,
+ double width_in_points,
+ double height_in_points)
+{
+ cairo_output_stream_t *stream;
+
+ stream = _cairo_output_stream_create (write_func, closure);
+ if (stream == NULL)
+ return NULL;
+
+ return _cairo_ps_surface_create_for_stream_internal (stream,
+ width_in_points,
+ height_in_points);
+}
+
static cairo_surface_t *
_cairo_ps_surface_create_similar (void *abstract_src,
cairo_format_t format,
@@ -136,7 +172,8 @@ _cairo_ps_surface_finish (void *abstract_surface)
cairo_ps_surface_t *surface = abstract_surface;
/* Document footer */
- fprintf (surface->file, "%%%%EOF\n");
+ _cairo_output_stream_printf (surface->stream,
+ "%%%%EOF\n");
cairo_surface_destroy (&surface->image->base);
@@ -192,7 +229,7 @@ _cairo_ps_surface_copy_page (void *abstract_surface)
cairo_ps_surface_t *surface = abstract_surface;
int width = surface->image->width;
int height = surface->image->height;
- FILE *file = surface->file;
+ cairo_output_stream_t *stream = surface->stream;
int i, x, y;
@@ -243,34 +280,34 @@ _cairo_ps_surface_copy_page (void *abstract_surface)
compress (compressed, &compressed_size, rgb, rgb_size);
/* Page header */
- fprintf (file, "%%%%Page: %d\n", ++surface->pages);
+ _cairo_output_stream_printf (stream, "%%%%Page: %d\n", ++surface->pages);
- fprintf (file, "gsave\n");
+ _cairo_output_stream_printf (stream, "gsave\n");
/* Image header goop */
- fprintf (file, "%g %g translate\n", 0.0, surface->height_inches * 72.0);
- fprintf (file, "%g %g scale\n", 72.0 / surface->x_ppi, 72.0 / surface->y_ppi);
- fprintf (file, "/DeviceRGB setcolorspace\n");
- fprintf (file, "<<\n");
- fprintf (file, " /ImageType 1\n");
- fprintf (file, " /Width %d\n", width);
- fprintf (file, " /Height %d\n", height);
- fprintf (file, " /BitsPerComponent 8\n");
- fprintf (file, " /Decode [ 0 1 0 1 0 1 ]\n");
- fprintf (file, " /DataSource currentfile /FlateDecode filter\n");
- fprintf (file, " /ImageMatrix [ 1 0 0 -1 0 1 ]\n");
- fprintf (file, ">>\n");
- fprintf (file, "image\n");
+ _cairo_output_stream_printf (stream, "%f %f translate\n",
+ 0.0, surface->height_in_points);
+ _cairo_output_stream_printf (stream, "/DeviceRGB setcolorspace\n");
+ _cairo_output_stream_printf (stream, "<<\n");
+ _cairo_output_stream_printf (stream, " /ImageType 1\n");
+ _cairo_output_stream_printf (stream, " /Width %d\n", width);
+ _cairo_output_stream_printf (stream, " /Height %d\n", height);
+ _cairo_output_stream_printf (stream, " /BitsPerComponent 8\n");
+ _cairo_output_stream_printf (stream, " /Decode [ 0 1 0 1 0 1 ]\n");
+ _cairo_output_stream_printf (stream, " /DataSource currentfile /FlateDecode filter\n");
+ _cairo_output_stream_printf (stream, " /ImageMatrix [ 1 0 0 -1 0 1 ]\n");
+ _cairo_output_stream_printf (stream, ">>\n");
+ _cairo_output_stream_printf (stream, "image\n");
/* Compressed image data */
- fwrite (compressed, 1, compressed_size, file);
+ _cairo_output_stream_write (stream, compressed, compressed_size);
- fprintf (file, "showpage\n");
+ _cairo_output_stream_printf (stream, "showpage\n");
- fprintf (file, "grestore\n");
+ _cairo_output_stream_printf (stream, "grestore\n");
/* Page footer */
- fprintf (file, "%%%%EndPage\n");
+ _cairo_output_stream_printf (stream, "%%%%EndPage\n");
free (compressed);
BAIL1:
@@ -316,8 +353,8 @@ _cairo_ps_surface_get_extents (void *abstract_surface,
* mention the aribitray limitation of width to a short(!). We
* may need to come up with a better interface for get_size.
*/
- rectangle->width = (surface->width_inches * 72.0 + 0.5);
- rectangle->height = (surface->height_inches * 72.0 + 0.5);
+ rectangle->width = (surface->width_in_points + 0.5);
+ rectangle->height = (surface->height_in_points + 0.5);
return CAIRO_STATUS_SUCCESS;
}
diff --git a/src/cairo-ps.h b/src/cairo-ps.h
index eca59e597..ea2d53d09 100644
--- a/src/cairo-ps.h
+++ b/src/cairo-ps.h
@@ -48,11 +48,20 @@ CAIRO_BEGIN_DECLS
/* PS-surface functions */
cairo_surface_t *
-cairo_ps_surface_create (FILE *file,
- double width_inches,
- double height_inches,
- double x_pixels_per_inch,
- double y_pixels_per_inch);
+cairo_ps_surface_create (const char *filename,
+ double width_in_points,
+ double height_in_points);
+
+cairo_surface_t *
+cairo_ps_surface_create_for_stream (cairo_write_func_t write_func,
+ void *closure,
+ double width_in_points,
+ double height_in_points);
+
+void
+cairo_ps_surface_set_dpi (cairo_surface_t *surface,
+ double x_dpi,
+ double y_dpi);
CAIRO_END_DECLS
diff --git a/src/cairoint.h b/src/cairoint.h
index 5f71fb9ba..90d59b2cb 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -1773,7 +1773,6 @@ typedef struct _cairo_output_stream cairo_output_stream_t;
cairo_private cairo_output_stream_t *
_cairo_output_stream_create (cairo_write_func_t write_func,
- cairo_destroy_func_t destroy_closure_func,
void *closure);
cairo_private void
diff --git a/test/pdf-surface.c b/test/pdf-surface.c
index ced2fe56d..863da539b 100644
--- a/test/pdf-surface.c
+++ b/test/pdf-surface.c
@@ -35,8 +35,8 @@
#define WIDTH_IN_INCHES 3
#define HEIGHT_IN_INCHES 3
-#define WIDTH (WIDTH_IN_INCHES * 72.0)
-#define HEIGHT (HEIGHT_IN_INCHES * 72.0)
+#define WIDTH_IN_POINTS (WIDTH_IN_INCHES * 72.0)
+#define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
static void
draw (cairo_t *cr, double width, double height)
@@ -93,15 +93,16 @@ main (void)
printf("\n");
- surface = cairo_pdf_surface_create (filename, WIDTH, HEIGHT);
+ surface = cairo_pdf_surface_create (filename,
+ WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
if (surface == NULL) {
- cairo_test_log ("Failed to create pdf surface for file %s\n", filename);
+ fprintf (stderr, "Failed to create pdf surface for file %s\n", filename);
return CAIRO_TEST_FAILURE;
}
cr = cairo_create (surface);
- draw (cr, WIDTH, HEIGHT);
+ draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
cairo_show_page (cr);
diff --git a/test/ps-surface.c b/test/ps-surface.c
index 306022368..5d5bf4ee3 100644
--- a/test/ps-surface.c
+++ b/test/ps-surface.c
@@ -87,21 +87,14 @@ draw (cairo_t *cr, double width, double height)
int
main (void)
{
- FILE *file;
cairo_t *cr;
const char *filename = "ps-surface.ps";
cairo_surface_t *surface;
printf("\n");
- file = fopen (filename, "wb");
- if (file == NULL) {
- fprintf (stderr, "Failed to open file %s\n", filename);
- return CAIRO_TEST_FAILURE;
- }
-
- surface = cairo_ps_surface_create (file, WIDTH_IN_INCHES, HEIGHT_IN_INCHES,
- 300, 300);
+ surface = cairo_ps_surface_create (filename,
+ WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
if (surface == NULL) {
cairo_test_log ("Failed to create pdf surface for file %s\n", filename);
return CAIRO_TEST_FAILURE;
@@ -109,7 +102,7 @@ main (void)
cr = cairo_create (surface);
- draw (cr, WIDTH_IN_INCHES * 300, HEIGHT_IN_INCHES * 300);
+ draw (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
cairo_show_page (cr);