From caa91583da8b1cc95a4b2c054f785b31cecfc962 Mon Sep 17 00:00:00 2001 From: Marek Kasik Date: Fri, 21 Mar 2014 14:41:02 +0100 Subject: [PATCH] Rotate documents correctly Set correct size of rendered images when a rotation is requested. Use "rotate" command to rotate documents instead of setting of "Orientation". Adjust offsets accordingly. --- libspectre/spectre-device.c | 52 +++++++++++++++++++++++++++------------------ libspectre/spectre-gs.c | 49 ++++++++++++++++++++++++++++++++++-------- libspectre/spectre-gs.h | 5 ++++- 3 files changed, 75 insertions(+), 31 deletions(-) diff --git a/libspectre/spectre-device.c b/libspectre/spectre-device.c index a527d86..2b235fb 100644 --- a/libspectre/spectre-device.c +++ b/libspectre/spectre-device.c @@ -181,10 +181,12 @@ spectre_device_render (SpectreDevice *device, char *fmt; char *text_alpha, *graph_alpha; char *size = NULL; - char *resolution, *set; + char *resolution; char *dsp_format, *dsp_handle; char *width_points = NULL; char *height_points = NULL; + int scaled_width; + int scaled_height; gs = spectre_gs_new (); if (!gs) @@ -204,8 +206,8 @@ spectre_device_render (SpectreDevice *device, return SPECTRE_STATUS_RENDER_ERROR; } - width = (int) ((width * rc->x_scale) + 0.5); - height = (int) ((height * rc->y_scale) + 0.5); + scaled_width = (int) ((width * rc->x_scale) + 0.5); + scaled_height = (int) ((height * rc->y_scale) + 0.5); if (rc->use_platform_fonts == FALSE) n_args++; @@ -224,10 +226,20 @@ spectre_device_render (SpectreDevice *device, rc->text_alpha_bits); args[arg++] = graph_alpha = _spectre_strdup_printf ("-dGraphicsAlphaBits=%d", rc->graphic_alpha_bits); - args[arg++] = size =_spectre_strdup_printf ("-g%dx%d", width, height); - args[arg++] = resolution = _spectre_strdup_printf ("-r%fx%f", - rc->x_scale * rc->x_dpi, - rc->y_scale * rc->y_dpi); + + if (rc->orientation == 0 || rc->orientation == 2) { + args[arg++] = size =_spectre_strdup_printf ("-g%dx%d", scaled_width, scaled_height); + args[arg++] = resolution = _spectre_strdup_printf ("-r%fx%f", + rc->x_scale * rc->x_dpi, + rc->y_scale * rc->y_dpi); + } + else { + args[arg++] = size =_spectre_strdup_printf ("-g%dx%d", scaled_height, scaled_width); + args[arg++] = resolution = _spectre_strdup_printf ("-r%fx%f", + rc->y_scale * rc->y_dpi, + rc->x_scale * rc->x_dpi); + } + args[arg++] = dsp_format = _spectre_strdup_printf ("-dDisplayFormat=%d", DISPLAY_COLORS_RGB | DISPLAY_DEPTH_8 | @@ -254,10 +266,17 @@ spectre_device_render (SpectreDevice *device, args[arg++] = "-dNOPLATFONTS"; if (rc->width != -1 && rc->height != -1) { - args[arg++] = width_points = _spectre_strdup_printf ("-dDEVICEWIDTHPOINTS=%d", - rc->width); - args[arg++] = height_points = _spectre_strdup_printf ("-dDEVICEHEIGHTPOINTS=%d", - rc->height); + if (rc->orientation == 0 || rc->orientation == 2) { + args[arg++] = width_points = _spectre_strdup_printf ("-dDEVICEWIDTHPOINTS=%d", + rc->width); + args[arg++] = height_points = _spectre_strdup_printf ("-dDEVICEHEIGHTPOINTS=%d", + rc->height); + } else { + args[arg++] = width_points = _spectre_strdup_printf ("-dDEVICEWIDTHPOINTS=%d", + rc->height); + args[arg++] = height_points = _spectre_strdup_printf ("-dDEVICEHEIGHTPOINTS=%d", + rc->width); + } args[arg++] = "-dFIXEDMEDIA"; } @@ -276,16 +295,7 @@ spectre_device_render (SpectreDevice *device, return SPECTRE_STATUS_RENDER_ERROR; } - set = _spectre_strdup_printf ("<< /Orientation %d >> setpagedevice .locksafe", - rc->orientation); - if (!spectre_gs_send_string (gs, set)) { - free (set); - spectre_gs_free (gs); - return SPECTRE_STATUS_RENDER_ERROR; - } - free (set); - - if (!spectre_gs_send_page (gs, device->doc, page, x, y)) { + if (!spectre_gs_send_page (gs, device->doc, page, x, y, width, height, spectre_render_context_get_rotation (rc))) { spectre_gs_free (gs); return SPECTRE_STATUS_RENDER_ERROR; } diff --git a/libspectre/spectre-gs.c b/libspectre/spectre-gs.c index 93444a4..07ceed8 100644 --- a/libspectre/spectre-gs.c +++ b/libspectre/spectre-gs.c @@ -206,17 +206,22 @@ spectre_gs_send_string (SpectreGS *gs, } int -spectre_gs_send_page (SpectreGS *gs, - struct document *doc, - unsigned int page_index, - int x, - int y) +spectre_gs_send_page (SpectreGS *gs, + struct document *doc, + unsigned int page_index, + int x, + int y, + int crop_width, + int crop_height, + int rotation) { int xoffset = 0, yoffset = 0; int page_urx, page_ury, page_llx, page_lly; int bbox_urx, bbox_ury, bbox_llx, bbox_lly; int doc_xoffset = 0, doc_yoffset = 0; int page_xoffset = 0, page_yoffset = 0; + int tmp_xoffset = 0, tmp_yoffset = 0; + char *set; if (psgetpagebbox (doc, page_index, &bbox_urx, &bbox_ury, &bbox_llx, &bbox_lly)) { psgetpagebox (doc, page_index, @@ -230,12 +235,31 @@ spectre_gs_send_page (SpectreGS *gs, } } + switch (rotation) { + default: + tmp_xoffset = xoffset + x; + tmp_yoffset = yoffset + y; + break; + case 90: + tmp_xoffset = xoffset + x; + tmp_yoffset = crop_height + yoffset + y; + break; + case 180: + tmp_xoffset = crop_width + xoffset + x; + tmp_yoffset = crop_height + yoffset + y; + break; + case 270: + tmp_xoffset = crop_width + xoffset + x; + tmp_yoffset = yoffset + y; + break; + } + if (doc->numpages > 0) { - page_xoffset = xoffset + x; - page_yoffset = yoffset + y; + page_xoffset = tmp_xoffset; + page_yoffset = tmp_yoffset; } else { - doc_xoffset = xoffset + x; - doc_yoffset = yoffset + y; + doc_xoffset = tmp_xoffset; + doc_yoffset = tmp_yoffset; } if (!spectre_gs_process (gs, @@ -253,6 +277,13 @@ spectre_gs_send_page (SpectreGS *gs, doc->endsetup)) return FALSE; + if (rotation != 0) { + set = _spectre_strdup_printf ("%d rotate", rotation); + if (!spectre_gs_send_string (gs, set)) + return FALSE; + free (set); + } + if (doc->numpages > 0) { if (doc->pageorder == SPECIAL) { unsigned int i; diff --git a/libspectre/spectre-gs.h b/libspectre/spectre-gs.h index b673ce8..9e95e9d 100644 --- a/libspectre/spectre-gs.h +++ b/libspectre/spectre-gs.h @@ -54,7 +54,10 @@ int spectre_gs_send_page (SpectreGS *gs, struct document *doc, unsigned int page_index, int x, - int y); + int y, + int crop_width, + int crop_height, + int rotation); void spectre_gs_cleanup (SpectreGS *gs, SpectreGSCleanupFlag flag); void spectre_gs_free (SpectreGS *gs); -- 1.8.5.3