[cairo] Need help using recording surface
cecashon at aol.com
cecashon at aol.com
Wed Apr 29 19:20:24 UTC 2020
Hi David,
There are some cairo_show_page() functions that you don't need. Going from pixels to points for the PDF can be a challenge. See if the following is of any help getting it to work.
Eric
//gcc -Wall -g copy_surface2.c -o copy_surface2 `pkg-config --cflags --libs cairo`
//Tested on Ubuntu18.04.
#include<cairo.h>
#include<cairo-pdf.h>
#include<stdio.h>
//Width and height of drawings.
static double width=400.0;
static double height=400.0;
//PDF's are in points so adjust the drawing if you want it the same size as the .png
//on screen. Try different values here.
static double ppi=100.0;
int main()
{
//Create a recording surface.
cairo_rectangle_t extents={.x=0.0, .y=0.0, .width=width, .height=height};
cairo_surface_t *surface1=cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, &extents);
cairo_t *cr1=cairo_create(surface1);
//Paint the background white.
cairo_set_source_rgba(cr1, 1.0, 1.0, 1.0, 1.0);
cairo_paint(cr1);
//Draw an x or other drawing in the recording surface.
cairo_set_line_width(cr1, 10.0);
cairo_set_source_rgba(cr1, 0.0, 1.0, 0.0, 1.0);
cairo_move_to(cr1, 0.0, 0.0);
cairo_line_to(cr1, 200.0, 200.0);
cairo_stroke(cr1);
cairo_move_to(cr1, 0.0, 200.0);
cairo_line_to(cr1, 200.0, 0.0);
cairo_stroke(cr1);
//Save the recording surface to a .png and .pdf.
printf("Save a_test.png\n");
cairo_surface_t *surface2=cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr2=cairo_create(surface2);
cairo_set_source_surface(cr2, surface1, 0.0, 0.0);
cairo_paint(cr2);
cairo_surface_write_to_png(surface2, "a_test.png");
printf("Save a_test.pdf\n");
double points_scale=72.0/ppi;
cairo_surface_t *surface3=cairo_pdf_surface_create("a_test.pdf", width*points_scale, height*points_scale);
cairo_t *cr3=cairo_create(surface3);
cairo_scale(cr3, points_scale, points_scale);
cairo_set_source_surface(cr3, surface1, 0.0, 0.0);
cairo_paint(cr3);
//Clean up.
cairo_destroy(cr1);
cairo_destroy(cr2);
cairo_destroy(cr3);
cairo_surface_destroy(surface1);
cairo_surface_destroy(surface2);
cairo_surface_destroy(surface3);
//For testing with valgrind.
cairo_debug_reset_static_data();
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20200429/479ff3c3/attachment.htm>
More information about the cairo
mailing list