]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
Add support for ps, eps and compressed documents thumbnails. Do not ignore
[evince.git] / thumbnailer / evince-thumbnailer.c
1 /*
2    Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
3
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.
8
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.
13
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.
17 */
18
19 #include <libgnomevfs/gnome-vfs-mime-utils.h>
20 #include <libgnomevfs/gnome-vfs-uri.h>
21 #include <libgnomevfs/gnome-vfs-utils.h>
22 #include <libgnomevfs/gnome-vfs-init.h>
23 #include <libgnomevfs/gnome-vfs-ops.h>
24
25 #include <ev-document.h>
26 #include <ev-document-thumbnails.h>
27 #include <ev-async-renderer.h>
28 #include <ev-document-factory.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define THUMBNAIL_SIZE 128
34
35 struct AsyncData {
36         EvDocument  *document;
37         const gchar *output;
38         gint         size;
39         gboolean     success;
40 };
41
42 static EvDocument *
43 evince_thumbnailer_get_document (const gchar *uri)
44 {
45         EvDocument *document = NULL;
46         GError     *error = NULL;
47
48         document = ev_document_factory_get_document  (uri, &error);
49         if (error) {
50                 if (error->domain == EV_DOCUMENT_ERROR &&
51                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
52                         /* FIXME: Create a thumb for cryp docs */
53                         g_error_free (error);
54                         return NULL;
55                 }
56                 g_error_free (error);
57                 return NULL;
58         }
59         
60         return document;
61 }
62
63 static gboolean
64 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
65 {
66         EvRenderContext *rc;
67         double width, height;
68         GdkPixbuf *pixbuf;
69
70         ev_document_get_page_size (document, 0, &width, &height);
71
72         rc = ev_render_context_new (0, 0, size / width);
73         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
74                                                        rc, FALSE);
75         g_object_unref (rc);
76         
77         if (pixbuf != NULL) {
78                 const char *overlaid_icon_name = NULL;
79
80                 if (overlaid_icon_name) {
81                         GdkPixbuf *overlaid_pixbuf;
82
83                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
84                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
85                         g_free (overlaid_icon_path);
86                         if (overlaid_pixbuf != NULL) {
87                                 int delta_height, delta_width;
88                                 
89                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
90                                         gdk_pixbuf_get_width (overlaid_pixbuf);
91                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
92                                         gdk_pixbuf_get_height (overlaid_pixbuf);
93                                 
94                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
95                                                       delta_width, delta_height,
96                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
97                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
98                                                       delta_width, delta_height,
99                                                       1, 1,
100                                                       GDK_INTERP_NEAREST, 100);
101                                 
102                                 g_object_unref  (overlaid_pixbuf);
103                         }
104                 }
105                 
106                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
107                         g_object_unref  (pixbuf);
108                         return TRUE;
109                 }
110
111                 g_object_unref  (pixbuf);
112         }
113         
114         return FALSE;
115 }
116
117 static gpointer
118 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
119 {
120         ev_document_doc_mutex_lock ();
121         data->success = evince_thumbnail_pngenc_get (data->document,
122                                                      data->output,
123                                                      data->size);
124         ev_document_doc_mutex_unlock ();
125         
126         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
127         
128         return NULL;
129 }
130
131 int
132 main (int argc, char *argv[])
133 {
134         EvDocument *document;
135         const char *input;
136         const char *output;
137         int         size;
138         char       *uri;
139
140         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
141             strcmp (argv[1], "--help") == 0) {
142                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
143                 return -1;
144         }
145
146         if (!strcmp (argv[1], "-s")) {
147                 input = argv[3];
148                 output = argv[4];
149                 size = atoi (argv[2]);
150         } else {
151                 input = argv[1];
152                 output = argv[2];
153                 size = THUMBNAIL_SIZE;
154         }
155
156         if (size < 40) {
157                 g_print ("Size cannot be smaller than 40 pixels\n");
158                 return -1;
159         }
160
161         if (!g_thread_supported ())
162                 g_thread_init (NULL);
163         
164         gnome_vfs_init ();
165
166         uri = gnome_vfs_make_uri_from_shell_arg (input);
167         document = evince_thumbnailer_get_document (uri);
168         g_free (uri);
169
170         if (!document)
171                 return -2;
172
173         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
174                 g_object_unref (document);
175                 return FALSE;
176         }
177
178         if (EV_IS_ASYNC_RENDERER (document)) {
179                 struct AsyncData data;
180
181                 gtk_init (&argc, &argv);
182                 
183                 data.document = document;
184                 data.output = output;
185                 data.size = size;
186
187                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
188                                  &data, FALSE, NULL);
189                 
190                 gtk_main ();
191
192                 g_object_unref (document);
193
194                 return data.success ? 0 : -2;
195         }
196
197         if (!evince_thumbnail_pngenc_get (document, output, size)) {
198                 g_object_unref (document);
199                 return -2;
200         }
201
202         g_object_unref (document);
203
204         return 0;
205 }