2005-08-15 03:56:15 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
|
|
|
|
*
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2005-08-15 03:56:15 +04:00
|
|
|
* Much of this shamelessly copied from utils/messages.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2008-08-11 20:44:12 +04:00
|
|
|
#include <strings.h>
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/content_protected.h"
|
|
|
|
#include "content/hlcache.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "render/box.h"
|
2011-05-07 00:40:09 +04:00
|
|
|
#include "render/html_internal.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "render/imagemap.h"
|
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/utils.h"
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
#define HASH_SIZE 31 /* fixed size hash table */
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
IMAGEMAP_DEFAULT,
|
|
|
|
IMAGEMAP_RECT,
|
|
|
|
IMAGEMAP_CIRCLE,
|
|
|
|
IMAGEMAP_POLY
|
|
|
|
} imagemap_entry_type;
|
|
|
|
|
|
|
|
struct mapentry {
|
|
|
|
imagemap_entry_type type; /**< type of shape */
|
|
|
|
char *url; /**< absolute url to go to */
|
2006-09-06 04:15:10 +04:00
|
|
|
char *target; /**< target frame (if any) */
|
2005-08-15 03:56:15 +04:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int x; /**< x coordinate of centre */
|
|
|
|
int y; /**< y coordinate of center */
|
|
|
|
int r; /**< radius of circle */
|
|
|
|
} circle;
|
|
|
|
struct {
|
|
|
|
int x0; /**< left hand edge */
|
|
|
|
int y0; /**< top edge */
|
|
|
|
int x1; /**< right hand edge */
|
|
|
|
int y1; /**< bottom edge */
|
|
|
|
} rect;
|
|
|
|
struct {
|
|
|
|
int num; /**< number of points */
|
|
|
|
float *xcoords; /**< x coordinates */
|
|
|
|
float *ycoords; /**< y coordinates */
|
|
|
|
} poly;
|
|
|
|
} bounds;
|
|
|
|
struct mapentry *next; /**< next entry in list */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct imagemap {
|
|
|
|
char *key; /**< key for this entry */
|
|
|
|
struct mapentry *list; /**< pointer to linked list of entries */
|
|
|
|
struct imagemap *next; /**< next entry in this hash chain */
|
|
|
|
};
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
static bool imagemap_add(html_content *c, const char *key,
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry *list);
|
2011-05-07 00:40:09 +04:00
|
|
|
static bool imagemap_create(html_content *c);
|
|
|
|
static bool imagemap_extract_map(xmlNode *node, html_content *c,
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry **entry);
|
|
|
|
static bool imagemap_addtolist(xmlNode *n, char *base_url,
|
|
|
|
struct mapentry **entry);
|
|
|
|
static void imagemap_freelist(struct mapentry *list);
|
|
|
|
static unsigned int imagemap_hash(const char *key);
|
|
|
|
static int imagemap_point_in_poly(int num, float *xpt, float *ypt,
|
|
|
|
unsigned long x, unsigned long y, unsigned long click_x,
|
|
|
|
unsigned long click_y);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an imagemap to the hashtable, creating it if it doesn't exist
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param c The containing content
|
|
|
|
* \param key The name of the imagemap
|
|
|
|
* \param list List of map regions
|
2005-08-15 03:56:15 +04:00
|
|
|
* \return true on succes, false otherwise
|
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
bool imagemap_add(html_content *c, const char *key, struct mapentry *list)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
struct imagemap *map;
|
|
|
|
unsigned int slot;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
assert(key != NULL);
|
|
|
|
assert(list != NULL);
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (imagemap_create(c) == false)
|
|
|
|
return false;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
map = calloc(1, sizeof(*map));
|
2009-07-03 02:35:15 +04:00
|
|
|
if (map == NULL)
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
map->key = strdup(key);
|
2009-07-03 02:35:15 +04:00
|
|
|
if (map->key == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
free(map);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
map->list = list;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
slot = imagemap_hash(key);
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
map->next = c->imagemaps[slot];
|
|
|
|
c->imagemaps[slot] = map;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create hashtable of imagemaps
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param c The containing content
|
2005-08-15 03:56:15 +04:00
|
|
|
* \return true on success, false otherwise
|
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
bool imagemap_create(html_content *c)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
assert(c != NULL);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (c->imagemaps == NULL) {
|
|
|
|
c->imagemaps = calloc(HASH_SIZE, sizeof(struct imagemap));
|
|
|
|
if (c->imagemaps == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy hashtable of imagemaps
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param c The containing content
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
void imagemap_destroy(html_content *c)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
|
|
|
/* no imagemaps -> return */
|
2011-05-07 00:40:09 +04:00
|
|
|
if (c->imagemaps == NULL)
|
2009-07-03 02:35:15 +04:00
|
|
|
return;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
for (i = 0; i != HASH_SIZE; i++) {
|
|
|
|
struct imagemap *map, *next;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
map = c->imagemaps[i];
|
2009-07-03 02:35:15 +04:00
|
|
|
while (map != NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
next = map->next;
|
|
|
|
imagemap_freelist(map->list);
|
|
|
|
free(map->key);
|
|
|
|
free(map);
|
|
|
|
map = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
free(c->imagemaps);
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump imagemap data to the log
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param c The containing content
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
void imagemap_dump(html_content *c)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (c->imagemaps == NULL)
|
2009-07-03 02:35:15 +04:00
|
|
|
return;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
for (i = 0; i != HASH_SIZE; i++) {
|
|
|
|
struct imagemap *map;
|
|
|
|
struct mapentry *entry;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
map = c->imagemaps[i];
|
2009-07-03 02:35:15 +04:00
|
|
|
while (map != NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
LOG(("Imagemap: %s", map->key));
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
for (entry = map->list; entry; entry = entry->next) {
|
|
|
|
switch (entry->type) {
|
|
|
|
case IMAGEMAP_DEFAULT:
|
|
|
|
LOG(("\tDefault: %s", entry->url));
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_RECT:
|
|
|
|
LOG(("\tRectangle: %s: [(%d,%d),(%d,%d)]",
|
|
|
|
entry->url,
|
|
|
|
entry->bounds.rect.x0,
|
|
|
|
entry->bounds.rect.y0,
|
|
|
|
entry->bounds.rect.x1,
|
|
|
|
entry->bounds.rect.y1));
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_CIRCLE:
|
|
|
|
LOG(("\tCircle: %s: [(%d,%d),%d]",
|
|
|
|
entry->url,
|
|
|
|
entry->bounds.circle.x,
|
|
|
|
entry->bounds.circle.y,
|
|
|
|
entry->bounds.circle.r));
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_POLY:
|
|
|
|
LOG(("\tPolygon: %s:", entry->url));
|
2009-07-03 02:35:15 +04:00
|
|
|
for (j = 0; j != entry->bounds.poly.num;
|
2005-08-15 03:56:15 +04:00
|
|
|
j++) {
|
|
|
|
fprintf(stderr, "(%d,%d) ",
|
|
|
|
(int)entry->bounds.poly.xcoords[j],
|
|
|
|
(int)entry->bounds.poly.ycoords[j]);
|
|
|
|
}
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map = map->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract all imagemaps from a document tree
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param node Root node of tree
|
|
|
|
* \param c The containing content
|
2005-08-15 03:56:15 +04:00
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
bool imagemap_extract(xmlNode *node, html_content *c)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
xmlNode *this_node;
|
2009-07-03 02:35:15 +04:00
|
|
|
struct mapentry *entry = NULL;
|
2005-08-15 03:56:15 +04:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
assert(node != NULL);
|
|
|
|
assert(c != NULL);
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (node->type == XML_ELEMENT_NODE &&
|
|
|
|
strcmp((const char *) node->name, "map") == 0) {
|
|
|
|
/* Ignore maps with no id or name */
|
|
|
|
if ((name = (char *) xmlGetProp(node,
|
|
|
|
(const xmlChar *) "id")) == NULL &&
|
|
|
|
(name = (char *) xmlGetProp(node,
|
|
|
|
(const xmlChar *) "name")) == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
|
|
|
if (imagemap_extract_map(node, c, &entry) == false) {
|
|
|
|
xmlFree(name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* imagemap_extract_map may not extract anything,
|
|
|
|
* so entry can still be NULL here. This isn't an
|
|
|
|
* error as it just means that we've encountered
|
|
|
|
* an incorrectly defined <map>...</map> block
|
|
|
|
*/
|
|
|
|
if (entry && imagemap_add(c, name, entry) == false) {
|
|
|
|
xmlFree(name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlFree(name);
|
|
|
|
return true;
|
2009-07-03 03:05:10 +04:00
|
|
|
} else if (node->type != XML_ELEMENT_NODE) {
|
2009-07-03 02:35:15 +04:00
|
|
|
return true;
|
2009-07-03 03:05:10 +04:00
|
|
|
}
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
/* now recurse */
|
2009-07-03 02:35:15 +04:00
|
|
|
for (this_node = node->children; this_node != NULL;
|
|
|
|
this_node = this_node->next) {
|
|
|
|
if (imagemap_extract(this_node, c) == false)
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract an imagemap from html source
|
|
|
|
*
|
|
|
|
* \param node XML node containing map
|
|
|
|
* \param c Content containing document
|
|
|
|
* \param entry List of map entries
|
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
2011-05-07 00:40:09 +04:00
|
|
|
bool imagemap_extract_map(xmlNode *node, html_content *c,
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry **entry)
|
|
|
|
{
|
|
|
|
xmlNode *this_node;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
|
|
|
assert(entry != NULL);
|
|
|
|
|
|
|
|
if (node->type == XML_ELEMENT_NODE) {
|
|
|
|
/** \todo ignore <area> elements if there are other
|
|
|
|
* block-level elements present in map
|
|
|
|
*/
|
2008-02-25 20:58:00 +03:00
|
|
|
if (strcmp((const char *) node->name, "area") == 0 ||
|
|
|
|
strcmp((const char *) node->name, "a") == 0) {
|
2011-05-07 00:40:09 +04:00
|
|
|
if (imagemap_addtolist(node, c->base_url,
|
2009-07-03 02:35:15 +04:00
|
|
|
entry) == false)
|
2008-05-29 00:05:30 +04:00
|
|
|
return false;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2008-05-29 00:05:30 +04:00
|
|
|
} else {
|
|
|
|
return true;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
for (this_node = node->children; this_node != NULL;
|
|
|
|
this_node = this_node->next) {
|
|
|
|
if (imagemap_extract_map(this_node, c, entry) == false)
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an imagemap entry to the list
|
|
|
|
*
|
|
|
|
* \param n The xmlNode representing the entry to add
|
|
|
|
* \param base_url Base URL for resolving relative URLs
|
|
|
|
* \param entry Pointer to list of entries
|
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
|
|
|
bool imagemap_addtolist(xmlNode *n, char *base_url, struct mapentry **entry)
|
|
|
|
{
|
2009-07-03 02:35:15 +04:00
|
|
|
char *shape, *coords = NULL, *href, *target = NULL;
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry *new_map, *temp;
|
|
|
|
|
|
|
|
assert(n != NULL);
|
|
|
|
assert(base_url != NULL);
|
|
|
|
assert(entry != NULL);
|
|
|
|
|
2008-02-25 20:58:00 +03:00
|
|
|
if (strcmp((const char *) n->name, "area") == 0) {
|
2009-07-03 02:35:15 +04:00
|
|
|
xmlChar *nohref = xmlGetProp(n, (const xmlChar *) "nohref");
|
|
|
|
|
|
|
|
if (nohref != NULL) {
|
|
|
|
/* nohref attribute present - ignore this entry */
|
|
|
|
xmlFree(nohref);
|
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
/* no href -> ignore */
|
2009-07-03 03:05:10 +04:00
|
|
|
if ((href = (char *) xmlGetProp(n, (const xmlChar *) "href")) == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
2006-09-06 04:15:10 +04:00
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
target = (char *) xmlGetProp(n, (const xmlChar *) "target");
|
2006-09-06 04:15:10 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
/* no shape -> shape is a rectangle */
|
2009-07-03 02:35:15 +04:00
|
|
|
if ((shape = (char *) xmlGetProp(n,
|
|
|
|
(const xmlChar *) "shape")) == NULL) {
|
|
|
|
shape = (char *) xmlMemStrdup("rect");
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
if (strcasecmp(shape, "default") != 0) {
|
|
|
|
/* no coords -> ignore */
|
2009-07-03 02:35:15 +04:00
|
|
|
if ((coords = (char *) xmlGetProp(n,
|
|
|
|
(const xmlChar *) "coords")) == NULL) {
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
xmlFree(target);
|
2005-08-15 03:56:15 +04:00
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_map = calloc(1, sizeof(*new_map));
|
2009-07-03 02:35:15 +04:00
|
|
|
if (new_map == NULL) {
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
xmlFree(target);
|
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract area shape */
|
2008-07-29 13:48:08 +04:00
|
|
|
if (strcasecmp(shape, "rect") == 0 ||
|
|
|
|
strcasecmp(shape, "rectangle") == 0) {
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_RECT;
|
2009-07-03 02:35:15 +04:00
|
|
|
} else if (strcasecmp(shape, "circle") == 0) {
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_CIRCLE;
|
2009-07-03 02:35:15 +04:00
|
|
|
} else if (strcasecmp(shape, "poly") == 0 ||
|
|
|
|
strcasecmp(shape, "polygon") == 0) {
|
2005-08-15 03:56:15 +04:00
|
|
|
/* polygon shape name is not valid but sites use it */
|
|
|
|
new_map->type = IMAGEMAP_POLY;
|
2009-07-03 02:35:15 +04:00
|
|
|
} else if (strcasecmp(shape, "default") == 0) {
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_DEFAULT;
|
2009-07-03 02:35:15 +04:00
|
|
|
} else { /* unknown shape -> bail */
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
xmlFree(target);
|
2005-08-15 03:56:15 +04:00
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (box_extract_link(href, base_url, &new_map->url) == false) {
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
xmlFree(target);
|
2005-08-15 03:56:15 +04:00
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (new_map->url == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
/* non-fatal error -> ignore this entry */
|
|
|
|
free(new_map);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
xmlFree(target);
|
2005-08-15 03:56:15 +04:00
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target) {
|
|
|
|
new_map->target = strdup(target);
|
2009-07-03 02:35:15 +04:00
|
|
|
if (new_map->target == NULL) {
|
2006-09-06 04:15:10 +04:00
|
|
|
free(new_map->url);
|
|
|
|
free(new_map);
|
|
|
|
xmlFree(target);
|
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no longer needed */
|
|
|
|
xmlFree(target);
|
|
|
|
}
|
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
if (new_map->type != IMAGEMAP_DEFAULT) {
|
2008-08-09 00:12:38 +04:00
|
|
|
int x, y;
|
|
|
|
float *xcoords, *ycoords;
|
2005-08-15 03:56:15 +04:00
|
|
|
/* coordinates are a comma-separated list of values */
|
2009-07-03 02:35:15 +04:00
|
|
|
char *val = strtok(coords, ",");
|
|
|
|
int num = 1;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
switch (new_map->type) {
|
|
|
|
case IMAGEMAP_RECT:
|
|
|
|
/* (left, top, right, bottom) */
|
2009-07-03 02:35:15 +04:00
|
|
|
while (val != NULL && num <= 4) {
|
2005-08-15 03:56:15 +04:00
|
|
|
switch (num) {
|
|
|
|
case 1:
|
|
|
|
new_map->bounds.rect.x0 = atoi(val);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
new_map->bounds.rect.y0 = atoi(val);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
new_map->bounds.rect.x1 = atoi(val);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
new_map->bounds.rect.y1 = atoi(val);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
num++;
|
|
|
|
val = strtok('\0', ",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_CIRCLE:
|
|
|
|
/* (x, y, radius ) */
|
2009-07-03 02:35:15 +04:00
|
|
|
while (val != NULL && num <= 3) {
|
2005-08-15 03:56:15 +04:00
|
|
|
switch (num) {
|
|
|
|
case 1:
|
|
|
|
new_map->bounds.circle.x = atoi(val);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
new_map->bounds.circle.y = atoi(val);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
new_map->bounds.circle.r = atoi(val);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
num++;
|
|
|
|
val = strtok('\0', ",");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_POLY:
|
2009-07-03 02:35:15 +04:00
|
|
|
new_map->bounds.poly.xcoords = NULL;
|
|
|
|
new_map->bounds.poly.ycoords = NULL;
|
|
|
|
|
|
|
|
while (val != NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
x = atoi(val);
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
val = strtok('\0', ",");
|
2009-07-03 02:35:15 +04:00
|
|
|
if (val == NULL)
|
2005-08-15 03:56:15 +04:00
|
|
|
break;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
y = atoi(val);
|
|
|
|
|
|
|
|
xcoords = realloc(new_map->bounds.poly.xcoords,
|
2009-07-03 02:35:15 +04:00
|
|
|
num * sizeof(float));
|
|
|
|
if (xcoords == NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map->bounds.poly.ycoords);
|
|
|
|
free(new_map->bounds.poly.xcoords);
|
2006-09-06 04:15:10 +04:00
|
|
|
free(new_map->target);
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map->url);
|
|
|
|
free(new_map);
|
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
|
|
|
xmlFree(coords);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
ycoords = realloc(new_map->bounds.poly.ycoords,
|
2009-07-03 02:35:15 +04:00
|
|
|
num * sizeof(float));
|
|
|
|
if (ycoords == NULL) {
|
2009-11-22 16:55:32 +03:00
|
|
|
free(xcoords);
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map->bounds.poly.ycoords);
|
|
|
|
free(new_map->bounds.poly.xcoords);
|
2006-09-06 04:15:10 +04:00
|
|
|
free(new_map->target);
|
2005-08-15 03:56:15 +04:00
|
|
|
free(new_map->url);
|
|
|
|
free(new_map);
|
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
|
|
|
xmlFree(coords);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_map->bounds.poly.xcoords = xcoords;
|
|
|
|
new_map->bounds.poly.ycoords = ycoords;
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
new_map->bounds.poly.xcoords[num - 1] = x;
|
|
|
|
new_map->bounds.poly.ycoords[num - 1] = y;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
num++;
|
|
|
|
val = strtok('\0', ",");
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
new_map->bounds.poly.num = num - 1;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
new_map->next = NULL;
|
|
|
|
|
|
|
|
if (entry && *entry) {
|
2005-08-15 03:56:15 +04:00
|
|
|
/* add to END of list */
|
2009-07-03 02:35:15 +04:00
|
|
|
for (temp = (*entry); temp->next != NULL; temp = temp->next)
|
2005-08-15 03:56:15 +04:00
|
|
|
;
|
|
|
|
temp->next = new_map;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(*entry) = new_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlFree(href);
|
|
|
|
xmlFree(shape);
|
2006-09-06 04:15:10 +04:00
|
|
|
if (coords)
|
|
|
|
xmlFree(coords);
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free list of imagemap entries
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param list Pointer to head of list
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
|
|
|
void imagemap_freelist(struct mapentry *list)
|
|
|
|
{
|
|
|
|
struct mapentry *entry, *prev;
|
|
|
|
|
|
|
|
assert(list != NULL);
|
|
|
|
|
|
|
|
entry = list;
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
while (entry != NULL) {
|
2005-08-15 03:56:15 +04:00
|
|
|
prev = entry;
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
free(entry->url);
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2006-09-06 04:15:10 +04:00
|
|
|
if (entry->target)
|
|
|
|
free(entry->target);
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
if (entry->type == IMAGEMAP_POLY) {
|
|
|
|
free(entry->bounds.poly.xcoords);
|
|
|
|
free(entry->bounds.poly.ycoords);
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
entry = entry->next;
|
|
|
|
free(prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve url associated with imagemap entry
|
|
|
|
*
|
2010-03-28 16:56:39 +04:00
|
|
|
* \param h The containing content
|
|
|
|
* \param key The map name to search for
|
|
|
|
* \param x The left edge of the containing box
|
|
|
|
* \param y The top edge of the containing box
|
|
|
|
* \param click_x The horizontal location of the click
|
|
|
|
* \param click_y The vertical location of the click
|
|
|
|
* \param target Pointer to location to receive target pointer (if any)
|
2006-09-06 04:15:10 +04:00
|
|
|
* \return The url associated with this area, or NULL if not found
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
2011-09-06 20:26:02 +04:00
|
|
|
const char *imagemap_get(struct html_content *c, const char *key,
|
2006-09-06 04:15:10 +04:00
|
|
|
unsigned long x, unsigned long y,
|
|
|
|
unsigned long click_x, unsigned long click_y,
|
|
|
|
const char **target)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
|
|
|
unsigned int slot = 0;
|
|
|
|
struct imagemap *map;
|
|
|
|
struct mapentry *entry;
|
|
|
|
unsigned long cx, cy;
|
|
|
|
|
|
|
|
assert(c != NULL);
|
2009-07-03 02:35:15 +04:00
|
|
|
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
if (c->imagemaps == NULL)
|
2009-07-03 02:35:15 +04:00
|
|
|
return NULL;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
slot = imagemap_hash(key);
|
|
|
|
|
2011-05-07 00:40:09 +04:00
|
|
|
for (map = c->imagemaps[slot]; map != NULL; map = map->next) {
|
2009-07-03 02:35:15 +04:00
|
|
|
if (map->key != NULL && strcasecmp(map->key, key) == 0)
|
2005-08-15 03:56:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (map == NULL || map->list == NULL)
|
|
|
|
return NULL;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
for (entry = map->list; entry; entry = entry->next) {
|
|
|
|
switch (entry->type) {
|
|
|
|
case IMAGEMAP_DEFAULT:
|
|
|
|
/* just return the URL. no checks required */
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
*target = entry->target;
|
2005-08-15 03:56:15 +04:00
|
|
|
return entry->url;
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_RECT:
|
|
|
|
if (click_x >= x + entry->bounds.rect.x0 &&
|
|
|
|
click_x <= x + entry->bounds.rect.x1 &&
|
|
|
|
click_y >= y + entry->bounds.rect.y0 &&
|
|
|
|
click_y <= y + entry->bounds.rect.y1) {
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
*target = entry->target;
|
2005-08-15 03:56:15 +04:00
|
|
|
return entry->url;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_CIRCLE:
|
|
|
|
cx = x + entry->bounds.circle.x - click_x;
|
|
|
|
cy = y + entry->bounds.circle.y - click_y;
|
|
|
|
if ((cx * cx + cy * cy) <=
|
2009-07-03 02:35:15 +04:00
|
|
|
(unsigned long) (entry->bounds.circle.r *
|
2005-08-15 03:56:15 +04:00
|
|
|
entry->bounds.circle.r)) {
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
*target = entry->target;
|
2005-08-15 03:56:15 +04:00
|
|
|
return entry->url;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_POLY:
|
|
|
|
if (imagemap_point_in_poly(entry->bounds.poly.num,
|
|
|
|
entry->bounds.poly.xcoords,
|
|
|
|
entry->bounds.poly.ycoords, x, y,
|
|
|
|
click_x, click_y)) {
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
*target = entry->target;
|
2005-08-15 03:56:15 +04:00
|
|
|
return entry->url;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-06 04:15:10 +04:00
|
|
|
if (target)
|
|
|
|
*target = NULL;
|
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash function
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param key The key to hash
|
|
|
|
* \return The hashed value
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
|
|
|
unsigned int imagemap_hash(const char *key)
|
|
|
|
{
|
|
|
|
unsigned int z = 0;
|
|
|
|
|
|
|
|
if (key == 0) return 0;
|
|
|
|
|
|
|
|
for (; *key != 0; key++) {
|
|
|
|
z += *key & 0x1f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (z % (HASH_SIZE - 1)) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a point lies within an arbitrary polygon
|
|
|
|
* Modified from comp.graphics.algorithms FAQ 2.03
|
|
|
|
*
|
2006-09-06 04:15:10 +04:00
|
|
|
* \param num Number of vertices
|
|
|
|
* \param xpt Array of x coordinates
|
|
|
|
* \param ypt Array of y coordinates
|
|
|
|
* \param x Left hand edge of containing box
|
|
|
|
* \param y Top edge of containing box
|
|
|
|
* \param click_x X coordinate of click
|
|
|
|
* \param click_y Y coordinate of click
|
|
|
|
* \return 1 if point is in polygon, 0 if outside. 0 or 1 if on boundary
|
2005-08-15 03:56:15 +04:00
|
|
|
*/
|
|
|
|
int imagemap_point_in_poly(int num, float *xpt, float *ypt, unsigned long x,
|
|
|
|
unsigned long y, unsigned long click_x,
|
|
|
|
unsigned long click_y)
|
|
|
|
{
|
2009-07-03 02:35:15 +04:00
|
|
|
int i, j, c = 0;
|
2005-08-15 03:56:15 +04:00
|
|
|
|
|
|
|
assert(xpt != NULL);
|
|
|
|
assert(ypt != NULL);
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
for (i = 0, j = num - 1; i < num; j = i++) {
|
|
|
|
if ((((ypt[i] + y <= click_y) && (click_y < ypt[j] + y)) ||
|
|
|
|
((ypt[j] + y <= click_y) && (click_y < ypt[i] + y))) &&
|
2005-08-15 03:56:15 +04:00
|
|
|
(click_x < (xpt[j] - xpt[i]) *
|
2009-07-03 02:35:15 +04:00
|
|
|
(click_y - (ypt[i] + y)) / (ypt[j] - ypt[i]) + xpt[i] + x))
|
2005-08-15 03:56:15 +04:00
|
|
|
c = !c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|