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>
|
2012-03-24 21:11:17 +04:00
|
|
|
|
2012-03-25 00:55:22 +04:00
|
|
|
#include <dom/dom.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"
|
2012-07-22 23:52:34 +04:00
|
|
|
#include "utils/corestrings.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#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 */
|
2011-10-04 00:28:29 +04:00
|
|
|
nsurl *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 */
|
|
|
|
};
|
|
|
|
|
2012-03-25 19:07:08 +04:00
|
|
|
static bool imagemap_add(html_content *c, dom_string *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);
|
2012-03-24 21:11:17 +04:00
|
|
|
static bool imagemap_extract_map(dom_node *node, html_content *c,
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry **entry);
|
2012-03-24 21:11:17 +04:00
|
|
|
static bool imagemap_addtolist(dom_node *n, nsurl *base_url,
|
2012-03-25 19:07:08 +04:00
|
|
|
struct mapentry **entry, dom_string *tagtype);
|
2005-08-15 03:56:15 +04:00
|
|
|
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
|
|
|
|
*/
|
2012-03-25 19:07:08 +04:00
|
|
|
bool imagemap_add(html_content *c, dom_string *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;
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
/* \todo Stop relying on NULL termination of dom_string */
|
|
|
|
map->key = strdup(dom_string_data(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
|
|
|
|
2012-03-25 19:07:08 +04:00
|
|
|
slot = imagemap_hash(map->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:
|
2011-10-04 00:28:29 +04:00
|
|
|
LOG(("\tDefault: %s", nsurl_access(
|
|
|
|
entry->url)));
|
2005-08-15 03:56:15 +04:00
|
|
|
break;
|
|
|
|
case IMAGEMAP_RECT:
|
|
|
|
LOG(("\tRectangle: %s: [(%d,%d),(%d,%d)]",
|
2011-10-04 00:28:29 +04:00
|
|
|
nsurl_access(entry->url),
|
2005-08-15 03:56:15 +04:00
|
|
|
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]",
|
2011-10-04 00:28:29 +04:00
|
|
|
nsurl_access(entry->url),
|
2005-08-15 03:56:15 +04:00
|
|
|
entry->bounds.circle.x,
|
|
|
|
entry->bounds.circle.y,
|
|
|
|
entry->bounds.circle.r));
|
|
|
|
break;
|
|
|
|
case IMAGEMAP_POLY:
|
2011-10-04 00:28:29 +04:00
|
|
|
LOG(("\tPolygon: %s:", nsurl_access(
|
|
|
|
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
|
|
|
|
*
|
2012-03-25 19:07:08 +04:00
|
|
|
* \param c The content
|
|
|
|
* \param map_str A dom_string which is "map"
|
2005-08-15 03:56:15 +04:00
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
2012-03-25 19:07:08 +04:00
|
|
|
bool
|
|
|
|
imagemap_extract(html_content *c)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
2012-03-25 19:07:08 +04:00
|
|
|
dom_nodelist *nlist;
|
|
|
|
dom_exception exc;
|
|
|
|
unsigned long maybe_maps, mapnr;
|
|
|
|
|
|
|
|
exc = dom_document_get_elements_by_tag_name(c->document,
|
2012-07-22 23:52:34 +04:00
|
|
|
corestring_dom_map,
|
2012-03-25 19:07:08 +04:00
|
|
|
&nlist);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_nodelist_get_length(nlist, &maybe_maps);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto out_nlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mapnr = 0; mapnr < maybe_maps; ++mapnr) {
|
|
|
|
dom_node *node;
|
|
|
|
dom_string *name;
|
|
|
|
exc = dom_nodelist_item(nlist, mapnr, &node);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto out_nlist;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
exc = dom_element_get_attribute(node, corestring_dom_id,
|
2012-03-25 19:07:08 +04:00
|
|
|
&name);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
dom_node_unref(node);
|
|
|
|
goto out_nlist;
|
2009-07-03 02:35:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
exc = dom_element_get_attribute(node,
|
2012-07-22 23:52:34 +04:00
|
|
|
corestring_dom_name,
|
2012-03-25 19:07:08 +04:00
|
|
|
&name);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
dom_node_unref(node);
|
|
|
|
goto out_nlist;
|
|
|
|
}
|
2009-07-03 02:35:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
if (name != NULL) {
|
|
|
|
struct mapentry *entry = NULL;
|
|
|
|
if (imagemap_extract_map(node, c, &entry) == false) {
|
|
|
|
dom_string_unref(name);
|
|
|
|
dom_node_unref(node);
|
|
|
|
goto out_nlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 != NULL) &&
|
|
|
|
(imagemap_add(c, name, entry) == false)) {
|
|
|
|
dom_string_unref(name);
|
|
|
|
dom_node_unref(node);
|
|
|
|
goto out_nlist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dom_string_unref(name);
|
|
|
|
dom_node_unref(node);
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
out_nlist:
|
|
|
|
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
return false;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract an imagemap from html source
|
|
|
|
*
|
|
|
|
* \param node XML node containing map
|
|
|
|
* \param c Content containing document
|
|
|
|
* \param entry List of map entries
|
2012-03-25 19:07:08 +04:00
|
|
|
* \param tname The sub-tags to consider on this pass
|
2005-08-15 03:56:15 +04:00
|
|
|
* \return false on memory exhaustion, true otherwise
|
|
|
|
*/
|
2012-03-25 19:07:08 +04:00
|
|
|
static bool
|
|
|
|
imagemap_extract_map_entries(dom_node *node, html_content *c,
|
|
|
|
struct mapentry **entry, dom_string *tname)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
2012-03-25 19:07:08 +04:00
|
|
|
dom_nodelist *nlist;
|
|
|
|
dom_exception exc;
|
|
|
|
unsigned long tag_count, ent;
|
|
|
|
|
|
|
|
exc = dom_element_get_elements_by_tag_name(node, tname, &nlist);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return false;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
exc = dom_nodelist_get_length(nlist, &tag_count);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ent = 0; ent < tag_count; ++ent) {
|
|
|
|
dom_node *subnode;
|
|
|
|
|
|
|
|
exc = dom_nodelist_item(nlist, ent, &subnode);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
dom_nodelist_unref(nlist);
|
2005-08-15 03:56:15 +04:00
|
|
|
return false;
|
2012-03-25 19:07:08 +04:00
|
|
|
}
|
|
|
|
if (imagemap_addtolist(subnode, c->base_url,
|
|
|
|
entry, tname) == false) {
|
|
|
|
dom_node_unref(subnode);
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dom_node_unref(subnode);
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-25 19:07:08 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
bool imagemap_extract_map(dom_node *node, html_content *c,
|
|
|
|
struct mapentry **entry)
|
|
|
|
{
|
|
|
|
if (imagemap_extract_map_entries(node, c, entry,
|
2012-07-22 23:52:34 +04:00
|
|
|
corestring_dom_area) == false)
|
2012-03-25 19:07:08 +04:00
|
|
|
return false;
|
|
|
|
return imagemap_extract_map_entries(node, c, entry,
|
2012-07-22 23:52:34 +04:00
|
|
|
corestring_dom_a);
|
2012-03-25 19:07:08 +04:00
|
|
|
}
|
2005-08-15 03:56:15 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-03-25 19:07:08 +04:00
|
|
|
bool
|
|
|
|
imagemap_addtolist(dom_node *n, nsurl *base_url,
|
|
|
|
struct mapentry **entry, dom_string *tagtype)
|
2005-08-15 03:56:15 +04:00
|
|
|
{
|
2012-03-25 19:07:08 +04:00
|
|
|
dom_exception exc;
|
|
|
|
dom_string *href = NULL, *target = NULL, *shape = NULL;
|
|
|
|
dom_string *coords = NULL;
|
2005-08-15 03:56:15 +04:00
|
|
|
struct mapentry *new_map, *temp;
|
2012-03-25 19:07:08 +04:00
|
|
|
bool ret = true;
|
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
if (dom_string_caseless_isequal(tagtype, corestring_dom_area)) {
|
2012-03-25 19:07:08 +04:00
|
|
|
bool nohref = false;
|
|
|
|
exc = dom_element_has_attribute(n,
|
2012-07-22 23:52:34 +04:00
|
|
|
corestring_dom_nohref, &nohref);
|
2012-03-25 19:07:08 +04:00
|
|
|
if ((exc != DOM_NO_ERR) || nohref)
|
|
|
|
/* Skip <area nohref="anything" /> */
|
|
|
|
goto ok_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
exc = dom_element_get_attribute(n, corestring_dom_href, &href);
|
2012-03-25 19:07:08 +04:00
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
/* No href="" attribute, skip this element */
|
|
|
|
goto ok_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
exc = dom_element_get_attribute(n, corestring_dom_target, &target);
|
2012-03-25 19:07:08 +04:00
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto ok_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
exc = dom_element_get_attribute(n, corestring_dom_shape, &shape);
|
2012-03-25 19:07:08 +04:00
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto ok_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's no shape, we default to rectangles */
|
|
|
|
if (shape == NULL)
|
2012-07-22 23:52:34 +04:00
|
|
|
shape = dom_string_ref(corestring_dom_rect);
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
if (!dom_string_caseless_lwc_isequal(shape, corestring_lwc_default)) {
|
2012-03-25 19:07:08 +04:00
|
|
|
/* If not 'default' and there's no 'coords' give up */
|
2012-07-22 23:52:34 +04:00
|
|
|
exc = dom_element_get_attribute(n, corestring_dom_coords,
|
2012-03-25 19:07:08 +04:00
|
|
|
&coords);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
goto ok_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map = calloc(1, sizeof(*new_map));
|
2009-07-03 02:35:15 +04:00
|
|
|
if (new_map == NULL) {
|
2012-03-25 19:07:08 +04:00
|
|
|
goto bad_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
2012-07-22 23:52:34 +04:00
|
|
|
if (dom_string_caseless_lwc_isequal(shape, corestring_lwc_rect) ||
|
|
|
|
dom_string_caseless_lwc_isequal(shape, corestring_lwc_rectangle))
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_RECT;
|
2012-07-22 23:52:34 +04:00
|
|
|
else if (dom_string_caseless_lwc_isequal(shape, corestring_lwc_circle))
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_CIRCLE;
|
2012-07-22 23:52:34 +04:00
|
|
|
else if (dom_string_caseless_lwc_isequal(shape, corestring_lwc_poly) ||
|
|
|
|
dom_string_caseless_lwc_isequal(shape, corestring_lwc_polygon))
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_POLY;
|
2012-07-22 23:52:34 +04:00
|
|
|
else if (dom_string_caseless_lwc_isequal(shape, corestring_lwc_default))
|
2005-08-15 03:56:15 +04:00
|
|
|
new_map->type = IMAGEMAP_DEFAULT;
|
2012-03-25 19:07:08 +04:00
|
|
|
else
|
|
|
|
goto bad_out;
|
|
|
|
|
|
|
|
if (box_extract_link(dom_string_data(href),
|
|
|
|
base_url, &new_map->url) == false)
|
|
|
|
goto bad_out;
|
|
|
|
|
2009-07-03 02:35:15 +04:00
|
|
|
if (new_map->url == NULL) {
|
2012-03-25 19:07:08 +04:00
|
|
|
/* non-fatal error -> ignore this */
|
|
|
|
goto ok_free_map_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
if (target != NULL) {
|
|
|
|
/* Copy target into the map */
|
|
|
|
new_map->target = malloc(dom_string_byte_length(target) + 1);
|
|
|
|
if (new_map->target == NULL)
|
|
|
|
goto bad_out;
|
|
|
|
/* Safe, but relies on dom_strings being NULL terminated */
|
|
|
|
/* \todo Do this better */
|
|
|
|
strcpy(new_map->target, dom_string_data(target));
|
2006-09-06 04:15:10 +04:00
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
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 */
|
2012-03-25 19:07:08 +04:00
|
|
|
char *val = strtok((char *)dom_string_data(coords), ",");
|
2009-07-03 02:35:15 +04:00
|
|
|
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) {
|
2012-03-25 19:07:08 +04:00
|
|
|
goto bad_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
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) {
|
2012-03-25 19:07:08 +04:00
|
|
|
goto bad_out;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2012-03-25 19:07:08 +04:00
|
|
|
|
|
|
|
/* All good, linked in, let's clean up */
|
|
|
|
goto ok_out;
|
|
|
|
|
|
|
|
bad_out:
|
|
|
|
ret = false;
|
|
|
|
ok_free_map_out:
|
|
|
|
if (new_map->url != NULL)
|
|
|
|
nsurl_unref(new_map->url);
|
|
|
|
if (new_map->type == IMAGEMAP_POLY &&
|
|
|
|
new_map->bounds.poly.ycoords != NULL)
|
|
|
|
free(new_map->bounds.poly.ycoords);
|
|
|
|
if (new_map->type == IMAGEMAP_POLY &&
|
|
|
|
new_map->bounds.poly.xcoords != NULL)
|
|
|
|
free(new_map->bounds.poly.xcoords);
|
|
|
|
if (new_map->target != NULL)
|
|
|
|
free(new_map->target);
|
|
|
|
if (new_map != NULL)
|
|
|
|
free(new_map);
|
|
|
|
ok_out:
|
|
|
|
if (href != NULL)
|
|
|
|
dom_string_unref(href);
|
|
|
|
if (target != NULL)
|
|
|
|
dom_string_unref(target);
|
|
|
|
if (shape != NULL)
|
|
|
|
dom_string_unref(shape);
|
|
|
|
if (coords != NULL)
|
|
|
|
dom_string_unref(coords);
|
|
|
|
|
|
|
|
return ret;
|
2005-08-15 03:56:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2011-10-04 00:28:29 +04:00
|
|
|
nsurl_unref(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-10-04 00:28:29 +04:00
|
|
|
nsurl *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;
|
|
|
|
}
|