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