Code

fix crash bug #197664
[inkscape.git] / src / extension / internal / gdkpixbuf-input.cpp
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 #include "document-private.h"
5 #include <dir-util.h>
6 #include "prefs-utils.h"
7 #include "extension/system.h"
8 #include "gdkpixbuf-input.h"
9 #include "selection-chemistry.h"
11 namespace Inkscape {
13 namespace IO {
14 GdkPixbuf* pixbuf_new_from_file( char const *utf8name, GError **error );
15 }
17 namespace Extension {
18 namespace Internal {
20 SPDocument *
21 GdkpixbufInput::open(Inkscape::Extension::Input */*mod*/, char const *uri)
22 {
23     SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
24     bool saved = sp_document_get_undo_sensitive(doc);
25     sp_document_set_undo_sensitive(doc, false); // no need to undo in this temporary document
26     GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
28     if (pb) {         /* We are readable */
29         Inkscape::XML::Node *repr = NULL;
31         double width = gdk_pixbuf_get_width(pb);
32         double height = gdk_pixbuf_get_height(pb);
33         gchar const *str = gdk_pixbuf_get_option( pb, "Inkscape::DpiX" );
34         if ( str )
35         {
36             gint dpi = atoi(str);
37             if ( dpi > 0 && dpi != 72 )
38             {
39                 double scale = 72.0 / (double)dpi;
40                 width *= scale;
41             }
42         }
43         str = gdk_pixbuf_get_option( pb, "Inkscape::DpiY" );
44         if ( str )
45         {
46             gint dpi = atoi(str);
47             if ( dpi > 0 && dpi != 72 )
48             {
49                 double scale = 72.0 / (double)dpi;
50                 height *= scale;
51             }
52         }
54         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
55         // import as <image>
56         repr = xml_doc->createElement("svg:image");
57         // both are the same, as we don't know our base dir here and cannot relativate href (importer will fixupHrefs):
58         repr->setAttribute("xlink:href", uri);
59         repr->setAttribute("sodipodi:absref", uri);
61         sp_repr_set_svg_double(repr, "width", width);
62         sp_repr_set_svg_double(repr, "height", height);
64         SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
65         Inkscape::GC::release(repr);
66         gdk_pixbuf_unref(pb);
67         //alter the canvas size to fit the image size
68         fit_canvas_to_drawing(doc);
69     } else {
70         printf("GdkPixbuf loader failed\n");
71     }
73     // restore undo, as now this document may be shown to the user if a bitmap was opened
74     sp_document_set_undo_sensitive(doc, saved);
76     return doc;
77 }
79 #include "clear-n_.h"
81 void
82 GdkpixbufInput::init(void)
83 {
84     GSList * formatlist, * formatlisthead;
86     /* \todo I'm not sure if I need to free this list */
87     for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
88          formatlist != NULL;
89          formatlist = g_slist_next(formatlist)) {
91         GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
93         gchar *name =        gdk_pixbuf_format_get_name(pixformat);
94         gchar *description = gdk_pixbuf_format_get_description(pixformat);
95         gchar **extensions =  gdk_pixbuf_format_get_extensions(pixformat);
96         gchar **mimetypes =   gdk_pixbuf_format_get_mime_types(pixformat);
98         for (int i = 0; extensions[i] != NULL; i++) {
99         for (int j = 0; mimetypes[j] != NULL; j++) {
101             /* thanks but no thanks, we'll handle SVG extensions... */
102             if (strcmp(extensions[i], "svg") == 0) {
103                 continue;
104             }
105             if (strcmp(extensions[i], "svgz") == 0) {
106                 continue;
107             }
108             if (strcmp(extensions[i], "svg.gz") == 0) {
109                 continue;
110             }
112             gchar *xmlString = g_strdup_printf(
113                 "<inkscape-extension>\n"
114                     "<name>" N_("%s GDK pixbuf Input") "</name>\n"
115                     "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
116                     "<input>\n"
117                         "<extension>.%s</extension>\n"
118                         "<mimetype>%s</mimetype>\n"
119                         "<filetypename>%s (*.%s)</filetypename>\n"
120                         "<filetypetooltip>%s</filetypetooltip>\n"
121                     "</input>\n"
122                 "</inkscape-extension>",
123                 name,
124                 extensions[i],
125                 extensions[i],
126                 mimetypes[j],
127                 name,
128                 extensions[i],
129                 description
130                 );
132             Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
133             g_free(xmlString);
134         }}
136         g_free(name);
137         g_free(description);
138         g_strfreev(mimetypes);
139         g_strfreev(extensions);
140     }
142     g_slist_free(formatlisthead);
145 } } }  /* namespace Inkscape, Extension, Implementation */
147 /*
148   Local Variables:
149   mode:c++
150   c-file-style:"stroustrup"
151   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
152   indent-tabs-mode:nil
153   fill-column:99
154   End:
155 */
156 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :