NetSurf
nssprite.c
Go to the documentation of this file.
1 /*
2 * Copyright 2008 James Shaw <js102@zepler.net>
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/**
20 * \file
21 * librosprite implementation for content image/x-riscos-sprite
22 */
23
24#include <stdbool.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include <librosprite.h>
29
30#include "utils/utils.h"
31#include "utils/log.h"
32#include "utils/messages.h"
33#include "netsurf/plotters.h"
34#include "netsurf/bitmap.h"
35#include "netsurf/content.h"
36#include "content/llcache.h"
40#include "desktop/bitmap.h"
41
42#include "image/nssprite.h"
43
44typedef struct nssprite_content {
45 struct content base;
46 struct bitmap *bitmap; /**< Created NetSurf bitmap */
47
48 struct rosprite_area* sprite_area;
50
51
52#define ERRCHK(x) do { \
53 rosprite_error err = x; \
54 if (err == ROSPRITE_EOF) { \
55 NSLOG(netsurf, INFO, "Got ROSPRITE_EOF when loading sprite file"); \
56 goto ro_sprite_error; \
57 } else if (err == ROSPRITE_BADMODE) { \
58 NSLOG(netsurf, INFO, "Got ROSPRITE_BADMODE when loading sprite file"); \
59 goto ro_sprite_error; \
60 } else if (err == ROSPRITE_OK) { \
61 } else { \
62 goto ro_sprite_error; \
63 } \
64} while(0)
65
66
67
68
70 lwc_string *imime_type, const struct http_parameter *params,
71 struct llcache_handle *llcache, const char *fallback_charset,
72 bool quirks, struct content **c)
73{
74 nssprite_content *sprite;
75 nserror error;
76
77 sprite = calloc(1, sizeof(nssprite_content));
78 if (sprite == NULL)
79 return NSERROR_NOMEM;
80
81 error = content__init(&sprite->base, handler, imime_type, params,
82 llcache, fallback_charset, quirks);
83 if (error != NSERROR_OK) {
84 free(sprite);
85 return error;
86 }
87
88 *c = (struct content *) sprite;
89
90 return NSERROR_OK;
91}
92
93/**
94 * Convert a CONTENT_SPRITE for display.
95 *
96 * No conversion is necessary. We merely read the sprite dimensions.
97 */
98
99static bool nssprite_convert(struct content *c)
100{
101 nssprite_content *nssprite = (nssprite_content *) c;
102
103 struct rosprite_mem_context* ctx = NULL;
104
105 const uint8_t *data;
106 size_t size;
107 char *title;
108
109 data = content__get_source_data(c, &size);
110
111 ERRCHK(rosprite_create_mem_context((uint8_t *) data, size, &ctx));
112
113 struct rosprite_area* sprite_area;
114 ERRCHK(rosprite_load(rosprite_mem_reader, ctx, &sprite_area));
115 rosprite_destroy_mem_context(ctx);
116 nssprite->sprite_area = sprite_area;
117
118 assert(sprite_area->sprite_count > 0);
119
120 struct rosprite* sprite = sprite_area->sprites[0];
121
122 nssprite->bitmap = guit->bitmap->create(sprite->width, sprite->height, BITMAP_NONE);
123 if (!nssprite->bitmap) {
125 return false;
126 }
127 uint32_t* imagebuf = (uint32_t *)(void *)guit->bitmap->get_buffer(nssprite->bitmap);
128 if (!imagebuf) {
130 return false;
131 }
132 unsigned char *spritebuf = (unsigned char *)sprite->image;
133
134 memcpy(imagebuf, spritebuf, sprite->width * sprite->height * 4);
135
136 c->width = sprite->width;
137 c->height = sprite->height;
138
139 /* set title text */
140 title = messages_get_buff("SpriteTitle",
142 c->width, c->height);
143 if (title != NULL) {
144 content__set_title(c, title);
145 free(title);
146 }
147
149 .layout = BITMAP_LAYOUT_A8B8G8R8,
150 });
151 guit->bitmap->modified(nssprite->bitmap);
152
155 content_set_status(c, ""); /* Done: update status bar */
156
157 return true;
158
159ro_sprite_error:
160 if (ctx != NULL) {
161 rosprite_destroy_mem_context(ctx);
162 }
164
165 return false;
166}
167
168
169/**
170 * Destroy a CONTENT_SPRITE and free all resources it owns.
171 */
172
173static void nssprite_destroy(struct content *c)
174{
175 nssprite_content *nssprite = (nssprite_content *) c;
176
177 if (nssprite->sprite_area != NULL)
178 rosprite_destroy_sprite_area(nssprite->sprite_area);
179 if (nssprite->bitmap != NULL)
180 guit->bitmap->destroy(nssprite->bitmap);
181}
182
183
184/**
185 * Redraw a CONTENT_SPRITE.
186 */
187
188static bool
190 struct content_redraw_data *data,
191 const struct rect *clip,
192 const struct redraw_context *ctx)
193{
194 nssprite_content *nssprite = (nssprite_content *) c;
196
197 if (data->repeat_x) {
198 flags |= BITMAPF_REPEAT_X;
199 }
200 if (data->repeat_y) {
201 flags |= BITMAPF_REPEAT_Y;
202 }
203
204 return (ctx->plot->bitmap(ctx,
205 nssprite->bitmap,
206 data->x, data->y,
207 data->width, data->height,
208 data->background_colour,
209 flags) == NSERROR_OK);
210}
211
212
213static nserror nssprite_clone(const struct content *old, struct content **newc)
214{
215 nssprite_content *sprite;
216 nserror error;
217
218 sprite = calloc(1, sizeof(nssprite_content));
219 if (sprite == NULL)
220 return NSERROR_NOMEM;
221
222 error = content__clone(old, &sprite->base);
223 if (error != NSERROR_OK) {
224 content_destroy(&sprite->base);
225 return error;
226 }
227
228 /* Simply replay convert */
229 if (old->status == CONTENT_STATUS_READY ||
230 old->status == CONTENT_STATUS_DONE) {
231 if (nssprite_convert(&sprite->base) == false) {
232 content_destroy(&sprite->base);
234 }
235 }
236
237 *newc = (struct content *) sprite;
238
239 return NSERROR_OK;
240}
241
242static void *nssprite_get_internal(const struct content *c, void *context)
243{
244 nssprite_content *nssprite = (nssprite_content *) c;
245
246 return nssprite->bitmap;
247}
248
250{
251 return CONTENT_IMAGE;
252}
253
254
256{
257 nssprite_content *nssprite = (nssprite_content *) c;
258
259 if (nssprite->bitmap != NULL) {
260 return guit->bitmap->get_opaque(nssprite->bitmap);
261 }
262
263 return false;
264}
265
268 .data_complete = nssprite_convert,
269 .destroy = nssprite_destroy,
270 .redraw = nssprite_redraw,
271 .clone = nssprite_clone,
272 .get_internal = nssprite_get_internal,
273 .type = nssprite_content_type,
274 .is_opaque = nssprite_content_is_opaque,
275 .no_share = false,
276};
277
278static const char *nssprite_types[] = {
279 "image/x-riscos-sprite"
280};
281
void content_destroy(struct content *c)
Destroy and free a content.
Definition: content.c:354
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
void content_broadcast_error(struct content *c, nserror errorcode, const char *msg)
Send an error message to all users.
Definition: content.c:769
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
Internal core bitmap interface.
static void bitmap_format_to_client(void *bitmap, const bitmap_fmt_t *current_fmt)
Convert a bitmap to the client bitmap format.
Definition: bitmap.h:117
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_SPRITE_ERROR
A RISC OS Sprite error occurred.
Definition: errors.h:43
@ 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
struct netsurf_table * guit
The global interface table.
Definition: gui_factory.c:50
Interface to core interface table.
Generic bitmap handling interface.
@ BITMAP_NONE
Definition: bitmap.h:37
Public content interface.
Target independent plotting interface.
#define BITMAPF_REPEAT_X
Definition: plotters.h:38
#define BITMAPF_REPEAT_Y
Definition: plotters.h:39
unsigned long bitmap_flags_t
Definition: plotters.h:36
#define BITMAPF_NONE
Definition: plotters.h:37
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)
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).
static bool nssprite_convert(struct content *c)
Convert a CONTENT_SPRITE for display.
Definition: nssprite.c:99
static void * nssprite_get_internal(const struct content *c, void *context)
Definition: nssprite.c:242
static nserror nssprite_clone(const struct content *old, struct content **newc)
Definition: nssprite.c:213
static bool nssprite_redraw(struct content *c, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
Redraw a CONTENT_SPRITE.
Definition: nssprite.c:189
static void nssprite_destroy(struct content *c)
Destroy a CONTENT_SPRITE and free all resources it owns.
Definition: nssprite.c:173
static nserror nssprite_create(const 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)
Definition: nssprite.c:69
static const content_handler nssprite_content_handler
Definition: nssprite.c:266
CONTENT_FACTORY_REGISTER_TYPES(nssprite, nssprite_types, nssprite_content_handler)
static const char * nssprite_types[]
Definition: nssprite.c:278
static content_type nssprite_content_type(void)
Definition: nssprite.c:249
struct nssprite_content nssprite_content
#define ERRCHK(x)
Definition: nssprite.c:52
static bool nssprite_content_is_opaque(struct content *c)
Definition: nssprite.c:255
Content for image/x-riscos-sprite (librosprite interface).
const char * nsurl_access_leaf(const nsurl *url)
Access a URL's path leaf as a string.
Interface to utility string handling.
Bitmap format specifier.
Definition: bitmap.h:95
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
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
bool repeat_y
whether content is tiled in y direction
Definition: content.h:59
bool repeat_x
whether content is tiled in x direction
Definition: content.h:58
int y
coordinate for top-left of redraw
Definition: content.h:42
int x
coordinate for top-left of redraw
Definition: content.h:41
colour background_colour
The background colour.
Definition: content.h:51
int width
dimensions to render content at (for scaling contents with intrinsic dimensions)
Definition: content.h:47
Content which corresponds to a single URL.
int height
Height dimension, if applicable.
int width
Width dimension, if applicable.
struct llcache_handle * llcache
Low-level cache object.
struct textsearch_context * context
content_status status
Current status.
void(* destroy)(void *bitmap)
Destroy a bitmap.
Definition: bitmap.h:143
void *(* create)(int width, int height, enum gui_bitmap_flags flags)
Create a new bitmap.
Definition: bitmap.h:136
bool(* get_opaque)(void *bitmap)
Get the opacity of a bitmap.
Definition: bitmap.h:159
void(* modified)(void *bitmap)
Marks a bitmap as modified.
Definition: bitmap.h:200
unsigned char *(* get_buffer)(void *bitmap)
Get the image buffer from a bitmap.
Definition: bitmap.h:169
Representation of an HTTP parameter.
Definition: parameter.c:31
Handle to low-level cache object.
Definition: llcache.c:76
struct gui_bitmap_table * bitmap
Bitmap table.
Definition: gui_table.h:153
struct rosprite_area * sprite_area
Definition: nssprite.c:48
struct content base
Definition: nssprite.c:45
struct bitmap * bitmap
Created NetSurf bitmap.
Definition: nssprite.c:46
nserror(* bitmap)(const struct redraw_context *ctx, struct bitmap *bitmap, int x, int y, int width, int height, colour bg, bitmap_flags_t flags)
Plot a bitmap.
Definition: plotters.h:257
Rectangle coordinates.
Definition: types.h:40
Redraw context.
Definition: plotters.h:51
const struct plotter_table * plot
Current plot operation table.
Definition: plotters.h:73
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