ee9fc408637de5e03e1622bcf647026d5b147add
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 }
101 void
102 GdkpixbufInput::init(void)
103 {
104 GSList * formatlist, * formatlisthead;
106 /* \todo I'm not sure if I need to free this list */
107 for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
108 formatlist != NULL;
109 formatlist = g_slist_next(formatlist)) {
111 GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
113 gchar *name = gdk_pixbuf_format_get_name(pixformat);
114 gchar *description = gdk_pixbuf_format_get_description(pixformat);
115 gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
116 gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
118 for (int i = 0; extensions[i] != NULL; i++) {
119 for (int j = 0; mimetypes[j] != NULL; j++) {
121 /* thanks but no thanks, we'll handle SVG extensions... */
122 if (strcmp(extensions[i], "svg") == 0) {
123 continue;
124 }
125 if (strcmp(extensions[i], "svgz") == 0) {
126 continue;
127 }
128 if (strcmp(extensions[i], "svg.gz") == 0) {
129 continue;
130 }
132 gchar *xmlString = g_strdup_printf(
133 "<inkscape-extension>\n"
134 "<name>%s GDK pixbuf Input</name>\n"
135 "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
136 "<input>\n"
137 "<extension>.%s</extension>\n"
138 "<mimetype>%s</mimetype>\n"
139 "<filetypename>%s (*.%s)</filetypename>\n"
140 "<filetypetooltip>%s</filetypetooltip>\n"
141 "</input>\n"
142 "</inkscape-extension>",
143 name,
144 extensions[i],
145 extensions[i],
146 mimetypes[j],
147 name,
148 extensions[i],
149 description
150 );
152 Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
153 g_free(xmlString);
154 }}
156 g_free(name);
157 g_free(description);
158 g_strfreev(mimetypes);
159 g_strfreev(extensions);
160 }
162 g_slist_free(formatlisthead);
163 }
165 } } } /* namespace Inkscape, Extension, Implementation */
167 /*
168 Local Variables:
169 mode:c++
170 c-file-style:"stroustrup"
171 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
172 indent-tabs-mode:nil
173 fill-column:99
174 End:
175 */
176 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :