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 );
27 Inkscape::XML::Node *rdoc = sp_document_repr_root(doc);
28 gchar const *docbase = rdoc->attribute("sodipodi:docbase");
29 gchar const *relname = sp_relative_path_from_path(uri, docbase);
31 if (pb) { /* We are readable */
32 Inkscape::XML::Node *repr = NULL;
34 double width = gdk_pixbuf_get_width(pb);
35 double height = gdk_pixbuf_get_height(pb);
36 gchar const *str = gdk_pixbuf_get_option( pb, "Inkscape::DpiX" );
37 if ( str )
38 {
39 gint dpi = atoi(str);
40 if ( dpi > 0 && dpi != 72 )
41 {
42 double scale = 72.0 / (double)dpi;
43 width *= scale;
44 }
45 }
46 str = gdk_pixbuf_get_option( pb, "Inkscape::DpiY" );
47 if ( str )
48 {
49 gint dpi = atoi(str);
50 if ( dpi > 0 && dpi != 72 )
51 {
52 double scale = 72.0 / (double)dpi;
53 height *= scale;
54 }
55 }
57 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
58 if (prefs_get_int_attribute("options.importbitmapsasimages", "value", 1) == 1) {
59 // import as <image>
60 repr = xml_doc->createElement("svg:image");
61 repr->setAttribute("xlink:href", relname);
62 repr->setAttribute("sodipodi:absref", uri);
64 sp_repr_set_svg_double(repr, "width", width);
65 sp_repr_set_svg_double(repr, "height", height);
67 } else {
68 // import as pattern-filled rect
69 Inkscape::XML::Node *pat = xml_doc->createElement("svg:pattern");
70 pat->setAttribute("inkscape:collect", "always");
71 pat->setAttribute("patternUnits", "userSpaceOnUse");
72 sp_repr_set_svg_double(pat, "width", width);
73 sp_repr_set_svg_double(pat, "height", height);
74 SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->appendChild(pat);
75 gchar const *pat_id = pat->attribute("id");
76 SPObject *pat_object = doc->getObjectById(pat_id);
78 Inkscape::XML::Node *im = xml_doc->createElement("svg:image");
79 im->setAttribute("xlink:href", relname);
80 im->setAttribute("sodipodi:absref", uri);
81 sp_repr_set_svg_double(im, "width", width);
82 sp_repr_set_svg_double(im, "height", height);
83 SP_OBJECT_REPR(pat_object)->addChild(im, NULL);
85 repr = xml_doc->createElement("svg:rect");
86 repr->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
87 sp_repr_set_svg_double(repr, "width", width);
88 sp_repr_set_svg_double(repr, "height", height);
89 }
91 SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
92 Inkscape::GC::release(repr);
93 gdk_pixbuf_unref(pb);
94 //alter the canvas size to fit the image size
95 fit_canvas_to_drawing(doc);
96 // restore undo, as now this document may be shown to the user if a bitmap was opened
97 sp_document_set_undo_sensitive(doc, saved);
98 } else {
99 printf("GdkPixbuf loader failed\n");
100 }
102 return doc;
103 }
105 #include "clear-n_.h"
107 void
108 GdkpixbufInput::init(void)
109 {
110 GSList * formatlist, * formatlisthead;
112 /* \todo I'm not sure if I need to free this list */
113 for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
114 formatlist != NULL;
115 formatlist = g_slist_next(formatlist)) {
117 GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
119 gchar *name = gdk_pixbuf_format_get_name(pixformat);
120 gchar *description = gdk_pixbuf_format_get_description(pixformat);
121 gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
122 gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
124 for (int i = 0; extensions[i] != NULL; i++) {
125 for (int j = 0; mimetypes[j] != NULL; j++) {
127 /* thanks but no thanks, we'll handle SVG extensions... */
128 if (strcmp(extensions[i], "svg") == 0) {
129 continue;
130 }
131 if (strcmp(extensions[i], "svgz") == 0) {
132 continue;
133 }
134 if (strcmp(extensions[i], "svg.gz") == 0) {
135 continue;
136 }
138 gchar *xmlString = g_strdup_printf(
139 "<inkscape-extension>\n"
140 "<name>" N_("%s GDK pixbuf Input") "</name>\n"
141 "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
142 "<input>\n"
143 "<extension>.%s</extension>\n"
144 "<mimetype>%s</mimetype>\n"
145 "<filetypename>%s (*.%s)</filetypename>\n"
146 "<filetypetooltip>%s</filetypetooltip>\n"
147 "</input>\n"
148 "</inkscape-extension>",
149 name,
150 extensions[i],
151 extensions[i],
152 mimetypes[j],
153 name,
154 extensions[i],
155 description
156 );
158 Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
159 g_free(xmlString);
160 }}
162 g_free(name);
163 g_free(description);
164 g_strfreev(mimetypes);
165 g_strfreev(extensions);
166 }
168 g_slist_free(formatlisthead);
169 }
171 } } } /* namespace Inkscape, Extension, Implementation */
173 /*
174 Local Variables:
175 mode:c++
176 c-file-style:"stroustrup"
177 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178 indent-tabs-mode:nil
179 fill-column:99
180 End:
181 */
182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :