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
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Content for image/svg (implementation).
|
|
|
|
*/
|
|
|
|
|
2007-07-18 18:49:26 +04:00
|
|
|
#include "utils/config.h"
|
|
|
|
#ifdef WITH_NS_SVG
|
2008-07-27 03:42:24 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
2009-03-27 05:47:16 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <svgtiny.h>
|
|
|
|
|
2010-03-29 02:11:30 +04:00
|
|
|
#include "content/content_protected.h"
|
2007-07-16 03:22:54 +04:00
|
|
|
#include "css/css.h"
|
2007-07-13 07:53:14 +04:00
|
|
|
#include "desktop/plotters.h"
|
|
|
|
#include "image/svg.h"
|
|
|
|
#include "utils/messages.h"
|
2011-05-07 00:40:09 +04:00
|
|
|
#include "utils/talloc.h"
|
2007-07-13 07:53:14 +04:00
|
|
|
#include "utils/utils.h"
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
typedef struct svg_content {
|
|
|
|
struct content base;
|
|
|
|
|
|
|
|
struct svgtiny_diagram *diagram;
|
|
|
|
bool done_parse;
|
|
|
|
} svg_content;
|
|
|
|
|
|
|
|
static nserror svg_create(const content_handler *handler,
|
|
|
|
lwc_string *imime_type, const http_parameter *params,
|
|
|
|
llcache_handle *llcache, const char *fallback_charset,
|
|
|
|
bool quirks, struct content **c);
|
|
|
|
static nserror svg_create_svg_data(svg_content *c);
|
|
|
|
static bool svg_convert(struct content *c);
|
|
|
|
static void svg_destroy(struct content *c);
|
|
|
|
static void svg_reformat(struct content *c, int width, int height);
|
|
|
|
static bool svg_redraw(struct content *c, int x, int y,
|
|
|
|
int width, int height, const struct rect *clip,
|
2011-05-09 12:10:27 +04:00
|
|
|
float scale, colour background_colour,
|
|
|
|
bool repeat_x, bool repeat_y);
|
2011-05-07 00:40:09 +04:00
|
|
|
static nserror svg_clone(const struct content *old, struct content **newc);
|
|
|
|
static content_type svg_content_type(lwc_string *mime_type);
|
|
|
|
|
|
|
|
static const content_handler svg_content_handler = {
|
2011-05-09 19:32:34 +04:00
|
|
|
.create = svg_create,
|
|
|
|
.data_complete = svg_convert,
|
|
|
|
.reformat = svg_reformat,
|
|
|
|
.destroy = svg_destroy,
|
|
|
|
.redraw = svg_redraw,
|
|
|
|
.clone = svg_clone,
|
|
|
|
.type = svg_content_type,
|
|
|
|
.no_share = false,
|
2011-05-07 00:40:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *svg_types[] = {
|
|
|
|
"image/svg",
|
|
|
|
"image/svg+xml"
|
|
|
|
};
|
|
|
|
|
|
|
|
static lwc_string *svg_mime_types[NOF_ELEMENTS(svg_types)];
|
|
|
|
|
|
|
|
nserror svg_init(void)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
lwc_error lerror;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
for (i = 0; i < NOF_ELEMENTS(svg_mime_types); i++) {
|
|
|
|
lerror = lwc_intern_string(svg_types[i],
|
|
|
|
strlen(svg_types[i]),
|
|
|
|
&svg_mime_types[i]);
|
|
|
|
if (lerror != lwc_error_ok) {
|
|
|
|
error = NSERROR_NOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = content_factory_register_handler(svg_mime_types[i],
|
|
|
|
&svg_content_handler);
|
|
|
|
if (error != NSERROR_OK)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
svg_fini();
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void svg_fini(void)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < NOF_ELEMENTS(svg_mime_types); i++) {
|
|
|
|
if (svg_mime_types[i] != NULL)
|
|
|
|
lwc_string_unref(svg_mime_types[i]);
|
|
|
|
}
|
|
|
|
}
|
2007-07-13 07:53:14 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a CONTENT_SVG.
|
|
|
|
*/
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
nserror svg_create(const content_handler *handler,
|
|
|
|
lwc_string *imime_type, const http_parameter *params,
|
|
|
|
llcache_handle *llcache, const char *fallback_charset,
|
|
|
|
bool quirks, struct content **c)
|
|
|
|
{
|
|
|
|
svg_content *svg;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
svg = talloc_zero(0, svg_content);
|
|
|
|
if (svg == NULL)
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
|
|
|
|
error = content__init(&svg->base, handler, imime_type, params,
|
|
|
|
llcache, fallback_charset, quirks);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
talloc_free(svg);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = svg_create_svg_data(svg);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
talloc_free(svg);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = (struct content *) svg;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nserror svg_create_svg_data(svg_content *c)
|
2007-07-13 07:53:14 +04:00
|
|
|
{
|
2008-02-03 22:11:36 +03:00
|
|
|
union content_msg_data msg_data;
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
c->diagram = svgtiny_create();
|
|
|
|
if (c->diagram == NULL)
|
2008-02-03 22:11:36 +03:00
|
|
|
goto no_memory;
|
2007-07-13 07:53:14 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
c->done_parse = false;
|
2010-03-30 02:33:21 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
return NSERROR_OK;
|
2008-02-03 22:11:36 +03:00
|
|
|
|
|
|
|
no_memory:
|
|
|
|
msg_data.error = messages_get("NoMemory");
|
2011-05-07 00:40:09 +04:00
|
|
|
content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data);
|
|
|
|
return NSERROR_NOMEM;
|
2007-07-13 07:53:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a CONTENT_SVG for display.
|
|
|
|
*/
|
|
|
|
|
2010-03-30 02:33:21 +04:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void svg_reformat(struct content *c, int width, int height)
|
|
|
|
{
|
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
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (svg->done_parse == false) {
|
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,
|
2010-03-30 22:12:21 +04:00
|
|
|
content__get_url(c), width, height);
|
2010-03-30 11:09:49 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
svg->done_parse = true;
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-05-09 17:02:22 +04:00
|
|
|
static bool svg_redraw_internal(struct content *c, int x, int y,
|
2011-02-14 22:17:08 +03:00
|
|
|
int width, int height, const struct rect *clip,
|
2011-05-09 17:02:22 +04:00
|
|
|
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
|
|
|
bool ok;
|
|
|
|
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;
|
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) {
|
|
|
|
ok = plot.path(diagram->shape[i].path,
|
|
|
|
diagram->shape[i].path_length,
|
2008-07-29 03:26:54 +04:00
|
|
|
BGR(diagram->shape[i].fill),
|
2008-02-03 22:11:36 +03:00
|
|
|
diagram->shape[i].stroke_width,
|
2008-07-29 03:26:54 +04:00
|
|
|
BGR(diagram->shape[i].stroke),
|
2008-02-03 22:11:36 +03:00
|
|
|
transform);
|
|
|
|
if (!ok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} 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;
|
2011-03-08 21:47:15 +03:00
|
|
|
fstyle.size = (8 * FONT_SIZE_SCALE) * scale;
|
2009-07-21 14:59:53 +04:00
|
|
|
|
2007-07-16 03:22:54 +04:00
|
|
|
ok = plot.text(px, py,
|
2008-02-03 22:11:36 +03:00
|
|
|
diagram->shape[i].text,
|
|
|
|
strlen(diagram->shape[i].text),
|
2009-07-21 14:59:53 +04:00
|
|
|
&fstyle);
|
2009-05-28 20:21:32 +04:00
|
|
|
if (!ok)
|
|
|
|
return false;
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool svg_redraw(struct content *c, int x, int y,
|
|
|
|
int width, int height, const struct rect *clip,
|
|
|
|
float scale, colour background_colour,
|
|
|
|
bool repeat_x, bool repeat_y)
|
|
|
|
{
|
|
|
|
if ((width <= 0) && (height <= 0)) {
|
|
|
|
/* No point trying to plot SVG if it does not occupy a valid
|
|
|
|
* area */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((repeat_x == false) && (repeat_y == false)) {
|
|
|
|
/* Simple case: SVG is not tiled */
|
|
|
|
return svg_redraw_internal(c, x, y, width, height,
|
|
|
|
clip, scale, background_colour);
|
|
|
|
} 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;
|
|
|
|
if (repeat_x) {
|
|
|
|
for (; x0 > clip->x0; x0 -= width);
|
|
|
|
x1 = clip->x1;
|
|
|
|
} else {
|
|
|
|
x1 = x + 1;
|
|
|
|
}
|
|
|
|
y0 = y;
|
|
|
|
if (repeat_y) {
|
|
|
|
for (; y0 > clip->y0; y0 -= height);
|
|
|
|
y1 = clip->y1;
|
|
|
|
} else {
|
|
|
|
y1 = y + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Repeatedly plot the SVG across the area */
|
|
|
|
for (y = y0; y < y1; y += height) {
|
|
|
|
for (x = x0; x < x1; x += width) {
|
|
|
|
if (!svg_redraw_internal(c, x, y,
|
|
|
|
width, height, clip, scale,
|
|
|
|
background_colour)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-13 07:53:14 +04:00
|
|
|
/**
|
|
|
|
* Destroy a CONTENT_SVG and free all resources it owns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void svg_destroy(struct content *c)
|
|
|
|
{
|
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-05-07 00:40:09 +04:00
|
|
|
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;
|
|
|
|
|
|
|
|
svg = talloc_zero(0, svg_content);
|
|
|
|
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-05-07 00:40:09 +04:00
|
|
|
content_type svg_content_type(lwc_string *mime_type)
|
|
|
|
{
|
|
|
|
return CONTENT_IMAGE;
|
|
|
|
}
|
2010-04-04 16:41:19 +04:00
|
|
|
|
2007-07-18 18:49:26 +04:00
|
|
|
#endif /* WITH_NS_SVG */
|