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 // restore undo, as now this document may be shown to the user if a bitmap was opened
70 sp_document_set_undo_sensitive(doc, saved);
71 } else {
72 printf("GdkPixbuf loader failed\n");
73 }
75 return doc;
76 }
78 #include "clear-n_.h"
80 void
81 GdkpixbufInput::init(void)
82 {
83 GSList * formatlist, * formatlisthead;
85 /* \todo I'm not sure if I need to free this list */
86 for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
87 formatlist != NULL;
88 formatlist = g_slist_next(formatlist)) {
90 GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
92 gchar *name = gdk_pixbuf_format_get_name(pixformat);
93 gchar *description = gdk_pixbuf_format_get_description(pixformat);
94 gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
95 gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
97 for (int i = 0; extensions[i] != NULL; i++) {
98 for (int j = 0; mimetypes[j] != NULL; j++) {
100 /* thanks but no thanks, we'll handle SVG extensions... */
101 if (strcmp(extensions[i], "svg") == 0) {
102 continue;
103 }
104 if (strcmp(extensions[i], "svgz") == 0) {
105 continue;
106 }
107 if (strcmp(extensions[i], "svg.gz") == 0) {
108 continue;
109 }
111 gchar *xmlString = g_strdup_printf(
112 "<inkscape-extension>\n"
113 "<name>" N_("%s GDK pixbuf Input") "</name>\n"
114 "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
115 "<input>\n"
116 "<extension>.%s</extension>\n"
117 "<mimetype>%s</mimetype>\n"
118 "<filetypename>%s (*.%s)</filetypename>\n"
119 "<filetypetooltip>%s</filetypetooltip>\n"
120 "</input>\n"
121 "</inkscape-extension>",
122 name,
123 extensions[i],
124 extensions[i],
125 mimetypes[j],
126 name,
127 extensions[i],
128 description
129 );
131 Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
132 g_free(xmlString);
133 }}
135 g_free(name);
136 g_free(description);
137 g_strfreev(mimetypes);
138 g_strfreev(extensions);
139 }
141 g_slist_free(formatlisthead);
142 }
144 } } } /* namespace Inkscape, Extension, Implementation */
146 /*
147 Local Variables:
148 mode:c++
149 c-file-style:"stroustrup"
150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
151 indent-tabs-mode:nil
152 fill-column:99
153 End:
154 */
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :