NetSurf
draw.c
Go to the documentation of this file.
1/*
2 * Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Content for image/x-drawfile (RISC OS implementation).
21 *
22 * The DrawFile module is used to plot the DrawFile.
23 */
24
25#include "utils/config.h"
26#ifdef WITH_DRAW
27
28#include <string.h>
29#include <stdlib.h>
30#include "oslib/drawfile.h"
31
32#include "utils/config.h"
33#include "utils/log.h"
34#include "utils/messages.h"
35#include "utils/utils.h"
36#include "netsurf/plotters.h"
37#include "netsurf/content.h"
38#include "content/content.h"
41#include "content/llcache.h"
42
44#include "riscos/gui.h"
45
46typedef struct draw_content {
47 struct content base;
48
49 int x0, y0;
50} draw_content;
51
52static nserror draw_create(const content_handler *handler,
53 lwc_string *imime_type, const struct http_parameter *params,
55 bool quirks, struct content **c);
56static bool draw_convert(struct content *c);
57static void draw_destroy(struct content *c);
58static bool draw_redraw(struct content *c, struct content_redraw_data *data,
59 const struct rect *clip, const struct redraw_context *ctx);
60static nserror draw_clone(const struct content *old, struct content **newc);
61static content_type draw_content_type(void);
62
63static const content_handler draw_content_handler = {
64 .create = draw_create,
65 .data_complete = draw_convert,
66 .destroy = draw_destroy,
67 .redraw = draw_redraw,
68 .clone = draw_clone,
69 .type = draw_content_type,
70 .no_share = false,
71};
72
73static const char *draw_types[] = {
74 "application/drawfile",
75 "application/x-drawfile",
76 "image/drawfile",
77 "image/x-drawfile"
78};
79
80CONTENT_FACTORY_REGISTER_TYPES(draw, draw_types, draw_content_handler)
81
82nserror draw_create(const content_handler *handler,
83 lwc_string *imime_type, const struct http_parameter *params,
85 bool quirks, struct content **c)
86{
87 draw_content *draw;
88 nserror error;
89
90 draw = calloc(1, sizeof(draw_content));
91 if (draw == NULL)
92 return NSERROR_NOMEM;
93
94 error = content__init(&draw->base, handler, imime_type, params,
96 if (error != NSERROR_OK) {
97 free(draw);
98 return error;
99 }
100
101 *c = (struct content *) draw;
102
103 return NSERROR_OK;
104}
105
106/**
107 * Convert a CONTENT_DRAW for display.
108 *
109 * No conversion is necessary. We merely read the DrawFile dimensions and
110 * bounding box bottom-left.
111 */
112
113bool draw_convert(struct content *c)
114{
115 draw_content *draw = (draw_content *) c;
116 union content_msg_data msg_data;
117 const uint8_t *source_data;
118 size_t source_size;
119 const void *data;
120 os_box bbox;
121 os_error *error;
122 char *title;
123
124 source_data = content__get_source_data(c, &source_size);
125 data = source_data;
126
127 /* BBox contents in Draw units (256*OS unit) */
128 error = xdrawfile_bbox(0, (drawfile_diagram *) data,
129 (int) source_size, 0, &bbox);
130 if (error) {
131 NSLOG(netsurf, INFO, "xdrawfile_bbox: 0x%x: %s",
132 error->errnum, error->errmess);
133 msg_data.errordata.errorcode = NSERROR_UNKNOWN;
134 msg_data.errordata.errormsg = error->errmess;
136 return false;
137 }
138
139 if (bbox.x1 > bbox.x0 && bbox.y1 > bbox.y0) {
140 /* c->width & c->height stored as (OS units/2)
141 => divide by 512 to convert from draw units */
142 c->width = ((bbox.x1 - bbox.x0) / 512);
143 c->height = ((bbox.y1 - bbox.y0) / 512);
144 }
145 else
146 /* invalid/undefined bounding box */
147 c->height = c->width = 0;
148
149 draw->x0 = bbox.x0;
150 draw->y0 = bbox.y0;
151
152 title = messages_get_buff("DrawTitle",
154 c->width, c->height);
155 if (title != NULL) {
157 free(title);
158 }
159
162 /* Done: update status bar */
163 content_set_status(c, "");
164 return true;
165}
166
167
168/**
169 * Destroy a CONTENT_DRAW and free all resources it owns.
170 */
171
172void draw_destroy(struct content *c)
173{
174}
175
176
177/**
178 * Redraw a CONTENT_DRAW.
179 */
180
181bool draw_redraw(struct content *c, struct content_redraw_data *data,
182 const struct rect *clip, const struct redraw_context *ctx)
183{
184 draw_content *draw = (draw_content *) c;
185 os_trfm matrix;
186 const uint8_t *source_data;
187 size_t source_size;
188 const void *src_data;
189 os_error *error;
190
191 if (ctx->plot->flush && (ctx->plot->flush(ctx) != NSERROR_OK))
192 return false;
193
194 if (!c->width || !c->height)
195 return false;
196
197 source_data = content__get_source_data(c, &source_size);
198 src_data = source_data;
199
200 /* Scaled image. Transform units (65536*OS units) */
201 matrix.entries[0][0] = data->width * 65536 / c->width;
202 matrix.entries[0][1] = 0;
203 matrix.entries[1][0] = 0;
204 matrix.entries[1][1] = data->height * 65536 / c->height;
205 /* Draw units. (x,y) = bottom left */
206 matrix.entries[2][0] = ro_plot_origin_x * 256 + data->x * 512 -
207 draw->x0 * data->width / c->width;
208 matrix.entries[2][1] = ro_plot_origin_y * 256 -
209 (data->y + data->height) * 512 -
210 draw->y0 * data->height / c->height;
211
212 error = xdrawfile_render(0, (drawfile_diagram *) src_data,
213 (int) source_size, &matrix, 0, 0);
214 if (error) {
215 NSLOG(netsurf, INFO, "xdrawfile_render: 0x%x: %s",
216 error->errnum, error->errmess);
217 return false;
218 }
219
220 return true;
221}
222
223/**
224 * Clone a CONTENT_DRAW
225 */
226
227nserror draw_clone(const struct content *old, struct content **newc)
228{
229 draw_content *draw;
230 nserror error;
231
232 draw = calloc(1, sizeof(draw_content));
233 if (draw == NULL)
234 return NSERROR_NOMEM;
235
236 error = content__clone(old, &draw->base);
237 if (error != NSERROR_OK) {
238 content_destroy(&draw->base);
239 return error;
240 }
241
242 /* Simply rerun convert */
243 if (old->status == CONTENT_STATUS_READY ||
244 old->status == CONTENT_STATUS_DONE) {
245 if (draw_convert(&draw->base) == false) {
246 content_destroy(&draw->base);
248 }
249 }
250
251 *newc = (struct content *) draw;
252
253 return NSERROR_OK;
254}
255
256content_type draw_content_type(void)
257{
258 return CONTENT_IMAGE;
259}
260
261#endif
Content handling interface.
void content_destroy(struct content *c)
Destroy and free a content.
Definition: content.c:354
void content_broadcast(struct content *c, content_msg msg, const union content_msg_data *data)
Send a message to all users.
Definition: content.c:752
void content_set_done(struct content *c)
Put a content in status CONTENT_STATUS_DONE.
Definition: content.c:299
bool content__set_title(struct content *c, const char *title)
Set title associated with content.
Definition: content.c:1090
nserror content__init(struct content *c, const content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, llcache_handle *llcache, const char *fallback_charset, bool quirks)
Definition: content.c:190
const uint8_t * content__get_source_data(struct content *c, size_t *size)
Retrieve source of content.
Definition: content.c:1216
nserror content__clone(const struct content *c, struct content *nc)
Clone a content's data members.
Definition: content.c:1382
void content_set_ready(struct content *c)
Put a content in status CONTENT_STATUS_READY and unlock the content.
Definition: content.c:285
void content_set_status(struct content *c, const char *status_message)
Updates content with new status.
Definition: content.c:270
#define CONTENT_FACTORY_REGISTER_TYPES(HNAME, HTYPELIST, HHANDLER)
Protected interface to Content handling.
@ CONTENT_STATUS_READY
Some parts of content still being loaded, but can be displayed.
Definition: content_type.h:92
@ CONTENT_STATUS_DONE
Content has completed all processing.
Definition: content_type.h:95
content_type
The type of a content.
Definition: content_type.h:53
@ CONTENT_IMAGE
All images.
Definition: content_type.h:67
@ CONTENT_MSG_ERROR
error occurred
Definition: content_type.h:122
Content for image/x-drawfile (RISC OS interface).
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_UNKNOWN
Unknown error - DO NOT USE.
Definition: errors.h:31
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_CLONE_FAILED
Failed to clone handle.
Definition: errors.h:37
@ NSERROR_OK
No error.
Definition: errors.h:30
Public content interface.
Target independent plotting interface.
static struct llcache_s * llcache
low level cache state
Definition: llcache.c:267
nsurl * llcache_handle_get_url(const llcache_handle *handle)
Retrieve the post-redirect URL of a low-level cache object.
Definition: llcache.c:4195
Low-level resource cache (interface)
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
char * messages_get_buff(const char *key,...)
Formatted message from a key in the global message hash.
Definition: messages.c:205
Localised message support (interface).
const char * nsurl_access_leaf(const nsurl *url)
Access a URL's path leaf as a string.
@ base
Definition: punycode.c:19
int ro_plot_origin_x
Definition: plotters.c:40
int ro_plot_origin_y
Definition: plotters.c:41
Interface to utility string handling.
Content operation function table.
nserror(* create)(const struct content_handler *handler, lwc_string *imime_type, const struct http_parameter *params, struct llcache_handle *llcache, const char *fallback_charset, bool quirks, struct content **c)
parameters to content redraw
Definition: content.h:40
int height
vertical dimension
Definition: content.h:48
int y
coordinate for top-left of redraw
Definition: content.h:42
int x
coordinate for top-left of redraw
Definition: content.h:41
int width
dimensions to render content at (for scaling contents with intrinsic dimensions)
Definition: content.h:47
Content which corresponds to a single URL.
bool quirks
Content is in quirks mode.
int height
Height dimension, if applicable.
char * fallback_charset
Fallback charset, or NULL.
int width
Width dimension, if applicable.
const struct content_handler * handler
Handler for content.
struct llcache_handle * llcache
Low-level cache object.
content_status status
Current status.
Representation of an HTTP parameter.
Definition: parameter.c:31
Handle to low-level cache object.
Definition: llcache.c:76
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
Extra data for some content_msg messages.
Definition: content.h:60
const char * title
Definition: content.h:186
void * ctx
context passed to browser_window_search()
Definition: content.h:277
Interface to a number of general purpose functionality.
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plot.c:357