summaryrefslogtreecommitdiff
path: root/content/content.c
blob: ecca5c2fd3216c01bb068a8f1849779057e5c43f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * $Id: content.c,v 1.3 2003/02/28 11:49:13 bursa Exp $
 */

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "netsurf/content/content.h"
#include "netsurf/render/html.h"
#include "netsurf/render/textplain.h"
#include "netsurf/riscos/jpeg.h"
#include "netsurf/utils/utils.h"


/* mime_map must be in sorted order by mime_type */
struct mime_entry {
	char mime_type[16];
	content_type type;
};
static const struct mime_entry mime_map[] = {
	{"image/jpeg", CONTENT_JPEG},
/*	{"image/png", CONTENT_PNG},
	{"text/css", CONTENT_CSS},*/
	{"text/html", CONTENT_HTML},
	{"text/plain", CONTENT_TEXTPLAIN},
};
#define MIME_MAP_COUNT (sizeof(mime_map) / sizeof(mime_map[0]))

/* handler_map must be ordered as enum content_type */
struct handler_entry {
	void (*create)(struct content *c);
	void (*process_data)(struct content *c, char *data, unsigned long size);
	int (*convert)(struct content *c, unsigned int width, unsigned int height);
	void (*revive)(struct content *c, unsigned int width, unsigned int height);
	void (*reformat)(struct content *c, unsigned int width, unsigned int height);
	void (*destroy)(struct content *c);
};
static const struct handler_entry handler_map[] = {
	{html_create, html_process_data, html_convert, html_revive, html_reformat, html_destroy},
	{textplain_create, textplain_process_data, textplain_convert,
		textplain_revive, textplain_reformat, textplain_destroy},
	{jpeg_create, jpeg_process_data, jpeg_convert, jpeg_revive, jpeg_reformat, jpeg_destroy},
/*	{css_create, css_process_data, css_convert, css_revive, css_destroy},
	{png_create, png_process_data, png_convert, png_revive, png_destroy},*/
};


/**
 * content_lookup -- look up mime type
 */

content_type content_lookup(const char *mime_type)
{
	struct mime_entry *m;
	m = bsearch(mime_type, mime_map, MIME_MAP_COUNT, sizeof(mime_map[0]),
			(int (*)(const void *, const void *)) strcmp);
	if (m == 0)
		return CONTENT_OTHER;
	return m->type;
}


/**
 * content_create -- create a content structure of the specified mime type
 */

struct content * content_create(content_type type, char *url)
{
	struct content *c;
	assert(type < CONTENT_OTHER);
	c = xcalloc(1, sizeof(struct content));
	c->url = xstrdup(url);
	c->type = type;
	c->status = CONTENT_LOADING;
	c->size = sizeof(struct content);
	handler_map[type].create(c);
	return c;
}


/**
 * content_process_data -- process a block source data
 */

void content_process_data(struct content *c, char *data, unsigned long size)
{
	assert(c != 0);
	assert(c->type < CONTENT_OTHER);
	assert(c->status == CONTENT_LOADING);
	handler_map[c->type].process_data(c, data, size);
}


/**
 * content_convert -- all data has arrived, complete the conversion
 */

int content_convert(struct content *c, unsigned long width, unsigned long height)
{
	assert(c != 0);
	assert(c->type < CONTENT_OTHER);
	assert(c->status == CONTENT_LOADING);
	if (handler_map[c->type].convert(c, width, height))
		return 1;
	c->status = CONTENT_READY;
	return 0;
}


/**
 * content_revive -- fix content that has been loaded from the cache
 *   eg. load dependencies, reformat to current width
 */

void content_revive(struct content *c, unsigned long width, unsigned long height)
{
	assert(c != 0);
	assert(c->type < CONTENT_OTHER);
	assert(c->status == CONTENT_READY);
	handler_map[c->type].revive(c, width, height);
}


/**
 * content_reformat -- reformat to new size
 */

void content_reformat(struct content *c, unsigned long width, unsigned long height)
{
	assert(c != 0);
	assert(c->type < CONTENT_OTHER);
	assert(c->status == CONTENT_READY);
	handler_map[c->type].reformat(c, width, height);
}


/**
 * content_destroy -- free content
 */

void content_destroy(struct content *c)
{
	assert(c != 0);
	assert(c->type < CONTENT_OTHER);
	handler_map[c->type].destroy(c);
	xfree(c);
}