static const char * _spectre_render_page (const char *filename, const char *page_label, cairo_surface_t **surface_out) { static const cairo_user_data_key_t key; SpectreDocument *document; SpectreStatus status; int width, height, stride; unsigned char *pixels; cairo_surface_t *surface; document = spectre_document_new (); spectre_document_load (document, filename); status = spectre_document_status (document); if (status) { spectre_document_free (document); return spectre_status_to_string (status); } if (page_label) { SpectrePage *page; SpectreRenderContext *rc; page = spectre_document_get_page_by_label (document, page_label); spectre_document_free (document); if (page == NULL) return "page not found"; spectre_page_get_size (page, &width, &height); rc = spectre_render_context_new (); spectre_render_context_set_page_size (rc, width, height); spectre_page_render (page, rc, &pixels, &stride); spectre_render_context_free (rc); status = spectre_page_status (page); spectre_page_free (page); if (status) { free (pixels); return spectre_status_to_string (status); } } else { spectre_document_get_page_size (document, &width, &height); spectre_document_render (document, &pixels, &stride); spectre_document_free (document); } surface = cairo_image_surface_create_for_data (pixels, CAIRO_FORMAT_RGB24, width, height, stride); cairo_surface_set_user_data (surface, &key, pixels, (cairo_destroy_func_t) free); *surface_out = surface; return NULL; }
static void go_spectre_build_surface (GOSpectre *spectre) { guint8 *data = NULL; int rowstride; static const cairo_user_data_key_t key; spectre_document_render (spectre->doc, &data, &rowstride); if (!data) return; if (spectre_document_status (spectre->doc) != SPECTRE_STATUS_SUCCESS) { g_free (data); return; } spectre->surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_RGB24, spectre->parent.width, spectre->parent.height, rowstride); cairo_surface_set_user_data (spectre->surface, &key, data, (cairo_destroy_func_t) g_free); }