summaryrefslogtreecommitdiff
path: root/source/fitz/draw-device.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-06-16 11:22:57 +0200
committerRobin Watts <robin.watts@artifex.com>2016-06-16 18:08:15 +0100
commit3ab4938180ee24769b081badfe6c2fe91c924b3f (patch)
treebb79612dc987a315f657b770f69b6c5134e1651c /source/fitz/draw-device.c
parent5825ef8805ca8439ac576fb0071f9ddbdf8f6281 (diff)
downloadmupdf-3ab4938180ee24769b081badfe6c2fe91c924b3f.tar.xz
Add fz_draw_options struct for use with draw device.
Diffstat (limited to 'source/fitz/draw-device.c')
-rw-r--r--source/fitz/draw-device.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c
index d72b20d0..f725fd41 100644
--- a/source/fitz/draw-device.c
+++ b/source/fitz/draw-device.c
@@ -1,6 +1,128 @@
#include "mupdf/fitz.h"
#include "draw-imp.h"
+const char *fz_draw_options_usage =
+ "Common raster format output options:\n"
+ "\trotate=N: rotate rendered pages N degrees counterclockwise\n"
+ "\tresolution=N: resolution of rendered pages in pixels per inch\n"
+ "\twidth=N: render pages to fit N pixels wide (ignore resolution option)\n"
+ "\theight=N: render pages to fit N pixels tall (ignore resolution option)\n"
+ "\tcolorspace=(gray|rgb|cmyk): render using specified colorspace\n"
+ "\talpha: render pages with alpha channel and transparent background\n"
+ ;
+
+static int opteq(const char *a, const char *b)
+{
+ int n = strlen(b);
+ return !strncmp(a, b, n) && (a[n] == ',' || a[n] == 0);
+}
+
+fz_draw_options *
+fz_parse_draw_options(fz_context *ctx, fz_draw_options *opts, const char *args)
+{
+ const char *val;
+
+ memset(opts, 0, sizeof *opts);
+
+ opts->resolution = 96;
+ opts->rotate = 0;
+ opts->width = 0;
+ opts->height = 0;
+ opts->colorspace = fz_device_rgb(ctx);
+ opts->alpha = 0;
+
+ if (fz_has_option(ctx, args, "rotate", &val))
+ opts->rotate = fz_atoi(val);
+ if (fz_has_option(ctx, args, "resolution", &val))
+ opts->resolution = fz_atoi(val);
+ if (fz_has_option(ctx, args, "width", &val))
+ opts->width = fz_atoi(val);
+ if (fz_has_option(ctx, args, "height", &val))
+ opts->height = fz_atoi(val);
+ if (fz_has_option(ctx, args, "colorspace", &val))
+ {
+ if (opteq(val, "gray") || opteq(val, "grey"))
+ opts->colorspace = fz_device_gray(ctx);
+ else if (opteq(val, "rgb"))
+ opts->colorspace = fz_device_rgb(ctx);
+ else if (opteq(val, "cmyk"))
+ opts->colorspace = fz_device_cmyk(ctx);
+ else
+ fz_throw(ctx, FZ_ERROR_GENERIC, "unknown colorspace in options");
+ }
+ if (fz_has_option(ctx, args, "alpha", &val))
+ opts->alpha = opteq(val, "yes");
+
+ /* Sanity check values */
+ if (opts->resolution <= 0) opts->resolution = 96;
+ if (opts->width < 0) opts->width = 0;
+ if (opts->height < 0) opts->height = 0;
+
+ return opts;
+}
+
+fz_device *
+fz_new_draw_device_with_options(fz_context *ctx, const fz_draw_options *opts, const fz_rect *mediabox,
+ fz_matrix *transform, fz_pixmap **pixmap)
+{
+ float zoom = opts->resolution / 72.0f;
+ int w = opts->width;
+ int h = opts->height;
+ fz_rect bounds;
+ fz_irect ibounds;
+ fz_device *dev;
+
+ fz_pre_scale(fz_rotate(transform, opts->rotate), zoom, zoom);
+ bounds = *mediabox;
+ fz_round_rect(&ibounds, fz_transform_rect(&bounds, transform));
+
+ /* If width or height are set, we may need to adjust the transform */
+ if (w || h)
+ {
+ float scalex = 1;
+ float scaley = 1;
+ if (w != 0)
+ scalex = w / (bounds.x1 - bounds.x0);
+ if (h != 0)
+ scaley = h / (bounds.y1 - bounds.y0);
+ if (scalex != scaley)
+ {
+ if (w == 0)
+ scalex = scaley;
+ else if (h == 0)
+ scaley = scalex;
+ else if (scalex > scaley)
+ scalex = scaley;
+ else
+ scaley = scalex;
+ }
+ if (scalex != 1 || scaley != 1)
+ {
+ fz_pre_scale(transform, scalex, scaley);
+ bounds = *mediabox;
+ fz_round_rect(&ibounds, fz_transform_rect(&bounds, transform));
+ }
+ }
+
+ *pixmap = fz_new_pixmap_with_bbox(ctx, opts->colorspace, &ibounds, opts->alpha);
+ fz_try(ctx)
+ {
+ if (opts->alpha)
+ fz_clear_pixmap(ctx, *pixmap);
+ else
+ fz_clear_pixmap_with_value(ctx, *pixmap, 255);
+
+ dev = fz_new_draw_device(ctx, *pixmap);
+ }
+ fz_catch(ctx)
+ {
+ fz_drop_pixmap(ctx, *pixmap);
+ *pixmap = NULL;
+ fz_rethrow(ctx);
+ }
+ return dev;
+}
+
#define STACK_SIZE 96
/* Enable the following to attempt to support knockout and/or isolated