2007-07-13 07:53:14 +04:00
|
|
|
/*
|
2008-02-03 22:11:36 +03:00
|
|
|
* Copyright 2007-2008 James Bursa <bursa@users.sourceforge.net>
|
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-13 07:53:14 +04:00
|
|
|
*/
|
|
|
|
|
2017-02-11 16:54:08 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* implementation of content for image/svg using libsvgtiny.
|
2007-07-13 07:53:14 +04:00
|
|
|
*/
|
|
|
|
|
2008-07-27 03:42:24 +04:00
|
|
|
#include <assert.h>
|
2012-11-12 00:02:45 +04:00
|
|
|
#include <limits.h>
|
2009-03-27 05:47:16 +03:00
|
|
|
#include <string.h>
|
2016-05-26 13:18:41 +03:00
|
|
|
#include <stdlib.h>
|
2009-03-27 05:47:16 +03:00
|
|
|
|
|
|
|
#include <svgtiny.h>
|
|
|
|
|
2014-11-02 23:10:32 +03:00
|
|
|
#include "utils/messages.h"
|
|
|
|
#include "utils/utils.h"
|
2016-05-30 23:05:57 +03:00
|
|
|
#include "netsurf/plotters.h"
|
2016-06-06 10:59:23 +03:00
|
|
|
#include "netsurf/content.h"
|
|
|
|
#include "content/content_protected.h"
|
2014-11-02 23:10:32 +03:00
|
|
|
|
2016-06-06 16:47:27 +03:00
|
|
|
#include "image/svg.h"
|
2007-07-13 07:53:14 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
typedef struct svg_content {
|
|
|
|
struct content base;
|
|
|
|
|
|
|
|
struct svgtiny_diagram *diagram;
|
2012-11-12 00:02:45 +04:00
|
|
|
|
|
|
|
int current_width;
|
|
|
|
int current_height;
|
2011-05-07 00:40:09 +04:00
|
|
|
} svg_content;
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static nserror svg_create_svg_data(svg_content *c)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
2011-08-27 12:43:51 +04:00
|
|
|
c->diagram = svgtiny_create();
|
|
|
|
if (c->diagram == NULL)
|
|
|
|
goto no_memory;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
2012-11-12 00:02:45 +04:00
|
|
|
c->current_width = INT_MAX;
|
|
|
|
c->current_height = INT_MAX;
|
2011-05-07 00:40:09 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
no_memory:
|
2017-08-28 12:24:04 +03:00
|
|
|
content_broadcast_errorcode(&c->base, NSERROR_NOMEM);
|
2011-08-27 12:43:51 +04:00
|
|
|
return NSERROR_NOMEM;
|
2011-05-07 00:40:09 +04:00
|
|
|
}
|
|
|
|
|
2007-07-13 07:53:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a CONTENT_SVG.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static nserror svg_create(const content_handler *handler,
|
2016-04-19 01:04:16 +03:00
|
|
|
lwc_string *imime_type, const struct http_parameter *params,
|
2016-06-06 10:59:23 +03:00
|
|
|
struct llcache_handle *llcache, const char *fallback_charset,
|
2011-05-07 00:40:09 +04:00
|
|
|
bool quirks, struct content **c)
|
|
|
|
{
|
|
|
|
svg_content *svg;
|
|
|
|
nserror error;
|
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
svg = calloc(1, sizeof(svg_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 = svg_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-13 07:53:14 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a CONTENT_SVG for display.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static bool svg_convert(struct content *c)
|
2007-07-13 07:53:14 +04:00
|
|
|
{
|
|
|
|
/*c->title = malloc(100);
|
|
|
|
if (c->title)
|
|
|
|
snprintf(c->title, 100, messages_get("svgTitle"),
|
|
|
|
width, height, c->source_size);*/
|
|
|
|
//c->size += ?;
|
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, "");
|
2008-02-03 22:11:36 +03:00
|
|
|
|
2007-07-13 07:53:14 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-30 02:33:21 +04:00
|
|
|
/**
|
|
|
|
* Reformat a CONTENT_SVG.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static void svg_reformat(struct content *c, int width, int height)
|
2010-03-30 02:33:21 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
svg_content *svg = (svg_content *) c;
|
2010-03-30 02:33:21 +04:00
|
|
|
const char *source_data;
|
|
|
|
unsigned long source_size;
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
assert(svg->diagram);
|
2010-03-30 02:33:21 +04:00
|
|
|
|
2012-11-12 00:02:45 +04:00
|
|
|
/* Avoid reformats to same width/height as we already reformatted to */
|
|
|
|
if (width != svg->current_width || height != svg->current_height) {
|
2010-03-30 02:33:21 +04:00
|
|
|
source_data = content__get_source_data(c, &source_size);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
svgtiny_parse(svg->diagram, source_data, source_size,
|
2011-12-04 18:55:23 +04:00
|
|
|
nsurl_access(content_get_url(c)),
|
2011-10-03 19:56:47 +04:00
|
|
|
width, height);
|
2012-11-12 00:14:18 +04:00
|
|
|
|
|
|
|
svg->current_width = width;
|
|
|
|
svg->current_height = height;
|
2010-03-30 02:33:21 +04:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
c->width = svg->diagram->width;
|
|
|
|
c->height = svg->diagram->height;
|
2010-03-30 02:33:21 +04:00
|
|
|
}
|
|
|
|
|
2007-07-13 07:53:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redraw a CONTENT_SVG.
|
|
|
|
*/
|
|
|
|
|
2017-02-11 16:54:08 +03:00
|
|
|
static bool
|
|
|
|
svg_redraw_internal(struct content *c,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
const struct rect *clip,
|
|
|
|
const struct redraw_context *ctx,
|
|
|
|
float scale,
|
|
|
|
colour background_colour)
|
2007-07-13 07:53:14 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
svg_content *svg = (svg_content *) c;
|
2008-02-03 22:11:36 +03:00
|
|
|
float transform[6];
|
2011-05-07 00:40:09 +04:00
|
|
|
struct svgtiny_diagram *diagram = svg->diagram;
|
2008-02-03 22:11:36 +03:00
|
|
|
int px, py;
|
2008-11-30 01:53:58 +03:00
|
|
|
unsigned int i;
|
2009-07-21 14:59:53 +04:00
|
|
|
plot_font_style_t fstyle = *plot_style_font;
|
2017-02-11 16:54:08 +03:00
|
|
|
plot_style_t pstyle;
|
|
|
|
nserror res;
|
2008-02-03 22:11:36 +03:00
|
|
|
|
|
|
|
assert(diagram);
|
|
|
|
|
|
|
|
transform[0] = (float) width / (float) c->width;
|
|
|
|
transform[1] = 0;
|
|
|
|
transform[2] = 0;
|
|
|
|
transform[3] = (float) height / (float) c->height;
|
|
|
|
transform[4] = x;
|
|
|
|
transform[5] = y;
|
|
|
|
|
2011-05-09 16:17:55 +04:00
|
|
|
#define BGR(c) ((c) == svgtiny_TRANSPARENT ? NS_TRANSPARENT : \
|
|
|
|
((svgtiny_RED((c))) | \
|
|
|
|
(svgtiny_GREEN((c)) << 8) | \
|
|
|
|
(svgtiny_BLUE((c)) << 16)))
|
2008-07-29 03:26:54 +04:00
|
|
|
|
2008-11-30 01:53:58 +03:00
|
|
|
for (i = 0; i != diagram->shape_count; i++) {
|
2008-02-03 22:11:36 +03:00
|
|
|
if (diagram->shape[i].path) {
|
2017-02-11 16:54:08 +03:00
|
|
|
pstyle.stroke_colour = BGR(diagram->shape[i].stroke);
|
|
|
|
pstyle.fill_colour = BGR(diagram->shape[i].fill);
|
|
|
|
res = ctx->plot->path(ctx,
|
2018-05-23 15:04:19 +03:00
|
|
|
&pstyle,
|
|
|
|
diagram->shape[i].path,
|
|
|
|
diagram->shape[i].path_length,
|
|
|
|
plot_style_int_to_fixed(
|
|
|
|
diagram->shape[i].stroke_width),
|
|
|
|
transform);
|
2017-02-11 16:54:08 +03:00
|
|
|
if (res != NSERROR_OK) {
|
2008-02-03 22:11:36 +03:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
2008-02-03 22:11:36 +03:00
|
|
|
|
|
|
|
} else if (diagram->shape[i].text) {
|
|
|
|
px = transform[0] * diagram->shape[i].text_x +
|
|
|
|
transform[2] * diagram->shape[i].text_y +
|
|
|
|
transform[4];
|
|
|
|
py = transform[1] * diagram->shape[i].text_x +
|
|
|
|
transform[3] * diagram->shape[i].text_y +
|
|
|
|
transform[5];
|
2009-07-21 14:59:53 +04:00
|
|
|
|
|
|
|
fstyle.background = 0xffffff;
|
|
|
|
fstyle.foreground = 0x000000;
|
2018-05-23 13:48:35 +03:00
|
|
|
fstyle.size = (8 * PLOT_STYLE_SCALE) * scale;
|
2009-07-21 14:59:53 +04:00
|
|
|
|
2017-02-11 16:54:08 +03:00
|
|
|
res = ctx->plot->text(ctx,
|
|
|
|
&fstyle,
|
|
|
|
px, py,
|
|
|
|
diagram->shape[i].text,
|
|
|
|
strlen(diagram->shape[i].text));
|
|
|
|
if (res != NSERROR_OK) {
|
2009-05-28 20:21:32 +04:00
|
|
|
return false;
|
2017-02-11 16:54:08 +03:00
|
|
|
}
|
2007-07-16 03:22:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-29 03:26:54 +04:00
|
|
|
#undef BGR
|
|
|
|
|
2008-02-03 22:11:36 +03:00
|
|
|
return true;
|
2007-11-29 08:56:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-09 17:02:22 +04:00
|
|
|
/**
|
|
|
|
* Redraw a CONTENT_SVG.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static bool svg_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)
|
2011-05-09 17:02:22 +04:00
|
|
|
{
|
2011-06-29 00:17:39 +04:00
|
|
|
int x = data->x;
|
|
|
|
int y = data->y;
|
|
|
|
|
|
|
|
if ((data->width <= 0) && (data->height <= 0)) {
|
2011-05-09 17:02:22 +04:00
|
|
|
/* No point trying to plot SVG if it does not occupy a valid
|
|
|
|
* area */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:17:39 +04:00
|
|
|
if ((data->repeat_x == false) && (data->repeat_y == false)) {
|
2011-05-09 17:02:22 +04:00
|
|
|
/* Simple case: SVG is not tiled */
|
2011-06-30 19:48:07 +04:00
|
|
|
return svg_redraw_internal(c, x, y,
|
|
|
|
data->width, data->height,
|
|
|
|
clip, ctx, data->scale,
|
|
|
|
data->background_colour);
|
2011-05-09 17:02:22 +04:00
|
|
|
} else {
|
|
|
|
/* Tiled redraw required. SVG repeats to extents of clip
|
|
|
|
* rectangle, in x, y or both directions */
|
|
|
|
int x0, y0, x1, y1;
|
|
|
|
|
|
|
|
/* Find the redraw boundaries to loop within */
|
|
|
|
x0 = x;
|
2011-06-29 00:17:39 +04:00
|
|
|
if (data->repeat_x) {
|
|
|
|
for (; x0 > clip->x0; x0 -= data->width);
|
2011-05-09 17:02:22 +04:00
|
|
|
x1 = clip->x1;
|
|
|
|
} else {
|
|
|
|
x1 = x + 1;
|
|
|
|
}
|
|
|
|
y0 = y;
|
2011-06-29 00:17:39 +04:00
|
|
|
if (data->repeat_y) {
|
|
|
|
for (; y0 > clip->y0; y0 -= data->height);
|
2011-05-09 17:02:22 +04:00
|
|
|
y1 = clip->y1;
|
|
|
|
} else {
|
|
|
|
y1 = y + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Repeatedly plot the SVG across the area */
|
2011-06-29 00:17:39 +04:00
|
|
|
for (y = y0; y < y1; y += data->height) {
|
|
|
|
for (x = x0; x < x1; x += data->width) {
|
2011-05-09 17:02:22 +04:00
|
|
|
if (!svg_redraw_internal(c, x, y,
|
2011-06-29 00:17:39 +04:00
|
|
|
data->width, data->height,
|
2011-06-30 19:48:07 +04:00
|
|
|
clip, ctx, data->scale,
|
2011-06-29 00:17:39 +04:00
|
|
|
data->background_colour)) {
|
2011-05-09 17:02:22 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-13 07:53:14 +04:00
|
|
|
/**
|
|
|
|
* Destroy a CONTENT_SVG and free all resources it owns.
|
|
|
|
*/
|
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static void svg_destroy(struct content *c)
|
2007-07-13 07:53:14 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
svg_content *svg = (svg_content *) c;
|
|
|
|
|
|
|
|
if (svg->diagram != NULL)
|
|
|
|
svgtiny_free(svg->diagram);
|
2007-07-13 07:53:14 +04:00
|
|
|
}
|
2007-07-18 18:49:26 +04:00
|
|
|
|
2008-02-03 22:11:36 +03:00
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static nserror svg_clone(const struct content *old, struct content **newc)
|
2010-04-04 16:41:19 +04:00
|
|
|
{
|
2011-05-07 00:40:09 +04:00
|
|
|
svg_content *svg;
|
|
|
|
nserror error;
|
|
|
|
|
2012-10-03 22:24:58 +04:00
|
|
|
svg = calloc(1, sizeof(svg_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/convert */
|
2011-05-07 00:40:09 +04:00
|
|
|
error = svg_create_svg_data(svg);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
content_destroy(&svg->base);
|
|
|
|
return error;
|
|
|
|
}
|
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 (svg_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;
|
2010-04-04 16:41:19 +04:00
|
|
|
}
|
|
|
|
|
2011-09-03 13:27:42 +04:00
|
|
|
static content_type svg_content_type(void)
|
2011-05-07 00:40:09 +04:00
|
|
|
{
|
|
|
|
return CONTENT_IMAGE;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2011-08-27 12:43:51 +04:00
|
|
|
static const content_handler svg_content_handler = {
|
|
|
|
.create = svg_create,
|
|
|
|
.data_complete = svg_convert,
|
|
|
|
.reformat = svg_reformat,
|
|
|
|
.destroy = svg_destroy,
|
|
|
|
.redraw = svg_redraw,
|
|
|
|
.clone = svg_clone,
|
|
|
|
.type = svg_content_type,
|
2012-11-12 00:14:18 +04:00
|
|
|
.no_share = true
|
2011-08-27 12:43:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *svg_types[] = {
|
|
|
|
"image/svg",
|
|
|
|
"image/svg+xml"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CONTENT_FACTORY_REGISTER_TYPES(svg, svg_types, svg_content_handler);
|
|
|
|
|
2011-08-28 18:29:15 +04:00
|
|
|
|