2 Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <evince-document.h>
34 #if !(_WIN32_WINNT >= 0x0500)
35 #error "_WIN32_WINNT must be defined >= 0x0500"
40 #define THUMBNAIL_SIZE 128
42 static gint size = THUMBNAIL_SIZE;
43 static const gchar **file_arguments;
45 static const GOptionEntry goption_options[] = {
46 { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
47 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
59 delete_temp_file (GFile *file)
61 ev_tmp_file_unlink (file);
62 g_object_unref (file);
66 evince_thumbnailer_get_document (GFile *file)
68 EvDocument *document = NULL;
70 GFile *tmp_file = NULL;
73 if (!g_file_is_native (file)) {
74 gchar *base_name, *template;
76 base_name = g_file_get_basename (file);
77 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
80 tmp_file = ev_mkstemp_file (template, &error);
83 g_printerr ("Error loading remote document: %s\n", error->message);
89 g_file_copy (file, tmp_file, G_FILE_COPY_OVERWRITE,
90 NULL, NULL, NULL, &error);
92 g_printerr ("Error loading remote document: %s\n", error->message);
94 g_object_unref (tmp_file);
98 uri = g_file_get_uri (tmp_file);
100 uri = g_file_get_uri (file);
103 document = ev_document_factory_get_document (uri, &error);
106 g_object_weak_ref (G_OBJECT (document),
107 (GWeakNotify)delete_temp_file,
110 ev_tmp_file_unlink (tmp_file);
111 g_object_unref (tmp_file);
116 if (error->domain == EV_DOCUMENT_ERROR &&
117 error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
118 /* FIXME: Create a thumb for cryp docs */
119 g_error_free (error);
122 g_printerr ("Error loading document: %s\n", error->message);
123 g_error_free (error);
131 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
134 double width, height;
138 page = ev_document_get_page (document, 0);
140 ev_document_get_page_size (document, 0, &width, &height);
142 rc = ev_render_context_new (page, 0, size / width);
143 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
146 g_object_unref (page);
148 if (pixbuf != NULL) {
149 const char *overlaid_icon_name = NULL;
151 if (overlaid_icon_name) {
152 GdkPixbuf *overlaid_pixbuf;
155 gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
156 gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
159 gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
161 overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
162 g_free (overlaid_icon_path);
163 if (overlaid_pixbuf != NULL) {
164 int delta_height, delta_width;
166 delta_width = gdk_pixbuf_get_width (pixbuf) -
167 gdk_pixbuf_get_width (overlaid_pixbuf);
168 delta_height = gdk_pixbuf_get_height (pixbuf) -
169 gdk_pixbuf_get_height (overlaid_pixbuf);
171 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
172 delta_width, delta_height,
173 gdk_pixbuf_get_width (overlaid_pixbuf),
174 gdk_pixbuf_get_height (overlaid_pixbuf),
175 delta_width, delta_height,
177 GDK_INTERP_NEAREST, 100);
179 g_object_unref (overlaid_pixbuf);
183 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
184 g_object_unref (pixbuf);
188 g_object_unref (pixbuf);
195 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
197 ev_document_doc_mutex_lock ();
198 data->success = evince_thumbnail_pngenc_get (data->document,
201 ev_document_doc_mutex_unlock ();
203 g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
209 print_usage (GOptionContext *context)
213 help = g_option_context_get_help (context, TRUE, NULL);
214 g_print ("%s", help);
219 main (int argc, char *argv[])
221 EvDocument *document;
222 GOptionContext *context;
226 GError *error = NULL;
228 context = g_option_context_new ("- GNOME Document Thumbnailer");
229 g_option_context_add_main_entries (context, goption_options, NULL);
231 if (!g_option_context_parse (context, &argc, &argv, &error)) {
232 g_printerr ("%s\n", error->message);
233 g_error_free (error);
234 print_usage (context);
235 g_option_context_free (context);
240 input = file_arguments ? file_arguments[0] : NULL;
241 output = input ? file_arguments[1] : NULL;
242 if (!input || !output) {
243 print_usage (context);
244 g_option_context_free (context);
249 g_option_context_free (context);
252 g_print ("Size cannot be smaller than 1 pixel\n");
256 input = file_arguments[0];
257 output = file_arguments[1];
261 if (!g_thread_supported ())
262 g_thread_init (NULL);
267 file = g_file_new_for_commandline_arg (input);
268 document = evince_thumbnailer_get_document (file);
269 g_object_unref (file);
276 if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
277 g_object_unref (document);
282 if (EV_IS_ASYNC_RENDERER (document)) {
283 struct AsyncData data;
285 gtk_init (&argc, &argv);
287 data.document = document;
288 data.output = output;
291 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
296 g_object_unref (document);
299 return data.success ? 0 : -2;
302 if (!evince_thumbnail_pngenc_get (document, output, size)) {
303 g_object_unref (document);
308 g_object_unref (document);