Code

7b802fee3c0877b461ddc2643629a6754f99c8a7
[inkscape.git] / src / ui / cache / svg_preview_cache.h
1 /** \file
2  * SPIcon: Generic icon widget
3  */
4 /*
5  * Copyright (C) 2007 Bryce W. Harrington <bryce@bryceharrington.org>
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  *
9  */
11 GdkPixbuf* get_pixbuf(GdkPixbuf* pixbuf) {
12     return pixbuf;
13 }
15 GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const NR::Rect& dbox, unsigned psize) {
16     NRGC gc(NULL);
17     NRMatrix t;
19     nr_matrix_set_scale(&t, scale_factor, scale_factor);
20     nr_arena_item_set_transform(root, &t);
22     nr_matrix_set_identity(&gc.transform);
23     nr_arena_item_invoke_update( root, NULL, &gc,
24                                  NR_ARENA_ITEM_STATE_ALL,
25                                  NR_ARENA_ITEM_STATE_NONE );
27     /* Item integer bbox in points */
28     NRRectL ibox;
29     ibox.x0 = (int) floor(scale_factor * dbox.min()[NR::X] + 0.5);
30     ibox.y0 = (int) floor(scale_factor * dbox.min()[NR::Y] + 0.5);
31     ibox.x1 = (int) floor(scale_factor * dbox.max()[NR::X] + 0.5);
32     ibox.y1 = (int) floor(scale_factor * dbox.max()[NR::Y] + 0.5);
34     /* Find visible area */
35     int width = ibox.x1 - ibox.x0;
36     int height = ibox.y1 - ibox.y0;
37     int dx = psize;
38     int dy = psize;
39     dx = (dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
40     dy = (dy - height)/2;
42     NRRectL area;
43     area.x0 = ibox.x0 - dx;
44     area.y0 = ibox.y0 - dy;
45     area.x1 = area.x0 + psize;
46     area.y1 = area.y0 + psize;
48     /* Actual renderable area */
49     NRRectL ua;
50     ua.x0 = std::max(ibox.x0, area.x0);
51     ua.y0 = std::max(ibox.y0, area.y0);
52     ua.x1 = std::min(ibox.x1, area.x1);
53     ua.y1 = std::min(ibox.y1, area.y1);
55     /* Set up pixblock */
56     guchar *px = g_new(guchar, 4 * psize * psize);
57     memset(px, 0x00, 4 * psize * psize);
59     /* Render */
60     NRPixBlock B;
61     nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
62                               ua.x0, ua.y0, ua.x1, ua.y1,
63                               px + 4 * psize * (ua.y0 - area.y0) +
64                               4 * (ua.x0 - area.x0),
65                               4 * psize, FALSE, FALSE );
66     nr_arena_item_invoke_render( root, &ua, &B,
67                                  NR_ARENA_ITEM_RENDER_NO_CACHE );
68     nr_pixblock_release(&B);
70     GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px,
71                                       GDK_COLORSPACE_RGB,
72                                       TRUE,
73                                       8, psize, psize, psize * 4,
74                                       (GdkPixbufDestroyNotify)g_free,
75                                       NULL);
77     return pixbuf;
78 }