Code

completet some more metainfo (title, creator, producer, date) for pdf
[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 "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     sp_document_set_undo_sensitive(doc, FALSE); // no need to undo in this temporary document
25     GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
26     Inkscape::XML::Node *rdoc = sp_document_repr_root(doc);
27     gchar const *docbase = rdoc->attribute("sodipodi:docbase");
28     gchar const *relname = sp_relative_path_from_path(uri, docbase);
30     if (pb) {         /* We are readable */
31         Inkscape::XML::Node *repr = NULL;
33         double width = gdk_pixbuf_get_width(pb);
34         double height = gdk_pixbuf_get_height(pb);
35         gchar const *str = gdk_pixbuf_get_option( pb, "Inkscape::DpiX" );
36         if ( str )
37         {
38             gint dpi = atoi(str);
39             if ( dpi > 0 && dpi != 72 )
40             {
41                 double scale = 72.0 / (double)dpi;
42                 width *= scale;
43             }
44         }
45         str = gdk_pixbuf_get_option( pb, "Inkscape::DpiY" );
46         if ( str )
47         {
48             gint dpi = atoi(str);
49             if ( dpi > 0 && dpi != 72 )
50             {
51                 double scale = 72.0 / (double)dpi;
52                 height *= scale;
53             }
54         }
56         if (prefs_get_int_attribute("options.importbitmapsasimages", "value", 1) == 1) {
57             // import as <image>
58             repr = sp_repr_new("svg:image");
59             repr->setAttribute("xlink:href", relname);
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 = sp_repr_new("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 = sp_repr_new("svg:image");
77             im->setAttribute("xlink:href", relname);
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 = sp_repr_new("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, TRUE);
96     } else {
97         printf("GdkPixbuf loader failed\n");
98     }
100     return doc;
103 #include "clear-n_.h"
105 void
106 GdkpixbufInput::init(void)
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);
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 :