1 /** \file
2 * SPIcon: Generic icon widget
3 */
4 /*
5 * Authors:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * Bryce Harrington <brycehar@bryceharrington.org>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 2001-2005 authors
11 * Copyright (C) 2001 Ximian, Inc.
12 * Copyright (C) 2004 John Cliff
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 *
16 */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include <glib/gmem.h>
23 #include <gtk/gtk.h>
24 #include "sp-namedview.h"
25 #include "selection.h"
26 #include "inkscape.h"
27 #include "inkscape-stock.h"
28 #include "sp-rect.h"
29 #include "document-private.h"
30 #include "display/nr-arena.h"
31 #include "display/nr-arena-item.h"
33 #include "ui/cache/svg_preview_cache.h"
35 GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const NR::Rect& dbox, unsigned psize) {
36 NRGC gc(NULL);
38 NR::Matrix t(NR::scale(scale_factor, scale_factor));
39 nr_arena_item_set_transform(root, t);
41 gc.transform.set_identity();
42 nr_arena_item_invoke_update( root, NULL, &gc,
43 NR_ARENA_ITEM_STATE_ALL,
44 NR_ARENA_ITEM_STATE_NONE );
46 /* Item integer bbox in points */
47 NRRectL ibox;
48 ibox.x0 = (int) floor(scale_factor * dbox.min()[NR::X] + 0.5);
49 ibox.y0 = (int) floor(scale_factor * dbox.min()[NR::Y] + 0.5);
50 ibox.x1 = (int) floor(scale_factor * dbox.max()[NR::X] + 0.5);
51 ibox.y1 = (int) floor(scale_factor * dbox.max()[NR::Y] + 0.5);
53 /* Find visible area */
54 int width = ibox.x1 - ibox.x0;
55 int height = ibox.y1 - ibox.y0;
56 int dx = psize;
57 int dy = psize;
58 dx = (dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
59 dy = (dy - height)/2;
61 NRRectL area;
62 area.x0 = ibox.x0 - dx;
63 area.y0 = ibox.y0 - dy;
64 area.x1 = area.x0 + psize;
65 area.y1 = area.y0 + psize;
67 /* Actual renderable area */
68 NRRectL ua;
69 ua.x0 = std::max(ibox.x0, area.x0);
70 ua.y0 = std::max(ibox.y0, area.y0);
71 ua.x1 = std::min(ibox.x1, area.x1);
72 ua.y1 = std::min(ibox.y1, area.y1);
74 /* Set up pixblock */
75 guchar *px = g_new(guchar, 4 * psize * psize);
76 memset(px, 0x00, 4 * psize * psize);
78 /* Render */
79 NRPixBlock B;
80 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
81 ua.x0, ua.y0, ua.x1, ua.y1,
82 px + 4 * psize * (ua.y0 - area.y0) +
83 4 * (ua.x0 - area.x0),
84 4 * psize, FALSE, FALSE );
85 nr_arena_item_invoke_render(NULL, root, &ua, &B,
86 NR_ARENA_ITEM_RENDER_NO_CACHE );
87 nr_pixblock_release(&B);
89 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px,
90 GDK_COLORSPACE_RGB,
91 TRUE,
92 8, psize, psize, psize * 4,
93 (GdkPixbufDestroyNotify)g_free,
94 NULL);
96 return pixbuf;
97 }
99 namespace Inkscape {
100 namespace UI {
101 namespace Cache {
103 SvgPreview::SvgPreview()
104 {
105 }
107 SvgPreview::~SvgPreview()
108 {
109 }
111 Glib::ustring SvgPreview::cache_key(gchar const *uri, gchar const *name, unsigned psize) const {
112 Glib::ustring key;
113 key += (uri!=NULL) ? uri : "";
114 key += ":";
115 key += (name!=NULL) ? name : "unknown";
116 key += ":";
117 key += psize;
118 return key;
119 }
121 GdkPixbuf* SvgPreview::get_preview_from_cache(const Glib::ustring& key) {
122 std::map<Glib::ustring, GdkPixbuf *>::iterator found = _pixmap_cache.find(key);
123 if ( found != _pixmap_cache.end() ) {
124 return found->second;
125 }
126 return NULL;
127 }
129 void SvgPreview::set_preview_in_cache(const Glib::ustring& key, GdkPixbuf* px) {
130 _pixmap_cache[key] = px;
131 }
133 GdkPixbuf* SvgPreview::get_preview(const gchar* uri, const gchar* id, NRArenaItem */*root*/,
134 double /*scale_factor*/, unsigned int psize) {
135 // First try looking up the cached preview in the cache map
136 Glib::ustring key = cache_key(uri, id, psize);
137 GdkPixbuf* px = get_preview_from_cache(key);
139 if (px == NULL) {
140 /*
141 px = render_pixbuf(root, scale_factor, dbox, psize);
142 set_preview_in_cache(key, px);
143 */
144 }
145 return px;
146 }
148 };
149 };
150 };