2007-07-18 21:58:35 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Rob Kendrick <rjek@netsurf-browser.org>
|
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/>.
|
2007-07-18 21:58:35 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Content handler for image/svg using librsvg (implementation).
|
|
|
|
*
|
|
|
|
* SVG files are rendered to a NetSurf bitmap by creating a Cairo rendering
|
|
|
|
* surface (content_rsvg_data.cs) over the bitmap's data, creating a Cairo
|
|
|
|
* drawing context using that surface, and then passing that drawing context
|
|
|
|
* to librsvg which then uses Cairo calls to plot the graphic to the bitmap.
|
|
|
|
* We store this in content->bitmap, and then use the usual bitmap plotter
|
|
|
|
* function to render it for redraw requests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2007-07-19 03:57:39 +04:00
|
|
|
#include <assert.h>
|
2011-05-07 00:40:09 +04:00
|
|
|
#include <string.h>
|
2007-07-18 21:58:35 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <librsvg/rsvg.h>
|
2014-11-15 18:53:58 +03:00
|
|
|
#ifndef RSVG_CAIRO_H
|
2007-07-18 21:58:35 +04:00
|
|
|
#include <librsvg/rsvg-cairo.h>
|
2014-11-15 18:53:58 +03:00
|
|
|
#endif
|
2007-07-18 21:58:35 +04:00
|
|
|
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/messages.h"
|
2015-04-17 02:15:07 +03:00
|
|
|
#include "content/content_protected.h"
|
|
|
|
#include "desktop/plotters.h"
|
|
|
|
#include "desktop/gui_internal.h"
|
2007-07-18 21:58:35 +04:00
|
|
|
|
2015-04-17 02:15:07 +03:00
|
|
|
#include "image/bitmap.h"
|
2011-08-28 18:29:15 +04:00
|
|
|
#include "image/rsvg.h"
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
typedef struct rsvg_content {
|
|
|
|
struct content base;
|
|
|
|
|
|
|
|
RsvgHandle *rsvgh; /**< Context handle for RSVG renderer */
|
|
|
|
cairo_surface_t *cs; /**< The surface built inside a nsbitmap */
|
|
|
|
cairo_t *ct; /**< Cairo drawing context */
|
|
|
|
struct bitmap *bitmap; /**< Created NetSurf bitmap */
|
|
|
|
} rsvg_content;
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static nserror rsvg_create_svg_data(rsvg_content *c)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
2011-05-08 23:54:35 +04:00
|
|
|
union content_msg_data msg_data;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
c->rsvgh = NULL;
|
|
|
|
c->cs = NULL;
|
|
|
|
c->ct = NULL;
|
|
|
|
c->bitmap = NULL;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
if ((c->rsvgh = rsvg_handle_new()) == NULL) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("rsvg_handle_new() returned NULL.");
|
2011-05-08 23:54:35 +04:00
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return NSERROR_NOMEM;
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static nserror rsvg_create(const content_handler *handler,
|
2011-05-07 00:40:09 +04:00
|
|
|
lwc_string *imime_type, const http_parameter *params,
|
|
|
|
llcache_handle *llcache, const char *fallback_charset,
|
|
|
|
bool quirks, struct content **c)
|
|
|
|
{
|
|
|
|
rsvg_content *svg;
|
|
|
|
nserror error;
|
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
svg = calloc(1, sizeof(rsvg_content));
|
2011-05-07 00:40:09 +04:00
|
|
|
if (svg == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
error = content__init(&svg->base, handler, imime_type, params,
|
|
|
|
llcache, fallback_charset, quirks);
|
|
|
|
if (error != NSERROR_OK) {
|
2012-10-03 22:24:58 +04:00
|
|
|
free(svg);
|
2011-05-07 00:40:09 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = rsvg_create_svg_data(svg);
|
|
|
|
if (error != NSERROR_OK) {
|
2012-10-03 22:24:58 +04:00
|
|
|
free(svg);
|
2011-05-07 00:40:09 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = (struct content *) svg;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static bool rsvg_process_data(struct content *c, const char *data,
|
2007-07-19 17:47:59 +04:00
|
|
|
unsigned int size)
|
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
rsvg_content *d = (rsvg_content *) c;
|
2007-07-19 17:47:59 +04:00
|
|
|
union content_msg_data msg_data;
|
|
|
|
GError *err = NULL;
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-19 17:47:59 +04:00
|
|
|
if (rsvg_handle_write(d->rsvgh, (const guchar *)data, (gsize)size,
|
|
|
|
&err) == FALSE) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("rsvg_handle_write returned an error: %s", err->message);
|
2007-07-19 17:47:59 +04:00
|
|
|
msg_data.error = err->message;
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
2007-08-08 20:16:03 +04:00
|
|
|
return false;
|
2007-07-19 17:47:59 +04:00
|
|
|
}
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-19 17:47:59 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
/** Convert Cairo's ARGB output to NetSurf's favoured ABGR format. It converts
|
2011-02-02 00:47:05 +03:00
|
|
|
* the data in-place.
|
2007-07-18 21:58:35 +04:00
|
|
|
*
|
2011-02-02 00:47:05 +03:00
|
|
|
* \param pixels Pixel data, in the form of ARGB. This will
|
2007-07-18 21:58:35 +04:00
|
|
|
* be overwritten with new data in the form of ABGR.
|
|
|
|
* \param width Width of the bitmap
|
|
|
|
* \param height Height of the bitmap
|
|
|
|
* \param rowstride Number of bytes to skip after each row (this
|
|
|
|
* implementation requires this to be a multiple of 4.)
|
|
|
|
*/
|
2011-02-02 00:47:05 +03:00
|
|
|
static inline void rsvg_argb_to_abgr(uint8_t *pixels,
|
|
|
|
int width, int height, size_t rowstride)
|
2007-07-18 21:58:35 +04:00
|
|
|
{
|
2011-02-02 00:47:05 +03:00
|
|
|
uint8_t *p = pixels;
|
2007-07-18 21:58:35 +04:00
|
|
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
2011-02-02 00:47:05 +03:00
|
|
|
/* Swap R and B */
|
|
|
|
const uint8_t r = p[x+3];
|
|
|
|
|
|
|
|
p[x+3] = p[x];
|
|
|
|
|
|
|
|
p[x] = r;
|
2007-07-18 21:58:35 +04:00
|
|
|
}
|
2011-02-02 00:47:05 +03:00
|
|
|
|
|
|
|
p += rowstride;
|
2007-07-18 21:58:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static bool rsvg_convert(struct content *c)
|
2007-07-18 21:58:35 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
rsvg_content *d = (rsvg_content *) c;
|
2007-07-18 21:58:35 +04:00
|
|
|
union content_msg_data msg_data;
|
2007-07-19 03:57:39 +04:00
|
|
|
RsvgDimensionData rsvgsize;
|
2007-07-18 21:58:35 +04:00
|
|
|
GError *err = NULL;
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
if (rsvg_handle_close(d->rsvgh, &err) == FALSE) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("rsvg_handle_close returned an error: %s", err->message);
|
2007-07-18 21:58:35 +04:00
|
|
|
msg_data.error = err->message;
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
|
|
|
}
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-19 03:57:39 +04:00
|
|
|
assert(err == NULL);
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-19 03:57:39 +04:00
|
|
|
/* we should now be able to query librsvg for the natural size of the
|
|
|
|
* graphic, so we can create our bitmap.
|
|
|
|
*/
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-19 03:57:39 +04:00
|
|
|
rsvg_handle_get_dimensions(d->rsvgh, &rsvgsize);
|
|
|
|
c->width = rsvgsize.width;
|
|
|
|
c->height = rsvgsize.height;
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2015-04-17 02:15:07 +03:00
|
|
|
if ((d->bitmap = guit->bitmap->create(c->width, c->height,
|
2007-07-18 21:58:35 +04:00
|
|
|
BITMAP_NEW)) == NULL) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("Failed to create bitmap for rsvg render.");
|
2007-07-18 21:58:35 +04:00
|
|
|
msg_data.error = messages_get("NoMemory");
|
2007-08-08 20:16:03 +04:00
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
2007-07-18 21:58:35 +04:00
|
|
|
}
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
if ((d->cs = cairo_image_surface_create_for_data(
|
2015-04-17 02:15:07 +03:00
|
|
|
(unsigned char *)guit->bitmap->get_buffer(d->bitmap),
|
2007-07-18 21:58:35 +04:00
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
c->width, c->height,
|
2015-04-17 02:15:07 +03:00
|
|
|
guit->bitmap->get_rowstride(d->bitmap))) == NULL) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("Failed to create Cairo image surface for rsvg render.");
|
2007-07-18 21:58:35 +04:00
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return false;
|
2007-08-08 20:16:03 +04:00
|
|
|
}
|
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
if ((d->ct = cairo_create(d->cs)) == NULL) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("Failed to create Cairo drawing context for rsvg render.");
|
2007-07-18 21:58:35 +04:00
|
|
|
msg_data.error = messages_get("NoMemory");
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
|
2007-08-08 20:16:03 +04:00
|
|
|
return false;
|
2007-07-18 21:58:35 +04:00
|
|
|
}
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
rsvg_handle_render_cairo(d->rsvgh, d->ct);
|
2015-04-17 02:15:07 +03:00
|
|
|
rsvg_argb_to_abgr(guit->bitmap->get_buffer(d->bitmap),
|
2007-07-19 03:57:39 +04:00
|
|
|
c->width, c->height,
|
2015-04-17 02:15:07 +03:00
|
|
|
guit->bitmap->get_rowstride(d->bitmap));
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2015-04-17 02:15:07 +03:00
|
|
|
guit->bitmap->modified(d->bitmap);
|
2011-02-27 23:11:39 +03:00
|
|
|
content_set_ready(c);
|
|
|
|
content_set_done(c);
|
2008-03-10 15:41:41 +03:00
|
|
|
/* Done: update status bar */
|
|
|
|
content_set_status(c, "");
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-07-18 21:58:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:17:39 +04:00
|
|
|
static bool rsvg_redraw(struct content *c, struct content_redraw_data *data,
|
2011-06-30 19:48:07 +04:00
|
|
|
const struct rect *clip, const struct redraw_context *ctx)
|
2007-07-18 21:58:35 +04:00
|
|
|
{
|
2011-08-31 16:12:41 +04:00
|
|
|
rsvg_content *rsvgcontent = (rsvg_content *) c;
|
2011-05-08 23:54:35 +04:00
|
|
|
bitmap_flags_t flags = BITMAPF_NONE;
|
|
|
|
|
2011-08-31 16:12:41 +04:00
|
|
|
assert(rsvgcontent->bitmap != NULL);
|
2011-05-08 23:54:35 +04:00
|
|
|
|
2011-06-29 00:17:39 +04:00
|
|
|
if (data->repeat_x)
|
2011-05-08 23:54:35 +04:00
|
|
|
flags |= BITMAPF_REPEAT_X;
|
2011-06-29 00:17:39 +04:00
|
|
|
if (data->repeat_y)
|
2011-05-08 23:54:35 +04:00
|
|
|
flags |= BITMAPF_REPEAT_Y;
|
|
|
|
|
2011-06-30 19:48:07 +04:00
|
|
|
return ctx->plot->bitmap(data->x, data->y, data->width, data->height,
|
2011-08-31 16:12:41 +04:00
|
|
|
rsvgcontent->bitmap, data->background_colour, flags);
|
2007-07-18 21:58:35 +04:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static void rsvg_destroy(struct content *c)
|
2007-07-18 21:58:35 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
rsvg_content *d = (rsvg_content *) c;
|
2007-07-18 21:58:35 +04:00
|
|
|
|
2015-04-17 02:15:07 +03:00
|
|
|
if (d->bitmap != NULL) guit->bitmap->destroy(d->bitmap);
|
2012-05-16 23:57:43 +04:00
|
|
|
if (d->rsvgh != NULL) g_object_unref(d->rsvgh);
|
2007-07-19 03:57:39 +04:00
|
|
|
if (d->ct != NULL) cairo_destroy(d->ct);
|
|
|
|
if (d->cs != NULL) cairo_surface_destroy(d->cs);
|
2007-07-18 21:58:35 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static nserror rsvg_clone(const struct content *old, struct content **newc)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
rsvg_content *svg;
|
|
|
|
nserror error;
|
2010-04-04 16:41:19 +04:00
|
|
|
const char *data;
|
|
|
|
unsigned long size;
|
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
svg = calloc(1, sizeof(rsvg_content));
|
2011-05-07 00:40:09 +04:00
|
|
|
if (svg == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
error = content__clone(old, &svg->base);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&svg->base);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2010-04-04 16:41:19 +04:00
|
|
|
/* Simply replay create/process/convert */
|
2011-05-07 00:40:09 +04:00
|
|
|
error = rsvg_create_svg_data(svg);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&svg->base);
|
|
|
|
return error;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
data = content__get_source_data(&svg->base, &size);
|
2010-04-04 16:41:19 +04:00
|
|
|
if (size > 0) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (rsvg_process_data(&svg->base, data, size) == false) {
|
|
|
|
content_destroy(&svg->base);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old->status == CONTENT_STATUS_READY ||
|
|
|
|
old->status == CONTENT_STATUS_DONE) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (rsvg_convert(&svg->base) == false) {
|
|
|
|
content_destroy(&svg->base);
|
|
|
|
return NSERROR_CLONE_FAILED;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
*newc = (struct content *) svg;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-31 16:12:41 +04:00
|
|
|
static void *rsvg_get_internal(const struct content *c, void *context)
|
|
|
|
{
|
|
|
|
rsvg_content *d = (rsvg_content *) c;
|
|
|
|
|
|
|
|
return d->bitmap;
|
|
|
|
}
|
|
|
|
|
2011-09-03 13:27:42 +04:00
|
|
|
static content_type rsvg_content_type(void)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
|
|
|
return CONTENT_IMAGE;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-05-08 23:54:35 +04:00
|
|
|
static const content_handler rsvg_content_handler = {
|
2011-05-09 19:32:34 +04:00
|
|
|
.create = rsvg_create,
|
|
|
|
.process_data = rsvg_process_data,
|
|
|
|
.data_complete = rsvg_convert,
|
|
|
|
.destroy = rsvg_destroy,
|
|
|
|
.redraw = rsvg_redraw,
|
|
|
|
.clone = rsvg_clone,
|
2011-08-31 16:12:41 +04:00
|
|
|
.get_internal = rsvg_get_internal,
|
2011-05-09 19:32:34 +04:00
|
|
|
.type = rsvg_content_type,
|
|
|
|
.no_share = false,
|
2011-05-08 23:54:35 +04:00
|
|
|
};
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static const char *rsvg_types[] = {
|
|
|
|
"image/svg",
|
|
|
|
"image/svg+xml"
|
|
|
|
};
|
2011-05-08 23:54:35 +04:00
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
CONTENT_FACTORY_REGISTER_TYPES(nsrsvg, rsvg_types, rsvg_content_handler);
|
2011-05-08 23:54:35 +04:00
|
|
|
|