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 if (prefs_get_int_attribute("options.importbitmapsasimages", "value", 1) == 1) {
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 } else {
66 // import as pattern-filled rect
67 Inkscape::XML::Node *pat = xml_doc->createElement("svg:pattern");
68 pat->setAttribute("inkscape:collect", "always");
69 pat->setAttribute("patternUnits", "userSpaceOnUse");
70 sp_repr_set_svg_double(pat, "width", width);
71 sp_repr_set_svg_double(pat, "height", height);
72 SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->appendChild(pat);
73 gchar const *pat_id = pat->attribute("id");
74 SPObject *pat_object = doc->getObjectById(pat_id);
76 Inkscape::XML::Node *im = xml_doc->createElement("svg:image");
77 im->setAttribute("xlink:href", uri);
78 im->setAttribute("sodipodi:absref", uri);
79 sp_repr_set_svg_double(im, "width", width);
80 sp_repr_set_svg_double(im, "height", height);
81 SP_OBJECT_REPR(pat_object)->addChild(im, NULL);
83 repr = xml_doc->createElement("svg:rect");
84 repr->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
85 sp_repr_set_svg_double(repr, "width", width);
86 sp_repr_set_svg_double(repr, "height", height);
87 }
89 SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
90 Inkscape::GC::release(repr);
91 gdk_pixbuf_unref(pb);
92 //alter the canvas size to fit the image size
93 fit_canvas_to_drawing(doc);
94 // restore undo, as now this document may be shown to the user if a bitmap was opened
95 sp_document_set_undo_sensitive(doc, saved);
96 } else {
97 printf("GdkPixbuf loader failed\n");
98 }
100 return doc;
101 }
103 #include "clear-n_.h"
105 void
106 GdkpixbufInput::init(void)
107 {
108 GSList * formatlist, * formatlisthead;
110 /* \todo I'm not sure if I need to free this list */
111 for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
112 formatlist != NULL;
113 formatlist = g_slist_next(formatlist)) {
115 GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
117 gchar *name = gdk_pixbuf_format_get_name(pixformat);
118 gchar *description = gdk_pixbuf_format_get_description(pixformat);
119 gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
120 gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
122 for (int i = 0; extensions[i] != NULL; i++) {
123 for (int j = 0; mimetypes[j] != NULL; j++) {
125 /* thanks but no thanks, we'll handle SVG extensions... */
126 if (strcmp(extensions[i], "svg") == 0) {
127 continue;
128 }
129 if (strcmp(extensions[i], "svgz") == 0) {
130 continue;
131 }
132 if (strcmp(extensions[i], "svg.gz") == 0) {
133 continue;
134 }
136 gchar *xmlString = g_strdup_printf(
137 "<inkscape-extension>\n"
138 "<name>" N_("%s GDK pixbuf Input") "</name>\n"
139 "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
140 "<input>\n"
141 "<extension>.%s</extension>\n"
142 "<mimetype>%s</mimetype>\n"
143 "<filetypename>%s (*.%s)</filetypename>\n"
144 "<filetypetooltip>%s</filetypetooltip>\n"
145 "</input>\n"
146 "</inkscape-extension>",
147 name,
148 extensions[i],
149 extensions[i],
150 mimetypes[j],
151 name,
152 extensions[i],
153 description
154 );
156 Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
157 g_free(xmlString);
158 }}
160 g_free(name);
161 g_free(description);
162 g_strfreev(mimetypes);
163 g_strfreev(extensions);
164 }
166 g_slist_free(formatlisthead);
167 }
169 } } } /* namespace Inkscape, Extension, Implementation */
171 /*
172 Local Variables:
173 mode:c++
174 c-file-style:"stroustrup"
175 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
176 indent-tabs-mode:nil
177 fill-column:99
178 End:
179 */
180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :