From 9b57f64c55d244d59ac0c00988ee60c514405d6d Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 27 Nov 2022 17:33:45 +0000 Subject: add rsvg image decoder that uses the new API --- content/handlers/image/Makefile | 2 +- content/handlers/image/rsvg246.c | 265 +++++++++++++++++++++++++++++++++++++++ frontends/gtk/Makefile | 2 + 3 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 content/handlers/image/rsvg246.c diff --git a/content/handlers/image/Makefile b/content/handlers/image/Makefile index 1c27f74d7..afc90f407 100644 --- a/content/handlers/image/Makefile +++ b/content/handlers/image/Makefile @@ -10,7 +10,7 @@ S_IMAGE_$(NETSURF_USE_JPEG) += jpeg.c S_IMAGE_$(NETSURF_USE_ROSPRITE) += nssprite.c S_IMAGE_$(NETSURF_USE_PNG) += png.c S_IMAGE_$(NETSURF_USE_NSSVG) += svg.c -S_IMAGE_$(NETSURF_USE_RSVG) += rsvg.c +S_IMAGE_$(NETSURF_USE_RSVG) += rsvg$(RSVG_API).c S_IMAGE_$(NETSURF_USE_VIDEO) += video.c S_IMAGE_$(NETSURF_USE_WEBP) += webp.c diff --git a/content/handlers/image/rsvg246.c b/content/handlers/image/rsvg246.c new file mode 100644 index 000000000..1769c505a --- /dev/null +++ b/content/handlers/image/rsvg246.c @@ -0,0 +1,265 @@ +/* + * Copyright 2022 Vincent Sanders + * + * 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 . + */ + +/** + * \file + * implementation of content handler for image/svg using librsvg 2.46 API. + * + * 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 +#include +#include +#include +#include + +#include + +#include + +#include "utils/log.h" +#include "utils/utils.h" +#include "utils/messages.h" +#include "netsurf/plotters.h" +#include "netsurf/bitmap.h" +#include "netsurf/content.h" +#include "content/llcache.h" +#include "content/content_protected.h" +#include "content/content_factory.h" +#include "desktop/gui_internal.h" +#include "desktop/bitmap.h" + +#include "image/image_cache.h" + +#include "image/rsvg.h" + + +typedef struct rsvg_content { + struct content base; + + RsvgHandle *rsvgh; /**< Context handle for RSVG renderer */ +} rsvg_content; + + +static nserror +rsvg_create(const content_handler *handler, + lwc_string *imime_type, + const struct http_parameter *params, + llcache_handle *llcache, + const char *fallback_charset, + bool quirks, + struct content **c) +{ + rsvg_content *svg; + nserror error; + + svg = calloc(1, sizeof(rsvg_content)); + if (svg == NULL) + return NSERROR_NOMEM; + + error = content__init(&svg->base, handler, imime_type, params, + llcache, fallback_charset, quirks); + if (error != NSERROR_OK) { + free(svg); + return error; + } + + *c = (struct content *)svg; + + return NSERROR_OK; +} + + +/** + * create a bitmap from jpeg content for the image cache. + */ +static struct bitmap * +rsvg_cache_convert(struct content *c) +{ + rsvg_content *svgc = (rsvg_content *)c; + struct bitmap *bitmap; + cairo_surface_t *cs; + cairo_t *cr; + RsvgRectangle viewport; + gboolean renderres; + + if ((bitmap = guit->bitmap->create(c->width, c->height, BITMAP_NONE)) == NULL) { + NSLOG(netsurf, INFO, "Failed to create bitmap for rsvg render."); + return NULL; + } + + if ((cs = cairo_image_surface_create_for_data( + (unsigned char *)guit->bitmap->get_buffer(bitmap), + CAIRO_FORMAT_ARGB32, + c->width, c->height, + guit->bitmap->get_rowstride(bitmap))) == NULL) { + NSLOG(netsurf, INFO, "Failed to create Cairo image surface for rsvg render."); + guit->bitmap->destroy(bitmap); + return NULL; + } + if ((cr = cairo_create(cs)) == NULL) { + NSLOG(netsurf, INFO, + "Failed to create Cairo drawing context for rsvg render."); + cairo_surface_destroy(cs); + guit->bitmap->destroy(bitmap); + return NULL; + } + + viewport.x = 0; + viewport.y = 0; + viewport.width = c->width; + viewport.height = c->height; + renderres = rsvg_handle_render_document(svgc->rsvgh, cr, &viewport, NULL); + NSLOG(netsurf, DEBUG, "rsvg render:%d, width:%d, height %d", renderres, c->width, c->height); + + bitmap_format_to_client(bitmap, &(bitmap_fmt_t) { + .layout = BITMAP_LAYOUT_ARGB8888, + }); + guit->bitmap->modified(bitmap); + + cairo_destroy(cr); + cairo_surface_destroy(cs); + + return bitmap; +} + + +static bool rsvg_convert(struct content *c) +{ + rsvg_content *svgc = (rsvg_content *)c; + const uint8_t *data; /* content data */ + size_t size; /* content data size */ + GInputStream * istream; + GError *gerror = NULL; + gdouble rwidth, rheight; + gboolean gotsize; + /* check image header is valid and get width/height */ + + data = content__get_source_data(c, &size); + + istream = g_memory_input_stream_new_from_data(data, size, NULL); + svgc->rsvgh = rsvg_handle_new_from_stream_sync(istream, + NULL, + RSVG_HANDLE_FLAGS_NONE, + NULL, + &gerror); + g_object_unref(istream); + if (svgc->rsvgh == NULL) { + NSLOG(netsurf, INFO, "Failed to create rsvg handle for content."); + return false; + } + + gotsize = rsvg_handle_get_intrinsic_size_in_pixels(svgc->rsvgh, + &rwidth, + &rheight); + if (gotsize == TRUE) { + c->width = rwidth; + c->height = rheight; + } else { + RsvgRectangle ink_rect; + RsvgRectangle logical_rect; + rsvg_handle_get_geometry_for_element(svgc->rsvgh, + NULL, + &ink_rect, + &logical_rect, + NULL); + c->width = ink_rect.width; + c->height = ink_rect.height; + } + + NSLOG(netsurf, DEBUG, "rsvg width:%d height:%d.",c->width, c->height); + + c->size = c->width * c->height * 4; + + image_cache_add(c, NULL, rsvg_cache_convert); + + content_set_ready(c); + content_set_done(c); + content_set_status(c, ""); /* Done: update status bar */ + + return true; +} + + +static nserror rsvg_clone(const struct content *old, struct content **newc) +{ + rsvg_content *svg; + nserror error; + + svg = calloc(1, sizeof(rsvg_content)); + if (svg == NULL) + return NSERROR_NOMEM; + + error = content__clone(old, &svg->base); + if (error != NSERROR_OK) { + content_destroy(&svg->base); + return error; + } + + /* re-convert if the content is ready */ + if ((old->status == CONTENT_STATUS_READY) || + (old->status == CONTENT_STATUS_DONE)) { + if (rsvg_convert(&svg->base) == false) { + content_destroy(&svg->base); + return NSERROR_CLONE_FAILED; + } + } + + *newc = (struct content *)svg; + + return NSERROR_OK; +} + + +static void rsvg_destroy(struct content *c) +{ + rsvg_content *d = (rsvg_content *) c; + + if (d->rsvgh != NULL) { + g_object_unref(d->rsvgh); + d->rsvgh = NULL; + } + + return image_cache_destroy(c); +} + +static const content_handler rsvg_content_handler = { + .create = rsvg_create, + .data_complete = rsvg_convert, + .destroy = rsvg_destroy, + .redraw = image_cache_redraw, + .clone = rsvg_clone, + .get_internal = image_cache_get_internal, + .type = image_cache_content_type, + .is_opaque = image_cache_is_opaque, + .no_share = false, +}; + +static const char *rsvg_types[] = { + "image/svg", + "image/svg+xml" +}; + +CONTENT_FACTORY_REGISTER_TYPES(nsrsvg, rsvg_types, rsvg_content_handler); + diff --git a/frontends/gtk/Makefile b/frontends/gtk/Makefile index 4f3fd5f52..79f03b370 100644 --- a/frontends/gtk/Makefile +++ b/frontends/gtk/Makefile @@ -11,6 +11,8 @@ NETSURF_FEATURE_RSVG_CFLAGS := -DWITH_RSVG NETSURF_FEATURE_VIDEO_CFLAGS := -DWITH_VIDEO +# determine if the rsvg library API version +RSVG_API := $(shell $(PKG_CONFIG) --atleast-version=2.46 librsvg-2.0 && echo 246) $(eval $(call pkg_config_find_and_add_enabled,RSVG,librsvg-2.0,SVG)) $(eval $(call pkg_config_find_and_add_enabled,VIDEO,gstreamer-0.10,Video)) -- cgit v1.2.3