// method: draw all the pages to appropriate recording surfaces and then get ink extents void evenodd_cropboxes(PopplerDocument *document, cairo_rectangle_t *odd_page_crop_box, cairo_rectangle_t *even_page_crop_box) { GError *error = NULL; int num_document_pages = poppler_document_get_n_pages(document); cairo_surface_t *odd_pages = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL); cairo_surface_t *even_pages = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL); int page_num; for (page_num = 0; page_num < num_document_pages; page_num++) { cairo_surface_t *surface = odd_pages; if (page_num % 2 == 1) { surface = even_pages; } cairo_t *cr = cairo_create(surface); PopplerPage *page = poppler_document_get_page(document, page_num); if (page == NULL) { printf("%s:%d: %s\n", __FILE__, __LINE__, error->message); exit(1); } poppler_page_render_for_printing(page, cr); g_object_unref(page); exit_if_cairo_status_not_success(cr, __FILE__, __LINE__); cairo_destroy(cr); } cairo_recording_surface_ink_extents(odd_pages, &odd_page_crop_box->x, &odd_page_crop_box->y, &odd_page_crop_box->width, &odd_page_crop_box->height); cairo_recording_surface_ink_extents(even_pages, &even_page_crop_box->x, &even_page_crop_box->y, &even_page_crop_box->width, &even_page_crop_box->height); // use to check extent and crop box handling // write_surface_to_file_showing_crop_box("odd.pdf", odd_pages, odd_page_crop_box); // write_surface_to_file_showing_crop_box("even.pdf", even_pages, even_page_crop_box); // cleanup surfaces used to get crop boxes cairo_surface_destroy(odd_pages); exit_if_cairo_surface_status_not_success(odd_pages, __FILE__, __LINE__); cairo_surface_destroy(even_pages); exit_if_cairo_surface_status_not_success(even_pages, __FILE__, __LINE__); }
static cairo_test_status_t test_cairo_recording_surface_ink_extents (cairo_surface_t *surface) { double x, y, w, h; cairo_recording_surface_ink_extents (surface, &x, &y, &w, &h); return x == 0 && y == 0 && w == 0 && h == 0 ? CAIRO_TEST_SUCCESS : CAIRO_TEST_ERROR; }
static int recording_surface_ink_extents (lua_State *L) { double x0, y0, width, height; cairo_surface_t **obj = luaL_checkudata(L, 1, OOCAIRO_MT_NAME_SURFACE); cairo_recording_surface_ink_extents(*obj, &x0, &y0, &width, &height); lua_pushnumber(L, x0); lua_pushnumber(L, y0); lua_pushnumber(L, width); lua_pushnumber(L, height); return 4; }
void add_per_page_cropboxes(PopplerDocument *document, struct pages_t *pages) { GError *error = NULL; int num_document_pages = poppler_document_get_n_pages(document); int page_num; for (page_num = 0; page_num < pages->npages; page_num++) { int document_page_num = pages->pages[page_num].num; printf("document_page_num: %d\n", document_page_num); if (document_page_num >= num_document_pages) { printf("ERROR: The document does not have page %d, it only has %d pages\n", document_page_num, num_document_pages); exit(2); } cairo_surface_t *surface = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL); cairo_t *cr = cairo_create(surface); PopplerPage *page = poppler_document_get_page(document, document_page_num); if (page == NULL) { printf("%s:%d: %s\n", __FILE__, __LINE__, error->message); exit(1); } poppler_page_render_for_printing(page, cr); g_object_unref(page); exit_if_cairo_status_not_success(cr, __FILE__, __LINE__); cairo_destroy(cr); cairo_rectangle_t *crop_box = malloc(sizeof(cairo_rectangle_t)); cairo_recording_surface_ink_extents(surface, &crop_box->x, &crop_box->y, &crop_box->width, &crop_box->height); // use to check extent and crop box handling // write_surface_to_file_showing_crop_box("per_page.pdf", surface, crop_box); // cleanup surfaces used to get crop boxes cairo_surface_destroy(surface); exit_if_cairo_surface_status_not_success(surface, __FILE__, __LINE__); pages->pages[page_num].crop_box = crop_box; } }
static cairo_rectangle_t * ar_card_theme_kde_get_card_extents (ArCardThemeKDE *theme, int card_id, const char *node) { ArSvg *svg; cairo_rectangle_t *card_extents; cairo_rectangle_t rect; cairo_surface_t *surface; cairo_t *cr; card_extents = &theme->card_extents[card_id]; /* Is it initalised yet? */ if (card_extents->width != 0. && card_extents->height != 0.) return card_extents; svg = ((ArCardThemePreimage *) theme)->cards_svg; surface = cairo_recording_surface_create (CAIRO_CONTENT_ALPHA, NULL); cr = cairo_create (surface); cairo_set_tolerance (cr, 1.); ar_profilestart ("getting ink extents for node %s", node); rsvg_handle_render_cairo_sub (RSVG_HANDLE (svg), cr, node); ar_profileend ("getting ink extents for node %s", node); cairo_destroy (cr); cairo_recording_surface_ink_extents (surface, &rect.x, &rect.y, &rect.width, &rect.height); cairo_surface_destroy (surface); ar_debug_print (AR_DEBUG_CARD_THEME, "card %s %.3f x%.3f at (%.3f | %.3f)\n", node, card_extents->width, card_extents->height, card_extents->x, card_extents->y); *card_extents = rect; /* Sanity check; necessary? */ if (rect.width == 0. || rect.height == 0.) return NULL; return card_extents; }