1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib/gprintf.h>
#include <glibmm/i18n.h>
#include "document-private.h"
#include <dir-util.h>
#include "extension/input.h"
#include "extension/system.h"
#include "gdkpixbuf-input.h"
#include "preferences.h"
#include "selection-chemistry.h"
#include "sp-image.h"
#include "document-undo.h"
#include <set>
namespace Inkscape {
namespace IO {
GdkPixbuf* pixbuf_new_from_file( char const *utf8name, GError **error );
}
namespace Extension {
namespace Internal {
static std::set<Glib::ustring> create_lossy_set()
{
std::set<Glib::ustring> lossy;
lossy.insert(".jpg");
lossy.insert(".jpeg");
return lossy;
}
SPDocument *
GdkpixbufInput::open(Inkscape::Extension::Input *mod, char const *uri)
{
bool embed = false;
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
Glib::ustring attr = prefs->getString("/dialogs/import/link");
if (strcmp(attr.c_str(), "embed") == 0) {
embed = true;
} else if (strcmp(attr.c_str(), "link") == 0) {
embed = false;
} else {
embed = (strcmp(mod->get_param_optiongroup("link"), "embed") == 0);
if (mod->get_param_bool("ask")) {
prefs->setString("/dialogs/import/link", mod->get_param_optiongroup("link"));
mod->set_param_bool("ask", false);
}
}
SPDocument *doc = NULL;
GdkPixbuf *pb = Inkscape::IO::pixbuf_new_from_file( uri, NULL );
static std::set<Glib::ustring> lossy = create_lossy_set();
if (pb) { /* We are readable */
// TODO revisit: bool is_lossy;
Glib::ustring mime_type, ext;
Glib::ustring u = uri;
std::size_t dotpos = u.rfind('.');
if (dotpos != Glib::ustring::npos) {
ext = u.substr(dotpos, Glib::ustring::npos);
}
// HACK: replace with something better based on GIO
if (!ext.empty() && lossy.find(ext) != lossy.end()) {
// TODO revisit: is_lossy = true;
mime_type = "image/jpeg";
} else {
// TODO revisit: is_lossy = false;
mime_type = "image/png";
}
doc = SPDocument::createNewDoc(NULL, TRUE, TRUE);
bool saved = DocumentUndo::getUndoSensitive(doc);
DocumentUndo::setUndoSensitive(doc, false); // no need to undo in this temporary document
double width = gdk_pixbuf_get_width(pb);
double height = gdk_pixbuf_get_height(pb);
gchar const *str = gdk_pixbuf_get_option( pb, "Inkscape::DpiX" );
if ( str ) {
gint dpi = atoi(str);
if ( dpi > 0 && dpi != 72 ) {
double scale = 72.0 / (double)dpi;
width *= scale;
}
}
str = gdk_pixbuf_get_option( pb, "Inkscape::DpiY" );
if ( str ) {
gint dpi = atoi(str);
if ( dpi > 0 && dpi != 72 ) {
double scale = 72.0 / (double)dpi;
height *= scale;
}
}
// Create image node
Inkscape::XML::Document *xml_doc = doc->getReprDoc();
Inkscape::XML::Node *image_node = xml_doc->createElement("svg:image");
sp_repr_set_svg_double(image_node, "width", width);
sp_repr_set_svg_double(image_node, "height", height);
if (embed) {
sp_embed_image(image_node, pb, mime_type);
} else {
// convert filename to uri
gchar* _uri = g_filename_to_uri(uri, NULL, NULL);
if(_uri) {
image_node->setAttribute("xlink:href", _uri);
g_free(_uri);
} else {
image_node->setAttribute("xlink:href", uri);
}
}
g_object_unref(pb);
// Add it to the current layer
doc->getRoot()->appendChildRepr(image_node);
Inkscape::GC::release(image_node);
fit_canvas_to_drawing(doc);
// restore undo, as now this document may be shown to the user if a bitmap was opened
DocumentUndo::setUndoSensitive(doc, saved);
} else {
printf("GdkPixbuf loader failed\n");
}
return doc;
}
#include "clear-n_.h"
void
GdkpixbufInput::init(void)
{
GSList * formatlist, * formatlisthead;
/* \todo I'm not sure if I need to free this list */
for (formatlist = formatlisthead = gdk_pixbuf_get_formats();
formatlist != NULL;
formatlist = g_slist_next(formatlist)) {
GdkPixbufFormat *pixformat = (GdkPixbufFormat *)formatlist->data;
gchar *name = gdk_pixbuf_format_get_name(pixformat);
gchar *description = gdk_pixbuf_format_get_description(pixformat);
gchar **extensions = gdk_pixbuf_format_get_extensions(pixformat);
gchar **mimetypes = gdk_pixbuf_format_get_mime_types(pixformat);
for (int i = 0; extensions[i] != NULL; i++) {
for (int j = 0; mimetypes[j] != NULL; j++) {
/* thanks but no thanks, we'll handle SVG extensions... */
if (strcmp(extensions[i], "svg") == 0) {
continue;
}
if (strcmp(extensions[i], "svgz") == 0) {
continue;
}
if (strcmp(extensions[i], "svg.gz") == 0) {
continue;
}
gchar *caption = g_strdup_printf(_("%s bitmap image import"), name);
gchar *xmlString = g_strdup_printf(
"<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
"<name>%s</name>\n"
"<id>org.inkscape.input.gdkpixbuf.%s</id>\n"
"<param name='link' type='optiongroup' appearance='full' _gui-text='" N_("Link or embed image:") "' >\n"
"<_option value='embed' >" N_("Embed") "</_option>\n"
"<_option value='link' >" N_("Link") "</_option>\n"
"</param>\n"
"<_param name='help' type='description'>" N_("Embed results in stand-alone, larger SVG files. Link references a file outside this SVG document and all files must be moved together.") "</_param>\n"
"<param name=\"ask\" _gui-description='" N_("Hide the dialog next time and always apply the same action.") "' gui-text=\"" N_("Don't ask again") "\" type=\"boolean\" >false</param>\n"
"<input>\n"
"<extension>.%s</extension>\n"
"<mimetype>%s</mimetype>\n"
"<filetypename>%s (*.%s)</filetypename>\n"
"<filetypetooltip>%s</filetypetooltip>\n"
"</input>\n"
"</inkscape-extension>",
caption,
extensions[i],
extensions[i],
mimetypes[j],
name,
extensions[i],
description
);
Inkscape::Extension::build_from_mem(xmlString, new GdkpixbufInput());
g_free(xmlString);
g_free(caption);
}}
g_free(name);
g_free(description);
g_strfreev(mimetypes);
g_strfreev(extensions);
}
g_slist_free(formatlisthead);
}
} } } /* namespace Inkscape, Extension, Implementation */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|