static cairo_test_status_t preamble (cairo_test_context_t *ctx) { cairo_surface_t *surface; /* get the error surface */ surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, INT_MAX, INT_MAX); #if CAIRO_HAS_GL_SURFACE cairo_gl_surface_set_size (surface, 0, 0); cairo_gl_surface_swapbuffers (surface); #endif #if CAIRO_HAS_OS2_SURFACE cairo_os2_surface_set_hwnd (surface, 0); cairo_os2_surface_set_size (surface, 0, 0); cairo_os2_surface_set_manual_window_refresh (surface, FALSE); #endif #if CAIRO_HAS_PDF_SURFACE cairo_pdf_surface_restrict_to_version (surface, CAIRO_PDF_VERSION_1_4); cairo_pdf_surface_set_size (surface, 0, 0); #endif #if CAIRO_HAS_PS_SURFACE cairo_ps_surface_set_eps (surface, FALSE); cairo_ps_surface_set_size (surface, 0, 0); cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2); cairo_ps_surface_dsc_comment (surface, NULL); cairo_ps_surface_dsc_begin_setup (surface); cairo_ps_surface_dsc_begin_page_setup (surface); #endif #if CAIRO_HAS_XCB_SURFACE cairo_xcb_surface_set_size (surface, 0, 0); #endif #if CAIRO_HAS_XLIB_SURFACE cairo_xlib_surface_set_size (surface, 0, 0); cairo_xlib_surface_set_drawable (surface, 0, 0, 0); #endif cairo_surface_set_mime_data (surface, NULL, NULL, 0, NULL, 0); cairo_surface_set_device_offset (surface, 0, 0); cairo_surface_set_fallback_resolution (surface, 0, 0); cairo_surface_destroy (surface); return CAIRO_TEST_SUCCESS; }
static cairo_test_status_t test_cairo_surface_set_mime_data (cairo_surface_t *surface) { const char *mimetype = "text/x-uri"; const char *data = "http://www.cairographics.org"; cairo_status_t status; status = cairo_surface_set_mime_data (surface, mimetype, (const unsigned char *) data, strlen (data), NULL, NULL); return status ? CAIRO_TEST_SUCCESS : CAIRO_TEST_ERROR; }
void Pixbuf::_setMimeData(guchar *data, gsize len, Glib::ustring const &format) { gchar const *mimetype = NULL; if (format == "jpeg") { mimetype = CAIRO_MIME_TYPE_JPEG; } else if (format == "jpeg2000") { mimetype = CAIRO_MIME_TYPE_JP2; } else if (format == "png") { mimetype = CAIRO_MIME_TYPE_PNG; } if (mimetype != NULL) { cairo_surface_set_mime_data(_surface, mimetype, data, len, g_free, data); //g_message("Setting Cairo MIME data: %s", mimetype); } else { g_free(data); //g_message("Not setting Cairo MIME data: unknown format %s", name.c_str()); } }
static cairo_surface_t * _cairo_get_surface (CairoRenderer *renderer, const char *file) { cairo_surface_t *surface; GdkPixbuf *pixbuf; GError *error = NULL; surface = g_hash_table_lookup (renderer->surfaces, file); if (surface) return surface; pixbuf = gdk_pixbuf_new_from_file (file, &error); if (pixbuf == NULL) { if (error) { g_warning ("could not load file %s: %s", file, error->message); g_clear_error (&error); } return NULL; } surface = _cairo_new_surface_from_pixbuf (pixbuf); g_hash_table_insert (renderer->surfaces, g_strdup (file), surface); /* If we embed a JPEG, we can actually insert the coded data into the PDF in * a lossless fashion (no recompression of the JPEG) */ if (g_str_has_suffix (file, ".jpg") || g_str_has_suffix (file, ".jpeg")) { unsigned char *data = NULL; guint len = 0; _cairo_read_file (file, &data, &len); cairo_surface_set_mime_data (surface, CAIRO_MIME_TYPE_JPEG, data, len, g_free, data); } return surface; }
static cairo_test_status_t paint_file (cairo_t *cr, const char *filename, const char *mime_type, int x, int y) { const cairo_test_context_t *ctx = cairo_test_get_context (cr); cairo_surface_t *image; unsigned char *mime_data; unsigned int mime_length; cairo_status_t status; /* Deliberately use a non-matching MIME images, so that we can identify * when the MIME representation is used in preference to the plain image * surface. */ status = read_file (ctx, filename, &mime_data, &mime_length); if (status) return cairo_test_status_from_status (ctx, status); image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50); status = cairo_surface_set_mime_data (image, mime_type, mime_data, mime_length, free, mime_data); if (status) { cairo_surface_destroy (image); free (mime_data); return cairo_test_status_from_status (ctx, status); } cairo_set_source_surface (cr, image, x, y); cairo_surface_destroy (image); cairo_paint (cr); return CAIRO_TEST_SUCCESS; }
static int set_mime_data(lua_State *L) { cairo_surface_t **obj = luaL_checkudata(L, 1, OOCAIRO_MT_NAME_SURFACE); const char *mime_type = luaL_checkstring(L, 2); unsigned char *mime_priv; size_t data_length; if (lua_isnil(L, 3)) { data_length = 0; mime_priv = NULL; } else { const char *mime_data; mime_data = luaL_checklstring(L, 3, &data_length); mime_priv = malloc(data_length); if (!mime_priv) { return push_cairo_status(L, CAIRO_STATUS_NO_MEMORY); } memcpy(mime_priv, mime_data, data_length); } return push_cairo_status(L, cairo_surface_set_mime_data(*obj, mime_type, mime_priv, data_length, free, mime_priv)); }
cairo_surface_t * rsvg_cairo_surface_new_from_href (RsvgHandle *handle, const char *href, GError **error) { char *data; gsize data_len; char *mime_type = NULL; GdkPixbufLoader *loader = NULL; GdkPixbuf *pixbuf = NULL; cairo_surface_t *surface = NULL; data = _rsvg_handle_acquire_data (handle, href, &mime_type, &data_len, error); if (data == NULL) return NULL; if (mime_type) { loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error); } else { loader = gdk_pixbuf_loader_new (); } if (loader == NULL) goto out; if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_len, error)) { gdk_pixbuf_loader_close (loader, NULL); goto out; } if (!gdk_pixbuf_loader_close (loader, error)) goto out; pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); if (!pixbuf) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Failed to load image '%s': reason not known, probably a corrupt image file"), href); goto out; } surface = rsvg_cairo_surface_from_pixbuf (pixbuf); if (mime_type == NULL) { /* Try to get the information from the loader */ GdkPixbufFormat *format; char **mime_types; if ((format = gdk_pixbuf_loader_get_format (loader)) != NULL) { mime_types = gdk_pixbuf_format_get_mime_types (format); if (mime_types != NULL) mime_type = g_strdup (mime_types[0]); g_strfreev (mime_types); } } if ((handle->priv->flags & RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA) != 0 && mime_type != NULL && cairo_surface_set_mime_data (surface, mime_type, (guchar *) data, data_len, g_free, data) == CAIRO_STATUS_SUCCESS) { data = NULL; /* transferred to the surface */ } out: if (loader) g_object_unref (loader); g_free (mime_type); g_free (data); return surface; }
static void xviewer_print_draw_page (GtkPrintOperation *operation, GtkPrintContext *context, gint page_nr, gpointer user_data) { cairo_t *cr; gdouble dpi_x, dpi_y; gdouble x0, y0; gdouble scale_factor; gdouble p_width, p_height; gint width, height; XviewerPrintData *data; GtkPageSetup *page_setup; xviewer_debug (DEBUG_PRINTING); data = (XviewerPrintData *) user_data; scale_factor = data->scale_factor/100; dpi_x = gtk_print_context_get_dpi_x (context); dpi_y = gtk_print_context_get_dpi_y (context); switch (data->unit) { case GTK_UNIT_INCH: x0 = data->left_margin * dpi_x; y0 = data->top_margin * dpi_y; break; case GTK_UNIT_MM: x0 = data->left_margin * dpi_x/25.4; y0 = data->top_margin * dpi_y/25.4; break; default: g_assert_not_reached (); } cr = gtk_print_context_get_cairo_context (context); cairo_translate (cr, x0, y0); page_setup = gtk_print_context_get_page_setup (context); p_width = gtk_page_setup_get_page_width (page_setup, GTK_UNIT_POINTS); p_height = gtk_page_setup_get_page_height (page_setup, GTK_UNIT_POINTS); xviewer_image_get_size (data->image, &width, &height); /* this is both a workaround for a bug in cairo's PDF backend, and a way to ensure we are not printing outside the page margins */ cairo_rectangle (cr, 0, 0, MIN (width*scale_factor, p_width), MIN (height*scale_factor, p_height)); cairo_clip (cr); cairo_scale (cr, scale_factor, scale_factor); #ifdef HAVE_RSVG if (xviewer_image_is_svg (data->image)) { RsvgHandle *svg = xviewer_image_get_svg (data->image); rsvg_handle_render_cairo (svg, cr); return; } else #endif /* JPEGs can be attached to the cairo surface which simply embeds the JPEG file into the * destination PDF skipping (PNG-)recompression. This should reduce PDF sizes enormously. */ if (xviewer_image_is_jpeg (data->image) && _cairo_ctx_supports_jpg_metadata (cr)) { GFile *file; char *img_data; gsize data_len; cairo_surface_t *surface = NULL; xviewer_debug_message (DEBUG_PRINTING, "Attaching image to cairo surface"); file = xviewer_image_get_file (data->image); if (g_file_load_contents (file, NULL, &img_data, &data_len, NULL, NULL)) { XviewerTransform *tf = xviewer_image_get_transform (data->image); XviewerTransform *auto_tf = xviewer_image_get_autorotate_transform (data->image); cairo_matrix_t mx, mx2; if (!tf && auto_tf) { /* If only autorotation data present, * make it the normal rotation. */ tf = auto_tf; auto_tf = NULL; } /* Care must be taken with height and width values. They are not the original * values but were affected by the transformation. As the surface needs to be * generated using the original dimensions they might need to be flipped. */ if (tf) { if (auto_tf) { /* If we have an autorotation apply * it before the others */ tf = xviewer_transform_compose (auto_tf, tf); } switch (xviewer_transform_get_transform_type (tf)) { case XVIEWER_TRANSFORM_ROT_90: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, height, width); cairo_rotate (cr, 90.0 * (G_PI/180.0)); cairo_translate (cr, 0.0, -width); break; case XVIEWER_TRANSFORM_ROT_180: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, width, height); cairo_rotate (cr, 180.0 * (G_PI/180.0)); cairo_translate (cr, -width, -height); break; case XVIEWER_TRANSFORM_ROT_270: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, height, width); cairo_rotate (cr, 270.0 * (G_PI/180.0)); cairo_translate (cr, -height, 0.0); break; case XVIEWER_TRANSFORM_FLIP_HORIZONTAL: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, width, height); cairo_matrix_init_identity (&mx); _xviewer_cairo_matrix_flip (&mx2, &mx, TRUE, FALSE); cairo_transform (cr, &mx2); cairo_translate (cr, -width, 0.0); break; case XVIEWER_TRANSFORM_FLIP_VERTICAL: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, width, height); cairo_matrix_init_identity (&mx); _xviewer_cairo_matrix_flip (&mx2, &mx, FALSE, TRUE); cairo_transform (cr, &mx2); cairo_translate (cr, 0.0, -height); break; case XVIEWER_TRANSFORM_TRANSPOSE: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, height, width); cairo_matrix_init_rotate (&mx, 90.0 * (G_PI/180.0)); cairo_matrix_init_identity (&mx2); _xviewer_cairo_matrix_flip (&mx2, &mx2, TRUE, FALSE); cairo_matrix_multiply (&mx2, &mx, &mx2); cairo_transform (cr, &mx2); break; case XVIEWER_TRANSFORM_TRANSVERSE: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, height, width); cairo_matrix_init_rotate (&mx, 90.0 * (G_PI/180.0)); cairo_matrix_init_identity (&mx2); _xviewer_cairo_matrix_flip (&mx2, &mx2, FALSE, TRUE); cairo_matrix_multiply (&mx2, &mx, &mx2); cairo_transform (cr, &mx2); cairo_translate (cr, -height , -width); break; case XVIEWER_TRANSFORM_NONE: default: surface = cairo_image_surface_create ( CAIRO_FORMAT_RGB24, width, height); break; } } if (!surface) surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); cairo_surface_set_mime_data (surface, CAIRO_MIME_TYPE_JPEG, (unsigned char*)img_data, data_len, g_free, img_data); cairo_set_source_surface (cr, surface, 0, 0); cairo_paint (cr); cairo_surface_destroy (surface); g_object_unref (file); return; } g_object_unref (file); } { GdkPixbuf *pixbuf; pixbuf = xviewer_image_get_pixbuf (data->image); gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0); cairo_paint (cr); g_object_unref (pixbuf); } }
JNIEXPORT void JNICALL Java_org_freedesktop_cairo_CairoSurfaceOverride_cairo_1surface_1set_1mime_1data ( JNIEnv* env, jclass cls, jlong _self, jstring _mimeType, jbyteArray _data ) { cairo_status_t result; cairo_surface_t* self; const char* mimeType; unsigned char* data; long length; ImageCleanup* cleanup; // convert parameter self self = (cairo_surface_t*) _self; // convert parameter mimeType mimeType = (const char*) bindings_java_getString(env, _mimeType); if (mimeType == NULL) { return; // Java Exception already thrown } // convert parameter data // set up the data and length parameters. length = (*env)->GetArrayLength(env, _data); data = (unsigned char*) (*env)->GetByteArrayElements(env, _data, NULL); if (data == NULL) { return; // Java Exception already thrown } /* * Setup transfer object */ cleanup = g_malloc(sizeof(ImageCleanup)); cleanup->array = (jbyteArray) (*env)->NewGlobalRef(env, _data); cleanup->data = (jbyte*) data; // call function result = cairo_surface_set_mime_data(self, mimeType, data, length, release_image_data, cleanup); // cleanup parameter self // cleanup parameter mimeType bindings_java_releaseString(mimeType); // cleanup parameter data // done in callback /* * Check return value */ if (result != CAIRO_STATUS_SUCCESS) { bindings_java_throwByName(env, "org/freedesktop/cairo/FatalError", "Out of memory"); return; } }