2006-03-26 03:31:42 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Rob Kendrick <rjek@rjek.com>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-03-26 03:31:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Page thumbnail creation (implementation).
|
|
|
|
*
|
|
|
|
* Thumbnails are created by setting the current drawing contexts to the
|
|
|
|
* bitmap (a gdk pixbuf) we are passed, and plotting the page at a small
|
|
|
|
* scale.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <gtk/gtk.h>
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/content.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/hlcache.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "content/urldb.h"
|
|
|
|
#include "desktop/plotters.h"
|
|
|
|
#include "desktop/browser.h"
|
2011-02-20 01:17:54 +03:00
|
|
|
#include "desktop/thumbnail.h"
|
2011-01-30 02:40:22 +03:00
|
|
|
#include "gtk/scaffolding.h"
|
|
|
|
#include "gtk/plotters.h"
|
|
|
|
#include "gtk/bitmap.h"
|
2008-08-13 21:16:39 +04:00
|
|
|
#include "image/bitmap.h"
|
|
|
|
#include "render/font.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
2006-03-26 03:31:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a thumbnail of a page.
|
|
|
|
*
|
|
|
|
* \param content content structure to thumbnail
|
|
|
|
* \param bitmap the bitmap to draw to
|
|
|
|
* \param url the URL the thumnail belongs to, or NULL
|
|
|
|
*/
|
2010-03-28 16:56:39 +04:00
|
|
|
bool thumbnail_create(hlcache_handle *content, struct bitmap *bitmap,
|
2006-03-26 03:31:42 +04:00
|
|
|
const char *url)
|
|
|
|
{
|
2008-03-25 14:42:59 +03:00
|
|
|
GdkPixbuf *pixbuf;
|
2008-08-13 21:16:39 +04:00
|
|
|
int cwidth, cheight;
|
2008-03-25 14:42:59 +03:00
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
gint depth;
|
|
|
|
GdkPixmap *pixmap;
|
2006-03-26 05:14:01 +04:00
|
|
|
GdkPixbuf *big;
|
2011-02-16 16:52:29 +03:00
|
|
|
float plot_scale;
|
|
|
|
double scale;
|
2006-03-26 03:31:42 +04:00
|
|
|
|
2008-03-25 14:42:59 +03:00
|
|
|
assert(content);
|
|
|
|
assert(bitmap);
|
|
|
|
|
2011-02-16 17:35:48 +03:00
|
|
|
/* Get details of required final thumbnail image */
|
2008-03-25 14:42:59 +03:00
|
|
|
pixbuf = gtk_bitmap_get_primary(bitmap);
|
|
|
|
width = gdk_pixbuf_get_width(pixbuf);
|
|
|
|
height = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
depth = (gdk_screen_get_system_visual(gdk_screen_get_default()))->depth;
|
|
|
|
|
2011-02-16 16:52:29 +03:00
|
|
|
/* Calculate size of buffer to render the content into */
|
2011-02-16 17:35:48 +03:00
|
|
|
/* We get the width from the content width, unless it exceeds 1024,
|
|
|
|
* in which case we use 1024. This means we never create excessively
|
|
|
|
* large render buffers for huge contents, which would eat memory and
|
|
|
|
* cripple performance. */
|
2011-02-16 16:52:29 +03:00
|
|
|
cwidth = min(content_get_width(content), 1024);
|
2011-02-16 17:35:48 +03:00
|
|
|
/* The height is set in proportion with the width, according to the
|
|
|
|
* aspect ratio of the required thumbnail. */
|
2011-02-16 16:52:29 +03:00
|
|
|
cheight = ((cwidth * height) + (width / 2)) / width;
|
|
|
|
|
|
|
|
/* Create buffer to render into */
|
|
|
|
pixmap = gdk_pixmap_new(NULL, cwidth, cheight, depth);
|
2008-03-25 14:42:59 +03:00
|
|
|
|
|
|
|
if (pixmap == NULL) {
|
|
|
|
/* the creation failed for some reason: most likely because
|
|
|
|
* we've been asked to create with with at least one dimention
|
|
|
|
* as zero. The RISC OS thumbnail generator returns false
|
|
|
|
* from here when it can't create a bitmap, so we assume it's
|
|
|
|
* safe to do so here too.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-26 03:31:42 +04:00
|
|
|
|
|
|
|
gdk_drawable_set_colormap(pixmap, gdk_colormap_get_system());
|
|
|
|
|
|
|
|
/* set the plotting functions up */
|
|
|
|
plot = nsgtk_plotters;
|
|
|
|
|
2011-02-20 01:24:36 +03:00
|
|
|
/* Plotters need to know thumbnail scale */
|
|
|
|
plot_scale = thumbnail_get_redraw_scale(content, cwidth);
|
2011-02-16 16:52:29 +03:00
|
|
|
nsgtk_plot_set_scale(plot_scale);
|
2006-03-26 03:31:42 +04:00
|
|
|
|
|
|
|
/* set to plot to pixmap */
|
|
|
|
current_drawable = pixmap;
|
|
|
|
current_gc = gdk_gc_new(current_drawable);
|
|
|
|
#ifdef CAIRO_VERSION
|
|
|
|
current_cr = gdk_cairo_create(current_drawable);
|
|
|
|
#endif
|
2011-02-16 16:52:29 +03:00
|
|
|
|
2006-03-26 03:31:42 +04:00
|
|
|
/* render the content */
|
2011-02-20 01:24:36 +03:00
|
|
|
thumbnail_redraw(content, cwidth, cheight);
|
2006-03-26 03:31:42 +04:00
|
|
|
|
2011-02-16 16:52:29 +03:00
|
|
|
/* get the pixbuf we rendered the content into */
|
2006-03-26 05:14:01 +04:00
|
|
|
big = gdk_pixbuf_get_from_drawable(NULL, pixmap, NULL, 0, 0, 0, 0,
|
2011-02-16 16:52:29 +03:00
|
|
|
cwidth, cheight);
|
2006-03-26 03:31:42 +04:00
|
|
|
|
2011-02-16 16:52:29 +03:00
|
|
|
/* resample the large plot down to the size of our thumbnail */
|
|
|
|
scale = (double)width / (double)cwidth;
|
2006-03-26 05:14:01 +04:00
|
|
|
gdk_pixbuf_scale(big, pixbuf, 0, 0, width, height, 0, 0,
|
2011-02-16 16:52:29 +03:00
|
|
|
scale, scale, GDK_INTERP_TILES);
|
2006-03-26 05:14:01 +04:00
|
|
|
|
|
|
|
/* As a debugging aid, try this to dump out a copy of the thumbnail as
|
|
|
|
* a PNG: gdk_pixbuf_save(pixbuf, "thumbnail.png", "png", NULL, NULL);
|
2006-03-26 03:31:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* register the thumbnail with the URL */
|
|
|
|
if (url)
|
2006-04-10 03:21:13 +04:00
|
|
|
urldb_set_thumbnail(url, bitmap);
|
2006-03-26 03:31:42 +04:00
|
|
|
|
|
|
|
bitmap_modified(bitmap);
|
|
|
|
|
|
|
|
g_object_unref(current_gc);
|
|
|
|
#ifdef CAIRO_VERSION
|
|
|
|
cairo_destroy(current_cr);
|
|
|
|
#endif
|
|
|
|
g_object_unref(pixmap);
|
2006-03-26 05:18:00 +04:00
|
|
|
g_object_unref(big);
|
2006-03-26 03:31:42 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-16 17:35:48 +03:00
|
|
|
|