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 GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
26 if (pb) { /* We are readable */
27 bool saved = sp_document_get_undo_sensitive(doc);
28 sp_document_set_undo_sensitive(doc, false); // no need to undo in this temporary document
30 Inkscape::XML::Node *repr = NULL;
32 double width = gdk_pixbuf_get_width(pb);
33 double height = gdk_pixbuf_get_height(pb);
34 gchar const *str = gdk_pixbuf_get_option( pb, "Inkscape::DpiX" );
35 if ( str )
36 {
37 gint dpi = atoi(str);
38 if ( dpi > 0 && dpi != 72 )
39 {
40 double scale = 72.0 / (double)dpi;
41 width *= scale;
42 }
43 }
44 str = gdk_pixbuf_get_option( pb, "Inkscape::DpiY" );
45 if ( str )
46 {
47 gint dpi = atoi(str);
48 if ( dpi > 0 && dpi != 72 )
49 {
50 double scale = 72.0 / (double)dpi;
51 height *= scale;
52 }
53 }
55 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
56 // import as <image>
57 repr = xml_doc->createElement("svg:image");
58 // both are the same, as we don't know our base dir here and cannot relativate href (importer will fixupHrefs):
59 repr->setAttribute("xlink:href", uri);
60 repr->setAttribute("sodipodi:absref", uri);
62 sp_repr_set_svg_double(repr, "width", width);
63 sp_repr_set_svg_double(repr, "height", height);
65 SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
66 Inkscape::GC::release(repr);
67 gdk_pixbuf_unref(pb);
68 //alter the canvas size to fit the image size
69 fit_canvas_to_drawing(doc);
70 // restore undo, as now this document may be shown to the user if a bitmap was opened
71 sp_document_set_undo_sensitive(doc, saved);
72 } else {
73 printf("GdkPixbuf loader failed\n");
74 }
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);
143 }
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 :