Code

Moving functions from svg_preview_cache.h to a new .cpp
authorbryce <bryce@users.sourceforge.net>
Tue, 20 Feb 2007 07:28:05 +0000 (07:28 +0000)
committerbryce <bryce@users.sourceforge.net>
Tue, 20 Feb 2007 07:28:05 +0000 (07:28 +0000)
configure.ac
src/Makefile.am
src/Makefile_insert
src/dialogs/stroke-style.cpp
src/ui/Makefile_insert
src/ui/cache/Makefile_insert [new file with mode: 0644]
src/ui/cache/makefile.in [new file with mode: 0644]
src/ui/cache/svg_preview_cache.cpp [new file with mode: 0644]
src/ui/cache/svg_preview_cache.h
src/widgets/icon.cpp

index 822d6c5be4bb0bf4bcf94f3c088af7dfe32d7f4b..d1d8eb114109146820015751f74f1d367d11630a 100644 (file)
@@ -814,6 +814,7 @@ src/removeoverlap/makefile
 src/svg/makefile
 src/trace/makefile
 src/traits/makefile
+src/ui/cache/makefile
 src/ui/dialog/makefile
 src/ui/makefile
 src/ui/view/makefile
index 7719cd2166a2be1b315db61d5163c2230a1f42ce..644ff8ae7240275f9d30c14ee51a32e9bf5d21f1 100644 (file)
@@ -57,6 +57,7 @@ include xml/Makefile_insert
 include traits/Makefile_insert
 include algorithms/Makefile_insert
 include ui/Makefile_insert
+include ui/cache/Makefile_insert
 include ui/dialog/Makefile_insert
 include ui/view/Makefile_insert
 include ui/widget/Makefile_insert
@@ -82,6 +83,7 @@ noinst_LIBRARIES =    \
        io/libio.a      \
        libcroco/libcroco.a     \
        ui/libui.a      \
+       ui/cache/libuicache.a   \
        ui/dialog/libuidialog.a \
        ui/view/libuiview.a     \
        ui/widget/libuiwidget.a \
@@ -148,6 +150,7 @@ EXTRA_DIST =        \
        traits/makefile.in      \
        utest/makefile.in       \
        ui/makefile.in          \
+       ui/cache/makefile.in    \
        ui/dialog/makefile.in   \
        ui/view/makefile.in     \
        ui/widget/makefile.in   \
index acccdc3273167fdfe342fb9070fcf409164f4ea7..29ea862686f973c9ebb6dd3b68981ec021690fc8 100644 (file)
@@ -301,6 +301,7 @@ inkscape_private_libs =     \
        application/libinkapp.a \
        ui/dialog/libuidialog.a \
        dialogs/libspdialogs.a  \
+       ui/cache/libuicache.a   \
        jabber_whiteboard/libjabber_whiteboard.a \
        pedro/libpedro.a        \
        trace/libtrace.a        \
index bab57b67906adeda000d66d8b3d06dc71894431a..0dedcfe4cdc5cb31f594e51b8068103349e1067e 100644 (file)
@@ -588,7 +588,6 @@ sp_marker_prev_new(unsigned psize, gchar const *mname,
 
     // Find object's bbox in document
     NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
-
     NR::Rect const dbox = SP_ITEM(object)->invokeBbox(i2doc);
 
     if (dbox.isEmpty()) {
@@ -600,7 +599,7 @@ sp_marker_prev_new(unsigned psize, gchar const *mname,
     GdkPixbuf* pixbuf = render_pixbuf(root, sf, dbox, psize);
 
     // Create widget
-    GtkWidget *pb = gtk_image_new_from_pixbuf(get_pixbuf(pixbuf));
+    GtkWidget *pb = gtk_image_new_from_pixbuf(pixbuf);
 
     return pb;
 }
index f3f24983d468671e4d77439993261e4fde906ac9..fc5ebad913a8a03e07485baae8a5a0e7e0f27582 100644 (file)
@@ -1,8 +1,8 @@
 ## Makefile.am fragment sourced by src/Makefile.am.
 
-ui/all: ui/libui.a ui/view/all ui/widget/all ui/dialog/all
+ui/all: ui/libui.a ui/view/all ui/widget/all ui/dialog/all ui/cache/all
 
-ui/clean: ui/widget/clean ui/dialog/clean
+ui/clean: ui/widget/clean ui/dialog/clean ui/cache/clean
        rm -f ui/libui.a $(ui_libui_a_OBJECTS)
 
 ui_libui_a_SOURCES =           \
diff --git a/src/ui/cache/Makefile_insert b/src/ui/cache/Makefile_insert
new file mode 100644 (file)
index 0000000..cb82280
--- /dev/null
@@ -0,0 +1,11 @@
+## Makefile.am fragment sourced by src/Makefile.am.
+
+ui/cache/all: ui/cache/libuicache.a
+
+ui/cache/clean:
+       rm -f ui/cache/libuicache.a $(ui_libuicache_a_OBJECTS)
+
+ui_cache_libuicache_a_SOURCES =        \
+       ui/cache/svg_preview_cache.h    \
+       ui/cache/svg_preview_cache.cpp
+
diff --git a/src/ui/cache/makefile.in b/src/ui/cache/makefile.in
new file mode 100644 (file)
index 0000000..08d8b03
--- /dev/null
@@ -0,0 +1,17 @@
+# Convenience stub makefile to call the real Makefile.
+
+@SET_MAKE@
+
+# Explicit so that it's the default rule.
+all:
+       cd ../.. && $(MAKE) ui/cache/all
+
+clean %.a %.o:
+       cd ../.. && $(MAKE) ui/cache/$@
+
+.PHONY: all clean
+
+OBJEXT = @OBJEXT@
+
+.SUFFIXES:
+.SUFFIXES: .a .$(OBJEXT)
diff --git a/src/ui/cache/svg_preview_cache.cpp b/src/ui/cache/svg_preview_cache.cpp
new file mode 100644 (file)
index 0000000..6b3bc39
--- /dev/null
@@ -0,0 +1,100 @@
+/** \file
+ * SPIcon: Generic icon widget
+ */
+/*
+ * Authors:
+ *   Lauris Kaplinski <lauris@kaplinski.com>
+ *   Bryce Harrington <brycehar@bryceharrington.org>
+ *   bulia byak <buliabyak@users.sf.net>
+ *
+ * Copyright (C) 2001-2005 authors
+ * Copyright (C) 2001 Ximian, Inc.
+ * Copyright (C) 2004 John Cliff
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <glib/gmem.h>
+#include <gtk/gtk.h>
+#include "sp-namedview.h"
+#include "selection.h"
+#include "inkscape.h"
+#include "inkscape-stock.h"
+#include "sp-rect.h"
+#include "document-private.h"
+#include "display/nr-arena.h"
+#include "display/nr-arena-item.h"
+
+#include "ui/cache/svg_preview_cache.h"
+
+GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const NR::Rect& dbox, unsigned psize) {
+    NRGC gc(NULL);
+    NRMatrix t;
+
+    nr_matrix_set_scale(&t, scale_factor, scale_factor);
+    nr_arena_item_set_transform(root, &t);
+
+    nr_matrix_set_identity(&gc.transform);
+    nr_arena_item_invoke_update( root, NULL, &gc,
+                                 NR_ARENA_ITEM_STATE_ALL,
+                                 NR_ARENA_ITEM_STATE_NONE );
+
+    /* Item integer bbox in points */
+    NRRectL ibox;
+    ibox.x0 = (int) floor(scale_factor * dbox.min()[NR::X] + 0.5);
+    ibox.y0 = (int) floor(scale_factor * dbox.min()[NR::Y] + 0.5);
+    ibox.x1 = (int) floor(scale_factor * dbox.max()[NR::X] + 0.5);
+    ibox.y1 = (int) floor(scale_factor * dbox.max()[NR::Y] + 0.5);
+
+    /* Find visible area */
+    int width = ibox.x1 - ibox.x0;
+    int height = ibox.y1 - ibox.y0;
+    int dx = psize;
+    int dy = psize;
+    dx = (dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
+    dy = (dy - height)/2;
+
+    NRRectL area;
+    area.x0 = ibox.x0 - dx;
+    area.y0 = ibox.y0 - dy;
+    area.x1 = area.x0 + psize;
+    area.y1 = area.y0 + psize;
+
+    /* Actual renderable area */
+    NRRectL ua;
+    ua.x0 = std::max(ibox.x0, area.x0);
+    ua.y0 = std::max(ibox.y0, area.y0);
+    ua.x1 = std::min(ibox.x1, area.x1);
+    ua.y1 = std::min(ibox.y1, area.y1);
+
+    /* Set up pixblock */
+    guchar *px = g_new(guchar, 4 * psize * psize);
+    memset(px, 0x00, 4 * psize * psize);
+
+    /* Render */
+    NRPixBlock B;
+    nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
+                              ua.x0, ua.y0, ua.x1, ua.y1,
+                              px + 4 * psize * (ua.y0 - area.y0) +
+                              4 * (ua.x0 - area.x0),
+                              4 * psize, FALSE, FALSE );
+    nr_arena_item_invoke_render( root, &ua, &B,
+                                 NR_ARENA_ITEM_RENDER_NO_CACHE );
+    nr_pixblock_release(&B);
+
+    GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px,
+                                      GDK_COLORSPACE_RGB,
+                                      TRUE,
+                                      8, psize, psize, psize * 4,
+                                      (GdkPixbufDestroyNotify)g_free,
+                                      NULL);
+
+    return pixbuf;
+}
+
+
index 7b802fee3c0877b461ddc2643629a6754f99c8a7..2d84b7273ae6f436e3fb13cc2fc55f3e7a9fb1ed 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef __SVG_PREVIEW_CACHE_H__
+#define __SVG_PREVIEW_CACHE_H__
+
 /** \file
  * SPIcon: Generic icon widget
  */
  *
  */
 
-GdkPixbuf* get_pixbuf(GdkPixbuf* pixbuf) {
-    return pixbuf;
-}
-
-GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const NR::Rect& dbox, unsigned psize) {
-    NRGC gc(NULL);
-    NRMatrix t;
-
-    nr_matrix_set_scale(&t, scale_factor, scale_factor);
-    nr_arena_item_set_transform(root, &t);
-
-    nr_matrix_set_identity(&gc.transform);
-    nr_arena_item_invoke_update( root, NULL, &gc,
-                                 NR_ARENA_ITEM_STATE_ALL,
-                                 NR_ARENA_ITEM_STATE_NONE );
-
-    /* Item integer bbox in points */
-    NRRectL ibox;
-    ibox.x0 = (int) floor(scale_factor * dbox.min()[NR::X] + 0.5);
-    ibox.y0 = (int) floor(scale_factor * dbox.min()[NR::Y] + 0.5);
-    ibox.x1 = (int) floor(scale_factor * dbox.max()[NR::X] + 0.5);
-    ibox.y1 = (int) floor(scale_factor * dbox.max()[NR::Y] + 0.5);
-
-    /* Find visible area */
-    int width = ibox.x1 - ibox.x0;
-    int height = ibox.y1 - ibox.y0;
-    int dx = psize;
-    int dy = psize;
-    dx = (dx - width)/2; // watch out for size, since 'unsigned'-'signed' can cause problems if the result is negative
-    dy = (dy - height)/2;
-
-    NRRectL area;
-    area.x0 = ibox.x0 - dx;
-    area.y0 = ibox.y0 - dy;
-    area.x1 = area.x0 + psize;
-    area.y1 = area.y0 + psize;
-
-    /* Actual renderable area */
-    NRRectL ua;
-    ua.x0 = std::max(ibox.x0, area.x0);
-    ua.y0 = std::max(ibox.y0, area.y0);
-    ua.x1 = std::min(ibox.x1, area.x1);
-    ua.y1 = std::min(ibox.y1, area.y1);
-
-    /* Set up pixblock */
-    guchar *px = g_new(guchar, 4 * psize * psize);
-    memset(px, 0x00, 4 * psize * psize);
-
-    /* Render */
-    NRPixBlock B;
-    nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
-                              ua.x0, ua.y0, ua.x1, ua.y1,
-                              px + 4 * psize * (ua.y0 - area.y0) +
-                              4 * (ua.x0 - area.x0),
-                              4 * psize, FALSE, FALSE );
-    nr_arena_item_invoke_render( root, &ua, &B,
-                                 NR_ARENA_ITEM_RENDER_NO_CACHE );
-    nr_pixblock_release(&B);
-
-    GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(px,
-                                      GDK_COLORSPACE_RGB,
-                                      TRUE,
-                                      8, psize, psize, psize * 4,
-                                      (GdkPixbufDestroyNotify)g_free,
-                                      NULL);
-
-    return pixbuf;
-}
+GdkPixbuf* render_pixbuf(NRArenaItem* root, double scale_factor, const NR::Rect& dbox, unsigned psize);
+
+namespace Inkscape {
+namespace UI {
+namespace Cache {
+
+class SvgPreview {
+ protected:
+    std::map<Glib::ustring, GdkPixbuf*>   _pixmap_cache;
+
+ public:
+    SvgPreview() {}
+    ~SvgPreview() {}
+
+    Glib::ustring cache_key(gchar const *name, unsigned psize) const {
+        Glib::ustring key = name;
+        key += ":";
+        key += psize;
+        return key;
+    }
+
+    GdkPixbuf* get_preview_from_cache(const Glib::ustring& key) {
+        std::map<Glib::ustring, GdkPixbuf *>::iterator found = _pixmap_cache.find(key);
+        if ( found != _pixmap_cache.end() ) {
+            return found->second;
+        }
+        return NULL;
+    }
+
+    void set_preview_in_cache(const Glib::ustring& key, GdkPixbuf* px) {
+        _pixmap_cache[key] = px;
+    }
+
+    GdkPixbuf* get_preview(const gchar* id, NRArenaItem *root, double scale_factor, unsigned int psize) {
+        // First try looking up the cached preview in the cache map
+        Glib::ustring key = cache_key(id, psize);
+        GdkPixbuf* px = get_preview_from_cache(key);
+
+        if (px == NULL) {
+/*
+            px = render_pixbuf(root, scale_factor, dbox, psize);
+            set_preview_in_cache(key, px);
+*/
+        }
+
+    }
+};
+
+}; // namespace Cache
+}; // namespace UI
+}; // namespace Inkscape
+
+
+
+#endif // __SVG_PREVIEW_CACHE_H__
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+
+
index 264349bd17f50a7d898c7d8e86400d32e04a3de6..dd7770ac108aca16f27153c6628d89a08add51d9 100644 (file)
@@ -833,8 +833,8 @@ static guchar *load_svg_pixels(gchar const *name,
 // returns true if icon needed preloading, false if nothing was done
 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
 {
-    Glib::ustring key=icon_cache_key(name, lsize, psize);
-    guchar *px=get_cached_pixels(key);
+    Glib::ustring key = icon_cache_key(name, lsize, psize);
+    guchar *px = get_cached_pixels(key);
     if (px) {
         return false;
     } else {
@@ -849,10 +849,10 @@ static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
 static guchar *
 sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
 {
-    Glib::ustring key=icon_cache_key(name, lsize, psize);
+    Glib::ustring key = icon_cache_key(name, lsize, psize);
 
     // did we already load this icon at this scale/size?
-    guchar *px=get_cached_pixels(key);
+    guchar *px = get_cached_pixels(key);
     if (!px) {
         px = load_svg_pixels(name, lsize, psize);
         if (px) {