From 5982a91e8e0aea5871c36350b888693340e54c9d Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Thu, 16 Dec 2010 20:12:30 -0800 Subject: Simple first pass for rough timing (bzr r9955.1.1) --- src/widgets/icon.cpp | 76 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 20 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 15900d340..d8d684614 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include "path-prefix.h" #include "preferences.h" @@ -542,8 +543,7 @@ static void setupLegacyNaming() { legacyNames["zoom"] ="sticky_zoom"; } -static GtkWidget * -sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) +static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) { static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); @@ -1203,6 +1203,7 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) { bool loadNeeded = false; static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); + static bool useCache = Inkscape::Preferences::get()->getBool("/debug/icons/useCache"); Glib::ustring key = icon_cache_key(name, psize); if ( !get_cached_pixbuf(key) ) { @@ -1211,28 +1212,62 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) if (dump) { g_message("prerender_icon [%s] %d:%d", name, lsize, psize); } - guchar* px = load_svg_pixels(name, lsize, psize); - if ( !px ) { - // check for a fallback name - if ( legacyNames.find(name) != legacyNames.end() ) { - if ( dump ) { - g_message("load_svg_pixels([%s]=%s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize); + // In file encoding: + std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); + std::string potentialFile = Glib::build_filename( iconCacheDir, name ); + potentialFile += ".png"; + + bool dataLoaded = false; + if ( useCache && Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { + Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); + if (pb) { + dataLoaded = true; + GdkPixbuf *obj = pb->gobj(); + g_object_ref(obj); + pb_cache[key] = obj; + addToIconSet(obj, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); } - px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize); } } - if (px) { - GdkPixbuf* pb = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, TRUE, 8, - psize, psize, psize * 4, - reinterpret_cast(g_free), NULL ); - pb_cache[key] = pb; - addToIconSet(pb, name, lsize, psize); - loadNeeded = true; - if (internalNames.find(name) == internalNames.end()) { - internalNames.insert(name); + + if (!dataLoaded) { + guchar* px = load_svg_pixels(name, lsize, psize); + if ( !px ) { + // check for a fallback name + if ( legacyNames.find(name) != legacyNames.end() ) { + if ( dump ) { + g_message("load_svg_pixels([%s]=%s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize); + } + px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize); + } + } + if (px) { + GdkPixbuf* pb = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, TRUE, 8, + psize, psize, psize * 4, + reinterpret_cast(g_free), NULL ); + pb_cache[key] = pb; + addToIconSet(pb, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); + } + if (useCache) { + g_object_ref(pb); + Glib::RefPtr ppp = Glib::wrap(pb); + try { + ppp->save( potentialFile, "png" ); + } catch ( Glib::FileError &ex ) { + g_warning("FileError [%s]", ex.what().c_str()); + } catch ( Gdk::PixbufError &ex ) { + g_warning("PixbufError [%s]", ex.what().c_str()); + } + } + } else if (dump) { + g_message("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX error!!! pixels not found for '%s'", name); } - } else if (dump) { - g_message("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX error!!! pixels not found for '%s'", name); } } else if (dump) { @@ -1249,6 +1284,7 @@ static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, u // did we already load this icon at this scale/size? GdkPixbuf* pb = get_cached_pixbuf(key); if (!pb) { + g_message("YYYYYYYYYYYYY YYYYYYYYYYYYYYY two load_svg_pixels(%s, %d, %d)", name, lsize, psize); guchar *px = load_svg_pixels(name, lsize, psize); if (px) { pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, -- cgit v1.2.3 From c1e346a68272b99fce380ea860fda0a70d3cf8e0 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Fri, 17 Dec 2010 22:22:17 -0800 Subject: Use subdirectories with icon sizes. Perform work only when cache loading is enabled. (bzr r9955.1.4) --- src/widgets/icon.cpp | 59 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 17 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index d8d684614..70dee457c 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -1198,6 +1198,19 @@ void Inkscape::queueIconPrerender( Glib::ustring const &name, Inkscape::IconSize } } +static std::map sizePaths; + +static std::string getDestDir( unsigned psize ) +{ + if ( sizePaths.find(psize) == sizePaths.end() ) { + gchar *tmp = g_strdup_printf("%dx%d", psize, psize); + sizePaths[psize] = tmp; + g_free(tmp); + } + + return sizePaths[psize]; +} + // returns true if icon needed preloading, false if nothing was done bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) { @@ -1212,23 +1225,36 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) if (dump) { g_message("prerender_icon [%s] %d:%d", name, lsize, psize); } - // In file encoding: - std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); - std::string potentialFile = Glib::build_filename( iconCacheDir, name ); - potentialFile += ".png"; - + + std::string potentialFile; bool dataLoaded = false; - if ( useCache && Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { - Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); - if (pb) { - dataLoaded = true; - GdkPixbuf *obj = pb->gobj(); - g_object_ref(obj); - pb_cache[key] = obj; - addToIconSet(obj, name, lsize, psize); - loadNeeded = true; - if (internalNames.find(name) == internalNames.end()) { - internalNames.insert(name); + if ( useCache ) { + // In file encoding: + std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); + std::string subpart = getDestDir(psize); + if (subpart.empty()) { + g_message("Is empty"); + } + std::string subdir = Glib::build_filename( iconCacheDir, subpart ); + if ( !Glib::file_test(subdir, Glib::FILE_TEST_EXISTS) ) { + g_message("NEED SUB OF [%s]", subdir.c_str()); + g_mkdir_with_parents( subdir.c_str(), 0x1ED ); + } + potentialFile = Glib::build_filename( subdir, name ); + potentialFile += ".png"; + + if ( Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { + Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); + if (pb) { + dataLoaded = true; + GdkPixbuf *obj = pb->gobj(); + g_object_ref(obj); + pb_cache[key] = obj; + addToIconSet(obj, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); + } } } } @@ -1284,7 +1310,6 @@ static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, u // did we already load this icon at this scale/size? GdkPixbuf* pb = get_cached_pixbuf(key); if (!pb) { - g_message("YYYYYYYYYYYYY YYYYYYYYYYYYYYY two load_svg_pixels(%s, %d, %d)", name, lsize, psize); guchar *px = load_svg_pixels(name, lsize, psize); if (px) { pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, -- cgit v1.2.3 From ce100a7a97d72a20337973219c7919b1d4bb0d45 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sat, 18 Dec 2010 10:05:01 -0800 Subject: Fix fallback icon loading order for icons with legacy names. (bzr r9955.1.5) --- src/widgets/icon.cpp | 161 ++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 91 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 70dee457c..7c8beeb75 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -1037,13 +1037,15 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, -struct svg_doc_cache_t +class SVGDocCache { +public: + SVGDocCache( SPDocument *doc, NRArenaItem *root ) : doc(doc), root(root) {} SPDocument *doc; NRArenaItem *root; }; -static std::map doc_cache; +static std::map doc_cache; static std::map pb_cache; Glib::ustring icon_cache_key(gchar const *name, unsigned psize) @@ -1079,92 +1081,59 @@ static std::list &icons_svg_paths() } // this function renders icons from icons.svg and returns the pixels. -static guchar *load_svg_pixels(gchar const *name, +static guchar *load_svg_pixels(std::list const &names, unsigned /*lsize*/, unsigned psize) { - SPDocument *doc = NULL; - NRArenaItem *root = NULL; - svg_doc_cache_t *info = NULL; - + bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); std::list &sources = icons_svg_paths(); // Try each document in turn until we successfully load the icon from one - guchar *px=NULL; - for (std::list::iterator i = sources.begin(); i != sources.end() && !px; ++i) { + guchar *px = NULL; + for (std::list::iterator i = sources.begin(); (i != sources.end()) && !px; ++i) { gchar *doc_filename = *i; + SVGDocCache *info = 0; // Did we already load this doc? Glib::ustring key(doc_filename); - info = 0; { - std::map::iterator i = doc_cache.find(key); + std::map::iterator i = doc_cache.find(key); if ( i != doc_cache.end() ) { info = i->second; } } - /* Try to load from document. */ - if (!info && - Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) && - (doc = SPDocument::createNewDoc( doc_filename, FALSE )) ) { - - //g_message("Loaded icon file %s", doc_filename); - // prep the document - doc->ensureUpToDate(); - /* Create new arena */ - NRArena *arena = NRArena::create(); - /* Create ArenaItem and set transform */ - unsigned visionkey = SPItem::display_key_new(1); - /* fixme: Memory manage root if needed (Lauris) */ - // This needs to be fixed indeed; this leads to a memory leak of a few megabytes these days - // because shapes are being rendered which are not being freed - // Valgrind output: - /*==7014== 1,548,344 bytes in 599 blocks are possibly lost in loss record 20,361 of 20,362 - ==7014== at 0x4A05974: operator new(unsigned long) (vg_replace_malloc.c:220) - ==7014== by 0x4F1015: __gnu_cxx::new_allocator::allocate(unsigned long, void const*) (new_allocator.h:89) - ==7014== by 0x4F02AC: std::_Vector_base >::_M_allocate(unsigned long) (stl_vector.h:140) - ==7014== by 0xCF62D7: std::vector >::_M_fill_insert(__gnu_cxx::__normal_iterator > >, unsigned long, Shape::point_data const&) (vector.tcc:414) - ==7014== by 0xCF4D45: std::vector >::insert(__gnu_cxx::__normal_iterator > >, unsigned long, Shape::point_data const&) (stl_vector.h:851) - ==7014== by 0xCF3DCD: std::vector >::resize(unsigned long, Shape::point_data) (stl_vector.h:557) - ==7014== by 0xCEA771: Shape::AddPoint(Geom::Point) (Shape.cpp:326) - ==7014== by 0xD0F413: Shape::ConvertToShape(Shape*, fill_typ, bool) (ShapeSweep.cpp:257) - ==7014== by 0x5ECD4F: nr_arena_shape_update_stroke(NRArenaShape*, NRGC*, NRRectL*) (nr-arena-shape.cpp:651) - ==7014== by 0x5EE0DA: nr_arena_shape_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-shape.cpp:862) - ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) - ==7014== by 0x5E9DDE: nr_arena_group_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-group.cpp:228) - ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) - ==7014== by 0x5E9DDE: nr_arena_group_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-group.cpp:228) - ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) - */ - root = SP_ITEM(doc->getRoot())->invoke_show(arena, visionkey, SP_ITEM_SHOW_DISPLAY ); - - // store into the cache - info = new svg_doc_cache_t; - g_assert(info); - - info->doc=doc; - info->root=root; - doc_cache[key]=info; + // Try to load from document. + if (!info && Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) ) { + SPDocument *doc = SPDocument::createNewDoc( doc_filename, FALSE ); + if ( doc ) { + if ( dump ) { + g_message("Loaded icon file %s", doc_filename); + } + // prep the document + doc->ensureUpToDate(); + + // Create new arena + NRArena *arena = NRArena::create(); + + // Create ArenaItem and set transform + unsigned visionkey = SPItem::display_key_new(1); + // fixme: Memory manage root if needed (Lauris) + // This needs to be fixed indeed; this leads to a memory leak of a few megabytes these days + // because shapes are being rendered which are not being freed + NRArenaItem *root = SP_ITEM(doc->getRoot())->invoke_show( arena, visionkey, SP_ITEM_SHOW_DISPLAY ); + + // store into the cache + info = new SVGDocCache(doc, root); + doc_cache[key] = info; + } } if (info) { - doc=info->doc; - root=info->root; - } - - // move on to the next document if we couldn't get anything - if (!info && !doc) { - continue; + for (std::list::const_iterator it = names.begin(); !px && (it != names.end()); ++it ) { + px = sp_icon_doc_icon( info->doc, info->root, it->c_str(), psize ); + } } - - px = sp_icon_doc_icon( doc, root, name, psize ); -// if (px) { -// g_message("Found icon %s in %s", name, doc_filename); -// } } -// if (!px) { -// g_message("Not found icon %s", name); -// } return px; } @@ -1232,44 +1201,52 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) // In file encoding: std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); std::string subpart = getDestDir(psize); - if (subpart.empty()) { - g_message("Is empty"); - } std::string subdir = Glib::build_filename( iconCacheDir, subpart ); if ( !Glib::file_test(subdir, Glib::FILE_TEST_EXISTS) ) { - g_message("NEED SUB OF [%s]", subdir.c_str()); g_mkdir_with_parents( subdir.c_str(), 0x1ED ); } potentialFile = Glib::build_filename( subdir, name ); potentialFile += ".png"; if ( Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { - Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); - if (pb) { - dataLoaded = true; - GdkPixbuf *obj = pb->gobj(); - g_object_ref(obj); - pb_cache[key] = obj; - addToIconSet(obj, name, lsize, psize); - loadNeeded = true; - if (internalNames.find(name) == internalNames.end()) { - internalNames.insert(name); + bool badFile = false; + try { + Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); + if (pb) { + dataLoaded = true; + GdkPixbuf *obj = pb->gobj(); + g_object_ref(obj); + pb_cache[key] = obj; + addToIconSet(obj, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); + } } + } catch ( Glib::FileError &ex ) { + g_warning("FileError [%s]", ex.what().c_str()); + badFile = true; + } catch ( Gdk::PixbufError &ex ) { + g_warning("PixbufError [%s]", ex.what().c_str()); + // Invalid contents. Remove cached item + badFile = true; + } + if ( badFile ) { + g_remove(potentialFile.c_str()); } } } if (!dataLoaded) { - guchar* px = load_svg_pixels(name, lsize, psize); - if ( !px ) { - // check for a fallback name - if ( legacyNames.find(name) != legacyNames.end() ) { - if ( dump ) { - g_message("load_svg_pixels([%s]=%s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize); - } - px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize); + std::list names; + names.push_back(name); + if ( legacyNames.find(name) != legacyNames.end() ) { + names.push_back(legacyNames[name]); + if ( dump ) { + g_message("load_svg_pixels([%s] = %s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize); } } + guchar* px = load_svg_pixels(names, lsize, psize); if (px) { GdkPixbuf* pb = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, TRUE, 8, psize, psize, psize * 4, @@ -1310,7 +1287,9 @@ static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, u // did we already load this icon at this scale/size? GdkPixbuf* pb = get_cached_pixbuf(key); if (!pb) { - guchar *px = load_svg_pixels(name, lsize, psize); + std::list names; + names.push_back(name); + guchar *px = load_svg_pixels(names, lsize, psize); if (px) { pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, psize, psize, psize * 4, -- cgit v1.2.3 From 304b6faeadb88b331f8395c105714f7924b9a7cf Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sat, 18 Dec 2010 10:53:34 -0800 Subject: Add missing include\. (bzr r9955.1.6) --- src/widgets/icon.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 7c8beeb75..46c7fdf90 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include -- cgit v1.2.3 From 415933e6c0f092bb1ec732cc61ddee0ceeb043e0 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sat, 18 Dec 2010 11:50:04 -0800 Subject: Fix other fallback icon lookup. (bzr r9955.1.7) --- src/widgets/icon.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 46c7fdf90..99acbb8ca 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -56,7 +56,7 @@ static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style ); static void sp_icon_theme_changed( SPIcon *icon ); static GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize); -static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize); +static GdkPixbuf *sp_icon_image_load_svg(std::list const &names, GtkIconSize lsize, unsigned psize); static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride, unsigned r, unsigned g, unsigned b ); @@ -230,14 +230,17 @@ GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize pb = gtk_icon_theme_load_icon(theme, name, psize, (GtkIconLookupFlags) 0, NULL); } if (!pb) { - pb = sp_icon_image_load_svg( name, Inkscape::getRegisteredIconSize(lsize), psize ); - if (!pb && (legacyNames.find(name) != legacyNames.end())) { + std::list names; + names.push_back(name); + if ( legacyNames.find(name) != legacyNames.end() ) { if ( Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg") ) { g_message("Checking fallback [%s]->[%s]", name, legacyNames[name].c_str()); } - pb = sp_icon_image_load_svg( legacyNames[name].c_str(), Inkscape::getRegisteredIconSize(lsize), psize ); + names.push_back(legacyNames[name]); } + pb = sp_icon_image_load_svg( names, Inkscape::getRegisteredIconSize(lsize), psize ); + // if this was loaded from SVG, add it as a builtin icon if (pb) { gtk_icon_theme_add_builtin_icon(name, psize, pb); @@ -286,7 +289,7 @@ static void sp_icon_theme_changed( SPIcon *icon ) static void imageMapCB(GtkWidget* widget, gpointer user_data); static void imageMapNamedCB(GtkWidget* widget, gpointer user_data); static bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize); -static Glib::ustring icon_cache_key(gchar const *name, unsigned psize); +static Glib::ustring icon_cache_key(Glib::ustring const &name, unsigned psize); static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key); static void setupLegacyNaming() { @@ -1049,9 +1052,9 @@ public: static std::map doc_cache; static std::map pb_cache; -Glib::ustring icon_cache_key(gchar const *name, unsigned psize) +Glib::ustring icon_cache_key(Glib::ustring const & name, unsigned psize) { - Glib::ustring key=name; + Glib::ustring key = name; key += ":"; key += psize; return key; @@ -1281,22 +1284,20 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) return loadNeeded; } -static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize) +static GdkPixbuf *sp_icon_image_load_svg(std::list const &names, GtkIconSize lsize, unsigned psize) { - Glib::ustring key = icon_cache_key(name, psize); + Glib::ustring key = icon_cache_key(*names.begin(), psize); // did we already load this icon at this scale/size? GdkPixbuf* pb = get_cached_pixbuf(key); if (!pb) { - std::list names; - names.push_back(name); guchar *px = load_svg_pixels(names, lsize, psize); if (px) { pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, psize, psize, psize * 4, (GdkPixbufDestroyNotify)g_free, NULL); pb_cache[key] = pb; - addToIconSet(pb, name, lsize, psize); + addToIconSet(pb, names.begin()->c_str(), lsize, psize); } } -- cgit v1.2.3 From 427a888cb886bf68bf12377cf7f53b78178e46b9 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sat, 18 Dec 2010 14:43:10 -0800 Subject: C++-ification. (bzr r9955.1.8) --- src/widgets/icon.cpp | 244 ++++++++++++++++++++++++++------------------------- 1 file changed, 125 insertions(+), 119 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 99acbb8ca..3b9b47425 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -34,36 +34,58 @@ #include "icon.h" -static gboolean icon_prerender_task(gpointer data); -static void addPreRender( GtkIconSize lsize, gchar const *name ); +struct IconImpl { + static void classInit(SPIconClass *klass); + static void init(SPIcon *icon); -static void sp_icon_class_init(SPIconClass *klass); -static void sp_icon_init(SPIcon *icon); -static void sp_icon_dispose(GObject *object); + static GtkWidget *newFull( Inkscape::IconSize lsize, gchar const *name ); -static void sp_icon_reset(SPIcon *icon); -static void sp_icon_clear(SPIcon *icon); + static void dispose(GObject *object); -static void sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition); -static void sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation); -static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event); + static void reset(SPIcon *icon); + static void clear(SPIcon *icon); -static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area); + static void sizeRequest(GtkWidget *widget, GtkRequisition *requisition); + static void sizeAllocate(GtkWidget *widget, GtkAllocation *allocation); + static int expose(GtkWidget *widget, GdkEventExpose *event); -static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen ); -static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style ); -static void sp_icon_theme_changed( SPIcon *icon ); + static void paint(SPIcon *icon, GdkRectangle const *area); -static GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize); -static GdkPixbuf *sp_icon_image_load_svg(std::list const &names, GtkIconSize lsize, unsigned psize); + static void screenChanged( GtkWidget *widget, GdkScreen *previous_screen ); + static void styleSet( GtkWidget *widget, GtkStyle *previous_style ); + static void themeChanged( SPIcon *icon ); -static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride, - unsigned r, unsigned g, unsigned b ); + static int getPhysSize(int size); + static void fetchPixbuf( SPIcon *icon ); -static void injectCustomSize(); + static gboolean prerenderTask(gpointer data); + static void addPreRender( GtkIconSize lsize, gchar const *name ); + static GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ); + + + static GdkPixbuf *loadPixmap(gchar const *name, unsigned lsize, unsigned psize); + static GdkPixbuf *loadSvg(std::list const &names, GtkIconSize lsize, unsigned psize); + + static void overlayPixels( guchar *px, int width, int height, int stride, + unsigned r, unsigned g, unsigned b ); + + static void injectCustomSize(); + + static void imageMapCB(GtkWidget* widget, gpointer user_data); + static void imageMapNamedCB(GtkWidget* widget, gpointer user_data); + static bool prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psize); + + static void setupLegacyNaming(); + +private: + static GtkWidgetClass *parent_class; + static std::map legacyNames; +}; + +GtkWidgetClass *IconImpl::parent_class = 0; +std::map IconImpl::legacyNames; -static GtkWidgetClass *parent_class; static bool sizeDirty = true; @@ -79,8 +101,6 @@ static GtkIconSize iconSizeLookup[] = { GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION }; -static std::map legacyNames; - class IconCacheItem { public: @@ -95,8 +115,7 @@ public: static std::map > iconSetCache; static std::set internalNames; -GType -sp_icon_get_type() +GType SPIcon::getType() { static GType type = 0; if (!type) { @@ -104,12 +123,12 @@ sp_icon_get_type() sizeof(SPIconClass), NULL, NULL, - (GClassInitFunc) sp_icon_class_init, + reinterpret_cast(IconImpl::classInit), NULL, NULL, sizeof(SPIcon), 0, - (GInstanceInitFunc) sp_icon_init, + reinterpret_cast(IconImpl::init), NULL }; type = g_type_register_static(GTK_TYPE_WIDGET, "SPIcon", &info, (GTypeFlags)0); @@ -117,8 +136,7 @@ sp_icon_get_type() return type; } -static void -sp_icon_class_init(SPIconClass *klass) +void IconImpl::classInit(SPIconClass *klass) { GObjectClass *object_class; GtkWidgetClass *widget_class; @@ -128,18 +146,16 @@ sp_icon_class_init(SPIconClass *klass) parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass); - object_class->dispose = sp_icon_dispose; + object_class->dispose = IconImpl::dispose; - widget_class->size_request = sp_icon_size_request; - widget_class->size_allocate = sp_icon_size_allocate; - widget_class->expose_event = sp_icon_expose; - widget_class->screen_changed = sp_icon_screen_changed; - widget_class->style_set = sp_icon_style_set; + widget_class->size_request = IconImpl::sizeRequest; + widget_class->size_allocate = IconImpl::sizeAllocate; + widget_class->expose_event = IconImpl::expose; + widget_class->screen_changed = IconImpl::screenChanged; + widget_class->style_set = IconImpl::styleSet; } - -static void -sp_icon_init(SPIcon *icon) +void IconImpl::init(SPIcon *icon) { GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW; icon->lsize = Inkscape::ICON_SIZE_BUTTON; @@ -148,11 +164,10 @@ sp_icon_init(SPIcon *icon) icon->pb = 0; } -static void -sp_icon_dispose(GObject *object) +void IconImpl::dispose(GObject *object) { SPIcon *icon = SP_ICON(object); - sp_icon_clear(icon); + clear(icon); if ( icon->name ) { g_free( icon->name ); icon->name = 0; @@ -161,32 +176,32 @@ sp_icon_dispose(GObject *object) ((GObjectClass *) (parent_class))->dispose(object); } -static void sp_icon_reset( SPIcon *icon ) { +void IconImpl::reset( SPIcon *icon ) +{ icon->psize = 0; - sp_icon_clear(icon); + clear(icon); } -static void sp_icon_clear( SPIcon *icon ) { +void IconImpl::clear( SPIcon *icon ) +{ if (icon->pb) { g_object_unref(G_OBJECT(icon->pb)); icon->pb = NULL; } } -static void -sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition) +void IconImpl::sizeRequest(GtkWidget *widget, GtkRequisition *requisition) { SPIcon const *icon = SP_ICON(widget); int const size = ( icon->psize ? icon->psize - : sp_icon_get_phys_size(icon->lsize) ); + : getPhysSize(icon->lsize) ); requisition->width = size; requisition->height = size; } -static void -sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation) +void IconImpl::sizeAllocate(GtkWidget *widget, GtkAllocation *allocation) { widget->allocation = *allocation; @@ -195,34 +210,37 @@ sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation) } } -static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event) +int IconImpl::expose(GtkWidget *widget, GdkEventExpose *event) { if ( GTK_WIDGET_DRAWABLE(widget) ) { SPIcon *icon = SP_ICON(widget); if ( !icon->pb ) { - sp_icon_fetch_pixbuf( icon ); + fetchPixbuf( icon ); } - sp_icon_paint(SP_ICON(widget), &event->area); + paint(icon, &event->area); } return TRUE; } -static GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ); - // PUBLIC CALL: void sp_icon_fetch_pixbuf( SPIcon *icon ) +{ + return IconImpl::fetchPixbuf(icon); +} + +void IconImpl::fetchPixbuf( SPIcon *icon ) { if ( icon ) { if ( !icon->pb ) { - icon->psize = sp_icon_get_phys_size(icon->lsize); + icon->psize = getPhysSize(icon->lsize); icon->pb = renderup(icon->name, icon->lsize, icon->psize); } } } -GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ) { +GdkPixbuf* IconImpl::renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ) { GtkIconTheme *theme = gtk_icon_theme_get_default(); GdkPixbuf *pb = 0; @@ -239,7 +257,7 @@ GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize names.push_back(legacyNames[name]); } - pb = sp_icon_image_load_svg( names, Inkscape::getRegisteredIconSize(lsize), psize ); + pb = loadSvg( names, Inkscape::getRegisteredIconSize(lsize), psize ); // if this was loaded from SVG, add it as a builtin icon if (pb) { @@ -247,7 +265,7 @@ GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize } } if (!pb) { - pb = sp_icon_image_load_pixmap( name, lsize, psize ); + pb = loadPixmap( name, lsize, psize ); } if ( !pb ) { // TODO: We should do something more useful if we can't load the image. @@ -256,43 +274,40 @@ GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize return pb; } -static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen ) +void IconImpl::screenChanged( GtkWidget *widget, GdkScreen *previous_screen ) { if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) { GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen ); } SPIcon *icon = SP_ICON(widget); - sp_icon_theme_changed(icon); + themeChanged(icon); } -static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style ) +void IconImpl::styleSet( GtkWidget *widget, GtkStyle *previous_style ) { if ( GTK_WIDGET_CLASS( parent_class )->style_set ) { GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style ); } SPIcon *icon = SP_ICON(widget); - sp_icon_theme_changed(icon); + themeChanged(icon); } -static void sp_icon_theme_changed( SPIcon *icon ) +void IconImpl::themeChanged( SPIcon *icon ) { bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); if ( dump ) { g_message("Got a change bump for this icon"); } sizeDirty = true; - sp_icon_reset(icon); + reset(icon); gtk_widget_queue_draw( GTK_WIDGET(icon) ); } -static void imageMapCB(GtkWidget* widget, gpointer user_data); -static void imageMapNamedCB(GtkWidget* widget, gpointer user_data); -static bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize); static Glib::ustring icon_cache_key(Glib::ustring const &name, unsigned psize); static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key); -static void setupLegacyNaming() { +void IconImpl::setupLegacyNaming() { legacyNames["document-import"] ="file_import"; legacyNames["document-export"] ="file_export"; legacyNames["document-import-ocal"] ="ocal_import"; @@ -547,7 +562,7 @@ static void setupLegacyNaming() { legacyNames["zoom"] ="sticky_zoom"; } -static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) +GtkWidget *IconImpl::newFull( Inkscape::IconSize lsize, gchar const *name ) { static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); @@ -603,8 +618,8 @@ static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name g_signal_connect( G_OBJECT(widget), "map", G_CALLBACK(imageMapNamedCB), GINT_TO_POINTER(0) ); if ( Inkscape::Preferences::get()->getBool("/options/iconrender/named_nodelay") ) { - int psize = sp_icon_get_phys_size(lsize); - prerender_icon(name, mappedSize, psize); + int psize = getPhysSize(lsize); + prerenderIcon(name, mappedSize, psize); } else { addPreRender( mappedSize, name ); } @@ -622,7 +637,7 @@ static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL); icon->lsize = lsize; icon->name = g_strdup(name); - icon->psize = sp_icon_get_phys_size(lsize); + icon->psize = getPhysSize(lsize); widget = GTK_WIDGET(icon); } @@ -630,17 +645,17 @@ static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name return widget; } -GtkWidget * -sp_icon_new( Inkscape::IconSize lsize, gchar const *name ) +// PUBLIC CALL: +GtkWidget *sp_icon_new( Inkscape::IconSize lsize, gchar const *name ) { - return sp_icon_new_full( lsize, name ); + return IconImpl::newFull( lsize, name ); } // PUBLIC CALL: Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size ) { Gtk::Widget *result = 0; - GtkWidget *widget = sp_icon_new_full( static_cast(Inkscape::getRegisteredIconSize(size)), oid.c_str() ); + GtkWidget *widget = IconImpl::newFull( static_cast(Inkscape::getRegisteredIconSize(size)), oid.c_str() ); if ( widget ) { if ( GTK_IS_IMAGE(widget) ) { @@ -654,22 +669,7 @@ Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size return result; } -GtkIconSize -sp_icon_get_gtk_size(int size) -{ - static GtkIconSize sizemap[64] = {(GtkIconSize)0}; - size = CLAMP(size, 4, 63); - if (!sizemap[size]) { - static int count = 0; - char c[64]; - g_snprintf(c, 64, "InkscapeIcon%d", count++); - sizemap[size] = gtk_icon_size_register(c, size, size); - } - return sizemap[size]; -} - - -static void injectCustomSize() +void IconImpl::injectCustomSize() { // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string. if ( !sizeMapDone ) @@ -700,7 +700,7 @@ static void injectCustomSize() GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size ) { GtkIconSize other = GTK_ICON_SIZE_MENU; - injectCustomSize(); + IconImpl::injectCustomSize(); size = CLAMP( size, Inkscape::ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION ); if ( size == Inkscape::ICON_SIZE_DECORATION ) { other = gtk_icon_size_from_name("inkscape-decoration"); @@ -714,6 +714,11 @@ GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size ) // PUBLIC CALL: int sp_icon_get_phys_size(int size) +{ + return IconImpl::getPhysSize(size); +} + +int IconImpl::getPhysSize(int size) { static bool init = false; static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1]; @@ -828,7 +833,7 @@ int sp_icon_get_phys_size(int size) return vals[size]; } -static void sp_icon_paint(SPIcon *icon, GdkRectangle const */*area*/) +void IconImpl::paint(SPIcon *icon, GdkRectangle const */*area*/) { GtkWidget &widget = *GTK_WIDGET(icon); GdkPixbuf *image = icon->pb; @@ -864,7 +869,7 @@ static void sp_icon_paint(SPIcon *icon, GdkRectangle const */*area*/) } } -GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize) +GdkPixbuf *IconImpl::loadPixmap(gchar const *name, unsigned /*lsize*/, unsigned psize) { gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name); // TODO: bulia, please look over @@ -912,9 +917,8 @@ GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsi } // takes doc, root, icon, and icon name to produce pixels -extern "C" guchar * -sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, - gchar const *name, unsigned psize ) +extern "C" guchar *sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, + gchar const *name, unsigned psize ) { bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); guchar *px = NULL; @@ -1030,7 +1034,7 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, nr_pixblock_release(&B); if ( Inkscape::Preferences::get()->getBool("/debug/icons/overlaySvg") ) { - sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff ); + IconImpl::overlayPixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff ); } } } @@ -1161,13 +1165,13 @@ void Inkscape::queueIconPrerender( Glib::ustring const &name, Inkscape::IconSize if (!stockFound && !themedFound ) { gint trySize = CLAMP( static_cast(lsize), 0, static_cast(G_N_ELEMENTS(iconSizeLookup) - 1) ); if ( !sizeMapDone ) { - injectCustomSize(); + IconImpl::injectCustomSize(); } GtkIconSize mappedSize = iconSizeLookup[trySize]; - int psize = sp_icon_get_phys_size(lsize); + int psize = IconImpl::getPhysSize(lsize); // TODO place in a queue that is triggered by other map events - prerender_icon(name.c_str(), mappedSize, psize); + IconImpl::prerenderIcon(name.c_str(), mappedSize, psize); } } @@ -1185,7 +1189,7 @@ static std::string getDestDir( unsigned psize ) } // returns true if icon needed preloading, false if nothing was done -bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) +bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psize) { bool loadNeeded = false; static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); @@ -1196,7 +1200,7 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) if ((internalNames.find(name) != internalNames.end()) || (!gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), name))) { if (dump) { - g_message("prerender_icon [%s] %d:%d", name, lsize, psize); + g_message("prerenderIcon [%s] %d:%d", name, lsize, psize); } std::string potentialFile; @@ -1278,13 +1282,13 @@ bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) } } else if (dump) { - g_message("prerender_icon [%s] %d NOT!!!!!!", name, psize); + g_message("prerenderIcon [%s] %d NOT!!!!!!", name, psize); } } return loadNeeded; } -static GdkPixbuf *sp_icon_image_load_svg(std::list const &names, GtkIconSize lsize, unsigned psize) +GdkPixbuf *IconImpl::loadSvg(std::list const &names, GtkIconSize lsize, unsigned psize) { Glib::ustring key = icon_cache_key(*names.begin(), psize); @@ -1308,7 +1312,7 @@ static GdkPixbuf *sp_icon_image_load_svg(std::list const &names, return pb; } -void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride, +void IconImpl::overlayPixels(guchar *px, int width, int height, int stride, unsigned r, unsigned g, unsigned b) { int bytesPerPixel = 4; @@ -1380,18 +1384,18 @@ public: static std::vector pendingRenders; static bool callbackHooked = false; -static void addPreRender( GtkIconSize lsize, gchar const *name ) +void IconImpl::addPreRender( GtkIconSize lsize, gchar const *name ) { if ( !callbackHooked ) { callbackHooked = true; - g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL ); + g_idle_add_full( G_PRIORITY_LOW, &prerenderTask, NULL, NULL ); } pendingRenders.push_back(preRenderItem(lsize, name)); } -gboolean icon_prerender_task(gpointer /*data*/) { +gboolean IconImpl::prerenderTask(gpointer /*data*/) { if ( inkscapeIsCrashing() ) { // stop } else if (!pendingRenders.empty()) { @@ -1399,8 +1403,8 @@ gboolean icon_prerender_task(gpointer /*data*/) { do { preRenderItem single = pendingRenders.front(); pendingRenders.erase(pendingRenders.begin()); - int psize = sp_icon_get_phys_size(single._lsize); - workDone = prerender_icon(single._name.c_str(), single._lsize, psize); + int psize = getPhysSize(single._lsize); + workDone = prerenderIcon(single._name.c_str(), single._lsize, psize); } while (!pendingRenders.empty() && !workDone); } @@ -1413,22 +1417,23 @@ gboolean icon_prerender_task(gpointer /*data*/) { } -void imageMapCB(GtkWidget* widget, gpointer user_data) { +void IconImpl::imageMapCB(GtkWidget* widget, gpointer user_data) +{ gchar* id = 0; GtkIconSize size = GTK_ICON_SIZE_INVALID; gtk_image_get_stock(GTK_IMAGE(widget), &id, &size); GtkIconSize lsize = static_cast(GPOINTER_TO_INT(user_data)); if ( id ) { - int psize = sp_icon_get_phys_size(lsize); + int psize = getPhysSize(lsize); g_message("imageMapCB(%p) for [%s]:%d:%d", widget, id, lsize, psize); for ( std::vector::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) { if ( (it->_name == id) && (it->_lsize == lsize) ) { - prerender_icon(id, lsize, psize); + prerenderIcon(id, lsize, psize); pendingRenders.erase(it); g_message(" prerender for %s:%d:%d", id, lsize, psize); if (lsize != size) { - int psize = sp_icon_get_phys_size(size); - prerender_icon(id, size, psize); + int psize = getPhysSize(size); + prerenderIcon(id, size, psize); } break; } @@ -1438,7 +1443,8 @@ void imageMapCB(GtkWidget* widget, gpointer user_data) { g_signal_handlers_disconnect_by_func(widget, (gpointer)imageMapCB, user_data); } -static void imageMapNamedCB(GtkWidget* widget, gpointer user_data) { +void IconImpl::imageMapNamedCB(GtkWidget* widget, gpointer user_data) +{ GtkImage* img = GTK_IMAGE(widget); gchar const* iconName = 0; GtkIconSize size = GTK_ICON_SIZE_INVALID; @@ -1458,8 +1464,8 @@ static void imageMapNamedCB(GtkWidget* widget, gpointer user_data) { for ( std::vector::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) { if ( (it->_name == iconName) && (it->_lsize == size) ) { - int psize = sp_icon_get_phys_size(size); - prerender_icon(iconName, size, psize); + int psize = getPhysSize(size); + prerenderIcon(iconName, size, psize); pendingRenders.erase(it); break; } -- cgit v1.2.3 From e076366297f9aeccb9944b06e3ce5985546d1964 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Tue, 21 Dec 2010 00:13:59 -0800 Subject: Clear cached items when state is invalid. (bzr r9955.1.12) --- src/widgets/icon.cpp | 148 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 145 insertions(+), 3 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 3b9b47425..3e0746edf 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "path-prefix.h" #include "preferences.h" @@ -76,13 +77,23 @@ struct IconImpl { static void imageMapNamedCB(GtkWidget* widget, gpointer user_data); static bool prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psize); + + static std::list &icons_svg_paths(); + static guchar *load_svg_pixels(std::list const &names, + unsigned lsize, unsigned psize); + + static std::string fileEscape( std::string const & str ); + + static void validateCache(); static void setupLegacyNaming(); private: + static const std::string magicNumber; static GtkWidgetClass *parent_class; static std::map legacyNames; }; +const std::string IconImpl::magicNumber = "1.0"; GtkWidgetClass *IconImpl::parent_class = 0; std::map IconImpl::legacyNames; @@ -303,6 +314,132 @@ void IconImpl::themeChanged( SPIcon *icon ) gtk_widget_queue_draw( GTK_WIDGET(icon) ); } +std::string IconImpl::fileEscape( std::string const & str ) +{ + std::string result; + result.reserve(str.size()); + for ( size_t i = 0; i < str.size(); ++i ) { + char ch = str[i]; + if ( (0x20 <= ch) && !(0x80 & ch) ) { + result += ch; + } else { + result += "\\x"; + gchar *tmp = g_strdup_printf("%02X", (0x0ff & ch)); + result += tmp; + g_free(tmp); + } + } + return result; +} + +static bool isSizedSubdir( std::string const &name ) +{ + bool isSized = false; + if ( (name.size() > 2) && (name.size() & 1) ) { // needs to be an odd length 3 or more + size_t mid = (name.size() - 1) / 2; + if ( (name[mid] == 'x') && (name.substr(0, mid) == name.substr(mid + 1)) ) { + isSized = true; + for ( size_t i = 0; (i < mid) && isSized; ++i ) { + isSized &= g_ascii_isdigit(name[i]); + } + } + } + return isSized; +} + +void IconImpl::validateCache() +{ + std::list &sources = icons_svg_paths(); + std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); + std::string iconCacheFile = Glib::build_filename( iconCacheDir, "cache.info" ); + + std::vector filesFound; + + for (std::list::iterator i = sources.begin(); i != sources.end(); ++i) { + gchar const* potentialFile = *i; + if ( Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { + filesFound.push_back(*i); + } + } + + unsigned long lastSeen = 0; + std::ostringstream out; + out << "Inkscape cache v" << std::hex << magicNumber << std::dec << std::endl; + out << "Sourcefiles: " << filesFound.size() << std::endl; + for ( std::vector::iterator it = filesFound.begin(); it != filesFound.end(); ++it ) { + GStatBuf st; + memset(&st, 0, sizeof(st)); + if ( !g_stat(it->c_str(), &st) ) { + unsigned long when = st.st_mtime; + lastSeen = std::max(lastSeen, when); + out << std::hex << when << std::dec << " " << fileEscape(*it) << std::endl; + } else { + out << "0 " << fileEscape(*it) << std::endl; + } + } + std::string wanted = out.str(); + + std::string present; + { + gchar *contents = 0; + if ( g_file_get_contents(iconCacheFile.c_str(), &contents, 0, 0) ) { + if ( contents ) { + present = contents; + } + g_free(contents); + contents = 0; + } + } + bool cacheValid = (present == wanted); + + if ( cacheValid ) { + // Check if any cached rasters are out of date + Glib::Dir dir(iconCacheDir); + for ( Glib::DirIterator it = dir.begin(); cacheValid && (it != dir.end()); ++it ) { + if ( isSizedSubdir(*it) ) { + std::string subdirName = Glib::build_filename( iconCacheDir, *it ); + Glib::Dir subdir(subdirName); + for ( Glib::DirIterator subit = subdir.begin(); cacheValid && (subit != subdir.end()); ++subit ) { + std::string fullpath = Glib::build_filename( subdirName, *subit ); + if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { + GStatBuf st; + memset(&st, 0, sizeof(st)); + if ( !g_stat(fullpath.c_str(), &st) ) { + unsigned long when = st.st_mtime; + if ( when < lastSeen ) { + cacheValid = false; + } + } + } + } + } + } + } + + if ( !cacheValid ) { + // Purge existing icons, but not possible future sub-directories. + Glib::Dir dir(iconCacheDir); + for ( Glib::DirIterator it = dir.begin(); it != dir.end(); ++it ) { + if ( isSizedSubdir(*it) ) { + std::string subdirName = Glib::build_filename( iconCacheDir, *it ); + Glib::Dir subdir(subdirName); + for ( Glib::DirIterator subit = subdir.begin(); subit != subdir.end(); ++subit ) { + std::string fullpath = Glib::build_filename( subdirName, *subit ); + if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { + g_remove(fullpath.c_str()); + } + } + g_rmdir( subdirName.c_str() ); + } + } + + if ( g_file_set_contents(iconCacheFile.c_str(), wanted.c_str(), wanted.size(), 0) ) { + // Caching may proceed + } else { + g_warning("Unable to write cache info file."); + } + } +} static Glib::ustring icon_cache_key(Glib::ustring const &name, unsigned psize); static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key); @@ -1073,7 +1210,7 @@ GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) { return pb; } -static std::list &icons_svg_paths() +std::list &IconImpl::icons_svg_paths() { static std::list sources; static bool initialized = false; @@ -1089,8 +1226,8 @@ static std::list &icons_svg_paths() } // this function renders icons from icons.svg and returns the pixels. -static guchar *load_svg_pixels(std::list const &names, - unsigned /*lsize*/, unsigned psize) +guchar *IconImpl::load_svg_pixels(std::list const &names, + unsigned /*lsize*/, unsigned psize) { bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); std::list &sources = icons_svg_paths(); @@ -1194,6 +1331,11 @@ bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psiz bool loadNeeded = false; static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); static bool useCache = Inkscape::Preferences::get()->getBool("/debug/icons/useCache"); + static bool cacheValidated = false; + if (!cacheValidated) { + cacheValidated = true; + validateCache(); + } Glib::ustring key = icon_cache_key(name, psize); if ( !get_cached_pixbuf(key) ) { -- cgit v1.2.3 From 20e4fc4bff32e775567487e16bb19e559475b628 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Tue, 21 Dec 2010 00:33:29 -0800 Subject: Enable icon disk cache by default. (bzr r9970) --- src/widgets/icon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 3e0746edf..bfdf2d3b9 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -1330,7 +1330,7 @@ bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psiz { bool loadNeeded = false; static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); - static bool useCache = Inkscape::Preferences::get()->getBool("/debug/icons/useCache"); + static bool useCache = Inkscape::Preferences::get()->getBool("/debug/icons/useCache", true); static bool cacheValidated = false; if (!cacheValidated) { cacheValidated = true; -- cgit v1.2.3 From ed0d3e31f2e6860a20661853c320c2eb8941e41a Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Tue, 21 Dec 2010 11:26:53 -0800 Subject: Unify stat type for older glib. (bzr r9972) --- src/widgets/icon.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index bfdf2d3b9..526309727 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -35,6 +35,14 @@ #include "icon.h" +// Bring in work-around for Glib versions missing GStatBuf +#if !GLIB_CHECK_VERSION(2,25,0) +#if defined (_MSC_VER) && !defined(_WIN64) +typedef struct _stat32 GStatBuf; +#else //defined (_MSC_VER) && !defined(_WIN64) +typedef struct stat GStatBuf; +#endif //defined (_MSC_VER) && !defined(_WIN64) +#endif //!GLIB_CHECK_VERSION(2,25,0) struct IconImpl { static void classInit(SPIconClass *klass); -- cgit v1.2.3 From a7ee5b9cbdb640f1bcefb9a5b500d0705a47808f Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Tue, 21 Dec 2010 12:43:41 -0800 Subject: Handle missing, inaccessible and misnamed cache directories. (bzr r9974) --- src/widgets/icon.cpp | 64 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 26 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 526309727..e7207d944 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -406,16 +406,18 @@ void IconImpl::validateCache() for ( Glib::DirIterator it = dir.begin(); cacheValid && (it != dir.end()); ++it ) { if ( isSizedSubdir(*it) ) { std::string subdirName = Glib::build_filename( iconCacheDir, *it ); - Glib::Dir subdir(subdirName); - for ( Glib::DirIterator subit = subdir.begin(); cacheValid && (subit != subdir.end()); ++subit ) { - std::string fullpath = Glib::build_filename( subdirName, *subit ); - if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { - GStatBuf st; - memset(&st, 0, sizeof(st)); - if ( !g_stat(fullpath.c_str(), &st) ) { - unsigned long when = st.st_mtime; - if ( when < lastSeen ) { - cacheValid = false; + if ( Glib::file_test(subdirName, Glib::FILE_TEST_IS_DIR) ) { + Glib::Dir subdir(subdirName); + for ( Glib::DirIterator subit = subdir.begin(); cacheValid && (subit != subdir.end()); ++subit ) { + std::string fullpath = Glib::build_filename( subdirName, *subit ); + if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { + GStatBuf st; + memset(&st, 0, sizeof(st)); + if ( !g_stat(fullpath.c_str(), &st) ) { + unsigned long when = st.st_mtime; + if ( when < lastSeen ) { + cacheValid = false; + } } } } @@ -425,20 +427,28 @@ void IconImpl::validateCache() } if ( !cacheValid ) { - // Purge existing icons, but not possible future sub-directories. - Glib::Dir dir(iconCacheDir); - for ( Glib::DirIterator it = dir.begin(); it != dir.end(); ++it ) { - if ( isSizedSubdir(*it) ) { - std::string subdirName = Glib::build_filename( iconCacheDir, *it ); - Glib::Dir subdir(subdirName); - for ( Glib::DirIterator subit = subdir.begin(); subit != subdir.end(); ++subit ) { - std::string fullpath = Glib::build_filename( subdirName, *subit ); - if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { - g_remove(fullpath.c_str()); + if ( Glib::file_test(iconCacheDir, Glib::FILE_TEST_EXISTS) ) { + // Purge existing icons, but not possible future sub-directories. + if ( Glib::file_test(iconCacheDir, Glib::FILE_TEST_IS_DIR) ) { + Glib::Dir dir(iconCacheDir); + for ( Glib::DirIterator it = dir.begin(); it != dir.end(); ++it ) { + if ( isSizedSubdir(*it) ) { + std::string subdirName = Glib::build_filename( iconCacheDir, *it ); + if ( Glib::file_test(subdirName, Glib::FILE_TEST_IS_DIR) ) { + Glib::Dir subdir(subdirName); + for ( Glib::DirIterator subit = subdir.begin(); subit != subdir.end(); ++subit ) { + std::string fullpath = Glib::build_filename( subdirName, *subit ); + if ( Glib::file_test(fullpath, Glib::FILE_TEST_EXISTS) && !Glib::file_test(fullpath, Glib::FILE_TEST_IS_DIR) ) { + g_remove(fullpath.c_str()); + } + } + g_rmdir( subdirName.c_str() ); + } } } - g_rmdir( subdirName.c_str() ); } + } else { + g_mkdir_with_parents( iconCacheDir.c_str(), 0x1ED ); } if ( g_file_set_contents(iconCacheFile.c_str(), wanted.c_str(), wanted.size(), 0) ) { @@ -1342,7 +1352,9 @@ bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psiz static bool cacheValidated = false; if (!cacheValidated) { cacheValidated = true; - validateCache(); + if ( useCache ) { + validateCache(); + } } Glib::ustring key = icon_cache_key(name, psize); @@ -1382,10 +1394,10 @@ bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psiz } } } catch ( Glib::FileError &ex ) { - g_warning("FileError [%s]", ex.what().c_str()); + //g_warning("FileError [%s]", ex.what().c_str()); badFile = true; } catch ( Gdk::PixbufError &ex ) { - g_warning("PixbufError [%s]", ex.what().c_str()); + //g_warning("PixbufError [%s]", ex.what().c_str()); // Invalid contents. Remove cached item badFile = true; } @@ -1421,9 +1433,9 @@ bool IconImpl::prerenderIcon(gchar const *name, GtkIconSize lsize, unsigned psiz try { ppp->save( potentialFile, "png" ); } catch ( Glib::FileError &ex ) { - g_warning("FileError [%s]", ex.what().c_str()); + //g_warning("FileError [%s]", ex.what().c_str()); } catch ( Gdk::PixbufError &ex ) { - g_warning("PixbufError [%s]", ex.what().c_str()); + //g_warning("PixbufError [%s]", ex.what().c_str()); } } } else if (dump) { -- cgit v1.2.3 From 53933f5fea9d07d1ba6304b88439fba257ee8c34 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Wed, 2 Feb 2011 22:24:36 +0100 Subject: update to latest 2geom ! (bzr r10025) --- src/widgets/icon.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index e7207d944..25be59942 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -1082,7 +1082,7 @@ extern "C" guchar *sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, SPObject *object = doc->getObjectById(name); if (object && SP_IS_ITEM(object)) { /* Find bbox in document */ - Geom::Matrix const i2doc(SP_ITEM(object)->i2doc_affine()); + Geom::Affine const i2doc(SP_ITEM(object)->i2doc_affine()); Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc); if ( SP_OBJECT_PARENT(object) == NULL ) @@ -1096,7 +1096,7 @@ extern "C" guchar *sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, NRGC gc(NULL); /* Update to renderable state */ double sf = 1.0; - nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf)); + nr_arena_item_set_transform(root, (Geom::Affine)Geom::Scale(sf, sf)); gc.transform.setIdentity(); nr_arena_item_invoke_update( root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, @@ -1128,7 +1128,7 @@ extern "C" guchar *sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, } sf = (double)psize / (double)block; - nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf)); + nr_arena_item_set_transform(root, (Geom::Affine)Geom::Scale(sf, sf)); gc.transform.setIdentity(); nr_arena_item_invoke_update( root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, -- cgit v1.2.3 From aa8c0d876949c952ab355c096017301a00734456 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Mon, 21 Feb 2011 13:43:30 -0800 Subject: Finished cleanup of outdated SP_OBJECT_PARENT C macro. (bzr r10064) --- src/widgets/icon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widgets/icon.cpp') diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index 25be59942..c2634f6a1 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -1085,7 +1085,7 @@ extern "C" guchar *sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, Geom::Affine const i2doc(SP_ITEM(object)->i2doc_affine()); Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc); - if ( SP_OBJECT_PARENT(object) == NULL ) + if ( object->parent == NULL ) { dbox = Geom::Rect(Geom::Point(0, 0), Geom::Point(doc->getWidth(), doc->getHeight())); -- cgit v1.2.3