c++performancegraphicscairoblurry

cairo : blurred shapes and text


I am drawing some random rectangles via cairo in its image surface, scaling and translating it and displaying the result using

unsigned char* data = cairo_image_surface_get_data(surface);

but the rectangles seem blurred: (I could not post the screenshot)

Corners are (probably) not mapped to integer coordinates. The texture containing data is the same size and format with the surface and I have already played with AntiAliasing settings (text display is even more blurry!!). What should I do to map the resulting shapes to integer coordinates and improve quality (without using super-sampling and without dropping scaling-translation)?

Edit: I used the below code (with and without the third line) and it didn't work!:

cairo_save(cr);    
cairo_identity_matrix(cr);
cairo_translate(cr, 0.5, 0.5);
cairo_scale(cr, size.width, size.height);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_GOOD);
(make the drawing!!)
cairo_restore(cr);

Edit2: I used the snapping fun. (after all transformations code), but it also did not work:

double szero1 = zero,szero2 = zero, sone1 = one, sone2 = one;
snapToPixel(cr, &szero1, &szero2);
snapToPixel(cr, &sone1, &sone2);
cairo_rectangle (cr, szero1, szero2, sone1, sone2);

Solution

  • Without preprocessing the coordinate there is no way to force cairo to snap to the nearest integer-pixel-coordinate.

    void snapToPixel(cairo_t *cr, float *x, float *y) {
         cairo_user_to_device (cr, x, y);
         *x = round(*x);
         *y = round(*y);
         cairo_device_to_user (cr, x, y);
    }
    

    Stroke-widths will have the same problem (+different in x, y if width != height).

    More information at http://cairographics.org/FAQ/#sharp_lines