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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <evince-document.h>
31 #if !(_WIN32_WINNT >= 0x0500)
32 #error "_WIN32_WINNT must be defined >= 0x0500"
37 #define THUMBNAIL_SIZE 128
39 static gint size = THUMBNAIL_SIZE;
40 static const gchar **file_arguments;
42 static const GOptionEntry goption_options[] = {
43 { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
44 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
56 delete_temp_file (GFile *file)
58 ev_tmp_file_unlink (file);
59 g_object_unref (file);
63 evince_thumbnailer_get_document (GFile *file)
65 EvDocument *document = NULL;
67 GFile *tmp_file = NULL;
70 if (!g_file_is_native (file)) {
71 gchar *base_name, *template;
73 base_name = g_file_get_basename (file);
74 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
77 tmp_file = ev_mkstemp_file (template, &error);
80 g_printerr ("Error loading remote document: %s\n", error->message);
86 g_file_copy (file, tmp_file, G_FILE_COPY_OVERWRITE,
87 NULL, NULL, NULL, &error);
89 g_printerr ("Error loading remote document: %s\n", error->message);
91 g_object_unref (tmp_file);
95 uri = g_file_get_uri (tmp_file);
97 uri = g_file_get_uri (file);
100 document = ev_document_factory_get_document (uri, &error);
103 g_object_weak_ref (G_OBJECT (document),
104 (GWeakNotify)delete_temp_file,
107 ev_tmp_file_unlink (tmp_file);
108 g_object_unref (tmp_file);
113 if (error->domain == EV_DOCUMENT_ERROR &&
114 error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
115 /* FIXME: Create a thumb for cryp docs */
116 g_error_free (error);
119 g_printerr ("Error loading document: %s\n", error->message);
120 g_error_free (error);
128 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
131 double width, height;
135 page = ev_document_get_page (document, 0);
137 ev_document_get_page_size (document, 0, &width, &height);
139 rc = ev_render_context_new (page, 0, size / width);
140 pixbuf = ev_document_get_thumbnail (document, rc);
142 g_object_unref (page);
144 if (pixbuf != NULL) {
145 const char *overlaid_icon_name = NULL;
147 if (overlaid_icon_name) {
148 GdkPixbuf *overlaid_pixbuf;
151 gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
152 gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
155 gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", EVINCEDATADIR, overlaid_icon_name);
157 overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
158 g_free (overlaid_icon_path);
159 if (overlaid_pixbuf != NULL) {
160 int delta_height, delta_width;
162 delta_width = gdk_pixbuf_get_width (pixbuf) -
163 gdk_pixbuf_get_width (overlaid_pixbuf);
164 delta_height = gdk_pixbuf_get_height (pixbuf) -
165 gdk_pixbuf_get_height (overlaid_pixbuf);
167 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
168 delta_width, delta_height,
169 gdk_pixbuf_get_width (overlaid_pixbuf),
170 gdk_pixbuf_get_height (overlaid_pixbuf),
171 delta_width, delta_height,
173 GDK_INTERP_NEAREST, 100);
175 g_object_unref (overlaid_pixbuf);
179 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
180 g_object_unref (pixbuf);
184 g_object_unref (pixbuf);
191 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
193 ev_document_doc_mutex_lock ();
194 data->success = evince_thumbnail_pngenc_get (data->document,
197 ev_document_doc_mutex_unlock ();
199 g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
205 print_usage (GOptionContext *context)
209 help = g_option_context_get_help (context, TRUE, NULL);
210 g_print ("%s", help);
215 main (int argc, char *argv[])
217 EvDocument *document;
218 GOptionContext *context;
222 GError *error = NULL;
224 context = g_option_context_new ("- GNOME Document Thumbnailer");
225 g_option_context_add_main_entries (context, goption_options, NULL);
227 if (!g_option_context_parse (context, &argc, &argv, &error)) {
228 g_printerr ("%s\n", error->message);
229 g_error_free (error);
230 print_usage (context);
231 g_option_context_free (context);
236 input = file_arguments ? file_arguments[0] : NULL;
237 output = input ? file_arguments[1] : NULL;
238 if (!input || !output) {
239 print_usage (context);
240 g_option_context_free (context);
245 g_option_context_free (context);
248 g_print ("Size cannot be smaller than 1 pixel\n");
252 input = file_arguments[0];
253 output = file_arguments[1];
257 if (!g_thread_supported ())
258 g_thread_init (NULL);
263 file = g_file_new_for_commandline_arg (input);
264 document = evince_thumbnailer_get_document (file);
265 g_object_unref (file);
272 if (EV_IS_ASYNC_RENDERER (document)) {
273 struct AsyncData data;
275 gtk_init (&argc, &argv);
277 data.document = document;
278 data.output = output;
281 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
286 g_object_unref (document);
289 return data.success ? 0 : -2;
292 if (!evince_thumbnail_pngenc_get (document, output, size)) {
293 g_object_unref (document);
298 g_object_unref (document);