1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 #include "document-private.h"
5 #include <dir-util.h>
6 #include "extension/system.h"
7 #include "gdkpixbuf-input.h"
8 #include "selection-chemistry.h"
10 namespace Inkscape {
12 namespace IO {
13 GdkPixbuf* pixbuf_new_from_file( char const *utf8name, GError **error );
14 }
16 namespace Extension {
17 namespace Internal {
19 SPDocument *
20 GdkpixbufInput::open(Inkscape::Extension::Input */*mod*/, char const *uri)
21 {
22 SPDocument *doc = sp_document_new(NULL, TRUE, TRUE);
23 GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
25 if (pb) { /* We are readable */
26 bool saved = sp_document_get_undo_sensitive(doc);
27 sp_document_set_undo_sensitive(doc, false); // no need to undo in this temporary document
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");
58 repr->setAttribute("xlink:href", uri);
59 /* impl: doc->base is currently NULL, so we can use uri for href whether it's absolute
60 * or relative. The href will get rewritten by rebase_hrefs if by chance uri is relative
61 * and doc gets saved to a different directory.
62 *
63 * We don't bother setting sodipodi:absref, as we assume it's never useful to have
64 * sodipodi:absref with the same value as xlink:href, and rebase_hrefs will provide
65 * sodipodi:absref values where necessary. */
67 sp_repr_set_svg_double(repr, "width", width);
68 sp_repr_set_svg_double(repr, "height", height);
70 SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
71 Inkscape::GC::release(repr);
72 gdk_pixbuf_unref(pb);
73 //alter the canvas size to fit the image size
74 fit_canvas_to_drawing(doc);
75 // restore undo, as now this document may be shown to the user if a bitmap was opened
76 sp_document_set_undo_sensitive(doc, saved);
77 } else {
78 printf("GdkPixbuf loader failed\n");
79 }
81 return doc;
82 }
84 #include "clear-n_.h"
86 void
87 GdkpixbufInput::init(void)
88 {
89 GSList * formatlist, * formatlisthead;
91 /* \todo I'm not sure if I need to free this list */
92 for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
93 formatlist != NULL;
94 formatlist = g_slist_next(formatlist)) {
96 GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
98 gchar *name = gdk_pixbuf_format_get_name(pixformat);
99 gchar *description = gdk_pixbuf_format_get_description(pixformat);
100 gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
101 gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
103 for (int i = 0; extensions[i] != NULL; i++) {
104 for (int j = 0; mimetypes[j] != NULL; j++) {
106 /* thanks but no thanks, we'll handle SVG extensions... */
107 if (strcmp(extensions[i], "svg") == 0) {
108 continue;
109 }
110 if (strcmp(extensions[i], "svgz") == 0) {
111 continue;
112 }
113 if (strcmp(extensions[i], "svg.gz") == 0) {
114 continue;
115 }
117 gchar *xmlString = g_strdup_printf(
118 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
119 "<name>" N_("%s GDK pixbuf Input") "</name>\n"
120 "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
121 "<input>\n"
122 "<extension>.%s</extension>\n"
123 "<mimetype>%s</mimetype>\n"
124 "<filetypename>%s (*.%s)</filetypename>\n"
125 "<filetypetooltip>%s</filetypetooltip>\n"
126 "</input>\n"
127 "</inkscape-extension>",
128 name,
129 extensions[i],
130 extensions[i],
131 mimetypes[j],
132 name,
133 extensions[i],
134 description
135 );
137 Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
138 g_free(xmlString);
139 }}
141 g_free(name);
142 g_free(description);
143 g_strfreev(mimetypes);
144 g_strfreev(extensions);
145 }
147 g_slist_free(formatlisthead);
148 }
150 } } } /* namespace Inkscape, Extension, Implementation */
152 /*
153 Local Variables:
154 mode:c++
155 c-file-style:"stroustrup"
156 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157 indent-tabs-mode:nil
158 fill-column:99
159 End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :