Code

SPDocument->Document
[inkscape.git] / src / extension / internal / gdkpixbuf-input.cpp
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 Document *
20 GdkpixbufInput::open(Inkscape::Extension::Input */*mod*/, char const *uri)
21 {
22     PDocument *doc = NULL;
23     GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
25     if (pb) {         /* We are readable */
26         doc = sp_document_new(NULL, TRUE, TRUE);
27         bool saved = sp_document_get_undo_sensitive(doc);
28         sp_document_set_undo_sensitive(doc, false); // no need to undo in this temporary document
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         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
56         // import as <image>
57         repr = xml_doc->createElement("svg:image");
59         // convert filename to uri
60         gchar* _uri = g_filename_to_uri(uri, NULL, NULL);
61         if(_uri) {
62             repr->setAttribute("xlink:href", _uri);
63             g_free(_uri);
64         } else {
65             repr->setAttribute("xlink:href", uri);
66         }
67         /* impl: doc->base is currently NULL, so we can use uri for href whether it's absolute
68          * or relative.  The href will get rewritten by rebase_hrefs if by chance uri is relative
69          * and doc gets saved to a different directory.
70          *
71          * We don't bother setting sodipodi:absref, as we assume it's never useful to have
72          * sodipodi:absref with the same value as xlink:href, and rebase_hrefs will provide
73          * sodipodi:absref values where necessary. */
75         sp_repr_set_svg_double(repr, "width", width);
76         sp_repr_set_svg_double(repr, "height", height);
78         SP_DOCUMENT_ROOT(doc)->appendChildRepr(repr);
79         Inkscape::GC::release(repr);
80         gdk_pixbuf_unref(pb);
81         //alter the canvas size to fit the image size
82         fit_canvas_to_drawing(doc);
83         // restore undo, as now this document may be shown to the user if a bitmap was opened
84         sp_document_set_undo_sensitive(doc, saved);
85     } else {
86         printf("GdkPixbuf loader failed\n");
87     }
89     return doc;
90 }
92 #include "clear-n_.h"
94 void
95 GdkpixbufInput::init(void)
96 {
97     GSList * formatlist, * formatlisthead;
99     /* \todo I'm not sure if I need to free this list */
100     for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
101          formatlist != NULL;
102          formatlist = g_slist_next(formatlist)) {
104         GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
106         gchar *name =        gdk_pixbuf_format_get_name(pixformat);
107         gchar *description = gdk_pixbuf_format_get_description(pixformat);
108         gchar **extensions =  gdk_pixbuf_format_get_extensions(pixformat);
109         gchar **mimetypes =   gdk_pixbuf_format_get_mime_types(pixformat);
111         for (int i = 0; extensions[i] != NULL; i++) {
112         for (int j = 0; mimetypes[j] != NULL; j++) {
114             /* thanks but no thanks, we'll handle SVG extensions... */
115             if (strcmp(extensions[i], "svg") == 0) {
116                 continue;
117             }
118             if (strcmp(extensions[i], "svgz") == 0) {
119                 continue;
120             }
121             if (strcmp(extensions[i], "svg.gz") == 0) {
122                 continue;
123             }
125             gchar *xmlString = g_strdup_printf(
126                 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
127                     "<name>" N_("%s GDK pixbuf Input") "</name>\n"
128                     "<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
129                     "<input>\n"
130                         "<extension>.%s</extension>\n"
131                         "<mimetype>%s</mimetype>\n"
132                         "<filetypename>%s (*.%s)</filetypename>\n"
133                         "<filetypetooltip>%s</filetypetooltip>\n"
134                     "</input>\n"
135                 "</inkscape-extension>",
136                 name,
137                 extensions[i],
138                 extensions[i],
139                 mimetypes[j],
140                 name,
141                 extensions[i],
142                 description
143                 );
145             Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
146             g_free(xmlString);
147         }}
149         g_free(name);
150         g_free(description);
151         g_strfreev(mimetypes);
152         g_strfreev(extensions);
153     }
155     g_slist_free(formatlisthead);
158 } } }  /* namespace Inkscape, Extension, Implementation */
160 /*
161   Local Variables:
162   mode:c++
163   c-file-style:"stroustrup"
164   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165   indent-tabs-mode:nil
166   fill-column:99
167   End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :